Fix file saving/loading for online mode.

pull/35/head
sigonasr2 10 months ago
parent 7961e3d8aa
commit 39a710727e
  1. 16
      Adventures in Lestoria/Error.h
  2. 13
      Adventures in Lestoria/LoadGameWindow.cpp
  3. 3
      Adventures in Lestoria/Menu.h
  4. 49
      Adventures in Lestoria/SaveFile.cpp
  5. 1
      Adventures in Lestoria/SaveFile.h
  6. 6
      Adventures in Lestoria/SaveFileWindow.cpp
  7. 4
      Adventures in Lestoria/ScrollableWindowComponent.h
  8. 2
      Adventures in Lestoria/Version.h

@ -41,9 +41,7 @@ All rights reserved.
#include <format>
#include <any>
#include <memory>
#ifndef __EMSCRIPTEN__
#include <source_location>
#endif
#include <source_location>
#ifdef _DEBUG
#define NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
@ -73,7 +71,17 @@ All rights reserved.
#define ERR(err) { \
std::stringstream errStream; \
errStream<<err; \
std::cout<<errStream.str(); }
Error::log(errStream,std::source_location::current());}
class Error{
public:
inline static void log(std::stringstream&str,std::source_location loc){
std::cout<<loc.file_name()<<"("<<loc.line()<<":"<<loc.column()<<") "<<loc.function_name()<<": "<<str.str()<<std::endl;
#ifdef __DEBUG__
throw;
#endif
}
};
#define _CrtDumpMemoryLeaks() ((int)0)
#endif

