AppRuntimesSetupValidation.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Newtonsoft.Json.Linq;
  6. using UnityEditor;
  7. using UnityEditorInternal;
  8. using UnityEngine;
  9. // Run in the Editor; creates only temporary fixtures and preserves project settings.
  10. public static class AppRuntimesSetupValidation
  11. {
  12. public static string Run()
  13. {
  14. var suffix = Guid.NewGuid().ToString("N");
  15. var assets = "Assets/AppRuntimesSetupValidation_" + suffix;
  16. var scratch = Path.GetFullPath("Library/AppRuntimesSetupValidation_" + suffix);
  17. var settings = ScriptableObject.CreateInstance<AppSettings>();
  18. var apply = HotPatchAssemblySelection.ApplyHybridCLRSettings;
  19. int applied = 0;
  20. EditorApplication.LockReloadAssemblies();
  21. AssetDatabase.DisallowAutoRefresh();
  22. try
  23. {
  24. Directory.CreateDirectory(assets);
  25. Directory.CreateDirectory(scratch);
  26. HotPatchAssemblySelection.ApplyHybridCLRSettings = _ => applied++;
  27. var path = assets + "/Game.asmdef";
  28. var name = "AppRuntimes.Validation.Game";
  29. var definition = new JObject { ["name"] = name, ["references"] = new JArray("UniTask"), ["allowUnsafeCode"] = true, ["autoReferenced"] = false };
  30. var assembly = Import(path, definition);
  31. Require(HotPatchAssemblySelection.BuildBlockReason(settings, null, "Game") != null, "missing assembly blocked");
  32. Require(HotPatchAssemblySelection.BuildBlockReason(settings, assembly, " ") != null, "blank scene blocked");
  33. settings.HotPatchAssemblyName = name;
  34. settings.GameSceneName = "Game";
  35. Require(HotPatchAssemblySelection.BuildBlockReason(settings, assembly, "Game") != null, "missing references blocked");
  36. HotPatchAssemblySelection.Save(settings, assembly);
  37. Require(applied == 1 && HotPatchAssemblySelection.HasRequiredReferences(assembly), "Save supplements references and synchronizes HybridCLR");
  38. Require(HotPatchAssemblySelection.BuildBlockReason(settings, assembly, "Game") == null, "saved complete configuration enabled");
  39. Require(HotPatchAssemblySelection.BuildBlockReason(settings) == null, "saved configuration supports menu validation");
  40. Require(HotPatchAssemblySelection.BuildBlockReason(settings, assembly, "OtherGame") != null, "unsaved scene blocked");
  41. var saved = File.ReadAllText(path);
  42. var result = JObject.Parse(saved);
  43. Require((bool)result["allowUnsafeCode"] && !(bool)result["autoReferenced"] && ((JArray)result["references"]).Values<string>().Contains("UniTask"), "existing asmdef fields preserved");
  44. HotPatchAssemblySelection.Save(settings, assembly);
  45. Require(saved == File.ReadAllText(path), "repeated Save does not rewrite or duplicate references");
  46. var basePath = UnityEditor.Compilation.CompilationPipeline.GetAssemblyDefinitionFilePathFromAssemblyName("AppRuntimes.Base");
  47. definition["references"] = new JArray("GUID:" + AssetDatabase.AssetPathToGUID(basePath));
  48. assembly = Import(path, definition);
  49. Require(!HotPatchAssemblySelection.HasRequiredReferences(assembly), "single GUID reference still incomplete");
  50. HotPatchAssemblySelection.Save(settings, assembly);
  51. var guidReferences = ((JArray)JObject.Parse(File.ReadAllText(path))["references"]).Values<string>().ToArray();
  52. Require(guidReferences.Length == 2 && guidReferences.All(r => r.StartsWith("GUID:", StringComparison.Ordinal)), "GUID reference style preserved");
  53. Require(HotPatchAssemblySelection.HasRequiredReferences(assembly), "GUID references recognized");
  54. saved = File.ReadAllText(path);
  55. HotPatchAssemblySelection.Save(settings, assembly);
  56. Require(saved == File.ReadAllText(path), "GUID references remain idempotent");
  57. HotPatchAssemblySelection.Save(settings, null);
  58. Require(settings.HotPatchAssemblyName == "" && HotPatchAssemblySelection.BuildBlockReason(settings) != null, "cleared assembly saved and build disabled");
  59. HotPatchAssemblySelection.Save(settings, assembly);
  60. settings.GameSceneName = "";
  61. Require(HotPatchAssemblySelection.BuildBlockReason(settings) != null, "cleared saved scene disables build");
  62. settings.GameSceneName = "Game";
  63. definition["includePlatforms"] = new JArray("Editor");
  64. assembly = Import(path, definition);
  65. Require(HotPatchAssemblySelection.BuildBlockReason(settings, assembly, "Game") != null, "Editor-only assembly blocked");
  66. var package = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(LaunchTemplateSetup).Assembly);
  67. var source = Path.Combine(package.resolvedPath, "Templates~/Launch");
  68. var target = Path.Combine(scratch, "Launch");
  69. var copy = typeof(LaunchTemplateSetup).GetMethod("CopyIfMissing", BindingFlags.Static | BindingFlags.NonPublic);
  70. Require((bool)copy.Invoke(null, new object[] { source, target }), "missing Launch copied");
  71. foreach (var file in Directory.GetFiles(source, "*", SearchOption.AllDirectories))
  72. Require(File.ReadAllBytes(file).SequenceEqual(File.ReadAllBytes(Path.Combine(target, file.Substring(source.Length + 1)))), "template bytes and metadata preserved");
  73. Require(File.ReadAllBytes(source + ".meta").SequenceEqual(File.ReadAllBytes(target + ".meta")), "folder metadata preserved");
  74. File.WriteAllText(Path.Combine(target, "Launch.cs"), "customized launch");
  75. Require(!(bool)copy.Invoke(null, new object[] { source, target }) && File.ReadAllText(Path.Combine(target, "Launch.cs")) == "customized launch", "existing Launch preserved");
  76. var existingScene = File.ReadAllBytes("Assets/Launch/Launch.unity");
  77. LaunchTemplateSetup.LoadLaunchTemplates();
  78. Require(existingScene.SequenceEqual(File.ReadAllBytes("Assets/Launch/Launch.unity")), "manual template loading preserves project Launch");
  79. Require(UnityMcpPackageSetup.IsInstalled && HybridCLRPackageSetup.IsInstalled, "existing dependencies detected");
  80. var manifest = File.ReadAllBytes("Packages/manifest.json");
  81. UnityMcpPackageSetup.Install();
  82. HybridCLRPackageSetup.RetryInstallation();
  83. Require(manifest.SequenceEqual(File.ReadAllBytes("Packages/manifest.json")), "installed packages not reinstalled");
  84. S3UploadSettings.ValidateDestination(S3UploadSettings.Bucket, S3UploadSettings.Region);
  85. S3UploadSettings.ValidateCredentials(S3UploadSettings.LoadCredentials());
  86. Require(S3UploadSettings.ConfigurationPath.Replace('\\', '/').Contains("/Editor/Config/"), "S3 settings remain Editor-only");
  87. return "PASS: Save/reference completion (names and GUIDs), build eligibility, Launch copying/preservation, installed-package detection, package S3 configuration.";
  88. }
  89. finally
  90. {
  91. HotPatchAssemblySelection.ApplyHybridCLRSettings = apply;
  92. UnityEngine.Object.DestroyImmediate(settings);
  93. AssetDatabase.DeleteAsset(assets);
  94. if (Directory.Exists(scratch)) Directory.Delete(scratch, true);
  95. AssetDatabase.AllowAutoRefresh();
  96. EditorApplication.UnlockReloadAssemblies();
  97. }
  98. }
  99. static AssemblyDefinitionAsset Import(string path, JObject definition)
  100. {
  101. File.WriteAllText(path, definition.ToString());
  102. AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceSynchronousImport);
  103. return AssetDatabase.LoadAssetAtPath<AssemblyDefinitionAsset>(path);
  104. }
  105. static void Require(bool value, string check)
  106. {
  107. if (!value) throw new InvalidOperationException("AppRuntimes setup validation failed: " + check);
  108. }
  109. }