BuildAssetsCommand.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 build1 = string.Format(BuildEditor.buildDllFolder, platformType,
  58. $"{1}.{1}.{1}");
  59. foreach (var bundle in allBundles)
  60. {
  61. if (bundle.Contains(".manifest")) continue;
  62. var fileInfo = new FileInfo(bundle);
  63. byte[] data = File.ReadAllBytes(bundle);
  64. string fileName = fileInfo.Name;
  65. string p = build1 + fileName;
  66. File.WriteAllBytes(p, data);
  67. }
  68. File.WriteAllText(build1 + "DllMD5.txt", md5Json);
  69. AssetDatabase.SaveAssets();
  70. AssetDatabase.Refresh();
  71. }
  72. [MenuItem("HybridCLR/打包dll到专门文件夹")]
  73. public static void BuildAndCopyAOTHotUpdateDllsToSpecialFolder()
  74. {
  75. var gameRuntimeConfig = Resources.Load<GameRuntimeConfig>("GameRuntimeConfig");
  76. PlatformType platformType;
  77. #if UNITY_ANDROID
  78. platformType = PlatformType.Android;
  79. #elif UNITY_IOS
  80. platformType = PlatformType.IOS;
  81. #elif UNITY_STANDALONE_WIN
  82. platformType = PlatformType.PC;
  83. #elif UNITY_STANDALONE_OSX
  84. platformType = PlatformType.MacOS;
  85. #else
  86. platformType = PlatformType.None;
  87. #endif
  88. var build = string.Format(BuildEditor.buildDllFolder, platformType,
  89. $"{1}.{1}.{1}");
  90. BuildAndCopyAOTHotUpdateDlls(build);
  91. var assetMd5Info = new AssetMD5Info(new List<MD5FileInfo>());
  92. var allBundles = Directory.GetFiles(build);
  93. foreach (var bundle in allBundles)
  94. {
  95. if (bundle.Contains(".dll.bytes"))
  96. {
  97. MD5FileInfo md5Info = new MD5FileInfo();
  98. md5Info.md5 = MD5Helper.FileMD5(bundle);
  99. FileInfo fileInfo = new FileInfo(bundle);
  100. md5Info.size = fileInfo.Length;
  101. md5Info.fileName = Path.GetFileName(bundle);
  102. assetMd5Info.fileInfo.Add(md5Info);
  103. }
  104. }
  105. var md5Json = JsonHelper.ToJson(assetMd5Info);
  106. File.WriteAllText(build + "/DllMD5.txt", md5Json);
  107. LogTool.Log("MD5文件生成完成");
  108. }
  109. public static void BuildAndCopyAOTHotUpdateDlls(string buildDstDir)
  110. {
  111. if (!Directory.Exists(buildDstDir))
  112. {
  113. Directory.CreateDirectory(buildDstDir);
  114. }
  115. BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
  116. PrebuildCommandExt.CompileAndObfuscateDll();
  117. // ObfuscateUtil.CompileAndObfuscateHotUpdateAssemblies(target);
  118. // CompileDllCommand.CompileDllActiveBuildTarget();
  119. CopyAOTAssembliesToStreamingAssets(buildDstDir);
  120. CopyHotUpdateAssembliesToStreamingAssets(buildDstDir);
  121. }
  122. /// <summary>
  123. /// 复制AOT的Assembly补充元数据到StreamingAssets(在LoadDll.AOTMetaAssemblyNames中设置的)
  124. /// </summary>
  125. private static void CopyAOTAssembliesToStreamingAssets(string aotAssembliesDstDir)
  126. {
  127. var target = EditorUserBuildSettings.activeBuildTarget;
  128. string aotAssembliesSrcDir = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
  129. foreach (var dll in SettingsUtil.HybridCLRSettings.patchAOTAssemblies)
  130. {
  131. string srcDllPath = $"{aotAssembliesSrcDir}/{dll}";
  132. if (!File.Exists(srcDllPath))
  133. {
  134. Debug.LogError($"ab中添加AOT补充元数据dll:{srcDllPath} 时发生错误,文件不存在。" +
  135. "裁剪后的AOT dll在BuildPlayer时才能生成,因此需要你先构建一次游戏App后再打包。");
  136. continue;
  137. }
  138. string dllBytesPath = $"{aotAssembliesDstDir}/{dll}.bytes";
  139. byte[] data = File.ReadAllBytes(srcDllPath);
  140. data = DllTool.KeyEncryption(data);
  141. File.WriteAllBytes(dllBytesPath, data);
  142. Debug.Log($"[CopyAOTAssembliesToStreamingAssets] copy AOT dll {srcDllPath} -> {dllBytesPath}");
  143. }
  144. }
  145. // [MenuItem("Build/CompileAndObfuscateAndCopyToStreamingAssets")]
  146. // public static void CompileAndObfuscateAndCopyToStreamingAssets()
  147. // {
  148. // BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
  149. // ObfuscateUtil.CompileAndObfuscateHotUpdateAssemblies(target);
  150. //
  151. // Directory.CreateDirectory(Application.streamingAssetsPath);
  152. //
  153. // string hotUpdateDllPath = $"{SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target)}";
  154. // foreach (string assName in SettingsUtil.HotUpdateAssemblyNamesIncludePreserved)
  155. // {
  156. // string srcFile = $"{hotUpdateDllPath}/{assName}.dll";
  157. // string dstFile = $"{Application.streamingAssetsPath}/{assName}.dll.bytes";
  158. // File.Copy(srcFile, dstFile, true);
  159. // Debug.Log($"[CompileAndObfuscate] Copy {srcFile} to {dstFile}");
  160. // }
  161. // }
  162. /// <summary>
  163. /// 复制热更的Dll到StreamingAssets, 在HybridCLR Setting中设置的。
  164. /// </summary>
  165. private static void CopyHotUpdateAssembliesToStreamingAssets(string hotfixAssembliesDstDir)
  166. {
  167. var target = EditorUserBuildSettings.activeBuildTarget;
  168. string hotfixDllSrcDir = SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target);
  169. foreach (var dll in SettingsUtil.HotUpdateAssemblyFilesExcludePreserved)
  170. {
  171. string dllPath = $"{hotfixDllSrcDir}/{dll}";
  172. string dllBytesPath = $"{hotfixAssembliesDstDir}/{dll}.bytes";
  173. byte[] data = File.ReadAllBytes(dllPath);
  174. data = DllTool.KeyEncryption(data);
  175. File.WriteAllBytes(dllBytesPath, data);
  176. Debug.Log($"[CopyHotUpdateAssembliesToStreamingAssets] copy hotfix dll {dllPath} -> {dllBytesPath}");
  177. }
  178. }
  179. }
  180. }