diff --git a/Assets/Mirror/Editor/Weaver/Processors/NetworkBehaviourProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/NetworkBehaviourProcessor.cs index 6be3e2230..2a90e1c67 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/NetworkBehaviourProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/NetworkBehaviourProcessor.cs @@ -561,7 +561,7 @@ void DeserializeNetworkIdentityField(FieldDefinition syncVar, ILProcessor worker // reader. for 'reader.Read()' below worker.Emit(OpCodes.Ldarg_1); // Read() - worker.Emit(OpCodes.Call, Readers.GetReadFunc(Weaver.weaverTypes.Import())); + worker.Emit(OpCodes.Call, ReaderWriterProcessor.readers.GetReadFunc(Weaver.weaverTypes.Import())); // netId worker.Emit(OpCodes.Stfld, netIdField); @@ -662,7 +662,7 @@ void DeserializeNetworkBehaviourField(FieldDefinition syncVar, ILProcessor worke // reader. for 'reader.Read()' below worker.Emit(OpCodes.Ldarg_1); // Read() - worker.Emit(OpCodes.Call, Readers.GetReadFunc(Weaver.weaverTypes.Import())); + worker.Emit(OpCodes.Call, ReaderWriterProcessor.readers.GetReadFunc(Weaver.weaverTypes.Import())); // netId worker.Emit(OpCodes.Stfld, netIdField); @@ -726,7 +726,7 @@ void DeserializeNormalField(FieldDefinition syncVar, ILProcessor serWorker, Meth } */ - MethodReference readFunc = Readers.GetReadFunc(syncVar.FieldType); + MethodReference readFunc = ReaderWriterProcessor.readers.GetReadFunc(syncVar.FieldType); if (readFunc == null) { Weaver.Error($"{syncVar.Name} has unsupported type. Use a supported Mirror type instead", syncVar); @@ -846,7 +846,7 @@ void GenerateDeSerialization() // get dirty bits serWorker.Append(serWorker.Create(OpCodes.Ldarg_1)); - serWorker.Append(serWorker.Create(OpCodes.Call, Readers.GetReadFunc(Weaver.weaverTypes.Import()))); + serWorker.Append(serWorker.Create(OpCodes.Call, ReaderWriterProcessor.readers.GetReadFunc(Weaver.weaverTypes.Import()))); serWorker.Append(serWorker.Create(OpCodes.Stloc_0)); // conditionally read each syncvar @@ -904,7 +904,7 @@ public static bool ReadArguments(MethodDefinition method, ILProcessor worker, Re } - MethodReference readFunc = Readers.GetReadFunc(param.ParameterType); + MethodReference readFunc = ReaderWriterProcessor.readers.GetReadFunc(param.ParameterType); if (readFunc == null) { diff --git a/Assets/Mirror/Editor/Weaver/Processors/ReaderWriterProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/ReaderWriterProcessor.cs index 226905a2e..ae9714584 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/ReaderWriterProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/ReaderWriterProcessor.cs @@ -12,11 +12,10 @@ namespace Mirror.Weaver public static class ReaderWriterProcessor { public static Writers writers = new Writers(); + public static Readers readers = new Readers(); public static bool Process(AssemblyDefinition CurrentAssembly) { - // initialize readers & writers - Readers.Init(); foreach (Assembly unityAsm in CompilationPipeline.GetAssemblies()) { if (unityAsm.name == "Mirror") @@ -60,7 +59,7 @@ static bool LoadMessageReadWriter(ModuleDefinition module, TypeDefinition klass) bool modified = false; if (!klass.IsAbstract && !klass.IsInterface && klass.ImplementsInterface()) { - Readers.GetReadFunc(module.ImportReference(klass)); + readers.GetReadFunc(module.ImportReference(klass)); writers.GetWriteFunc(module.ImportReference(klass)); modified = true; } @@ -121,7 +120,7 @@ static bool LoadDeclaredReaders(AssemblyDefinition currentAssembly, TypeDefiniti if (method.HasGenericParameters) continue; - Readers.Register(method.ReturnType, currentAssembly.MainModule.ImportReference(method)); + readers.Register(method.ReturnType, currentAssembly.MainModule.ImportReference(method)); modified = true; } return modified; @@ -171,7 +170,7 @@ public static void InitializeReaderAndWriters(AssemblyDefinition currentAssembly //worker.Emit(OpCodes.Call, Weaver.weaverTypes.logWarningReference); writers.InitializeWriters(worker); - Readers.InitializeReaders(worker); + readers.InitializeReaders(worker); worker.Emit(OpCodes.Ret); diff --git a/Assets/Mirror/Editor/Weaver/Processors/SyncObjectProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/SyncObjectProcessor.cs index f2f3a0e23..e716d7ba6 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/SyncObjectProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/SyncObjectProcessor.cs @@ -41,7 +41,7 @@ static void GenerateReadersAndWriters(TypeReference tr) { if (!argument.IsGenericParameter) { - Readers.GetReadFunc(argument); + ReaderWriterProcessor.readers.GetReadFunc(argument); ReaderWriterProcessor.writers.GetWriteFunc(argument); } } diff --git a/Assets/Mirror/Editor/Weaver/Readers.cs b/Assets/Mirror/Editor/Weaver/Readers.cs index 437e6eb0d..fe2d502ca 100644 --- a/Assets/Mirror/Editor/Weaver/Readers.cs +++ b/Assets/Mirror/Editor/Weaver/Readers.cs @@ -6,16 +6,13 @@ namespace Mirror.Weaver { - public static class Readers + // not static, because ILPostProcessor is multithreaded + public class Readers { - static Dictionary readFuncs; + Dictionary readFuncs = + new Dictionary(new TypeReferenceComparer()); - public static void Init() - { - readFuncs = new Dictionary(new TypeReferenceComparer()); - } - - internal static void Register(TypeReference dataType, MethodReference methodReference) + internal void Register(TypeReference dataType, MethodReference methodReference) { if (readFuncs.ContainsKey(dataType)) { @@ -30,7 +27,7 @@ internal static void Register(TypeReference dataType, MethodReference methodRefe readFuncs[imported] = methodReference; } - static void RegisterReadFunc(TypeReference typeReference, MethodDefinition newReaderFunc) + void RegisterReadFunc(TypeReference typeReference, MethodDefinition newReaderFunc) { Register(typeReference, newReaderFunc); @@ -38,7 +35,7 @@ static void RegisterReadFunc(TypeReference typeReference, MethodDefinition newRe } // Finds existing reader for type, if non exists trys to create one - public static MethodReference GetReadFunc(TypeReference variable) + public MethodReference GetReadFunc(TypeReference variable) { if (readFuncs.TryGetValue(variable, out MethodReference foundFunc)) { @@ -51,7 +48,7 @@ public static MethodReference GetReadFunc(TypeReference variable) } } - static MethodReference GenerateReader(TypeReference variableReference) + MethodReference GenerateReader(TypeReference variableReference) { // Arrays are special, if we resolve them, we get the element type, // so the following ifs might choke on it for scriptable objects @@ -148,7 +145,7 @@ static MethodReference GenerateReader(TypeReference variableReference) return GenerateClassOrStructReadFunction(variableReference); } - static MethodReference GetNetworkBehaviourReader(TypeReference variableReference) + MethodReference GetNetworkBehaviourReader(TypeReference variableReference) { // uses generic ReadNetworkBehaviour rather than having weaver create one for each NB MethodReference generic = Weaver.weaverTypes.readNetworkBehaviourGeneric; @@ -162,7 +159,7 @@ static MethodReference GetNetworkBehaviourReader(TypeReference variableReference return readFunc; } - static MethodDefinition GenerateEnumReadFunc(TypeReference variable) + MethodDefinition GenerateEnumReadFunc(TypeReference variable) { MethodDefinition readerFunc = GenerateReaderFunction(variable); @@ -178,7 +175,7 @@ static MethodDefinition GenerateEnumReadFunc(TypeReference variable) return readerFunc; } - static MethodDefinition GenerateArraySegmentReadFunc(TypeReference variable) + MethodDefinition GenerateArraySegmentReadFunc(TypeReference variable) { GenericInstanceType genericInstance = (GenericInstanceType)variable; TypeReference elementType = genericInstance.GenericArguments[0]; @@ -198,7 +195,7 @@ static MethodDefinition GenerateArraySegmentReadFunc(TypeReference variable) return readerFunc; } - static MethodDefinition GenerateReaderFunction(TypeReference variable) + MethodDefinition GenerateReaderFunction(TypeReference variable) { string functionName = "_Read_" + variable.FullName; @@ -216,7 +213,7 @@ static MethodDefinition GenerateReaderFunction(TypeReference variable) return readerFunc; } - static MethodDefinition GenerateReadCollection(TypeReference variable, TypeReference elementType, string readerFunction) + MethodDefinition GenerateReadCollection(TypeReference variable, TypeReference elementType, string readerFunction) { MethodDefinition readerFunc = GenerateReaderFunction(variable); // generate readers for the element @@ -241,7 +238,7 @@ static MethodDefinition GenerateReadCollection(TypeReference variable, TypeRefer return readerFunc; } - static MethodDefinition GenerateClassOrStructReadFunction(TypeReference variable) + MethodDefinition GenerateClassOrStructReadFunction(TypeReference variable) { MethodDefinition readerFunc = GenerateReaderFunction(variable); @@ -263,7 +260,7 @@ static MethodDefinition GenerateClassOrStructReadFunction(TypeReference variable return readerFunc; } - static void GenerateNullCheck(ILProcessor worker) + void GenerateNullCheck(ILProcessor worker) { // if (!reader.ReadBoolean()) { // return null; @@ -280,7 +277,7 @@ static void GenerateNullCheck(ILProcessor worker) } // Initialize the local variable with a new instance - static void CreateNew(TypeReference variable, ILProcessor worker, TypeDefinition td) + void CreateNew(TypeReference variable, ILProcessor worker, TypeDefinition td) { if (variable.IsValueType) { @@ -313,7 +310,7 @@ static void CreateNew(TypeReference variable, ILProcessor worker, TypeDefinition } } - static void ReadAllFields(TypeReference variable, ILProcessor worker) + void ReadAllFields(TypeReference variable, ILProcessor worker) { foreach (FieldDefinition field in variable.FindAllPublicFields()) { @@ -338,7 +335,7 @@ static void ReadAllFields(TypeReference variable, ILProcessor worker) } // Save a delegate for each one of the readers into Reader{T}.read - internal static void InitializeReaders(ILProcessor worker) + internal void InitializeReaders(ILProcessor worker) { ModuleDefinition module = Weaver.CurrentAssembly.MainModule;