예제 #1
0
        public AssemblyDefinition Weave(ICompiledAssembly compiledAssembly)
        {
            try
            {
                CurrentAssembly = AssemblyDefinitionFor(compiledAssembly);

                ModuleDefinition module = CurrentAssembly.MainModule;
                readers = new Readers(module, logger);
                writers = new Writers(module, logger);
                var rwstopwatch = System.Diagnostics.Stopwatch.StartNew();
                propertySiteProcessor = new PropertySiteProcessor();
                var rwProcessor = new ReaderWriterProcessor(module, readers, writers);

                bool modified = rwProcessor.Process();
                rwstopwatch.Stop();
                Console.WriteLine($"Find all reader and writers took {rwstopwatch.ElapsedMilliseconds} milliseconds");

                Console.WriteLine($"Script Module: {module.Name}");

                modified |= WeaveModule(module);

                if (!modified)
                    return CurrentAssembly;

                rwProcessor.InitializeReaderAndWriters();

                return CurrentAssembly;
            }
            catch (Exception e)
            {
                logger.Error("Exception :" + e);
                return null;
            }
        }
        public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
        {
            if (!WillProcess(compiledAssembly))
            {
                return(null);
            }

            var assemblyDefinition = AssemblyDefinitionFor(compiledAssembly);
            var postProcessors     = FindAllEntitiesILPostProcessors();

            var  diagnostics   = new List <DiagnosticMessage>();
            bool madeAnyChange = false;

            foreach (var postProcessor in postProcessors)
            {
                diagnostics.AddRange(postProcessor.PostProcess(assemblyDefinition, out var madeChange));
                madeAnyChange |= madeChange;
            }
            ;

            if (!madeAnyChange || diagnostics.Any(d => d.DiagnosticType == DiagnosticType.Error))
            {
                return(new ILPostProcessResult(null, diagnostics));
            }

            var pe  = new MemoryStream();
            var pdb = new MemoryStream();
            var writerParameters = new WriterParameters
            {
                SymbolWriterProvider = new PortablePdbWriterProvider(), SymbolStream = pdb, WriteSymbols = true
            };

            assemblyDefinition.Write(pe, writerParameters);
            return(new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()), diagnostics));
        }
예제 #3
0
        public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
        {
            if (!WillProcess(compiledAssembly))
            {
                return(null);
            }

            Defines = compiledAssembly.Defines;

            var assemblyDefinition = EntitiesILPostProcessors.AssemblyDefinitionFor(compiledAssembly);
            var diagnostics        = new List <DiagnosticMessage>();

            bool anythingChanged = GenerateReflectionDataSetup(assemblyDefinition, diagnostics);

            if (!anythingChanged)
            {
                return(new ILPostProcessResult(null, diagnostics));
            }

            var pe  = new MemoryStream();
            var pdb = new MemoryStream();
            var writerParameters = new WriterParameters
            {
                SymbolWriterProvider = new PortablePdbWriterProvider(), SymbolStream = pdb, WriteSymbols = true
            };

            assemblyDefinition.Write(pe, writerParameters);
            return(new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()), diagnostics));
        }
        public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
        {
            if (!WillProcess(compiledAssembly))
            {
                return(new ILPostProcessResult(null, null));
            }

            var assemblyDefinition = AssemblyDefinitionFor(compiledAssembly);

            ModuleDefinition mainModule = assemblyDefinition.MainModule;

            var codegen = new JsInteropCodegen(mainModule);

            codegen.ProcessAssembly();

            var pe  = new MemoryStream();
            var pdb = new MemoryStream();
            var writerParameters = new WriterParameters
            {
                SymbolWriterProvider = new PortablePdbWriterProvider(), SymbolStream = pdb, WriteSymbols = true
            };

            assemblyDefinition.Write(pe, writerParameters);
            return(new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()), null));
        }
예제 #5
0
        public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
        {
            if (!WillProcess(compiledAssembly))
            {
                return(null);
            }

            var assemblyDefinition = Utils.LoadAssemblyDefinition(compiledAssembly);

            var generator = new InjectionILGenerator(
                assemblyDefinition.MainModule,
                VContainerSettings.Instance.CodeGen.Namespaces);

            if (generator.TryGenerate(out var diagnosticMessages))
            {
                if (diagnosticMessages.Any(d => d.DiagnosticType == DiagnosticType.Error))
                {
                    return(new ILPostProcessResult(null, diagnosticMessages));
                }

                var pe  = new MemoryStream();
                var pdb = new MemoryStream();
                var writerParameters = new WriterParameters
                {
                    SymbolWriterProvider = new PortablePdbWriterProvider(),
                    SymbolStream         = pdb,
                    WriteSymbols         = true
                };

                assemblyDefinition.Write(pe, writerParameters);

                return(new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()), diagnosticMessages));
            }
            return(new ILPostProcessResult(null, diagnosticMessages));
        }
