| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using System;
- using System.IO;
- using System.Linq;
- using Newtonsoft.Json.Linq;
- using UnityEditor;
- using UnityEditorInternal;
- public static class HotPatchAssemblySelection
- {
- public static Action<AssemblyDefinitionAsset> ApplyHybridCLRSettings;
- public static AssemblyDefinitionAsset Find(string name)
- {
- if (string.IsNullOrWhiteSpace(name)) return null;
- var paths = AssetDatabase.FindAssets("t:AssemblyDefinitionAsset", new[] { "Assets" })
- .Select(AssetDatabase.GUIDToAssetPath)
- .Where(p => (string)JObject.Parse(File.ReadAllText(p))["name"] == name).ToArray();
- return paths.Length == 1 ? AssetDatabase.LoadAssetAtPath<AssemblyDefinitionAsset>(paths[0]) : null;
- }
- public static string Validate(AssemblyDefinitionAsset assembly)
- {
- var path = AssetDatabase.GetAssetPath(assembly);
- if (assembly == null || !path.StartsWith("Assets/", StringComparison.Ordinal) || !path.EndsWith(".asmdef", StringComparison.Ordinal))
- throw new InvalidOperationException("请选择 Assets 中的游戏运行时 asmdef。");
- var definition = JObject.Parse(assembly.text);
- var name = (string)definition["name"];
- if (string.IsNullOrWhiteSpace(name) || name.IndexOfAny(new[] { '/', '\\', ':', ',' }) >= 0 || name.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
- throw new InvalidOperationException("热补丁程序集名称不正确。");
- var platforms = definition["includePlatforms"] as JArray;
- if (platforms != null && platforms.Count > 0 && platforms.All(p => (string)p == "Editor"))
- throw new InvalidOperationException("不能选择 Editor 专用程序集。");
- if (name == "AppRuntimes" || name == "AppRuntimes.Base" || name == "HotPatch.Runtime" ||
- name == "AppRuntimes.Sources.Editor" || ((JArray)definition["references"] ?? new JArray()).Values<string>().Any(IsLoaderReference))
- throw new InvalidOperationException("宿主或热补丁加载器程序集不能作为热更目标。");
- return name;
- }
- static bool IsLoaderReference(string reference)
- {
- if (reference.StartsWith("GUID:", StringComparison.Ordinal))
- {
- var path = AssetDatabase.GUIDToAssetPath(reference.Substring(5));
- if (File.Exists(path)) reference = (string)JObject.Parse(File.ReadAllText(path))["name"];
- }
- return reference == "HotPatch.Runtime";
- }
- public static void Save(AppSettings settings, AssemblyDefinitionAsset assembly)
- {
- if (settings == null) throw new ArgumentNullException(nameof(settings));
- var name = Validate(assembly);
- if (ApplyHybridCLRSettings == null)
- throw new InvalidOperationException("HybridCLR 编辑器集成未就绪,请安装 HybridCLR 并等待编译完成。");
- ApplyHybridCLRSettings(assembly);
- Undo.RecordObject(settings, "Select hot-patch assembly");
- settings.HotPatchAssemblyName = name;
- EditorUtility.SetDirty(settings);
- }
- }
|