BoardDemoBootstrap.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.IO;
  3. using DevilLink2.Gameplay;
  4. using DevilLink2.Persistence;
  5. using UnityEngine;
  6. namespace DevilLink2.Presentation
  7. {
  8. [DisallowMultipleComponent]
  9. public sealed class BoardDemoBootstrap : MonoBehaviour
  10. {
  11. [SerializeField] private BoardView boardView;
  12. [SerializeField] private int randomSeed = 2026;
  13. private ModeProgress progress;
  14. private PlayerEconomy memoryEconomy;
  15. private SavedPlayer savedPlayer;
  16. private bool ownsSavedPlayer;
  17. private bool automaticStart = true;
  18. public PlayerEconomy Economy => savedPlayer == null ? memoryEconomy : savedPlayer.Economy;
  19. public SavedPlayer SavedPlayer => savedPlayer;
  20. public GameFlowController Flow { get; private set; }
  21. public GameSession Session => Flow?.Runtime.Session;
  22. public void Configure(BoardView view) { boardView = view; }
  23. public void ConfigureAutomaticStart(bool enabled) { automaticStart = enabled; }
  24. private void Start()
  25. {
  26. if (!automaticStart) return;
  27. try { InitializePersistent(GameMode.Classic, Path.Combine(Application.persistentDataPath, "DevilLink2")); }
  28. catch (Exception error) when (error is IOException || error is UnauthorizedAccessException || error is UnsupportedSaveVersionException || error is SaveFormatException)
  29. { Debug.LogError("DevilLink2 save load failed; no session created: " + error.Message, this); }
  30. }
  31. private void OnDestroy()
  32. {
  33. Flow?.Dispose();
  34. if (savedPlayer != null) savedPlayer.SaveFailed -= OnSaveFailed;
  35. if (ownsSavedPlayer) savedPlayer?.Dispose();
  36. }
  37. // Explicit in-memory initialization remains available for demos/tests, without touching player files.
  38. public void Initialize()
  39. {
  40. if (savedPlayer != null) { InitializePersistent(GameMode.Classic, savedPlayer); return; }
  41. Initialize(GameMode.Classic, progress ?? new ModeProgress());
  42. }
  43. public void Initialize(GameMode mode, ModeProgress progress)
  44. {
  45. if (savedPlayer != null)
  46. {
  47. if (!ReferenceEquals(savedPlayer.Progress, progress)) throw new InvalidOperationException("Use the loaded player's progress.");
  48. InitializePersistent(mode, savedPlayer); return;
  49. }
  50. if (!CheckView()) return;
  51. if (progress == null) throw new ArgumentNullException(nameof(progress));
  52. Flow?.Dispose(); this.progress = progress;
  53. if (memoryEconomy == null) memoryEconomy = new PlayerEconomy();
  54. StartFlow(new GameFlowController(mode, progress, memoryEconomy, new StopwatchClock(), randomSeed));
  55. }
  56. public void InitializePersistent(GameMode mode, string saveDirectory)
  57. {
  58. if (!CheckView()) return;
  59. // Complete disk loading and model restoration before creating any GameSession.
  60. bool atomicReplace = Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor;
  61. var player = new SavedPlayer(new LocalSaveService(new FileSaveStorage(saveDirectory, atomicReplace)));
  62. try { InitializePersistent(mode, player); ownsSavedPlayer = true; }
  63. catch { player.Dispose(); throw; }
  64. }
  65. public void InitializePersistent(GameMode mode, SavedPlayer player)
  66. {
  67. if (!CheckView()) return;
  68. if (player == null) throw new ArgumentNullException(nameof(player));
  69. Flow?.Dispose();
  70. if (!ReferenceEquals(savedPlayer, player))
  71. {
  72. if (savedPlayer != null) savedPlayer.SaveFailed -= OnSaveFailed;
  73. if (ownsSavedPlayer) savedPlayer?.Dispose();
  74. savedPlayer = player; ownsSavedPlayer = false; savedPlayer.SaveFailed += OnSaveFailed;
  75. }
  76. progress = player.Progress; memoryEconomy = null;
  77. StartFlow(player.CreateFlow(mode, new StopwatchClock(), randomSeed));
  78. }
  79. public void ResetSaveData()
  80. {
  81. if (savedPlayer == null) throw new InvalidOperationException("No persistent player loaded.");
  82. savedPlayer.ResetSaveData(); progress = savedPlayer.Progress;
  83. }
  84. private bool CheckView()
  85. {
  86. if (boardView) return true;
  87. Debug.LogError("DevilLink2 demo requires a BoardView.", this); return false;
  88. }
  89. private void StartFlow(GameFlowController flow)
  90. {
  91. Canvas.ForceUpdateCanvases(); Flow = flow;
  92. var interaction = GetComponent<BoardInteractionController>();
  93. if (!interaction) interaction = gameObject.AddComponent<BoardInteractionController>();
  94. interaction.Initialize(boardView, Flow); Flow.Start();
  95. }
  96. private void OnSaveFailed(Exception error)
  97. { Debug.LogError("DevilLink2 save failed; in-memory rewards retained for retry: " + error.Message, this); }
  98. }
  99. }