예제 #6
0
        public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
        {
            var logger = new ILPostProcessorLogger(new List <DiagnosticMessage>());

            logger.Debug($"process {compiledAssembly.Name}({string.Join("|", compiledAssembly.References.Select(Path.GetFileName).Where(f => !new [] {"System", "Mono", "mscorlib", "netstandard", "Microsoft", "Unity", "UnityEngine"}.Any(f.StartsWith)))})");

            using var resolver           = new PostProcessorAssemblyResolver(compiledAssembly.References);
            using var assemblyDefinition = compiledAssembly.LoadAssembly(resolver);

            var modified = false;

            var nodes = assemblyDefinition.MainModule.GetAllTypes()
                        .Where(type => type.IsClass && !type.IsAbstract && type.TypeImplements(typeof(INodeData)))
                        .ToArray()
            ;

            var methods = nodes.SelectMany(FetchNodeDataMethods)
                          .Where(method => method != null && !method.CustomAttributes.FindAccessorAttributes().Any())
            ;

            foreach (var method in methods)
            {
                var attributes = method.GenerateAccessorAttributes();
                if (!attributes.Any())
                {
                    continue;
                }

                modified = true;
                method.CustomAttributes.AddRange(attributes);
            }

            return(modified ? assemblyDefinition.Write(logger.Messages) : new ILPostProcessResult(null, logger.Messages));
        }
예제 #7
0
        public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
        {
            if (!WillProcess(compiledAssembly))
            {
                return(null);
            }

            var assemblyDefinition = AssemblyDefinitionFor(compiledAssembly);

            var ilSupportType = assemblyDefinition.MainModule.Types.FirstOrDefault(t => t.FullName == "Unity.Collections.LowLevel.Unsafe.ILSupport");

            if (ilSupportType == null)
            {
                throw new InvalidOperationException();
            }

            InjectUtilityAddressOfIn(ilSupportType);
            InjectUtilityAsRefIn(ilSupportType);

            var pe  = new MemoryStream();
            var pdb = new MemoryStream();
            var writerParameters = new WriterParameters
            {
                SymbolWriterProvider = new PortablePdbWriterProvider(), SymbolStream = pdb, WriteSymbols = true
            };

            assemblyDefinition.Write(pe, writerParameters);
            return(new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray())));
        }
 public PostProcessorAssemblyResolver(ICompiledAssembly compiledAssembly)
 {
     _compiledAssembly   = compiledAssembly;
     _assemblyReferences = compiledAssembly.References;
     // cache paths here so we dont need to call it each time we resolve
     _assemblyReferencesFileName = _assemblyReferences.Select(r => Path.GetFileName(r)).ToArray();
 }
예제 #9
0
        public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
        {
            bool   willProcess = WillProcess(compiledAssembly);
            string logText     = willProcess ? "Processing" : "Skipping";

            Log($"{logText} {compiledAssembly.Name}");
            if (!willProcess)
            {
                return(null);
            }

            var logger = new WeaverLogger();
            var weaver = new Weaver(logger);

            var assemblyDefinition = weaver.Weave(compiledAssembly);

            // write
            var pe  = new MemoryStream();
            var pdb = new MemoryStream();

            var writerParameters = new WriterParameters
            {
                SymbolWriterProvider = new PortablePdbWriterProvider(),
                SymbolStream         = pdb,
                WriteSymbols         = true
            };

            assemblyDefinition?.Write(pe, writerParameters);

            logText = assemblyDefinition != null ? "Success" : "Failed";
            Log($"{logText} {compiledAssembly.Name}");
            return(new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()), logger.Diagnostics));
        }
