add VersionedSerialization + source generator
This commit is contained in:
6
VersionedSerialization/Attributes/AlignedAttribute.cs
Normal file
6
VersionedSerialization/Attributes/AlignedAttribute.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace VersionedSerialization.Attributes;
|
||||
|
||||
#pragma warning disable CS9113 // Parameter is unread.
|
||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
|
||||
public class AlignedAttribute(int alignment) : Attribute;
|
||||
#pragma warning restore CS9113 // Parameter is unread.
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace VersionedSerialization.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
||||
#pragma warning disable CS9113 // Parameter is unread.
|
||||
public class CustomSerializationAttribute(string methodName, string sizeExpression) : Attribute;
|
||||
#pragma warning restore CS9113 // Parameter is unread.
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace VersionedSerialization.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)]
|
||||
public class VersionConditionAttribute : Attribute
|
||||
{
|
||||
public string LessThan { get; set; } = "";
|
||||
public string GreaterThan { get; set; } = "";
|
||||
public string EqualTo { get; set; } = "";
|
||||
public string IncludingTag { get; set; } = "";
|
||||
public string ExcludingTag { get; set; } = "";
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace VersionedSerialization.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
|
||||
public class VersionedStructAttribute : Attribute;
|
||||
7
VersionedSerialization/IReadable.cs
Normal file
7
VersionedSerialization/IReadable.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace VersionedSerialization;
|
||||
|
||||
public interface IReadable
|
||||
{
|
||||
public void Read<TReader>(ref TReader reader, in StructVersion version = default) where TReader : IReader, allows ref struct;
|
||||
public static abstract int Size(in StructVersion version = default, bool is32Bit = false);
|
||||
}
|
||||
22
VersionedSerialization/IReader.cs
Normal file
22
VersionedSerialization/IReader.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace VersionedSerialization;
|
||||
|
||||
public interface IReader
|
||||
{
|
||||
bool Is32Bit { get; }
|
||||
|
||||
bool ReadBoolean();
|
||||
long ReadNInt();
|
||||
ulong ReadNUInt();
|
||||
string ReadString();
|
||||
ReadOnlySpan<byte> ReadBytes(int length);
|
||||
|
||||
T Read<T>() where T : unmanaged;
|
||||
ImmutableArray<T> ReadArray<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();
|
||||
|
||||
public void Align(int alignment = 0);
|
||||
}
|
||||
62
VersionedSerialization/ReaderExtensions.cs
Normal file
62
VersionedSerialization/ReaderExtensions.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace VersionedSerialization;
|
||||
|
||||
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>();
|
||||
|
||||
if ((first & 0b10000000) == 0b00000000)
|
||||
return first;
|
||||
|
||||
if ((first & 0b11000000) == 0b10000000)
|
||||
return (uint)(((first & ~0b10000000) << 8) | reader.Read<byte>());
|
||||
|
||||
if ((first & 0b11100000) == 0b11000000)
|
||||
return (uint)(((first & ~0b11000000) << 24) | (reader.Read<byte>() << 16) | (reader.Read<byte>() << 8) | reader.Read<byte>());
|
||||
|
||||
return first switch
|
||||
{
|
||||
0b11110000 => reader.Read<uint>(),
|
||||
0b11111110 => uint.MaxValue - 1,
|
||||
0b11111111 => uint.MaxValue,
|
||||
_ => throw new InvalidDataException("Invalid compressed uint")
|
||||
};
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int ReadCompressedInt<T>(this ref T reader) where T : struct, IReader, allows ref struct
|
||||
{
|
||||
var value = reader.ReadCompressedUInt();
|
||||
if (value == uint.MaxValue)
|
||||
return int.MinValue;
|
||||
|
||||
var isNegative = (value & 0b1) == 1;
|
||||
value >>= 1;
|
||||
|
||||
return (int)(isNegative ? -(value + 1) : value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ulong ReadSLEB128<T>(this ref T reader) where T : struct, IReader, allows ref struct
|
||||
{
|
||||
var value = 0uL;
|
||||
var shift = 0;
|
||||
byte current;
|
||||
|
||||
do
|
||||
{
|
||||
current = reader.Read<byte>();
|
||||
value |= (current & 0x7FuL) << shift;
|
||||
shift += 7;
|
||||
} while ((current & 0x80) != 0);
|
||||
|
||||
if (64 >= shift && (current & 0x40) != 0)
|
||||
value |= ulong.MaxValue << shift;
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
146
VersionedSerialization/SpanReader.cs
Normal file
146
VersionedSerialization/SpanReader.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
using System.Buffers.Binary;
|
||||
using System.Collections.Immutable;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace VersionedSerialization;
|
||||
|
||||
// ReSharper disable ReplaceSliceWithRangeIndexer | The range indexer gets compiled into .Slice(x, y) and not .Slice(x) which worsens performance
|
||||
public ref struct SpanReader(ReadOnlySpan<byte> data, int offset = 0, bool littleEndian = true, bool is32Bit = false) : IReader
|
||||
{
|
||||
public int Offset = offset;
|
||||
public readonly byte Peek => _data[Offset];
|
||||
public readonly bool IsLittleEndian => _littleEndian;
|
||||
public readonly bool Is32Bit => _is32Bit;
|
||||
public readonly int Length => _data.Length;
|
||||
public readonly int PointerSize => Is32Bit ? sizeof(uint) : sizeof(ulong);
|
||||
|
||||
private readonly ReadOnlySpan<byte> _data = data;
|
||||
private readonly bool _littleEndian = littleEndian;
|
||||
private readonly bool _is32Bit = is32Bit;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private T ReadInternal<T>() where T : unmanaged
|
||||
{
|
||||
var value = MemoryMarshal.Read<T>(_data.Slice(Offset));
|
||||
Offset += Unsafe.SizeOf<T>();
|
||||
return value;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static TTo Cast<TFrom, TTo>(in TFrom from) => Unsafe.As<TFrom, TTo>(ref Unsafe.AsRef(in from));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ReadOnlySpan<byte> ReadBytes(int length)
|
||||
{
|
||||
var val = _data.Slice(Offset, length);
|
||||
Offset += length;
|
||||
return val;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public T Read<T>() where T : unmanaged
|
||||
{
|
||||
if (typeof(T) == typeof(byte))
|
||||
return Cast<byte, T>(_data[Offset++]);
|
||||
|
||||
var value = ReadInternal<T>();
|
||||
if (!_littleEndian)
|
||||
{
|
||||
if (value is ulong val)
|
||||
{
|
||||
var converted = BinaryPrimitives.ReverseEndianness(val);
|
||||
value = Cast<ulong, T>(converted);
|
||||
}
|
||||
else if (typeof(T) == typeof(long))
|
||||
{
|
||||
var converted = BinaryPrimitives.ReverseEndianness(Cast<T, long>(value));
|
||||
value = Cast<long, T>(converted);
|
||||
}
|
||||
else if (typeof(T) == typeof(uint))
|
||||
{
|
||||
var converted = BinaryPrimitives.ReverseEndianness(Cast<T, uint>(value));
|
||||
value = Cast<uint, T>(converted);
|
||||
}
|
||||
else if (typeof(T) == typeof(int))
|
||||
{
|
||||
var converted = BinaryPrimitives.ReverseEndianness(Cast<T, int>(value));
|
||||
value = Cast<int, T>(converted);
|
||||
}
|
||||
else if (typeof(T) == typeof(ushort))
|
||||
{
|
||||
var converted = BinaryPrimitives.ReverseEndianness(Cast<T, ushort>(value));
|
||||
value = Cast<ushort, T>(converted);
|
||||
}
|
||||
else if (typeof(T) == typeof(short))
|
||||
{
|
||||
var converted = BinaryPrimitives.ReverseEndianness(Cast<T, short>(value));
|
||||
value = Cast<short, T>(converted);
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ImmutableArray<T> ReadArray<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>());
|
||||
|
||||
return array.MoveToImmutable();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public T ReadObject<T>(in StructVersion version = default) where T : IReadable, new()
|
||||
{
|
||||
var obj = new T();
|
||||
obj.Read(ref this, in version);
|
||||
return obj;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ImmutableArray<T> ReadObjectArray<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));
|
||||
|
||||
return array.MoveToImmutable();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadString()
|
||||
{
|
||||
var length = _data.Slice(Offset).IndexOf(byte.MinValue);
|
||||
|
||||
if (length == -1)
|
||||
throw new InvalidDataException("Failed to find string in span.");
|
||||
|
||||
var val = _data.Slice(Offset, length);
|
||||
Offset += length + 1; // Skip null terminator
|
||||
|
||||
return Encoding.UTF8.GetString(val);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool ReadBoolean() => Read<byte>() != 0;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ulong ReadNUInt() => _is32Bit ? Read<uint>() : Read<ulong>();
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public long ReadNInt() => _is32Bit ? Read<int>() : Read<long>();
|
||||
|
||||
public void Align(int alignment = 0)
|
||||
{
|
||||
if (alignment == 0)
|
||||
alignment = Is32Bit ? 4 : 8;
|
||||
|
||||
var rem = Offset % alignment;
|
||||
if (rem != 0)
|
||||
Offset += alignment - rem;
|
||||
}
|
||||
}
|
||||
58
VersionedSerialization/StructVersion.cs
Normal file
58
VersionedSerialization/StructVersion.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
namespace VersionedSerialization;
|
||||
|
||||
public readonly struct StructVersion(int major = 0, int minor = 0, string? tag = null) : IEquatable<StructVersion>
|
||||
{
|
||||
public readonly int Major = major;
|
||||
public readonly int Minor = minor;
|
||||
public readonly string? Tag = tag;
|
||||
|
||||
#region Equality operators
|
||||
|
||||
public static bool operator ==(StructVersion left, StructVersion right)
|
||||
=> left.Major == right.Major && left.Minor == right.Minor;
|
||||
|
||||
public static bool operator !=(StructVersion left, StructVersion right)
|
||||
=> !(left == right);
|
||||
|
||||
public static bool operator >(StructVersion left, StructVersion right)
|
||||
=> left.Major > right.Major || (left.Major == right.Major && left.Minor > right.Minor);
|
||||
|
||||
public static bool operator <(StructVersion left, StructVersion right)
|
||||
=> left.Major < right.Major || (left.Major == right.Major && left.Minor < right.Minor);
|
||||
|
||||
public static bool operator >=(StructVersion left, StructVersion right)
|
||||
=> left.Major > right.Major || (left.Major == right.Major && left.Minor >= right.Minor);
|
||||
|
||||
public static bool operator <=(StructVersion left, StructVersion right)
|
||||
=> left.Major < right.Major || (left.Major == right.Major && left.Minor <= right.Minor);
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
=> obj is StructVersion other && Equals(other);
|
||||
|
||||
public bool Equals(StructVersion other)
|
||||
=> Major == other.Major && Minor == other.Minor;
|
||||
|
||||
public override int GetHashCode()
|
||||
=> HashCode.Combine(Major, Minor);
|
||||
|
||||
#endregion
|
||||
|
||||
public override string ToString() => $"{Major}.{Minor}{(Tag != null ? $"-{Tag}" : "")}";
|
||||
|
||||
public static implicit operator StructVersion(string value)
|
||||
{
|
||||
var versionParts = value.Split('.');
|
||||
if (versionParts.Length is 1 or > 2)
|
||||
throw new InvalidOperationException("Invalid version string.");
|
||||
|
||||
var tagParts = versionParts[1].Split("-");
|
||||
if (tagParts.Length > 2)
|
||||
throw new InvalidOperationException("Invalid version string.");
|
||||
|
||||
var major = int.Parse(versionParts[0]);
|
||||
var minor = int.Parse(tagParts[0]);
|
||||
var tag = tagParts.Length == 1 ? null : tagParts[1];
|
||||
|
||||
return new StructVersion(major, minor, tag);
|
||||
}
|
||||
}
|
||||
10
VersionedSerialization/VersionedSerialization.csproj
Normal file
10
VersionedSerialization/VersionedSerialization.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>preview</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user