Skip to content

Commit

Permalink
Fix writing SpotlightDrawer, tweak max block size
Browse files Browse the repository at this point in the history
  • Loading branch information
ihatecompvir committed Jan 6, 2025
1 parent 7813491 commit 9d2da76
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
8 changes: 5 additions & 3 deletions MiloLib/Assets/SpotlightDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
namespace MiloLib.Assets
{
[Name("SpotlightDrawer"), Description("")]
public class SpotlightDrawer : RndDrawable
public class SpotlightDrawer : Object
{
private ushort altRevision;
private ushort revision;

public RndDrawable draw = new();

[Name("Intensity"), Description("global intensity scale")]
public float intensity;
[Name("Color"), Description("color of ambient (unlit) fog")]
Expand Down Expand Up @@ -56,7 +58,7 @@ public SpotlightDrawer Read(EndianReader reader, bool standalone, DirectoryMeta

if (revision > 0)
{
base.Read(reader, false, parent, entry);
draw.Read(reader, false, parent, entry);
}
else
{
Expand Down Expand Up @@ -115,7 +117,7 @@ public override void Write(EndianWriter writer, bool standalone, DirectoryMeta p

if (revision > 0)
{
base.Write(writer, false, parent, entry);
draw.Write(writer, false, true);
}
else
{
Expand Down
21 changes: 15 additions & 6 deletions MiloLib/MiloFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MiloFile
/// The maximum size a block can be.
/// TODO: check if there is some "best" value for this
/// </summary>
private const int MAX_BLOCK_SIZE = 0xF00000;
private const int MAX_BLOCK_SIZE = 0xFFFFFF;

/// <summary>
/// The type of the Milo file. Determines if it's compressed or not and how it's compressed.
Expand Down Expand Up @@ -202,8 +202,6 @@ public MiloFile(string path)

meta = new DirectoryMeta();
meta.platform = DetectPlatform();
// write the decompressed data to a file for debugging
File.WriteAllBytes("decompressed.gz", compressedStream.ToArray());
dirMeta = meta.Read(decompressedReader);
break;
case Type.Uncompressed:
Expand Down Expand Up @@ -327,6 +325,7 @@ public void Save(string? path, Type? type, uint startingOffset = 0x810, Endian h
MemoryStream compressedStream;
EndianWriter compressedWriter;
List<byte[]> compressedBlocks;
List<int> uncompressedBlockSizes = new List<int>();

switch (type)
{
Expand All @@ -349,6 +348,8 @@ public void Save(string? path, Type? type, uint startingOffset = 0x810, Endian h
byte[] block = new byte[bytesRead];
Array.Copy(buffer, block, bytesRead);

uncompressedBlockSizes.Add(block.Length);

byte[] compressedBlock = null;
using (MemoryStream blockStream = new MemoryStream())
{
Expand Down Expand Up @@ -380,7 +381,8 @@ public void Save(string? path, Type? type, uint startingOffset = 0x810, Endian h
writer.SeekTo(0x8);
writer.Endianness = Endian.LittleEndian;
writer.WriteUInt32((uint)compressedBlocks.Count);
writer.WriteUInt32(MAX_BLOCK_SIZE);
uint maxUncompressedBlockSize = (uint)uncompressedBlockSizes.Max();
writer.WriteUInt32(maxUncompressedBlockSize);
foreach (var block in compressedBlocks)
{
if (type == Type.CompressedZlibAlt)
Expand Down Expand Up @@ -409,10 +411,17 @@ public void Save(string? path, Type? type, uint startingOffset = 0x810, Endian h
writer.WriteUInt32(MAX_BLOCK_SIZE);


// write block sizes, all the MAX_BLOCK_SIZE as if there are blocks
for (int i = 0; i < numBlocks; i++)
{
writer.WriteUInt32(MAX_BLOCK_SIZE);
if (i == numBlocks - 1)
{
uint remainingSize = (totalSize % MAX_BLOCK_SIZE) - startingOffset;
writer.WriteUInt32(remainingSize == 0 ? MAX_BLOCK_SIZE : remainingSize);
}
else
{
writer.WriteUInt32(MAX_BLOCK_SIZE);
}
}
break;
default:
Expand Down

0 comments on commit 9d2da76

Please sign in to comment.