123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- 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>("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<MD5FileInfo>());
- 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>("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<MD5FileInfo>());
- 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);
- }
- /// <summary>
- /// 复制AOT的Assembly补充元数据到StreamingAssets(在LoadDll.AOTMetaAssemblyNames中设置的)
- /// </summary>
- 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}");
- }
- }
- /// <summary>
- /// 复制热更的Dll到StreamingAssets, 在HybridCLR Setting中设置的。
- /// </summary>
- 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}");
- }
- }
- }
- }
|