Restructured for new direction.
This commit is contained in:
@@ -14,7 +14,7 @@ namespace BriarQueen.Framework.Services.Puzzles
|
||||
{
|
||||
private readonly SaveManager _saveManager;
|
||||
|
||||
private BasePuzzle _currentBasePuzzle;
|
||||
private readonly Dictionary<string, BasePuzzle> _activePuzzles = new();
|
||||
private bool _isWritingState;
|
||||
|
||||
[Inject]
|
||||
@@ -31,40 +31,98 @@ namespace BriarQueen.Framework.Services.Puzzles
|
||||
|
||||
public async UniTask LoadPuzzle(BasePuzzle basePuzzle)
|
||||
{
|
||||
_currentBasePuzzle = basePuzzle;
|
||||
if (_currentBasePuzzle == null)
|
||||
if (basePuzzle == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await TryRestorePuzzleState(_currentBasePuzzle);
|
||||
if (string.IsNullOrWhiteSpace(basePuzzle.PuzzleID))
|
||||
{
|
||||
Debug.LogWarning($"[PuzzleService] Cannot load puzzle '{basePuzzle.name}' with null/empty PuzzleID.");
|
||||
return;
|
||||
}
|
||||
|
||||
_activePuzzles[basePuzzle.PuzzleID] = basePuzzle;
|
||||
|
||||
await basePuzzle.PostLoad();
|
||||
await TryRestorePuzzleState(basePuzzle);
|
||||
}
|
||||
|
||||
public async UniTask LoadPuzzles(IEnumerable<BasePuzzle> basePuzzles)
|
||||
{
|
||||
_activePuzzles.Clear();
|
||||
|
||||
if (basePuzzles == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var basePuzzle in basePuzzles)
|
||||
{
|
||||
await LoadPuzzle(basePuzzle);
|
||||
}
|
||||
}
|
||||
|
||||
public async UniTask SavePuzzle(BasePuzzle basePuzzle)
|
||||
{
|
||||
if (basePuzzle == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_currentBasePuzzle != null && basePuzzle != _currentBasePuzzle)
|
||||
if (!string.IsNullOrWhiteSpace(basePuzzle.PuzzleID) &&
|
||||
_activePuzzles.TryGetValue(basePuzzle.PuzzleID, out var activePuzzle) &&
|
||||
activePuzzle != basePuzzle)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await SavePuzzleState(basePuzzle, flushToDisk: true);
|
||||
_currentBasePuzzle = null;
|
||||
await basePuzzle.PreUnload();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(basePuzzle.PuzzleID))
|
||||
{
|
||||
_activePuzzles.Remove(basePuzzle.PuzzleID);
|
||||
}
|
||||
}
|
||||
|
||||
public async UniTask SavePuzzles(IEnumerable<BasePuzzle> basePuzzles)
|
||||
{
|
||||
if (basePuzzles != null)
|
||||
{
|
||||
foreach (var basePuzzle in basePuzzles)
|
||||
{
|
||||
await SavePuzzle(basePuzzle);
|
||||
}
|
||||
}
|
||||
|
||||
_activePuzzles.Clear();
|
||||
}
|
||||
|
||||
private async UniTask OnBeforeSaveRequestedAsync()
|
||||
{
|
||||
if (_currentBasePuzzle == null)
|
||||
if (_activePuzzles.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await SavePuzzleState(_currentBasePuzzle, flushToDisk: false);
|
||||
foreach (var basePuzzle in _activePuzzles.Values.ToList())
|
||||
{
|
||||
await SavePuzzleState(basePuzzle, flushToDisk: false);
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTask TryRestorePuzzleState(BasePuzzle basePuzzle)
|
||||
{
|
||||
if (basePuzzle == null || _saveManager.CurrentSave == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (basePuzzle is not IPuzzleStateful stateful)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var save = _saveManager.CurrentSave;
|
||||
var entry = save.PuzzleStates?.FirstOrDefault(x => x != null && x.PuzzleID == basePuzzle.PuzzleID);
|
||||
@@ -83,20 +141,28 @@ namespace BriarQueen.Framework.Services.Puzzles
|
||||
private async UniTask SavePuzzleState(BasePuzzle basePuzzle, bool flushToDisk)
|
||||
{
|
||||
if (basePuzzle == null || _saveManager.CurrentSave == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (basePuzzle is not IPuzzleStateful stateful)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_isWritingState)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isWritingState = true;
|
||||
|
||||
try
|
||||
{
|
||||
if (basePuzzle is IPuzzleWorldStateSync worldStateSync)
|
||||
{
|
||||
worldStateSync.SyncWorldStateToSave();
|
||||
}
|
||||
|
||||
var save = _saveManager.CurrentSave;
|
||||
save.PuzzleStates ??= new List<PuzzleStateSaveData>();
|
||||
@@ -123,7 +189,9 @@ namespace BriarQueen.Framework.Services.Puzzles
|
||||
existing.Completed = stateful.IsCompleted;
|
||||
|
||||
if (flushToDisk)
|
||||
{
|
||||
await _saveManager.SaveGameDataLatest();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -131,4 +199,4 @@ namespace BriarQueen.Framework.Services.Puzzles
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user