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

            if (settings == null)
            {
                return(false);
            }

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var runner = new ReflectionBakingRunner(settings);

            int numTypesChanged = runner.Run(assemblyAssetPath);

            stopwatch.Start();

            if (numTypesChanged > 0)
            {
                UnityEngine.Debug.Log("Added reflection baking to '{0}' types in assembly '{1}', took {2:0.00} seconds"
                                      .Fmt(numTypesChanged, Path.GetFileName(assemblyAssetPath), stopwatch.Elapsed.TotalSeconds));
                return(true);
            }

            return(false);
        }
        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));
            }
        }
        void ReadModule(string assemblyAssetPath)
        {
            var readerParameters = new ReaderParameters()
            {
                AssemblyResolver = new UnityAssemblyResolver(),
                // Tell the reader to look at symbols so we can get line numbers for errors, warnings, and logs.
                ReadSymbols = true,
            };

            Assert.IsNull(_module);
            Assert.IsNull(_assemblyFullPath);

            _assemblyFullPath = ReflectionBakingInternalUtil.ConvertAssetPathToSystemPath(assemblyAssetPath);
            _module           = ModuleDefinition.ReadModule(_assemblyFullPath, readerParameters);

            var assemblyName = Path.GetFileNameWithoutExtension(assemblyAssetPath);

            _assembly = AppDomain.CurrentDomain.GetAssemblies()
                        .Where(x => x.GetName().Name == assemblyName).SingleOrDefault();

            Assert.IsNotNull(_assembly, "Could not find assembly '{0}' in currently loaded list of assemblies", assemblyName);
        }
Example #4
0
        public override void OnInspectorGUI()
        {
            EditorGUI.BeginChangeCheck();
            {
                GUILayout.Label("Settings", EditorStyles.boldLabel);

                EditorGUILayout.PropertyField(_isEnabledInBuilds, true);

                var oldIsEnabledInEditorValue = _isEnabledInEditor.boolValue;
                EditorGUILayout.PropertyField(_isEnabledInEditor, true);

                if (oldIsEnabledInEditorValue != _isEnabledInEditor.boolValue)
                {
                    ReflectionBakingInternalUtil.TryForceUnityFullCompile();
                }

#if !UNITY_2018
                if (_isEnabledInEditor.boolValue)
                {
                    EditorGUILayout.HelpBox(
                        "Reflection baking inside unity editor requires Unity 2018+!  It is however supported for builds", MessageType.Error);
                }
#endif
                EditorGUILayout.PropertyField(_allGeneratedAssemblies, true);

                if (_allGeneratedAssemblies.boolValue)
                {
                    _excludeAssembliesList.DoLayoutList();

                    GUI.enabled = false;

                    try
                    {
                        _includeAssembliesList.DoLayoutList();
                    }
                    finally
                    {
                        GUI.enabled = true;
                    }
                }
                else
                {
                    GUI.enabled = false;

                    try
                    {
                        _excludeAssembliesList.DoLayoutList();
                    }
                    finally
                    {
                        GUI.enabled = true;
                    }

                    _includeAssembliesList.DoLayoutList();
                }

                _namespacePatternsList.DoLayoutList();
            }

            if (EditorGUI.EndChangeCheck())
            {
                _hasModifiedProperties = true;
            }

            if (_hasModifiedProperties)
            {
                _hasModifiedProperties = false;
                ApplyModifiedProperties();
            }
        }