From 05a5c587f6c59aee26038cc4bdf657d9040340e3 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Wed, 28 Oct 2026 23:27:51 +0100 Subject: Relax Embedded Cart Loading Errors Relax loading errors when we are unable to decode an embedded PNG cart and just log a warning instead, continuing with the load. Some other minor fixes included as well. --- src/cart.c | 27 +++++++++++++++------------ src/config.c | 6 +++--- src/env.c | 26 +++++++++++++++++++------- src/fs.c | 39 +++++++++++++++++++++++++++++++++------ src/fs.h | 1 + 5 files changed, 71 insertions(+), 28 deletions(-) diff --git a/src/cart.c b/src/cart.c index baed5eb..f639aec 100644 --- a/src/cart.c +++ b/src/cart.c @@ -297,19 +297,16 @@ struct pw_cart *pw_cart_load(struct pw_cart_file *file, // While `LOAD("CART.P8")` works with the filesystem, `RELOAD("CART.P8")` // only works for embedded carts. In order to support all cases, we read // through our storage and add any valid - size_t count = 1; - for (size_t i = 0; i < storage->root.count; ++i) { + size_t max_count = 1; + for (size_t i = 0; + i < storage->root.count && max_count < PW_CART_EMBEDDED_MAX; ++i) { struct pw_fs_entry *e = storage->root.entries[i]; if (!embedded_cart(e, NULL)) { continue; } - if (count >= PW_CART_EMBEDDED_MAX) { - SDL_LogWarn(0, "ignoring cart past embedding limit %s", e->path); - continue; - } - ++count; + ++max_count; } - cart = SDL_calloc(1, sizeof(*cart) + count * sizeof(*cart->embedded)); + cart = SDL_calloc(1, sizeof(*cart) + max_count * sizeof(*cart->embedded)); if (!cart) { goto error; } @@ -321,7 +318,8 @@ struct pw_cart *pw_cart_load(struct pw_cart_file *file, goto error; } - for (size_t i = 0, j = 1; i < storage->root.count && j < count; ++i) { + size_t count = 1; + for (size_t i = 0; i < storage->root.count; ++i) { struct pw_fs_entry *e = storage->root.entries[i]; char const *fn; @@ -330,15 +328,20 @@ struct pw_cart *pw_cart_load(struct pw_cart_file *file, continue; } - struct pw_cart_data *d = cart->embedded + j; + if (count >= max_count) { + SDL_LogWarn(0, "ignoring cart past embedding limit: %s", fn); + continue; + } + + struct pw_cart_data *d = cart->embedded + count; if (!pw_stripext(d->name, PW_CART_NAME_MAX, fn, ".png") || !decode_cart_png(b->data, b->size, NULL, d->rom)) { SDL_LogWarn(0, "malformed embedded cart %s", e->path); - goto error; + continue; } SDL_LogDebug(0, "embedding cart %s", d->name); - ++j; + ++count; } cart->count = count; diff --git a/src/config.c b/src/config.c index 944101b..1981a4f 100644 --- a/src/config.c +++ b/src/config.c @@ -1,6 +1,3 @@ -#include -#include - // We don't have `getopt` on all platforms (notably, it is not available on // Windows), so detect whether or not it is there and avoid command line // parsing if it is not available. @@ -9,6 +6,9 @@ #include #endif +#include +#include + #include #include "config.h" diff --git a/src/env.c b/src/env.c index f60d43f..e64b3fb 100644 --- a/src/env.c +++ b/src/env.c @@ -802,9 +802,9 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env, case 33: return (uint32_t)pw_input_get_pointer_lock(env->context.input); case 34: - // Whether or not `pico8_mouse` is set - we always keep track of the mouse - // so this is always true. - return 1; + // Whether or not `pico8_mouse` is set - since we use SDL_GetMouseState to + // provide mouse updates to PICO-8, keep this as "false". + return 0; case 35: { struct pw_coordinates pos; pw_input_mouse(env->context.input, &pos); @@ -848,10 +848,22 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env, // do this as our SDL application handles them already; no-op. return 0; case 45: - // The touch PICO-8 button state, note this only ever considers left mouse - // button clicks, so mask appropriately. - return (uint32_t)(pw_input_mouse(env->context.input, NULL) & - PW_MOUSE_BUTTON_LEFT); + // When querying the PICO-8 mouse state this way, it expects PICO-8's + // internal mouse ordering, and not SDL's, so remap it. + { + uint32_t result = 0; + enum pw_mouse_button b = pw_input_mouse(env->context.input, NULL); + if (b & PW_MOUSE_BUTTON_LEFT) { + result |= 1; + } + if (b & PW_MOUSE_BUTTON_RIGHT) { + result |= 2; + } + if (b & PW_MOUSE_BUTTON_MIDDLE) { + result |= 4; + } + return result; + } case 46: case 48: // Machinery for creating directories, we have implicit directories in our diff --git a/src/fs.c b/src/fs.c index 4a1f133..3b450e8 100644 --- a/src/fs.c +++ b/src/fs.c @@ -180,6 +180,11 @@ struct pw_fs *pw_fs_init(char const *path) { free_entry(fs->root.entries[i]); } fs->root.count = 1; + + // In case the Tar is corrupt, do not clobber it with an empty tar + // and fallback to in-memory cart storage. + SDL_free(fs->path); + fs->path = NULL; } pw_tar_free(tar); @@ -187,11 +192,10 @@ struct pw_fs *pw_fs_init(char const *path) { fs->dirty = false; } else if (path && SDL_GetPathInfo(path, NULL)) { - SDL_LogWarn(0, "corrupt cart storage: %s", SDL_GetError()); + SDL_LogWarn(0, "cannot access cart storage: %s", SDL_GetError()); - // In case of a corrupt cart storage, do not clobber it (in case it is - // a file intended for something else, and instead fallback to in-memory - // storage; just to be sure. + // In case the Tar file exists but we cannot access it, fall back to + // in-memory cart storage and prevent writing over the file. SDL_free(fs->path); fs->path = NULL; @@ -268,6 +272,14 @@ bool pw_fs_sync(struct pw_fs *fs) { return true; } + // Currently, we re-write the entire archive every time we sync it + // to the file-system. This is a bit wasteful (and can wear storage + // devices). That being said, the "dirty" flag paired with the fact + // that PICO-8 does not seem to write often to disk and this tar is + // typically quite small, makes this acceptable for now. Log that we + // are writing to disk to keep an eye on how often it happens. + SDL_LogDebug(0, "writing cart storage to disk %s", fs->path); + bool ok = false; char *tmp = NULL; struct pw_tar *tar = NULL; @@ -400,7 +412,10 @@ static struct pw_fs_entry *create_entry(struct pw_fs *fs, char const *path) { } sort_entries(&fs->root); + // We added a new file, mark the file-system as dirty to ensure a new Tar + // entry gets created for our new file. fs->dirty = true; + return e; error: @@ -426,6 +441,10 @@ bool pw_fs_open(struct pw_fs *fs, char const *path, enum pw_fs_open_flag flags, fs->err = PW_EINVAL; return SDL_InvalidParamError("fd"); } + if (flags & PW_O_DIRECTORY) { + fs->err = PW_ENOTDIR; + return SDL_InvalidParamError("flags"); + } int32_t f = 0; for (; f < PW_FS_STREAM_MAX && fs->streams[f].node; ++f) { @@ -486,7 +505,10 @@ bool pw_fs_open(struct pw_fs *fs, char const *path, enum pw_fs_open_flag flags, s->node = &e->node; s->pos = 0; if (flags & PW_O_TRUNC) { - fs->dirty = true; + // Make sure that our truncation persists to disk for files. + if (e->node.type == PW_FS_NODE_BUFFER) { + fs->dirty = true; + } e->node.w.size = 0; } @@ -817,7 +839,12 @@ uint8_t *pw_fs_write(struct pw_fs *fs, pw_fd fd, uint32_t len) { w->size = SDL_max(w->size, end); s->pos = end; - fs->dirty = true; + // In case we are writting to a file's buffer, and not just to stdout + // or stderr, make the file system as dirty. + if (w->type == PW_FS_NODE_BUFFER) { + fs->dirty = true; + } + return buffer; } diff --git a/src/fs.h b/src/fs.h index af80474..7a79bda 100644 --- a/src/fs.h +++ b/src/fs.h @@ -85,6 +85,7 @@ enum pw_fs_open_flag { PW_O_EXCL = 0200, PW_O_TRUNC = 01000, PW_O_APPEND = 02000, + PW_O_DIRECTORY = 0200000, }; /** -- cgit v1.2.3