handle filesystem sync in a better way
authorTaylor Richards <taylor@icculus.org>
Sat, 24 Feb 2018 13:10:33 +0000 (08:10 -0500)
committerTaylor Richards <taylor@icculus.org>
Sat, 17 Mar 2018 15:31:15 +0000 (11:31 -0400)
src/cfile/cfile.cpp
src/freespace2/freespace.cpp

index 1280b64..bca6b7e 100644 (file)
@@ -1499,6 +1499,7 @@ int cfile_init_paths()
                return 0;
        }
 
+#ifndef __EMSCRIPTEN__
        char *t_path = SDL_GetBasePath();
 
        // make sure we have something
@@ -1519,7 +1520,6 @@ int cfile_init_paths()
        SDL_free(t_path);
        t_path = NULL;
 
-#ifndef __EMSCRIPTEN__
        // are we in a root directory?
        if ( cfile_in_root_dir(Cfile_root_dir) ) {
                SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Freespace2/Fred2 cannot be run from a drive root directory!", NULL);
@@ -1547,24 +1547,37 @@ int cfile_init_paths()
        SDL_free(u_path);
        u_path = NULL;
 #else
-       SDL_snprintf(Cfile_user_dir, SDL_arraysize(Cfile_user_dir), "/%s/", Osreg_app_name);
+       const char *root_path = "/Game";
+       const char *user_path = "/User";
+
+       SDL_snprintf(Cfile_root_dir, SDL_arraysize(Cfile_root_dir), "%s/", root_path);
+       SDL_snprintf(Cfile_user_dir, SDL_arraysize(Cfile_user_dir), "%s/", user_path);
 
        EM_ASM({
-               const base_path = '/' + UTF8ToString($0);
-               FS.mkdir(base_path);
-               FS.mount(IDBFS, {}, base_path);
+               const user_path = UTF8ToString($1);
 
-               Module.syncdone = 0;
+               FS.mkdir(user_path);
+               FS.mount(IDBFS, { name: UTF8ToString($0) }, user_path);
+
+               Module.sync_in_progress = 1;
+
+               if (Module['setStatus']) {
+                       Module['setStatus']('Syncing user data...');
+               }
 
                FS.syncfs(true, function(err) {
                        if (err && err.code !== 'EEXIST') {
                                console.log('FS.syncfs() load error: ' + err);
                                assert(err);
-                       }
+                       } else {
+                               Module.sync_in_progress = 0;
 
-                       Module.syncdone = 1;
+                               // remove initial loading/progress screen
+                               var progress = document.getElementById('progress');
+                               progress.hidden = true;
+                       }
                });
-       }, Osreg_app_name);
+       }, Osreg_app_name, user_path);
 #endif
 
        // see if CF_TYPE_DATA exists for user and if not populate user path
index b5f6f83..1415923 100644 (file)
@@ -5329,14 +5329,6 @@ 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                  
@@ -6747,6 +6739,11 @@ static bool game_loop()
 extern "C"
 void game_loop_caller()
 {
+       // keep looping through until the persistent storage has sync'd
+       if (emscripten_run_script_int("Module.sync_in_progress") == 1) {
+               return;
+       }
+
        if ( !game_loop() ) {
                game_shutdown();
                emscripten_cancel_main_loop();
@@ -6861,11 +6858,6 @@ int game_main(const char *szCmdLine)
        }
 
 #ifdef __EMSCRIPTEN__
-       EM_ASM({
-               var progress = document.getElementById('progress');
-               progress.hidden = true;
-       });
-
        emscripten_set_main_loop(game_loop_caller, 0, 1);
 #else
        while ( game_loop() ) { /* nothing */ }