-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some serialization cleanup, optimization, fixes #1864
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,8 @@ public long Position | |
get => Stream.Position; | ||
set | ||
{ | ||
#if DEBUG | ||
if (value > Length) | ||
if (value < 0 || value > Length) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, can be changed into a pattern |
||
throw new IOException("Reading out of bounds."); | ||
#endif | ||
Stream.Position = value; | ||
} | ||
} | ||
|
@@ -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(); | ||
} | ||
|
||
|
@@ -63,10 +62,15 @@ public virtual bool ReadBoolean() | |
|
||
public string ReadChars(int count) | ||
{ | ||
#if DEBUG | ||
if (count < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(count)); | ||
} | ||
if (Stream.Position + count > _length) | ||
{ | ||
throw new IOException("Reading out of bounds"); | ||
#endif | ||
} | ||
|
||
if (count > 1024) | ||
{ | ||
byte[] buf = new byte[count]; | ||
|
@@ -85,118 +89,134 @@ public string ReadChars(int count) | |
|
||
public byte[] ReadBytes(int count) | ||
{ | ||
#if DEBUG | ||
if (count < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(count)); | ||
} | ||
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 (length < 0) | ||
throw new IOException("Invalid string length"); | ||
if (Stream.Position + length + 1 >= _length) | ||
throw new IOException("Reading out of bounds"); | ||
#endif | ||
|
||
string res; | ||
if (length > 1024) | ||
{ | ||
|
@@ -210,18 +230,20 @@ 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)); | ||
if (length < 0) | ||
throw new IOException("Invalid string length"); | ||
Position += (uint)length + 1; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above