BuildAssetsCommand.cs 6.4 KB

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