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;
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

48
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

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);
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
/**

Loading…
Cancel
Save