Skip to content

Commit 003fb1e

Browse files
committed
Handle FONT padding size being potentially variable pre-12
1 parent 8172a1b commit 003fb1e

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

UndertaleModLib/UndertaleChunks.cs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,16 @@ internal override void UnserializeChunk(UndertaleReader reader)
674674
public class UndertaleChunkFONT : UndertaleListChunk<UndertaleFont>
675675
{
676676
public override string Name => "FONT";
677+
678+
/// <summary>
679+
/// Padding bytes at the end of the chunk.
680+
/// </summary>
681+
/// <remarks>
682+
/// Appeared at some point after bytecode version 6, according to the existing copy of GMS 1.0.98.
683+
/// Before bytecode version 12 (observed in bytecode 11), this contained unknown data of varying length.
684+
/// Afterwards, this contains UTF-16 codepoints representing ASCII's 0x00-0xFF, and is consistently
685+
/// 512 bytes long.
686+
/// </remarks>
677687
public byte[] Padding;
678688

679689
private static bool checkedFor2022_2;
@@ -795,14 +805,22 @@ internal override void SerializeChunk(UndertaleWriter writer)
795805
{
796806
base.SerializeChunk(writer);
797807

798-
if (Padding == null)
808+
if (writer.undertaleData.GeneralInfo.BytecodeVersion > 6)
799809
{
800-
for (ushort i = 0; i < 0x80; i++)
801-
writer.Write(i);
802-
for (ushort i = 0; i < 0x80; i++)
803-
writer.Write((ushort)0x3f);
804-
} else
805-
writer.Write(Padding);
810+
if (Padding == null)
811+
{
812+
// TODO: Before bytecode 12 the size of Padding is variable.
813+
// (though we don't properly support them in other places yet)
814+
for (ushort i = 0; i < 0x80; i++)
815+
writer.Write(i);
816+
for (ushort i = 0; i < 0x80; i++)
817+
writer.Write((ushort)0x3f);
818+
}
819+
else
820+
{
821+
writer.Write(Padding);
822+
}
823+
}
806824
}
807825

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

816834
base.UnserializeChunk(reader);
817835

818-
Padding = reader.ReadBytes(512);
836+
if (reader.undertaleData.GeneralInfo.BytecodeVersion > 6)
837+
{
838+
if (reader.undertaleData.GeneralInfo.BytecodeVersion >= 12)
839+
{
840+
Padding = reader.ReadBytes(512);
841+
}
842+
else
843+
{
844+
// FIXME: how did GMAC the calculate the size of padding?
845+
int remainingBytes = (int)(Length - (reader.Position - 8));
846+
Padding = reader.ReadBytes(remainingBytes);
847+
}
848+
}
819849
}
820850

821851
internal override uint UnserializeObjectCount(UndertaleReader reader)

0 commit comments

Comments
 (0)