private static void Process(Assembly mainAssembly, Assembly editorAssembly, AssemblyDefinition engineAssemblyDefinition)
        {
            var readerParameters = new ReaderParameters
            {
                ReadWrite   = true,
                InMemory    = true,
                ReadingMode = ReadingMode.Immediate
            };

            var mainAssemblyDefinition = AssemblyDefinition.ReadAssembly(mainAssembly.Location, readerParameters);
            var mainModuleDefinition   = mainAssemblyDefinition.MainModule;

            var editorAssemblyDefinition = AssemblyDefinition.ReadAssembly(editorAssembly.Location);
            var editorModuleDefinition   = editorAssemblyDefinition.MainModule;

            var engineModuleDefinition = engineAssemblyDefinition.MainModule;

            var generateAttributeTypeDefinition = editorModuleDefinition.GetType("Mono_Cecil_Sample.Attributes", "GenerateAttribute");
            var generateAttributeFullName       = generateAttributeTypeDefinition.FullName;

            var typeDefinitions = mainModuleDefinition
                                  .Types
                                  .Where(x => x.CustomAttributes.Any(a => a.AttributeType.FullName == generateAttributeFullName))
                                  .ToArray();

            foreach (var classTypeDefinition in typeDefinitions)
            {
                classTypeDefinition.Methods.Add(DefineAwake(mainModuleDefinition, engineModuleDefinition));
                CecilUtility.RemoveAttribute(classTypeDefinition, generateAttributeFullName);
            }

            mainModuleDefinition.Write(mainAssembly.Location);
        }
Exemple #2
0
        private static void PostCompile()
        {
            EditorApplication.LockReloadAssemblies();
            try
            {
                var mainAssembly   = CecilUtility.GetAssembly("Assembly-CSharp").ToAssemblyDefinition();
                var editorAssembly = CecilUtility.GetAssembly("MCI").ToAssemblyDefinition();
                var engineAssembly = CecilUtility.EngineAssemblyDefinition();
                var modAssembly    = CreateAssembly();

                Process(mainAssembly, editorAssembly, engineAssembly, modAssembly);
            }
            finally
            {
                EditorApplication.UnlockReloadAssemblies();
            }
        }
        private static void PostCompile()
        {
            EditorApplication.LockReloadAssemblies();
            try
            {
                var mainAssembly   = CecilUtility.GetAssembly("Assembly-CSharp");
                var editorAssembly = CecilUtility.GetAssembly("MCI");
                var engineAssembly = EngineAssemblyDefinition();

                Process(mainAssembly, editorAssembly, engineAssembly);
            }
            finally
            {
                EditorApplication.UnlockReloadAssemblies();
            }

            AssemblyDefinition EngineAssemblyDefinition()
            {
                const string path = "C:\\Program Files\\Unity\\Editor\\Data\\Managed\\UnityEngine.dll";

                return(AssemblyDefinition.ReadAssembly(path));
            }
        }
Exemple #4
0
        private static void Process
        (
            AssemblyDefinition mainAssemblyDefinition,
            AssemblyDefinition editorAssemblyDefinition,
            AssemblyDefinition engineAssemblyDefinition,
            AssemblyDefinition modAssemblyDefinition
        )
        {
            var mainModuleDefinition   = mainAssemblyDefinition.MainModule;
            var editorModuleDefinition = editorAssemblyDefinition.MainModule;
            var engineModuleDefinition = engineAssemblyDefinition.MainModule;
            var modModuleDefinition    = modAssemblyDefinition.MainModule;

            var modAttributeTypeDefinition = editorModuleDefinition.GetType("Mono_Cecil_Sample.Attributes", "ModAttribute");
            var modAttributeFullName       = modAttributeTypeDefinition.FullName;

            var definitions = mainModuleDefinition
                              .Types
                              .Where(x => CecilUtility.IsExistAttributeInGlobal(x, modAttributeFullName))
                              .ToArray();

            TryInjectTypes(in definitions, modModuleDefinition);
            //modModuleDefinition.Write("Mod.dll");
        }