diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-29 00:45:24 +0100 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-29 00:45:24 +0100 |
| commit | ec6ed6404cdc29fc204eb93fdeb5025deb42f49d (patch) | |
| tree | 65dc21b389bc716163d9c0ba9a3e5f88ce883d69 /src/env.c | |
| parent | 05a5c587f6c59aee26038cc4bdf657d9040340e3 (diff) | |
Support the Shutdown Option
It looks like the `SHUTDOWN` menu option shows up when the `-run`
CLI argument is passed, so pass it. There is a catch though, in
that PICO-8 does not properly report shutdown requests in those
cases, so read Wasm memory directly to detect the shutdown request.
Diffstat (limited to 'src/env.c')
| -rw-r--r-- | src/env.c | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -116,6 +116,33 @@ bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context) { return true; } +bool pw_env_shutdown(struct w2c_env *env) { + if (!env) { + return SDL_InvalidParamError("env"); + } + + if (env->data.state.shutdown) { + return true; + } + + // HERE BE DRAGONS... Taking a look at the Web Assembly module exported + // by PICO-8, it calls `env_emscripten_asm_iiiiiiiii(10, ...)` with the + // `request_shutdown` flag read from global memory offset `2220668`. For + // whatever reason, the `SHUTDOWN` menu flag is setting this location in + // memory to a non-zero value, but without calling the ASM const. In order + // to correctly support shutting down, we need to not only read the flag + // from the ASM const, but also this memory offset. This is tied to the + // PICO-8 version, so whenever it gets updated, just: + // + // grep -B2 'asm_const_iiiiiiiii' build/system/pico8.c + // + // And check the address that is being loaded for the last parameter to the + // state update ASM const call. + + uint32_t value; + return wasm_rt_memory_load32_checked(&env->memory, 2220668, &value) && value; +} + /** * Convenience macro for writing to wasm memory and moving the wasm pointer * forward for that amount. |
