fix: #3301 Read/Write HashSet<T> support is now detected by Weaver

This commit is contained in:
mischa 2024-10-29 09:38:42 +01:00
parent 03357f4275
commit c4f803fcf6
5 changed files with 43 additions and 27 deletions

View File

@ -313,8 +313,6 @@ public static List<T> ReadList<T>(this NetworkReader reader)
// structs may have .Set<T> members which weaver needs to be able to // structs may have .Set<T> members which weaver needs to be able to
// fully serialize for NetworkMessages etc. // fully serialize for NetworkMessages etc.
// note that Weaver/Readers/GenerateReader() handles this manually. // note that Weaver/Readers/GenerateReader() handles this manually.
// TODO writer not found. need to adjust weaver first. see tests.
/*
public static HashSet<T> ReadHashSet<T>(this NetworkReader reader) public static HashSet<T> ReadHashSet<T>(this NetworkReader reader)
{ {
// we offset count by '1' to easily support null without writing another byte. // we offset count by '1' to easily support null without writing another byte.
@ -334,7 +332,6 @@ public static HashSet<T> ReadHashSet<T>(this NetworkReader reader)
} }
return result; return result;
} }
*/
public static T[] ReadArray<T>(this NetworkReader reader) public static T[] ReadArray<T>(this NetworkReader reader)
{ {

View File

@ -365,28 +365,25 @@ public static void WriteList<T>(this NetworkWriter writer, List<T> list)
// structs may have .Set<T> members which weaver needs to be able to // structs may have .Set<T> members which weaver needs to be able to
// fully serialize for NetworkMessages etc. // fully serialize for NetworkMessages etc.
// note that Weaver/Writers/GenerateWriter() handles this manually. // note that Weaver/Writers/GenerateWriter() handles this manually.
// TODO writer not found. need to adjust weaver first. see tests. public static void WriteHashSet<T>(this NetworkWriter writer, HashSet<T> hashSet)
// /* {
// public static void WriteHashSet<T>(this NetworkWriter writer, HashSet<T> 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
// // we offset count by '1' to easily support null without writing another byte. // (ushort vs. short / varuint vs. varint) etc.
// // encoding null as '0' instead of '-1' also allows for better compression if (hashSet is null)
// // (ushort vs. short / varuint vs. varint) etc. {
// if (hashSet is null) // most sizes are small, write size as VarUInt!
// { Compression.CompressVarUInt(writer, 0u);
// // most sizes are small, write size as VarUInt! //writer.WriteUInt(0);
// Compression.CompressVarUInt(writer, 0u); return;
// //writer.WriteUInt(0); }
// return;
// } // most sizes are small, write size as VarUInt!
// Compression.CompressVarUInt(writer, checked((uint)hashSet.Count) + 1u);
// // most sizes are small, write size as VarUInt! //writer.WriteUInt(checked((uint)hashSet.Count) + 1u);
// Compression.CompressVarUInt(writer, checked((uint)hashSet.Count) + 1u); foreach (T item in hashSet)
// //writer.WriteUInt(checked((uint)hashSet.Count) + 1u); writer.Write(item);
// foreach (T item in hashSet) }
// writer.Write(item);
// }
// */
public static void WriteArray<T>(this NetworkWriter writer, T[] array) public static void WriteArray<T>(this NetworkWriter writer, T[] array)
{ {

View File

@ -125,6 +125,13 @@ MethodReference GenerateReader(TypeReference variableReference, ref bool Weaving
return GenerateReadCollection(variableReference, elementType, nameof(NetworkReaderExtensions.ReadList), ref WeavingFailed); 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. // handle both NetworkBehaviour and inheritors.
// fixes: https://github.com/MirrorNetworking/Mirror/issues/2939 // fixes: https://github.com/MirrorNetworking/Mirror/issues/2939
else if (variableReference.IsDerivedFrom<NetworkBehaviour>() || variableReference.Is<NetworkBehaviour>()) else if (variableReference.IsDerivedFrom<NetworkBehaviour>() || variableReference.Is<NetworkBehaviour>())

View File

@ -126,6 +126,13 @@ MethodReference GenerateWriter(TypeReference variableReference, ref bool Weaving
return GenerateCollectionWriter(variableReference, elementType, nameof(NetworkWriterExtensions.WriteList), ref WeavingFailed); 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. // handle both NetworkBehaviour and inheritors.
// fixes: https://github.com/MirrorNetworking/Mirror/issues/2939 // fixes: https://github.com/MirrorNetworking/Mirror/issues/2939

View File

@ -1341,7 +1341,15 @@ public void TestNullList()
Assert.That(readList, Is.Null); Assert.That(readList, Is.Null);
} }
[Test, Ignore("TODO")] // writer.Write<T> for HashSet only works if it's actually used by weaver somewhere.
// so for TestHashSet() to pass, we need to pretend using a HashSet<int> somewhere.
class HashSetNetworkBehaviour : NetworkBehaviour
{
[Command]
public void CmdHashSet(HashSet<int> hashSet) {}
}
[Test] // requires HashSetNetworkBehaviour to exits!
public void TestHashSet() public void TestHashSet()
{ {
HashSet<int> original = new HashSet<int>() { 1, 2, 3, 4, 5 }; HashSet<int> original = new HashSet<int>() { 1, 2, 3, 4, 5 };
@ -1353,7 +1361,7 @@ public void TestHashSet()
Assert.That(readHashSet, Is.EqualTo(original)); Assert.That(readHashSet, Is.EqualTo(original));
} }
[Test, Ignore("TODO")] [Test] // requires HashSetNetworkBehaviour to exits!
public void TestNullHashSet() public void TestNullHashSet()
{ {
NetworkWriter writer = new NetworkWriter(); NetworkWriter writer = new NetworkWriter();