Browse Source

Clarify save location naming

Rename save backup and restore helpers to use location terminology, pass source and target locations explicitly, and reuse the copy helper for stash backups.
pull/8497/head
morfidon 2 weeks ago
parent
commit
2ac1fa143e
  1. 103
      Source/pfile.cpp

103
Source/pfile.cpp

@ -170,43 +170,41 @@ SaveWriter GetStashWriter()
return SaveWriter(GetStashSavePath()); return SaveWriter(GetStashSavePath());
} }
void CopySaveFile(uint32_t saveNum, std::string targetPath) void CopySaveLocation(const std::string &sourceLocation, const std::string &targetLocation)
{ {
const std::string savePath = GetSavePath(saveNum);
#if defined(UNPACKED_SAVES) #if defined(UNPACKED_SAVES)
#ifdef DVL_NO_FILESYSTEM #ifdef DVL_NO_FILESYSTEM
#error "UNPACKED_SAVES requires either DISABLE_DEMOMODE or C++17 <filesystem>" #error "UNPACKED_SAVES requires either DISABLE_DEMOMODE or C++17 <filesystem>"
#endif #endif
if (!targetPath.empty()) { if (!targetLocation.empty()) {
CreateDir(targetPath.c_str()); CreateDir(targetLocation.c_str());
} }
for (const std::filesystem::directory_entry &entry : std::filesystem::directory_iterator(savePath)) { for (const std::filesystem::directory_entry &entry : std::filesystem::directory_iterator(sourceLocation)) {
const std::filesystem::path targetFilePath = std::filesystem::path(targetPath) / entry.path().filename(); const std::filesystem::path targetFilePath = std::filesystem::path(targetLocation) / entry.path().filename();
CopyFileOverwrite(entry.path().string().c_str(), targetFilePath.string().c_str()); CopyFileOverwrite(entry.path().string().c_str(), targetFilePath.string().c_str());
} }
#else #else
CopyFileOverwrite(savePath.c_str(), targetPath.c_str()); CopyFileOverwrite(sourceLocation.c_str(), targetLocation.c_str());
#endif #endif
} }
void RestoreSaveFile(const std::string &targetPath, const std::string &backupPath) void RestoreSaveLocation(const std::string &targetLocation, const std::string &backupLocation)
{ {
#if defined(UNPACKED_SAVES) #if defined(UNPACKED_SAVES)
#ifdef DVL_NO_FILESYSTEM #ifdef DVL_NO_FILESYSTEM
#error "UNPACKED_SAVES requires either DISABLE_DEMOMODE or C++17 <filesystem>" #error "UNPACKED_SAVES requires either DISABLE_DEMOMODE or C++17 <filesystem>"
#endif #endif
if (DirectoryExists(targetPath.c_str())) { if (DirectoryExists(targetLocation.c_str())) {
for (const std::filesystem::directory_entry &entry : std::filesystem::directory_iterator(targetPath)) for (const std::filesystem::directory_entry &entry : std::filesystem::directory_iterator(targetLocation))
RemoveFile(entry.path().string().c_str()); RemoveFile(entry.path().string().c_str());
} }
CreateDir(targetPath.c_str()); CreateDir(targetLocation.c_str());
for (const std::filesystem::directory_entry &entry : std::filesystem::directory_iterator(backupPath)) { for (const std::filesystem::directory_entry &entry : std::filesystem::directory_iterator(backupLocation)) {
const std::filesystem::path restoredFilePath = std::filesystem::path(targetPath) / entry.path().filename(); const std::filesystem::path restoredFilePath = std::filesystem::path(targetLocation) / entry.path().filename();
CopyFileOverwrite(entry.path().string().c_str(), restoredFilePath.string().c_str()); CopyFileOverwrite(entry.path().string().c_str(), restoredFilePath.string().c_str());
} }
#else #else
CopyFileOverwrite(backupPath.c_str(), targetPath.c_str()); CopyFileOverwrite(backupLocation.c_str(), targetLocation.c_str());
#endif #endif
} }
@ -650,23 +648,23 @@ void pfile_write_hero(bool writeGameData)
} }
bool pfile_write_hero_with_backup(bool writeGameData) bool pfile_write_hero_with_backup(bool writeGameData)
{ {
const std::string backupPrefix = "backup_"; const std::string backupPrefix = "backup_";
const std::string backupPath = GetSavePath(gSaveNumber, backupPrefix); const std::string backupLocation = GetSavePath(gSaveNumber, backupPrefix);
const std::string savePath = GetSavePath(gSaveNumber); const std::string saveLocation = GetSavePath(gSaveNumber);
if (FileExists(savePath) || DirectoryExists(savePath.c_str())) if (FileExists(saveLocation) || DirectoryExists(saveLocation.c_str()))
CopySaveFile(gSaveNumber, backupPath); CopySaveLocation(saveLocation, backupLocation);
pfile_write_hero(writeGameData); pfile_write_hero(writeGameData);
auto archive = OpenSaveArchive(gSaveNumber); auto archive = OpenSaveArchive(gSaveNumber);
const bool saveIsValid = archive && ArchiveContainsGame(*archive); const bool saveIsValid = archive && ArchiveContainsGame(*archive);
if (saveIsValid || !(FileExists(backupPath) || DirectoryExists(backupPath.c_str()))) if (saveIsValid || !(FileExists(backupLocation) || DirectoryExists(backupLocation.c_str())))
return saveIsValid; return saveIsValid;
RestoreSaveFile(savePath, backupPath); RestoreSaveLocation(saveLocation, backupLocation);
return false; return false;
} }
@ -676,20 +674,11 @@ bool pfile_write_stash_with_backup()
return true; return true;
const std::string backupPrefix = "backup_"; const std::string backupPrefix = "backup_";
const std::string backupPath = GetStashSavePath(backupPrefix); const std::string backupLocation = GetStashSavePath(backupPrefix);
const std::string stashPath = GetStashSavePath(); const std::string stashLocation = GetStashSavePath();
if (FileExists(stashPath) || DirectoryExists(stashPath.c_str())) { if (FileExists(stashLocation) || DirectoryExists(stashLocation.c_str()))
#if defined(UNPACKED_SAVES) CopySaveLocation(stashLocation, backupLocation);
CreateDir(backupPath.c_str());
for (const std::filesystem::directory_entry &entry : std::filesystem::directory_iterator(stashPath)) {
const std::filesystem::path targetFilePath = std::filesystem::path(backupPath) / entry.path().filename();
CopyFileOverwrite(entry.path().string().c_str(), targetFilePath.string().c_str());
}
#else
CopyFileOverwrite(stashPath.c_str(), backupPath.c_str());
#endif
}
SaveWriter stashWriter = GetStashWriter(); SaveWriter stashWriter = GetStashWriter();
SaveStash(stashWriter); SaveStash(stashWriter);
@ -697,25 +686,25 @@ bool pfile_write_stash_with_backup()
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 || !(FileExists(backupPath) || DirectoryExists(backupPath.c_str()))) { if (stashIsValid || !(FileExists(backupLocation) || DirectoryExists(backupLocation.c_str()))) {
if (stashIsValid) if (stashIsValid)
Stash.dirty = false; Stash.dirty = false;
return stashIsValid; return stashIsValid;
} }
RestoreSaveFile(stashPath, backupPath); RestoreSaveLocation(stashLocation, backupLocation);
return false; return false;
} }
#ifndef DISABLE_DEMOMODE #ifndef DISABLE_DEMOMODE
void pfile_write_hero_demo(int demo) void pfile_write_hero_demo(int demo)
{ {
const std::string savePath = GetSavePath(gSaveNumber, StrCat("demo_", demo, "_reference_")); const std::string saveLocation = GetSavePath(gSaveNumber, StrCat("demo_", demo, "_reference_"));
CopySaveFile(gSaveNumber, savePath); CopySaveLocation(GetSavePath(gSaveNumber), saveLocation);
auto saveWriter = SaveWriter(savePath.c_str()); auto saveWriter = SaveWriter(saveLocation.c_str());
pfile_write_hero(saveWriter, true); pfile_write_hero(saveWriter, true);
} }
HeroCompareResult pfile_compare_hero_demo(int demo, bool logDetails) HeroCompareResult pfile_compare_hero_demo(int demo, bool logDetails)
{ {
@ -724,12 +713,12 @@ HeroCompareResult pfile_compare_hero_demo(int demo, bool logDetails)
if (!FileExists(referenceSavePath.c_str())) if (!FileExists(referenceSavePath.c_str()))
return { HeroCompareResult::ReferenceNotFound, {} }; return { HeroCompareResult::ReferenceNotFound, {} };
const std::string actualSavePath = GetSavePath(gSaveNumber, StrCat("demo_", demo, "_actual_")); const std::string actualSavePath = GetSavePath(gSaveNumber, StrCat("demo_", demo, "_actual_"));
{ {
CopySaveFile(gSaveNumber, actualSavePath); CopySaveLocation(GetSavePath(gSaveNumber), actualSavePath);
SaveWriter saveWriter(actualSavePath.c_str()); SaveWriter saveWriter(actualSavePath.c_str());
pfile_write_hero(saveWriter, true); pfile_write_hero(saveWriter, true);
} }
return CompareSaves(actualSavePath, referenceSavePath, logDetails); return CompareSaves(actualSavePath, referenceSavePath, logDetails);
} }

Loading…
Cancel
Save