Extensions: remove Weaver.CurrentAssembly static references

This commit is contained in:
vis2k 2021-08-17 22:31:36 +08:00
parent f471b85dcb
commit 6af792e4c5
4 changed files with 15 additions and 15 deletions

View File

@ -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<T>.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<TAttribute>(this ICustomAttributeProvider method)

View File

@ -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

View File

@ -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<T>
// 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<T>($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<T>.read
GenericInstanceType genericInstance = genericReaderClassRef.MakeGenericInstanceType(targetType);
FieldReference specializedField = fieldRef.SpecializeField(genericInstance);
FieldReference specializedField = fieldRef.SpecializeField(assembly.MainModule, genericInstance);
worker.Emit(OpCodes.Stsfld, specializedField);
}
}

View File

@ -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<T>.write
GenericInstanceType genericInstance = genericWriterClassRef.MakeGenericInstanceType(targetType);
FieldReference specializedField = fieldRef.SpecializeField(genericInstance);
FieldReference specializedField = fieldRef.SpecializeField(assembly.MainModule, genericInstance);
worker.Emit(OpCodes.Stsfld, specializedField);
}
}