예제 #10
0
        public override bool WillProcess(ICompiledAssembly compiledAssembly)
        {
            if (configReader == null)
            {
                configReader = new ConfigReader();
            }
            if (!configReader.ExecuteRemoveOthers)
            {
                return(false);
            }

            //System.IO.File.AppendAllText("debug.txt", "Compile:" + compiledAssembly.Name + "\n");
            if (!compiledAssembly.Name.StartsWith("Unity.") &&
                !compiledAssembly.Name.StartsWith("UnityEngine.") &&
                !compiledAssembly.Name.StartsWith("UnityEditor."))
            {
                return(false);
            }


            if (compiledAssembly.Name == "UTJ.StripVariant")
            {
                return(false);
            }
            var isForEditor = compiledAssembly.Defines?.Contains("UNITY_EDITOR") ?? false;

            // Debugクラス読み込み前なので呼ぶと死ぬ
            // Debug.Log("ILPostProcess " + compiledAssembly.Name + "::" + isForEditor);
            //System.IO.File.WriteAllText(compiledAssembly.Name + ".txt", "AA:" + compiledAssembly.Name);
            if (!isForEditor)
            {
                return(false);
            }
            return(true);
        }
예제 #11
0
        public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
        {
            bool willProcess = WillProcess(compiledAssembly);

            Console.WriteLine($"Mirage ILPP: Checking: {compiledAssembly.Name} will process: {willProcess}");
            if (!willProcess)
            {
                return(null);
            }

            var logger = new Logger();
            var weaver = new Weaver(logger);

            Console.WriteLine($"Mirage ILPP: Weave Started on {compiledAssembly.Name}");
            AssemblyDefinition assemblyDefinition = weaver.Weave(compiledAssembly);

            Console.WriteLine($"Mirage ILPP: Weave Finished on {compiledAssembly.Name}");

            // write
            var pe  = new MemoryStream();
            var pdb = new MemoryStream();

            var writerParameters = new WriterParameters
            {
                SymbolWriterProvider = new PortablePdbWriterProvider(),
                SymbolStream         = pdb,
                WriteSymbols         = true
            };

            assemblyDefinition?.Write(pe, writerParameters);

            return(new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()), logger.Diagnostics));
        }
예제 #12
0
        static AssemblyDefinition CreateAssemblyDefinition(ICompiledAssembly compiledAssembly)
        {
            var resolver = new AssemblyResolver(compiledAssembly);

            var readerParameters = new ReaderParameters
            {
                AssemblyResolver = resolver,
                ReadingMode      = ReadingMode.Deferred,

                // We _could_ be running in .NET core. In this case we need to force imports to resolve to mscorlib.
                ReflectionImporterProvider = new PostProcessorReflectionImporterProvider()
            };

            if (null != compiledAssembly.InMemoryAssembly.PdbData)
            {
                readerParameters.ReadSymbols          = true;
                readerParameters.SymbolStream         = new MemoryStream(compiledAssembly.InMemoryAssembly.PdbData.ToArray());
                readerParameters.SymbolReaderProvider = new PortablePdbReaderProvider();
            }

            var peStream           = new MemoryStream(compiledAssembly.InMemoryAssembly.PeData.ToArray());
            var assemblyDefinition = AssemblyDefinition.ReadAssembly(peStream, readerParameters);

            resolver.AddAssemblyDefinitionBeingOperatedOn(assemblyDefinition);

            return(assemblyDefinition);
        }
예제 #13
0
        public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
        {
            if (!WillProcess(compiledAssembly))
            {
                return(null);
            }

            var logger = new Logger();
            var weaver = new Weaver(logger);

            AssemblyDefinition assemblyDefinition = weaver.Weave(compiledAssembly);

            // write
            var pe  = new MemoryStream();
            var pdb = new MemoryStream();

            var writerParameters = new WriterParameters
            {
                SymbolWriterProvider = new PortablePdbWriterProvider(),
                SymbolStream         = pdb,
                WriteSymbols         = true
            };

            assemblyDefinition?.Write(pe, writerParameters);

            return(new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()), logger.Diagnostics));
        }
