From 7961e3d8aad30cf8594a8c56e24222c9a9bca566 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sat, 27 Jan 2024 03:24:05 -0600 Subject: [PATCH] Loading a file should call LoadFile instead of recursively calling LoadGame. --- Adventures in Lestoria/SaveFile.cpp | 105 ++++++++++++++-------------- Adventures in Lestoria/SaveFile.h | 1 + 2 files changed, 54 insertions(+), 52 deletions(-) diff --git a/Adventures in Lestoria/SaveFile.cpp b/Adventures in Lestoria/SaveFile.cpp index 22f3b4eb..77c32567 100644 --- a/Adventures in Lestoria/SaveFile.cpp +++ b/Adventures in Lestoria/SaveFile.cpp @@ -180,64 +180,65 @@ const void SaveFile::SaveGame(){ #endif } -const void SaveFile::LoadGame(){ - std::filesystem::create_directories("save_file_path"_S); - auto LoadFile=[&](){ - utils::datafile loadFile; +void SaveFile::LoadFile(){ + utils::datafile loadFile; - std::string loadFilename="save_file_path"_S+std::format("save.{:04}",saveFileID); - - if(std::filesystem::exists(loadFilename)){ - utils::datafile::Read(loadFile,"save_file_path"_S+std::format("save.{:04}",saveFileID)); - game->ResetGame(); - for(auto&[key,data]:loadFile["Items"].GetOrderedKeys()){ - std::weak_ptrnewItem=Inventory::AddItem(data["Item Name"].GetString(),data["Amt"].GetInt()); - newItem.lock()->enhancementLevel=data["Enhancement Level"].GetInt(); - if(loadFile.GetProperty(std::format("Items.{}",key)).HasProperty("Attributes")){ - for(auto&[attr,data]:loadFile.GetProperty(std::format("Items.{}.Attributes",key)).GetOrderedKeys()){ - newItem.lock()->randomizedStats.A(attr)=data.GetReal(); - } - } - if(data.HasProperty("LoadoutSlot")){ - uint8_t loadoutSlot=data["LoadoutSlot"].GetInt(); - if(loadoutSlot!=255){ - game->SetLoadoutItem(loadoutSlot,newItem.lock()->ActualName()); - } + std::string loadFilename="save_file_path"_S+std::format("save.{:04}",saveFileID); + + if(std::filesystem::exists(loadFilename)){ + utils::datafile::Read(loadFile,"save_file_path"_S+std::format("save.{:04}",saveFileID)); + game->ResetGame(); + for(auto&[key,data]:loadFile["Items"].GetOrderedKeys()){ + std::weak_ptrnewItem=Inventory::AddItem(data["Item Name"].GetString(),data["Amt"].GetInt()); + newItem.lock()->enhancementLevel=data["Enhancement Level"].GetInt(); + if(loadFile.GetProperty(std::format("Items.{}",key)).HasProperty("Attributes")){ + for(auto&[attr,data]:loadFile.GetProperty(std::format("Items.{}.Attributes",key)).GetOrderedKeys()){ + newItem.lock()->randomizedStats.A(attr)=data.GetReal(); } - - EquipSlot slot=EquipSlot(loadFile.GetProperty(std::format("Items.{}.Equip Slot",key)).GetInt()); - if(slot!=EquipSlot::NONE){ //This should be equipped somewhere! - Inventory::EquipItem(newItem,slot); + } + if(data.HasProperty("LoadoutSlot")){ + uint8_t loadoutSlot=data["LoadoutSlot"].GetInt(); + if(loadoutSlot!=255){ + game->SetLoadoutItem(loadoutSlot,newItem.lock()->ActualName()); } } - game->ChangePlayerClass(classutils::StringToClass(loadFile["Player"]["Class"].GetString())); - game->GetPlayer()->level=loadFile["Player"]["Level"].GetInt(); - game->GetPlayer()->SetMoney(loadFile["Player"]["Money"].GetInt()); - game->GetPlayer()->currentLevelXP=loadFile["Player"]["Current EXP"].GetInt(); - game->GetPlayer()->totalXPEarned=loadFile["Player"]["Total EXP"].GetInt(); - for(auto&[key,data]:loadFile["Player"]["Base Stats"].GetOrderedKeys()){ - game->GetPlayer()->SetBaseStat(key,data.GetReal()); + + EquipSlot slot=EquipSlot(loadFile.GetProperty(std::format("Items.{}.Equip Slot",key)).GetInt()); + if(slot!=EquipSlot::NONE){ //This should be equipped somewhere! + Inventory::EquipItem(newItem,slot); } - for(const auto&[key,data]:loadFile["Unlocks"].GetOrderedKeys()){ - Unlock::UnlockArea(key); - if(data.GetValueCount()>1&&data.GetBool(1U)){ - auto opt_cp=State_OverworldMap::ConnectionPointFromString(key); - if(!opt_cp)ERR(std::format("WARNING! Could not find connection point {}! THIS SHOULD NOT BE HAPPENING! Potential invalid unlock name.",key)) - else opt_cp.value()->SetVisited(); - } + } + game->ChangePlayerClass(classutils::StringToClass(loadFile["Player"]["Class"].GetString())); + game->GetPlayer()->level=loadFile["Player"]["Level"].GetInt(); + game->GetPlayer()->SetMoney(loadFile["Player"]["Money"].GetInt()); + game->GetPlayer()->currentLevelXP=loadFile["Player"]["Current EXP"].GetInt(); + game->GetPlayer()->totalXPEarned=loadFile["Player"]["Total EXP"].GetInt(); + for(auto&[key,data]:loadFile["Player"]["Base Stats"].GetOrderedKeys()){ + game->GetPlayer()->SetBaseStat(key,data.GetReal()); + } + for(const auto&[key,data]:loadFile["Unlocks"].GetOrderedKeys()){ + Unlock::UnlockArea(key); + if(data.GetValueCount()>1&&data.GetBool(1U)){ + auto opt_cp=State_OverworldMap::ConnectionPointFromString(key); + if(!opt_cp)ERR(std::format("WARNING! Could not find connection point {}! THIS SHOULD NOT BE HAPPENING! Potential invalid unlock name.",key)) + else opt_cp.value()->SetVisited(); } - State_OverworldMap::SetStageMarker(loadFile["Overworld Map Location"].GetString()); - State_OverworldMap::UpdateCurrentConnectionPoint(const_cast(State_OverworldMap::GetCurrentConnectionPoint())); - game->SetChapter(loadFile["Chapter"].GetInt()); - SaveFile::SetSaveFileName(loadFile["Save Name"].GetString()); - game->SetRuntime(loadFile["Game Time"].GetReal()); - game->GetPlayer()->RecalculateEquipStats(); - - GameState::ChangeState(States::OVERWORLD_MAP,0.5f); - }else{ - std::cout<(State_OverworldMap::GetCurrentConnectionPoint())); + game->SetChapter(loadFile["Chapter"].GetInt()); + SaveFile::SetSaveFileName(loadFile["Save Name"].GetString()); + game->SetRuntime(loadFile["Game Time"].GetReal()); + game->GetPlayer()->RecalculateEquipStats(); + + GameState::ChangeState(States::OVERWORLD_MAP,0.5f); + }else{ + std::cout<