From c4f803fcf690be830a80b1a595dfdaf9932ff2dc Mon Sep 17 00:00:00 2001 From: mischa Date: Tue, 29 Oct 2024 09:38:42 +0100 Subject: [PATCH] fix: #3301 Read/Write HashSet support is now detected by Weaver --- Assets/Mirror/Core/NetworkReaderExtensions.cs | 3 -- Assets/Mirror/Core/NetworkWriterExtensions.cs | 41 +++++++++---------- Assets/Mirror/Editor/Weaver/Readers.cs | 7 ++++ Assets/Mirror/Editor/Weaver/Writers.cs | 7 ++++ .../NetworkReaderWriter/NetworkWriterTest.cs | 12 +++++- 5 files changed, 43 insertions(+), 27 deletions(-) diff --git a/Assets/Mirror/Core/NetworkReaderExtensions.cs b/Assets/Mirror/Core/NetworkReaderExtensions.cs index 7d3d00c31..dd366f667 100644 --- a/Assets/Mirror/Core/NetworkReaderExtensions.cs +++ b/Assets/Mirror/Core/NetworkReaderExtensions.cs @@ -313,8 +313,6 @@ public static List ReadList(this NetworkReader reader) // structs may have .Set members which weaver needs to be able to // fully serialize for NetworkMessages etc. // note that Weaver/Readers/GenerateReader() handles this manually. - // TODO writer not found. need to adjust weaver first. see tests. - /* public static HashSet ReadHashSet(this NetworkReader reader) { // we offset count by '1' to easily support null without writing another byte. @@ -334,7 +332,6 @@ public static HashSet ReadHashSet(this NetworkReader reader) } return result; } - */ public static T[] ReadArray(this NetworkReader reader) { diff --git a/Assets/Mirror/Core/NetworkWriterExtensions.cs b/Assets/Mirror/Core/NetworkWriterExtensions.cs index 6ee5be32a..1f6588f77 100644 --- a/Assets/Mirror/Core/NetworkWriterExtensions.cs +++ b/Assets/Mirror/Core/NetworkWriterExtensions.cs @@ -365,28 +365,25 @@ public static void WriteList(this NetworkWriter writer, List list) // structs may have .Set members which weaver needs to be able to // fully serialize for NetworkMessages etc. // note that Weaver/Writers/GenerateWriter() handles this manually. - // TODO writer not found. need to adjust weaver first. see tests. - // /* - // public static void WriteHashSet(this NetworkWriter writer, HashSet hashSet) - // { - // // we offset count by '1' to easily support null without writing another byte. - // // encoding null as '0' instead of '-1' also allows for better compression - // // (ushort vs. short / varuint vs. varint) etc. - // if (hashSet is null) - // { - // // most sizes are small, write size as VarUInt! - // Compression.CompressVarUInt(writer, 0u); - // //writer.WriteUInt(0); - // return; - // } - // - // // most sizes are small, write size as VarUInt! - // Compression.CompressVarUInt(writer, checked((uint)hashSet.Count) + 1u); - // //writer.WriteUInt(checked((uint)hashSet.Count) + 1u); - // foreach (T item in hashSet) - // writer.Write(item); - // } - // */ + public static void WriteHashSet(this NetworkWriter writer, HashSet hashSet) + { + // we offset count by '1' to easily support null without writing another byte. + // encoding null as '0' instead of '-1' also allows for better compression + // (ushort vs. short / varuint vs. varint) etc. + if (hashSet is null) + { + // most sizes are small, write size as VarUInt! + Compression.CompressVarUInt(writer, 0u); + //writer.WriteUInt(0); + return; + } + + // most sizes are small, write size as VarUInt! + Compression.CompressVarUInt(writer, checked((uint)hashSet.Count) + 1u); + //writer.WriteUInt(checked((uint)hashSet.Count) + 1u); + foreach (T item in hashSet) + writer.Write(item); + } public static void WriteArray(this NetworkWriter writer, T[] array) { diff --git a/Assets/Mirror/Editor/Weaver/Readers.cs b/Assets/Mirror/Editor/Weaver/Readers.cs index 9963f6422..875f8b7b9 100644 --- a/Assets/Mirror/Editor/Weaver/Readers.cs +++ b/Assets/Mirror/Editor/Weaver/Readers.cs @@ -125,6 +125,13 @@ MethodReference GenerateReader(TypeReference variableReference, ref bool Weaving return GenerateReadCollection(variableReference, elementType, nameof(NetworkReaderExtensions.ReadList), ref WeavingFailed); } + else if (variableDefinition.Is(typeof(HashSet<>))) + { + GenericInstanceType genericInstance = (GenericInstanceType)variableReference; + TypeReference elementType = genericInstance.GenericArguments[0]; + + return GenerateReadCollection(variableReference, elementType, nameof(NetworkReaderExtensions.ReadHashSet), ref WeavingFailed); + } // handle both NetworkBehaviour and inheritors. // fixes: https://github.com/MirrorNetworking/Mirror/issues/2939 else if (variableReference.IsDerivedFrom() || variableReference.Is()) diff --git a/Assets/Mirror/Editor/Weaver/Writers.cs b/Assets/Mirror/Editor/Weaver/Writers.cs index a1cb00eb2..31a4ac7f7 100644 --- a/Assets/Mirror/Editor/Weaver/Writers.cs +++ b/Assets/Mirror/Editor/Weaver/Writers.cs @@ -126,6 +126,13 @@ MethodReference GenerateWriter(TypeReference variableReference, ref bool Weaving return GenerateCollectionWriter(variableReference, elementType, nameof(NetworkWriterExtensions.WriteList), ref WeavingFailed); } + if (variableReference.Is(typeof(HashSet<>))) + { + GenericInstanceType genericInstance = (GenericInstanceType)variableReference; + TypeReference elementType = genericInstance.GenericArguments[0]; + + return GenerateCollectionWriter(variableReference, elementType, nameof(NetworkWriterExtensions.WriteHashSet), ref WeavingFailed); + } // handle both NetworkBehaviour and inheritors. // fixes: https://github.com/MirrorNetworking/Mirror/issues/2939 diff --git a/Assets/Mirror/Tests/Editor/NetworkReaderWriter/NetworkWriterTest.cs b/Assets/Mirror/Tests/Editor/NetworkReaderWriter/NetworkWriterTest.cs index 95fd5394b..5edc18b33 100644 --- a/Assets/Mirror/Tests/Editor/NetworkReaderWriter/NetworkWriterTest.cs +++ b/Assets/Mirror/Tests/Editor/NetworkReaderWriter/NetworkWriterTest.cs @@ -1341,7 +1341,15 @@ public void TestNullList() Assert.That(readList, Is.Null); } - [Test, Ignore("TODO")] + // writer.Write for HashSet only works if it's actually used by weaver somewhere. + // so for TestHashSet() to pass, we need to pretend using a HashSet somewhere. + class HashSetNetworkBehaviour : NetworkBehaviour + { + [Command] + public void CmdHashSet(HashSet hashSet) {} + } + + [Test] // requires HashSetNetworkBehaviour to exits! public void TestHashSet() { HashSet original = new HashSet() { 1, 2, 3, 4, 5 }; @@ -1353,7 +1361,7 @@ public void TestHashSet() Assert.That(readHashSet, Is.EqualTo(original)); } - [Test, Ignore("TODO")] + [Test] // requires HashSetNetworkBehaviour to exits! public void TestNullHashSet() { NetworkWriter writer = new NetworkWriter();