public static ScriptCompilationResult CompileScript(ImmutableList <SyntaxTree> compilationSources, AsmDetail detailsToUseForTarget,
                                                            IEnumerable <string> additionalUsings = null, IEnumerable <MetadataReference> additionalReferences = null)
        {
            var compilationPrep = PrepareCompilation(compilationSources, detailsToUseForTarget, additionalUsings, additionalReferences);

            var scriptCompilation = new ScriptCompilationResult();

            using (var dllStream = new MemoryStream())
                using (var pdbStream = new MemoryStream())
                {
                    var emitResult = compilationPrep.Compilation.Emit(dllStream, pdbStream);

                    //success sometimes was false when there are warnings, but i didnt write it down
                    // so maybe it was a specific kind. Or I should pay more attention.
                    scriptCompilation.CompilationResult = emitResult;

                    if (emitResult.Diagnostics.Count(d => d.Severity == DiagnosticSeverity.Error) == 0)
                    {
                        scriptCompilation.IsCompiled = true;
                        dllStream.Seek(0, SeekOrigin.Begin);
                        pdbStream.Seek(0, SeekOrigin.Begin);
                        scriptCompilation.AssemblyBytes = dllStream.ToArray();
                        scriptCompilation.PdbBytes      = pdbStream.ToArray();

                        scriptCompilation.AssemblyFilePath = detailsToUseForTarget.AsmPath;
                        scriptCompilation.PdbFilePath      = detailsToUseForTarget.PdbPath;
                        scriptCompilation.FoundNamespaces.AddRange(compilationPrep.Usings);
                        scriptCompilation.FoundMetadataReferences.AddRange(compilationPrep.MetadataReferences);
                    }
                }
            return(scriptCompilation);
        }
Exemple #2
0
        //生成特定平台的patch
        public static void GenPlatformPatch(Platform platform, string patchOutputDir,
                                            string corePath = "./Assets/Plugins/IFix.Core.dll")
        {
            var outputDir = "Temp/ifix";

            Directory.CreateDirectory("Temp");
            Directory.CreateDirectory(outputDir);
#if UNITY_2018_3_OR_NEWER
            ScriptCompilationSettings scriptCompilationSettings = new ScriptCompilationSettings();
            if (platform == Platform.android)
            {
                scriptCompilationSettings.group  = BuildTargetGroup.Android;
                scriptCompilationSettings.target = BuildTarget.Android;
            }
            else if (platform == Platform.ios)
            {
                scriptCompilationSettings.group  = BuildTargetGroup.iOS;
                scriptCompilationSettings.target = BuildTarget.iOS;
            }
            else
            {
                scriptCompilationSettings.group  = BuildTargetGroup.Standalone;
                scriptCompilationSettings.target = BuildTarget.StandaloneWindows;
            }

            ScriptCompilationResult scriptCompilationResult = PlayerBuildInterface.CompilePlayerScripts(scriptCompilationSettings, outputDir);

            foreach (var assembly in injectAssemblys)
            {
                if (!Directory.Exists(patchOutputDir))
                {
                    Directory.CreateDirectory(patchOutputDir);
                }

                GenPatch(assembly, string.Format("{0}/{1}.dll", outputDir, assembly),
                         "./Assets/Plugins/IFix.Core.dll", string.Format("{0}{1}.patch.bytes", patchOutputDir, assembly));
            }
#else
            throw new NotImplementedException();
            //var compileArgFile = "Temp/ifix/unity_" + platform + "_compile_argument";
            //var tmpDllPath = "Temp/ifix/Assembly-CSharp.dll";
            //File.WriteAllText(compileArgFile, getCompileArguments(platform, tmpDllPath));
            //先编译dll到Temp目录下
            //Compile(compileArgFile);
            //对编译后的dll生成补丁
            //GenPatch("Assembly-CSharp", tmpDllPath, corePath, patchPath);

            //File.Delete(compileArgFile);
            //File.Delete(tmpDllPath);
            //File.Delete(tmpDllPath + ".mdb");
#endif
        }
