From 07bfba0162bb260f4f1f6cd7fb6f25ea82f3a2ad Mon Sep 17 00:00:00 2001 From: Malkierian Date: Wed, 10 May 2023 14:10:13 -0700 Subject: [PATCH] Preliminary work on persistent save data json block. Added autosaveRegistry to set section to save with overall autosave triggers. --- soh/soh/SaveManager.cpp | 40 ++++++++++++++++++++++++++++++++-------- soh/soh/SaveManager.h | 8 +++++++- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/soh/soh/SaveManager.cpp b/soh/soh/SaveManager.cpp index 466e9a7b3..82364fd08 100644 --- a/soh/soh/SaveManager.cpp +++ b/soh/soh/SaveManager.cpp @@ -50,10 +50,12 @@ SaveManager::SaveManager() { AddLoadFunction("base", 2, LoadBaseVersion2); AddLoadFunction("base", 3, LoadBaseVersion3); AddSaveFunction("base", 3, SaveBase); + RegisterSectionAutoSave("base"); AddLoadFunction("randomizer", 1, LoadRandomizerVersion1); AddLoadFunction("randomizer", 2, LoadRandomizerVersion2); AddSaveFunction("randomizer", 2, SaveRandomizer); + RegisterSectionAutoSave("randomizer"); AddInitFunction(InitFileImpl); @@ -83,6 +85,18 @@ SaveManager::SaveManager() { } } +void SaveManager::RegisterSectionAutoSave(std::string section) { + if (!std::any_of(autosaveRegistry.begin(), autosaveRegistry.end(), section)) { + autosaveRegistry.push_back(section); + } +} + +void SaveManager::UnregisterSectionAutoSave(std::string section) { + if (std::any_of(autosaveRegistry.begin(), autosaveRegistry.end(), section)) { + autosaveRegistry.erase(find(autosaveRegistry.begin(), autosaveRegistry.end(), section)); + } +} + void SaveManager::LoadRandomizerVersion1() { for (int i = 0; i < ARRAY_COUNT(gSaveContext.itemLocations); i++) { SaveManager::Instance->LoadStruct("get" + std::to_string(i), [&]() { @@ -726,17 +740,27 @@ void SaveManager::InitFileDebug() { } // Threaded SaveFile takes copy of gSaveContext for local unmodified storage -void SaveManager::SaveFileThreaded(int fileNum, SaveContext* saveContext) { - nlohmann::json baseBlock; +void SaveManager::SaveFileThreaded(int fileNum, SaveContext* saveContext, const std::string sectionString = "all") { + /*nlohmann::json baseBlock; baseBlock["version"] = 1; - baseBlock["sections"] = nlohmann::json::object(); - for (auto& section : sectionSaveHandlers) { - nlohmann::json& sectionBlock = baseBlock["sections"][section.first]; - sectionBlock["version"] = section.second.first; + baseBlock["sections"] = nlohmann::json::object();*/ + if (sectionString == "all") { + for (auto& sectionHandler : sectionSaveHandlers) { + nlohmann::json& sectionBlock = saveBlock["sections"][sectionHandler.first]; + sectionBlock["version"] = sectionHandler.second.first; + currentJsonContext = §ionBlock["data"]; + sectionHandler.second.second(saveContext); + } + } else if (sectionSaveHandlers.contains(sectionString) && std::any_of(autosaveRegistry.begin(), autosaveRegistry.end(), sectionString)) { + SectionSaveHandler handler = sectionSaveHandlers.find(sectionString)->second; + nlohmann::json& sectionBlock = saveBlock["sections"][sectionString]; + sectionBlock["version"] = handler.first; currentJsonContext = §ionBlock["data"]; - section.second.second(saveContext); + handler.second(saveContext); + } else { + return; } #if defined(__SWITCH__) || defined(__WIIU__) @@ -746,7 +770,7 @@ void SaveManager::SaveFileThreaded(int fileNum, SaveContext* saveContext) { fclose(w); #else std::ofstream output(GetFileName(fileNum)); - output << std::setw(4) << baseBlock << std::endl; + output << std::setw(4) << saveBlock << std::endl; #endif delete saveContext; diff --git a/soh/soh/SaveManager.h b/soh/soh/SaveManager.h index 8b32774e1..e31696fcf 100644 --- a/soh/soh/SaveManager.h +++ b/soh/soh/SaveManager.h @@ -118,13 +118,17 @@ public: static const int MaxFiles = 3; std::array fileMetaInfo; + void RegisterSectionAutoSave(std::string section); + void UnregisterSectionAutoSave(std::string section); + private: std::filesystem::path GetFileName(int fileNum); + nlohmann::json saveBlock; void ConvertFromUnversioned(); void CreateDefaultGlobal(); - void SaveFileThreaded(int fileNum, SaveContext* saveContext); + void SaveFileThreaded(int fileNum, SaveContext* saveContext, const std::string sectionString); void InitMeta(int slotNum); static void InitFileImpl(bool isDebug); @@ -147,6 +151,8 @@ public: using SectionSaveHandler = std::pair; std::map sectionSaveHandlers; + // sets a section to + std::vector autosaveRegistry; std::map postHandlers;