SaveManager cleanup (#3386)

* Move threadpool initialization and `OnExitGame` registration from `SaveManager::Init` to SM's constructor.
Comment on `Init` to mention it's not an initializer for `SaveManager`.
Added check for `SaveManager::SaveSection` to prevent firing a save worker if the game is already exited from a reset.

* Removed `IsSaveLoaded` check in favor of another `ThreadPoolWait()` at the start of `SaveManager::Init()`.
This commit is contained in:
Malkierian 2023-11-14 14:46:38 -07:00 committed by GitHub
parent bd0672767a
commit ba987c49e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -65,6 +65,10 @@ SaveManager::SaveManager() {
AddInitFunction(InitFileImpl); AddInitFunction(InitFileImpl);
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnExitGame>([this](uint32_t fileNum) { ThreadPoolWait(); });
smThreadPool = std::make_shared<BS::thread_pool>(1);
for (SaveFileMetaInfo& info : fileMetaInfo) { for (SaveFileMetaInfo& info : fileMetaInfo) {
info.valid = false; info.valid = false;
info.deaths = 0; info.deaths = 0;
@ -357,12 +361,14 @@ void SaveManager::SaveRandomizer(SaveContext* saveContext, int sectionID, bool f
}); });
} }
// Init() here is an extension of InitSram, and thus not truly an initializer for SaveManager itself. don't put any class initialization stuff here
void SaveManager::Init() { void SaveManager::Init() {
// Wait on saves that snuck through the Wait in OnExitGame
ThreadPoolWait();
const std::filesystem::path sSavePath(LUS::Context::GetPathRelativeToAppDirectory("Save")); const std::filesystem::path sSavePath(LUS::Context::GetPathRelativeToAppDirectory("Save"));
const std::filesystem::path sGlobalPath = sSavePath / std::string("global.sav"); const std::filesystem::path sGlobalPath = sSavePath / std::string("global.sav");
auto sOldSavePath = LUS::Context::GetPathRelativeToAppDirectory("oot_save.sav"); auto sOldSavePath = LUS::Context::GetPathRelativeToAppDirectory("oot_save.sav");
auto sOldBackupSavePath = LUS::Context::GetPathRelativeToAppDirectory("oot_save.bak"); auto sOldBackupSavePath = LUS::Context::GetPathRelativeToAppDirectory("oot_save.bak");
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnExitGame>([this](uint32_t fileNum) { ThreadPoolWait(); });
// If the save directory does not exist, create it // If the save directory does not exist, create it
if (!std::filesystem::exists(sSavePath)) { if (!std::filesystem::exists(sSavePath)) {
@ -403,7 +409,6 @@ void SaveManager::Init() {
} else { } else {
CreateDefaultGlobal(); CreateDefaultGlobal();
} }
smThreadPool = std::make_shared<BS::thread_pool>(1);
// Load files to initialize metadata // Load files to initialize metadata
for (int fileNum = 0; fileNum < MaxFiles; fileNum++) { for (int fileNum = 0; fileNum < MaxFiles; fileNum++) {