BuildAssetsCommand.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. PrebuildCommandExt.CompileAndObfuscateDll();
  107. // ObfuscateUtil.CompileAndObfuscateHotUpdateAssemblies(target);
  108. // CompileDllCommand.CompileDllActiveBuildTarget();
  109. CopyAOTAssembliesToStreamingAssets(buildDstDir);
  110. CopyHotUpdateAssembliesToStreamingAssets(buildDstDir);
  111. }
  112. /// <summary>
  113. /// 复制AOT的Assembly补充元数据到StreamingAssets(在LoadDll.AOTMetaAssemblyNames中设置的)
  114. /// </summary>
  115. private static void CopyAOTAssembliesToStreamingAssets(string aotAssembliesDstDir)
  116. {
  117. var target = EditorUserBuildSettings.activeBuildTarget;
  118. string aotAssembliesSrcDir = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
  119. foreach (var dll in SettingsUtil.HybridCLRSettings.patchAOTAssemblies)
  120. {
  121. string srcDllPath = $"{aotAssembliesSrcDir}/{dll}";
  122. if (!File.Exists(srcDllPath))
  123. {
  124. Debug.LogError($"ab中添加AOT补充元数据dll:{srcDllPath} 时发生错误,文件不存在。" +
  125. "裁剪后的AOT dll在BuildPlayer时才能生成,因此需要你先构建一次游戏App后再打包。");
  126. continue;
  127. }
  128. string dllBytesPath = $"{aotAssembliesDstDir}/{dll}.bytes";
  129. byte[] data = File.ReadAllBytes(srcDllPath);
  130. data = DllTool.KeyEncryption(data);
  131. File.WriteAllBytes(dllBytesPath, data);
  132. Debug.Log($"[CopyAOTAssembliesToStreamingAssets] copy AOT dll {srcDllPath} -> {dllBytesPath}");
  133. }
  134. }
  135. // [MenuItem("Build/CompileAndObfuscateAndCopyToStreamingAssets")]
  136. // public static void CompileAndObfuscateAndCopyToStreamingAssets()
  137. // {
  138. // BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
  139. // ObfuscateUtil.CompileAndObfuscateHotUpdateAssemblies(target);
  140. //
  141. // Directory.CreateDirectory(Application.streamingAssetsPath);
  142. //
  143. // string hotUpdateDllPath = $"{SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target)}";
  144. // foreach (string assName in SettingsUtil.HotUpdateAssemblyNamesIncludePreserved)
  145. // {
  146. // string srcFile = $"{hotUpdateDllPath}/{assName}.dll";
  147. // string dstFile = $"{Application.streamingAssetsPath}/{assName}.dll.bytes";
  148. // File.Copy(srcFile, dstFile, true);
  149. // Debug.Log($"[CompileAndObfuscate] Copy {srcFile} to {dstFile}");
  150. // }
  151. // }
  152. /// <summary>
  153. /// 复制热更的Dll到StreamingAssets, 在HybridCLR Setting中设置的。
  154. /// </summary>
  155. private static void CopyHotUpdateAssembliesToStreamingAssets(string hotfixAssembliesDstDir)
  156. {
  157. var target = EditorUserBuildSettings.activeBuildTarget;
  158. string hotfixDllSrcDir = SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target);
  159. foreach (var dll in SettingsUtil.HotUpdateAssemblyFilesExcludePreserved)
  160. {
  161. string dllPath = $"{hotfixDllSrcDir}/{dll}";
  162. string dllBytesPath = $"{hotfixAssembliesDstDir}/{dll}.bytes";
  163. byte[] data = File.ReadAllBytes(dllPath);
  164. data = DllTool.KeyEncryption(data);
  165. File.WriteAllBytes(dllBytesPath, data);
  166. Debug.Log($"[CopyHotUpdateAssembliesToStreamingAssets] copy hotfix dll {dllPath} -> {dllBytesPath}");
  167. }
  168. }
  169. }
  170. }