From 9d043182bfb5be6f05db43cf9cdd3a2eb6133fc2 Mon Sep 17 00:00:00 2001 From: Paul Pacheco Date: Wed, 20 Mar 2019 06:45:46 -0500 Subject: [PATCH] feature: SyncList now support structs (#614) Previously, if you wanted a synclist of structures, you would do: SyncListSTRUCT Now you can use SyncList SyncListSTRUCT is left there, but obsolete --- ...tructProcessor.cs => SyncListProcessor.cs} | 20 +++++++++---------- ...ssor.cs.meta => SyncListProcessor.cs.meta} | 2 +- Assets/Mirror/Editor/Weaver/Weaver.cs | 14 ++++++------- Assets/Mirror/Runtime/SyncList.cs | 5 +++-- Assets/Mirror/Tests/WeaverTest.cs | 2 +- .../SyncListStructGenericGeneric.cs | 2 +- .../SyncListStructMemberBasicType.cs | 2 +- .../SyncListStructMemberGeneric.cs | 2 +- .../SyncListStructMemberInterface.cs | 2 +- .../Tests/WeaverTests~/SyncListStructValid.cs | 2 +- 10 files changed, 27 insertions(+), 26 deletions(-) rename Assets/Mirror/Editor/Weaver/Processors/{SyncListStructProcessor.cs => SyncListProcessor.cs} (89%) rename Assets/Mirror/Editor/Weaver/Processors/{SyncListStructProcessor.cs.meta => SyncListProcessor.cs.meta} (83%) diff --git a/Assets/Mirror/Editor/Weaver/Processors/SyncListStructProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/SyncListProcessor.cs similarity index 89% rename from Assets/Mirror/Editor/Weaver/Processors/SyncListStructProcessor.cs rename to Assets/Mirror/Editor/Weaver/Processors/SyncListProcessor.cs index 931dc67a3..9cb491747 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/SyncListStructProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/SyncListProcessor.cs @@ -1,10 +1,10 @@ -// this class generates OnSerialize/OnDeserialize for SyncListStructs +// this class generates OnSerialize/OnDeserialize for SyncLists using Mono.Cecil; using Mono.Cecil.Cil; namespace Mirror.Weaver { - static class SyncListStructProcessor + static class SyncListProcessor { public static void Process(TypeDefinition td) { @@ -12,12 +12,12 @@ public static void Process(TypeDefinition td) GenericInstanceType gt = (GenericInstanceType)td.BaseType; if (gt.GenericArguments.Count == 0) { - Weaver.Error("SyncListStructProcessor no generic args"); + Weaver.Error("SyncListProcessor no generic args"); return; } TypeReference itemType = Weaver.CurrentAssembly.MainModule.ImportReference(gt.GenericArguments[0]); - Weaver.DLog(td, "SyncListStructProcessor Start item:" + itemType.FullName); + Weaver.DLog(td, "SyncListProcessor Start item:" + itemType.FullName); Weaver.ResetRecursionCount(); MethodReference writeItemFunc = GenerateSerialization(td, itemType); @@ -31,7 +31,7 @@ public static void Process(TypeDefinition td) if (readItemFunc == null || writeItemFunc == null) return; - Weaver.DLog(td, "SyncListStructProcessor Done"); + Weaver.DLog(td, "SyncListProcessor Done"); } // serialization of individual element @@ -56,7 +56,7 @@ static MethodReference GenerateSerialization(TypeDefinition td, TypeReference it if (itemType.IsGenericInstance) { - Weaver.Error("GenerateSerialization for " + Helpers.PrettyPrintType(itemType) + " failed. Struct passed into SyncListStruct can't have generic parameters"); + Weaver.Error("GenerateSerialization for " + Helpers.PrettyPrintType(itemType) + " failed. Struct passed into SyncList can't have generic parameters"); return null; } @@ -70,13 +70,13 @@ static MethodReference GenerateSerialization(TypeDefinition td, TypeReference it if (ft.HasGenericParameters) { - Weaver.Error("GenerateSerialization for " + td.Name + " [" + ft + "/" + ft.FullName + "]. [SyncListStruct] member cannot have generic parameters."); + Weaver.Error("GenerateSerialization for " + td.Name + " [" + ft + "/" + ft.FullName + "]. [SyncList] member cannot have generic parameters."); return null; } if (ft.IsInterface) { - Weaver.Error("GenerateSerialization for " + td.Name + " [" + ft + "/" + ft.FullName + "]. [SyncListStruct] member cannot be an interface."); + Weaver.Error("GenerateSerialization for " + td.Name + " [" + ft + "/" + ft.FullName + "]. [SyncList] member cannot be an interface."); return null; } @@ -90,7 +90,7 @@ static MethodReference GenerateSerialization(TypeDefinition td, TypeReference it } else { - Weaver.Error("GenerateSerialization for " + td.Name + " unknown type [" + ft + "/" + ft.FullName + "]. [SyncListStruct] member variables must be basic types."); + Weaver.Error("GenerateSerialization for " + td.Name + " unknown type [" + ft + "/" + ft.FullName + "]. [SyncList] member variables must be basic types."); return null; } } @@ -144,7 +144,7 @@ static MethodReference GenerateDeserialization(TypeDefinition td, TypeReference } else { - Weaver.Error("GenerateDeserialization for " + td.Name + " unknown type [" + ft + "]. [SyncListStruct] member variables must be basic types."); + Weaver.Error("GenerateDeserialization for " + td.Name + " unknown type [" + ft + "]. [SyncList] member variables must be basic types."); return null; } } diff --git a/Assets/Mirror/Editor/Weaver/Processors/SyncListStructProcessor.cs.meta b/Assets/Mirror/Editor/Weaver/Processors/SyncListProcessor.cs.meta similarity index 83% rename from Assets/Mirror/Editor/Weaver/Processors/SyncListStructProcessor.cs.meta rename to Assets/Mirror/Editor/Weaver/Processors/SyncListProcessor.cs.meta index 4a96fc5d5..b73b04756 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/SyncListStructProcessor.cs.meta +++ b/Assets/Mirror/Editor/Weaver/Processors/SyncListProcessor.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 93397916cae0248bc9294f863fa49f81 +guid: 4f3445268e45d437fac325837aff3246 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Mirror/Editor/Weaver/Weaver.cs b/Assets/Mirror/Editor/Weaver/Weaver.cs index 97b924aea..5355ba7c2 100644 --- a/Assets/Mirror/Editor/Weaver/Weaver.cs +++ b/Assets/Mirror/Editor/Weaver/Weaver.cs @@ -63,7 +63,7 @@ class Weaver public static TypeReference NetworkConnectionType; public static TypeReference MessageBaseType; - public static TypeReference SyncListStructType; + public static TypeReference SyncListType; public static MethodReference NetworkBehaviourDirtyBitsReference; public static TypeReference NetworkClientType; @@ -1124,7 +1124,7 @@ static void SetupTargetTypes() NetworkConnectionType = CurrentAssembly.MainModule.ImportReference(NetworkConnectionType); MessageBaseType = NetAssembly.MainModule.GetType("Mirror.MessageBase"); - SyncListStructType = NetAssembly.MainModule.GetType("Mirror.SyncListSTRUCT`1"); + SyncListType = NetAssembly.MainModule.GetType("Mirror.SyncList`1"); NetworkBehaviourDirtyBitsReference = Resolvers.ResolveProperty(NetworkBehaviourType, CurrentAssembly, "syncVarDirtyBits"); @@ -1347,7 +1347,7 @@ static bool CheckMessageBase(TypeDefinition td) return didWork; } - static bool CheckSyncListStruct(TypeDefinition td) + static bool CheckSyncList(TypeDefinition td) { if (!td.IsClass) return false; @@ -1358,9 +1358,9 @@ static bool CheckSyncListStruct(TypeDefinition td) TypeReference parent = td.BaseType; while (parent != null) { - if (parent.FullName.StartsWith(SyncListStructType.FullName)) + if (parent.FullName.StartsWith(SyncListType.FullName)) { - SyncListStructProcessor.Process(td); + SyncListProcessor.Process(td); didWork = true; break; } @@ -1379,7 +1379,7 @@ static bool CheckSyncListStruct(TypeDefinition td) // check for embedded types foreach (TypeDefinition embedded in td.NestedTypes) { - didWork |= CheckSyncListStruct(embedded); + didWork |= CheckSyncList(embedded); } return didWork; @@ -1414,7 +1414,7 @@ static bool Weave(string assName, IEnumerable dependencies, IAssemblyRes { if (pass == 0) { - didWork |= CheckSyncListStruct(td); + didWork |= CheckSyncList(td); } else { diff --git a/Assets/Mirror/Runtime/SyncList.cs b/Assets/Mirror/Runtime/SyncList.cs index b13997b19..405e1fb1e 100644 --- a/Assets/Mirror/Runtime/SyncList.cs +++ b/Assets/Mirror/Runtime/SyncList.cs @@ -43,6 +43,7 @@ public class SyncListBool : SyncList // in Unity 2019.1. // // TODO rename back to SyncListStruct after 2019.1! + [Obsolete("Use SyncList instead")] public class SyncListSTRUCT : SyncList where T : struct { protected override void SerializeItem(NetworkWriter writer, T item) {} @@ -86,8 +87,8 @@ struct Change // so we need to skip them int changesAhead = 0; - protected abstract void SerializeItem(NetworkWriter writer, T item); - protected abstract T DeserializeItem(NetworkReader reader); + protected virtual void SerializeItem(NetworkWriter writer, T item) { } + protected virtual T DeserializeItem(NetworkReader reader) => default(T); public bool IsDirty => Changes.Count > 0; diff --git a/Assets/Mirror/Tests/WeaverTest.cs b/Assets/Mirror/Tests/WeaverTest.cs index 636878b5e..86e535c52 100644 --- a/Assets/Mirror/Tests/WeaverTest.cs +++ b/Assets/Mirror/Tests/WeaverTest.cs @@ -278,7 +278,7 @@ public void SyncListStructGenericGeneric() { Assert.That(CompilationFinishedHook.WeaveFailed, Is.True); Assert.That(m_weaverErrors.Count, Is.EqualTo(1)); - Assert.That(m_weaverErrors[0], Does.Match("Struct passed into SyncListStruct can't have generic parameters")); + Assert.That(m_weaverErrors[0], Does.Match("Struct passed into SyncList can't have generic parameters")); } [Test] diff --git a/Assets/Mirror/Tests/WeaverTests~/SyncListStructGenericGeneric.cs b/Assets/Mirror/Tests/WeaverTests~/SyncListStructGenericGeneric.cs index 7c5dc988b..5cd402e24 100644 --- a/Assets/Mirror/Tests/WeaverTests~/SyncListStructGenericGeneric.cs +++ b/Assets/Mirror/Tests/WeaverTests~/SyncListStructGenericGeneric.cs @@ -20,7 +20,7 @@ struct MyStruct MyGenericStruct potato; } - class MyStructClass : SyncListSTRUCT> {}; + class MyStructClass : SyncList> {}; MyStructClass harpseals; } diff --git a/Assets/Mirror/Tests/WeaverTests~/SyncListStructMemberBasicType.cs b/Assets/Mirror/Tests/WeaverTests~/SyncListStructMemberBasicType.cs index 2fb4815ac..f3c8d4ab2 100644 --- a/Assets/Mirror/Tests/WeaverTests~/SyncListStructMemberBasicType.cs +++ b/Assets/Mirror/Tests/WeaverTests~/SyncListStructMemberBasicType.cs @@ -16,7 +16,7 @@ struct MyStruct public object nonbasicpotato; } - class MyStructClass : SyncListSTRUCT + class MyStructClass : SyncList { int potatoCount; public MyStructClass(int numberOfPotatoes) diff --git a/Assets/Mirror/Tests/WeaverTests~/SyncListStructMemberGeneric.cs b/Assets/Mirror/Tests/WeaverTests~/SyncListStructMemberGeneric.cs index 37b555944..52a555ff3 100644 --- a/Assets/Mirror/Tests/WeaverTests~/SyncListStructMemberGeneric.cs +++ b/Assets/Mirror/Tests/WeaverTests~/SyncListStructMemberGeneric.cs @@ -20,7 +20,7 @@ struct MyStruct public MyGenericStruct potato; } - class MyStructClass : SyncListSTRUCT {}; + class MyStructClass : SyncList {}; MyStructClass harpseals; } diff --git a/Assets/Mirror/Tests/WeaverTests~/SyncListStructMemberInterface.cs b/Assets/Mirror/Tests/WeaverTests~/SyncListStructMemberInterface.cs index 34b7ee69a..c81722814 100644 --- a/Assets/Mirror/Tests/WeaverTests~/SyncListStructMemberInterface.cs +++ b/Assets/Mirror/Tests/WeaverTests~/SyncListStructMemberInterface.cs @@ -15,7 +15,7 @@ struct MyStruct public IPotato potato; } - class MyStructClass : SyncListSTRUCT {}; + class MyStructClass : SyncList {}; MyStructClass harpseals; } diff --git a/Assets/Mirror/Tests/WeaverTests~/SyncListStructValid.cs b/Assets/Mirror/Tests/WeaverTests~/SyncListStructValid.cs index 57af48eb6..d43a7ccc1 100644 --- a/Assets/Mirror/Tests/WeaverTests~/SyncListStructValid.cs +++ b/Assets/Mirror/Tests/WeaverTests~/SyncListStructValid.cs @@ -11,7 +11,7 @@ struct MyStruct float floatingpotato; double givemetwopotatoes; } - class MyStructClass : SyncListSTRUCT {}; + class MyStructClass : SyncList {}; MyStructClass Foo; } }