BuildAssetsCommand.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using Fort23.Core;
  4. using Fort23.Editor;
  5. using Fort23.GameData;
  6. using Fort23.Mono;
  7. using Fort23.UTool;
  8. using hirdParty.DownloadSystem;
  9. using HybridCLR.Editor.Commands;
  10. using Obfuz4HybridCLR;
  11. using UnityEditor;
  12. using UnityEngine;
  13. namespace HybridCLR.Editor
  14. {
  15. public static class BuildAssetsCommand
  16. {
  17. [MenuItem("HybridCLR/打包dll到StreamingAssets")]
  18. public static void BuildAndCopyAOTHotUpdateDllsToStreamingAssets()
  19. {
  20. // var gameRuntimeConfig = Resources.Load<GameRuntimeConfig>("GameRuntimeConfig");
  21. var build = Application.streamingAssetsPath + "/Dll/";
  22. if (Directory.Exists(build))
  23. {
  24. Directory.Delete(build,true);
  25. }
  26. Directory.CreateDirectory(build);
  27. BuildAndCopyAOTHotUpdateDlls(build);
  28. var assetMd5Info = new AssetMD5Info(new List<MD5FileInfo>());
  29. var allBundles = Directory.GetFiles(build);
  30. foreach (var bundle in allBundles)
  31. {
  32. if (bundle.Contains(".dll.bytes"))
  33. {
  34. MD5FileInfo md5Info = new MD5FileInfo();
  35. md5Info.md5 = MD5Helper.FileMD5(bundle);
  36. FileInfo fileInfo = new FileInfo(bundle);
  37. md5Info.size = fileInfo.Length;
  38. md5Info.fileName = Path.GetFileName(bundle);
  39. assetMd5Info.fileInfo.Add(md5Info);
  40. }
  41. }
  42. var md5Json = JsonHelper.ToJson(assetMd5Info);
  43. File.WriteAllText(build + "DllMD5.txt", md5Json);
  44. LogTool.Log("MD5文件生成完成");
  45. PlatformType platformType;
  46. #if UNITY_ANDROID
  47. platformType = PlatformType.Android;
  48. #elif UNITY_IOS
  49. platformType = PlatformType.IOS;
  50. #elif UNITY_STANDALONE_WIN
  51. platformType = PlatformType.PC;
  52. #elif UNITY_STANDALONE_OSX
  53. platformType = PlatformType.MacOS;
  54. #else
  55. platformType = PlatformType.None;
  56. #endif
  57. // var build = Application.streamingAssetsPath;
  58. BuildAndCopyAOTHotUpdateDlls(build);
  59. AssetDatabase.SaveAssets();
  60. AssetDatabase.Refresh();
  61. }
  62. [MenuItem("HybridCLR/打包dll到专门文件夹")]
  63. public static void BuildAndCopyAOTHotUpdateDllsToSpecialFolder()
  64. {
  65. var gameRuntimeConfig = Resources.Load<GameRuntimeConfig>("GameRuntimeConfig");
  66. PlatformType platformType;
  67. #if UNITY_ANDROID
  68. platformType = PlatformType.Android;
  69. #elif UNITY_IOS
  70. platformType = PlatformType.IOS;
  71. #elif UNITY_STANDALONE_WIN
  72. platformType = PlatformType.PC;
  73. #elif UNITY_STANDALONE_OSX
  74. platformType = PlatformType.MacOS;
  75. #else
  76. platformType = PlatformType.None;
  77. #endif
  78. var build = string.Format(BuildEditor.buildDllFolder, platformType,
  79. $"{1}.{1}.{1}");
  80. BuildAndCopyAOTHotUpdateDlls(build);
  81. var assetMd5Info = new AssetMD5Info(new List<MD5FileInfo>());
  82. var allBundles = Directory.GetFiles(build);
  83. foreach (var bundle in allBundles)
  84. {
  85. if (bundle.Contains(".dll.bytes"))
  86. {
  87. MD5FileInfo md5Info = new MD5FileInfo();
  88. md5Info.md5 = MD5Helper.FileMD5(bundle);
  89. FileInfo fileInfo = new FileInfo(bundle);
  90. md5Info.size = fileInfo.Length;
  91. md5Info.fileName = Path.GetFileName(bundle);
  92. assetMd5Info.fileInfo.Add(md5Info);
  93. }
  94. }
  95. var md5Json = JsonHelper.ToJson(assetMd5Info);
  96. File.WriteAllText(build + "/DllMD5.txt", md5Json);
  97. LogTool.Log("MD5文件生成完成");
  98. }
  99. public static void BuildAndCopyAOTHotUpdateDlls(string buildDstDir)
  100. {
  101. if (!Directory.Exists(buildDstDir))
  102. {
  103. Directory.CreateDirectory(buildDstDir);
  104. }
  105. BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
  106. ObfuscateUtil.CompileAndObfuscateHotUpdateAssemblies(target);
  107. // CompileDllCommand.CompileDllActiveBuildTarget();
  108. CopyAOTAssembliesToStreamingAssets(buildDstDir);
  109. CopyHotUpdateAssembliesToStreamingAssets(buildDstDir);
  110. }
  111. /// <summary>
  112. /// 复制AOT的Assembly补充元数据到StreamingAssets(在LoadDll.AOTMetaAssemblyNames中设置的)
  113. /// </summary>
  114. private static void CopyAOTAssembliesToStreamingAssets(string aotAssembliesDstDir)
  115. {
  116. var target = EditorUserBuildSettings.activeBuildTarget;
  117. string aotAssembliesSrcDir = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
  118. foreach (var dll in SettingsUtil.HybridCLRSettings.patchAOTAssemblies)
  119. {
  120. string srcDllPath = $"{aotAssembliesSrcDir}/{dll}";
  121. if (!File.Exists(srcDllPath))
  122. {
  123. Debug.LogError($"ab中添加AOT补充元数据dll:{srcDllPath} 时发生错误,文件不存在。" +
  124. "裁剪后的AOT dll在BuildPlayer时才能生成,因此需要你先构建一次游戏App后再打包。");
  125. continue;
  126. }
  127. string dllBytesPath = $"{aotAssembliesDstDir}/{dll}.bytes";
  128. byte[] data = File.ReadAllBytes(srcDllPath);
  129. data = DllTool.KeyEncryption(data);
  130. File.WriteAllBytes(dllBytesPath, data);
  131. Debug.Log($"[CopyAOTAssembliesToStreamingAssets] copy AOT dll {srcDllPath} -> {dllBytesPath}");
  132. }
  133. }
  134. // [MenuItem("Build/CompileAndObfuscateAndCopyToStreamingAssets")]
  135. // public static void CompileAndObfuscateAndCopyToStreamingAssets()
  136. // {
  137. // BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
  138. // ObfuscateUtil.CompileAndObfuscateHotUpdateAssemblies(target);
  139. //
  140. // Directory.CreateDirectory(Application.streamingAssetsPath);
  141. //
  142. // string hotUpdateDllPath = $"{SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target)}";
  143. // foreach (string assName in SettingsUtil.HotUpdateAssemblyNamesIncludePreserved)
  144. // {
  145. // string srcFile = $"{hotUpdateDllPath}/{assName}.dll";
  146. // string dstFile = $"{Application.streamingAssetsPath}/{assName}.dll.bytes";
  147. // File.Copy(srcFile, dstFile, true);
  148. // Debug.Log($"[CompileAndObfuscate] Copy {srcFile} to {dstFile}");
  149. // }
  150. // }
  151. /// <summary>
  152. /// 复制热更的Dll到StreamingAssets, 在HybridCLR Setting中设置的。
  153. /// </summary>
  154. private static void CopyHotUpdateAssembliesToStreamingAssets(string hotfixAssembliesDstDir)
  155. {
  156. var target = EditorUserBuildSettings.activeBuildTarget;
  157. string hotfixDllSrcDir = SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target);
  158. foreach (var dll in SettingsUtil.HotUpdateAssemblyFilesExcludePreserved)
  159. {
  160. string dllPath = $"{hotfixDllSrcDir}/{dll}";
  161. string dllBytesPath = $"{hotfixAssembliesDstDir}/{dll}.bytes";
  162. byte[] data = File.ReadAllBytes(dllPath);
  163. data = DllTool.KeyEncryption(data);
  164. File.WriteAllBytes(dllBytesPath, data);
  165. Debug.Log($"[CopyHotUpdateAssembliesToStreamingAssets] copy hotfix dll {dllPath} -> {dllBytesPath}");
  166. }
  167. }
  168. }
  169. }