Preliminary work on persistent save data json block.

Added autosaveRegistry to set section to save with overall autosave triggers.
This commit is contained in:
Malkierian 2023-05-10 14:10:13 -07:00
parent dd43719a14
commit 07bfba0162
2 changed files with 39 additions and 9 deletions

View File

@ -50,10 +50,12 @@ SaveManager::SaveManager() {
AddLoadFunction("base", 2, LoadBaseVersion2); AddLoadFunction("base", 2, LoadBaseVersion2);
AddLoadFunction("base", 3, LoadBaseVersion3); AddLoadFunction("base", 3, LoadBaseVersion3);
AddSaveFunction("base", 3, SaveBase); AddSaveFunction("base", 3, SaveBase);
RegisterSectionAutoSave("base");
AddLoadFunction("randomizer", 1, LoadRandomizerVersion1); AddLoadFunction("randomizer", 1, LoadRandomizerVersion1);
AddLoadFunction("randomizer", 2, LoadRandomizerVersion2); AddLoadFunction("randomizer", 2, LoadRandomizerVersion2);
AddSaveFunction("randomizer", 2, SaveRandomizer); AddSaveFunction("randomizer", 2, SaveRandomizer);
RegisterSectionAutoSave("randomizer");
AddInitFunction(InitFileImpl); 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() { void SaveManager::LoadRandomizerVersion1() {
for (int i = 0; i < ARRAY_COUNT(gSaveContext.itemLocations); i++) { for (int i = 0; i < ARRAY_COUNT(gSaveContext.itemLocations); i++) {
SaveManager::Instance->LoadStruct("get" + std::to_string(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 // Threaded SaveFile takes copy of gSaveContext for local unmodified storage
void SaveManager::SaveFileThreaded(int fileNum, SaveContext* saveContext) { void SaveManager::SaveFileThreaded(int fileNum, SaveContext* saveContext, const std::string sectionString = "all") {
nlohmann::json baseBlock; /*nlohmann::json baseBlock;
baseBlock["version"] = 1; baseBlock["version"] = 1;
baseBlock["sections"] = nlohmann::json::object(); baseBlock["sections"] = nlohmann::json::object();*/
for (auto& section : sectionSaveHandlers) { if (sectionString == "all") {
nlohmann::json& sectionBlock = baseBlock["sections"][section.first]; for (auto& sectionHandler : sectionSaveHandlers) {
sectionBlock["version"] = section.second.first; nlohmann::json& sectionBlock = saveBlock["sections"][sectionHandler.first];
sectionBlock["version"] = sectionHandler.second.first;
currentJsonContext = &sectionBlock["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 = &sectionBlock["data"]; currentJsonContext = &sectionBlock["data"];
section.second.second(saveContext); handler.second(saveContext);
} else {
return;
} }
#if defined(__SWITCH__) || defined(__WIIU__) #if defined(__SWITCH__) || defined(__WIIU__)
@ -746,7 +770,7 @@ void SaveManager::SaveFileThreaded(int fileNum, SaveContext* saveContext) {
fclose(w); fclose(w);
#else #else
std::ofstream output(GetFileName(fileNum)); std::ofstream output(GetFileName(fileNum));
output << std::setw(4) << baseBlock << std::endl; output << std::setw(4) << saveBlock << std::endl;
#endif #endif
delete saveContext; delete saveContext;

View File

@ -118,13 +118,17 @@ public:
static const int MaxFiles = 3; static const int MaxFiles = 3;
std::array<SaveFileMetaInfo, MaxFiles> fileMetaInfo; std::array<SaveFileMetaInfo, MaxFiles> fileMetaInfo;
void RegisterSectionAutoSave(std::string section);
void UnregisterSectionAutoSave(std::string section);
private: private:
std::filesystem::path GetFileName(int fileNum); std::filesystem::path GetFileName(int fileNum);
nlohmann::json saveBlock;
void ConvertFromUnversioned(); void ConvertFromUnversioned();
void CreateDefaultGlobal(); void CreateDefaultGlobal();
void SaveFileThreaded(int fileNum, SaveContext* saveContext); void SaveFileThreaded(int fileNum, SaveContext* saveContext, const std::string sectionString);
void InitMeta(int slotNum); void InitMeta(int slotNum);
static void InitFileImpl(bool isDebug); static void InitFileImpl(bool isDebug);
@ -147,6 +151,8 @@ public:
using SectionSaveHandler = std::pair<int, SaveFunc>; using SectionSaveHandler = std::pair<int, SaveFunc>;
std::map<std::string, SectionSaveHandler> sectionSaveHandlers; std::map<std::string, SectionSaveHandler> sectionSaveHandlers;
// sets a section to
std::vector<std::string> autosaveRegistry;
std::map<std::string, PostFunc> postHandlers; std::map<std::string, PostFunc> postHandlers;