Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into underanalyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
colinator27 committed Jul 21, 2024
2 parents 7a72203 + d4f6431 commit 59d958c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
13 changes: 11 additions & 2 deletions UndertaleModLib/UndertaleChunks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ private void CheckForEffectData(UndertaleReader reader)
case LayerType.Assets:
reader.Position += 6 * 4;
int tileOffset = reader.ReadInt32();
if (tileOffset != reader.AbsPosition + 8)
if (tileOffset != reader.AbsPosition + 8 && tileOffset != reader.AbsPosition + 12)
reader.undertaleData.SetGMS2Version(2022, 1);
break;
case LayerType.Tiles:
Expand Down Expand Up @@ -1459,6 +1459,8 @@ public class UndertaleChunkTXTR : UndertaleListChunk<UndertaleEmbeddedTexture>
public override string Name => "TXTR";

private static bool checkedFor2022_3;
private static bool checkedFor2_0_6;

private void CheckFor2022_3And5(UndertaleReader reader)
{
// Detect GM2022.3
Expand Down Expand Up @@ -1580,7 +1582,10 @@ private void CheckForGMS2_0_6(UndertaleReader reader)
{
bool atLeastGMS2_0 = reader.undertaleData.IsGameMaker2();
if (!atLeastGMS2_0 || reader.undertaleData.IsVersionAtLeast(2, 0, 6))
{
checkedFor2_0_6 = true;
return;
}

long returnPos = reader.Position;
bool noGeneratedMips = false;
Expand Down Expand Up @@ -1617,14 +1622,16 @@ private void CheckForGMS2_0_6(UndertaleReader reader)
reader.undertaleData.SetGMS2Version(2, 0, 6);

reader.Position = returnPos;
checkedFor2_0_6 = true;
}

internal override void UnserializeChunk(UndertaleReader reader)
{
if (!checkedFor2022_3)
CheckFor2022_3And5(reader);

CheckForGMS2_0_6(reader);
if (!checkedFor2_0_6)
CheckForGMS2_0_6(reader);

base.UnserializeChunk(reader);
reader.SwitchReaderType(false);
Expand All @@ -1648,8 +1655,10 @@ internal override void UnserializeChunk(UndertaleReader reader)
internal override uint UnserializeObjectCount(UndertaleReader reader)
{
checkedFor2022_3 = false;
checkedFor2_0_6 = false;

CheckFor2022_3And5(reader);
CheckForGMS2_0_6(reader);

// Texture blobs are already included in the count
return base.UnserializeObjectCount(reader);
Expand Down
38 changes: 13 additions & 25 deletions UndertaleModLib/Util/BufferBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ public int Read(byte[] buffer, int count)
n = count;
if (n <= 0)
{
#if DEBUG
throw new IOException("Reading out of chunk bounds");
#else
return 0;
#endif
}

if (n <= 8)
Expand All @@ -56,11 +52,7 @@ public int Read(Span<byte> buffer)
int n = Math.Min(_length - _position, buffer.Length);
if (n <= 0)
{
#if DEBUG
throw new IOException("Reading out of chunk bounds");
#else
return 0;
#endif
}

new Span<byte>(_buffer, _position, n).CopyTo(buffer);
Expand All @@ -74,11 +66,7 @@ public byte ReadByte()
int newPos = _position + 1;
if (newPos > _length)
{
#if DEBUG
throw new IOException("Reading out of chunk bounds");
#else
return 0;
#endif
}

_position = newPos;
Expand Down Expand Up @@ -186,10 +174,11 @@ public virtual bool ReadBoolean()

public string ReadChars(int count)
{
#if DEBUG
if (chunkBuffer.Position + count > _length)
{
throw new IOException("Reading out of chunk bounds");
#endif
}

if (count > 1024)
{
byte[] buf = new byte[count];
Expand All @@ -209,10 +198,11 @@ public string ReadChars(int count)

public byte[] ReadBytes(int count)
{
#if DEBUG
if (chunkBuffer.Position + count > _length)
{
throw new IOException("Reading out of chunk bounds");
#endif
}

byte[] val = new byte[count];
if (count > 0)
chunkBuffer.Read(val, count);
Expand Down Expand Up @@ -273,15 +263,14 @@ public ulong ReadUInt64()

public string ReadGMString()
{
#if DEBUG
if (chunkBuffer.Position + 5 > _length)
throw new IOException("Reading out of chunk bounds");
#endif

int length = BinaryPrimitives.ReadInt32LittleEndian(ReadToBuffer(4));
#if DEBUG

if (chunkBuffer.Position + length + 1 >= _length)
throw new IOException("Reading out of chunk bounds");
#endif

string res;
if (length > 1024)
{
Expand All @@ -296,13 +285,12 @@ public string ReadGMString()
chunkBuffer.Read(buf);
res = encoding.GetString(buf);
}

#if DEBUG

if (ReadByte() != 0)
{
throw new IOException("String not null terminated!");
#else
Position++;
#endif
}

return res;
}
public void SkipGMString()
Expand Down

0 comments on commit 59d958c

Please sign in to comment.