diff --git a/Assets/Mirror/Core/NetworkReaderExtensions.cs b/Assets/Mirror/Core/NetworkReaderExtensions.cs index 77b7eb86d..4b86a5f79 100644 --- a/Assets/Mirror/Core/NetworkReaderExtensions.cs +++ b/Assets/Mirror/Core/NetworkReaderExtensions.cs @@ -249,6 +249,19 @@ public static List ReadList(this NetworkReader reader) return result; } + public static HashSet ReadHashSet(this NetworkReader reader) + { + int length = reader.ReadInt(); + if (length < 0) + return null; + HashSet result = new HashSet(length); + for (int i = 0; i < length; i++) + { + result.Add(reader.Read()); + } + return result; + } + public static T[] ReadArray(this NetworkReader reader) { int length = reader.ReadInt(); diff --git a/Assets/Mirror/Core/NetworkWriterExtensions.cs b/Assets/Mirror/Core/NetworkWriterExtensions.cs index 41c57627c..a90ed4d87 100644 --- a/Assets/Mirror/Core/NetworkWriterExtensions.cs +++ b/Assets/Mirror/Core/NetworkWriterExtensions.cs @@ -293,6 +293,19 @@ public static void WriteArray(this NetworkWriter writer, T[] array) writer.Write(array[i]); } + public static void WriteHashSet(this NetworkWriter writer, HashSet hashSet) + { + if (hashSet is null) + { + writer.WriteInt(-1); + return; + } + writer.WriteInt(hashSet.Count); + foreach (T item in hashSet) + writer.Write(item); + } + + public static void WriteUri(this NetworkWriter writer, Uri uri) { writer.WriteString(uri?.ToString()); diff --git a/Assets/Mirror/Tests/Editor/NetworkWriterTest.cs b/Assets/Mirror/Tests/Editor/NetworkWriterTest.cs index 5d726637d..e2048a9b8 100644 --- a/Assets/Mirror/Tests/Editor/NetworkWriterTest.cs +++ b/Assets/Mirror/Tests/Editor/NetworkWriterTest.cs @@ -1317,6 +1317,28 @@ public void TestNullList() Assert.That(readList, Is.Null); } + public void TestHashSet() + { + HashSet original = new HashSet() { 1, 2, 3, 4, 5 }; + NetworkWriter writer = new NetworkWriter(); + writer.Write(original); + + NetworkReader reader = new NetworkReader(writer.ToArray()); + HashSet readHashSet = reader.Read>(); + Assert.That(readHashSet, Is.EqualTo(original)); + } + + [Test] + public void TestNullHashSet() + { + NetworkWriter writer = new NetworkWriter(); + writer.Write>(null); + + NetworkReader reader = new NetworkReader(writer.ToArray()); + HashSet readHashSet = reader.Read>(); + Assert.That(readHashSet, Is.Null); + } + const int testArraySize = 4; [Test]