Skip to content

Commit

Permalink
Merge pull request #551 from s-rayleigh/master
Browse files Browse the repository at this point in the history
Serialization enhancements
  • Loading branch information
RevenantX committed May 10, 2024
2 parents bf5a1cc + e60271d commit 367c35b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions LiteNetLib.Tests/NetSerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void Init()
SomeFloat = 3.42f,
SomeIntArray = new[] { 6, 5, 4 },
SomeString = "Test String",
SomeGuid = Guid.NewGuid(),
SomeVector2 = new SomeVector2(4, 5),
SomeVectors = new[] { new SomeVector2(1, 2), new SomeVector2(3, 4) },
SomeEnum = TestEnum.B,
Expand Down Expand Up @@ -148,6 +149,7 @@ private class SamplePacket
public int[] SomeIntArray { get; set; }
public byte[] SomeByteArray { get; set; }
public string SomeString { get; set; }
public Guid SomeGuid { get; set; }
public SomeVector2 SomeVector2 { get; set; }
public SomeVector2[] SomeVectors { get; set; }
public TestEnum SomeEnum { get; set; }
Expand Down Expand Up @@ -200,6 +202,7 @@ public void CustomPackageTest()
Assert.AreEqual(_samplePacket.SomeFloat, readPackage.SomeFloat);
Assert.AreEqual(_samplePacket.SomeIntArray, readPackage.SomeIntArray);
Assert.IsTrue(AreSame(_samplePacket.SomeString, readPackage.SomeString));
Assert.AreEqual(_samplePacket.SomeGuid, readPackage.SomeGuid);
Assert.AreEqual(_samplePacket.SomeVector2, readPackage.SomeVector2);
Assert.AreEqual(_samplePacket.SomeVectors, readPackage.SomeVectors);
Assert.AreEqual(_samplePacket.SomeEnum, readPackage.SomeEnum);
Expand Down
26 changes: 26 additions & 0 deletions LiteNetLib/Utils/NetDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ public void Get(out string result, int maxLength)
{
result = GetString(maxLength);
}

public void Get(out Guid result)
{
result = GetGuid();
}

public IPEndPoint GetNetEndPoint()
{
Expand Down Expand Up @@ -239,6 +244,16 @@ public T[] GetArray<T>() where T : INetSerializable, new()
return result;
}

public T[] GetArray<T>(Func<T> constructor) where T : class, INetSerializable
{
ushort length = BitConverter.ToUInt16(_data, _position);
_position += 2;
T[] result = new T[length];
for (int i = 0; i < length; i++)
Get(out result[i], constructor);
return result;
}

public bool[] GetBoolArray()
{
return GetArray<bool>(1);
Expand Down Expand Up @@ -419,6 +434,17 @@ public string GetString()

return NetDataWriter.uTF8Encoding.Value.GetString(data.Array, data.Offset, data.Count);
}

public Guid GetGuid()
{
#if LITENETLIB_SPANS || NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1 || NETCOREAPP3_1 || NET5_0 || NETSTANDARD2_1
var result = new Guid(_data.AsSpan(_position, 16));
_position += 16;
return result;
#else
return new Guid(GetBytesWithLength());
#endif
}

public ArraySegment<byte> GetBytesSegment(int count)
{
Expand Down
12 changes: 12 additions & 0 deletions LiteNetLib/Utils/NetDataWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ public void Put(byte value)
_position++;
}

public void Put(Guid value)
{
#if LITENETLIB_SPANS || NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1 || NETCOREAPP3_1 || NET5_0 || NETSTANDARD2_1
if (_autoResize)
ResizeIfNeed(_position + 16);
value.TryWriteBytes(_data.AsSpan(_position));
_position += 16;
#else
PutBytesWithLength(value.ToByteArray());
#endif
}

public void Put(byte[] data, int offset, int length)
{
if (_autoResize)
Expand Down
8 changes: 8 additions & 0 deletions LiteNetLib/Utils/NetSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,12 @@ private class IPEndPointSerializer<T> : FastCallSpecificAuto<T, IPEndPoint>
protected override void ElementWrite(NetDataWriter w, ref IPEndPoint prop) { w.Put(prop); }
protected override void ElementRead(NetDataReader r, out IPEndPoint prop) { prop = r.GetNetEndPoint(); }
}

private class GuidSerializer<T> : FastCallSpecificAuto<T, Guid>
{
protected override void ElementWrite(NetDataWriter w, ref Guid guid) { w.Put(guid); }
protected override void ElementRead(NetDataReader r, out Guid guid) { guid = r.GetGuid(); }
}

private class StringSerializer<T> : FastCallSpecific<T, string>
{
Expand Down Expand Up @@ -645,6 +651,8 @@ public NetSerializer(int maxStringLength)
serialzer = new CharSerializer<T>();
else if (elementType == typeof(IPEndPoint))
serialzer = new IPEndPointSerializer<T>();
else if (elementType == typeof(Guid))
serialzer = new GuidSerializer<T>();
else
{
_registeredTypes.TryGetValue(elementType, out var customType);
Expand Down

0 comments on commit 367c35b

Please sign in to comment.