/// <summary>
    /// 编译模式
    /// </summary>
    /// <param name="outpath"></param>
    /// <param name="platform"></param>
    /// <param name="mode"></param>
    static public void RoslynBuild(string outpath, RuntimePlatform platform, ScriptBuildTools.BuildMode mode)
    {
        //触发bd环境周期
        BDFrameEditorBehaviorHelper.OnBeginBuildDLL();

        var targetPath = "Assets/Code/Game/ILRuntime/Binding/Analysis";

        //1.分析之前先删除,然后生成临时文件防止报错
        if (Directory.Exists(targetPath))
        {
            Directory.Delete(targetPath, true);
        }

        var fileContent = @"
        namespace ILRuntime.Runtime.Generated
        {
            class CLRBindings
            {
                public static void Initialize(ILRuntime.Runtime.Enviorment.AppDomain app)
                {
                }
            } 
        }   ";

        FileHelper.WriteAllText(targetPath + "/CLRBindings.cs", fileContent);
        AssetDatabase.Refresh(); //这里必须要刷新

        //2.生成DLL
        ScriptBuildTools.BuildDll(outpath, platform, mode);

        //3.预绑定
        //GenPreCLRBinding();
        //4.生成自动分析绑定
        GenCLRBindingByAnalysis(platform, outpath);
        AssetDatabase.Refresh();
        //触发bd环境周期
        BDFrameEditorBehaviorHelper.OnEndBuildDLL(outpath);
        AssetHelper.GenPackageBuildInfo(outpath, platform);
        Debug.Log("脚本打包完毕");
    }
    /// <summary>
    /// 编译DLL
    /// </summary>
    static public void BuildDll(string outPath, RuntimePlatform platform, BuildMode mode, bool isShowTips = true)
    {
        IsShowTips = isShowTips;

        if (IsShowTips)
        {
            EditorUtility.DisplayProgressBar("编译服务", "准备编译环境...", 0.1f);
        }

        //生成CSProj
        EditorApplication.ExecuteMenuItem("Assets/Open C# Project");


        //准备输出环境
        var _outPath = Path.Combine(outPath, BDApplication.GetPlatformPath(platform));

        try
        {
            var path = _outPath + "/Hotfix";
            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }

            Directory.CreateDirectory(path);
        }
        catch (Exception e)
        {
            Debug.LogError(e.Message);
            if (IsShowTips)
            {
                EditorUtility.ClearProgressBar();
                EditorUtility.DisplayDialog("提示", "请手动删除hotfix文件后重试!", "OK");
            }

            return;
        }

        if (IsShowTips)
        {
            EditorUtility.DisplayProgressBar("编译服务", "开始处理脚本", 0.2f);
        }

        #region CS DLL引用搜集处理

        List <string> dllFileList = new List <string>();
        List <string> csFileList  = new List <string>();
        //所有宏
        defineList = new List <string>();

        var gameLogicCsproj = "Assembly-CSharp.csproj";  //游戏逻辑的代码
        var frameworkCsproj = "BDFramework.Core.csproj"; //框架部分的代码
        ParseCsprojFile(gameLogicCsproj, new List <string>()
        {
            frameworkCsproj
        }, ref csFileList, ref dllFileList);
        ParseCsprojFile(frameworkCsproj, new List <string>(), ref csFileList, ref dllFileList);
        //去重
        dllFileList = dllFileList.Distinct().ToList();
        csFileList  = csFileList.Distinct().ToList();
        defineList  = defineList.Distinct().ToList();

        //宏解析
        //移除editor相关宏
        for (int i = defineList.Count - 1; i >= 0; i--)
        {
            var symbol = defineList[i];
            if (symbol.Contains("UNITY_EDITOR"))
            {
                defineList.RemoveAt(i);
            }
        }

        //剔除不存的dll
        //TODO 这里是File 接口mac下有bug 会判断文件不存在
        if (Application.platform == RuntimePlatform.WindowsEditor)
        {
            for (int i = dllFileList.Count - 1; i >= 0; i--)
            {
                var dll = dllFileList[i];
                if (!File.Exists(dll))
                {
                    dllFileList.RemoveAt(i);
                    Debug.Log("剔除:" + dll);
                }
            }
        }
        else if (Application.platform == RuntimePlatform.OSXEditor)
        {
        }

        #endregion


        // 热更代码 = 框架部分@hotfix  +  游戏逻辑部分@hotfix
        var baseCs = csFileList.FindAll(f => !f.Contains("@hotfix") && f.EndsWith(".cs")); //筛选cs
        //不用ILR binding进行编译base.dll,因为binding本身会因为@hotfix调整容易报错
        baseCs = baseCs.Where((cs) =>
                              (!cs.Contains("\\ILRuntime\\Binding\\Analysis\\") && !cs.Contains("/ILRuntime/Binding/Analysis/")) ||
                              cs.EndsWith("CLRBindings.cs"))
                 .ToList();
        //
        var hotfixCs      = csFileList.FindAll(f => f.Contains("@hotfix") && f.EndsWith(".cs"));
        var outHotfixPath = Path.Combine(_outPath, DLLPATH);



        if (mode == BuildMode.Release)
        {
            Build(baseCs, hotfixCs, dllFileList, outHotfixPath);
        }
        else if (mode == BuildMode.Debug)
        {
            Build(baseCs, hotfixCs, dllFileList, outHotfixPath, true);
        }

        AssetHelper.GenPackageBuildInfo(outPath, platform);
    }