Exemple #3
0
    private static void CheckCompilePlayerScripts(ScriptCompilationSettings settings)
    {
        EditorUtility.DisplayProgressBar("Check Compile Player Scripts", "Do not operate the computer", 0);
        ScriptCompilationResult result = PlayerBuildInterface.CompilePlayerScripts(settings, string.Format("{0}/Unity_{1}/", System.IO.Path.GetTempPath(), GUID.Generate()));

        EditorUtility.ClearProgressBar();

        if ((result.assemblies == null ||
             result.assemblies.Count == 0) &&
            result.typeDB == null)
        {
            EditorUtility.DisplayDialog("Check Compile Player Scripts", "Failed\nSee Console Window", "OK");
        }
        else
        {
            EditorUtility.DisplayDialog("Check Compile Player Scripts", "Success", "OK");
        }
    }
        private static ScriptCompilationResult BuildScriptCompilationResult(AsmDetail detailsToUseForTarget, EmitResult emitResult,
                                                                            PreparedCompilation compilationPrep)
        {
            var result = new ScriptCompilationResult {
                CompilationResult = emitResult
            };

            if (emitResult.Diagnostics.Count(d => d.Severity == DiagnosticSeverity.Error) == 0)
            {
                result.IsCompiled       = true;
                result.AssemblyFilePath = detailsToUseForTarget.AsmPath;
                result.PdbFilePath      = detailsToUseForTarget.PdbPath;
                result.FoundNamespaces.AddRange(compilationPrep.Usings);
                result.FoundMetadataReferences.AddRange(compilationPrep.MetadataReferences);
            }

            return(result);
        }
Exemple #5
0
        //生成特定平台的patch
        public static void GenPlatformPatch(Platform platform, string patchOutputDir,
                                            string corePath = CoreDllPath)
        {
            var outputDir = "Temp/ifix";

            Directory.CreateDirectory("Temp");
            Directory.CreateDirectory(outputDir);
#if UNITY_2018_3_OR_NEWER
            ScriptCompilationSettings scriptCompilationSettings = new ScriptCompilationSettings();
            if (platform == Platform.android)
            {
                scriptCompilationSettings.group  = BuildTargetGroup.Android;
                scriptCompilationSettings.target = BuildTarget.Android;
            }
            else if (platform == Platform.ios)
            {
                scriptCompilationSettings.group  = BuildTargetGroup.iOS;
                scriptCompilationSettings.target = BuildTarget.iOS;
            }
            else
            {
                scriptCompilationSettings.group  = BuildTargetGroup.Standalone;
                scriptCompilationSettings.target = BuildTarget.StandaloneWindows;
            }

            ScriptCompilationResult scriptCompilationResult = PlayerBuildInterface.CompilePlayerScripts(scriptCompilationSettings, outputDir);

            // 走自定义生成补丁函数,统一处理
            CustomizedGenPatch();
#else
            throw new NotImplementedException();
            //var compileArgFile = "Temp/ifix/unity_" + platform + "_compile_argument";
            //var tmpDllPath = "Temp/ifix/Assembly-CSharp.dll";
            //File.WriteAllText(compileArgFile, getCompileArguments(platform, tmpDllPath));
            //先编译dll到Temp目录下
            //Compile(compileArgFile);
            //对编译后的dll生成补丁
            //GenPatch("Assembly-CSharp", tmpDllPath, corePath, patchPath);

            //File.Delete(compileArgFile);
            //File.Delete(tmpDllPath);
            //File.Delete(tmpDllPath + ".mdb");
#endif
        }
