From 8dda4698251ac7c724005a158d9c11e90a44c7e9 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Tue, 27 Oct 2026 15:26:20 +0100 Subject: Multicart Loading with BBS --- README.md | 14 +++++ doc/journal.md | 30 +++++++++ src/cart.c | 193 +++++++++++++++++---------------------------------------- src/cart.h | 54 +++++----------- src/env.c | 82 +++++++++++++++--------- src/env.h | 4 +- src/fs.c | 36 +++++++++-- src/game.c | 30 +++------ src/game.h | 5 +- src/menu.c | 32 +++++----- src/menu.h | 4 +- src/path.c | 16 +++++ src/path.h | 5 ++ src/picow.c | 10 +-- 14 files changed, 259 insertions(+), 256 deletions(-) diff --git a/README.md b/README.md index c61aed3..603b5be 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,17 @@ # PICO-w PICO-w provides a minimal launcher for the officially exported PICO-8 Wasm runtime. This enables support on other platforms (such as homebrew) by running the official PICO-8 as a Wasm program instead of emulating it. + +## TODOs + +- [ ] Codereview +- [ ] Add clipboard support +- [ ] Improve audio error handling +- [ ] Add `.p8.tar` support +- [ ] Add `wget` shim for `.nfo` requests +- [ ] Implement clean cart shutdown (needs sample cart) + - Not sure this is possible, see: `shutdown.p8.png` cart +- [ ] The Mythical Shutdown Menu Item +- [ ] Add sysroot.tar recipe +- [ ] Building from pre-bundled sysroot +- [ ] The Legendary PS Vita Port diff --git a/doc/journal.md b/doc/journal.md index a55cb81..86a427f 100644 --- a/doc/journal.md +++ b/doc/journal.md @@ -1,3 +1,33 @@ +# 27.10.2026 + +Some progress on the `LOAD("#FOO")` support! It turns out that loading these kinds of carts is a 2 step process: + +First, PICO-8 will first try to fetch an `.nfo` on the cart which looks like this: + +```text +lid:cartid-42 +mid:cartid +title:My Cool Cart +author:Me +ts:1970-01-01 00:00:00 +catsub:1792 +``` + +> I have absolutely no idea what `catsub` is, but it appears to be the same for the few carts that I tried. + +Secondly, it will try to load the cart specified by `lid` from disk, and if that fails additionally use the `wget` machinery to try and download it. + +A few things to note: + +1. The `.nfo` doesn't have to be complete - a simple `lid:$NAME` would work and case PICO-8 to try and load `$NAME.p8.png` (PNG!) via `open()` syscall. +2. If the file exists (i.e. `open()` succeeds) it will use that instead of trying to `wget` it. + +This means that supporting BBS multicarts is possible, and we can do it either: + +- automatically by generating `.p8.png` files from ROMs on the fly +- requiring these kinds of multicarts have `.p8.png` files in their `.p8.dir`s +- manually setting up a `.p8.tar` files for the filesystem to include the multicarts, and have PICO-w read them in when the FS is setup (we don't read any Tar file ATM, but that _was_ the plan) + # 26.10.2026 We are able to boot! It was mostly mechanically porting the Emscripten glue code to C+SDL3, and not weird surprises. One issue I _do_ have right now is that there is no "QUIT" entry in the pause menu like there is when you export as `.BIN`; I would _love_ for this to work so that I can use this entry as a mechanism to navigate back to the cart selection menu from a running game. diff --git a/src/cart.c b/src/cart.c index 3e61e18..e4dbfbb 100644 --- a/src/cart.c +++ b/src/cart.c @@ -16,12 +16,6 @@ static bool cart_file_path(struct pw_cart_file *file, char const *dir, return true; } -static int cmp_cart_entry(void const *a, void const *b) { - struct pw_cart_entry const *ea = a; - struct pw_cart_entry const *eb = b; - return SDL_strcmp(ea->file.name, eb->file.name); -} - static int cmp_cart_file(void const *a, void const *b) { struct pw_cart_file const *fa = a; struct pw_cart_file const *fb = b; @@ -54,69 +48,25 @@ struct pw_carts *pw_carts_list(char const *dir) { } size_t count = (size_t)icount; - if (count > SIZE_MAX / sizeof(struct pw_cart_entry)) { + if (count > SIZE_MAX / sizeof(struct pw_cart_file)) { goto error; } - carts = SDL_malloc(sizeof(struct pw_carts) + - count * sizeof(struct pw_cart_entry)); + carts = SDL_malloc(sizeof(*carts) + count * sizeof(*carts->files)); if (!carts) { goto error; } carts->count = 0; for (size_t i = 0; i < count; i++) { - struct pw_cart_entry *entry = &carts->entries[i]; - if (!cart_file_path(&entry->file, dir, cartfiles[i], ".png")) { + struct pw_cart_file *file = &carts->files[i]; + if (!cart_file_path(file, dir, cartfiles[i], ".png")) { goto error; } - entry->multi = NULL; - entry->nmulti = 0; } carts->count = count; - SDL_qsort(carts->entries, carts->count, sizeof(*carts->entries), - cmp_cart_entry); - - char multidir[PW_CART_PATH_MAX]; - for (size_t i = 0; i < count; i++) { - struct pw_cart_entry *entry = &carts->entries[i]; - - // We are _guaranteed_ that `len < PW_PATH_MAX` by construction, and that - // it has a `.p8.png` suffix. This allows us to easily add the `.p8.dir` - // suffix for the multi-cart directory. Skip carts without a multi-dir. - size_t len = SDL_strlcpy(multidir, entry->file.path, PW_CART_PATH_MAX); - SDL_memcpy(multidir + len - 3, "dir", 4); - if (!SDL_GetPathInfo(multidir, &pinfo) || - pinfo.type != SDL_PATHTYPE_DIRECTORY) { - continue; - } - - SDL_free(cartfiles); - cartfiles = SDL_GlobDirectory(multidir, "*.p8.rom", - SDL_GLOB_CASEINSENSITIVE, &icount); - if (!cartfiles || icount < 0) { - goto error; - } - if (!icount) { - continue; - } - - entry->nmulti = (size_t)icount; - entry->multi = SDL_malloc(entry->nmulti * sizeof(struct pw_cart_file)); - if (!entry->multi) { - goto error; - } - - for (size_t j = 0; j < entry->nmulti; j++) { - if (!cart_file_path(&entry->multi[j], multidir, cartfiles[j], ".rom")) { - goto error; - } - } - - SDL_qsort(entry->multi, entry->nmulti, sizeof(*entry->multi), - cmp_cart_file); - } + SDL_qsort(carts->files, carts->count, sizeof(*carts->files), cmp_cart_file); SDL_free(cartfiles); return carts; @@ -137,11 +87,6 @@ void pw_carts_free(struct pw_carts *carts) { return; } - for (size_t i = 0; i < carts->count; i++) { - if (carts->entries[i].multi) { - SDL_free(carts->entries[i].multi); - } - } SDL_free(carts); } @@ -175,18 +120,18 @@ static bool blit_image(union pw_png_pixel const *image, SDL_Texture *texture) { } struct pw_cart_png *pw_cart_png_init(SDL_Renderer *renderer) { - struct pw_cart_png *cart = SDL_malloc(sizeof(struct pw_cart_png)); - if (!cart) { + struct pw_cart_png *png = SDL_malloc(sizeof(struct pw_cart_png)); + if (!png) { return NULL; } - cart->texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, - SDL_TEXTUREACCESS_STREAMING, - PW_CART_PNG_WIDTH, PW_CART_PNG_HEIGHT); - if (!cart->texture) { + png->texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, + SDL_TEXTUREACCESS_STREAMING, + PW_CART_PNG_WIDTH, PW_CART_PNG_HEIGHT); + if (!png->texture) { goto error; } - if (!SDL_SetTextureScaleMode(cart->texture, SDL_SCALEMODE_NEAREST)) { + if (!SDL_SetTextureScaleMode(png->texture, SDL_SCALEMODE_NEAREST)) { goto error; } @@ -197,30 +142,30 @@ struct pw_cart_png *pw_cart_png_init(SDL_Renderer *renderer) { goto error; } - bool ok = blit_image(image, cart->texture); + bool ok = blit_image(image, png->texture); SDL_free(image); if (!ok) { goto error; } - SDL_zeroa(cart->rom); - return cart; + SDL_zeroa(png->rom); + return png; error: - pw_cart_png_free(cart); + pw_cart_png_free(png); return NULL; } -void pw_cart_png_free(struct pw_cart_png *cart) { - if (!cart) { - SDL_InvalidParamError("cart"); +void pw_cart_png_free(struct pw_cart_png *png) { + if (!png) { + SDL_InvalidParamError("png"); return; } - if (cart->texture) { - SDL_DestroyTexture(cart->texture); + if (png->texture) { + SDL_DestroyTexture(png->texture); } - SDL_free(cart); + SDL_free(png); } static bool load_cart_png(char const *path, SDL_Texture *texture, @@ -284,91 +229,65 @@ cleanup: return result; } -bool pw_cart_png_load(struct pw_cart_entry *entry, struct pw_cart_png *cart) { - if (!entry) { - SDL_InvalidParamError("entry"); - return false; - } - if (!cart) { - SDL_InvalidParamError("cart"); +bool pw_cart_png_load(struct pw_cart_file *file, struct pw_cart_png *png) { + if (!file) { + SDL_InvalidParamError("file"); return false; } - - return load_cart_png(entry->file.path, cart->texture, cart->rom); -} - -static bool load_cart_rom(char const *path, uint8_t rom[PW_CART_ROM_SIZE]) { - SDL_IOStream *stream = SDL_IOFromFile(path, "rb"); - if (!stream) { + if (!png) { + SDL_InvalidParamError("png"); return false; } - bool result = false; - - Sint64 ssize = SDL_GetIOSize(stream); - if (ssize != PW_CART_ROM_SIZE) { - if (ssize >= 0) { - SDL_SetError("cart ROM is incorrect size"); - } - goto cleanup; - } - - size_t n = SDL_ReadIO(stream, rom, PW_CART_ROM_SIZE); - if (n != PW_CART_ROM_SIZE) { - goto cleanup; - } - - result = true; - -cleanup: - result &= SDL_CloseIO(stream); - return result; + return load_cart_png(file->path, png->texture, png->rom); } -struct pw_cart_data *pw_cart_data_load(struct pw_cart_entry *entry, - struct pw_cart_png *cart) { - if (!entry) { - SDL_InvalidParamError("entry"); +struct pw_cart *pw_cart_load(struct pw_cart_file *file, + struct pw_cart_png *png) { + if (!file) { + SDL_InvalidParamError("file"); return NULL; } - size_t count = entry->nmulti + 1; - struct pw_cart_data *cartdat = - SDL_malloc(sizeof(struct pw_cart_data) + count * sizeof(struct pw_cart)); - if (!cartdat) { + struct pw_cart *cart = SDL_calloc(1, sizeof(*cart)); + if (!cart) { return NULL; } - SDL_strlcpy(cartdat->carts->name, entry->file.name, PW_CART_NAME_MAX); + SDL_strlcpy(cart->name, file->name, PW_CART_NAME_MAX); if (cart) { - SDL_memcpy(cartdat->carts->rom, cart->rom, PW_CART_ROM_SIZE); - } else if (!load_cart_png(entry->file.path, NULL, cartdat->carts->rom)) { + SDL_memcpy(cart->rom, png->rom, PW_CART_ROM_SIZE); + } else if (!load_cart_png(file->path, NULL, cart->rom)) { goto error; } - for (size_t i = 1; i < count; ++i) { - struct pw_cart *c = &cartdat->carts[i]; - struct pw_cart_file *f = &entry->multi[i - 1]; - - SDL_strlcpy(c->name, f->name, PW_CART_NAME_MAX); - if (!load_cart_rom(f->path, c->rom)) { - goto error; - } + // We are _guaranteed_ that `len < PW_PATH_MAX` by construction, and that + // it has a `.p8.png` suffix. This allows us to easily add the `.p8.tar` + // suffix for the user-data mount. + char datpath[PW_CART_PATH_MAX]; + size_t len = SDL_strlcpy(datpath, file->path, PW_CART_PATH_MAX); + SDL_memcpy(datpath + len - 3, "tar", 4); + cart->dat = pw_fs_init(datpath); + if (!cart->dat) { + goto error; } - cartdat->count = count; - return cartdat; + return cart; error: - pw_cart_data_free(cartdat); + if (cart->dat) { + pw_fs_free(cart->dat); + } + SDL_free(cart); return NULL; } -void pw_cart_data_free(struct pw_cart_data *cartdat) { - if (!cartdat) { - SDL_InvalidParamError("cartdat"); +void pw_cart_free(struct pw_cart *cart) { + if (!cart) { + SDL_InvalidParamError("cart"); return; } - SDL_free(cartdat); + pw_fs_free(cart->dat); + SDL_free(cart); } diff --git a/src/cart.h b/src/cart.h index 7478472..47ef77f 100644 --- a/src/cart.h +++ b/src/cart.h @@ -3,6 +3,8 @@ #include +#include "fs.h" + /** The maximum supported PICO-8 cart path size. */ #define PW_CART_PATH_MAX 960 @@ -20,33 +22,18 @@ /** * A PICO-8 cart file that can be loaded. - * - * Carts are identified by name for for `LOAD()`-ing within PICO-8, this - * enables multi-cart support. */ struct pw_cart_file { char name[PW_CART_NAME_MAX]; char path[PW_CART_PATH_MAX]; }; -/** - * A top-level loadable cart that can be selected from the cart menu. - * - * Each cart has a separate multi-cart sub-directory for adding additional - * carts that can be `LOAD()`-ed at runtime. - */ -struct pw_cart_entry { - struct pw_cart_file file; - struct pw_cart_file *multi; - size_t nmulti; -}; - /** * The list of carts read from the carts directory. */ struct pw_carts { size_t count; - struct pw_cart_entry entries[]; + struct pw_cart_file files[]; }; /** @@ -59,19 +46,12 @@ struct pw_cart_png { }; /** - * A PICO-8 cart data. + * A PICO-8 cart, its loaded rom and mounted user data file-system. */ struct pw_cart { char name[PW_CART_NAME_MAX]; uint8_t rom[PW_CART_ROM_SIZE]; -}; - -/** - * Embedded cart data for running in PICO-8. - */ -struct pw_cart_data { - size_t count; - struct pw_cart carts[]; + struct pw_fs *dat; }; /** @@ -85,36 +65,34 @@ struct pw_carts *pw_carts_list(char const *dir); void pw_carts_free(struct pw_carts *carts); /** - * Initializes an empty cart. + * Initializes an empty cart PNG. */ struct pw_cart_png *pw_cart_png_init(SDL_Renderer *renderer); /** * Frees a cart and all of its resources. */ -void pw_cart_png_free(struct pw_cart_png *cart); +void pw_cart_png_free(struct pw_cart_png *png); /** - * Loads the main cart from an entry. + * Loads the main cart PNG from a file. * - * This will also decode and load the PNG to the cart `png` texture for use in - * rendering. + * This will also decode and load the steganographically encoded ROM. */ -bool pw_cart_png_load(struct pw_cart_entry *entry, struct pw_cart_png *cart); +bool pw_cart_png_load(struct pw_cart_file *file, struct pw_cart_png *png); /** - * Loads cart data from a cart entry. It always has at least one cart (the main - * cart PNG from the entry), with an additional entry for each sub-cart in the - * case of a multi-cart. + * Loads a cart from a file, including mounting its user data file-system. * - * Optionally specify a loaded cart PNG to copy the initial ROM from. + * Optionally specify a loaded cart PNG to copy the ROM from instead of + * re-reading from disk. */ -struct pw_cart_data *pw_cart_data_load(struct pw_cart_entry *entry, - struct pw_cart_png *cart); +struct pw_cart *pw_cart_load(struct pw_cart_file *file, + struct pw_cart_png *png); /** * Frees a multi-cart and all of its resources. */ -void pw_cart_data_free(struct pw_cart_data *cart); +void pw_cart_free(struct pw_cart *cart); #endif diff --git a/src/env.c b/src/env.c index 7798eaa..f109c5f 100644 --- a/src/env.c +++ b/src/env.c @@ -7,6 +7,7 @@ #include #include "env.h" +#include "pico8.h" // PICO-8 allocates a fix 128 Mb heap unconditinoally; but for some platforms, // that may be too much. Specify a lower bound, in 64Kb pages, for the memory @@ -94,7 +95,7 @@ bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context) { if (env->memory.size < ENV_MIN_PAGES * wasm2c_pico8_pagesize_env_memory) { return SDL_SetError("cannot write to fixed memory offsets"); } - if (!context.fb || !context.input || !context.cartdat || !context.instance) { + if (!context.fb || !context.input || !context.cart || !context.instance) { return SDL_InvalidParamError("context"); } @@ -512,9 +513,9 @@ uint32_t w2c_env_0x5F_0x5Fsyscall140(struct w2c_env *env, uint32_t which, uint32_t whence = env_varg(env, ap, 4); int64_t pos; - if (!pw_fs_seek(env->context.userdat, fd, offset, whence, &pos)) { + if (!pw_fs_seek(env->context.cart->dat, fd, offset, whence, &pos)) { SDL_LogDebug(0, "error seeking file: %s", SDL_GetError()); - return -pw_fs_errno(env->context.userdat); + return -pw_fs_errno(env->context.cart->dat); } wasm_rt_memory_store64(&env->memory, result, pos); @@ -546,9 +547,10 @@ uint32_t w2c_env_0x5F_0x5Fsyscall145(struct w2c_env *env, uint32_t which, uint32_t buffer = wasm_rt_memory_load32(&env->memory, iov + 8 * i); uint32_t len = wasm_rt_memory_load32(&env->memory, iov + 8 * i + 4); uint32_t size; - uint8_t const *contents = pw_fs_read(env->context.userdat, fd, len, &size); + uint8_t const *contents = + pw_fs_read(env->context.cart->dat, fd, len, &size); if (!contents) { - return -pw_fs_errno(env->context.userdat); + return -pw_fs_errno(env->context.cart->dat); } wasm_rt_memory_store(&env->memory, buffer, contents, size); n += size; @@ -580,15 +582,15 @@ uint32_t w2c_env_0x5F_0x5Fsyscall146(struct w2c_env *env, uint32_t which, for (uint32_t i = 0; i < iovcnt; i++) { uint32_t buffer = wasm_rt_memory_load32(&env->memory, iov + 8 * i); uint32_t len = wasm_rt_memory_load32(&env->memory, iov + 8 * i + 4); - uint8_t *contents = pw_fs_write(env->context.userdat, fd, len); + uint8_t *contents = pw_fs_write(env->context.cart->dat, fd, len); if (!contents) { - return -pw_fs_errno(env->context.userdat); + return -pw_fs_errno(env->context.cart->dat); } wasm_rt_memory_load(&env->memory, buffer, contents, len); } - if (!pw_fs_flush(env->context.userdat, fd)) { - return -pw_fs_errno(env->context.userdat); + if (!pw_fs_flush(env->context.cart->dat, fd)) { + return -pw_fs_errno(env->context.cart->dat); } return (uint32_t)n; @@ -629,11 +631,15 @@ uint32_t w2c_env_0x5F_0x5Fsyscall5(struct w2c_env *env, uint32_t which, } uint32_t flags = env_varg(env, ap, 1); + SDL_Log("OPEN: %s", path); + int32_t fd; - if (!pw_fs_open(env->context.userdat, path, flags, &fd)) { - return -pw_fs_errno(env->context.userdat); + if (!pw_fs_open(env->context.cart->dat, path, flags, &fd)) { + SDL_Log("ERR: %d", -pw_fs_errno(env->context.cart->dat)); + return -pw_fs_errno(env->context.cart->dat); } + SDL_Log("FD: %d", fd); return fd; } @@ -653,8 +659,11 @@ uint32_t w2c_env_0x5F_0x5Fsyscall6(struct w2c_env *env, uint32_t which, (void)which; int32_t fd = (int32_t)env_varg(env, ap, 0); - if (!pw_fs_close(env->context.userdat, fd)) { - return -pw_fs_errno(env->context.userdat); + + SDL_Log("CLOSE %d", fd); + + if (!pw_fs_close(env->context.cart->dat, fd)) { + return -pw_fs_errno(env->context.cart->dat); } return 0; } @@ -678,24 +687,20 @@ uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env, // embedded cart data, this is always 1 (i.e. true). return 1; case 1: - return (uint32_t)env->context.cartdat->count; - case 2: - // Gets a byte from the embedded ROM data, note that we need to convert - // the byte position to a cart index and offset within that cart. - { - uint32_t i = env->data.cartpos / PW_CART_ROM_SIZE; - if (i < env->context.cartdat->count) { - uint32_t offset = env->data.cartpos++ % PW_CART_ROM_SIZE; - return env->context.cartdat->carts[i].rom[offset]; - } else { - return 0; - } + // We only ever include a single embedded cart. + return 1; + case 2: { + uint32_t pos = env->data.cartpos++; + if (pos < PW_CART_ROM_SIZE) { + return env->context.cart->rom[pos]; + } else { + return 0; } + } case 4: - if (env->data.cartname.index < env->context.cartdat->count && + if (!env->data.cartname.index && env->data.cartname.pos < PW_CART_NAME_MAX) { - return env->context.cartdat->carts[env->data.cartname.index] - .name[env->data.cartname.pos++]; + return env->context.cart->name[env->data.cartname.pos++]; } else { return 0; } @@ -966,6 +971,27 @@ void w2c_env_0x5Femscripten_async_wget_data(struct w2c_env *env, uint32_t url, char urlstr[256] = {0}; wasm_rt_memory_loada(&env->memory, url, urlstr, sizeof(urlstr)); + // In order to fudge `LOAD("#...")` support, we implement the _bear_ + // minimum `.nfo` file fetcher that just echos the `lid` back. This + // makes PICO-8 attempt to load the `.p8.png` from the mounted cart + // filesystem from `user_data/bbs/carts/$lid.p8.png`, enabling BBS + // multicart support. + static char const nfo[] = "/bbs/cpost_lister3.php?nfo=1&version=000207e&lid="; + if (SDL_strstr(urlstr, nfo) == urlstr) { + // Point to the `lid=...`, replace `=` with `:` for the `.nfo` + // format, and then call the `onload` handler with the allocated + // data buffer. + char *buf = urlstr + sizeof(nfo) - 5; + buf[3] = ':'; + + uint32_t len = SDL_strlen(buf); + uint32_t data = env_strndup(env, buf, len + 1); + w2c_pico8_dynCall_viii(env->context.instance, onload, userdata, data, len); + w2c_pico8_0x5Ffree(env->context.instance, data); + + return; + } + SDL_LogDebug(0, "ignoring attempt to fetch %s", urlstr); w2c_pico8_dynCall_vi(env->context.instance, onerror, userdata); } diff --git a/src/env.h b/src/env.h index d5e56b1..2b36773 100644 --- a/src/env.h +++ b/src/env.h @@ -4,7 +4,6 @@ #include "audio.h" #include "cart.h" #include "fb.h" -#include "fs.h" #include "input.h" #include "pico8.h" #include "wasmrt.h" @@ -21,8 +20,7 @@ struct pw_env_ctx { struct pw_framebuffer *fb; struct pw_audio *audio; struct pw_input *input; - struct pw_cart_data *cartdat; - struct pw_fs *userdat; + struct pw_cart *cart; struct w2c_pico8 *instance; }; diff --git a/src/fs.c b/src/fs.c index 365b0df..e4a2d4e 100644 --- a/src/fs.c +++ b/src/fs.c @@ -3,6 +3,7 @@ #include #include "fs.h" +#include "path.h" #include "sysroot.h" #include "tar.h" @@ -188,6 +189,10 @@ struct pw_fs *pw_fs_init(char const *path) { SDL_LogDebug(0, "using in-memory cart storage"); } + for (size_t i = 0; i < fs->root.count; ++i) { + SDL_LogDebug(0, "mounted %s", fs->root.entries[i]->path); + } + fs->dev.in.r.type = PW_FS_NODE_STATIC; fs->dev.in.r.data = empty_bytes; fs->dev.in.r.size = 0; @@ -321,9 +326,29 @@ static int cmp_path(void const *a, void const *b) { return SDL_strcmp(path, (*entry)->path); } -static struct pw_fs_entry *find_entry(struct pw_fs *fs, char const *path) { +static char const cart_dir[] = "user_data/carts/"; + +static int cmp_cart_path(void const *a, void const *b) { + char const *cart = a; + struct pw_fs_entry const *const *entry = b; + + char const *path = (*entry)->path; + if (SDL_strstr(path, cart_dir) == path) { + return SDL_strcmp(cart, path + sizeof(cart_dir) - 1); + } else { + return SDL_strcmp(cart_dir, path); + } +} + +static struct pw_fs_entry *find_entry(struct pw_fs *fs, char const *path, + bool absolute) { + SDL_CompareCallback compare = cmp_path; + if (!absolute && (pw_hasext(path, ".p8") || pw_hasext(path, ".p8.png"))) { + compare = cmp_cart_path; + } + struct pw_fs_entry **e = SDL_bsearch(path, fs->root.entries, fs->root.count, - sizeof(*fs->root.entries), cmp_path); + sizeof(*fs->root.entries), compare); if (!e) { return NULL; } @@ -372,6 +397,8 @@ error: bool pw_fs_open(struct pw_fs *fs, char const *path, enum pw_fs_open_flag flags, pw_fd *fd) { + char const *rawpath = path; + if (!fs) { return SDL_InvalidParamError("fs"); } @@ -415,7 +442,8 @@ bool pw_fs_open(struct pw_fs *fs, char const *path, enum pw_fs_open_flag flags, s->flags |= PW_FS_STREAM_APPEND; } - struct pw_fs_entry *e = find_entry(fs, path); + bool absolute = path != rawpath; + struct pw_fs_entry *e = find_entry(fs, path, absolute); if (e && (flags & PW_O_CREAT) && (flags & PW_O_EXCL)) { fs->err = PW_EEXIST; return SDL_SetError("file already exists"); @@ -720,7 +748,7 @@ bool pw_fs_flush(struct pw_fs *fs, pw_fd fd) { } b->data[i] = '\0'; - SDL_Log("PICO-8>%c %s", marker, b->data + offset); + SDL_LogDebug(0, "PICO-8>%c %s", marker, b->data + offset); offset = i + 1; } diff --git a/src/game.c b/src/game.c index 14f7126..c1b52da 100644 --- a/src/game.c +++ b/src/game.c @@ -36,11 +36,6 @@ struct pw_game *pw_game_init(SDL_Renderer *renderer, enum pw_scale_mode mode, goto error; } - game->userdat = pw_fs_init(NULL); - if (!game->userdat) { - goto error; - } - return game; error: @@ -56,9 +51,6 @@ error: if (game->input) { pw_input_free(game->input); } - if (game->userdat) { - pw_fs_free(game->userdat); - } SDL_free(game); return NULL; @@ -74,38 +66,36 @@ void pw_game_free(struct pw_game *game) { pw_audio_free(game->audio); pw_input_free(game->input); pw_env_free(game->env); - if (game->cartdat) { - pw_cart_data_free(game->cartdat); + if (game->cart) { + pw_cart_free(game->cart); } - pw_fs_free(game->userdat); SDL_free(game); } bool pw_game_start(struct pw_game *game, struct pw_players *players, - struct pw_cart_data *cartdat) { + struct pw_cart *cart) { if (!game) { return SDL_InvalidParamError("game"); } if (!players) { return SDL_InvalidParamError("players"); } - if (!cartdat) { - return SDL_InvalidParamError("cartdat"); + if (!cart) { + return SDL_InvalidParamError("cart"); } pw_input_reset(game->input, players); - if (game->cartdat) { - pw_cart_data_free(game->cartdat); + if (game->cart) { + pw_cart_free(game->cart); } - game->cartdat = cartdat; + game->cart = cart; if (!pw_env_link(game->env, (struct pw_env_ctx){ .fb = game->fb, .audio = game->audio, .input = game->input, - .cartdat = game->cartdat, - .userdat = game->userdat, + .cart = game->cart, .instance = &game->instance, })) { return false; @@ -213,7 +203,7 @@ mixdone: if (game->env->data.state.fssync) { game->env->data.state.fssync = false; - if (!pw_fs_sync(game->userdat)) { + if (!pw_fs_sync(game->cart->dat)) { SDL_LogWarn(0, "failed to persist user storage: %s", SDL_GetError()); } } diff --git a/src/game.h b/src/game.h index 013b386..6f2c98b 100644 --- a/src/game.h +++ b/src/game.h @@ -22,8 +22,7 @@ struct pw_game { struct pw_framebuffer *fb; struct pw_audio *audio; struct pw_input *input; - struct pw_cart_data *cartdat; - struct pw_fs *userdat; + struct pw_cart *cart; }; /** @@ -41,7 +40,7 @@ void pw_game_free(struct pw_game *game); * Starts a PICO-8 game. */ bool pw_game_start(struct pw_game *game, struct pw_players *players, - struct pw_cart_data *cartdat); + struct pw_cart *cart); /** * Handles SDL events for game state. diff --git a/src/menu.c b/src/menu.c index 1902f19..e89020e 100644 --- a/src/menu.c +++ b/src/menu.c @@ -25,14 +25,14 @@ struct pw_menu *pw_menu_init(SDL_Renderer *renderer, char const *carts, goto error; } - menu->cart = pw_cart_png_init(renderer); - if (!menu->cart) { + menu->png = pw_cart_png_init(renderer); + if (!menu->png) { goto error; } if (!menu->carts->count) { SDL_LogWarn(0, "empty or missing carts directory"); - } else if (!pw_cart_png_load(&menu->carts->entries[0], menu->cart)) { + } else if (!pw_cart_png_load(&menu->carts->files[0], menu->png)) { goto error; } @@ -53,8 +53,8 @@ error: if (menu->carts) { pw_carts_free(menu->carts); } - if (menu->cart) { - pw_cart_png_free(menu->cart); + if (menu->png) { + pw_cart_png_free(menu->png); } SDL_free(menu); return NULL; @@ -67,7 +67,7 @@ void pw_menu_free(struct pw_menu *menu) { } pw_carts_free(menu->carts); - pw_cart_png_free(menu->cart); + pw_cart_png_free(menu->png); SDL_free(menu); } @@ -176,40 +176,40 @@ bool pw_menu_event(struct pw_menu *menu, SDL_Event *event) { return true; } -bool pw_menu_iterate(struct pw_menu *menu, struct pw_cart_data **cartdat) { +bool pw_menu_iterate(struct pw_menu *menu, struct pw_cart **cart) { if (!menu) { SDL_InvalidParamError("menu"); return false; } - if (!cartdat) { + if (!cart) { SDL_InvalidParamError("cartdat"); return false; } if (menu->selection.current != menu->selection.next) { - if (!pw_cart_png_load(&menu->carts->entries[menu->selection.next], - menu->cart)) { + if (!pw_cart_png_load(&menu->carts->files[menu->selection.next], + menu->png)) { return false; } menu->selection.current = menu->selection.next; } if (menu->selection.selected) { - *cartdat = pw_cart_data_load(&menu->carts->entries[menu->selection.current], - menu->cart); - if (!*cartdat) { + *cart = + pw_cart_load(&menu->carts->files[menu->selection.current], menu->png); + if (!*cart) { return false; } menu->selection.selected = false; - return true; + } else { + *cart = NULL; } - *cartdat = NULL; return true; } bool pw_menu_render(struct pw_menu *menu) { - SDL_Texture *png = menu->cart->texture; + SDL_Texture *png = menu->png->texture; SDL_Renderer *renderer = SDL_GetRendererFromTexture(png); if (!renderer) { return false; diff --git a/src/menu.h b/src/menu.h index 513c303..9f839d8 100644 --- a/src/menu.h +++ b/src/menu.h @@ -12,7 +12,7 @@ */ struct pw_menu { struct pw_carts *carts; - struct pw_cart_png *cart; + struct pw_cart_png *png; enum pw_scale_mode mode; struct { size_t current; @@ -41,7 +41,7 @@ bool pw_menu_event(struct pw_menu *menu, SDL_Event *event); * Iterate a new frame for the cart selection menu. Sets `cart` to non-`NULL` * if a cart was selected to run in the menu. */ -bool pw_menu_iterate(struct pw_menu *menu, struct pw_cart_data **cartdat); +bool pw_menu_iterate(struct pw_menu *menu, struct pw_cart **cart); /** * Renders the cart selection menu. diff --git a/src/path.c b/src/path.c index e2751a9..6177278 100644 --- a/src/path.c +++ b/src/path.c @@ -59,6 +59,22 @@ bool pw_stripext(char *name, size_t sz, char const *path, char const *ext) { return true; } +bool pw_hasext(char const *path, char const *ext) { + if (!path) { + SDL_InvalidParamError("path"); + return false; + } + if (!ext) { + SDL_InvalidParamError("ext"); + return false; + } + + size_t plen = SDL_strlen(path); + size_t elen = SDL_strlen(ext); + size_t nlen = plen - elen; + return plen >= elen && !SDL_strcasecmp(path + nlen, ext); +} + bool pw_pathjoin(char *path, size_t sz, ...) { if (!path) { SDL_InvalidParamError("path"); diff --git a/src/path.h b/src/path.h index 096434b..286c7e4 100644 --- a/src/path.h +++ b/src/path.h @@ -16,6 +16,11 @@ char const *pw_basename(char const *path); */ bool pw_stripext(char *name, size_t sz, char const *path, char const *ext); +/** + * Returns whether or not `path` has `ext`. + */ +bool pw_hasext(char const *path, char const *ext); + /** * Joins multiple components into `path`. returns `false` if `path` is not * large enough to fit all of the components. diff --git a/src/picow.c b/src/picow.c index 1918889..97e9539 100644 --- a/src/picow.c +++ b/src/picow.c @@ -159,12 +159,12 @@ SDL_AppResult SDL_AppIterate(void *appstate) { } if (state->scene == PW_SCENE_CART_SELECTION) { - struct pw_cart_data *cartdat; - if (!pw_menu_iterate(state->menu, &cartdat)) { + struct pw_cart *cart; + if (!pw_menu_iterate(state->menu, &cart)) { return failf("failed to update cart selection menu state"); } - if (cartdat) { - if (!pw_game_start(state->game, state->players, cartdat)) { + if (cart) { + if (!pw_game_start(state->game, state->players, cart)) { return failf("failed to start selected cart"); } state->scene = PW_SCENE_GAME; @@ -218,7 +218,7 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result) { bye: if (result == SDL_APP_SUCCESS) { - SDL_Log("bye"); + SDL_LogInfo(0, "bye"); } else { SDL_LogCritical(0, "application error"); } -- cgit v1.2.3