예제 #14
0
 public ILProcessResolver(ICompiledAssembly compiledAssembly)
 {
     m_DefaultAssemblyResolver = new DefaultAssemblyResolver();
     m_CompiledAssembly        = compiledAssembly;
     m_AssemblyReferences      = compiledAssembly.References;
     //System.IO.File.AppendAllText("debuglog.txt", "Start::" + compiledAssembly.Name + "(" + compiledAssembly.References.Length + "\n");
 }
        static bool ShouldProcess(ICompiledAssembly compiledAssembly)
        {
            if (!compiledAssembly.RequiresCodegen())
            {
                return(false);
            }

            var assemblyName = compiledAssembly.Name;

            if (assemblyName == CodeGenUtils.ExternalPropertyBagAssemblyName)
            {
                return(false);
            }

            // TODO: Debug type load exception and other assembly-specific issues
            if (k_IgnoredAssemblies.Contains(assemblyName))
            {
                return(false);
            }

            if (RuntimeSerializationSettingsUtils.GetAssemblyExceptions().Contains(assemblyName))
            {
                return(false);
            }

            if (CodeGenUtils.IsTestAssembly(compiledAssembly))
            {
                return(false);
            }

            return(true);
        }
예제 #16
0
        public override bool WillProcess(ICompiledAssembly compiledAssembly)
        {
            var name = compiledAssembly.Name;

            // Exclude based on name.
            return(!s_ExcludeIfAssemblyNameContains.Any(x => name.Contains(x)));
        }
예제 #17
0
        public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
        {
            if (!WillProcess(compiledAssembly))
            {
                return(null);
            }

            var sw = Stopwatch.StartNew();

            var assemblyDefinition = AssemblyDefinitionFor(compiledAssembly);
            var diagnostics        = new List <DiagnosticMessage>();

            diagnostics.AddRange(new AtomWeaverV2().Weave(assemblyDefinition, out var madeAnyChange));

            if (!madeAnyChange || diagnostics.Any(d => d.DiagnosticType == DiagnosticType.Error))
            {
                return(new ILPostProcessResult(null, diagnostics));
            }

            var pe  = new MemoryStream();
            var pdb = new MemoryStream();
            var writerParameters = new WriterParameters
            {
                SymbolWriterProvider = new PortablePdbWriterProvider(), SymbolStream = pdb, WriteSymbols = true
            };

            assemblyDefinition.Write(pe, writerParameters);

#if UNIMOB_CODEGEN_LOGGING_ENABLED
            UnityEngine.Debug.Log($"Weaved {compiledAssembly.Name} in {sw.ElapsedMilliseconds}ms");
#endif

            return(new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()), diagnostics));
        }
예제 #18
0
        public override bool WillProcess(ICompiledAssembly compiledAssembly)
        {
            var referenceDlls = compiledAssembly.References
                                .Select(Path.GetFileNameWithoutExtension);

            return(referenceDlls.Any(x => x == "VContainer") &&
                   referenceDlls.Any(x => x == "VContainer.EnableCodeGen"));
        }
예제 #19
0
 public override bool WillProcess(ICompiledAssembly compiledAssembly)
 {
     if (compiledAssembly.Name == "Unity.Collections.LowLevel.ILSupport")
     {
         return(true);
     }
     return(false);
 }
예제 #20
0
 public InjectionILGenerator(
     ModuleDefinition module,
     ICompiledAssembly compiledAssembly,
     IList <string> targetNamespaces = null)
 {
     this.module           = module;
     this.compiledAssembly = compiledAssembly;
     this.targetNamespaces = targetNamespaces;
 }
예제 #21
0
        static bool ShouldProcess(ICompiledAssembly compiledAssembly)
        {
            if (!compiledAssembly.RequiresCodegen())
            {
                return(false);
            }

            return(compiledAssembly.Name == CodeGenUtils.ExternalPropertyBagAssemblyName);
        }
        static bool ShouldProcess(ICompiledAssembly compiledAssembly)
        {
            if (!compiledAssembly.RequiresCodegen())
            {
                return(false);
            }

            return(compiledAssembly.Name == k_SerializationAssemblyName);
        }
 public override bool WillProcess(ICompiledAssembly compiledAssembly)
 {
     if (compiledAssembly.Name == "Unity.Entities")
     {
         return(true);
     }
     return(compiledAssembly.References.Any(f => Path.GetFileName(f) == "Unity.Entities.dll") &&
            !compiledAssembly.Name.Contains("CodeGen.Tests"));
 }
예제 #24
0
        public override bool WillProcess(ICompiledAssembly compiledAssembly)
        {
            var settings = VContainerSettings.Instance;

            return(settings != null &&
                   settings.CodeGen.Enabled &&
                   settings.CodeGen.AssemblyNames.Contains(compiledAssembly.Name) &&
                   compiledAssembly.References.Any(x => x.EndsWith("VContainer.dll")));
        }
