From e5eecbff729f426e0de387f86fed70dc1c28b35a Mon Sep 17 00:00:00 2001 From: MichalPetryka <35800402+MichalPetryka@users.noreply.github.com> Date: Thu, 13 Jun 2019 16:03:54 +0200 Subject: [PATCH] feat: support sending and receiving ArraySegment (#898) Add ArraySegment support to the weaver --- Assets/Mirror/Editor/Weaver/Readers.cs | 1 + Assets/Mirror/Editor/Weaver/Writers.cs | 3 ++- Assets/Mirror/Runtime/NetworkReader.cs | 1 + Assets/Mirror/Runtime/NetworkWriter.cs | 5 +++++ 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Assets/Mirror/Editor/Weaver/Readers.cs b/Assets/Mirror/Editor/Weaver/Readers.cs index 59657568c..d2d3bb0ac 100644 --- a/Assets/Mirror/Editor/Weaver/Readers.cs +++ b/Assets/Mirror/Editor/Weaver/Readers.cs @@ -48,6 +48,7 @@ public static void Init(AssemblyDefinition CurrentAssembly) { Weaver.NetworkIdentityType.FullName, Resolvers.ResolveMethod(networkReaderType, CurrentAssembly, "ReadNetworkIdentity") }, { Weaver.transformType.FullName, Resolvers.ResolveMethod(networkReaderType, CurrentAssembly, "ReadTransform") }, { "System.Byte[]", Resolvers.ResolveMethod(networkReaderType, CurrentAssembly, "ReadBytesAndSize") }, + { "System.ArraySegment`1", Resolvers.ResolveMethod(networkReaderType, CurrentAssembly, "ReadBytesSegment") } }; } diff --git a/Assets/Mirror/Editor/Weaver/Writers.cs b/Assets/Mirror/Editor/Weaver/Writers.cs index ca6f7ee86..7add21639 100644 --- a/Assets/Mirror/Editor/Weaver/Writers.cs +++ b/Assets/Mirror/Editor/Weaver/Writers.cs @@ -48,7 +48,8 @@ public static void Init(AssemblyDefinition CurrentAssembly) { Weaver.gameObjectType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.gameObjectType) }, { Weaver.NetworkIdentityType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.NetworkIdentityType) }, { 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", Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "WriteBytesSegment", "System.ArraySegment`1") } }; } diff --git a/Assets/Mirror/Runtime/NetworkReader.cs b/Assets/Mirror/Runtime/NetworkReader.cs index f328f94b0..ac1cf77c5 100644 --- a/Assets/Mirror/Runtime/NetworkReader.cs +++ b/Assets/Mirror/Runtime/NetworkReader.cs @@ -60,6 +60,7 @@ public byte[] ReadBytes(int count) // Use checked() to force it to throw OverflowException if data is invalid // null support, see NetworkWriter public byte[] ReadBytesAndSize() => ReadBoolean() ? ReadBytes(checked((int)ReadPackedUInt32())) : null; + public ArraySegment ReadBytesSegment() => new ArraySegment(ReadBytesAndSize()); // zigzag decoding https://gist.github.com/mfuerstenau/ba870a29e16536fdbaba public int ReadPackedInt32() diff --git a/Assets/Mirror/Runtime/NetworkWriter.cs b/Assets/Mirror/Runtime/NetworkWriter.cs index 854969826..538f530a2 100644 --- a/Assets/Mirror/Runtime/NetworkWriter.cs +++ b/Assets/Mirror/Runtime/NetworkWriter.cs @@ -94,6 +94,11 @@ public void WriteBytesAndSize(byte[] buffer) WriteBytesAndSize(buffer, 0, buffer != null ? buffer.Length : 0); } + public void WriteBytesSegment(ArraySegment buffer) + { + WriteBytesAndSize(buffer.Array, buffer.Offset, buffer.Count); + } + // zigzag encoding https://gist.github.com/mfuerstenau/ba870a29e16536fdbaba public void WritePackedInt32(int i) {