From 7cb0ac96c9798936b86d40613bb46cd9a29a4896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Mon, 21 Aug 2023 20:32:16 +0200 Subject: [PATCH] Emscripten: Enable modularity This allows multiple instances of the game to be loaded in a single page. Also stop emitting htmls as a build artifact and require presence of #PowderSessionInfo. --- .github/build.sh | 2 +- meson.build | 4 +++- src/client/AuthUserEmscripten.cpp | 22 +++++++++---------- src/client/http/requestmanager/Emscripten.cpp | 3 --- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/.github/build.sh b/.github/build.sh index 3f8f2ec90..b6ccdece8 100755 --- a/.github/build.sh +++ b/.github/build.sh @@ -449,7 +449,7 @@ if [[ $PACKAGE_MODE == dmg ]]; then cp ../README.md dmgroot/README.md hdiutil create -format UDZO -volname $APP_NAME -fs HFS+ -srcfolder dmgroot -o $ASSET_PATH elif [[ $PACKAGE_MODE == emscripten ]]; then - tar cvf $ASSET_PATH $APP_EXE.html $APP_EXE.js $APP_EXE.worker.js $APP_EXE.wasm + tar cvf $ASSET_PATH $APP_EXE.js $APP_EXE.worker.js $APP_EXE.wasm elif [[ $PACKAGE_MODE == appimage ]]; then # so far this can only happen with $BSH_HOST_PLATFORM-$BSH_HOST_LIBC == linux-gnu, but this may change later case $BSH_HOST_ARCH in diff --git a/meson.build b/meson.build index c2443be45..4bacc75ee 100644 --- a/meson.build +++ b/meson.build @@ -212,6 +212,8 @@ if host_platform == 'emscripten' '-s', 'EXIT_RUNTIME=0', '-s', 'EXPORTED_RUNTIME_METHODS=ccall,cwrap', '-s', 'FS_DEBUG', + '-s', 'MODULARIZE', + '-s', 'EXPORT_NAME=create_' + app_exe, '-lidbfs.js', ] emcc_args = [ @@ -371,7 +373,7 @@ font_files += data_files if host_platform == 'emscripten' project_link_args += [ - '-o', app_exe + '.html', # so we get a .wasm, a .js, and a .html + '-o', app_exe + '.js', # so we get a .wasm, and a .js ] endif diff --git a/src/client/AuthUserEmscripten.cpp b/src/client/AuthUserEmscripten.cpp index 2fcff3277..40006cd5c 100644 --- a/src/client/AuthUserEmscripten.cpp +++ b/src/client/AuthUserEmscripten.cpp @@ -1,33 +1,33 @@ #include "Client.h" #include "prefs/GlobalPrefs.h" #include +#include void Client::LoadAuthUser() { - std::optional newUsername; + ByteString newUsername, newSessionKey; if (EM_ASM_INT({ - return document.querySelector("#PowderSessionInfo [name='Username']") ? 1 : 0; + return (document.querySelector("#PowderSessionInfo [name='Username']") && + document.querySelector("#PowderSessionInfo [name='SessionKey']")) ? 1 : 0; })) { newUsername = ByteString(std::unique_ptr((char *)EM_ASM_PTR({ return stringToNewUTF8(document.querySelector("#PowderSessionInfo [name='Username']").value); }), free).get()); - } - std::optional newSessionKey; - if (EM_ASM_INT({ - return document.querySelector("#PowderSessionInfo [name='SessionKey']") ? 1 : 0; - })) - { newSessionKey = ByteString(std::unique_ptr((char *)EM_ASM_PTR({ return stringToNewUTF8(document.querySelector("#PowderSessionInfo [name='SessionKey']").value); }), free).get()); } - if (newUsername && newSessionKey) + else + { + std::cerr << "required #PowderSessionInfo elements not found, can't authenticate" << std::endl; + } + if (newUsername.size() && newSessionKey.size()) { authUser.UserID = -1; // Not quite valid but evaluates to true and that's all that matters for this codebase. - authUser.Username = *newUsername; + authUser.Username = newUsername; authUser.SessionID = "(invalid)"; - authUser.SessionKey = *newSessionKey; + authUser.SessionKey = newSessionKey; authUser.UserElevation = User::ElevationNone; // We don't deal with this in the browser. } else diff --git a/src/client/http/requestmanager/Emscripten.cpp b/src/client/http/requestmanager/Emscripten.cpp index 0ff7017ea..7c87b1e51 100644 --- a/src/client/http/requestmanager/Emscripten.cpp +++ b/src/client/http/requestmanager/Emscripten.cpp @@ -232,9 +232,6 @@ namespace http Module.emscriptenRequestManager.requests[$0].fetchMethod = UTF8ToString($1); }, handle->id, requestHandle->verb->c_str()); } - // TODO: set max redirects - // TODO: set max concurrent streams - // TODO: set connect timeout EM_ASM({ let request = Module.emscriptenRequestManager.requests[$0]; let token = $1;