From 718aea2d19bb2715082411f99ccadb34c2098baa Mon Sep 17 00:00:00 2001 From: Derek Gooding Date: Mon, 30 Sep 2024 14:31:09 -0400 Subject: [PATCH 01/13] summary information for unmanagedDataReader --- .../Serialization/UnmanagedDataReader.cs | 121 +++++++++++++----- 1 file changed, 90 insertions(+), 31 deletions(-) diff --git a/Projects/Server/Serialization/UnmanagedDataReader.cs b/Projects/Server/Serialization/UnmanagedDataReader.cs index ed9675abb..61f1dc0bc 100644 --- a/Projects/Server/Serialization/UnmanagedDataReader.cs +++ b/Projects/Server/Serialization/UnmanagedDataReader.cs @@ -25,18 +25,20 @@ namespace Server; +/// +/// Read bits of data raw from a serialized file +/// public unsafe class UnmanagedDataReader : IGenericReader { private static readonly ILogger logger = LogFactory.GetLogger(typeof(UnmanagedDataReader)); private readonly byte* _ptr; - private long _position; private readonly long _size; private readonly Dictionary _typesDb; private readonly Encoding _encoding; - public long Position => _position; + public long Position { get; private set; } public UnmanagedDataReader(byte* ptr, long size, Dictionary typesDb = null, Encoding encoding = null) { @@ -46,9 +48,20 @@ public UnmanagedDataReader(byte* ptr, long size, Dictionary types _size = size; } + /// + /// Reads the next bit as a bool. If that bit is true, return a ReadStringRaw(intern) else null + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public string ReadString(bool intern = false) => ReadBool() ? ReadStringRaw(intern) : null; + + /// + /// Reads the next bit as a bool. If that bit is true, return a ReadStringRaw(intern) else null + /// + /// + /// public string ReadStringRaw(bool intern = false) { // ReadEncodedInt @@ -57,7 +70,7 @@ public string ReadStringRaw(bool intern = false) do { - b = *(_ptr + _position++); + b = *(_ptr + Position++); length |= (b & 0x7F) << shift; shift += 7; } @@ -68,84 +81,130 @@ public string ReadStringRaw(bool intern = false) return "".Intern(); } - var str = TextEncoding.GetString(new ReadOnlySpan(_ptr + _position, length), _encoding); - _position += length; + var str = TextEncoding.GetString(new ReadOnlySpan(_ptr + Position, length), _encoding); + Position += length; return intern ? str.Intern() : str; } + /// + /// Read the next 64 bits to make up a long (int64) + /// + /// Next long value [MethodImpl(MethodImplOptions.AggressiveInlining)] public long ReadLong() { - var v = BinaryPrimitives.ReadInt64LittleEndian(new ReadOnlySpan(_ptr + _position, sizeof(long))); - _position += sizeof(long); + var v = BinaryPrimitives.ReadInt64LittleEndian(new ReadOnlySpan(_ptr + Position, sizeof(long))); + Position += sizeof(long); return v; } + /// + /// Read the next 64 bits to make up an unsigned long (uint64) + /// + /// Next ulong value [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong ReadULong() { - var v = BinaryPrimitives.ReadUInt64LittleEndian(new ReadOnlySpan(_ptr + _position, sizeof(ulong))); - _position += sizeof(ulong); + var v = BinaryPrimitives.ReadUInt64LittleEndian(new ReadOnlySpan(_ptr + Position, sizeof(ulong))); + Position += sizeof(ulong); return v; } + /// + /// Read the next 32 bits to make up an int (int32) + /// + /// Next int value [MethodImpl(MethodImplOptions.AggressiveInlining)] public int ReadInt() { - var v = BinaryPrimitives.ReadInt32LittleEndian(new ReadOnlySpan(_ptr + _position, sizeof(int))); - _position += sizeof(int); + var v = BinaryPrimitives.ReadInt32LittleEndian(new ReadOnlySpan(_ptr + Position, sizeof(int))); + Position += sizeof(int); return v; } + /// + /// Read the next 32 bits to make up an unsigned int (uint32) + /// + /// Next uint value [MethodImpl(MethodImplOptions.AggressiveInlining)] public uint ReadUInt() { - var v = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan(_ptr + _position, sizeof(uint))); - _position += sizeof(uint); + var v = BinaryPrimitives.ReadUInt32LittleEndian(new ReadOnlySpan(_ptr + Position, sizeof(uint))); + Position += sizeof(uint); return v; } + /// + /// Read the next 16 bits to make up a short (int16) + /// + /// Next short value [MethodImpl(MethodImplOptions.AggressiveInlining)] public short ReadShort() { - var v = BinaryPrimitives.ReadInt16LittleEndian(new ReadOnlySpan(_ptr + _position, sizeof(short))); - _position += sizeof(short); + var v = BinaryPrimitives.ReadInt16LittleEndian(new ReadOnlySpan(_ptr + Position, sizeof(short))); + Position += sizeof(short); return v; } + /// + /// Read the next 16 bits to make up an unsigned short (int16) + /// + /// Next ushort value [MethodImpl(MethodImplOptions.AggressiveInlining)] public ushort ReadUShort() { - var v = BinaryPrimitives.ReadUInt16LittleEndian(new ReadOnlySpan(_ptr + _position, sizeof(ushort))); - _position += sizeof(ushort); + var v = BinaryPrimitives.ReadUInt16LittleEndian(new ReadOnlySpan(_ptr + Position, sizeof(ushort))); + Position += sizeof(ushort); return v; } + /// + /// Read the next 8 bits to make up a float point double + /// + /// Next double value [MethodImpl(MethodImplOptions.AggressiveInlining)] public double ReadDouble() { - var v = BinaryPrimitives.ReadDoubleLittleEndian(new ReadOnlySpan(_ptr + _position, sizeof(double))); - _position += sizeof(double); + var v = BinaryPrimitives.ReadDoubleLittleEndian(new ReadOnlySpan(_ptr + Position, sizeof(double))); + Position += sizeof(double); return v; } + /// + /// Read the next 4 bits to make up a float point + /// + /// Next float value [MethodImpl(MethodImplOptions.AggressiveInlining)] public float ReadFloat() { - var v = BinaryPrimitives.ReadSingleLittleEndian(new ReadOnlySpan(_ptr + _position, sizeof(float))); - _position += sizeof(float); + var v = BinaryPrimitives.ReadSingleLittleEndian(new ReadOnlySpan(_ptr + Position, sizeof(float))); + Position += sizeof(float); return v; } + /// + /// Read the next 8 bits to make up a byte + /// + /// Next byte value [MethodImpl(MethodImplOptions.AggressiveInlining)] - public byte ReadByte() => *(_ptr + _position++); - + public byte ReadByte() => *(_ptr + Position++); + /// + /// Read the next 8 bits to make up a signed byte + /// + /// Next sbyte value [MethodImpl(MethodImplOptions.AggressiveInlining)] public sbyte ReadSByte() => (sbyte)ReadByte(); - + /// + /// Read the next 1 bit to make up a boolean + /// + /// Next bool value [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool ReadBool() => ReadByte() != 0; + /// + /// Read the next 32 bytes to make up a Serial + /// + /// Next uint value cast as a Serial struct [MethodImpl(MethodImplOptions.AggressiveInlining)] public Serial ReadSerial() => (Serial)ReadUInt(); @@ -208,13 +267,13 @@ public Type ReadTypeByHash() public int Read(Span buffer) { var length = buffer.Length; - if (length > _size - _position) + if (length > _size - Position) { throw new OutOfMemoryException(); } - new ReadOnlySpan(_ptr + _position, length).CopyTo(buffer); - _position += length; + new ReadOnlySpan(_ptr + Position, length).CopyTo(buffer); + Position += length; return length; } @@ -229,18 +288,18 @@ public virtual long Seek(long offset, SeekOrigin origin) "Attempting to seek to an invalid position using SeekOrigin.Begin" ); Debug.Assert( - origin != SeekOrigin.Current || _position + offset >= 0 && _position + offset < _size, + origin != SeekOrigin.Current || Position + offset >= 0 && Position + offset < _size, "Attempting to seek to an invalid position using SeekOrigin.Current" ); var position = Math.Max(0L, origin switch { - SeekOrigin.Current => _position + offset, + SeekOrigin.Current => Position + offset, SeekOrigin.End => _size + offset, _ => offset // Begin }); - _position = position; - return _position; + Position = position; + return Position; } } From a2f3c8b647439aa52940310e031ad9ddb66be8a1 Mon Sep 17 00:00:00 2001 From: Derek Gooding Date: Mon, 30 Sep 2024 14:36:18 -0400 Subject: [PATCH 02/13] Make sertain all summary comments end in a period --- .../Server/Serialization/BinaryFileReader.cs | 4 ++ .../Serialization/UnmanagedDataReader.cs | 64 +++++++++---------- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/Projects/Server/Serialization/BinaryFileReader.cs b/Projects/Server/Serialization/BinaryFileReader.cs index 7a6ac2fde..941c709ec 100644 --- a/Projects/Server/Serialization/BinaryFileReader.cs +++ b/Projects/Server/Serialization/BinaryFileReader.cs @@ -21,6 +21,9 @@ namespace Server; +/// +/// Read bits of data from a serialized file in a managed environment. +/// public sealed unsafe class BinaryFileReader : IDisposable, IGenericReader { private readonly bool _usePrefixes; @@ -56,6 +59,7 @@ public void Dispose() _mmf?.Dispose(); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public string ReadString(bool intern = false) => _usePrefixes ? _reader.ReadString(intern) : _reader.ReadStringRaw(intern); diff --git a/Projects/Server/Serialization/UnmanagedDataReader.cs b/Projects/Server/Serialization/UnmanagedDataReader.cs index 61f1dc0bc..a1567afb9 100644 --- a/Projects/Server/Serialization/UnmanagedDataReader.cs +++ b/Projects/Server/Serialization/UnmanagedDataReader.cs @@ -26,7 +26,7 @@ namespace Server; /// -/// Read bits of data raw from a serialized file +/// Read bits of data raw from a serialized file using Little-endian. /// public unsafe class UnmanagedDataReader : IGenericReader { @@ -49,7 +49,7 @@ public UnmanagedDataReader(byte* ptr, long size, Dictionary types } /// - /// Reads the next bit as a bool. If that bit is true, return a ReadStringRaw(intern) else null + /// Reads the next bit as a bool. If that bit is true, return a ReadStringRaw(intern) else null. /// /// /// @@ -58,7 +58,7 @@ public UnmanagedDataReader(byte* ptr, long size, Dictionary types /// - /// Reads the next bit as a bool. If that bit is true, return a ReadStringRaw(intern) else null + /// Reads the next bit as a bool. If that bit is true, return a ReadStringRaw(intern) else null. /// /// /// @@ -87,9 +87,9 @@ public string ReadStringRaw(bool intern = false) } /// - /// Read the next 64 bits to make up a long (int64) + /// Read the next 64 bits to make up a long (int64). /// - /// Next long value + /// Next long value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public long ReadLong() { @@ -99,9 +99,9 @@ public long ReadLong() } /// - /// Read the next 64 bits to make up an unsigned long (uint64) + /// Read the next 64 bits to make up an unsigned long (uint64). /// - /// Next ulong value + /// Next ulong value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong ReadULong() { @@ -111,9 +111,9 @@ public ulong ReadULong() } /// - /// Read the next 32 bits to make up an int (int32) + /// Read the next 32 bits to make up an int (int32). /// - /// Next int value + /// Next int value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public int ReadInt() { @@ -123,9 +123,9 @@ public int ReadInt() } /// - /// Read the next 32 bits to make up an unsigned int (uint32) + /// Read the next 32 bits to make up an unsigned int (uint32). /// - /// Next uint value + /// Next uint value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public uint ReadUInt() { @@ -135,9 +135,9 @@ public uint ReadUInt() } /// - /// Read the next 16 bits to make up a short (int16) + /// Read the next 16 bits to make up a short (int16). /// - /// Next short value + /// Next short value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public short ReadShort() { @@ -147,9 +147,9 @@ public short ReadShort() } /// - /// Read the next 16 bits to make up an unsigned short (int16) + /// Read the next 16 bits to make up an unsigned short (int16). /// - /// Next ushort value + /// Next ushort value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public ushort ReadUShort() { @@ -159,9 +159,9 @@ public ushort ReadUShort() } /// - /// Read the next 8 bits to make up a float point double + /// Read the next 8 bits to make up a float point double. /// - /// Next double value + /// Next double value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public double ReadDouble() { @@ -171,9 +171,9 @@ public double ReadDouble() } /// - /// Read the next 4 bits to make up a float point + /// Read the next 4 bits to make up a float point. /// - /// Next float value + /// Next float value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public float ReadFloat() { @@ -183,28 +183,28 @@ public float ReadFloat() } /// - /// Read the next 8 bits to make up a byte + /// Read the next 8 bits to make up a byte. /// - /// Next byte value + /// Next byte value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public byte ReadByte() => *(_ptr + Position++); /// - /// Read the next 8 bits to make up a signed byte + /// Read the next 8 bits to make up a signed byte. /// - /// Next sbyte value + /// Next sbyte value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public sbyte ReadSByte() => (sbyte)ReadByte(); /// - /// Read the next 1 bit to make up a boolean + /// Read the next 1 bit to make up a boolean. /// - /// Next bool value + /// Next bool value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool ReadBool() => ReadByte() != 0; /// - /// Read the next 32 bytes to make up a Serial + /// Read the next 32 bytes to make up a Serial. /// - /// Next uint value cast as a Serial struct + /// Next uint value cast as a Serial struct. [MethodImpl(MethodImplOptions.AggressiveInlining)] public Serial ReadSerial() => (Serial)ReadUInt(); @@ -280,26 +280,24 @@ public int Read(Span buffer) public virtual long Seek(long offset, SeekOrigin origin) { Debug.Assert( - origin != SeekOrigin.End || offset <= 0 && offset > _size, + origin != SeekOrigin.End || (offset <= 0 && offset > _size), "Attempting to seek to an invalid position using SeekOrigin.End" ); Debug.Assert( - origin != SeekOrigin.Begin || offset >= 0 && offset < _size, + origin != SeekOrigin.Begin || (offset >= 0 && offset < _size), "Attempting to seek to an invalid position using SeekOrigin.Begin" ); Debug.Assert( - origin != SeekOrigin.Current || Position + offset >= 0 && Position + offset < _size, + origin != SeekOrigin.Current || (Position + offset >= 0 && Position + offset < _size), "Attempting to seek to an invalid position using SeekOrigin.Current" ); - var position = Math.Max(0L, origin switch + Position = Math.Max(0L, origin switch { SeekOrigin.Current => Position + offset, SeekOrigin.End => _size + offset, _ => offset // Begin }); - - Position = position; return Position; } } From 180d99c646e275cdc4416e8a200b407325fdc70d Mon Sep 17 00:00:00 2001 From: Derek Gooding Date: Mon, 30 Sep 2024 14:41:21 -0400 Subject: [PATCH 03/13] Copy summary comments to BinaryFileReader --- .../Server/Serialization/BinaryFileReader.cs | 70 +++++++++++++++---- .../Serialization/UnmanagedDataReader.cs | 7 +- 2 files changed, 59 insertions(+), 18 deletions(-) diff --git a/Projects/Server/Serialization/BinaryFileReader.cs b/Projects/Server/Serialization/BinaryFileReader.cs index 941c709ec..5f7498822 100644 --- a/Projects/Server/Serialization/BinaryFileReader.cs +++ b/Projects/Server/Serialization/BinaryFileReader.cs @@ -59,46 +59,88 @@ public void Dispose() _mmf?.Dispose(); } - + /// + /// If usePrefixes is true, return a ReadString(intern) else ReadStringRaw(intern). + /// + /// A string value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public string ReadString(bool intern = false) => _usePrefixes ? _reader.ReadString(intern) : _reader.ReadStringRaw(intern); - + /// + /// Returns the next set of bits that make up a string. + /// + /// Next string value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public string ReadStringRaw(bool intern = false) => _reader.ReadStringRaw(intern); - + /// + /// Read the next 64 bits to make up a long (int64). + /// + /// Next long value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public long ReadLong() => _reader.ReadLong(); - + /// + /// Read the next 64 bits to make up an unsigned long (uint64). + /// + /// Next ulong value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong ReadULong() => _reader.ReadULong(); - + /// + /// Read the next 32 bits to make up an int (int32). + /// + /// Next int value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public int ReadInt() => _reader.ReadInt(); - + /// + /// Read the next 32 bits to make up an unsigned int (uint32). + /// + /// Next uint value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public uint ReadUInt() => _reader.ReadUInt(); - + /// + /// Read the next 16 bits to make up a short (int16). + /// + /// Next short value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public short ReadShort() => _reader.ReadShort(); - + /// + /// Read the next 16 bits to make up an unsigned short (int16). + /// + /// Next ushort value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public ushort ReadUShort() => _reader.ReadUShort(); - + /// + /// Read the next 8 bits to make up a float point double. + /// + /// Next double value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public double ReadDouble() => _reader.ReadDouble(); - + /// + /// Read the next 4 bits to make up a float point. + /// + /// Next float value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public float ReadFloat() => _reader.ReadFloat(); - + /// + /// Read the next 8 bits to make up a byte. + /// + /// Next byte value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public byte ReadByte() => _reader.ReadByte(); - + /// + /// Read the next 8 bits to make up a signed byte. + /// + /// Next sbyte value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public sbyte ReadSByte() => _reader.ReadSByte(); - + /// + /// Read the next 1 bit to make up a boolean. + /// + /// Next bool value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool ReadBool() => _reader.ReadBool(); - + /// + /// Read the next 32 bytes to make up a Serial. + /// + /// Next uint value cast as a Serial struct. [MethodImpl(MethodImplOptions.AggressiveInlining)] public Serial ReadSerial() => _reader.ReadSerial(); diff --git a/Projects/Server/Serialization/UnmanagedDataReader.cs b/Projects/Server/Serialization/UnmanagedDataReader.cs index a1567afb9..b06ed41ce 100644 --- a/Projects/Server/Serialization/UnmanagedDataReader.cs +++ b/Projects/Server/Serialization/UnmanagedDataReader.cs @@ -52,16 +52,15 @@ public UnmanagedDataReader(byte* ptr, long size, Dictionary types /// Reads the next bit as a bool. If that bit is true, return a ReadStringRaw(intern) else null. /// /// - /// + /// Next string value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public string ReadString(bool intern = false) => ReadBool() ? ReadStringRaw(intern) : null; - /// - /// Reads the next bit as a bool. If that bit is true, return a ReadStringRaw(intern) else null. + /// Returns the next set of bits that make up a string. /// /// - /// + /// Next string value. public string ReadStringRaw(bool intern = false) { // ReadEncodedInt From 37571fa72a7c78f289744e9fa81f459671b77968 Mon Sep 17 00:00:00 2001 From: Derek Gooding Date: Mon, 30 Sep 2024 14:48:46 -0400 Subject: [PATCH 04/13] Add param definitions for BinaryFileReader --- Projects/Server/Serialization/BinaryFileReader.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Projects/Server/Serialization/BinaryFileReader.cs b/Projects/Server/Serialization/BinaryFileReader.cs index 5f7498822..2d40ee254 100644 --- a/Projects/Server/Serialization/BinaryFileReader.cs +++ b/Projects/Server/Serialization/BinaryFileReader.cs @@ -31,6 +31,13 @@ public sealed unsafe class BinaryFileReader : IDisposable, IGenericReader private readonly MemoryMappedViewStream _accessor; private readonly UnmanagedDataReader _reader; + /// + /// Read bits of data from a serialized file in a managed environment. + ///
Encoding is TextEncoding.UTF8 by default.
+ ///
+ /// Full file path of the file to be deserialized. + /// Sets if strings should be read with itern. + /// Set an encoding. By default TextEncoding.UTF8 public BinaryFileReader(string path, bool usePrefixes = true, Encoding encoding = null) { _usePrefixes = usePrefixes; From e7aaa6b43ae7dbf44f038dcb96ca02a1e5b427ed Mon Sep 17 00:00:00 2001 From: Derek Gooding Date: Mon, 30 Sep 2024 14:52:49 -0400 Subject: [PATCH 05/13] UnmanagedDataReader param descriptions --- Projects/Server/Serialization/UnmanagedDataReader.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Projects/Server/Serialization/UnmanagedDataReader.cs b/Projects/Server/Serialization/UnmanagedDataReader.cs index b06ed41ce..477545aeb 100644 --- a/Projects/Server/Serialization/UnmanagedDataReader.cs +++ b/Projects/Server/Serialization/UnmanagedDataReader.cs @@ -40,6 +40,13 @@ public unsafe class UnmanagedDataReader : IGenericReader public long Position { get; private set; } + /// + /// Read bits of data raw from a serialized file using Little-endian. + /// + /// The starting address for reading bits. + /// The total size of memory to be read. + /// The custom type dictionary. Will throw an error if left null ReadType is called. + /// UTF8 by default. public UnmanagedDataReader(byte* ptr, long size, Dictionary typesDb = null, Encoding encoding = null) { _encoding = encoding ?? TextEncoding.UTF8; From ace30b01179e92ae17964ff3a46e674996267131 Mon Sep 17 00:00:00 2001 From: Derek Gooding Date: Tue, 1 Oct 2024 08:14:43 -0400 Subject: [PATCH 06/13] more robust reference types --- .../Server/Serialization/BinaryFileReader.cs | 20 +++++++++++++++---- .../Serialization/UnmanagedDataReader.cs | 8 ++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Projects/Server/Serialization/BinaryFileReader.cs b/Projects/Server/Serialization/BinaryFileReader.cs index 2d40ee254..731ae2af6 100644 --- a/Projects/Server/Serialization/BinaryFileReader.cs +++ b/Projects/Server/Serialization/BinaryFileReader.cs @@ -23,6 +23,12 @@ namespace Server; /// /// Read bits of data from a serialized file in a managed environment. +/// +/// Uses the following components collectively: +///

