Skip to content

Commit

Permalink
Merge pull request #1884 from UnderminersTeam/remove-extra-debug-checks
Browse files Browse the repository at this point in the history
Remove extra debug checks
  • Loading branch information
Miepee authored Aug 23, 2024
2 parents 320d789 + d889e5e commit 4e3f420
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
2 changes: 0 additions & 2 deletions UndertaleModLib/Util/AdaptiveBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ public long AbsPosition
{
if (isUsingBufferReader)
{
#if DEBUG
if (value > Length)
throw new IOException("Reading out of bounds.");
#endif
bufferBinaryReader.Position = value - bufferBinaryReader.ChunkStartPosition + 8;
}
else
Expand Down
84 changes: 47 additions & 37 deletions UndertaleModLib/Util/FileBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ public long Position
get => Stream.Position;
set
{
#if DEBUG
if (value > Length)
throw new IOException("Reading out of bounds.");
#endif
Stream.Position = value;
}
}
Expand All @@ -49,10 +47,11 @@ private ReadOnlySpan<byte> ReadToBuffer(int count)

public byte ReadByte()
{
#if DEBUG
if (Stream.Position + 1 > _length)
{
throw new IOException("Reading out of bounds");
#endif
}

return (byte)Stream.ReadByte();
}

Expand All @@ -63,10 +62,11 @@ public virtual bool ReadBoolean()

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

if (count > 1024)
{
byte[] buf = new byte[count];
Expand All @@ -85,118 +85,128 @@ public string ReadChars(int count)

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

byte[] val = new byte[count];
Stream.Read(val, 0, count);
return val;
}

public short ReadInt16()
{
#if DEBUG
if (Stream.Position + 2 > _length)
{
throw new IOException("Reading out of bounds");
#endif
}

return BinaryPrimitives.ReadInt16LittleEndian(ReadToBuffer(2));
}

public ushort ReadUInt16()
{
#if DEBUG
if (Stream.Position + 2 > _length)
{
throw new IOException("Reading out of bounds");
#endif
}

return BinaryPrimitives.ReadUInt16LittleEndian(ReadToBuffer(2));
}

public int ReadInt24()
{
#if DEBUG
if (Stream.Position + 3 > _length)
{
throw new IOException("Reading out of bounds");
#endif
}

ReadToBuffer(3);
return buffer[0] | buffer[1] << 8 | (sbyte)buffer[2] << 16;
}

public uint ReadUInt24()
{
#if DEBUG
if (Stream.Position + 3 > _length)
{
throw new IOException("Reading out of bounds");
#endif
}

ReadToBuffer(3);
return (uint)(buffer[0] | buffer[1] << 8 | buffer[2] << 16);
}

public int ReadInt32()
{
#if DEBUG
if (Stream.Position + 4 > _length)
{
throw new IOException("Reading out of bounds");
#endif
}

return BinaryPrimitives.ReadInt32LittleEndian(ReadToBuffer(4));
}

public uint ReadUInt32()
{
#if DEBUG
if (Stream.Position + 4 > _length)
{
throw new IOException("Reading out of bounds");
#endif
}

return BinaryPrimitives.ReadUInt32LittleEndian(ReadToBuffer(4));
}

public float ReadSingle()
{
#if DEBUG
if (Stream.Position + 4 > _length)
{
throw new IOException("Reading out of bounds");
#endif
}

return BitConverter.Int32BitsToSingle(BinaryPrimitives.ReadInt32LittleEndian(ReadToBuffer(4)));
}

public double ReadDouble()
{
#if DEBUG
if (Stream.Position + 8 > _length)
{
throw new IOException("Reading out of bounds");
#endif
}

return BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64LittleEndian(ReadToBuffer(8)));
}

public long ReadInt64()
{
#if DEBUG
if (Stream.Position + 8 > _length)
{
throw new IOException("Reading out of bounds");
#endif
}

return BinaryPrimitives.ReadInt64LittleEndian(ReadToBuffer(8));
}

public ulong ReadUInt64()
{
#if DEBUG
if (Stream.Position + 8 > _length)
{
throw new IOException("Reading out of bounds");
#endif
}

return BinaryPrimitives.ReadUInt64LittleEndian(ReadToBuffer(8));
}

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

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

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

string res;
if (length > 1024)
{
Expand All @@ -210,15 +220,15 @@ public string ReadGMString()
Stream.Read(buf);
res = encoding.GetString(buf);
}

#if DEBUG

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

return res;
}

public void SkipGMString()
{
int length = BinaryPrimitives.ReadInt32LittleEndian(ReadToBuffer(4));
Expand Down

0 comments on commit 4e3f420

Please sign in to comment.