@ -44,9 +44,10 @@ void Menu::InitializeLoadGameWindow(){
Menu*loadGameWindow=CreateMenu(LOAD_GAME,CENTERED,vi2d{96,120});
loadGameWindow->ADD("Game Files Label",MenuLabel)(geom2d::rect<float>{{-8,-12},{112,12}},"Load Game",1.0f,ComponentAttr::SHADOW|ComponentAttr::OUTLINE|ComponentAttr::BACKGROUND)END;
loadGameWindow->ADD("Game Files List",ScrollableWindowComponent)(geom2d::rect<float>{{-8,4},{112,116}})END;
loadGameWindow->ADD("Online Game Files List",ScrollableWindowComponent)(geom2d::rect<float>{{-8,4},{112,116}})END
loadGameWindow->ADD("Game Files List",ScrollableWindowComponent)(geom2d::rect<float>{{-8,4},{112,116}})END
->Enable(false);
loadGameWindow->ADD("Online Game Files List",ScrollableWindowComponent)(geom2d::rect<float>{{-8,4},{112,116}})END
->Enable(true);
loadGameWindow->ADD("Go Back Button",MenuComponent)(geom2d::rect<float>{{24,124},{48,12}},"Back",[](MenuFuncData menu){Menu::CloseMenu();return true;})END;
@ -64,8 +65,12 @@ void Menu::InitializeLoadGameWindow(){
#pragma region Keyboard Navigation Rules
loadGameWindow->SetupKeyboardNavigation(
[](MenuType type,Data&returnData){ //On Open
if(SaveFile::GetSaveFileCount()>0){
returnData=Component<ScrollableWindowComponent>(type,"Game Files List")->GetComponents()[0];
if(SaveFile::GetSaveFileCount()>0||SaveFile::GetOnlineSaveFileCount()>0){
if(SaveFile::IsOnline()){
returnData=Component<ScrollableWindowComponent>(type,"Online Game Files List")->GetComponents()[0];
}else{
returnData=Component<ScrollableWindowComponent>(type,"Game Files List")->GetComponents()[0];
}
}else{
returnData="Go Back Button";
}

@ -133,9 +133,6 @@ public:
component->depth=depth;
}
if(components.count(componentKey)){
ERR("WARNING! Key "<<componentKey<<" for this sub-menu already exists! Key names must be unique!")
}
component->name=componentKey;
components.Unlock(); //It's possible we can add a component later on, so we will make sure we remove the lock first.
components[componentKey]=component;

@ -51,7 +51,7 @@ INCLUDE_game
size_t SaveFile::saveFileID=0;
std::string SaveFile::saveFileName="";
std::string SaveFile::username="";
bool SaveFile::onlineMode=false;
bool SaveFile::onlineMode=true;
const size_t SaveFile::GetSaveFileCount(){
std::filesystem::create_directories("save_file_path"_S);
@ -64,6 +64,17 @@ const size_t SaveFile::GetSaveFileCount(){
return count;
}
const size_t SaveFile::GetOnlineSaveFileCount(){
std::filesystem::create_directories("save_file_path"_S);
size_t count=0;
if(std::filesystem::exists("save_file_path"_S+"metadata.dat"+"_online")){
utils::datafile metadata;
utils::datafile::Read(metadata,"save_file_path"_S+"metadata.dat"+"_online");
return metadata.GetKeys().size();
}
return count;
}
const void SaveFile::SaveGame(){
std::filesystem::create_directories("save_file_path"_S);
utils::datafile saveFile;
@ -109,12 +120,16 @@ const void SaveFile::SaveGame(){
saveFile["Chapter"].SetInt(game->GetCurrentChapter());
saveFile["Save Name"].SetString(std::string(GetSaveFileName()));
saveFile["Game Time"].SetReal(game->GetRuntime());
if(!onlineMode){
utils::datafile::Write(saveFile,"save_file_path"_S+std::format("save.{:04}",saveFileID));
}
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");
if(onlineMode){
if(std::filesystem::exists("save_file_path"_S+"metadata.dat"+"_online")){
utils::datafile::Read(metadata,"save_file_path"_S+"metadata.dat"+"_online");
}
}else{
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);
@ -123,6 +138,8 @@ const void SaveFile::SaveGame(){
metadata.GetProperty(std::format("save{}",saveFileID)).SetString(std::string(SaveFile::GetSaveFileName()),4U);
if(!onlineMode){
utils::datafile::Write(metadata,"save_file_path"_S+"metadata.dat");
}else{
utils::datafile::Write(metadata,"save_file_path"_S+"metadata.dat"+"_online");
}
utils::datafile::INITIAL_SETUP_COMPLETE=true;
@ -244,7 +261,7 @@ const void SaveFile::LoadGame(){
if(onlineMode){
Server_GetFile([&](std::string_view response){
if(response!="ERR"){
std::ofstream file("save_file_path"_S+std::format("save.{:04}",saveFileID)+"_online");
std::ofstream file("save_file_path"_S+std::format("save.{:04}",saveFileID));
file<<response;
file.close();
LoadFile();
@ -288,8 +305,11 @@ const void SaveFile::UpdateSaveGameData(){
std::filesystem::create_directories("save_file_path"_S);
auto LoadMetadataFile=[](){
auto gameFilesList=Component<ScrollableWindowComponent>(LOAD_GAME,"Game Files List");
auto onlineGameFilesList=Component<ScrollableWindowComponent>(LOAD_GAME,"Online Game Files List");
gameFilesList->RemoveAllComponents();
onlineGameFilesList->RemoveAllComponents();
const size_t saveFileCount=GetSaveFileCount();
const size_t onlineSaveFileCount=GetOnlineSaveFileCount();
utils::datafile metadata,online_metadata;
if(!std::filesystem::exists("save_file_path"_S+"metadata.dat")){
utils::datafile::Write(metadata,"save_file_path"_S+"metadata.dat");
@ -313,6 +333,18 @@ const void SaveFile::UpdateSaveGameData(){
offsetY+=49;
}
}
offsetY=0;
for(size_t i=0;i<onlineSaveFileCount;i++){
if(online_metadata.HasProperty(std::format("save{}",i))){
onlineGameFilesList->ADD(std::format("Online Load File Button - Save {}",i),LoadFileButton)(geom2d::rect<float>{{0,offsetY},{onlineGameFilesList->GetSize().x-13,48}},online_metadata[std::format("save{}",i)],i,[](MenuFuncData data){
std::weak_ptr<LoadFileButton>comp=DYNAMIC_POINTER_CAST<LoadFileButton>(data.component.lock());
saveFileID=comp.lock()->getSaveFileID();
SaveFile::LoadGame();
return true;
},ButtonAttr::NONE)END;
offsetY+=49;
}
}
};
auto LoadMetadataFromDB=[](){
auto gameFilesList=Component<ScrollableWindowComponent>(LOAD_GAME,"Game Files List");
@ -364,6 +396,7 @@ const void SaveFile::UpdateSaveGameData(){
file<<response;
file.close();
LoadMetadataFile();
Menu::OpenMenu(LOAD_GAME);
}
});
}else{
@ -429,7 +462,7 @@ const void SaveFile::Server_GetFile(std::function<void(std::string_view)>respCal
}
const void SaveFile::Server_SaveFile(std::function<void(std::string_view)>respCallbackFunc){
std::stringstream fileContents;
std::ifstream file("save_file_path"_S+std::format("save.{:04}",saveFileID)+"_online");
std::ifstream file("save_file_path"_S+std::format("save.{:04}",saveFileID));
while(file.good()){
int val=file.get();
if(val!=-1){

@ -57,6 +57,7 @@ public:
static const std::string_view GetSaveFileName();
static const void SetSaveFileName(std::string_view saveFileName);
static const size_t GetSaveFileCount();
static const size_t GetOnlineSaveFileCount();
static const bool IsOnline();
static void SetOnlineMode(bool online);
static const std::string_view GetUserID();

@ -49,7 +49,11 @@ void Menu::InitializeSaveFileWindow(){
saveFileWindow->ADD("Back Button",MenuComponent)(geom2d::rect<float>{{-8,68},{48,12}},"Back",[](MenuFuncData data){Menu::CloseMenu();game->TextEntryEnable(false);return true;})END;
saveFileWindow->ADD("Continue Button",MenuComponent)(geom2d::rect<float>{{56,68},{48,12}},"Begin",MenuType::CLASS_SELECTION,[](MenuFuncData data){
SaveFile::SetSaveFileName(game->TextEntryGetString());
SaveFile::SetSaveFileID(SaveFile::GetSaveFileCount());
if(SaveFile::IsOnline()){
SaveFile::SetSaveFileID(SaveFile::GetOnlineSaveFileCount());
}else{
SaveFile::SetSaveFileID(SaveFile::GetSaveFileCount());
}
game->TextEntryEnable(false);
SaveFile::SaveGame();
return true;

@ -276,6 +276,10 @@ protected:
public:
template<class T>
std::shared_ptr<T> _AddComponent(std::string key,std::shared_ptr<T>button){
if(Menu::menus[parentMenu]->components.count(key)){
ERR("WARNING! Key "<<key<<" for menu"<<parentMenu<<" already exists! Key names must be unique!")
}
components.push_back(button);
button->renderInMain=false; //Now we are in control!
button->parentComponent=DYNAMIC_POINTER_CAST<ScrollableWindowComponent>(Menu::menus[parentMenu]->components[this->GetName()]);

@ -39,7 +39,7 @@ All rights reserved.
#define VERSION_MAJOR 0
#define VERSION_MINOR 3
#define VERSION_PATCH 0
#define VERSION_BUILD 6401
#define VERSION_BUILD 6402
#define stringify(a) stringify_(a)
#define stringify_(a) #a

Loading…
Cancel
Save