diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2025-04-12 22:27:40 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2025-04-12 22:27:40 +0200 |
| commit | b5620018b8b9f78742dd1ac08a64f2f19241346e (patch) | |
| tree | 896a0d1bacbc599edcf5733b3e0f3ceecec4cff9 /tools/extract-data-pod.c | |
| parent | 48d3c24837bef74a14b0c78e959f2c601b397208 (diff) | |
Extra PICO-8 Data Pod
Add a tool for extracting the PICO-8 data.pod file that is exported
with the cart. This includes (on inspecting the binary) the boot
sequence cart among some other things.
It is written as a quick and dirty C program (since we will have to
practice our C anyway).
Diffstat (limited to 'tools/extract-data-pod.c')
| -rw-r--r-- | tools/extract-data-pod.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/tools/extract-data-pod.c b/tools/extract-data-pod.c new file mode 100644 index 0000000..e2cc651 --- /dev/null +++ b/tools/extract-data-pod.c @@ -0,0 +1,61 @@ +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static inline void check(bool cond, char const *msg) { + if (!cond) { + perror(msg); + exit(EXIT_FAILURE); + } +} + +static inline void ensure(bool cond, char const *msg) { + if (!cond) { + fprintf(stderr, "error: %s.\n", msg); + exit(EXIT_FAILURE); + } +} + +int main(int argc, char *argv[]) { + if (argc != 3) { + printf("Usage: %s <pico8.js> <data.pod>\n", argv[0]); + return EXIT_FAILURE; + } + + FILE *f = fopen(argv[1], "r"); + check(f, "fopen(input)"); + + char buffer[1024 * 1024]; + size_t n = fread(buffer, 1, sizeof(buffer) - 1, f); + check(n, "fread(input)"); + buffer[n] = '\0'; + + uint8_t data[32 * 1024]; + uint8_t *dataptr = data; + + char *pos = buffer; + char *start = NULL, *end = NULL; + char stag[] = "fileData0.push.apply(fileData0,[", etag[] = "]);", sep[] = ","; + while ((start = strstr(pos, stag))) { + start += sizeof(stag) - 1; + ensure((end = strstr(start, etag)), "can't find end of file data"); + pos = end + sizeof(etag) - 1; + *end = '\0'; + + for (char *s = strtok(start, sep); s; s = strtok(NULL, sep)) { + long b = strtol(s, &s, 10); + ensure(b < 256 && *s == '\0', "failed to read byte"); + *(dataptr++) = (uint8_t)b; + } + } + + FILE *o = fopen(argv[2], "w"); + check(o, "fopen(output)"); + + size_t datalen = (size_t)(dataptr - data); + check(datalen == fwrite(data, 1, datalen, o), "fwrite(output)"); + + return EXIT_SUCCESS; +} |
