CopyStrippedAOTAssemblies.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using HybridCLR.Editor.Installer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using UnityEditor;
  9. using UnityEditor.Build;
  10. using UnityEditor.Build.Reporting;
  11. using UnityEditor.Il2Cpp;
  12. using UnityEditor.UnityLinker;
  13. using UnityEngine;
  14. namespace HybridCLR.Editor.BuildProcessors
  15. {
  16. internal class CopyStrippedAOTAssemblies : IPostprocessBuildWithReport, IPreprocessBuildWithReport
  17. #if !UNITY_2021_1_OR_NEWER
  18. , IIl2CppProcessor
  19. #endif
  20. {
  21. public int callbackOrder => 0;
  22. #if UNITY_2021_1_OR_NEWER
  23. public static string GetStripAssembliesDir2021(BuildTarget target)
  24. {
  25. string projectDir = SettingsUtil.ProjectDir;
  26. switch (target)
  27. {
  28. case BuildTarget.StandaloneWindows:
  29. case BuildTarget.StandaloneWindows64:
  30. return $"{projectDir}/Library/Bee/artifacts/WinPlayerBuildProgram/ManagedStripped";
  31. case BuildTarget.StandaloneLinux64:
  32. return $"{projectDir}/Library/Bee/artifacts/LinuxPlayerBuildProgram/ManagedStripped";
  33. case BuildTarget.Android:
  34. return $"{projectDir}/Library/Bee/artifacts/Android/ManagedStripped";
  35. case BuildTarget.iOS:
  36. return $"{projectDir}/Library/Bee/artifacts/iOS/ManagedStripped";
  37. case BuildTarget.WebGL:
  38. return $"{projectDir}/Library/Bee/artifacts/WebGL/ManagedStripped";
  39. case BuildTarget.StandaloneOSX:
  40. return $"{projectDir}/Library/Bee/artifacts/MacStandalonePlayerBuildProgram/ManagedStripped";
  41. case BuildTarget.PS4:
  42. return $"{projectDir}/Library/Bee/artifacts/PS4PlayerBuildProgram/ManagedStripped";
  43. case BuildTarget.PS5:
  44. return $"{projectDir}/Library/Bee/artifacts/PS5PlayerBuildProgram/ManagedStripped";
  45. default: return "";
  46. }
  47. }
  48. #else
  49. private string GetStripAssembliesDir2020(BuildTarget target)
  50. {
  51. string subPath = target == BuildTarget.Android ?
  52. "assets/bin/Data/Managed" :
  53. "Data/Managed/";
  54. return $"{SettingsUtil.ProjectDir}/Temp/StagingArea/{subPath}";
  55. }
  56. public void OnBeforeConvertRun(BuildReport report, Il2CppBuildPipelineData data)
  57. {
  58. // 此回调只在 2020中调用
  59. CopyStripDlls(GetStripAssembliesDir2020(data.target), data.target);
  60. }
  61. #endif
  62. public static void CopyStripDlls(string srcStripDllPath, BuildTarget target)
  63. {
  64. if (!SettingsUtil.Enable)
  65. {
  66. Debug.Log($"[CopyStrippedAOTAssemblies] disabled");
  67. return;
  68. }
  69. Debug.Log($"[CopyStrippedAOTAssemblies] CopyScripDlls. src:{srcStripDllPath} target:{target}");
  70. var dstPath = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
  71. Directory.CreateDirectory(dstPath);
  72. foreach (var fileFullPath in Directory.GetFiles(srcStripDllPath, "*.dll"))
  73. {
  74. var file = Path.GetFileName(fileFullPath);
  75. Debug.Log($"[CopyStrippedAOTAssemblies] copy strip dll {fileFullPath} ==> {dstPath}/{file}");
  76. File.Copy($"{fileFullPath}", $"{dstPath}/{file}", true);
  77. }
  78. }
  79. public void OnPostprocessBuild(BuildReport report)
  80. {
  81. #if UNITY_2021_1_OR_NEWER
  82. BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
  83. string srcStripDllPath = GetStripAssembliesDir2021(target);
  84. if (!string.IsNullOrEmpty(srcStripDllPath) && Directory.Exists(srcStripDllPath))
  85. {
  86. CopyStripDlls(srcStripDllPath, target);
  87. }
  88. #endif
  89. }
  90. public void OnPreprocessBuild(BuildReport report)
  91. {
  92. BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
  93. var dstPath = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
  94. BashUtil.RecreateDir(dstPath);
  95. }
  96. }
  97. }