SDKLinkProcessBuild.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEditor.Build;
  6. using UnityEditor.Build.Reporting;
  7. namespace TapSDK.Core.Editor {
  8. /// <summary>
  9. /// 模块 SDK 生成 link.xml 构建过程
  10. /// </summary>
  11. public abstract class SDKLinkProcessBuild : IPreprocessBuildWithReport, IPostprocessBuildWithReport {
  12. /// <summary>
  13. /// 执行顺序
  14. /// </summary>
  15. public abstract int callbackOrder { get; }
  16. /// <summary>
  17. /// 生成 link.xml 路径
  18. /// </summary>
  19. public abstract string LinkPath { get; }
  20. /// <summary>
  21. /// 防止被裁剪的 Assembly
  22. /// </summary>
  23. public abstract LinkedAssembly[] LinkedAssemblies { get; }
  24. /// <summary>
  25. /// 执行平台委托
  26. /// </summary>
  27. public abstract Func<BuildReport, bool> IsTargetPlatform { get; }
  28. /// <summary>
  29. /// 构建时忽略目录,目前主要是 PC 内置浏览器 Vuplex
  30. /// </summary>
  31. public virtual string[] BuildingIgnorePaths => null;
  32. /// <summary>
  33. /// 构建前处理
  34. /// </summary>
  35. /// <param name="report"></param>
  36. public void OnPreprocessBuild(BuildReport report) {
  37. if (!IsTargetPlatform.Invoke(report)) {
  38. return;
  39. }
  40. Application.logMessageReceived += OnBuildError;
  41. IgnorePaths();
  42. string linkPath = Path.Combine(Application.dataPath, LinkPath);
  43. LinkXMLGenerator.Generate(linkPath, LinkedAssemblies);
  44. }
  45. /// <summary>
  46. /// 构建后处理
  47. /// </summary>
  48. /// <param name="report"></param>
  49. public void OnPostprocessBuild(BuildReport report) {
  50. if (!IsTargetPlatform.Invoke(report)) {
  51. return;
  52. }
  53. Application.logMessageReceived -= OnBuildError;
  54. RecoverIgnoredPaths();
  55. string linkPath = Path.Combine(Application.dataPath, LinkPath);
  56. LinkXMLGenerator.Delete(linkPath);
  57. }
  58. /// <summary>
  59. /// 错误日志回调
  60. /// </summary>
  61. /// <param name="condition"></param>
  62. /// <param name="stacktrace"></param>
  63. /// <param name="logType"></param>
  64. private void OnBuildError(string condition, string stacktrace, LogType logType) {
  65. // TRICK: 通过捕获错误日志来监听打包错误事件
  66. if (logType == LogType.Error) {
  67. Application.logMessageReceived -= OnBuildError;
  68. RecoverIgnoredPaths();
  69. }
  70. }
  71. /// <summary>
  72. /// 忽略目录
  73. /// </summary>
  74. private void IgnorePaths() {
  75. if (BuildingIgnorePaths == null) {
  76. return;
  77. }
  78. foreach (string ignorePath in BuildingIgnorePaths) {
  79. if (!Directory.Exists(Path.Combine(Application.dataPath, ignorePath))) {
  80. continue;
  81. }
  82. string ignoreName = Path.GetFileName(ignorePath);
  83. AssetDatabase.RenameAsset(Path.Combine("Assets", ignorePath), $"{ignoreName}~");
  84. }
  85. }
  86. /// <summary>
  87. /// 恢复目录
  88. /// </summary>
  89. private void RecoverIgnoredPaths() {
  90. if (BuildingIgnorePaths == null) {
  91. return;
  92. }
  93. foreach (string ignorePath in BuildingIgnorePaths) {
  94. if (!Directory.Exists(Path.Combine(Application.dataPath, $"{ignorePath}~"))) {
  95. continue;
  96. }
  97. Directory.Move(Path.Combine(Application.dataPath, $"{ignorePath}~"),
  98. Path.Combine(Application.dataPath, $"{ignorePath}"));
  99. AssetDatabase.ImportAsset(Path.Combine("Assets", ignorePath));
  100. }
  101. }
  102. }
  103. }