static void TryWeaveAssembly(string assemblyAssetPath)
        {
            var settings = ReflectionBakingInternalUtil.TryGetEnabledSettingsInstance();

            if (settings == null)
            {
                return;
            }

            if (settings.AllGeneratedAssemblies && settings.ExcludeAssemblies.Contains(assemblyAssetPath))
            {
                return;
            }

            if (!settings.AllGeneratedAssemblies && !settings.IncludeAssemblies.Contains(assemblyAssetPath))
            {
                return;
            }

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var assemblyFullPath = ReflectionBakingInternalUtil.ConvertAssetPathToSystemPath(assemblyAssetPath);

            var readerParameters = new ReaderParameters
            {
                AssemblyResolver = new UnityAssemblyResolver(),
            };

            var module = ModuleDefinition.ReadModule(assemblyFullPath, readerParameters);

            var assemblyRefNames = module.AssemblyReferences.Select(x => x.Name.ToLower()).ToList();

            if (!assemblyRefNames.Contains("zenject-usage"))
            {
                // Zenject-usage is used by the generated methods
                // Important that we do this check otherwise we can corrupt some dlls that don't have access to it
                return;
            }

            var assemblyName = Path.GetFileNameWithoutExtension(assemblyAssetPath);
            var assembly     = AppDomain.CurrentDomain.GetAssemblies()
                               .Where(x => x.GetName().Name == assemblyName).OnlyOrDefault();

            Assert.IsNotNull(assembly, "Could not find unique assembly '{0}' in currently loaded list of assemblies", assemblyName);

            int numTypesChanged = ReflectionBakingModuleEditor.WeaveAssembly(
                module, assembly, settings.NamespacePatterns);

            if (numTypesChanged > 0)
            {
                module.Write(assemblyFullPath, new WriterParameters());

                Debug.Log("Added reflection baking to '{0}' types in assembly '{1}', took {2:0.00} seconds"
                          .Fmt(numTypesChanged, Path.GetFileName(assemblyAssetPath), stopwatch.Elapsed.TotalSeconds));
            }
        }
Exemplo n.º 2
0
        static int Main(string[] args)
        {
            List <string> namespacePatterns;
            List <string> searchDirectories;

            if (!TryParseArgs(args, out namespacePatterns, out searchDirectories))
            {
                Console.WriteLine("Invalid Arguments specified.  Usage: {0} -d \"DLL Search Directory 1\" -d \"DLL Search Directory 2\" -p \"Namespace Regex Pattern 1\" -p \"Namespace Regex Pattern 2\"...", System.AppDomain.CurrentDomain.FriendlyName);
                return(1);
            }

            var resolver = new DefaultAssemblyResolver();

            foreach (var path in searchDirectories)
            {
                resolver.AddSearchDirectory(path);
            }

            var readerParams = new ReaderParameters()
            {
                AssemblyResolver = resolver,
            };

            var writerParams = new WriterParameters()
            {
            };

            foreach (var assemblyPath in searchDirectories.SelectMany(path => Directory.GetFiles(path, "*.dll")))
            {
                var module = ModuleDefinition.ReadModule(assemblyPath, readerParams);

                var assembly = Assembly.Load(File.ReadAllBytes(assemblyPath));

                int numChanges = ReflectionBakingModuleEditor.WeaveAssembly(
                    module, assembly, namespacePatterns);

                if (numChanges > 0)
                {
                    module.Write(assemblyPath, writerParams);
                    Console.WriteLine("Changed {0} types in assembly '{1}'", numChanges, module.Name);
                }
            }

            return(0);
        }