コード例 #1
0
        /// <summary>
        /// Builds and Weaves an Assembly with references to unity engine and other asmdefs.
        /// <para>
        ///     NOTE: Does not write the weaved assemble to disk
        /// </para>
        /// </summary>
        public AssemblyDefinition Build(IWeaverLogger logger)
        {
            AssemblyDefinition assembly = null;

            // This will compile scripts with the same references as files in the asset folder.
            // This means that the dll will get references to all asmdef just as if it was the default "Assembly-CSharp.dll"
            var assemblyBuilder = new AssemblyBuilder(ProjectPathFile, sourceFiles.ToArray())
            {
                referencesOptions = ReferencesOptions.UseEngineModules
            };

            assemblyBuilder.buildFinished += delegate(string assemblyPath, CompilerMessage[] compilerMessages)
            {
#if !UNITY_2020_2_OR_NEWER
                CompilerMessages.AddRange(compilerMessages);
                foreach (CompilerMessage cm in compilerMessages)
                {
                    if (cm.type == CompilerMessageType.Error)
                    {
                        Debug.LogErrorFormat("{0}:{1} -- {2}", cm.file, cm.line, cm.message);
                        CompilerErrors = true;
                    }
                }
#endif

                // assembly builder does not call ILPostProcessor (WTF Unity?),  so we must invoke it ourselves.
                var compiledAssembly = new CompiledAssembly(assemblyPath, assemblyBuilder);

                var weaver = new Mirage.Weaver.Weaver(logger);

                assembly = weaver.Weave(compiledAssembly);
            };

            // Start build of assembly
            if (!assemblyBuilder.Build())
            {
                Debug.LogErrorFormat("Failed to start build of assembly {0}", assemblyBuilder.assemblyPath);
                return(assembly);
            }

            while (assemblyBuilder.status != AssemblyBuilderStatus.Finished)
            {
                System.Threading.Thread.Sleep(10);
            }

            return(assembly);
        }
コード例 #2
0
        private void buildFinished(string assemblyPath, CompilerMessage[] compilerMessages)
        {
            Log($"buildFinished for {OutputFile}");

            // in unity2020, ILPP runs automatically. but in unity2021 it does not
#if !UNITY_2020_2_OR_NEWER || UNITY_2021_3_OR_NEWER
            CompilerMessages.AddRange(compilerMessages);
            foreach (CompilerMessage m in compilerMessages)
            {
                if (m.type == CompilerMessageType.Error)
                {
                    // first error
                    if (!CompilerErrors)
                    {
                        LogWarning($"Batch failed!!!");
                    }

                    CompilerErrors = true;
                    LogWarning($"{m.file}:{m.line} -- {m.message}");
                }
            }

            // we can't run weaver if there are compile errors
            if (CompilerErrors)
            {
                return;
            }
#endif

            // call weaver on result
            var compiledAssembly = new CompiledAssembly(assemblyPath, builder);

            Log($"Starting weaver on {OutputFile}");
            var weaver = new Mirage.Weaver.Weaver(logger);
            builtAssembly = weaver.Weave(compiledAssembly);
            Log($"Finished weaver on {OutputFile}");

            // NOTE: we need to write to check for ArgumentException from writing
            TryWriteAssembly(builtAssembly);
        }