diff --git a/emscripten/preamble.js b/emscripten/preamble.js index da24e4c9..5e7c7ee0 100644 --- a/emscripten/preamble.js +++ b/emscripten/preamble.js @@ -23,12 +23,16 @@ glContext.getSupportedExtensions().forEach(function(ext) { function SyncFS(is_load, ccptr) { FS.syncfs(is_load, function(err) { - Module['ccall']( - 'vfs_sync_callback', - null, ["boolean", "string", "number"], - [is_load, err, ccptr], - { async: true } - ); + // wasmga.me: without IndexedDB this calls back at once, while the + // game is still on the stack; go back to the game from a fresh stack. + setTimeout(function() { + Module['ccall']( + 'vfs_sync_callback', + null, ["boolean", "string", "number"], + [is_load, err, ccptr], + { async: true } + ); + }, 0); }); } @@ -39,3 +43,16 @@ if(typeof dynCall === 'undefined') { }; var debug_tables; // closure may fail on debug builds without this + +// wasmga.me: the play page reaches the file system through these (closure +// renames FS's own methods). It writes the game files to /res-cache and keeps +// the player's files in /persistent/storage itself, so IndexedDB isn't used. +Module['wgFS'] = { + 'mkdirTree': function(path) { FS.mkdirTree(path); }, + 'writeFile': function(path, data) { FS.writeFile(path, data, { canOwn: true }); }, + 'readFile': function(path) { return FS.readFile(path); }, + 'readdir': function(path) { return FS.readdir(path); }, + 'exists': function(path) { return FS.analyzePath(path).exists; }, + 'isDir': function(path) { return FS.isDir(FS.stat(path).mode); }, + 'mtime': function(path) { return FS.stat(path).mtime.getTime(); }, +}; diff --git a/src/vfs/emscripten_fetch.c b/src/vfs/emscripten_fetch.c index 1b88da11..41ba20a0 100644 --- a/src/vfs/emscripten_fetch.c +++ b/src/vfs/emscripten_fetch.c @@ -19,7 +19,9 @@ #include #include -#define CACHE_PATH "/persistent/res-cache" +// wasmga.me: the play page writes game files here (in memory) as its data +// zips arrive, so nothing is downloaded file by file or kept in IndexedDB. +#define CACHE_PATH "/res-cache" #define MAX_PREFETCH_REQUESTS 4 #define MAKE_CACHED_PATH(_var_name, _content_id) \ @@ -48,6 +50,7 @@ EM_JS(void, update_dl_status, (int done, int total), { }); static void prefetch_next(FetchFSContext *ctx) { + return; // wasmga.me: the play page downloads the data while(ctx->requests.num_elements_occupied < MAX_PREFETCH_REQUESTS) { update_dl_status(ctx->prefetch_iter, resindex_num_file_entries()); @@ -145,6 +148,12 @@ static SDL_IOStream *vfs_fetch_open(VFSResIndexFSContext *resindex_ctx, const ch return cached_rw; } + // wasmga.me: wait for the play page to write the file + while(!(cached_rw = SDL_IOFromFile(cached_path, "r"))) { + emscripten_sleep(10); + } + return cached_rw; + for(;;) { fetch = fetch_begin_request(ctx, content_id); fetch_wait(fetch);