| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- using System;
- using System.IO;
- using DevilLink2.Gameplay;
- using DevilLink2.Persistence;
- using UnityEngine;
- namespace DevilLink2.Presentation
- {
- [DisallowMultipleComponent]
- public sealed class BoardDemoBootstrap : MonoBehaviour
- {
- [SerializeField] private BoardView boardView;
- [SerializeField] private int randomSeed = 2026;
- private ModeProgress progress;
- private PlayerEconomy memoryEconomy;
- private SavedPlayer savedPlayer;
- private bool ownsSavedPlayer;
- private bool automaticStart = true;
- public PlayerEconomy Economy => savedPlayer == null ? memoryEconomy : savedPlayer.Economy;
- public SavedPlayer SavedPlayer => savedPlayer;
- public GameFlowController Flow { get; private set; }
- public GameSession Session => Flow?.Runtime.Session;
- public void Configure(BoardView view) { boardView = view; }
- public void ConfigureAutomaticStart(bool enabled) { automaticStart = enabled; }
- private void Start()
- {
- if (!automaticStart) return;
- try { InitializePersistent(GameMode.Classic, Path.Combine(Application.persistentDataPath, "DevilLink2")); }
- catch (Exception error) when (error is IOException || error is UnauthorizedAccessException || error is UnsupportedSaveVersionException || error is SaveFormatException)
- { Debug.LogError("DevilLink2 save load failed; no session created: " + error.Message, this); }
- }
- private void OnDestroy()
- {
- Flow?.Dispose();
- if (savedPlayer != null) savedPlayer.SaveFailed -= OnSaveFailed;
- if (ownsSavedPlayer) savedPlayer?.Dispose();
- }
- // Explicit in-memory initialization remains available for demos/tests, without touching player files.
- public void Initialize()
- {
- if (savedPlayer != null) { InitializePersistent(GameMode.Classic, savedPlayer); return; }
- Initialize(GameMode.Classic, progress ?? new ModeProgress());
- }
- public void Initialize(GameMode mode, ModeProgress progress)
- {
- if (savedPlayer != null)
- {
- if (!ReferenceEquals(savedPlayer.Progress, progress)) throw new InvalidOperationException("Use the loaded player's progress.");
- InitializePersistent(mode, savedPlayer); return;
- }
- if (!CheckView()) return;
- if (progress == null) throw new ArgumentNullException(nameof(progress));
- Flow?.Dispose(); this.progress = progress;
- if (memoryEconomy == null) memoryEconomy = new PlayerEconomy();
- StartFlow(new GameFlowController(mode, progress, memoryEconomy, new StopwatchClock(), randomSeed));
- }
- public void InitializePersistent(GameMode mode, string saveDirectory)
- {
- if (!CheckView()) return;
- // Complete disk loading and model restoration before creating any GameSession.
- bool atomicReplace = Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor;
- var player = new SavedPlayer(new LocalSaveService(new FileSaveStorage(saveDirectory, atomicReplace)));
- try { InitializePersistent(mode, player); ownsSavedPlayer = true; }
- catch { player.Dispose(); throw; }
- }
- public void InitializePersistent(GameMode mode, SavedPlayer player)
- {
- if (!CheckView()) return;
- if (player == null) throw new ArgumentNullException(nameof(player));
- Flow?.Dispose();
- if (!ReferenceEquals(savedPlayer, player))
- {
- if (savedPlayer != null) savedPlayer.SaveFailed -= OnSaveFailed;
- if (ownsSavedPlayer) savedPlayer?.Dispose();
- savedPlayer = player; ownsSavedPlayer = false; savedPlayer.SaveFailed += OnSaveFailed;
- }
- progress = player.Progress; memoryEconomy = null;
- StartFlow(player.CreateFlow(mode, new StopwatchClock(), randomSeed));
- }
- public void ResetSaveData()
- {
- if (savedPlayer == null) throw new InvalidOperationException("No persistent player loaded.");
- savedPlayer.ResetSaveData(); progress = savedPlayer.Progress;
- }
- private bool CheckView()
- {
- if (boardView) return true;
- Debug.LogError("DevilLink2 demo requires a BoardView.", this); return false;
- }
- private void StartFlow(GameFlowController flow)
- {
- Canvas.ForceUpdateCanvases(); Flow = flow;
- var interaction = GetComponent<BoardInteractionController>();
- if (!interaction) interaction = gameObject.AddComponent<BoardInteractionController>();
- interaction.Initialize(boardView, Flow); Flow.Start();
- }
- private void OnSaveFailed(Exception error)
- { Debug.LogError("DevilLink2 save failed; in-memory rewards retained for retry: " + error.Message, this); }
- }
- }
|