@ -109,8 +109,9 @@ const void SaveFile::SaveGame(){
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 ) ) ;
if ( ! onlineMode ) {
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 " ) ;
@ -120,13 +121,29 @@ const void SaveFile::SaveGame(){
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 " ) ;
if ( ! onlineMode ) {
utils : : datafile : : Write ( metadata , " save_file_path " _S + " metadata.dat " ) ;
}
utils : : datafile : : INITIAL_SETUP_COMPLETE = true ;
# ifdef __EMSCRIPTEN__
if ( onlineMode ) {
std : : function < void ( std : : string_view response ) > RetryResponse ;
RetryResponse = [ & ] ( std : : string_view response ) {
if ( response ! = " ERR " ) {
Server_SaveFile ( [ ] ( std : : string_view response ) {
if ( response = = " ERR " ) {
std : : cout < < " WARNING! Could not save data to server! " < < std : : endl ;
}
} ) ;
} else {
std : : cout < < " WARNING! Could not save metadata to server! " < < std : : endl ;
}
} ;
Server_SaveMetadataFile ( RetryResponse ) ;
} else {
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 ) {
@ -134,26 +151,31 @@ const void SaveFile::SaveGame(){
}
}
std : : string contents = fileContents . str ( ) ;
emscripten_idb_async_store ( " /assets " , ( " save_file_path " _S + std : : format ( " save.{:04} " , saveFileID ) + " _online " ) . c_str ( ) , contents . data ( ) , contents . length ( ) , 0 , [ ] ( void * arg ) {
emscripten_idb_async_store ( " /assets " , ( " save_file_path " _S + std : : format ( " save.{:04} " , saveFileID ) ) . c_str ( ) , contents . data ( ) , contents . length ( ) , 0 , [ ] ( void * arg ) {
std : : cout < < " Successfully saved save file " < < saveFileID < < " ! " < < std : : endl ;
} , [ ] ( void * arg ) {
std : : cout < < " Failed to save save file " < < saveFileID < < " ! " < < std : : endl ;
} ) ;
file . close ( ) ;
std : : function < void ( std : : string_view response ) > RetryResponse ;
RetryResponse = [ & ] ( std : : string_view response ) {
if ( response ! = " ERR " ) {
Server_SaveFile ( [ ] ( std : : string_view response ) {
if ( response = = " ERR " ) {
std : : cout < < " WARNING! Could not save data to server! " < < std : : endl ;
}
} ) ;
} else {
std : : cout < < " WARNING! Could not save metadata to server! " < < std : : endl ;
std : : stringstream metafileContents ;
std : : ifstream metafile ( " save_file_path " _S + " metadata.dat " ) ;
while ( metafile . good ( ) ) {
int val = metafile . get ( ) ;
if ( val ! = - 1 ) {
metafileContents < < char ( val ) ;
}
} ;
Server_SaveMetadataFile ( RetryResponse ) ;
}
std : : string metaContents = metafileContents . str ( ) ;
emscripten_idb_async_store ( " /assets " , ( " save_file_path " _S + " metadata.dat " ) . c_str ( ) , metaContents . data ( ) , metaContents . length ( ) , 0 , [ ] ( void * arg ) {
std : : cout < < " Successfully saved metafile! " < < std : : endl ;
} , [ ] ( void * arg ) {
std : : cout < < " Failed to save save metafile! " < < std : : endl ;
} ) ;
metafile . close ( ) ;
}
# endif
}
@ -162,7 +184,10 @@ const void SaveFile::LoadGame(){
std : : filesystem : : create_directories ( " save_file_path " _S ) ;
auto LoadFile = [ & ] ( ) {
utils : : datafile loadFile ;
if ( std : : filesystem : : exists ( " 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 ) ) ;
game - > ResetGame ( ) ;
for ( auto & [ key , data ] : loadFile [ " Items " ] . GetOrderedKeys ( ) ) {
@ -197,8 +222,8 @@ const void SaveFile::LoadGame(){
Unlock : : UnlockArea ( key ) ;
if ( data . GetValueCount ( ) > 1 & & data . GetBool ( 1U ) ) {
auto opt_cp = State_OverworldMap : : ConnectionPointFromString ( key ) ;
if ( ! opt_cp . has_value ( ) ) ERR ( std : : format ( " WARNING! Could not find connection point {}! THIS SHOULD NOT BE HAPPENING! Potential invalid unlock name. " , key ) ) ;
opt_cp . value ( ) - > SetVisited ( ) ;
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 ( ) ) ;
@ -215,16 +240,32 @@ const void SaveFile::LoadGame(){
} ;
# ifdef __EMSCRIPTEN__
Server_GetFile ( [ & ] ( std : : string_view response ) {
if ( response ! = " ERR " ) {
if ( onlineMode ) {
Server_GetFile ( [ & ] ( std : : string_view response ) {
if ( response ! = " ERR " ) {
std : : ofstream file ( " save_file_path " _S + std : : format ( " save.{:04} " , saveFileID ) + " _online " ) ;
file < < response ;
file . close ( ) ;
LoadFile ( ) ;
} else {
std : : cout < < " WARNING! Could not load save file! " < < std : : endl ;
}
} ) ;
} else {
emscripten_idb_async_load ( " /assets " , ( " save_file_path " _S + std : : format ( " save.{:04} " , saveFileID ) ) . c_str ( ) , 0 , [ ] ( void * arg , void * data , int length ) {
std : : cout < < " Loaded Save File " < < saveFileID < < " successfully! " < < std : : endl ;
std : : string rawMetadata = ( char * ) data ;
std : : ofstream file ( " save_file_path " _S + std : : format ( " save.{:04} " , saveFileID ) ) ;
file < < response ;
for ( int i = 0 ; i < length ; i + + ) {
file < < rawMetadata [ i ] ;
}
file . close ( ) ;
LoadFile ( ) ;
} else {
std : : cout < < " WARNING! Could not load save file! " < < std : : endl ;
}
} ) ;
LoadGam e ( ) ;
} , [ ] ( void * arg ) {
std : : cout < < " Failed to load Save File " < < saveFileID < < " ! " < < std : : endl ;
} ) ;
}
# else
LoadFile ( ) ;
# endif
@ -248,11 +289,17 @@ const void SaveFile::UpdateSaveGameData(){
auto gameFilesList = Component < ScrollableWindowComponent > ( LOAD_GAME , " Game Files List " ) ;
gameFilesList - > RemoveAllComponents ( ) ;
const size_t saveFileCount = GetSaveFileCount ( ) ;
utils : : datafile metadata ;
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 " ) ;
}
utils : : datafile : : Read ( metadata , " save_file_path " _S + " metadata.dat " ) ;
if ( ! std : : filesystem : : exists ( " save_file_path " _S + " metadata.dat " + " _online " ) ) {
utils : : datafile : : Write ( online_metadata , " save_file_path " _S + " metadata.dat " + " _online " ) ;
}
std : : string saveGameFilename = " save_file_path " _S + " metadata.dat " ;
std : : string onlineSaveGameFilename = " save_file_path " _S + " metadata.dat " + " _online " ;
utils : : datafile : : Read ( metadata , saveGameFilename ) ;
utils : : datafile : : Read ( online_metadata , onlineSaveGameFilename ) ;
float offsetY = 0 ;
for ( size_t i = 0 ; i < saveFileCount ; i + + ) {
if ( metadata . HasProperty ( std : : format ( " save{} " , i ) ) ) {
@ -266,43 +313,61 @@ const void SaveFile::UpdateSaveGameData(){
}
}
} ;
auto LoadOnlineMetadataFile = [ ] ( ) {
auto LoadMetadataFromDB = [ ] ( ) {
auto gameFilesList = Component < ScrollableWindowComponent > ( LOAD_GAME , " Game Files List " ) ;
gameFilesList - > RemoveAllComponents ( ) ;
if ( SaveFile : : IsOnline ( ) ) {
const size_t saveFileCount = GetSaveFileCount ( ) ;
utils : : datafile metadata ;
if ( ! std : : filesystem : : exists ( " save_file_path " _S + " metadata.dat " ) ) {
utils : : datafile : : Write ( metadata , " save_file_path " _S + " metadata.dat " ) ;
}
utils : : datafile : : Read ( metadata , " save_file_path " _S + " metadata.dat " ) ;
float offsetY = 0 ;
for ( size_t i = 0 ; i < saveFileCount ; i + + ) {
if ( metadata . HasProperty ( std : : format ( " save{} " , i ) ) ) {
gameFilesList - > ADD ( std : : format ( " Load File Button - Save {} " , i ) , LoadFileButton ) ( geom2d : : rect < float > { { 0 , offsetY } , { gameFilesList - > GetSize ( ) . x - 13 , 48 } } , 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 ;
# ifdef __EMSCRIPTEN__
emscripten_idb_async_load ( " /assets " , ( " save_file_path " _S + " metadata.dat " ) . c_str ( ) , 0 , [ ] ( void * arg , void * data , int length ) {
std : : cout < < " Loaded metadata successfully! " < < std : : endl ;
auto gameFilesList = Component < ScrollableWindowComponent > ( LOAD_GAME , " Game Files List " ) ;
std : : string rawMetadata = ( char * ) data ;
std : : ofstream file ( " save_file_path " _S + " metadata.dat " ) ;
for ( int i = 0 ; i < length ; i + + ) {
file < < rawMetadata [ i ] ;
}
}
}
file . close ( ) ;
utils : : datafile metadata ;
utils : : datafile : : Read ( metadata , " save_file_path " _S + " metadata.dat " ) ;
const size_t saveFileCount = metadata . GetKeys ( ) . size ( ) ;
float offsetY = 0 ;
for ( size_t i = 0 ; i < saveFileCount ; i + + ) {
if ( metadata . HasProperty ( std : : format ( " save{} " , i ) ) ) {
gameFilesList - > ADD ( std : : format ( " Load File Button - Save {} " , i ) , LoadFileButton ) ( geom2d : : rect < float > { { 0 , offsetY } , { gameFilesList - > GetSize ( ) . x - 13 , 48 } } , 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 ;
}
}
Menu : : OpenMenu ( LOAD_GAME ) ;
} , [ ] ( void * arg ) {
std : : cout < < " Failed to load metadata! " < < std : : endl ;
} ) ;
# endif
} ;
LoadMetadataFile ( ) ;
# ifndef __EMSCRIPTEN__
LoadMetadataFile ( ) ;
# endif
# ifdef __EMSCRIPTEN__
Server_GetLoadInfo ( [ & ] ( std : : string_view response ) {
if ( response ! = " ERR " ) {
std : : ofstream file ( " save_file_path " _S + " metadata.dat " ) ;
std : : stringstream str ;
file < < response ;
file . close ( ) ;
LoadMetadataFile ( ) ;
}
} ) ;
if ( onlineMode ) {
Server_GetLoadInfo ( [ & ] ( std : : string_view response ) {
if ( response ! = " ERR " ) {
std : : ofstream file ( " save_file_path " _S + " metadata.dat " + " _online " ) ;
std : : stringstream str ;
file < < response ;
file . close ( ) ;
LoadMetadataFile ( ) ;
}
} ) ;
} else {
LoadMetadataFromDB ( ) ;
}
# endif
}