+///

+///

+///
///
public sealed unsafe class BinaryFileReader : IDisposable, IGenericReader { @@ -33,11 +39,17 @@ public sealed unsafe class BinaryFileReader : IDisposable, IGenericReader /// /// Read bits of data from a serialized file in a managed environment. - ///
Encoding is TextEncoding.UTF8 by default.
+ ///
Encoding is UTF8 if left null.
+ /// + /// Uses the following components collectively: + ///

+ ///

+ ///

+ ///
///
/// Full file path of the file to be deserialized. /// Sets if strings should be read with itern. - /// Set an encoding. By default TextEncoding.UTF8 + /// Set an encoding. By default UTF8 public BinaryFileReader(string path, bool usePrefixes = true, Encoding encoding = null) { _usePrefixes = usePrefixes; @@ -145,9 +157,9 @@ public void Dispose() [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool ReadBool() => _reader.ReadBool(); /// - /// Read the next 32 bytes to make up a Serial. + /// Read the next 32 bytes to make up a . /// - /// Next uint value cast as a Serial struct. + /// Next uint value cast as a struct. [MethodImpl(MethodImplOptions.AggressiveInlining)] public Serial ReadSerial() => _reader.ReadSerial(); diff --git a/Projects/Server/Serialization/UnmanagedDataReader.cs b/Projects/Server/Serialization/UnmanagedDataReader.cs index 477545aeb..fe7b0cfc0 100644 --- a/Projects/Server/Serialization/UnmanagedDataReader.cs +++ b/Projects/Server/Serialization/UnmanagedDataReader.cs @@ -46,7 +46,7 @@ public unsafe class UnmanagedDataReader : IGenericReader /// The starting address for reading bits. /// The total size of memory to be read. /// The custom type dictionary. Will throw an error if left null ReadType is called. - /// UTF8 by default. + /// by default. public UnmanagedDataReader(byte* ptr, long size, Dictionary typesDb = null, Encoding encoding = null) { _encoding = encoding ?? TextEncoding.UTF8; @@ -56,7 +56,7 @@ public UnmanagedDataReader(byte* ptr, long size, Dictionary types } /// - /// Reads the next bit as a bool. If that bit is true, return a ReadStringRaw(intern) else null. + /// Reads the next bit as a bool. If that bit is true, return an else null. /// /// /// Next string value. @@ -208,9 +208,9 @@ public float ReadFloat() public bool ReadBool() => ReadByte() != 0; /// - /// Read the next 32 bytes to make up a Serial. + /// Read the next 32 bytes to make up a . /// - /// Next uint value cast as a Serial struct. + /// Next uint value cast as a struct. [MethodImpl(MethodImplOptions.AggressiveInlining)] public Serial ReadSerial() => (Serial)ReadUInt(); From 62d62d1e6be01b9bdf6de3cf7da22b4b0e42a78f Mon Sep 17 00:00:00 2001 From: Derek Gooding Date: Tue, 1 Oct 2024 08:35:48 -0400 Subject: [PATCH 07/13] position summary --- Projects/Server/Serialization/BinaryFileReader.cs | 3 +++ Projects/Server/Serialization/UnmanagedDataReader.cs | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Projects/Server/Serialization/BinaryFileReader.cs b/Projects/Server/Serialization/BinaryFileReader.cs index 731ae2af6..458ffcd3f 100644 --- a/Projects/Server/Serialization/BinaryFileReader.cs +++ b/Projects/Server/Serialization/BinaryFileReader.cs @@ -69,6 +69,9 @@ public BinaryFileReader(string path, bool usePrefixes = true, Encoding encoding } } + /// + /// How many bits deep into the file is the reader at currently. + /// public long Position => _reader.Position; public void Dispose() diff --git a/Projects/Server/Serialization/UnmanagedDataReader.cs b/Projects/Server/Serialization/UnmanagedDataReader.cs index fe7b0cfc0..a1f3f2f4e 100644 --- a/Projects/Server/Serialization/UnmanagedDataReader.cs +++ b/Projects/Server/Serialization/UnmanagedDataReader.cs @@ -38,6 +38,9 @@ public unsafe class UnmanagedDataReader : IGenericReader private readonly Dictionary _typesDb; private readonly Encoding _encoding; + /// + /// How many bits deep into the file is the reader at currently. + /// public long Position { get; private set; } /// @@ -201,7 +204,7 @@ public float ReadFloat() [MethodImpl(MethodImplOptions.AggressiveInlining)] public sbyte ReadSByte() => (sbyte)ReadByte(); /// - /// Read the next 1 bit to make up a boolean. + /// Read the next Byte and return true if it's value returns zero. /// /// Next bool value. [MethodImpl(MethodImplOptions.AggressiveInlining)] From 2299677074e461d5ed38da5943704205049e9bda Mon Sep 17 00:00:00 2001 From: Derek Gooding Date: Tue, 1 Oct 2024 08:44:52 -0400 Subject: [PATCH 08/13] ReadType Also updated the switch statement to handle unexpected byte results as null. --- Projects/Server/Serialization/BinaryFileReader.cs | 7 +++++++ Projects/Server/Serialization/UnmanagedDataReader.cs | 11 +++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Projects/Server/Serialization/BinaryFileReader.cs b/Projects/Server/Serialization/BinaryFileReader.cs index 458ffcd3f..336e0d297 100644 --- a/Projects/Server/Serialization/BinaryFileReader.cs +++ b/Projects/Server/Serialization/BinaryFileReader.cs @@ -166,6 +166,13 @@ public void Dispose() [MethodImpl(MethodImplOptions.AggressiveInlining)] public Serial ReadSerial() => _reader.ReadSerial(); + /// + /// Reads the next Byte which helps determin how to read the following Type. + ///
If the byte returns 1 => and translate into a Type via the
+ ///
If the byte returns 2 =>
+ ///
else return null
+ ///
+ /// Next Type value [MethodImpl(MethodImplOptions.AggressiveInlining)] public Type ReadType() => _reader.ReadType(); diff --git a/Projects/Server/Serialization/UnmanagedDataReader.cs b/Projects/Server/Serialization/UnmanagedDataReader.cs index a1f3f2f4e..7864edc6d 100644 --- a/Projects/Server/Serialization/UnmanagedDataReader.cs +++ b/Projects/Server/Serialization/UnmanagedDataReader.cs @@ -217,12 +217,19 @@ public float ReadFloat() [MethodImpl(MethodImplOptions.AggressiveInlining)] public Serial ReadSerial() => (Serial)ReadUInt(); + /// + /// Reads the next Byte which helps determin how to read the following Type. + ///
If the byte returns 1 => and translate into a Type via the
+ ///
If the byte returns 2 =>
+ ///
else return null
+ ///
+ /// Next Type value public Type ReadType() => ReadByte() switch { - 0 => null, 1 => AssemblyHandler.FindTypeByFullName(ReadStringRaw()), // Backward compatibility - 2 => ReadTypeByHash() + 2 => ReadTypeByHash(), + _ => null, }; public Type ReadTypeByHash() From 7f77075edc4146d8671aeec27a1ff5dd2505869f Mon Sep 17 00:00:00 2001 From: Derek Gooding Date: Tue, 1 Oct 2024 08:49:28 -0400 Subject: [PATCH 09/13] ReadTypeByHash --- Projects/Server/Serialization/UnmanagedDataReader.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Projects/Server/Serialization/UnmanagedDataReader.cs b/Projects/Server/Serialization/UnmanagedDataReader.cs index 7864edc6d..e3d18e0f7 100644 --- a/Projects/Server/Serialization/UnmanagedDataReader.cs +++ b/Projects/Server/Serialization/UnmanagedDataReader.cs @@ -232,6 +232,11 @@ public Type ReadType() => _ => null, }; + /// + /// Reads the next to create a hash and convert that into a Type using the . + /// Will throw an if typesDb is null or typesDb doesn't contain the hash. + /// + /// Next Type value public Type ReadTypeByHash() { var hash = ReadULong(); From 4bf2d0f557ac0b5a047284137314b0c796ba8719 Mon Sep 17 00:00:00 2001 From: Derek Gooding Date: Tue, 1 Oct 2024 09:07:40 -0400 Subject: [PATCH 10/13] Read span --- Projects/Server/Serialization/BinaryFileReader.cs | 6 ++++++ Projects/Server/Serialization/UnmanagedDataReader.cs | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Projects/Server/Serialization/BinaryFileReader.cs b/Projects/Server/Serialization/BinaryFileReader.cs index 336e0d297..830e3c649 100644 --- a/Projects/Server/Serialization/BinaryFileReader.cs +++ b/Projects/Server/Serialization/BinaryFileReader.cs @@ -176,6 +176,12 @@ public void Dispose() [MethodImpl(MethodImplOptions.AggressiveInlining)] public Type ReadType() => _reader.ReadType(); + /// + /// Reads the next set of bytes to fill the buffer. + /// + /// A reference span that will be filled with the next set of bytes. + /// The length of the buffer. + /// Thrown if the buffer is larger than the remaining data to read in the file. [MethodImpl(MethodImplOptions.AggressiveInlining)] public int Read(Span buffer) => _reader.Read(buffer); diff --git a/Projects/Server/Serialization/UnmanagedDataReader.cs b/Projects/Server/Serialization/UnmanagedDataReader.cs index e3d18e0f7..db453492f 100644 --- a/Projects/Server/Serialization/UnmanagedDataReader.cs +++ b/Projects/Server/Serialization/UnmanagedDataReader.cs @@ -234,7 +234,7 @@ public Type ReadType() => /// /// Reads the next to create a hash and convert that into a Type using the . - /// Will throw an if typesDb is null or typesDb doesn't contain the hash. + /// Will log an if typesDb is null or typesDb doesn't contain the hash and return null /// /// Next Type value public Type ReadTypeByHash() @@ -285,6 +285,12 @@ public Type ReadTypeByHash() return t; } + /// + /// Reads the next set of bytes to fill the buffer. + /// + /// A reference span that will be filled with the next set of bytes. + /// The length of the buffer. + /// Thrown if the buffer is larger than the remaining data to read in the file. public int Read(Span buffer) { var length = buffer.Length; From bd9517b3c49959656de8e4c36e512dee560453df Mon Sep 17 00:00:00 2001 From: Derek Gooding Date: Tue, 1 Oct 2024 09:09:43 -0400 Subject: [PATCH 11/13] seek --- Projects/Server/Serialization/BinaryFileReader.cs | 8 ++++++++ Projects/Server/Serialization/UnmanagedDataReader.cs | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/Projects/Server/Serialization/BinaryFileReader.cs b/Projects/Server/Serialization/BinaryFileReader.cs index 830e3c649..f540f8e2e 100644 --- a/Projects/Server/Serialization/BinaryFileReader.cs +++ b/Projects/Server/Serialization/BinaryFileReader.cs @@ -185,6 +185,14 @@ public void Dispose() [MethodImpl(MethodImplOptions.AggressiveInlining)] public int Read(Span buffer) => _reader.Read(buffer); + /// + /// Sets the current position of the stream to a specified value. + /// + /// The new position, relative to the parameter. + /// The reference point for the parameter. It can be one of the values of . + /// The new position in the stream, in bytes. + /// Thrown when the or the resulting position is out of the valid range. + /// Thrown if the stream does not support seeking. [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Seek(long offset, SeekOrigin origin) => _reader.Seek(offset, origin); } diff --git a/Projects/Server/Serialization/UnmanagedDataReader.cs b/Projects/Server/Serialization/UnmanagedDataReader.cs index db453492f..e9f861721 100644 --- a/Projects/Server/Serialization/UnmanagedDataReader.cs +++ b/Projects/Server/Serialization/UnmanagedDataReader.cs @@ -304,6 +304,14 @@ public int Read(Span buffer) return length; } + /// + /// Sets the current position of the stream to a specified value. + /// + /// The new position, relative to the parameter. + /// The reference point for the parameter. It can be one of the values of . + /// The new position in the stream, in bytes. + /// Thrown when the or the resulting position is out of the valid range. + /// Thrown if the stream does not support seeking. public virtual long Seek(long offset, SeekOrigin origin) { Debug.Assert( From 8f357263cd8646eda07fb49dc053e6874d6788e0 Mon Sep 17 00:00:00 2001 From: Derek Gooding Date: Tue, 1 Oct 2024 18:00:06 -0400 Subject: [PATCH 12/13] Remove RCS1123 --- .editorconfig | 1 + Projects/Server/Serialization/UnmanagedDataReader.cs | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.editorconfig b/.editorconfig index 84d50536e..5acefbd37 100644 --- a/.editorconfig +++ b/.editorconfig @@ -147,6 +147,7 @@ dotnet_diagnostic.IDE0008.severity = none # using var # csharp_style_var_for_built_in_types = true:suggestion # suggest using var (implied) variables # csharp_style_var_when_type_is_apparent = true:suggestion # suggest using var (implied) variables # csharp_style_var_elsewhere = true:suggestion # suggest using var (implied) variables +dotnet_diagnostic.RCS1123.severity = none # ReSharper properties resharper_apply_auto_detected_rules=false diff --git a/Projects/Server/Serialization/UnmanagedDataReader.cs b/Projects/Server/Serialization/UnmanagedDataReader.cs index e9f861721..9babb86fd 100644 --- a/Projects/Server/Serialization/UnmanagedDataReader.cs +++ b/Projects/Server/Serialization/UnmanagedDataReader.cs @@ -315,15 +315,15 @@ public int Read(Span buffer) public virtual long Seek(long offset, SeekOrigin origin) { Debug.Assert( - origin != SeekOrigin.End || (offset <= 0 && offset > _size), + origin != SeekOrigin.End || offset <= 0 && offset > _size, "Attempting to seek to an invalid position using SeekOrigin.End" ); Debug.Assert( - origin != SeekOrigin.Begin || (offset >= 0 && offset < _size), + origin != SeekOrigin.Begin || offset >= 0 && offset < _size, "Attempting to seek to an invalid position using SeekOrigin.Begin" ); Debug.Assert( - origin != SeekOrigin.Current || (Position + offset >= 0 && Position + offset < _size), + origin != SeekOrigin.Current || Position + offset >= 0 && Position + offset < _size, "Attempting to seek to an invalid position using SeekOrigin.Current" ); From f0868ec04e491712b85f9369b89e506910389b0c Mon Sep 17 00:00:00 2001 From: Derek Gooding Date: Tue, 1 Oct 2024 18:01:41 -0400 Subject: [PATCH 13/13] update ruleset --- Rules.ruleset | 993 +++++++++++++++++++++++++------------------------- 1 file changed, 498 insertions(+), 495 deletions(-) diff --git a/Rules.ruleset b/Rules.ruleset index 32bd4e62c..84b14555f 100644 --- a/Rules.ruleset +++ b/Rules.ruleset @@ -1,496 +1,499 @@  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file