From 4c6af1ff6fbc22017ac92b7eb0d2220089ad42e3 Mon Sep 17 00:00:00 2001 From: vis2k Date: Wed, 18 Aug 2021 23:24:30 +0800 Subject: [PATCH] Readers: remove static weaverTypes references --- .../Weaver/Processors/ReaderWriterProcessor.cs | 4 ++-- Assets/Mirror/Editor/Weaver/Readers.cs | 18 ++++++++++++------ Assets/Mirror/Editor/Weaver/Weaver.cs | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Assets/Mirror/Editor/Weaver/Processors/ReaderWriterProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/ReaderWriterProcessor.cs index 15c26a4e3..5248fec05 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/ReaderWriterProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/ReaderWriterProcessor.cs @@ -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()) { diff --git a/Assets/Mirror/Editor/Weaver/Readers.cs b/Assets/Mirror/Editor/Weaver/Readers.cs index ca48cb91e..86892feba 100644 --- a/Assets/Mirror/Editor/Weaver/Readers.cs +++ b/Assets/Mirror/Editor/Weaver/Readers.cs @@ -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 readFuncs = new Dictionary(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($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())); + readerFunc.Parameters.Add(new ParameterDefinition("reader", ParameterAttributes.None, weaverTypes.Import())); 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())); + worker.Emit(OpCodes.Call, GetReadFunc(weaverTypes.Import())); 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()) { - 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); diff --git a/Assets/Mirror/Editor/Weaver/Weaver.cs b/Assets/Mirror/Editor/Weaver/Weaver.cs index 880428103..767bf5df8 100644 --- a/Assets/Mirror/Editor/Weaver/Weaver.cs +++ b/Assets/Mirror/Editor/Weaver/Weaver.cs @@ -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");