예제 #25
0
        public ILPostProcessResult PostProcessInternal(ICompiledAssembly compiledAssembly)
        {
            AssemblyDefinition assemblyDefinition;

#if MEDICINE_IL_DEBUG
            using (NonAlloc.Benchmark.Start($"GetAssemblyDefinition ({compiledAssembly.Name})"))
#endif
            assemblyDefinition = PostProcessorAssemblyResolver.GetAssemblyDefinitionFor(compiledAssembly);

            try
            {
                CecilExtensions.CurrentModule = assemblyDefinition.MainModule;
                PostProcessorContext context;
#if MEDICINE_IL_DEBUG
                using (NonAlloc.Benchmark.Start($"CreatePostProcessorContext ({compiledAssembly.Name})"))
#endif
                context = new PostProcessorContext(assemblyDefinition.MainModule);

#if MEDICINE_IL_DEBUG
                using (NonAlloc.Benchmark.Start($"MedicineInjection ({compiledAssembly.Name})"))
#endif
                new InjectionPostProcessor(context).ProcessAssembly();

                var pe  = new MemoryStream(capacity: 1024 * 64);
                var pdb = new MemoryStream(capacity: 1024 * 16);

                var writerParameters = new WriterParameters
                {
                    SymbolWriterProvider = new PortablePdbWriterProvider(),
                    SymbolStream         = pdb,
                    WriteSymbols         = true,
                };

                assemblyDefinition.Write(pe, writerParameters);
                var inMemoryAssembly = new InMemoryAssembly(pe.ToArray(), pdb.ToArray());

                return(new ILPostProcessResult(inMemoryAssembly, context.DiagnosticMessages));
            }
            catch (Exception ex)
            {
                var error = new DiagnosticMessage
                {
                    MessageData    = $"Unexpected exception while post-processing assembly {compiledAssembly.Name}:\n{ex}",
                    DiagnosticType = DiagnosticType.Error,
                };
                return(new ILPostProcessResult(compiledAssembly.InMemoryAssembly, new List <DiagnosticMessage> {
                    error
                }));
            }
            finally
            {
                CecilExtensions.CurrentModule.Dispose();
                CecilExtensions.CurrentModule = null;
            }
        }
예제 #26
0
        public override bool WillProcess(ICompiledAssembly compiledAssembly)
        {
            if (compiledAssembly.Name == _overrideAssemblyCSharp)
            {
                return(true);
            }
            var overrideDll = $"{_overrideAssemblyCSharp}.dll";
            var hasOverride = compiledAssembly.References.Any(@ref => @ref.EndsWith(overrideDll));

            return(compiledAssembly.Name == "Assembly-CSharp" && !hasOverride);
        }
예제 #27
0
        public override bool WillProcess(ICompiledAssembly compiledAssembly)
        {
            if (VContainerSettings.Instance is VContainerSettings settings)
            {
                return(settings.CodeGen.Enabled &&
                       settings.CodeGen.AssemblyNames.Contains(compiledAssembly.Name) &&
                       compiledAssembly.References.Any(x => x.EndsWith("VContainer.dll")));
            }

            return(false);
        }
        public override bool WillProcess(ICompiledAssembly compiledAssembly)
        {
            bool usesMirror =
                compiledAssembly.Name == RuntimeAssemblyName ||
                compiledAssembly.References.Any(filePath => Path.GetFileNameWithoutExtension(filePath) == RuntimeAssemblyName);

            bool usesMirrorEditor =
                compiledAssembly.References.Any(filePath => Path.GetFileNameWithoutExtension(filePath) == EditorAssemblyName);

            return(usesMirror);// && !usesMirrorEditor;
        }
예제 #29
0
 public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
 {
     try
     {
         return(ProcessBody(compiledAssembly));
     }catch (System.Exception e)
     {
         //System.IO.File.WriteAllText("" + compiledAssembly.Name + "_err.txt", e.Message + "\n" + e.Source + "\n" + e.StackTrace );
         return(new ILPostProcessResult(null, new List <DiagnosticMessage>()));
     }
 }
        public override bool WillProcess(ICompiledAssembly compiledAssembly)
        {
            for (int i = 0; i < compiledAssembly.References.Length; i++)
            {
                if (Path.GetFileNameWithoutExtension(compiledAssembly.References[i]) == RUNTIME_ASSEMBLY)
                {
                    return(true);
                }
            }

            return(false);
        }