Skip to content

Commit

Permalink
Add temp file flow to SaveManager::SaveFileThreaded.
Browse files Browse the repository at this point in the history
Add "Save finish" info log message.
  • Loading branch information
Malkierian committed Nov 14, 2023
1 parent fb45b66 commit e002cae
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
32 changes: 30 additions & 2 deletions soh/soh/SaveManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ std::filesystem::path SaveManager::GetFileName(int fileNum) {
return sSavePath / ("file" + std::to_string(fileNum + 1) + ".sav");
}

std::filesystem::path SaveManager::GetFileTempName(int fileNum) {
const std::filesystem::path sSavePath(LUS::Context::GetPathRelativeToAppDirectory("Save"));
return sSavePath / ("file" + std::to_string(fileNum + 1) + ".temp");
}

SaveManager::SaveManager() {
coreSectionIDsByName["base"] = SECTION_ID_BASE;
coreSectionIDsByName["randomizer"] = SECTION_ID_RANDOMIZER;
Expand Down Expand Up @@ -915,19 +920,42 @@ void SaveManager::SaveFileThreaded(int fileNum, SaveContext* saveContext, int se
svi.func(saveContext, sectionID, false);
}

std::filesystem::path fileName = GetFileName(fileNum);
std::filesystem::path tempFile = GetFileTempName(fileNum);

if (std::filesystem::exists(tempFile)) {
std::filesystem::remove(tempFile);
}

#if defined(__SWITCH__) || defined(__WIIU__)
FILE* w = fopen(GetFileName(fileNum).c_str(), "w");
FILE* w = fopen(tempFile.c_str(), "w");
std::string json_string = saveBlock.dump(4);
fwrite(json_string.c_str(), sizeof(char), json_string.length(), w);
fclose(w);
#else
std::ofstream output(GetFileName(fileNum));
std::ofstream output(tempFile);
output << std::setw(4) << saveBlock << std::endl;
output.close();
#endif

if (std::filesystem::exists(fileName)) {
std::filesystem::remove(fileName);
}

#if defined(__SWITCH__) || defined(__WIIU__)
copy_file(tempFile.c_str(), fileName.c_str());
#else
std::filesystem::copy_file(tempFile, fileName);
#endif

if (std::filesystem::exists(tempFile)) {
std::filesystem::remove(tempFile);
}

delete saveContext;
InitMeta(fileNum);
GameInteractor::Instance->ExecuteHooks<GameInteractor::OnSaveFile>(fileNum);
SPDLOG_INFO("Save File Finish - fileNum: {}", fileNum);
}

// SaveSection creates a copy of gSaveContext to prevent mid-save data modification, and passes its reference to SaveFileThreaded
Expand Down
1 change: 1 addition & 0 deletions soh/soh/SaveManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class SaveManager {

private:
std::filesystem::path GetFileName(int fileNum);
std::filesystem::path GetFileTempName(int fileNum);
nlohmann::json saveBlock;

void ConvertFromUnversioned();
Expand Down

0 comments on commit e002cae

Please sign in to comment.