Readers: remove static weaverTypes references

This commit is contained in:
vis2k 2021-08-18 23:24:30 +08:00
parent 00357d119a
commit 4c6af1ff6f
3 changed files with 15 additions and 9 deletions

View File

@ -14,7 +14,7 @@ public static class ReaderWriterProcessor
public static Writers writers;
public static Readers readers;
public static bool Process(AssemblyDefinition CurrentAssembly)
public static bool Process(AssemblyDefinition CurrentAssembly, WeaverTypes weaverTypes)
{
// initialize readers & writers with this assembly.
// we need to do this in every Process() call.
@ -22,7 +22,7 @@ public static bool Process(AssemblyDefinition CurrentAssembly)
// "System.ArgumentException: Member ... is declared in another module and needs to be imported"
// errors when still using the previous module's reader/writer funcs.
writers = new Writers(CurrentAssembly);
readers = new Readers(CurrentAssembly);
readers = new Readers(CurrentAssembly, weaverTypes);
foreach (Assembly unityAsm in CompilationPipeline.GetAssemblies())
{

View File

@ -14,10 +14,16 @@ public class Readers
// "System.ArgumentException: Member ... is declared in another module and needs to be imported"
AssemblyDefinition assembly;
WeaverTypes weaverTypes;
Dictionary<TypeReference, MethodReference> readFuncs =
new Dictionary<TypeReference, MethodReference>(new TypeReferenceComparer());
public Readers(AssemblyDefinition assembly) => this.assembly = assembly;
public Readers(AssemblyDefinition assembly, WeaverTypes weaverTypes)
{
this.assembly = assembly;
this.weaverTypes = weaverTypes;
}
internal void Register(TypeReference dataType, MethodReference methodReference)
{
@ -155,7 +161,7 @@ MethodReference GenerateReader(TypeReference variableReference)
MethodReference GetNetworkBehaviourReader(TypeReference variableReference)
{
// uses generic ReadNetworkBehaviour rather than having weaver create one for each NB
MethodReference generic = Weaver.weaverTypes.readNetworkBehaviourGeneric;
MethodReference generic = weaverTypes.readNetworkBehaviourGeneric;
MethodReference readFunc = generic.MakeGeneric(assembly.MainModule, variableReference);
@ -197,7 +203,7 @@ MethodDefinition GenerateArraySegmentReadFunc(TypeReference variable)
worker.Emit(OpCodes.Call, GetReadFunc(arrayType));
// return new ArraySegment<T>($array);
worker.Emit(OpCodes.Newobj, Weaver.weaverTypes.ArraySegmentConstructorReference.MakeHostInstanceGeneric(assembly.MainModule, genericInstance));
worker.Emit(OpCodes.Newobj, weaverTypes.ArraySegmentConstructorReference.MakeHostInstanceGeneric(assembly.MainModule, genericInstance));
worker.Emit(OpCodes.Ret);
return readerFunc;
}
@ -213,7 +219,7 @@ MethodDefinition GenerateReaderFunction(TypeReference variable)
MethodAttributes.HideBySig,
variable);
readerFunc.Parameters.Add(new ParameterDefinition("reader", ParameterAttributes.None, Weaver.weaverTypes.Import<NetworkReader>()));
readerFunc.Parameters.Add(new ParameterDefinition("reader", ParameterAttributes.None, weaverTypes.Import<NetworkReader>()));
readerFunc.Body.InitLocals = true;
RegisterReadFunc(variable, readerFunc);
@ -273,7 +279,7 @@ void GenerateNullCheck(ILProcessor worker)
// return null;
// }
worker.Emit(OpCodes.Ldarg_0);
worker.Emit(OpCodes.Call, GetReadFunc(Weaver.weaverTypes.Import<bool>()));
worker.Emit(OpCodes.Call, GetReadFunc(weaverTypes.Import<bool>()));
Instruction labelEmptyArray = worker.Create(OpCodes.Nop);
worker.Emit(OpCodes.Brtrue, labelEmptyArray);
@ -294,7 +300,7 @@ void CreateNew(TypeReference variable, ILProcessor worker, TypeDefinition td)
}
else if (td.IsDerivedFrom<UnityEngine.ScriptableObject>())
{
GenericInstanceMethod genericInstanceMethod = new GenericInstanceMethod(Weaver.weaverTypes.ScriptableObjectCreateInstanceMethod);
GenericInstanceMethod genericInstanceMethod = new GenericInstanceMethod(weaverTypes.ScriptableObjectCreateInstanceMethod);
genericInstanceMethod.GenericArguments.Add(variable);
worker.Emit(OpCodes.Call, genericInstanceMethod);
worker.Emit(OpCodes.Stloc_0);

View File

@ -164,7 +164,7 @@ public static bool Weave(AssemblyDefinition asmDef)
System.Diagnostics.Stopwatch rwstopwatch = System.Diagnostics.Stopwatch.StartNew();
// Need to track modified from ReaderWriterProcessor too because it could find custom read/write functions or create functions for NetworkMessages
bool modified = ReaderWriterProcessor.Process(CurrentAssembly);
bool modified = ReaderWriterProcessor.Process(CurrentAssembly, weaverTypes);
rwstopwatch.Stop();
Console.WriteLine($"Find all reader and writers took {rwstopwatch.ElapsedMilliseconds} milliseconds");