PatchScriptingAssemblyList.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using UnityEditor;
  8. using UnityEditor.Android;
  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. public class PatchScriptingAssemblyList :
  17. #if UNITY_ANDROID
  18. IPostGenerateGradleAndroidProject,
  19. #endif
  20. IPostprocessBuildWithReport
  21. #if !UNITY_2021_1_OR_NEWER && UNITY_WEBGL
  22. , IIl2CppProcessor
  23. #endif
  24. #if UNITY_PS5
  25. , IUnityLinkerProcessor
  26. #endif
  27. {
  28. public int callbackOrder => 0;
  29. public void OnPostGenerateGradleAndroidProject(string path)
  30. {
  31. // 如果直接打包apk,没有机会在PostprocessBuild中修改ScriptingAssemblies.json。
  32. // 因此需要在这个时机处理
  33. // Unity有bug,偶然情况下会传入apk的路径,导致替换失败
  34. if (Directory.Exists(path))
  35. {
  36. PathScriptingAssembilesFile(path);
  37. }
  38. else
  39. {
  40. PathScriptingAssembilesFile($"{SettingsUtil.ProjectDir}/Library");
  41. }
  42. }
  43. public void OnPostprocessBuild(BuildReport report)
  44. {
  45. // 如果target为Android,由于已经在OnPostGenerateGradelAndroidProject中处理过,
  46. // 这里不再重复处理
  47. #if !UNITY_ANDROID && !UNITY_WEBGL
  48. PathScriptingAssembilesFile(report.summary.outputPath);
  49. #endif
  50. }
  51. #if UNITY_PS5
  52. /// <summary>
  53. /// 打包模式如果是 Package 需要在这个阶段提前处理 .json , PC Hosted 和 GP5 模式不受影响
  54. /// </summary>
  55. public string GenerateAdditionalLinkXmlFile(UnityEditor.Build.Reporting.BuildReport report, UnityEditor.UnityLinker.UnityLinkerBuildPipelineData data)
  56. {
  57. string path = $"{SettingsUtil.ProjectDir}/Library/PlayerDataCache/PS5/Data";
  58. PathScriptingAssembilesFile(path);
  59. return null;
  60. }
  61. #endif
  62. public void PathScriptingAssembilesFile(string path)
  63. {
  64. if (!SettingsUtil.Enable)
  65. {
  66. Debug.Log($"[PatchScriptingAssemblyList] disabled");
  67. return;
  68. }
  69. Debug.Log($"[PatchScriptingAssemblyList]. path:{path}");
  70. if (!Directory.Exists(path))
  71. {
  72. path = Path.GetDirectoryName(path);
  73. Debug.Log($"[PatchScriptingAssemblyList] get path parent:{path}");
  74. }
  75. AddHotFixAssembliesToScriptingAssembliesJson(path);
  76. }
  77. private void AddHotFixAssembliesToScriptingAssembliesJson(string path)
  78. {
  79. Debug.Log($"[PatchScriptingAssemblyList]. path:{path}");
  80. /*
  81. * ScriptingAssemblies.json 文件中记录了所有的dll名称,此列表在游戏启动时自动加载,
  82. * 不在此列表中的dll在资源反序列化时无法被找到其类型
  83. * 因此 OnFilterAssemblies 中移除的条目需要再加回来
  84. */
  85. string[] jsonFiles = Directory.GetFiles(path, SettingsUtil.ScriptingAssembliesJsonFile, SearchOption.AllDirectories);
  86. if (jsonFiles.Length == 0)
  87. {
  88. Debug.LogWarning($"can not find file {SettingsUtil.ScriptingAssembliesJsonFile}");
  89. return;
  90. }
  91. foreach (string file in jsonFiles)
  92. {
  93. var patcher = new ScriptingAssembliesJsonPatcher();
  94. patcher.Load(file);
  95. patcher.AddScriptingAssemblies(SettingsUtil.HotUpdateAssemblyFilesIncludePreserved);
  96. patcher.Save(file);
  97. }
  98. }
  99. #if UNITY_WEBGL
  100. public void OnBeforeConvertRun(BuildReport report, Il2CppBuildPipelineData data)
  101. {
  102. PathScriptingAssembilesFile($"{SettingsUtil.ProjectDir}/Temp/StagingArea/Data");
  103. }
  104. #endif
  105. }
  106. }