Exemple #6
0
    public static void BuildDll()
    {
        string outputDir = ".";
        var    target    = EditorUserBuildSettings.activeBuildTarget;
        var    group     = BuildPipeline.GetBuildTargetGroup(target);

        ScriptCompilationSettings scriptCompilationSettings = new ScriptCompilationSettings();

        scriptCompilationSettings.group  = group;
        scriptCompilationSettings.target = target;
        var buildDir = $"{outputDir}/build";

        if (false == Directory.Exists(buildDir))
        {
            Directory.CreateDirectory(buildDir);
        }
        ScriptCompilationResult scriptCompilationResult = PlayerBuildInterface.CompilePlayerScripts(scriptCompilationSettings, buildDir);

        //MoveAssemblys(outputDir, buildDir, true);
    }
        public static Assembly WriteScriptCompilationAsAssemblyAndLoadIt(ScriptCompilationResult compResult)
        {
            if (compResult.IsCompiled == false)
            {
                throw new CompilationErrorException("Failed to compile", compResult.CompilationResult.Diagnostics);
            }

            if (FileUtilities.WriteFile(compResult.AssemblyFilePath, compResult.AssemblyBytes) == false)
            {
                throw new IOException("Failed to write assembly dll to disk");
            }

            if (FileUtilities.WriteFile(compResult.PdbFilePath, compResult.PdbBytes) == false)
            {
                throw new IOException("Failed to write assembly pdb to disk");
            }


            return(Assembly.LoadFile(compResult.AssemblyFilePath));
        }
Exemple #8
0
        //生成特定平台的patch
        public static void GenPlatformPatch(BuildTargetGroup group, BuildTarget buildTarget,
                                            string corePath = IFixCoreDllPath)
        {
#if UNITY_2018_3_OR_NEWER
            RuntimePlatform           rplatform;
            ScriptCompilationSettings scriptCompilationSettings = new ScriptCompilationSettings()
            {
                group  = group,
                target = buildTarget,
            };

            var patchOutputDir = $"Assets/AppRes/ifix/{IFixCfg.TargetName(buildTarget)}/";
            Directory.CreateDirectory(patchOutputDir);

            var assemblyOutputDir = $"obj/{IFixCfg.TargetName(buildTarget)}/";
            Directory.CreateDirectory(assemblyOutputDir);
            ScriptCompilationResult scriptCompilationResult = PlayerBuildInterface.CompilePlayerScripts(scriptCompilationSettings, assemblyOutputDir);

            foreach (var assembly in injectAssemblys)
            {
                GenPatch(assembly, string.Format("{0}/{1}.dll", assemblyOutputDir, assembly),
                         IFixCoreDllPath, string.Format("{0}{1}.patch.ifix", patchOutputDir, assembly));
            }
#else
            throw new NotImplementedException();
            //var compileArgFile = "Temp/ifix/unity_" + platform + "_compile_argument";
            //var tmpDllPath = "Temp/ifix/Assembly-CSharp.dll";
            //File.WriteAllText(compileArgFile, getCompileArguments(platform, tmpDllPath));
            //先编译dll到Temp目录下
            //Compile(compileArgFile);
            //对编译后的dll生成补丁
            //GenPatch("Assembly-CSharp", tmpDllPath, corePath, patchPath);

            //File.Delete(compileArgFile);
            //File.Delete(tmpDllPath);
            //File.Delete(tmpDllPath + ".mdb");
#endif
        }
