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

pull/35/head
sigonasr2 10 months 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
}
const void SaveFile::LoadGame(){
std::filesystem::create_directories("save_file_path"_S);
auto 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_ptr<Item>newItem=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());
}
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_ptr<Item>newItem=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<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__
if(onlineMode){
@ -261,7 +262,7 @@ const void SaveFile::LoadGame(){
file<<rawMetadata[i];
}
file.close();
LoadGame();
LoadFile();
},[](void*arg){
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 SaveGame();
static const void LoadGame();
static void LoadFile();
static const void SetSaveFileID(size_t saveFileID);
//Called whenever the save game data is updated.
static const void UpdateSaveGameData();

Loading…
Cancel
Save