using System; using System.IO; using System.Linq; using System.Reflection; using Newtonsoft.Json.Linq; using UnityEditor; using UnityEditorInternal; using UnityEngine; // Run in the Editor; creates only temporary fixtures and preserves project settings. public static class AppRuntimesSetupValidation { public static string Run() { var suffix = Guid.NewGuid().ToString("N"); var assets = "Assets/AppRuntimesSetupValidation_" + suffix; var scratch = Path.GetFullPath("Library/AppRuntimesSetupValidation_" + suffix); var settings = ScriptableObject.CreateInstance(); var apply = HotPatchAssemblySelection.ApplyHybridCLRSettings; int applied = 0; EditorApplication.LockReloadAssemblies(); AssetDatabase.DisallowAutoRefresh(); try { Directory.CreateDirectory(assets); Directory.CreateDirectory(scratch); HotPatchAssemblySelection.ApplyHybridCLRSettings = _ => applied++; var path = assets + "/Game.asmdef"; var name = "AppRuntimes.Validation.Game"; var definition = new JObject { ["name"] = name, ["references"] = new JArray("UniTask"), ["allowUnsafeCode"] = true, ["autoReferenced"] = false }; var assembly = Import(path, definition); Require(HotPatchAssemblySelection.BuildBlockReason(settings, null, "Game") != null, "missing assembly blocked"); Require(HotPatchAssemblySelection.BuildBlockReason(settings, assembly, " ") != null, "blank scene blocked"); settings.HotPatchAssemblyName = name; settings.GameSceneName = "Game"; Require(HotPatchAssemblySelection.BuildBlockReason(settings, assembly, "Game") != null, "missing references blocked"); HotPatchAssemblySelection.Save(settings, assembly); Require(applied == 1 && HotPatchAssemblySelection.HasRequiredReferences(assembly), "Save supplements references and synchronizes HybridCLR"); Require(HotPatchAssemblySelection.BuildBlockReason(settings, assembly, "Game") == null, "saved complete configuration enabled"); Require(HotPatchAssemblySelection.BuildBlockReason(settings) == null, "saved configuration supports menu validation"); Require(HotPatchAssemblySelection.BuildBlockReason(settings, assembly, "OtherGame") != null, "unsaved scene blocked"); var saved = File.ReadAllText(path); var result = JObject.Parse(saved); Require((bool)result["allowUnsafeCode"] && !(bool)result["autoReferenced"] && ((JArray)result["references"]).Values().Contains("UniTask"), "existing asmdef fields preserved"); HotPatchAssemblySelection.Save(settings, assembly); Require(saved == File.ReadAllText(path), "repeated Save does not rewrite or duplicate references"); var basePath = UnityEditor.Compilation.CompilationPipeline.GetAssemblyDefinitionFilePathFromAssemblyName("AppRuntimes.Base"); definition["references"] = new JArray("GUID:" + AssetDatabase.AssetPathToGUID(basePath)); assembly = Import(path, definition); Require(!HotPatchAssemblySelection.HasRequiredReferences(assembly), "single GUID reference still incomplete"); HotPatchAssemblySelection.Save(settings, assembly); var guidReferences = ((JArray)JObject.Parse(File.ReadAllText(path))["references"]).Values().ToArray(); Require(guidReferences.Length == 2 && guidReferences.All(r => r.StartsWith("GUID:", StringComparison.Ordinal)), "GUID reference style preserved"); Require(HotPatchAssemblySelection.HasRequiredReferences(assembly), "GUID references recognized"); saved = File.ReadAllText(path); HotPatchAssemblySelection.Save(settings, assembly); Require(saved == File.ReadAllText(path), "GUID references remain idempotent"); HotPatchAssemblySelection.Save(settings, null); Require(settings.HotPatchAssemblyName == "" && HotPatchAssemblySelection.BuildBlockReason(settings) != null, "cleared assembly saved and build disabled"); HotPatchAssemblySelection.Save(settings, assembly); settings.GameSceneName = ""; Require(HotPatchAssemblySelection.BuildBlockReason(settings) != null, "cleared saved scene disables build"); settings.GameSceneName = "Game"; definition["includePlatforms"] = new JArray("Editor"); assembly = Import(path, definition); Require(HotPatchAssemblySelection.BuildBlockReason(settings, assembly, "Game") != null, "Editor-only assembly blocked"); var package = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(LaunchTemplateSetup).Assembly); var source = Path.Combine(package.resolvedPath, "Templates~/Launch"); var target = Path.Combine(scratch, "Launch"); var copy = typeof(LaunchTemplateSetup).GetMethod("CopyIfMissing", BindingFlags.Static | BindingFlags.NonPublic); Require((bool)copy.Invoke(null, new object[] { source, target }), "missing Launch copied"); foreach (var file in Directory.GetFiles(source, "*", SearchOption.AllDirectories)) Require(File.ReadAllBytes(file).SequenceEqual(File.ReadAllBytes(Path.Combine(target, file.Substring(source.Length + 1)))), "template bytes and metadata preserved"); Require(File.ReadAllBytes(source + ".meta").SequenceEqual(File.ReadAllBytes(target + ".meta")), "folder metadata preserved"); File.WriteAllText(Path.Combine(target, "Launch.cs"), "customized launch"); Require(!(bool)copy.Invoke(null, new object[] { source, target }) && File.ReadAllText(Path.Combine(target, "Launch.cs")) == "customized launch", "existing Launch preserved"); var existingScene = File.ReadAllBytes("Assets/Launch/Launch.unity"); LaunchTemplateSetup.LoadLaunchTemplates(); Require(existingScene.SequenceEqual(File.ReadAllBytes("Assets/Launch/Launch.unity")), "manual template loading preserves project Launch"); Require(UnityMcpPackageSetup.IsInstalled && HybridCLRPackageSetup.IsInstalled, "existing dependencies detected"); var manifest = File.ReadAllBytes("Packages/manifest.json"); UnityMcpPackageSetup.Install(); HybridCLRPackageSetup.RetryInstallation(); Require(manifest.SequenceEqual(File.ReadAllBytes("Packages/manifest.json")), "installed packages not reinstalled"); S3UploadSettings.ValidateDestination(S3UploadSettings.Bucket, S3UploadSettings.Region); S3UploadSettings.ValidateCredentials(S3UploadSettings.LoadCredentials()); Require(S3UploadSettings.ConfigurationPath.Replace('\\', '/').Contains("/Editor/Config/"), "S3 settings remain Editor-only"); return "PASS: Save/reference completion (names and GUIDs), build eligibility, Launch copying/preservation, installed-package detection, package S3 configuration."; } finally { HotPatchAssemblySelection.ApplyHybridCLRSettings = apply; UnityEngine.Object.DestroyImmediate(settings); AssetDatabase.DeleteAsset(assets); if (Directory.Exists(scratch)) Directory.Delete(scratch, true); AssetDatabase.AllowAutoRefresh(); EditorApplication.UnlockReloadAssemblies(); } } static AssemblyDefinitionAsset Import(string path, JObject definition) { File.WriteAllText(path, definition.ToString()); AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceSynchronousImport); return AssetDatabase.LoadAssetAtPath(path); } static void Require(bool value, string check) { if (!value) throw new InvalidOperationException("AppRuntimes setup validation failed: " + check); } }