Browse Source

Split stash save paths for manual and auto saves

pull/8497/head
morfidon 6 days ago
parent
commit
13fe716938
  1. 21
      Source/loadsave.cpp
  2. 48
      Source/pfile.cpp
  3. 3
      Source/pfile.h

21
Source/loadsave.cpp

@ -2956,15 +2956,22 @@ SaveResult SaveGame(SaveKind kind)
gbValidSaveFile = true; gbValidSaveFile = true;
return SaveResult::Success; return SaveResult::Success;
#else #else
const bool gameSaved = kind == SaveKind::Manual if (kind == SaveKind::Manual) {
? pfile_write_manual_game_with_backup() if (!pfile_write_manual_game_with_backup())
: pfile_write_auto_game(); return GetSaveFailureResult();
if (!gameSaved) } else {
return GetSaveFailureResult(); if (!pfile_write_auto_game())
return GetSaveFailureResult();
}
gbValidSaveFile = true; gbValidSaveFile = true;
if (!pfile_write_stash_with_backup()) if (kind == SaveKind::Manual) {
return GetSaveFailureResult(); if (!pfile_write_manual_stash_with_backup())
return GetSaveFailureResult();
} else {
if (!pfile_write_auto_stash())
return GetSaveFailureResult();
}
return SaveResult::Success; return SaveResult::Success;
#endif #endif

48
Source/pfile.cpp

@ -713,34 +713,60 @@ bool pfile_write_auto_game()
return saveIsValid; return saveIsValid;
} }
bool pfile_write_stash_with_backup() bool WriteStashAndRestoreOnFailure(std::string_view restorePrefix, bool deleteRestoreLocationOnSuccess)
{ {
if (!Stash.dirty) if (!Stash.dirty)
return true; return true;
const std::string backupPrefix = "backup_"; const std::string restoreLocation = GetStashSavePath(restorePrefix);
const std::string backupLocation = GetStashSavePath(backupPrefix);
const std::string stashLocation = GetStashSavePath(); const std::string stashLocation = GetStashSavePath();
if (SaveLocationExists(stashLocation))
CopySaveLocation(stashLocation, backupLocation);
SaveWriter stashWriter = GetStashWriter(); SaveWriter stashWriter = GetStashWriter();
SaveStash(stashWriter); SaveStash(stashWriter);
auto archive = OpenStashArchive(); auto archive = OpenStashArchive();
const char *stashFileName = gbIsMultiplayer ? "mpstashitems" : "spstashitems"; const char *stashFileName = gbIsMultiplayer ? "mpstashitems" : "spstashitems";
const bool stashIsValid = archive && ReadArchive(*archive, stashFileName) != nullptr; const bool stashIsValid = archive && ReadArchive(*archive, stashFileName) != nullptr;
if (stashIsValid || !SaveLocationExists(backupLocation)) { if (stashIsValid) {
if (stashIsValid) Stash.dirty = false;
Stash.dirty = false; if (deleteRestoreLocationOnSuccess)
return stashIsValid; DeleteSaveLocation(restoreLocation);
return true;
} }
RestoreSaveLocation(stashLocation, backupLocation); if (!SaveLocationExists(restoreLocation))
return false;
RestoreSaveLocation(stashLocation, restoreLocation);
return false; 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 #endif
#ifndef DISABLE_DEMOMODE #ifndef DISABLE_DEMOMODE

3
Source/pfile.h

@ -103,7 +103,8 @@ std::unique_ptr<std::byte[]> ReadArchive(SaveReader &archive, const char *pszNam
void pfile_write_hero(bool writeGameData = false); void pfile_write_hero(bool writeGameData = false);
bool pfile_write_manual_game_with_backup(); bool pfile_write_manual_game_with_backup();
bool pfile_write_auto_game(); 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 #ifndef DISABLE_DEMOMODE
/** /**

Loading…
Cancel
Save