Loading a file should call LoadFile instead of recursively calling LoadGame.

pull/35/head
sigonasr2 1 year ago
parent e036302b58
commit 7961e3d8aa
  1. 107
      Adventures in Lestoria/SaveFile.cpp
  2. 1
      Adventures in Lestoria/SaveFile.h

@ -180,64 +180,65 @@ const void SaveFile::SaveGame(){
#endif #endif
} }
const void SaveFile::LoadGame(){ void SaveFile::LoadFile(){
std::filesystem::create_directories("save_file_path"_S); utils::datafile loadFile;
auto LoadFile=[&](){
utils::datafile loadFile; std::string loadFilename="save_file_path"_S+std::format("save.{:04}",saveFileID);
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));
if(std::filesystem::exists(loadFilename)){ game->ResetGame();
utils::datafile::Read(loadFile,"save_file_path"_S+std::format("save.{:04}",saveFileID)); for(auto&[key,data]:loadFile["Items"].GetOrderedKeys()){
game->ResetGame(); std::weak_ptr<Item>newItem=Inventory::AddItem(data["Item Name"].GetString(),data["Amt"].GetInt());
for(auto&[key,data]:loadFile["Items"].GetOrderedKeys()){ newItem.lock()->enhancementLevel=data["Enhancement Level"].GetInt();
std::weak_ptr<Item>newItem=Inventory::AddItem(data["Item Name"].GetString(),data["Amt"].GetInt()); if(loadFile.GetProperty(std::format("Items.{}",key)).HasProperty("Attributes")){
newItem.lock()->enhancementLevel=data["Enhancement Level"].GetInt(); for(auto&[attr,data]:loadFile.GetProperty(std::format("Items.{}.Attributes",key)).GetOrderedKeys()){
if(loadFile.GetProperty(std::format("Items.{}",key)).HasProperty("Attributes")){ newItem.lock()->randomizedStats.A(attr)=data.GetReal();
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());
}
} }
}
EquipSlot slot=EquipSlot(loadFile.GetProperty(std::format("Items.{}.Equip Slot",key)).GetInt()); if(data.HasProperty("LoadoutSlot")){
if(slot!=EquipSlot::NONE){ //This should be equipped somewhere! uint8_t loadoutSlot=data["LoadoutSlot"].GetInt();
Inventory::EquipItem(newItem,slot); if(loadoutSlot!=255){
game->SetLoadoutItem(loadoutSlot,newItem.lock()->ActualName());
} }
} }
game->ChangePlayerClass(classutils::StringToClass(loadFile["Player"]["Class"].GetString()));
game->GetPlayer()->level=loadFile["Player"]["Level"].GetInt(); EquipSlot slot=EquipSlot(loadFile.GetProperty(std::format("Items.{}.Equip Slot",key)).GetInt());
game->GetPlayer()->SetMoney(loadFile["Player"]["Money"].GetInt()); if(slot!=EquipSlot::NONE){ //This should be equipped somewhere!
game->GetPlayer()->currentLevelXP=loadFile["Player"]["Current EXP"].GetInt(); Inventory::EquipItem(newItem,slot);
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); game->ChangePlayerClass(classutils::StringToClass(loadFile["Player"]["Class"].GetString()));
if(data.GetValueCount()>1&&data.GetBool(1U)){ game->GetPlayer()->level=loadFile["Player"]["Level"].GetInt();
auto opt_cp=State_OverworldMap::ConnectionPointFromString(key); game->GetPlayer()->SetMoney(loadFile["Player"]["Money"].GetInt());
if(!opt_cp)ERR(std::format("WARNING! Could not find connection point {}! THIS SHOULD NOT BE HAPPENING! Potential invalid unlock name.",key)) game->GetPlayer()->currentLevelXP=loadFile["Player"]["Current EXP"].GetInt();
else opt_cp.value()->SetVisited(); 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<ConnectionPoint&>(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<<std::format("WARNING! File {} does not exist for loading!","save_file_path"_S+std::format("save.{:04}",saveFileID))<<std::endl;
} }
}; State_OverworldMap::SetStageMarker(loadFile["Overworld Map Location"].GetString());
State_OverworldMap::UpdateCurrentConnectionPoint(const_cast<ConnectionPoint&>(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<<std::format("WARNING! File {} does not exist for loading!","save_file_path"_S+std::format("save.{:04}",saveFileID))<<std::endl;
}
};
const void SaveFile::LoadGame(){
std::filesystem::create_directories("save_file_path"_S);
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
if(onlineMode){ if(onlineMode){
@ -261,7 +262,7 @@ const void SaveFile::LoadGame(){
file<<rawMetadata[i]; file<<rawMetadata[i];
} }
file.close(); file.close();
LoadGame(); LoadFile();
},[](void*arg){ },[](void*arg){
std::cout<<"Failed to load Save File "<<saveFileID<<"!"<<std::endl; std::cout<<"Failed to load Save File "<<saveFileID<<"!"<<std::endl;
}); });

@ -63,6 +63,7 @@ public:
static const void SetUserID(std::string_view userID); static const void SetUserID(std::string_view userID);
static const void SaveGame(); static const void SaveGame();
static const void LoadGame(); static const void LoadGame();
static void LoadFile();
static const void SetSaveFileID(size_t saveFileID); static const void SetSaveFileID(size_t saveFileID);
//Called whenever the save game data is updated. //Called whenever the save game data is updated.
static const void UpdateSaveGameData(); static const void UpdateSaveGameData();

Loading…
Cancel
Save