UIPlayModeValidation.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using DevilLink2.Gameplay;
  5. using DevilLink2.Persistence;
  6. using DevilLink2.Presentation;
  7. using DevilLink2.UI;
  8. using UnityEditor;
  9. using UnityEngine;
  10. using UnityEngine.SceneManagement;
  11. using Object = UnityEngine.Object;
  12. namespace DevilLink2.Editor
  13. {
  14. /// <summary>Actual Main scene Play Mode acceptance, entirely through structured code.
  15. /// No scene is saved. The test profile always lives under Temp, never persistentDataPath.</summary>
  16. [InitializeOnLoad]
  17. public static class UIPlayModeValidation
  18. {
  19. private const string Prefix = "DevilLink2.Task08A.Play.";
  20. private static string ReportPath => SessionState.GetString(Prefix + "Scenario", "08A") == "12" ? "Temp/DevilLink2Task12/PlayModeResults.json" : SessionState.GetString(Prefix + "Scenario", "08A") == "11C" ? "Temp/DevilLink2Task11C/PlayModeResults.json" : SessionState.GetString(Prefix + "Scenario", "08A") == "11A2" ? "Temp/DevilLink2Task11A2/PlayModeResults.json" : SessionState.GetString(Prefix + "Scenario", "08A") == "08C6" ? "Temp/DevilLink2Task08C6/PlayModeResults.json" : SessionState.GetString(Prefix + "Scenario", "08A") == "08C5" ? "Temp/DevilLink2Task08C5/PlayModeResults.json" : SessionState.GetString(Prefix + "Scenario", "08A") == "08C4" ? "Temp/DevilLink2Task08C4/PlayModeResults.json" : SessionState.GetString(Prefix + "Scenario", "08A") == "08C3" ? "Temp/DevilLink2Task08C3/PlayModeResults.json" : SessionState.GetString(Prefix + "Scenario", "08A") == "08C2" ? "Temp/DevilLink2Task08C2/PlayModeResults.json" : SessionState.GetString(Prefix + "Scenario", "08A") == "08C1" ? "Temp/DevilLink2Task08C1/PlayModeResults.json" : SessionState.GetString(Prefix + "Scenario", "08A") == "08B" ? "Temp/DevilLink2Task08B/PlayModeResults.json" : "Temp/DevilLink2Task08A/PlayModeResults.json";
  21. private sealed class FixedUtc : IUtcClock
  22. { public DateTimeOffset UtcNow => new DateTimeOffset(2026,11,3,12,0,0,TimeSpan.Zero); }
  23. [Serializable] private sealed class Report
  24. { public bool Passed; public string Scene; public string Checks; public string Error; }
  25. static UIPlayModeValidation()
  26. {
  27. EditorApplication.update += Tick;
  28. EditorApplication.playModeStateChanged += Changed;
  29. InstallOverride();
  30. }
  31. [InitializeOnEnterPlayMode]
  32. private static void BeforePlay(EnterPlayModeOptions options) { InstallOverride(); }
  33. private static void InstallOverride()
  34. {
  35. if (!SessionState.GetBool(MainUIInstaller.ValidationSessionKey, false)) return;
  36. string path = SessionState.GetString(Prefix + "Directory", "");
  37. MainUIInstaller.PlayerLoaderOverride = () => new SavedPlayer(new LocalSaveService(new FileSaveStorage(path)), new FixedUtc());
  38. }
  39. [MenuItem("Tools/Devil Link 2/Validate Start Lobby Play Mode")]
  40. public static void Run() { RunScenario("08A"); }
  41. [MenuItem("Tools/Devil Link 2/Validate Game HUD Pause Result Play Mode")]
  42. public static void RunGameHUD() { RunScenario("08B"); }
  43. [MenuItem("Tools/Devil Link 2/Validate Shop Play Mode")]
  44. public static void RunShop() { RunScenario("08C1"); }
  45. [MenuItem("Tools/Devil Link 2/Validate Daily Check-in Play Mode")]
  46. public static void RunDaily() { RunScenario("08C2"); }
  47. [MenuItem("Tools/Devil Link 2/Validate Lucky Wheel Play Mode")]
  48. public static void RunWheel() { RunScenario("08C3"); }
  49. [MenuItem("Tools/Devil Link 2/Validate Profile Play Mode")]
  50. public static void RunProfile(){RunScenario("08C4");}
  51. [MenuItem("Tools/Devil Link 2/Validate Settings Play Mode")]
  52. public static void RunSettings(){RunScenario("08C5");}
  53. [MenuItem("Tools/Devil Link 2/Validate Help Play Mode")]
  54. public static void RunHelp(){RunScenario("08C6");}
  55. [MenuItem("Tools/Devil Link 2/Validate Lobby Visual Play Mode")]
  56. public static void RunLobbyVisuals(){RunScenario("11A2");}
  57. [MenuItem("Tools/Devil Link 2/Validate Functional Screens Play Mode")]
  58. public static void RunFunctional(){RunScenario("11C");}
  59. [MenuItem("Tools/Devil Link 2/Validate Task 12 Runtime Matrix")]
  60. public static void RunTask12(){Task12Acceptance.Reset();RunScenario("12");}
  61. private static void RunScenario(string scenario)
  62. {
  63. var scene = SceneManager.GetActiveScene();
  64. if (EditorApplication.isPlayingOrWillChangePlaymode || EditorApplication.isCompiling || scene.isDirty || scene.path != "Assets/DevilLink2/Scenes/Main.unity")
  65. throw new InvalidOperationException("Requires the saved Main scene in idle EditMode. No scenes will be opened or saved.");
  66. SessionState.SetString(Prefix + "Scenario", scenario);
  67. string path = Path.GetFullPath(Path.Combine("Temp/DevilLink2Task" + scenario + "/PlayProfiles", Guid.NewGuid().ToString("N")));
  68. if(scenario=="12" && SessionState.GetBool("DL2Task12.Reopen",false))path=SessionState.GetString("DL2Task12.Profile",path);
  69. else new LocalSaveService(new FileSaveStorage(path)).Save(new SaveData {
  70. Coins = 9876, SpinTickets = 7, ClassicProgress = scenario == "12" ? 19 : scenario == "08B" ? 19 : 2, TimeAttackProgress = scenario == "12" ? 19 : scenario == "08B" ? 0 : 3, ZenProgress = scenario == "12" ? 19 : scenario == "08B" ? 0 : 19,
  71. HintCount = scenario == "08B" ? 2 : 1, ShuffleCount = scenario == "08B" ? 2 : 1, TimeCount = scenario == "08B" ? 3 : 0
  72. });
  73. if(scenario=="12")SessionState.SetString("DL2Task12.Profile",path);
  74. SessionState.SetString(Prefix + "Directory", path); SessionState.SetInt(Prefix + "Step", 0);
  75. SessionState.SetString(Prefix + "Checks", ""); SessionState.SetFloat(Prefix + "Start", (float)EditorApplication.timeSinceStartup);
  76. SessionState.SetBool(MainUIInstaller.ValidationSessionKey, true); InstallOverride();
  77. WheelPlayAcceptance.Reset(); HUDPlayAcceptance.Reset(); ShopPlayAcceptance.Reset();
  78. EditorApplication.isPlaying = true;
  79. }
  80. private static void Check(bool condition, string name)
  81. {
  82. if (!condition) throw new InvalidOperationException(name);
  83. SessionState.SetString(Prefix + "Checks", SessionState.GetString(Prefix + "Checks", "") + name + "\n");
  84. }
  85. private static void Tick()
  86. {
  87. if (!SessionState.GetBool(MainUIInstaller.ValidationSessionKey, false) || !EditorApplication.isPlaying || EditorApplication.isPaused) return;
  88. try
  89. {
  90. if (EditorApplication.timeSinceStartup - SessionState.GetFloat(Prefix + "Start", 0) > (SessionState.GetString(Prefix+"Scenario","")=="12"?600:180)) throw new TimeoutException("UI startup did not finish within the acceptance timeout.");
  91. var manager = Object.FindFirstObjectByType<UIManager>();
  92. if (!manager || !manager.IsInitialized || manager.transform.localScale.x <= 0) return;
  93. var bootstrap = Object.FindFirstObjectByType<BoardDemoBootstrap>();
  94. if (SessionState.GetString(Prefix + "Scenario", "08A") == "12")
  95. {if(Task12Acceptance.Tick(manager,bootstrap,Check))Finish(true,null);return;}
  96. if (SessionState.GetString(Prefix + "Scenario", "08A") == "11C")
  97. {if(FunctionalScreensAcceptance.Tick(manager,bootstrap,Check))Finish(true,null);return;}
  98. if (SessionState.GetString(Prefix + "Scenario", "08A") == "11A2")
  99. {if(LobbyArtPlayAcceptance.Tick(manager,bootstrap,Check))Finish(true,null);return;}
  100. if (SessionState.GetString(Prefix + "Scenario", "08A") == "08C6")
  101. {if(HelpPlayAcceptance.Tick(manager,bootstrap,Check))Finish(true,null);return;}
  102. if (SessionState.GetString(Prefix + "Scenario", "08A") == "08C5")
  103. {if(SettingsPlayAcceptance.Tick(manager,bootstrap,Check))Finish(true,null);return;}
  104. if (SessionState.GetString(Prefix + "Scenario", "08A") == "08C4")
  105. {if(ProfilePlayAcceptance.Tick(manager,bootstrap,Check))Finish(true,null);return;}
  106. if (SessionState.GetString(Prefix + "Scenario", "08A") == "08C3")
  107. { if (WheelPlayAcceptance.Tick(manager, bootstrap, Check)) Finish(true, null); return; }
  108. if (SessionState.GetString(Prefix + "Scenario", "08A") == "08C2")
  109. { if (DailyPlayAcceptance.Tick(manager, bootstrap, Check)) Finish(true, null); return; }
  110. if (SessionState.GetString(Prefix + "Scenario", "08A") == "08C1")
  111. { if (ShopPlayAcceptance.Tick(manager, bootstrap, Check)) Finish(true, null); return; }
  112. if (SessionState.GetString(Prefix + "Scenario", "08A") == "08B")
  113. { if (HUDPlayAcceptance.Tick(manager, bootstrap, Check)) Finish(true, null); return; }
  114. int step = SessionState.GetInt(Prefix + "Step", 0);
  115. if (step == 0)
  116. {
  117. Check(SceneManager.GetActiveScene().path == "Assets/DevilLink2/Scenes/Main.unity", "Actual Main scene running");
  118. Check(manager.CurrentPage == UIPage.Start && bootstrap.Session == null, "Start is first; no eager GameSession");
  119. Check(bootstrap.GetComponentsInChildren<Canvas>(true).Length == 1, "Single existing Canvas reused");
  120. manager.StartScreen.StartButton.onClick.Invoke(); manager.StartScreen.StartButton.onClick.Invoke();
  121. Check(manager.CurrentPage == UIPage.Lobby, "Repeated Start enters one Lobby");
  122. Check(manager.Lobby.CoinsLabel.text == "9,876" && manager.Lobby.TicketsLabel.text == "7", "Loaded economy displayed");
  123. }
  124. else if (step <= 6)
  125. {
  126. int index = (step - 1) / 2;
  127. if (step % 2 == 1)
  128. {
  129. manager.Lobby.Cards[index].Button.onClick.Invoke();
  130. var session = bootstrap.Session; var mode = (GameMode)index; int level = index == 0 ? 3 : index == 1 ? 4 : 20;
  131. var expected = LevelCatalog.Get(mode, level);
  132. Check(manager.CurrentPage == UIPage.Gameplay && session.Mode == mode && session.Definition.Level == level, mode + " independent next level");
  133. Check(session.Board.Columns == expected.Columns && session.Board.Rows == expected.Rows && session.Board.RemainingTileCount == expected.Columns * expected.Rows, mode + " real board dimensions");
  134. Check(bootstrap.Flow.Runtime.Timer.InitialTime == expected.TimeLimitSeconds && bootstrap.Flow.Runtime.Timer.HasTimer == expected.HasTimer, mode + " timer parameters");
  135. Check(manager.GameTitle.text.Contains("LEVEL " + level), mode + " title");
  136. var id = session.Id; manager.Lobby.Cards[index].Button.onClick.Invoke();
  137. Check(bootstrap.Session.Id == id, mode + " hidden/duplicate input ignored");
  138. }
  139. else
  140. {
  141. manager.LobbyButton.onClick.Invoke();
  142. Check(manager.CurrentPage == UIPage.Lobby && bootstrap.Flow.State == GameFlowState.Abandoned, "Return to Lobby abandons run");
  143. }
  144. }
  145. else { Finish(true, null); return; }
  146. SessionState.SetInt(Prefix + "Step", step + 1);
  147. }
  148. catch (Exception error) { Finish(false, error.ToString()); }
  149. }
  150. private static void Finish(bool passed, string error)
  151. {
  152. Directory.CreateDirectory(Path.GetDirectoryName(ReportPath));
  153. File.WriteAllText(ReportPath, JsonUtility.ToJson(new Report { Passed = passed, Scene = SceneManager.GetActiveScene().path,
  154. Checks = SessionState.GetString(Prefix + "Checks", ""), Error = error }, true));
  155. if(SessionState.GetString(Prefix+"Scenario","")=="12")Task12Acceptance.Restore();
  156. SessionState.SetBool(MainUIInstaller.ValidationSessionKey, false); MainUIInstaller.PlayerLoaderOverride = null;
  157. if (passed) Debug.Log("[DevilLink2.UI] PLAY MODE ACCEPTANCE PASSED: " + ReportPath);
  158. else Debug.LogError("[DevilLink2.UI] PLAY MODE ACCEPTANCE FAILED: " + error);
  159. if (EditorApplication.isPlaying) EditorApplication.isPlaying = false;
  160. }
  161. private static void Changed(PlayModeStateChange state)
  162. {
  163. if (state == PlayModeStateChange.EnteredPlayMode) InstallOverride();
  164. if (state == PlayModeStateChange.ExitingPlayMode && SessionState.GetBool(MainUIInstaller.ValidationSessionKey, false))
  165. Finish(false, "Play Mode was stopped before acceptance finished.");
  166. }
  167. }
  168. }