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