DevilLink2BoardSetup.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.IO;
  3. using DevilLink2.Presentation;
  4. using UnityEditor;
  5. using UnityEditor.SceneManagement;
  6. using UnityEditor.TestTools.TestRunner.Api;
  7. using UnityEngine;
  8. using UnityEngine.EventSystems;
  9. using UnityEngine.SceneManagement;
  10. using UnityEngine.UI;
  11. namespace DevilLink2.Editor
  12. {
  13. [InitializeOnLoad]
  14. public static class DevilLink2BoardSetup
  15. {
  16. public const string ScenePath = "Assets/DevilLink2/Scenes/Main.unity";
  17. public const string PrefabPath = "Assets/DevilLink2/Prefabs/Gameplay/TileView.prefab";
  18. public const string RootName = "DevilLink2BoardDemo";
  19. private const string SessionKey = "DevilLink2.Task04B.ReadyV1";
  20. private static TestRunnerApi runner;
  21. static DevilLink2BoardSetup() { EditorApplication.delayCall += AutoSetup; }
  22. private static void AutoSetup()
  23. {
  24. if (EditorApplication.isPlayingOrWillChangePlaymode || SessionState.GetBool(SessionKey, false) ||
  25. File.Exists("Temp/DevilLink2BoardValidation/Task04BResults.xml")) return;
  26. if (EditorApplication.isCompiling || EditorApplication.isUpdating)
  27. { EditorApplication.delayCall += AutoSetup; return; }
  28. SessionState.SetBool(SessionKey, true);
  29. try
  30. {
  31. SetupInteraction();
  32. SetupInteraction();
  33. EditorApplication.delayCall += RunValidation;
  34. }
  35. catch (Exception e) { Debug.LogError("[DevilLink2.Task04B] SETUP BLOCKED: " + e); }
  36. }
  37. [MenuItem("Tools/Devil Link 2/Set Up Board Interaction")]
  38. public static void SetupInteraction()
  39. {
  40. Setup();
  41. var scene = SceneManager.GetActiveScene(); var root = FindRoot(scene);
  42. if (!root.GetComponent<BoardInteractionController>())
  43. {
  44. root.AddComponent<BoardInteractionController>();
  45. EditorSceneManager.MarkSceneDirty(scene); EditorSceneManager.SaveScene(scene);
  46. }
  47. Debug.Log("[DevilLink2.Task04B] Interaction configured; existing layout retained.");
  48. }
  49. [MenuItem("Tools/Devil Link 2/Set Up Board Demo")]
  50. public static void Setup()
  51. {
  52. if (EditorApplication.isPlayingOrWillChangePlaymode) throw new InvalidOperationException("Exit Play Mode before setup.");
  53. var scene = SceneManager.GetActiveScene();
  54. if (scene.path != ScenePath || scene.isDirty)
  55. throw new InvalidOperationException("Setup requires the saved Main scene active, with no pending scene edits. No other scene will be opened or saved.");
  56. var existing = FindRoot(scene);
  57. if (existing)
  58. {
  59. if (!AssetDatabase.LoadAssetAtPath<GameObject>(PrefabPath) || !existing.GetComponentInChildren<BoardView>(true))
  60. throw new InvalidOperationException("Existing demo is incomplete; inspect it before running setup again.");
  61. Debug.Log("[DevilLink2.Task04B] SETUP idempotence: existing root and prefab retained.");
  62. return;
  63. }
  64. EnsureFolder("Assets/DevilLink2/Prefabs", "Gameplay");
  65. var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(PrefabPath);
  66. if (!prefab)
  67. {
  68. var tile = Rect("TileView", null);
  69. try
  70. {
  71. var outline = tile.gameObject.AddComponent<Image>(); outline.color = new Color32(255, 211, 77, 255); outline.raycastTarget = false;
  72. tile.sizeDelta = new Vector2(42, 42);
  73. var face = Rect("Face", tile); Stretch(face, 1, 1, 1, 1);
  74. var image = face.gameObject.AddComponent<Image>(); image.color = new Color32(26, 35, 56, 255); image.raycastTarget = false;
  75. var label = MakeText("Symbol", face, "IMP", 11); Stretch((RectTransform)label.transform, 1, 1, 1, 1);
  76. tile.gameObject.AddComponent<TileView>().Configure(image, outline, label);
  77. prefab = PrefabUtility.SaveAsPrefabAsset(tile.gameObject, PrefabPath);
  78. if (!prefab) throw new InvalidOperationException("Tile prefab could not be saved.");
  79. }
  80. finally { UnityEngine.Object.DestroyImmediate(tile.gameObject); }
  81. }
  82. var root = Rect(RootName, null);
  83. SceneManager.MoveGameObjectToScene(root.gameObject, scene);
  84. var canvas = root.gameObject.AddComponent<Canvas>(); canvas.renderMode = RenderMode.ScreenSpaceOverlay;
  85. var scaler = root.gameObject.AddComponent<CanvasScaler>(); scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
  86. scaler.referenceResolution = new Vector2(430, 932); scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.Expand;
  87. root.gameObject.AddComponent<GraphicRaycaster>();
  88. var background = Rect("Background", root); Stretch(background);
  89. var bg = background.gameObject.AddComponent<Image>(); bg.color = new Color32(5, 8, 15, 255); bg.raycastTarget = false;
  90. var safe = Rect("SafeArea", root); Stretch(safe); safe.gameObject.AddComponent<SafeAreaAdapter>();
  91. var frame = Rect("PortraitFrame", safe); frame.anchorMin = frame.anchorMax = frame.pivot = new Vector2(.5f, .5f);
  92. var aspect = frame.gameObject.AddComponent<AspectRatioFitter>(); aspect.aspectMode = AspectRatioFitter.AspectMode.FitInParent; aspect.aspectRatio = 430f / 932;
  93. var frameImage = frame.gameObject.AddComponent<Image>(); frameImage.color = new Color32(13, 18, 32, 255); frameImage.raycastTarget = false;
  94. var title = MakeText("Title", frame, "DEVIL LINK 2 / CLASSIC 01", 15);
  95. var titleRect = (RectTransform)title.transform; titleRect.anchorMin = new Vector2(0, 1); titleRect.anchorMax = Vector2.one;
  96. titleRect.pivot = new Vector2(.5f, 1); titleRect.sizeDelta = new Vector2(0, 48); titleRect.anchoredPosition = Vector2.zero;
  97. var viewport = Rect("BoardViewport", frame); Stretch(viewport, 6, 12, 6, 60);
  98. var hit = viewport.gameObject.AddComponent<Image>(); hit.color = new Color(0, 0, 0, 0); hit.raycastTarget = true;
  99. viewport.gameObject.AddComponent<RectMask2D>();
  100. var scroll = viewport.gameObject.AddComponent<ScrollRect>(); scroll.viewport = viewport;
  101. scroll.horizontal = false; scroll.vertical = true; scroll.movementType = ScrollRect.MovementType.Clamped;
  102. var content = Rect("Content", viewport); content.anchorMin = new Vector2(0, 1); content.anchorMax = Vector2.one;
  103. content.pivot = new Vector2(.5f, 1); content.anchoredPosition = Vector2.zero; content.sizeDelta = Vector2.zero;
  104. scroll.content = content;
  105. var board = Rect("Board", content);
  106. var panel = board.gameObject.AddComponent<Image>(); panel.color = new Color32(5, 8, 15, 255); panel.raycastTarget = false;
  107. var view = viewport.gameObject.AddComponent<BoardView>(); view.Configure(prefab.GetComponent<TileView>(), viewport, content, board);
  108. root.gameObject.AddComponent<BoardDemoBootstrap>().Configure(view);
  109. if (!UnityEngine.Object.FindFirstObjectByType<EventSystem>())
  110. {
  111. var events = new GameObject("EventSystem", typeof(EventSystem), typeof(StandaloneInputModule));
  112. events.transform.SetParent(root, false);
  113. }
  114. Canvas.ForceUpdateCanvases();
  115. EditorSceneManager.MarkSceneDirty(scene);
  116. if (!EditorSceneManager.SaveScene(scene)) throw new InvalidOperationException("Main could not be saved.");
  117. Debug.Log("[DevilLink2.Task04B] SETUP COMPLETE: Main + TileView.prefab. Runtime board: 4 columns x 8 rows, seed 2026.");
  118. }
  119. private static GameObject FindRoot(Scene scene)
  120. {
  121. GameObject found = null;
  122. foreach (var root in scene.GetRootGameObjects()) if (root.name == RootName)
  123. { if (found) throw new InvalidOperationException("Duplicate managed demo roots."); found = root; }
  124. return found;
  125. }
  126. private static void EnsureFolder(string parent, string name)
  127. { if (!AssetDatabase.IsValidFolder(parent + "/" + name)) AssetDatabase.CreateFolder(parent, name); }
  128. private static RectTransform Rect(string name, Transform parent)
  129. {
  130. var rect = (RectTransform)new GameObject(name, typeof(RectTransform)).transform;
  131. if (parent) rect.SetParent(parent, false);
  132. return rect;
  133. }
  134. private static void Stretch(RectTransform rect, float left = 0, float bottom = 0, float right = 0, float top = 0)
  135. { rect.anchorMin = Vector2.zero; rect.anchorMax = Vector2.one; rect.offsetMin = new Vector2(left, bottom); rect.offsetMax = new Vector2(-right, -top); }
  136. private static Text MakeText(string name, Transform parent, string value, int size)
  137. {
  138. var text = Rect(name, parent).gameObject.AddComponent<Text>();
  139. text.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf"); text.text = value; text.fontSize = size;
  140. text.alignment = TextAnchor.MiddleCenter; text.color = new Color32(220, 230, 247, 255); text.raycastTarget = false;
  141. return text;
  142. }
  143. [MenuItem("Tools/Devil Link 2/Validate Board Demo")]
  144. public static void RunValidation()
  145. {
  146. runner = ScriptableObject.CreateInstance<TestRunnerApi>(); runner.RegisterCallbacks(new Results());
  147. runner.Execute(new ExecutionSettings(new Filter { testMode = TestMode.EditMode,
  148. assemblyNames = new[] { "DevilLink2.Core.Tests", "DevilLink2.Presentation.Tests", "DevilLink2.Interaction.Tests" } }));
  149. }
  150. private sealed class Results : ICallbacks
  151. {
  152. public void RunStarted(ITestAdaptor testsToRun) { Debug.Log("[DevilLink2.Task04B] TESTS STARTED"); }
  153. public void RunFinished(ITestResultAdaptor result)
  154. {
  155. Directory.CreateDirectory("Temp/DevilLink2BoardValidation");
  156. TestRunnerApi.SaveResultToFile(result, "Temp/DevilLink2BoardValidation/Task04BResults.xml");
  157. Debug.Log($"[DevilLink2.Task04B] TESTS FINISHED passed={result.PassCount} failed={result.FailCount} skipped={result.SkipCount}");
  158. runner.UnregisterCallbacks(this); UnityEngine.Object.DestroyImmediate(runner);
  159. }
  160. public void TestStarted(ITestAdaptor test) { }
  161. public void TestFinished(ITestResultAdaptor result)
  162. { if (result.TestStatus == TestStatus.Failed) Debug.LogError("[DevilLink2.Task04B] TEST FAILED " + result.FullName + " " + result.Message + " " + result.StackTrace); }
  163. }
  164. }
  165. }