| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using System;
- using System.IO;
- using DevilLink2.Persistence;
- using DevilLink2.Presentation;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- namespace DevilLink2.UI
- {
- public static class MainUIInstaller
- {
- public const string ValidationSessionKey = "DevilLink2.Task08A.PlayPending";
- // Tests/editor acceptance can inject an isolated player without touching real player saves.
- public static Func<SavedPlayer> PlayerLoaderOverride;
- [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
- private static void InstallMain()
- {
- if (SceneManager.GetActiveScene().path != "Assets/DevilLink2/Scenes/Main.unity") return;
- var bootstrap = UnityEngine.Object.FindFirstObjectByType<BoardDemoBootstrap>();
- if (!bootstrap || bootstrap.GetComponentInChildren<UIManager>(true)) return;
- bootstrap.ConfigureAutomaticStart(false);
- TryStart(bootstrap, false);
- }
- private static void TryStart(BoardDemoBootstrap bootstrap, bool memoryOnly)
- {
- SavedPlayer player = null;
- try
- {
- #if UNITY_EDITOR
- if (UnityEditor.SessionState.GetBool(ValidationSessionKey, false) && PlayerLoaderOverride == null)
- throw new InvalidOperationException("Isolated Play Mode test player has not been installed; refusing real player storage.");
- #endif
- bool atomic = Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor;
- player = memoryOnly ? SavedPlayer.CreateMemoryOnly() : PlayerLoaderOverride != null ? PlayerLoaderOverride() : new SavedPlayer(new LocalSaveService(
- new FileSaveStorage(Path.Combine(Application.persistentDataPath, "DevilLink2"), atomic)));
- var frame = bootstrap.transform.Find("SafeArea/PortraitFrame") as RectTransform;
- var view = bootstrap.GetComponentInChildren<BoardView>(true);
- if (!frame || !view) throw new InvalidOperationException("Existing Main Canvas/board structure is incomplete.");
- var title = frame.Find("Title")?.GetComponent<Text>();
- var audio = DevilLink2.Presentation.AudioManager.Ensure(bootstrap.gameObject);
- audio.SetMusic(MusicTrack.Lobby);
- var manager = UIBuilder.Build(frame, bootstrap, view, title);
- manager.Initialize(player, bootstrap, true);
- manager.Settings.BindAudio(audio);
- var recovery = manager.gameObject.AddComponent<SaveRecoveryController>();
- recovery.Bind(player, frame);
- }
- catch (Exception error) when (error is IOException || error is UnauthorizedAccessException ||
- error is UnsupportedSaveVersionException || error is SaveFormatException || error is System.Security.SecurityException)
- {
- player?.Dispose();
- ShowRecovery(bootstrap, error);
- }
- catch (Exception error)
- { player?.Dispose(); Debug.LogError("DevilLink2 UI startup failed: " + error); }
- }
- private static void ShowRecovery(BoardDemoBootstrap bootstrap, Exception error)
- {
- var frame = bootstrap.transform.Find("SafeArea/PortraitFrame") as RectTransform;
- if (!frame) { Debug.LogError("Save recovery requires the existing PortraitFrame: " + error); return; }
- Debug.LogWarning("DevilLink2 save unavailable: " + error.Message);
- var overlay = UIBuilder.Rect("SaveRecovery", frame, 0, 0, 430, 932);
- var shade = overlay.gameObject.AddComponent<Image>(); shade.color = new Color(.04f,.015f,.07f,.96f);
- var card = UIBuilder.Fill("RecoveryCard", overlay, UIBuilder.Hex("291333"), UIBuilder.Hex("150D20"), 35, 280, 360, 360, 18);
- bool future = error is UnsupportedSaveVersionException;
- UIBuilder.Label("Heading",card.transform,future ? "SAVE VERSION INCOMPATIBLE" : "SAVE UNAVAILABLE",19,UIBuilder.Gold,16,18,328,50);
- UIBuilder.Label("Message",card.transform,future ?
- "This save needs a newer game version. Your original save and backup will not be overwritten. Retry, exit, or play temporarily without saving." :
- "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);
- AddRecoveryButton(card.transform,"RETRY",208,() => { overlay.gameObject.SetActive(false); UnityEngine.Object.Destroy(overlay.gameObject); TryStart(bootstrap,false); });
- AddRecoveryButton(card.transform,"PLAY WITHOUT SAVING",254,() => { overlay.gameObject.SetActive(false); UnityEngine.Object.Destroy(overlay.gameObject); TryStart(bootstrap,true); });
- AddRecoveryButton(card.transform,"EXIT",300,() => {
- #if UNITY_EDITOR
- UnityEditor.EditorApplication.isPlaying = false;
- #else
- Application.Quit();
- #endif
- });
- overlay.SetAsLastSibling();
- }
- private static void AddRecoveryButton(Transform parent, string caption, float y, UnityEngine.Events.UnityAction action)
- {
- var button = UIBuilder.Button(caption,parent,caption,36,y,288,40,UIBuilder.Hex("382044"),UIBuilder.Hex("21122F"),14);
- button.transition = Selectable.Transition.None;
- button.GetComponentInChildren<Text>().color = UIBuilder.LightGold;
- button.onClick.AddListener(action);
- }
- }
- }
|