From: Holger Schemel Date: Mon, 15 Feb 2021 23:20:27 +0000 (+0100) Subject: added persistent personal game data for Emscripten platform X-Git-Tag: 4.2.3.0~12 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=ba6af23b5637bcd5fa4e247dc336b45ba1e6fd1a;p=rocksndiamonds.git added persistent personal game data for Emscripten platform --- diff --git a/src/Makefile b/src/Makefile index e525f1f0..39029a0c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -97,7 +97,7 @@ ifeq ($(PLATFORM),emscripten) SDL_LIBS = -s USE_SDL_IMAGE=2 -s USE_SDL_MIXER=2 -s USE_SDL_NET=2 -s USE_ZLIB=1 SDL_FMTS = -s SDL2_IMAGE_FORMATS='["bmp","png","pcx","xpm"]' EXTRA_CFLAGS = $(SDL_LIBS) -EXTRA_LDFLAGS = $(SDL_FMTS) -s INITIAL_MEMORY=81920000 -s ALLOW_MEMORY_GROWTH=1 --preload-file ../graphics/ --preload-file ../sounds/ --preload-file ../levels/ --preload-file ../music/ -s NO_EXIT_RUNTIME=0 -s ASYNCIFY -O2 +EXTRA_LDFLAGS = $(SDL_FMTS) -s INITIAL_MEMORY=81920000 -s ALLOW_MEMORY_GROWTH=1 --preload-file ../graphics/ --preload-file ../sounds/ --preload-file ../levels/ --preload-file ../music/ -s NO_EXIT_RUNTIME=0 -s ASYNCIFY -O2 -lidbfs.js else SDL_LIBS = -lSDL2_image -lSDL2_mixer -lSDL2_net endif diff --git a/src/libgame/setup.c b/src/libgame/setup.c index a135b0d5..dc470a81 100644 --- a/src/libgame/setup.c +++ b/src/libgame/setup.c @@ -1511,6 +1511,8 @@ char *getHomeDir(void) if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, dir))) strcpy(dir, "."); } +#elif defined(PLATFORM_EMSCRIPTEN) + dir = "/persistent"; #elif defined(PLATFORM_UNIX) if (dir == NULL) { diff --git a/src/libgame/system.c b/src/libgame/system.c index e852d2ed..a2f7ebec 100644 --- a/src/libgame/system.c +++ b/src/libgame/system.c @@ -104,6 +104,25 @@ void InitProgramInfo(char *argv0, char *config_filename, char *userdata_subdir, program.log_file[LOG_ERR_ID] = program.log_file_default[LOG_ERR_ID] = stderr; program.headless = FALSE; + +#if defined(PLATFORM_EMSCRIPTEN) + EM_ASM + ( + Module.sync_done = 0; + + FS.mkdir('/persistent'); // create persistent data directory + FS.mount(IDBFS, {}, '/persistent'); // mount with IDBFS filesystem type + FS.syncfs(true, function(err) // sync persistent data into memory + { + assert(!err); + Module.sync_done = 1; + }); + ); + + // wait for persistent data to be synchronized to memory + while (emscripten_run_script_int("Module.sync_done") == 0) + Delay(20); +#endif } void InitNetworkInfo(boolean enabled, boolean connected, boolean serveronly, diff --git a/src/libgame/system.h b/src/libgame/system.h index 9b3dfe75..8ebd43dc 100644 --- a/src/libgame/system.h +++ b/src/libgame/system.h @@ -22,6 +22,8 @@ #include "windows.h" #elif defined(PLATFORM_ANDROID) #include "android.h" +#elif defined(PLATFORM_EMSCRIPTEN) +#include "emscripten.h" #endif #include "sdl.h" diff --git a/src/screens.c b/src/screens.c index 81a54b06..0ce46341 100644 --- a/src/screens.c +++ b/src/screens.c @@ -1739,6 +1739,16 @@ void DrawMainMenu(void) SetMouseCursor(CURSOR_DEFAULT); OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2); + +#if defined(PLATFORM_EMSCRIPTEN) + EM_ASM + ( + FS.syncfs(function(err) + { + assert(!err); + }); + ); +#endif } static void gotoTopLevelDir(void)