using System; using System.IO; using System.Linq; using System.Text; using Newtonsoft.Json.Linq; using UnityEditor; using UnityEditorInternal; public static class HotPatchAssemblySelection { public static Action ApplyHybridCLRSettings; static readonly string[] RequiredReferences = { "AppRuntimes.Base", "Unity.Advertisement.IosSupport" }; 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(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(File.ReadAllText(path)); 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().Any(IsLoaderReference)) throw new InvalidOperationException("宿主或热补丁加载器程序集不能作为热更目标。"); return name; } static bool IsLoaderReference(string reference) { return ResolveReferenceName(reference) == "HotPatch.Runtime"; } static string ResolveReferenceName(string reference) { if (reference != null && 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; } public static bool HasRequiredReferences(AssemblyDefinitionAsset assembly) { if (assembly == null) return false; var definition = JObject.Parse(File.ReadAllText(AssetDatabase.GetAssetPath(assembly))); var references = ((JArray)definition["references"] ?? new JArray()).Values().Select(ResolveReferenceName).ToArray(); return RequiredReferences.All(references.Contains); } public static void EnsureRequiredReferences(AssemblyDefinitionAsset assembly) { Validate(assembly); var path = AssetDatabase.GetAssetPath(assembly); var definition = JObject.Parse(File.ReadAllText(path)); var references = (JArray)definition["references"] ?? new JArray(); var names = references.Values().Select(ResolveReferenceName).ToArray(); var missing = RequiredReferences.Where(name => !names.Contains(name)).ToArray(); if (missing.Length == 0) return; bool useGuids = references.Values().Any(reference => reference != null && reference.StartsWith("GUID:", StringComparison.Ordinal)); foreach (var name in missing) { var reference = name; if (useGuids) { var dependency = UnityEditor.Compilation.CompilationPipeline.GetAssemblyDefinitionFilePathFromAssemblyName(name); var guid = string.IsNullOrEmpty(dependency) ? "" : AssetDatabase.AssetPathToGUID(dependency); if (string.IsNullOrEmpty(guid)) throw new InvalidOperationException("找不到必需程序集:" + name + ",请等待依赖包导入完成。"); reference = "GUID:" + guid; } references.Add(reference); } definition["references"] = references; File.WriteAllText(path, definition.ToString() + "\n", new UTF8Encoding(false)); AssetDatabase.ImportAsset(path); if (!HasRequiredReferences(assembly)) throw new InvalidOperationException("程序集引用补全失败,请检查 asmdef 后重新 Save。"); } public static string BuildBlockReason(AppSettings settings, AssemblyDefinitionAsset assembly, string sceneName) { if (settings == null || assembly == null || string.IsNullOrWhiteSpace(sceneName)) return "请设置 Hot Patch Assembly、Game Scene Name 并点击 Save。"; try { var name = Validate(assembly); if (!HasRequiredReferences(assembly)) return "程序集缺少 AppRuntimes.Base 或 Unity.Advertisement.IosSupport 引用,请点击 Save 自动补全。"; if (settings.HotPatchAssemblyName != name || string.IsNullOrWhiteSpace(settings.GameSceneName) || settings.GameSceneName.Trim() != sceneName.Trim()) return "程序集或场景设置尚未保存,请点击 Save。"; if (ApplyHybridCLRSettings == null) return "请等待 HybridCLR 安装和编译完成。"; return null; } catch (Exception exception) { return exception.Message; } } public static string BuildBlockReason(AppSettings settings) { if (settings == null) return "请先配置 AppSettings 并点击 Save。"; try { return BuildBlockReason(settings, Find(settings.HotPatchAssemblyName), settings.GameSceneName); } catch (Exception exception) { return exception.Message; } } public static void Save(AppSettings settings, AssemblyDefinitionAsset assembly) { if (settings == null) throw new ArgumentNullException(nameof(settings)); var name = assembly != null ? Validate(assembly) : string.Empty; if (assembly != null) { EnsureRequiredReferences(assembly); if (ApplyHybridCLRSettings == null) throw new InvalidOperationException("HybridCLR 编辑器集成未就绪,请安装 HybridCLR 并等待编译完成。"); } ApplyHybridCLRSettings?.Invoke(assembly); Undo.RecordObject(settings, "Select hot-patch assembly"); settings.HotPatchAssemblyName = name; EditorUtility.SetDirty(settings); } }