using System.Collections.Generic; using System.IO; using Fort23.Core; using Fort23.Editor; using Fort23.GameData; using Fort23.Mono; using Fort23.UTool; using hirdParty.DownloadSystem; using HybridCLR.Editor.Commands; using UnityEditor; using UnityEngine; namespace HybridCLR.Editor { public static class BuildAssetsCommand { [MenuItem("HybridCLR/打包dll到StreamingAssets")] public static void BuildAndCopyAOTHotUpdateDllsToStreamingAssets() { // var gameRuntimeConfig = Resources.Load("GameRuntimeConfig"); var build = Application.streamingAssetsPath + "/Dll/"; if (Directory.Exists(build)) { Directory.Delete(build,true); } Directory.CreateDirectory(build); BuildAndCopyAOTHotUpdateDlls(build); var assetMd5Info = new AssetMD5Info(new List()); var allBundles = Directory.GetFiles(build); foreach (var bundle in allBundles) { if (bundle.Contains(".dll.bytes")) { MD5FileInfo md5Info = new MD5FileInfo(); md5Info.md5 = MD5Helper.FileMD5(bundle); FileInfo fileInfo = new FileInfo(bundle); md5Info.size = fileInfo.Length; md5Info.fileName = Path.GetFileName(bundle); assetMd5Info.fileInfo.Add(md5Info); } } var md5Json = JsonHelper.ToJson(assetMd5Info); File.WriteAllText(build + "DllMD5.txt", md5Json); LogTool.Log("MD5文件生成完成"); PlatformType platformType; #if UNITY_ANDROID platformType = PlatformType.Android; #elif UNITY_IOS platformType = PlatformType.IOS; #elif UNITY_STANDALONE_WIN platformType = PlatformType.PC; #elif UNITY_STANDALONE_OSX platformType = PlatformType.MacOS; #else platformType = PlatformType.None; #endif // var build = Application.streamingAssetsPath; BuildAndCopyAOTHotUpdateDlls(build); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } [MenuItem("HybridCLR/打包dll到专门文件夹")] public static void BuildAndCopyAOTHotUpdateDllsToSpecialFolder() { var gameRuntimeConfig = Resources.Load("GameRuntimeConfig"); PlatformType platformType; #if UNITY_ANDROID platformType = PlatformType.Android; #elif UNITY_IOS platformType = PlatformType.IOS; #elif UNITY_STANDALONE_WIN platformType = PlatformType.PC; #elif UNITY_STANDALONE_OSX platformType = PlatformType.MacOS; #else platformType = PlatformType.None; #endif var build = string.Format(BuildEditor.buildDllFolder, platformType, $"{1}.{1}.{1}"); BuildAndCopyAOTHotUpdateDlls(build); var assetMd5Info = new AssetMD5Info(new List()); var allBundles = Directory.GetFiles(build); foreach (var bundle in allBundles) { if (bundle.Contains(".dll.bytes")) { MD5FileInfo md5Info = new MD5FileInfo(); md5Info.md5 = MD5Helper.FileMD5(bundle); FileInfo fileInfo = new FileInfo(bundle); md5Info.size = fileInfo.Length; md5Info.fileName = Path.GetFileName(bundle); assetMd5Info.fileInfo.Add(md5Info); } } var md5Json = JsonHelper.ToJson(assetMd5Info); File.WriteAllText(build + "/DllMD5.txt", md5Json); LogTool.Log("MD5文件生成完成"); } public static void BuildAndCopyAOTHotUpdateDlls(string buildDstDir) { if (!Directory.Exists(buildDstDir)) { Directory.CreateDirectory(buildDstDir); } CompileDllCommand.CompileDllActiveBuildTarget(); CopyAOTAssembliesToStreamingAssets(buildDstDir); CopyHotUpdateAssembliesToStreamingAssets(buildDstDir); } /// /// 复制AOT的Assembly补充元数据到StreamingAssets(在LoadDll.AOTMetaAssemblyNames中设置的) /// private static void CopyAOTAssembliesToStreamingAssets(string aotAssembliesDstDir) { var target = EditorUserBuildSettings.activeBuildTarget; string aotAssembliesSrcDir = SettingsUtil.GetAssembliesPostIl2CppStripDir(target); foreach (var dll in SettingsUtil.HybridCLRSettings.patchAOTAssemblies) { string srcDllPath = $"{aotAssembliesSrcDir}/{dll}"; if (!File.Exists(srcDllPath)) { Debug.LogError($"ab中添加AOT补充元数据dll:{srcDllPath} 时发生错误,文件不存在。" + "裁剪后的AOT dll在BuildPlayer时才能生成,因此需要你先构建一次游戏App后再打包。"); continue; } string dllBytesPath = $"{aotAssembliesDstDir}/{dll}.bytes"; byte[] data = File.ReadAllBytes(srcDllPath); data = DllTool.KeyEncryption(data); File.WriteAllBytes(dllBytesPath, data); Debug.Log($"[CopyAOTAssembliesToStreamingAssets] copy AOT dll {srcDllPath} -> {dllBytesPath}"); } } /// /// 复制热更的Dll到StreamingAssets, 在HybridCLR Setting中设置的。 /// private static void CopyHotUpdateAssembliesToStreamingAssets(string hotfixAssembliesDstDir) { var target = EditorUserBuildSettings.activeBuildTarget; string hotfixDllSrcDir = SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target); foreach (var dll in SettingsUtil.HotUpdateAssemblyFilesExcludePreserved) { string dllPath = $"{hotfixDllSrcDir}/{dll}"; string dllBytesPath = $"{hotfixAssembliesDstDir}/{dll}.bytes"; byte[] data = File.ReadAllBytes(dllPath); data = DllTool.KeyEncryption(data); File.WriteAllBytes(dllBytesPath, data); Debug.Log($"[CopyHotUpdateAssembliesToStreamingAssets] copy hotfix dll {dllPath} -> {dllBytesPath}"); } } } }