feat: support sending and receiving ArraySegment<byte> (#898)

Add ArraySegment<byte> support to the weaver
This commit is contained in:
MichalPetryka 2019-06-13 16:03:54 +02:00 committed by Paul Pacheco
parent 954a3d594d
commit e5eecbff72
4 changed files with 9 additions and 1 deletions

View File

@ -48,6 +48,7 @@ public static void Init(AssemblyDefinition CurrentAssembly)
{ Weaver.NetworkIdentityType.FullName, Resolvers.ResolveMethod(networkReaderType, CurrentAssembly, "ReadNetworkIdentity") }, { Weaver.NetworkIdentityType.FullName, Resolvers.ResolveMethod(networkReaderType, CurrentAssembly, "ReadNetworkIdentity") },
{ Weaver.transformType.FullName, Resolvers.ResolveMethod(networkReaderType, CurrentAssembly, "ReadTransform") }, { Weaver.transformType.FullName, Resolvers.ResolveMethod(networkReaderType, CurrentAssembly, "ReadTransform") },
{ "System.Byte[]", Resolvers.ResolveMethod(networkReaderType, CurrentAssembly, "ReadBytesAndSize") }, { "System.Byte[]", Resolvers.ResolveMethod(networkReaderType, CurrentAssembly, "ReadBytesAndSize") },
{ "System.ArraySegment`1<System.Byte>", Resolvers.ResolveMethod(networkReaderType, CurrentAssembly, "ReadBytesSegment") }
}; };
} }

View File

@ -48,7 +48,8 @@ public static void Init(AssemblyDefinition CurrentAssembly)
{ Weaver.gameObjectType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.gameObjectType) }, { Weaver.gameObjectType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.gameObjectType) },
{ Weaver.NetworkIdentityType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.NetworkIdentityType) }, { Weaver.NetworkIdentityType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.NetworkIdentityType) },
{ Weaver.transformType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.transformType) }, { Weaver.transformType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.transformType) },
{ "System.Byte[]", Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "WriteBytesAndSize", "System.Byte[]") } { "System.Byte[]", Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "WriteBytesAndSize", "System.Byte[]") },
{ "System.ArraySegment`1<System.Byte>", Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "WriteBytesSegment", "System.ArraySegment`1<System.Byte>") }
}; };
} }

View File

@ -60,6 +60,7 @@ public byte[] ReadBytes(int count)
// Use checked() to force it to throw OverflowException if data is invalid // Use checked() to force it to throw OverflowException if data is invalid
// null support, see NetworkWriter // null support, see NetworkWriter
public byte[] ReadBytesAndSize() => ReadBoolean() ? ReadBytes(checked((int)ReadPackedUInt32())) : null; public byte[] ReadBytesAndSize() => ReadBoolean() ? ReadBytes(checked((int)ReadPackedUInt32())) : null;
public ArraySegment<byte> ReadBytesSegment() => new ArraySegment<byte>(ReadBytesAndSize());
// zigzag decoding https://gist.github.com/mfuerstenau/ba870a29e16536fdbaba // zigzag decoding https://gist.github.com/mfuerstenau/ba870a29e16536fdbaba
public int ReadPackedInt32() public int ReadPackedInt32()

View File

@ -94,6 +94,11 @@ public void WriteBytesAndSize(byte[] buffer)
WriteBytesAndSize(buffer, 0, buffer != null ? buffer.Length : 0); WriteBytesAndSize(buffer, 0, buffer != null ? buffer.Length : 0);
} }
public void WriteBytesSegment(ArraySegment<byte> buffer)
{
WriteBytesAndSize(buffer.Array, buffer.Offset, buffer.Count);
}
// zigzag encoding https://gist.github.com/mfuerstenau/ba870a29e16536fdbaba // zigzag encoding https://gist.github.com/mfuerstenau/ba870a29e16536fdbaba
public void WritePackedInt32(int i) public void WritePackedInt32(int i)
{ {