From 13fe71693845cb11f8f8cca634edf59154758117 Mon Sep 17 00:00:00 2001 From: morfidon <57798071+morfidon@users.noreply.github.com> Date: Wed, 11 Mar 2026 16:18:15 +0100 Subject: [PATCH] Split stash save paths for manual and auto saves --- Source/loadsave.cpp | 21 +++++++++++++------- Source/pfile.cpp | 48 ++++++++++++++++++++++++++++++++++----------- Source/pfile.h | 3 ++- 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index 9fafb01a2..8cd7c972d 100644 --- a/Source/loadsave.cpp +++ b/Source/loadsave.cpp @@ -2956,15 +2956,22 @@ SaveResult SaveGame(SaveKind kind) gbValidSaveFile = true; return SaveResult::Success; #else - const bool gameSaved = kind == SaveKind::Manual - ? pfile_write_manual_game_with_backup() - : pfile_write_auto_game(); - if (!gameSaved) - return GetSaveFailureResult(); + if (kind == SaveKind::Manual) { + if (!pfile_write_manual_game_with_backup()) + return GetSaveFailureResult(); + } else { + if (!pfile_write_auto_game()) + return GetSaveFailureResult(); + } gbValidSaveFile = true; - if (!pfile_write_stash_with_backup()) - return GetSaveFailureResult(); + if (kind == SaveKind::Manual) { + if (!pfile_write_manual_stash_with_backup()) + return GetSaveFailureResult(); + } else { + if (!pfile_write_auto_stash()) + return GetSaveFailureResult(); + } return SaveResult::Success; #endif diff --git a/Source/pfile.cpp b/Source/pfile.cpp index a4865c252..a77dc0e07 100644 --- a/Source/pfile.cpp +++ b/Source/pfile.cpp @@ -713,34 +713,60 @@ bool pfile_write_auto_game() return saveIsValid; } -bool pfile_write_stash_with_backup() +bool WriteStashAndRestoreOnFailure(std::string_view restorePrefix, bool deleteRestoreLocationOnSuccess) { if (!Stash.dirty) return true; - const std::string backupPrefix = "backup_"; - const std::string backupLocation = GetStashSavePath(backupPrefix); + const std::string restoreLocation = GetStashSavePath(restorePrefix); const std::string stashLocation = GetStashSavePath(); - if (SaveLocationExists(stashLocation)) - CopySaveLocation(stashLocation, backupLocation); - SaveWriter stashWriter = GetStashWriter(); SaveStash(stashWriter); auto archive = OpenStashArchive(); const char *stashFileName = gbIsMultiplayer ? "mpstashitems" : "spstashitems"; const bool stashIsValid = archive && ReadArchive(*archive, stashFileName) != nullptr; - if (stashIsValid || !SaveLocationExists(backupLocation)) { - if (stashIsValid) - Stash.dirty = false; - return stashIsValid; + if (stashIsValid) { + Stash.dirty = false; + if (deleteRestoreLocationOnSuccess) + DeleteSaveLocation(restoreLocation); + return true; } - RestoreSaveLocation(stashLocation, backupLocation); + if (!SaveLocationExists(restoreLocation)) + return false; + RestoreSaveLocation(stashLocation, restoreLocation); return false; } + +bool pfile_write_manual_stash_with_backup() +{ + const std::string backupPrefix = "backup_"; + const std::string backupLocation = GetStashSavePath(backupPrefix); + const std::string stashLocation = GetStashSavePath(); + + if (SaveLocationExists(stashLocation)) + CopySaveLocation(stashLocation, backupLocation); + + return WriteStashAndRestoreOnFailure(backupPrefix, false); +} + +bool pfile_write_auto_stash() +{ + const std::string restorePrefix = "autosave_restore_"; + const std::string restoreLocation = GetStashSavePath(restorePrefix); + const std::string stashLocation = GetStashSavePath(); + + if (SaveLocationExists(stashLocation)) + CopySaveLocation(stashLocation, restoreLocation); + + const bool stashIsValid = WriteStashAndRestoreOnFailure(restorePrefix, true); + if (!stashIsValid && SaveLocationExists(restoreLocation)) + DeleteSaveLocation(restoreLocation); + return stashIsValid; +} #endif #ifndef DISABLE_DEMOMODE diff --git a/Source/pfile.h b/Source/pfile.h index 307d20d5c..d41c00d44 100644 --- a/Source/pfile.h +++ b/Source/pfile.h @@ -103,7 +103,8 @@ std::unique_ptr ReadArchive(SaveReader &archive, const char *pszNam void pfile_write_hero(bool writeGameData = false); bool pfile_write_manual_game_with_backup(); bool pfile_write_auto_game(); -bool pfile_write_stash_with_backup(); +bool pfile_write_manual_stash_with_backup(); +bool pfile_write_auto_stash(); #ifndef DISABLE_DEMOMODE /**