Restructured for new direction.
This commit is contained in:
@@ -26,7 +26,6 @@ namespace BriarQueen.Framework.Managers.IO
|
||||
private readonly object _saveLock = new();
|
||||
|
||||
private CancellationTokenSource _currentSaveCts;
|
||||
private DateTime _lastSaveTime;
|
||||
|
||||
[Inject]
|
||||
public SaveManager(EventCoordinator eventCoordinator)
|
||||
@@ -112,12 +111,6 @@ namespace BriarQueen.Framework.Managers.IO
|
||||
|
||||
private async UniTask SaveGameDataInternal(CancellationToken ct)
|
||||
{
|
||||
if ((DateTime.UtcNow - _lastSaveTime).TotalMilliseconds < 250)
|
||||
{
|
||||
Debug.Log("[SaveManager] Last save within 250ms, skipping.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (CurrentSave == null)
|
||||
CurrentSave = new SaveGame { SaveFileName = "NewGame" };
|
||||
|
||||
@@ -186,7 +179,6 @@ namespace BriarQueen.Framework.Managers.IO
|
||||
|
||||
CurrentSave = saveClone;
|
||||
IsGameLoaded = true;
|
||||
_lastSaveTime = DateTime.UtcNow;
|
||||
|
||||
OnSaveGameSaved?.Invoke();
|
||||
Debug.Log($"[SaveManager] Save complete: {CurrentSave.SaveFileName}");
|
||||
@@ -272,8 +264,7 @@ namespace BriarQueen.Framework.Managers.IO
|
||||
|
||||
if (loadedSave != null)
|
||||
{
|
||||
CurrentSave = loadedSave;
|
||||
await SaveGameDataLatest();
|
||||
RestoreBackupToMain(mainPath, backupPath);
|
||||
Debug.Log("[SaveManager] Restored save from backup.");
|
||||
}
|
||||
}
|
||||
@@ -285,6 +276,41 @@ namespace BriarQueen.Framework.Managers.IO
|
||||
OnSaveGameLoaded?.Invoke(CurrentSave);
|
||||
}
|
||||
|
||||
private void RestoreBackupToMain(string mainPath, string backupPath)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mainPath) || string.IsNullOrWhiteSpace(backupPath))
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
var mainDirectory = Path.GetDirectoryName(mainPath);
|
||||
if (!string.IsNullOrWhiteSpace(mainDirectory))
|
||||
Directory.CreateDirectory(mainDirectory);
|
||||
|
||||
var tempRestorePath = mainPath + ".restoretmp";
|
||||
|
||||
if (File.Exists(tempRestorePath))
|
||||
File.Delete(tempRestorePath);
|
||||
|
||||
File.Copy(backupPath, tempRestorePath, overwrite: true);
|
||||
|
||||
if (File.Exists(mainPath))
|
||||
File.Replace(tempRestorePath, mainPath, null, ignoreMetadataErrors: true);
|
||||
else
|
||||
File.Move(tempRestorePath, mainPath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"[SaveManager] Failed to restore backup '{backupPath}' to '{mainPath}': {ex}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
var tempRestorePath = mainPath + ".restoretmp";
|
||||
if (File.Exists(tempRestorePath))
|
||||
File.Delete(tempRestorePath);
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTask<SaveGame> LoadFromFileAsync(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path) || !File.Exists(path)) return null;
|
||||
@@ -432,4 +458,4 @@ namespace BriarQueen.Framework.Managers.IO
|
||||
return collected.Any(x => x.UniqueIdentifier == uniqueIdentifier);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user