rename serialization methods and add BinaryObjectStreamReader for interop
This commit is contained in:
@@ -12,11 +12,11 @@ public interface IReader
|
||||
string ReadString();
|
||||
ReadOnlySpan<byte> ReadBytes(int length);
|
||||
|
||||
T Read<T>() where T : unmanaged;
|
||||
ImmutableArray<T> ReadArray<T>(long count) where T : unmanaged;
|
||||
T ReadPrimitive<T>() where T : unmanaged;
|
||||
ImmutableArray<T> ReadPrimitiveArray<T>(long count) where T : unmanaged;
|
||||
|
||||
T ReadObject<T>(in StructVersion version = default) where T : IReadable, new();
|
||||
ImmutableArray<T> ReadObjectArray<T>(long count, in StructVersion version = default) where T : IReadable, new();
|
||||
T ReadVersionedObject<T>(in StructVersion version = default) where T : IReadable, new();
|
||||
ImmutableArray<T> ReadVersionedObjectArray<T>(long count, in StructVersion version = default) where T : IReadable, new();
|
||||
|
||||
public void Align(int alignment = 0);
|
||||
}
|
||||
@@ -7,20 +7,20 @@ public static class ReaderExtensions
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint ReadCompressedUInt<T>(this ref T reader) where T : struct, IReader, allows ref struct
|
||||
{
|
||||
var first = reader.Read<byte>();
|
||||
var first = reader.ReadPrimitive<byte>();
|
||||
|
||||
if ((first & 0b10000000) == 0b00000000)
|
||||
return first;
|
||||
|
||||
if ((first & 0b11000000) == 0b10000000)
|
||||
return (uint)(((first & ~0b10000000) << 8) | reader.Read<byte>());
|
||||
return (uint)(((first & ~0b10000000) << 8) | reader.ReadPrimitive<byte>());
|
||||
|
||||
if ((first & 0b11100000) == 0b11000000)
|
||||
return (uint)(((first & ~0b11000000) << 24) | (reader.Read<byte>() << 16) | (reader.Read<byte>() << 8) | reader.Read<byte>());
|
||||
return (uint)(((first & ~0b11000000) << 24) | (reader.ReadPrimitive<byte>() << 16) | (reader.ReadPrimitive<byte>() << 8) | reader.ReadPrimitive<byte>());
|
||||
|
||||
return first switch
|
||||
{
|
||||
0b11110000 => reader.Read<uint>(),
|
||||
0b11110000 => reader.ReadPrimitive<uint>(),
|
||||
0b11111110 => uint.MaxValue - 1,
|
||||
0b11111111 => uint.MaxValue,
|
||||
_ => throw new InvalidDataException("Invalid compressed uint")
|
||||
@@ -49,7 +49,7 @@ public static class ReaderExtensions
|
||||
|
||||
do
|
||||
{
|
||||
current = reader.Read<byte>();
|
||||
current = reader.ReadPrimitive<byte>();
|
||||
value |= (current & 0x7FuL) << shift;
|
||||
shift += 7;
|
||||
} while ((current & 0x80) != 0);
|
||||
|
||||
@@ -40,7 +40,7 @@ public ref struct SpanReader(ReadOnlySpan<byte> data, int offset = 0, bool littl
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public T Read<T>() where T : unmanaged
|
||||
public T ReadPrimitive<T>() where T : unmanaged
|
||||
{
|
||||
if (typeof(T) == typeof(byte))
|
||||
return Cast<byte, T>(_data[Offset++]);
|
||||
@@ -84,17 +84,17 @@ public ref struct SpanReader(ReadOnlySpan<byte> data, int offset = 0, bool littl
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ImmutableArray<T> ReadArray<T>(long count) where T : unmanaged
|
||||
public ImmutableArray<T> ReadPrimitiveArray<T>(long count) where T : unmanaged
|
||||
{
|
||||
var array = ImmutableArray.CreateBuilder<T>(checked((int)count));
|
||||
for (long i = 0; i < count; i++)
|
||||
array.Add(Read<T>());
|
||||
array.Add(ReadPrimitive<T>());
|
||||
|
||||
return array.MoveToImmutable();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public T ReadObject<T>(in StructVersion version = default) where T : IReadable, new()
|
||||
public T ReadVersionedObject<T>(in StructVersion version = default) where T : IReadable, new()
|
||||
{
|
||||
var obj = new T();
|
||||
obj.Read(ref this, in version);
|
||||
@@ -102,11 +102,11 @@ public ref struct SpanReader(ReadOnlySpan<byte> data, int offset = 0, bool littl
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ImmutableArray<T> ReadObjectArray<T>(long count, in StructVersion version = default) where T : IReadable, new()
|
||||
public ImmutableArray<T> ReadVersionedObjectArray<T>(long count, in StructVersion version = default) where T : IReadable, new()
|
||||
{
|
||||
var array = ImmutableArray.CreateBuilder<T>(checked((int)count));
|
||||
for (long i = 0; i < count; i++)
|
||||
array.Add(ReadObject<T>(in version));
|
||||
array.Add(ReadVersionedObject<T>(in version));
|
||||
|
||||
return array.MoveToImmutable();
|
||||
}
|
||||
@@ -126,13 +126,13 @@ public ref struct SpanReader(ReadOnlySpan<byte> data, int offset = 0, bool littl
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool ReadBoolean() => Read<byte>() != 0;
|
||||
public bool ReadBoolean() => ReadPrimitive<byte>() != 0;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ulong ReadNUInt() => _is32Bit ? Read<uint>() : Read<ulong>();
|
||||
public ulong ReadNUInt() => _is32Bit ? ReadPrimitive<uint>() : ReadPrimitive<ulong>();
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public long ReadNInt() => _is32Bit ? Read<int>() : Read<long>();
|
||||
public long ReadNInt() => _is32Bit ? ReadPrimitive<int>() : ReadPrimitive<long>();
|
||||
|
||||
public void Align(int alignment = 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user