Exemple #1
0
        private void OnEnable()
        {
            if (m_Log == null)
            {
                m_Log = new Log(this);
            }
            if (m_Components == null)
            {
                m_Components = new ComponentController();
            }
            if (m_WeavedAssemblies == null)
            {
                m_WeavedAssemblies = new List <WeavedAssembly>();
            }

            m_Components.SetOwner(this);
            m_RequiredScriptingSymbols.ValidateSymbols();

            // Enable all our components
            for (int i = 0; i < m_WeavedAssemblies.Count; i++)
            {
                m_WeavedAssemblies[i].OnEnable();
            }
            m_Log.context = this;
            // Subscribe to the before reload event so we can modify the assemblies!
            m_Log.Info("Weaver Settings", "Subscribing to next assembly reload.", false);
            AssemblyUtility.PopulateAssemblyCache();
#if UNITY_2017_1_OR_NEWER
            AssemblyReloadEvents.beforeAssemblyReload += CheckForAssemblyModifications;
#else
            m_Log.Warning("Dynamic Assembly Reload not support until Unity 2017. Enter play mode to reload assemblies to see the effects of Weaving.", false);
#endif
        }
Exemple #2
0
        /// <summary>
        /// Takes in a collection of assemblies and starts the weaving process for
        /// all of them.
        /// </summary>
        private void WeaveAssemblies(IList <WeavedAssembly> assemblies)
        {
            try
            {
                m_Log.Info("Weaver Settings", "Populating Assembly Cache", false);
                AssemblyUtility.PopulateAssemblyCache();
                // Create new resolver
                m_Resolver = new WeaverAssemblyResolver();
                // Create a new reader
                m_Log.Info("Weaver Settings", "Creating Reader Parameters", false);
                ReaderParameters readerParameters = new ReaderParameters();
                // Pass the reader our resolver
                readerParameters.AssemblyResolver = m_Resolver;
                // Tell the reader to look at symbols so we can get line numbers for errors, warnings, and logs.
                readerParameters.ReadSymbols = true;
                // Create our writer
                WriterParameters writerParameters = new WriterParameters();
                // We do want to write our symbols
                writerParameters.WriteSymbols = true;
                // Create a list of definitions
                Collection <ModuleDefinition> editingModules = new Collection <ModuleDefinition>();
                for (int i = 0; i < assemblies.Count; i++)
                {
                    // We have a changed assembly so we need to get the definition to modify.
                    m_Log.Info("Weaver Settings", "Creating ModuleDefinition for <i>" + assemblies[i].relativePath + "</i>.", false);
                    ModuleDefinition moduleDefinition = ModuleDefinition.ReadModule(assemblies[i].GetSystemPath(), readerParameters);
                    // Add it to our list
                    editingModules.Add(moduleDefinition);
                }
                m_Log.Info("Weaver Settings", "Initializing Components.", false);
                // Initialize our component manager
                m_Components.Initialize(this);
                // Visit Modules
                m_Log.Info("Weaver Settings", "Visiting Modules.", false);
                m_Components.VisitModules(editingModules, m_Log);
                // Save
                for (int i = 0; i < assemblies.Count; i++)
                {
                    m_Log.Info("Weaver Settings", "Writing Module <i>" + assemblies[i].relativePath + "</i> to disk.", false);
                    editingModules[i].Write(assemblies[i].GetSystemPath(), writerParameters);
                    // remember write time of modified assembly
                    assemblies[i].HasChanges();
                }
                assemblies.Clear();
                m_Log.Info("Weaver Settings", "Weaving Successfully Completed", false);

                // Stats
                m_Log.Info("Statistics", "Weaving Time ms: " + m_Timer.ElapsedMilliseconds, false);
                m_Log.Info("Statistics", "Modules Visited: " + m_Components.totalModulesVisited, false);
                m_Log.Info("Statistics", "Types Visited: " + m_Components.totalTypesVisited, false);
                m_Log.Info("Statistics", "Methods Visited: " + m_Components.totalMethodsVisited, false);
                m_Log.Info("Statistics", "Fields Visited: " + m_Components.totalFieldsVisited, false);
                m_Log.Info("Statistics", "Properties Visited: " + m_Components.totalPropertiesVisited, false);
            }
            catch (Exception e)
            {
                throw e;
            }
        }