#pragma region License /* License (OLC-3) ~~~~~~~~~~~~~~~ Copyright 2024 Joshua Sigona Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions or derivations of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions or derivative works in binary form must reproduce the above copyright notice. This list of conditions and the following disclaimer must be reproduced in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Portions of this software are copyright © 2023 The FreeType Project (www.freetype.org). Please see LICENSE_FT.txt for more information. All rights reserved. */ #pragma endregion #include #include "config.h" #include "Item.h" #include "Crawler.h" #include "Unlock.h" #include "State_OverworldMap.h" #include "SaveFile.h" #include "ClassInfo.h" #include "ScrollableWindowComponent.h" #include "LoadFileButton.h" INCLUDE_game size_t SaveFile::saveFileID=0; std::string SaveFile::saveFileName=""; std::string SaveFile::username=""; const size_t SaveFile::GetSaveFileCount(){ size_t count=0; if(std::filesystem::exists("save_file_path"_S)){ for(auto&path:std::filesystem::directory_iterator("save_file_path"_S)){ if(path.is_regular_file()&&path.path()!="metadata.dat"){ count++; } } } return count; } const void SaveFile::SaveGame(){ std::filesystem::create_directories("save_file_path"_S); utils::datafile saveFile; utils::datafile::INITIAL_SETUP_COMPLETE=false; for(size_t itemCount=0;auto&[cat,items]:Inventory::sortedInv){ for(std::shared_ptr&item:items){ saveFile["Items"][std::format("Item[{}]",itemCount)]["Amt"].SetInt(item->Amt()); saveFile["Items"][std::format("Item[{}]",itemCount)]["Enhancement Level"].SetInt(item->EnhancementLevel()); saveFile["Items"][std::format("Item[{}]",itemCount)]["Item Name"].SetString(item->ActualName()); saveFile["Items"][std::format("Item[{}]",itemCount)]["Equip Slot"].SetInt(int(Inventory::GetSlotEquippedIn(item))); uint8_t loadoutSlotNumber=255; for(int i=0;iloadout.size();i++){ if(item==game->GetLoadoutItem(i)){loadoutSlotNumber=i;break;} } saveFile["Items"][std::format("Item[{}]",itemCount)]["LoadoutSlot"].SetInt(loadoutSlotNumber); for(const auto&[attr,val]:item->RandomStats()){ saveFile["Items"][std::format("Item[{}]",itemCount)]["Attributes"][std::string(attr.ActualName())].SetReal(val); } itemCount++; } } saveFile["Player"]["Class"].SetString(game->GetPlayer()->GetClassName()); saveFile["Player"]["Level"].SetInt(game->GetPlayer()->Level()); saveFile["Player"]["Money"].SetInt(game->GetPlayer()->GetMoney()); saveFile["Player"]["Current EXP"].SetInt(game->GetPlayer()->CurrentXP()); saveFile["Player"]["Total EXP"].SetInt(game->GetPlayer()->TotalXP()); for(const auto&[attr,val]:game->GetPlayer()->GetBaseStats()){ saveFile["Player"]["Base Stats"][std::string(attr.ActualName())].SetReal(val); } for(const std::string&unlockName:Unlock::unlocks){ saveFile["Unlocks"][unlockName].SetString("True"); } saveFile["Overworld Map Location"].SetString(State_OverworldMap::GetCurrentConnectionPoint().name); saveFile["Chapter"].SetInt(game->GetCurrentChapter()); saveFile["Save Name"].SetString(std::string(GetSaveFileName())); saveFile["Game Time"].SetReal(game->GetRuntime()); utils::datafile::Write(saveFile,"save_file_path"_S+std::format("save.{:04}",saveFileID)); utils::datafile metadata; if(std::filesystem::exists("save_file_path"_S+"metadata.dat")){ utils::datafile::Read(metadata,"save_file_path"_S+"metadata.dat"); } metadata.GetProperty(std::format("save{}",saveFileID)).SetReal(game->GetRuntime(),0U); metadata.GetProperty(std::format("save{}",saveFileID)).SetInt(game->GetCurrentChapter(),1U); metadata.GetProperty(std::format("save{}",saveFileID)).SetInt(game->GetPlayer()->Level(),2U); metadata.GetProperty(std::format("save{}",saveFileID)).SetString(game->GetPlayer()->GetClassName(),3U); metadata.GetProperty(std::format("save{}",saveFileID)).SetString(std::string(SaveFile::GetSaveFileName()),4U); utils::datafile::Write(metadata,"save_file_path"_S+"metadata.dat"); utils::datafile::INITIAL_SETUP_COMPLETE=true; #ifdef __EMSCRIPTEN__ Server_SaveFile([](std::string_view response){}); #endif } const void SaveFile::LoadGame(){ #ifdef __EMSCRIPTEN__ Server_GetFile([](std::string_view response){}); #else std::filesystem::create_directories("save_file_path"_S); utils::datafile loadFile; if(std::filesystem::exists("save_file_path"_S+std::format("save.{:04}",saveFileID))){ 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()); } } 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); } } 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); } State_OverworldMap::SetStageMarker(loadFile["Overworld Map Location"].GetString()); 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<(LOAD_GAME,"Game Files List"); gameFilesList->RemoveAllComponents(); const size_t saveFileCount=GetSaveFileCount(); utils::datafile metadata; utils::datafile::Read(metadata,"save_file_path"_S+"metadata.dat"); float offsetY=0; for(size_t i=0;iADD(std::format("Load File Button - Save {}",i),LoadFileButton)({{0,offsetY},{gameFilesList->GetSize().x-13,48}},metadata[std::format("save{}",i)],i,[](MenuFuncData data){ LoadFileButton*comp=DYNAMIC_CAST(data.component); saveFileID=comp->getSaveFileID(); SaveFile::LoadGame(); return true; },ButtonAttr::NONE)END; offsetY+=49; } } } const std::string SaveFile::CreateServerRequest(const SaveFileOperation::Operation operation,std::string_view data){ auto CalculateChecksum=[](std::string_view operation,std::string_view data){ return username.length()*8+data.length()*2+operation.length(); }; std::string dataString=std::format("?username={}",username); std::string operationName=""; switch(operation){ case SaveFileOperation::GET_LOAD_FILES:{ operationName=std::format("GET_LOAD_FILES"); //Data should be blank. }break; case SaveFileOperation::GET_FILE:{ operationName=std::format("GET_FILE"); //Data should contain the file ID we want. }break; case SaveFileOperation::SAVE_FILE:{ operationName=std::format("SAVE_FILE"); //Data should contain the entire contents of our save file. }break; case SaveFileOperation::SAVE_METADATA_FILE:{ operationName=std::format("SAVE_METADATA_FILE"); //Data should contain the entire contents of our metadata save file. }break; } dataString+="&operation="+operationName; dataString+="&checksum="+std::to_string(CalculateChecksum(operationName,data)); dataString+="&data="+std::string(data); return "save_server"_S+dataString; } const void SaveFile::Server_GetLoadInfo(std::functionrespCallbackFunc){ game->SendRequest(CreateServerRequest(SaveFileOperation::GET_LOAD_FILES,"0"),""); game->responseCallback=respCallbackFunc; } const void SaveFile::Server_GetFile(std::functionrespCallbackFunc){ game->SendRequest(CreateServerRequest(SaveFileOperation::GET_FILE,std::to_string(saveFileID)),""); game->responseCallback=respCallbackFunc; } const void SaveFile::Server_SaveFile(std::functionrespCallbackFunc){ std::stringstream fileContents; std::ifstream file("save_file_path"_S+std::format("save.{:04}",saveFileID)); while(file.good()){ fileContents<SendRequest(CreateServerRequest(SaveFileOperation::SAVE_FILE,std::to_string(saveFileID)+"|"+fileContents.str()),""); game->responseCallback=respCallbackFunc; } const void SaveFile::Server_SaveMetadataFile(std::functionrespCallbackFunc){ std::stringstream fileContents; std::ifstream file("save_file_path"_S+"metadata.dat"); while(file.good()){ fileContents<SendRequest(CreateServerRequest(SaveFileOperation::SAVE_METADATA_FILE,fileContents.str()),""); game->responseCallback=respCallbackFunc; }