Skip to content

Commit

Permalink
Merge pull request #1838 from UnderminersTeam/fix-erroneous-2022-1-de…
Browse files Browse the repository at this point in the history
…tection

Fix erroneous 2022.1 detection
  • Loading branch information
colinator27 authored Jul 21, 2024
2 parents e5e0d5a + b6b29bf commit 0334778
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
2 changes: 1 addition & 1 deletion 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
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 0334778

Please sign in to comment.