HotPatchAssemblySelection.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Newtonsoft.Json.Linq;
  5. using UnityEditor;
  6. using UnityEditorInternal;
  7. public static class HotPatchAssemblySelection
  8. {
  9. public static Action<AssemblyDefinitionAsset> ApplyHybridCLRSettings;
  10. public static AssemblyDefinitionAsset Find(string name)
  11. {
  12. if (string.IsNullOrWhiteSpace(name)) return null;
  13. var paths = AssetDatabase.FindAssets("t:AssemblyDefinitionAsset", new[] { "Assets" })
  14. .Select(AssetDatabase.GUIDToAssetPath)
  15. .Where(p => (string)JObject.Parse(File.ReadAllText(p))["name"] == name).ToArray();
  16. return paths.Length == 1 ? AssetDatabase.LoadAssetAtPath<AssemblyDefinitionAsset>(paths[0]) : null;
  17. }
  18. public static string Validate(AssemblyDefinitionAsset assembly)
  19. {
  20. var path = AssetDatabase.GetAssetPath(assembly);
  21. if (assembly == null || !path.StartsWith("Assets/", StringComparison.Ordinal) || !path.EndsWith(".asmdef", StringComparison.Ordinal))
  22. throw new InvalidOperationException("请选择 Assets 中的游戏运行时 asmdef。");
  23. var definition = JObject.Parse(assembly.text);
  24. var name = (string)definition["name"];
  25. if (string.IsNullOrWhiteSpace(name) || name.IndexOfAny(new[] { '/', '\\', ':', ',' }) >= 0 || name.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
  26. throw new InvalidOperationException("热补丁程序集名称不正确。");
  27. var platforms = definition["includePlatforms"] as JArray;
  28. if (platforms != null && platforms.Count > 0 && platforms.All(p => (string)p == "Editor"))
  29. throw new InvalidOperationException("不能选择 Editor 专用程序集。");
  30. if (name == "AppRuntimes" || name == "AppRuntimes.Base" || name == "HotPatch.Runtime" ||
  31. name == "AppRuntimes.Sources.Editor" || ((JArray)definition["references"] ?? new JArray()).Values<string>().Any(IsLoaderReference))
  32. throw new InvalidOperationException("宿主或热补丁加载器程序集不能作为热更目标。");
  33. return name;
  34. }
  35. static bool IsLoaderReference(string reference)
  36. {
  37. if (reference.StartsWith("GUID:", StringComparison.Ordinal))
  38. {
  39. var path = AssetDatabase.GUIDToAssetPath(reference.Substring(5));
  40. if (File.Exists(path)) reference = (string)JObject.Parse(File.ReadAllText(path))["name"];
  41. }
  42. return reference == "HotPatch.Runtime";
  43. }
  44. public static void Save(AppSettings settings, AssemblyDefinitionAsset assembly)
  45. {
  46. if (settings == null) throw new ArgumentNullException(nameof(settings));
  47. var name = Validate(assembly);
  48. if (ApplyHybridCLRSettings == null)
  49. throw new InvalidOperationException("HybridCLR 编辑器集成未就绪,请安装 HybridCLR 并等待编译完成。");
  50. ApplyHybridCLRSettings(assembly);
  51. Undo.RecordObject(settings, "Select hot-patch assembly");
  52. settings.HotPatchAssemblyName = name;
  53. EditorUtility.SetDirty(settings);
  54. }
  55. }