@ -51,7 +51,7 @@ INCLUDE_game
size_t SaveFile : : saveFileID = 0 ;
size_t SaveFile : : saveFileID = 0 ;
std : : string SaveFile : : saveFileName = " " ;
std : : string SaveFile : : saveFileName = " " ;
std : : string SaveFile : : username = " " ;
std : : string SaveFile : : username = " " ;
bool SaveFile : : onlineMode = fals e;
bool SaveFile : : onlineMode = tru e;
const size_t SaveFile : : GetSaveFileCount ( ) {
const size_t SaveFile : : GetSaveFileCount ( ) {
std : : filesystem : : create_directories ( " save_file_path " _S ) ;
std : : filesystem : : create_directories ( " save_file_path " _S ) ;
@ -64,6 +64,17 @@ const size_t SaveFile::GetSaveFileCount(){
return count ;
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 ( ) {
const void SaveFile : : SaveGame ( ) {
std : : filesystem : : create_directories ( " save_file_path " _S ) ;
std : : filesystem : : create_directories ( " save_file_path " _S ) ;
utils : : datafile saveFile ;
utils : : datafile saveFile ;
@ -109,12 +120,16 @@ const void SaveFile::SaveGame(){
saveFile [ " Chapter " ] . SetInt ( game - > GetCurrentChapter ( ) ) ;
saveFile [ " Chapter " ] . SetInt ( game - > GetCurrentChapter ( ) ) ;
saveFile [ " Save Name " ] . SetString ( std : : string ( GetSaveFileName ( ) ) ) ;
saveFile [ " Save Name " ] . SetString ( std : : string ( GetSaveFileName ( ) ) ) ;
saveFile [ " Game Time " ] . SetReal ( game - > GetRuntime ( ) ) ;
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 ;
utils : : datafile metadata ;
if ( std : : filesystem : : exists ( " save_file_path " _S + " metadata.dat " ) ) {
if ( onlineMode ) {
utils : : datafile : : Read ( metadata , " save_file_path " _S + " metadata.dat " ) ;
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 ) ) . SetReal ( game - > GetRuntime ( ) , 0U ) ;
metadata . GetProperty ( std : : format ( " save{} " , saveFileID ) ) . SetInt ( game - > GetCurrentChapter ( ) , 1U ) ;
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 ) ;
metadata . GetProperty ( std : : format ( " save{} " , saveFileID ) ) . SetString ( std : : string ( SaveFile : : GetSaveFileName ( ) ) , 4U ) ;
if ( ! onlineMode ) {
if ( ! onlineMode ) {
utils : : datafile : : Write ( metadata , " save_file_path " _S + " metadata.dat " ) ;
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 ;
utils : : datafile : : INITIAL_SETUP_COMPLETE = true ;
@ -244,7 +261,7 @@ const void SaveFile::LoadGame(){
if ( onlineMode ) {
if ( onlineMode ) {
Server_GetFile ( [ & ] ( std : : string_view response ) {
Server_GetFile ( [ & ] ( std : : string_view response ) {
if ( response ! = " ERR " ) {
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 < < response ;
file . close ( ) ;
file . close ( ) ;
LoadFile ( ) ;
LoadFile ( ) ;
@ -288,8 +305,11 @@ const void SaveFile::UpdateSaveGameData(){
std : : filesystem : : create_directories ( " save_file_path " _S ) ;
std : : filesystem : : create_directories ( " save_file_path " _S ) ;
auto LoadMetadataFile = [ ] ( ) {
auto LoadMetadataFile = [ ] ( ) {
auto gameFilesList = Component < ScrollableWindowComponent > ( LOAD_GAME , " Game Files List " ) ;
auto gameFilesList = Component < ScrollableWindowComponent > ( LOAD_GAME , " Game Files List " ) ;
auto onlineGameFilesList = Component < ScrollableWindowComponent > ( LOAD_GAME , " Online Game Files List " ) ;
gameFilesList - > RemoveAllComponents ( ) ;
gameFilesList - > RemoveAllComponents ( ) ;
onlineGameFilesList - > RemoveAllComponents ( ) ;
const size_t saveFileCount = GetSaveFileCount ( ) ;
const size_t saveFileCount = GetSaveFileCount ( ) ;
const size_t onlineSaveFileCount = GetOnlineSaveFileCount ( ) ;
utils : : datafile metadata , online_metadata ;
utils : : datafile metadata , online_metadata ;
if ( ! std : : filesystem : : exists ( " save_file_path " _S + " metadata.dat " ) ) {
if ( ! std : : filesystem : : exists ( " save_file_path " _S + " metadata.dat " ) ) {
utils : : datafile : : Write ( metadata , " 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 + = 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 LoadMetadataFromDB = [ ] ( ) {
auto gameFilesList = Component < ScrollableWindowComponent > ( LOAD_GAME , " Game Files List " ) ;
auto gameFilesList = Component < ScrollableWindowComponent > ( LOAD_GAME , " Game Files List " ) ;
@ -364,6 +396,7 @@ const void SaveFile::UpdateSaveGameData(){
file < < response ;
file < < response ;
file . close ( ) ;
file . close ( ) ;
LoadMetadataFile ( ) ;
LoadMetadataFile ( ) ;
Menu : : OpenMenu ( LOAD_GAME ) ;
}
}
} ) ;
} ) ;
} else {
} 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 ) {
const void SaveFile : : Server_SaveFile ( std : : function < void ( std : : string_view ) > respCallbackFunc ) {
std : : stringstream fileContents ;
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 ( ) ) {
while ( file . good ( ) ) {
int val = file . get ( ) ;
int val = file . get ( ) ;
if ( val ! = - 1 ) {
if ( val ! = - 1 ) {