Skip to content

Commit

Permalink
Handle FONT padding size being potentially variable pre-12
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobby233Liu committed Jan 21, 2025
1 parent 8172a1b commit 003fb1e
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions UndertaleModLib/UndertaleChunks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,16 @@ internal override void UnserializeChunk(UndertaleReader reader)
public class UndertaleChunkFONT : UndertaleListChunk<UndertaleFont>
{
public override string Name => "FONT";

/// <summary>
/// Padding bytes at the end of the chunk.
/// </summary>
/// <remarks>
/// Appeared at some point after bytecode version 6, according to the existing copy of GMS 1.0.98.
/// Before bytecode version 12 (observed in bytecode 11), this contained unknown data of varying length.
/// Afterwards, this contains UTF-16 codepoints representing ASCII's 0x00-0xFF, and is consistently
/// 512 bytes long.
/// </remarks>
public byte[] Padding;

private static bool checkedFor2022_2;
Expand Down Expand Up @@ -795,14 +805,22 @@ internal override void SerializeChunk(UndertaleWriter writer)
{
base.SerializeChunk(writer);

if (Padding == null)
if (writer.undertaleData.GeneralInfo.BytecodeVersion > 6)
{
for (ushort i = 0; i < 0x80; i++)
writer.Write(i);
for (ushort i = 0; i < 0x80; i++)
writer.Write((ushort)0x3f);
} else
writer.Write(Padding);
if (Padding == null)
{
// TODO: Before bytecode 12 the size of Padding is variable.
// (though we don't properly support them in other places yet)
for (ushort i = 0; i < 0x80; i++)
writer.Write(i);
for (ushort i = 0; i < 0x80; i++)
writer.Write((ushort)0x3f);
}
else
{
writer.Write(Padding);
}
}
}

internal override void UnserializeChunk(UndertaleReader reader)
Expand All @@ -815,7 +833,19 @@ internal override void UnserializeChunk(UndertaleReader reader)

base.UnserializeChunk(reader);

Padding = reader.ReadBytes(512);
if (reader.undertaleData.GeneralInfo.BytecodeVersion > 6)
{
if (reader.undertaleData.GeneralInfo.BytecodeVersion >= 12)
{
Padding = reader.ReadBytes(512);
}
else
{
// FIXME: how did GMAC the calculate the size of padding?
int remainingBytes = (int)(Length - (reader.Position - 8));
Padding = reader.ReadBytes(remainingBytes);
}
}
}

internal override uint UnserializeObjectCount(UndertaleReader reader)
Expand Down

0 comments on commit 003fb1e

Please sign in to comment.