aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 3527d268cb4964e534212768daa36ea38cee1dd0
parent 5d656f43c7fa66db213a9c33a8d029d44ac7d255
Author: anon <anon@example.com>
Date:   Sat, 17 Oct 2026 02:12:44 +0000

Keep browser occlusion caches in writable memory

Diffstat:
Msrc/Makefile.wasm | 4++++
Msrc/runtime/browser_host.c | 13++++++++++---
Asrc/tests/browser_storage.c | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 71 insertions(+), 3 deletions(-)

diff --git a/src/Makefile.wasm b/src/Makefile.wasm @@ -41,6 +41,10 @@ build/wasm/web/files-test.html: tests/files.c runtime/linux_file.c runtime/guest @mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -sWASMFS -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sALLOW_MEMORY_GROWTH -o $@ +build/wasm/web/storage-test.html: tests/browser_storage.c runtime/browser_host.c runtime/browser_host.js Makefile.wasm + @mkdir -p $(@D) + $(CC) $(CFLAGS) $(filter %.c,$^) -sWASMFS -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sALLOW_MEMORY_GROWTH --js-library runtime/browser_host.js -o $@ + build/wasm/web/suspension-test.html: tests/browser_suspension.c Makefile.wasm @mkdir -p $(@D) $(CC) $(CFLAGS) $< -sJSPI -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -o $@ diff --git a/src/runtime/browser_host.c b/src/runtime/browser_host.c @@ -20,6 +20,7 @@ void browser_initialize(void) assert(wasmfs_create_directory("/remote", 0755, remote) == 0); assert(mkdir("/game", 0755) == 0); assert(chdir("/game") == 0); + assert(mkdir("cd_image", 0755) == 0); assert(mkdir("/proc", 0755) == 0); assert(mkdir("/proc/self", 0755) == 0); assert(symlink("/game/game.wasm", "/proc/self/exe") == 0); @@ -36,8 +37,8 @@ void browser_initialize(void) assert(file >= 0); close(file); } - /* Preferences live in writable memory, independent of the native client. */ - if (!strncmp(name, "Prefs/", 6)) { + /* Preferences and occlusion caches are private, writable browser data. */ + if (!strncmp(name, "Prefs/", 6) || !strncmp(name, "cd_image/OCC/", 13)) { if (directory) { name[length - 1] = 0; assert(mkdir(name, 0755) == 0); @@ -52,10 +53,16 @@ void browser_initialize(void) fclose(source); fclose(destination); } + } else if (!strncmp(name, "cd_image/", 9) && name[9]) { + /* Link the other immediate children to their HTTP-backed asset tree. */ + const char *slash = strchr(name + 9, '/'); + if (!slash || slash == name + length - 1) { + if (directory) name[length - 1] = 0; + assert(symlink(path, name) == 0); + } } } free(manifest); - assert(symlink("/remote/cd_image", "cd_image") == 0); assert(symlink("/remote/Setupf", "Setupf") == 0); assert(symlink("/remote/fonts", "fonts") == 0); assert(symlink("/remote/version.id", "version.id") == 0); diff --git a/src/tests/browser_storage.c b/src/tests/browser_storage.c @@ -0,0 +1,57 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> + +void browser_initialize(void); + +static void replace_occlusion_cache(void) +{ + const char *path = "cd_image/OCC/OCCBL_705.i"; + const char *remote = "/remote/cd_image/OCC/OCCBL_705.i"; + FILE *source = fopen(remote, "rb"), *copy = fopen(path, "rb"); + assert(source && copy); + unsigned char expected[8192], actual[8192]; + size_t size; + while ((size = fread(expected, 1, sizeof(expected), source))) { + assert(fread(actual, 1, size, copy) == size); + assert(!memcmp(expected, actual, size)); + } + assert(!ferror(source) && fgetc(copy) == EOF); + fclose(source); + fclose(copy); + struct stat before, after; + assert(stat(remote, &before) == 0 && before.st_size > 8); + + copy = fopen(path, "wb"); + assert(copy); + assert(fwrite("occluded", 1, 8, copy) == 8); + assert(fclose(copy) == 0); + + assert(stat(remote, &after) == 0 && after.st_size == before.st_size); + assert(stat(path, &after) == 0 && after.st_size == 8); + copy = fopen(path, "rb"); + assert(copy && fread(actual, 1, 8, copy) == 8); + assert(!memcmp(actual, "occluded", 8)); + fclose(copy); +} + +static void create_occlusion_cache(void) +{ + struct stat info; + assert(stat("cd_image/OCC/storage-test.i", &info) == -1); + + FILE *file = fopen("cd_image/OCC/storage-test.i", "wb"); + assert(file && fwrite("cache", 1, 5, file) == 5); + assert(fclose(file) == 0); + + assert(stat("cd_image/OCC/storage-test.i", &info) == 0 && info.st_size == 5); +} + +int main(void) +{ + browser_initialize(); + replace_occlusion_cache(); + create_occlusion_cache(); + puts("PASS: browser occlusion caches preserve assets and support writes"); +}