// find all readers and writers and register them public static void ProcessReadersAndWriters(AssemblyDefinition CurrentAssembly) { Readers.Init(); Writers.Init(); foreach (Assembly unityAsm in CompilationPipeline.GetAssemblies()) { if (unityAsm.name != CurrentAssembly.Name.Name) { try { using (DefaultAssemblyResolver asmResolver = new DefaultAssemblyResolver()) using (AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(unityAsm.outputPath, new ReaderParameters { ReadWrite = false, ReadSymbols = false, AssemblyResolver = asmResolver })) { ProcessAssemblyClasses(CurrentAssembly, assembly); } } catch (FileNotFoundException) { // During first import, this gets called before some assemblies // are built, just skip them } } } ProcessAssemblyClasses(CurrentAssembly, CurrentAssembly); }
public static bool Process(ModuleDefinition module, Assembly unityAssembly) { // darn global state causing bugs Readers.Init(); Writers.Init(); messages.Clear(); foreach (Assembly unityAsm in unityAssembly.assemblyReferences) { if (unityAsm.name == "Mirror") { using (var asmResolver = new DefaultAssemblyResolver()) using (var assembly = AssemblyDefinition.ReadAssembly(unityAsm.outputPath, new ReaderParameters { ReadWrite = false, ReadSymbols = false, AssemblyResolver = asmResolver })) { ProcessAssemblyClasses(module, assembly.MainModule); } } } int writeCount = Writers.Count; int readCount = Readers.Count; ProcessAssemblyClasses(module, module); return(Writers.Count != writeCount || Readers.Count != readCount); }
public static bool Process(AssemblyDefinition CurrentAssembly) { Readers.Init(); Writers.Init(); foreach (Assembly unityAsm in CompilationPipeline.GetAssemblies()) { if (unityAsm.name == "Mirror") { using (DefaultAssemblyResolver asmResolver = new DefaultAssemblyResolver()) using (AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(unityAsm.outputPath, new ReaderParameters { ReadWrite = false, ReadSymbols = false, AssemblyResolver = asmResolver })) { ProcessAssemblyClasses(CurrentAssembly, assembly); } } } return(ProcessAssemblyClasses(CurrentAssembly, CurrentAssembly)); }
public static void Process(AssemblyDefinition CurrentAssembly, Assembly unityAssembly) { // darn global state causing bugs Weaver.WeaveLists.generateContainerClass = null; Readers.Init(); Writers.Init(); foreach (Assembly unityAsm in unityAssembly.assemblyReferences) { if (unityAsm.name == "Mirror") { using (var asmResolver = new DefaultAssemblyResolver()) using (var assembly = AssemblyDefinition.ReadAssembly(unityAsm.outputPath, new ReaderParameters { ReadWrite = false, ReadSymbols = false, AssemblyResolver = asmResolver })) { ProcessAssemblyClasses(CurrentAssembly, assembly); } } } ProcessAssemblyClasses(CurrentAssembly, CurrentAssembly); }
static bool Weave(string assName, IEnumerable <string> dependencies, IAssemblyResolver assemblyResolver, string unityEngineDLLPath, string mirrorNetDLLPath, string outputDir) { using (DefaultAssemblyResolver asmResolver = new DefaultAssemblyResolver()) using (CurrentAssembly = AssemblyDefinition.ReadAssembly(assName, new ReaderParameters { ReadWrite = true, ReadSymbols = true, AssemblyResolver = asmResolver })) { asmResolver.AddSearchDirectory(Path.GetDirectoryName(assName)); asmResolver.AddSearchDirectory(Helpers.UnityEngineDLLDirectoryName()); asmResolver.AddSearchDirectory(Path.GetDirectoryName(unityEngineDLLPath)); asmResolver.AddSearchDirectory(Path.GetDirectoryName(mirrorNetDLLPath)); if (dependencies != null) { foreach (string path in dependencies) { asmResolver.AddSearchDirectory(path); } } SetupTargetTypes(); Readers.Init(CurrentAssembly); Writers.Init(CurrentAssembly); ModuleDefinition moduleDefinition = CurrentAssembly.MainModule; Console.WriteLine("Script Module: {0}", moduleDefinition.Name); // Process each NetworkBehaviour bool didWork = false; // We need to do 2 passes, because SyncListStructs might be referenced from other modules, so we must make sure we generate them first. for (int pass = 0; pass < 2; pass++) { System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew(); foreach (TypeDefinition td in moduleDefinition.Types) { if (td.IsClass && td.BaseType.CanBeResolved()) { try { if (pass == 0) { didWork |= CheckSyncList(td); } else { didWork |= CheckNetworkBehaviour(td); didWork |= CheckMessageBase(td); } } catch (Exception ex) { Weaver.Error(ex.Message); throw ex; } } if (WeavingFailed) { return(false); } } watch.Stop(); Console.WriteLine("Pass: "******" took " + watch.ElapsedMilliseconds + " milliseconds"); } if (didWork) { // this must be done for ALL code, not just NetworkBehaviours try { PropertySiteProcessor.ProcessSitesModule(CurrentAssembly.MainModule); } catch (Exception e) { Log.Error("ProcessPropertySites exception: " + e); return(false); } if (WeavingFailed) { //Log.Error("Failed phase II."); return(false); } // write to outputDir if specified, otherwise perform in-place write WriterParameters writeParams = new WriterParameters { WriteSymbols = true }; if (outputDir != null) { CurrentAssembly.Write(Helpers.DestinationFileFor(outputDir, assName), writeParams); } else { CurrentAssembly.Write(writeParams); } } } return(true); }
static bool Weave(string assName, IEnumerable <string> dependencies, IAssemblyResolver assemblyResolver, string unityEngineDLLPath, string mirrorNetDLLPath, string outputDir) { ReaderParameters readParams = Helpers.ReaderParameters(assName, dependencies, assemblyResolver, unityEngineDLLPath, mirrorNetDLLPath); using (CurrentAssembly = AssemblyDefinition.ReadAssembly(assName, readParams)) { SetupTargetTypes(); Readers.Init(CurrentAssembly); Writers.Init(CurrentAssembly); ModuleDefinition moduleDefinition = CurrentAssembly.MainModule; Console.WriteLine("Script Module: {0}", moduleDefinition.Name); // Process each NetworkBehaviour bool didWork = false; // We need to do 2 passes, because SyncListStructs might be referenced from other modules, so we must make sure we generate them first. for (int pass = 0; pass < 2; pass++) { System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew(); foreach (TypeDefinition td in moduleDefinition.Types) { if (td.IsClass && td.BaseType.CanBeResolved()) { try { if (pass == 0) { didWork |= CheckSyncList(td); } else { didWork |= CheckNetworkBehaviour(td); didWork |= CheckMessageBase(td); } } catch (Exception ex) { if (CurrentAssembly.MainModule.SymbolReader != null) { CurrentAssembly.MainModule.SymbolReader.Dispose(); } Weaver.Error(ex.Message); throw ex; } } if (WeavingFailed) { if (CurrentAssembly.MainModule.SymbolReader != null) { CurrentAssembly.MainModule.SymbolReader.Dispose(); } return(false); } } watch.Stop(); Console.WriteLine("Pass: "******" took " + watch.ElapsedMilliseconds + " milliseconds"); } if (didWork) { // this must be done for ALL code, not just NetworkBehaviours try { PropertySiteProcessor.ProcessSitesModule(CurrentAssembly.MainModule); } catch (Exception e) { Log.Error("ProcessPropertySites exception: " + e); if (CurrentAssembly.MainModule.SymbolReader != null) { CurrentAssembly.MainModule.SymbolReader.Dispose(); } return(false); } if (WeavingFailed) { //Log.Error("Failed phase II."); if (CurrentAssembly.MainModule.SymbolReader != null) { CurrentAssembly.MainModule.SymbolReader.Dispose(); } return(false); } string dest = Helpers.DestinationFileFor(outputDir, assName); //Console.WriteLine ("Output:" + dest); WriterParameters writeParams = Helpers.GetWriterParameters(readParams); CurrentAssembly.Write(dest, writeParams); } if (CurrentAssembly.MainModule.SymbolReader != null) { CurrentAssembly.MainModule.SymbolReader.Dispose(); } } return(true); }