From bea7af4e8ac2b74f9590ac9fede8a25cf6758fb5 Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Fri, 8 Dec 2017 01:14:58 -0500 Subject: [PATCH] support persistent file storage for Emscripten --- src/cfile/cfile.cpp | 24 ++++++++++++++++++++++-- src/freespace2/freespace.cpp | 17 +++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/cfile/cfile.cpp b/src/cfile/cfile.cpp index 4505ab3..f14d3d7 100644 --- a/src/cfile/cfile.cpp +++ b/src/cfile/cfile.cpp @@ -224,6 +224,10 @@ #include #include +#ifdef __EMSCRIPTEN__ +#include +#endif + #include "pstypes.h" #include "cfile.h" #include "encrypt.h" @@ -1547,8 +1551,24 @@ int cfile_init_paths() SDL_free(u_path); u_path = NULL; #else - SDL_snprintf(Cfile_user_dir, SDL_arraysize(Cfile_user_dir), "/persist/"); - mkdir("/.prefs", 0777); + SDL_snprintf(Cfile_user_dir, SDL_arraysize(Cfile_user_dir), "/%s/%s/", Osreg_company_name, Osreg_app_name); + + EM_ASM({ + const base_path = UTF8ToString($0); + FS.mkdir(base_path); + FS.mount(IDBFS, {}, base_path); + + Module.syncdone = 0; + + FS.syncfs(true, function(err) { + if (err && err.code !== 'EEXIST') { + console.log('FS.syncfs() load error: ' + err); + assert(err); + } + + Module.syncdone = 1; + }); + }, Osreg_company_name); #endif // see if CF_TYPE_DATA exists for user and if not populate user path diff --git a/src/freespace2/freespace.cpp b/src/freespace2/freespace.cpp index 79b9342..bded2b0 100644 --- a/src/freespace2/freespace.cpp +++ b/src/freespace2/freespace.cpp @@ -5295,6 +5295,14 @@ void game_process_event( int current_state, int event ) break; case GS_EVENT_GAME_INIT: +#ifdef __EMSCRIPTEN__ + // keep looping through until the persistent storage has sync'd + if (emscripten_run_script_int("Module.syncdone") == 0) { + // this event got popped, so add it back into the queue + gameseq_post_event(GS_EVENT_GAME_INIT); + break; + } +#endif #if defined(FS2_DEMO) || defined(OEM_BUILD) || defined(FS1_DEMO) gameseq_set_state(GS_STATE_INITIAL_PLAYER_SELECT); #else @@ -6885,6 +6893,15 @@ void game_shutdown(void) psnet_close(); os_cleanup(); +#ifdef __EMSCRIPTEN__ + // sync files to persistent storage + EM_ASM( + FS.syncfs(function(err) { + assert(!err); + }); + ); +#endif + // HACKITY HACK HACK // if this flag is set, we should be firing up the launcher when exiting freespace extern int Multi_update_fireup_launcher_on_exit; -- 2.39.2