MainUIInstaller.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.IO;
  3. using DevilLink2.Persistence;
  4. using DevilLink2.Presentation;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. using UnityEngine.UI;
  8. namespace DevilLink2.UI
  9. {
  10. public static class MainUIInstaller
  11. {
  12. public const string ValidationSessionKey = "DevilLink2.Task08A.PlayPending";
  13. // Tests/editor acceptance can inject an isolated player without touching real player saves.
  14. public static Func<SavedPlayer> PlayerLoaderOverride;
  15. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
  16. private static void InstallMain()
  17. {
  18. if (SceneManager.GetActiveScene().path != "Assets/DevilLink2/Scenes/Main.unity") return;
  19. var bootstrap = UnityEngine.Object.FindFirstObjectByType<BoardDemoBootstrap>();
  20. if (!bootstrap || bootstrap.GetComponentInChildren<UIManager>(true)) return;
  21. bootstrap.ConfigureAutomaticStart(false);
  22. TryStart(bootstrap, false);
  23. }
  24. private static void TryStart(BoardDemoBootstrap bootstrap, bool memoryOnly)
  25. {
  26. SavedPlayer player = null;
  27. try
  28. {
  29. #if UNITY_EDITOR
  30. if (UnityEditor.SessionState.GetBool(ValidationSessionKey, false) && PlayerLoaderOverride == null)
  31. throw new InvalidOperationException("Isolated Play Mode test player has not been installed; refusing real player storage.");
  32. #endif
  33. bool atomic = Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor;
  34. player = memoryOnly ? SavedPlayer.CreateMemoryOnly() : PlayerLoaderOverride != null ? PlayerLoaderOverride() : new SavedPlayer(new LocalSaveService(
  35. new FileSaveStorage(Path.Combine(Application.persistentDataPath, "DevilLink2"), atomic)));
  36. var frame = bootstrap.transform.Find("SafeArea/PortraitFrame") as RectTransform;
  37. var view = bootstrap.GetComponentInChildren<BoardView>(true);
  38. if (!frame || !view) throw new InvalidOperationException("Existing Main Canvas/board structure is incomplete.");
  39. var title = frame.Find("Title")?.GetComponent<Text>();
  40. var audio = DevilLink2.Presentation.AudioManager.Ensure(bootstrap.gameObject);
  41. audio.SetMusic(MusicTrack.Lobby);
  42. var manager = UIBuilder.Build(frame, bootstrap, view, title);
  43. manager.Initialize(player, bootstrap, true);
  44. manager.Settings.BindAudio(audio);
  45. var recovery = manager.gameObject.AddComponent<SaveRecoveryController>();
  46. recovery.Bind(player, frame);
  47. }
  48. catch (Exception error) when (error is IOException || error is UnauthorizedAccessException ||
  49. error is UnsupportedSaveVersionException || error is SaveFormatException || error is System.Security.SecurityException)
  50. {
  51. player?.Dispose();
  52. ShowRecovery(bootstrap, error);
  53. }
  54. catch (Exception error)
  55. { player?.Dispose(); Debug.LogError("DevilLink2 UI startup failed: " + error); }
  56. }
  57. private static void ShowRecovery(BoardDemoBootstrap bootstrap, Exception error)
  58. {
  59. var frame = bootstrap.transform.Find("SafeArea/PortraitFrame") as RectTransform;
  60. if (!frame) { Debug.LogError("Save recovery requires the existing PortraitFrame: " + error); return; }
  61. Debug.LogWarning("DevilLink2 save unavailable: " + error.Message);
  62. var overlay = UIBuilder.Rect("SaveRecovery", frame, 0, 0, 430, 932);
  63. var shade = overlay.gameObject.AddComponent<Image>(); shade.color = new Color(.04f,.015f,.07f,.96f);
  64. var card = UIBuilder.Fill("RecoveryCard", overlay, UIBuilder.Hex("291333"), UIBuilder.Hex("150D20"), 35, 280, 360, 360, 18);
  65. bool future = error is UnsupportedSaveVersionException;
  66. UIBuilder.Label("Heading",card.transform,future ? "SAVE VERSION INCOMPATIBLE" : "SAVE UNAVAILABLE",19,UIBuilder.Gold,16,18,328,50);
  67. UIBuilder.Label("Message",card.transform,future ?
  68. "This save needs a newer game version. Your original save and backup will not be overwritten. Retry, exit, or play temporarily without saving." :
  69. "Your save could not be accessed. Retry when storage is available, or play temporarily. Temporary progress will not be saved and your disk saves will not be changed.",14,UIBuilder.LightGold,20,76,320,120);
  70. AddRecoveryButton(card.transform,"RETRY",208,() => { overlay.gameObject.SetActive(false); UnityEngine.Object.Destroy(overlay.gameObject); TryStart(bootstrap,false); });
  71. AddRecoveryButton(card.transform,"PLAY WITHOUT SAVING",254,() => { overlay.gameObject.SetActive(false); UnityEngine.Object.Destroy(overlay.gameObject); TryStart(bootstrap,true); });
  72. AddRecoveryButton(card.transform,"EXIT",300,() => {
  73. #if UNITY_EDITOR
  74. UnityEditor.EditorApplication.isPlaying = false;
  75. #else
  76. Application.Quit();
  77. #endif
  78. });
  79. overlay.SetAsLastSibling();
  80. }
  81. private static void AddRecoveryButton(Transform parent, string caption, float y, UnityEngine.Events.UnityAction action)
  82. {
  83. var button = UIBuilder.Button(caption,parent,caption,36,y,288,40,UIBuilder.Hex("382044"),UIBuilder.Hex("21122F"),14);
  84. button.transition = Selectable.Transition.None;
  85. button.GetComponentInChildren<Text>().color = UIBuilder.LightGold;
  86. button.onClick.AddListener(action);
  87. }
  88. }
  89. }