示例#3
0
    /// <summary>
    /// 编译DLL
    /// </summary>
    static public void BuildDll(string outPath, RuntimePlatform platform, BuildMode mode, bool isShowTips = true)
    {
        IsShowTips = isShowTips;

        if (IsShowTips)
        {
            EditorUtility.DisplayProgressBar("编译服务", "准备编译环境...", 0.1f);
        }
        //生成CSProj

        var csproj = Directory.GetFiles(BDApplication.ProjectRoot, "*.csproj", SearchOption.AllDirectories);

        if (csproj.Length == 0)
        {
            EditorApplication.ExecuteMenuItem("Assets/Open C# Project");
            CodeEditor.CurrentEditor.SyncAll();
            Debug.Log("生成Csproj!");
        }


        //
        var _outPath = Path.Combine(outPath, BDApplication.GetPlatformPath(platform));

        //准备输出环境
        try
        {
            var path = _outPath + "/Hotfix";
            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }

            Directory.CreateDirectory(path);
        }
        catch (Exception e)
        {
            Debug.LogError(e.Message);
            if (IsShowTips)
            {
                EditorUtility.ClearProgressBar();
                EditorUtility.DisplayDialog("提示", "请手动删除hotfix文件后重试!", "OK");
            }

            return;
        }

        if (IsShowTips)
        {
            EditorUtility.DisplayProgressBar("编译服务", "开始处理脚本", 0.2f);
        }

        #region CS DLL引用搜集处理

        List <string> dllFileList = new List <string>();
        List <string> csFileList  = new List <string>();
        //所有宏
        defineList = new List <string>();

        var gameLogicCsproj = "Assembly-CSharp.csproj";  //游戏逻辑的代码
        var frameworkCsproj = "BDFramework.Core.csproj"; //框架部分的代码
        ParseCsprojFile(gameLogicCsproj, new List <string>()
        {
            frameworkCsproj
        }, ref csFileList, ref dllFileList);
        ParseCsprojFile(frameworkCsproj, new List <string>(), ref csFileList, ref dllFileList);
        //去重
        dllFileList = dllFileList.Distinct().ToList();
        csFileList  = csFileList.Distinct().ToList();
        defineList  = defineList.Distinct().ToList();

        //宏解析
        //移除editor相关宏
        for (int i = defineList.Count - 1; i >= 0; i--)
        {
            var symbol = defineList[i];
            if (symbol.Contains("UNITY_EDITOR"))
            {
                defineList.RemoveAt(i);
            }
        }

        #endregion


        // 热更代码 = 框架部分@hotfix  +  游戏逻辑部分@hotfix
        var baseCs   = csFileList.FindAll(f => !f.Contains("@hotfix") && f.EndsWith(".cs"));
        var hotfixCs = csFileList.FindAll(f => f.Contains("@hotfix") && f.EndsWith(".cs"));

        var outHotfixPath = Path.Combine(_outPath, DLLPATH);

        if (mode == BuildMode.Release)
        {
            Build(baseCs, hotfixCs, dllFileList, outHotfixPath);
        }
        else if (mode == BuildMode.Debug)
        {
            Build(baseCs, hotfixCs, dllFileList, outHotfixPath, true);
        }
        AssetHelper.GenPackageBuildInfo(outPath, platform);
    }