Exemple #9
0
        // 采用新的ScriptableBuildPipeline
        private static void InternalScriptableBuildAndPushDll()
        {
            EditorUtility.DisplayProgressBar("BuildingDll", "Building Dll...", 0);

            string projectPath  = Application.dataPath.Replace("Assets", "");
            string srcDllFolder = $@"{projectPath}AndroidDll/{Application.productName}";
            ScriptCompilationSettings scriptCompilationSettings = new ScriptCompilationSettings();

            scriptCompilationSettings.group  = BuildTargetGroup.Android;
            scriptCompilationSettings.target = BuildTarget.Android;
            try
            {
                ScriptCompilationResult scriptCompilationResult = PlayerBuildInterface.CompilePlayerScripts(scriptCompilationSettings, srcDllFolder);
                string srcDllPath = $@"{srcDllFolder}/{DLL_NAME}";
                PushDll(srcDllPath);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            EditorUtility.ClearProgressBar();
        }
        public static ScriptCompilationResult GetCompilationForLoadDirective(CompileDirection compileDirection, string path, AsmDetail asmDetail,
                                                                             List <string> namespaces, IEnumerable <MetadataReference> metadataReferences)
        {
            var targetType            = GetResolutionTargetType(path);
            var referencedCompilation = new ScriptCompilationResult();

            if (compileDirection == CompileDirection.EverythingBuiltAsClassesAndReffed)
            {
                if (targetType == ResolutionTargetType.Cs)
                {
                    var compilationUnit = GetRootMainCompilationUnit(FileUtilities.GetFileContent(path), CsRewriter.DefaultParseOptions);
                    referencedCompilation = CompileAndWriteAssembly(ImmutableList.Create(compilationUnit.SyntaxTree),
                                                                    asmDetail, namespaces, metadataReferences);
                }
            }
            else //compileDirection == CompileDirection.OnlyClassesBuiltAsScriptsAndReffed
            {
                if (targetType == ResolutionTargetType.Cs)
                {
                    var extraction = CsRewriter.ExtractCompilationDetailFromClassFile(path);
                    referencedCompilation = CompileAndWriteScript(extraction.CompilationTargets.First(),
                                                                  asmDetail, namespaces, metadataReferences);
                }
                else
                {
                    var compilationUnit = GetRootMainCompilationUnit(FileUtilities.GetFileContent(path));
                    referencedCompilation = CompileAndWriteAssembly(ImmutableList.Create(compilationUnit.SyntaxTree),
                                                                    asmDetail, namespaces, metadataReferences);
                }
            }

            Debug.Assert(referencedCompilation != null, "load directive compilation should not be null",
                         $"path '{path}' - compileDirection {compileDirection} - targetType {targetType}\n\n\n");

            return(referencedCompilation);
        }
        /// <returns>是否成功</returns>
        public static bool CompilePlayerScripts(ScriptCompilationSettings settings, string outputFolder, bool displayUI, out ScriptCompilationResult result)
        {
            if (displayUI)
            {
                EditorUtility.DisplayProgressBar($"Compile Player Scripts ({settings.target})", "Do not operate the computer", 0);
            }
            result = PlayerBuildInterface.CompilePlayerScripts(settings, outputFolder);
            EditorUtility.ClearProgressBar();

            if (result.assemblies != null &&
                result.assemblies.Count > 0 &&
                result.typeDB != null)
            {
                if (settings.target == EditorUserBuildSettings.activeBuildTarget)
                {
                    s_CurrentTargetTypeDBCache = result.typeDB;
                }

                if (displayUI)
                {
                    EditorUtility.DisplayDialog("Check Compile Player Scripts"
                                                , $"Success\n{settings.target}: {settings.options}"
                                                , "OK");
                }
                return(true);
            }
            else
            {
                if (displayUI)
                {
                    EditorUtility.DisplayDialog("Check Compile Player Scripts"
                                                , $"Failed\n{settings.target}: {settings.options}\nSee Console Window"
                                                , "OK");
                }
                return(false);
            }
        }
        public static bool CompilePlayerScripts(BuildTarget buildTarget, ScriptCompilationOptions options, string outputFolder, bool displayUI, out ScriptCompilationResult result)
        {
            ScriptCompilationSettings settings = new ScriptCompilationSettings();

            settings.target  = buildTarget;
            settings.group   = BuildPipeline.GetBuildTargetGroup(buildTarget);
            settings.options = options;
            return(CompilePlayerScripts(settings, outputFolder, displayUI, out result));
        }