diff --git a/Assets/Mirror/Editor/Weaver/Extensions.cs b/Assets/Mirror/Editor/Weaver/Extensions.cs index 13fb2a544..8bc881fba 100644 --- a/Assets/Mirror/Editor/Weaver/Extensions.cs +++ b/Assets/Mirror/Editor/Weaver/Extensions.cs @@ -113,12 +113,12 @@ public static bool CanBeResolved(this TypeReference parent) } // Makes T => Variable and imports function - public static MethodReference MakeGeneric(this MethodReference generic, TypeReference variableReference) + public static MethodReference MakeGeneric(this MethodReference generic, ModuleDefinition module, TypeReference variableReference) { GenericInstanceMethod instance = new GenericInstanceMethod(generic); instance.GenericArguments.Add(variableReference); - MethodReference readFunc = Weaver.CurrentAssembly.MainModule.ImportReference(instance); + MethodReference readFunc = module.ImportReference(instance); return readFunc; } @@ -126,7 +126,7 @@ public static MethodReference MakeGeneric(this MethodReference generic, TypeRefe // and a generic instance such as ArraySegment`int // Creates a reference to the specialized method ArraySegment`int`.get_Count // Note that calling ArraySegment`T.get_Count directly gives an invalid IL error - public static MethodReference MakeHostInstanceGeneric(this MethodReference self, GenericInstanceType instanceType) + public static MethodReference MakeHostInstanceGeneric(this MethodReference self, ModuleDefinition module, GenericInstanceType instanceType) { MethodReference reference = new MethodReference(self.Name, self.ReturnType, instanceType) { @@ -141,17 +141,17 @@ public static MethodReference MakeHostInstanceGeneric(this MethodReference self, foreach (GenericParameter generic_parameter in self.GenericParameters) reference.GenericParameters.Add(new GenericParameter(generic_parameter.Name, reference)); - return Weaver.CurrentAssembly.MainModule.ImportReference(reference); + return module.ImportReference(reference); } // Given a field of a generic class such as Writer.write, // and a generic instance such as ArraySegment`int // Creates a reference to the specialized method ArraySegment`int`.get_Count // Note that calling ArraySegment`T.get_Count directly gives an invalid IL error - public static FieldReference SpecializeField(this FieldReference self, GenericInstanceType instanceType) + public static FieldReference SpecializeField(this FieldReference self, ModuleDefinition module, GenericInstanceType instanceType) { FieldReference reference = new FieldReference(self.Name, self.FieldType, instanceType); - return Weaver.CurrentAssembly.MainModule.ImportReference(reference); + return module.ImportReference(reference); } public static CustomAttribute GetCustomAttribute(this ICustomAttributeProvider method) diff --git a/Assets/Mirror/Editor/Weaver/Processors/SyncVarProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/SyncVarProcessor.cs index cda473f7a..b547f096e 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/SyncVarProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/SyncVarProcessor.cs @@ -115,7 +115,7 @@ public static MethodDefinition GenerateSyncVarGetter(FieldDefinition fd, string worker.Emit(OpCodes.Ldfld, netFieldId); worker.Emit(OpCodes.Ldarg_0); worker.Emit(OpCodes.Ldflda, fd); - MethodReference getFunc = Weaver.weaverTypes.getSyncVarNetworkBehaviourReference.MakeGeneric(fd.FieldType); + MethodReference getFunc = Weaver.weaverTypes.getSyncVarNetworkBehaviourReference.MakeGeneric(Weaver.CurrentAssembly.MainModule, fd.FieldType); worker.Emit(OpCodes.Call, getFunc); worker.Emit(OpCodes.Ret); } @@ -175,7 +175,7 @@ public static MethodDefinition GenerateSyncVarSetter(TypeDefinition td, FieldDef worker.Emit(OpCodes.Ldarg_0); worker.Emit(OpCodes.Ldfld, netFieldId); - MethodReference getFunc = Weaver.weaverTypes.syncVarNetworkBehaviourEqualReference.MakeGeneric(fd.FieldType); + MethodReference getFunc = Weaver.weaverTypes.syncVarNetworkBehaviourEqualReference.MakeGeneric(Weaver.CurrentAssembly.MainModule, fd.FieldType); worker.Emit(OpCodes.Call, getFunc); } else @@ -234,7 +234,7 @@ public static MethodDefinition GenerateSyncVarSetter(TypeDefinition td, FieldDef worker.Emit(OpCodes.Ldarg_0); worker.Emit(OpCodes.Ldflda, netFieldId); - MethodReference getFunc = Weaver.weaverTypes.setSyncVarNetworkBehaviourReference.MakeGeneric(fd.FieldType); + MethodReference getFunc = Weaver.weaverTypes.setSyncVarNetworkBehaviourReference.MakeGeneric(Weaver.CurrentAssembly.MainModule, fd.FieldType); worker.Emit(OpCodes.Call, getFunc); } else diff --git a/Assets/Mirror/Editor/Weaver/Readers.cs b/Assets/Mirror/Editor/Weaver/Readers.cs index 2553d8a79..ca48cb91e 100644 --- a/Assets/Mirror/Editor/Weaver/Readers.cs +++ b/Assets/Mirror/Editor/Weaver/Readers.cs @@ -157,7 +157,7 @@ MethodReference GetNetworkBehaviourReader(TypeReference variableReference) // uses generic ReadNetworkBehaviour rather than having weaver create one for each NB MethodReference generic = Weaver.weaverTypes.readNetworkBehaviourGeneric; - MethodReference readFunc = generic.MakeGeneric(variableReference); + MethodReference readFunc = generic.MakeGeneric(assembly.MainModule, variableReference); // register function so it is added to Reader // use Register instead of RegisterWriteFunc because this is not a generated function @@ -197,7 +197,7 @@ MethodDefinition GenerateArraySegmentReadFunc(TypeReference variable) worker.Emit(OpCodes.Call, GetReadFunc(arrayType)); // return new ArraySegment($array); - worker.Emit(OpCodes.Newobj, Weaver.weaverTypes.ArraySegmentConstructorReference.MakeHostInstanceGeneric(genericInstance)); + worker.Emit(OpCodes.Newobj, Weaver.weaverTypes.ArraySegmentConstructorReference.MakeHostInstanceGeneric(assembly.MainModule, genericInstance)); worker.Emit(OpCodes.Ret); return readerFunc; } @@ -363,12 +363,12 @@ internal void InitializeReaders(ILProcessor worker) worker.Emit(OpCodes.Ldnull); worker.Emit(OpCodes.Ldftn, readFunc); GenericInstanceType funcGenericInstance = funcRef.MakeGenericInstanceType(networkReaderRef, targetType); - MethodReference funcConstructorInstance = funcConstructorRef.MakeHostInstanceGeneric(funcGenericInstance); + MethodReference funcConstructorInstance = funcConstructorRef.MakeHostInstanceGeneric(assembly.MainModule, funcGenericInstance); worker.Emit(OpCodes.Newobj, funcConstructorInstance); // save it in Reader.read GenericInstanceType genericInstance = genericReaderClassRef.MakeGenericInstanceType(targetType); - FieldReference specializedField = fieldRef.SpecializeField(genericInstance); + FieldReference specializedField = fieldRef.SpecializeField(assembly.MainModule, genericInstance); worker.Emit(OpCodes.Stsfld, specializedField); } } diff --git a/Assets/Mirror/Editor/Weaver/Writers.cs b/Assets/Mirror/Editor/Weaver/Writers.cs index b28f549e3..71bd1909c 100644 --- a/Assets/Mirror/Editor/Weaver/Writers.cs +++ b/Assets/Mirror/Editor/Weaver/Writers.cs @@ -321,12 +321,12 @@ internal void InitializeWriters(ILProcessor worker) worker.Emit(OpCodes.Ldnull); worker.Emit(OpCodes.Ldftn, writeFunc); GenericInstanceType actionGenericInstance = actionRef.MakeGenericInstanceType(networkWriterRef, targetType); - MethodReference actionRefInstance = actionConstructorRef.MakeHostInstanceGeneric(actionGenericInstance); + MethodReference actionRefInstance = actionConstructorRef.MakeHostInstanceGeneric(assembly.MainModule, actionGenericInstance); worker.Emit(OpCodes.Newobj, actionRefInstance); // save it in Writer.write GenericInstanceType genericInstance = genericWriterClassRef.MakeGenericInstanceType(targetType); - FieldReference specializedField = fieldRef.SpecializeField(genericInstance); + FieldReference specializedField = fieldRef.SpecializeField(assembly.MainModule, genericInstance); worker.Emit(OpCodes.Stsfld, specializedField); } }