Skip to content

Commit 87b0d8b

Browse files
committed
fix: web, use IDBFS to persist files correctly
this makes recordings and similar things persistent across reloads
1 parent 6a9a4c0 commit 87b0d8b

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

platforms/build-web.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ fi
3838
# shellcheck disable=SC1091
3939
EMSDK_QUIET=1 source "$EMSCRIPTEN_ROOT/emsdk_env.sh" >/dev/null
4040

41+
PTHREAD_POOL_SIZE="8"
42+
4143
## build the needed dependencies
4244
embuilder build sdl2-mt harfbuzz-mt freetype zlib sdl2_ttf mpg123 "sdl2_mixer:formats=mp3" libpng-mt "sdl2_image:formats=png,svg:mt=1" icu-mt
4345

@@ -133,7 +135,7 @@ if [ "$COMPILE_TYPE" == "complete_rebuild" ] || ! [ -e "$BUILD_FFMPEG_FILE" ]; t
133135

134136
FFMPEG_COMMON_FLAGS="-pthread -sUSE_PTHREADS=1"
135137

136-
FFMPEG_LINK_FLAGS="$COMMON_FLAGS -sWASM=1 -sALLOW_MEMORY_GROWTH=1 -sASSERTIONS=1 -sERROR_ON_UNDEFINED_SYMBOLS=1"
138+
FFMPEG_LINK_FLAGS="$COMMON_FLAGS -sWASM=1 -sALLOW_MEMORY_GROWTH=1 -sASSERTIONS=1 -sERROR_ON_UNDEFINED_SYMBOLS=1 -sPTHREAD_POOL_SIZE=$PTHREAD_POOL_SIZE"
137139

138140
##TODO: add --disable-debug, in release mode
139141

@@ -205,7 +207,7 @@ export PACKAGE_FLAGS="'--use-port=sdl2', '--use-port=harfbuzz', '--use-port=free
205207
export COMMON_FLAGS="'-fexceptions', '-pthread', '-sUSE_PTHREADS=1', '-sEXCEPTION_CATCHING_ALLOWED=[..]', $PACKAGE_FLAGS"
206208

207209
# TODO see if ALLOW_MEMORY_GROWTH is needed, but if we load ttf's and music it likely is and we don't have to debug OOm crashes, that aren't handled by some third party library, which is painful
208-
export LINK_FLAGS="$COMMON_FLAGS, '-sEXPORT_ALL=1', '-sUSE_WEBGPU=1', '-sWASM=1', '-sALLOW_MEMORY_GROWTH=1', '-sASSERTIONS=1','-sERROR_ON_UNDEFINED_SYMBOLS=1', '-sFETCH=1', '-sEXIT_RUNTIME=1'"
210+
export LINK_FLAGS="$COMMON_FLAGS, '-sEXPORT_ALL=1', '-sUSE_WEBGPU=1', '-sWASM=1', '-sALLOW_MEMORY_GROWTH=1', '-sASSERTIONS=1','-sERROR_ON_UNDEFINED_SYMBOLS=1', '-sFETCH=1', '-sEXIT_RUNTIME=1', '-sPTHREAD_POOL_SIZE=$PTHREAD_POOL_SIZE','-lidbfs.js'"
209211
export COMPILE_FLAGS="$COMMON_FLAGS ,'-DAUDIO_PREFER_MP3'"
210212

211213
export CROSS_FILE="./platforms/crossbuild-web.ini"

src/executables/game/main.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,19 @@ namespace {
4848
#endif
4949

5050

51-
#if !(defined(__EMSCRIPTEN__))
51+
#if defined(__EMSCRIPTEN__)
52+
53+
// See: https://emscripten.org/docs/api_reference/Filesystem-API.html#filesystem-api-idbfs
54+
EM_ASM(FS.mkdir('/persistent'); FS.mount(IDBFS, { autoPersist: true }, '/persistent'); FS.syncfs(
55+
true,
56+
function(err) {
57+
if (err) {
58+
console.error(err);
59+
}
60+
}
61+
););
62+
63+
#endif
5264

5365
const auto logs_path = utils::get_root_folder() / "logs";
5466

@@ -64,7 +76,7 @@ namespace {
6476
fmt::format("{}/oopetris.log", logs_path.string()), 1024 * 1024 * 10, 5, true
6577
));
6678
}
67-
#endif
79+
6880

6981
auto combined_logger = std::make_shared<spdlog::logger>("combined_logger", begin(sinks), end(sinks));
7082
spdlog::set_default_logger(combined_logger);

src/helper/graphic_utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ std::vector<std::string> utils::supported_features() {
4848
}
4949
return std::filesystem::path{ std::string{ pref_path } };
5050
#elif defined(__EMSCRIPTEN__)
51-
return std::filesystem::path{ "/" };
51+
return std::filesystem::path{ "/persistent/" };
5252
#elif defined(__CONSOLE__)
5353
// this is in the sdcard of the switch / 3ds , since internal storage is read-only for applications!
5454
return std::filesystem::path{ "." };

0 commit comments

Comments
 (0)