Commit MetaInfo

Revisiona42222b26ab5019cbd385aa7d756987049cbd419 (tree)
Time2020-01-23 09:01:44
AuthorDavid Ludwig <dludwig@pobo...>
CommiterDavid Ludwig

Log Message

persist save-data across page reloads

Change Summary

Incremental Difference

diff -r 91b56854e368 -r a42222b26ab5 Makefile
--- a/Makefile Fri Jan 17 20:20:21 2020 -0500
+++ b/Makefile Wed Jan 22 19:01:44 2020 -0500
@@ -84,10 +84,13 @@
8484 LDLIBS :=
8585
8686 ifeq ($(PLATFORM), EMSCRIPTEN)
87- EMSCRIPTEN_CFLAGS := -s ALLOW_MEMORY_GROWTH=1 -s ASYNCIFY
87+ EMSCRIPTEN_CFLAGS := -s ALLOW_MEMORY_GROWTH=1 -s ASYNCIFY -s "EXPORTED_FUNCTIONS=['_main','_emscripten_main']" -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall','callMain']"
8888 CPPFLAGS += $(EMSCRIPTEN_CFLAGS)
8989 CFLAGS += $(EMSCRIPTEN_CFLAGS)
90- LDFLAGS += --preload-file tyrian21/
90+ LDFLAGS += --preload-file tyrian21/ -lidbfs.js
91+ CSTD := gnu99 # GNU C extensions are needed for use of Emscripten's macro, EM_ASM
92+else
93+ CSTD := iso9899:1999
9194 endif
9295
9396 SDL_CPPFLAGS := $(shell $(PKG_CONFIG) sdl2 --cflags)
@@ -102,7 +105,7 @@
102105 $(EXTRA_CPPFLAGS) \
103106 $(SDL_CPPFLAGS) \
104107 $(CPPFLAGS)
105-ALL_CFLAGS = -std=iso9899:1999 \
108+ALL_CFLAGS = -std=$(CSTD) \
106109 $(CFLAGS)
107110 ALL_LDFLAGS = $(SDL_LDFLAGS) \
108111 $(LDFLAGS)
diff -r 91b56854e368 -r a42222b26ab5 opentyrian.html
--- a/opentyrian.html Fri Jan 17 20:20:21 2020 -0500
+++ b/opentyrian.html Wed Jan 22 19:01:44 2020 -0500
@@ -122,6 +122,15 @@
122122 console.error("Cannot execute MainCode, which has not been fully-loaded")
123123 return;
124124 }
125+
126+ // We will invoke main() ourselves (via emscripten_main(),
127+ // a function of ours and not Emscripten's), after an
128+ // asynchronous load of save-data.
129+ Module.noInitialRun = true;
130+ Module.postRun = function () { // This gets called regardless of noInitialRun=true
131+ Module.ccall('emscripten_main');
132+ }
133+
125134 const execMainCode = new Function('Module', MainCode);
126135 execMainCode(Module);
127136 }
diff -r 91b56854e368 -r a42222b26ab5 src/config.c
--- a/src/config.c Fri Jan 17 20:20:21 2020 -0500
+++ b/src/config.c Wed Jan 22 19:01:44 2020 -0500
@@ -292,7 +292,7 @@
292292
293293 config_write(config, file);
294294
295-#if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
295+#if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE || __EMSCRIPTEN__
296296 fsync(fileno(file));
297297 #endif
298298 fclose(file);
@@ -700,7 +700,9 @@
700700
701701 if (strlen(user_dir) == 0)
702702 {
703-#ifndef TARGET_WIN32
703+#if defined(__EMSCRIPTEN__)
704+ strcpy(user_dir, "/persist");
705+#elif !defined(TARGET_WIN32)
704706 char *xdg_config_home = getenv("XDG_CONFIG_HOME");
705707 if (xdg_config_home != NULL)
706708 {
@@ -979,7 +981,7 @@
979981 {
980982 efwrite(saveTemp, 1, sizeof(saveTemp), f);
981983
982-#if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
984+#if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE || __EMSCRIPTEN__
983985 fsync(fileno(f));
984986 #endif
985987 fclose(f);
@@ -1011,7 +1013,7 @@
10111013
10121014 efwrite(keySettings, sizeof(*keySettings), COUNTOF(keySettings), f);
10131015
1014-#if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
1016+#if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE || __EMSCRIPTEN__
10151017 fsync(fileno(f));
10161018 #endif
10171019 fclose(f);
diff -r 91b56854e368 -r a42222b26ab5 src/opentyr.c
--- a/src/opentyr.c Fri Jan 17 20:20:21 2020 -0500
+++ b/src/opentyr.c Wed Jan 22 19:01:44 2020 -0500
@@ -54,6 +54,10 @@
5454 #include <string.h>
5555 #include <time.h>
5656
57+#if __EMSCRIPTEN__
58+#include <emscripten.h>
59+#endif
60+
5761 const char *opentyrian_str = "OpenTyrian",
5862 *opentyrian_version = HG_REV;
5963
@@ -315,6 +319,26 @@
315319 } while (!quit);
316320 }
317321
322+#if __EMSCRIPTEN__
323+void emscripten_main()
324+{
325+ // Before running main(), make sure any previously saved data gets
326+ // brought into Emscripten's virtual file-system.
327+ EM_ASM((
328+ FS.mkdir('/persist');
329+ FS.mount(IDBFS, {}, '/persist');
330+ const populate = true;
331+ FS.syncfs(populate, function (err) {
332+ // TODO: handle err, if present (in the initial syncfs' callback)
333+ if (err) {
334+ console.error('initial syncfs failed', err);
335+ }
336+ Module.callMain(); // This will invoke main().
337+ });
338+ ));
339+}
340+#endif
341+
318342 int main( int argc, char *argv[] )
319343 {
320344 mt_srand(time(NULL));
Show on old repository browser