aorecomp

Anarchy Online classic in the browser

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


commit 12b9c7a2318902c505ae86fece1e69f6a445862a
parent efedefe5ffce46d0971b081f3270975fca698122
Author: anon <anon@example.com>
Date:   Thu, 22 Oct 2026 03:47:55 +0000

Persist native WASM inventory ordering across reloads

Diffstat:
MDEVELOPERS.md | 4+++-
MTODO.md | 4++--
Mclient/game/ccr.dll/lifted-0005.c | 9+++++++++
Mclient/runtime/browser_host.c | 28+++++++++++++++++++++++++++-
Mclient/runtime/browser_host.js | 2++
Mclient/tests/browser_storage.c | 41+++++++++++++++++++++++++++++++++++++++--
Mdocs/inventory.md | 35+++++++++++++++++++++++++++++------
7 files changed, 111 insertions(+), 12 deletions(-)

diff --git a/DEVELOPERS.md b/DEVELOPERS.md @@ -99,7 +99,9 @@ Autorun keeps moving after releasing Move. Jump, Attack and Use buttons provide the common keyboard actions; inventory uses the game's native INV button. Tap the game to left-click, drag to move windows or items, and hold briefly for -a right-click. Touch controls hide while settings or the chat composer is open. +a right-click. Native inventory icon positions persist in browser storage for +each server URL and character, including after reload. Touch controls hide while +settings or the chat composer is open. Tap Chat to open the phone keyboard; Send enters the message through the game's chat input. The composer accepts the game's Windows-1252 character set and reports unsupported characters before sending. diff --git a/TODO.md b/TODO.md @@ -19,14 +19,14 @@ no extra player-facing provisioning system is needed just for testing. - [x] Show one owned item in the native inventory. Check: a fixture grants one nonstacking item with the correct template, QL and slot; it remains after reconnect and server restart. -- [ ] Move an item to an empty inventory slot. Check: the native drag/drop move +- [x] Move an item to an empty inventory slot. Check: the native drag/drop move survives reconnect without changing its identity or creating another copy. - [x] Delete an inventory item. Check: it disappears permanently and another character cannot delete it by supplying its identity. - [x] Handle a full inventory. Check: a grant or pickup fails without losing the source item or overwriting an occupied slot. -Inventory persistence, protocol evidence and the remaining visual-order gap are +Inventory persistence, protocol evidence and native visual ordering are recorded in [inventory behavior](docs/inventory.md). ## Stacks and backpacks diff --git a/client/game/ccr.dll/lifted-0005.c b/client/game/ccr.dll/lifted-0005.c @@ -13278,6 +13278,15 @@ L_270116F4: PUSH32(esp, eax); /* 0x27011704: push eax */ RECOMP_ICALL(0x2701B5D0u); /* 0x27011705: call 0x2701b5d0 */ /* not lifted */ RECOMP_FLAGS_IN(); +#ifdef __EMSCRIPTEN__ + /* IconGrid_t::CheckGrid has committed its IconPositionMap_t. Browser reloads + skip IconPositionSystem_t's destructor, so save the completed map now. */ + if (MEM32(esi + 0x38)) { + ecx = MEM32(0x27048BA0); + RECOMP_ICALL(0x27013060u); + RECOMP_FLAGS_IN(); + } +#endif edi = POP32_VAL(esp); /* 0x2701170A: pop edi */ esi = POP32_VAL(esp); /* 0x2701170B: pop esi */ ebx = POP32_VAL(esp); /* 0x2701170C: pop ebx */ diff --git a/client/runtime/browser_host.c b/client/runtime/browser_host.c @@ -15,6 +15,7 @@ char *ao_asset_base_url(void); void ao_asset_manifest_js(em_proxying_ctx *context, void *request); char *ao_asset_manifest_parse(const char *json); +char *browser_icon_positions_directory(const char *base); static char *ao_asset_manifest(const char *base) { @@ -67,7 +68,6 @@ void browser_initialize(unsigned character_id) backend_t remote = wasmfs_create_fetch_backend(base, 1024 * 1024); char *manifest = ao_asset_manifest(base); assert(manifest); - free(base); assert(wasmfs_create_directory("/remote", 0755, remote) == 0); assert(mkdir("/game", 0755) == 0); assert(chdir("/game") == 0); @@ -136,6 +136,32 @@ void browser_initialize(unsigned character_id) assert(fclose(character_preferences) == 0); assert(rename("Prefs/visitor.cfg", character_path) == 0); + /* Keep the native icon-position file across reloads without persisting caches. */ + backend_t persistent = wasmfs_create_opfs_backend(); + assert(wasmfs_create_directory("/persistent", 0755, persistent) == 0); + char *directory = browser_icon_positions_directory(base); + free(base); + assert(mkdir(directory, 0755) == 0 || errno == EEXIST); + char persistent_path[4096]; + snprintf(persistent_path, sizeof(persistent_path), "%s/%u.bin", directory, character_id); + free(directory); + snprintf(character_path, sizeof(character_path), "Prefs/localtest/Char%u/IconPositions.bin", character_id); + int saved = open(persistent_path, O_CREAT | O_EXCL | O_WRONLY, 0600); + if (saved >= 0) { + FILE *original = fopen(character_path, "rb"); + if (original) { + unsigned char buffer[8192]; + size_t size; + while ((size = fread(buffer, 1, sizeof(buffer), original))) + assert(write(saved, buffer, size) == (ssize_t)size); + assert(!ferror(original)); + fclose(original); + } else assert(errno == ENOENT); + assert(close(saved) == 0); + } else assert(errno == EEXIST); + assert(unlink(character_path) == 0 || errno == ENOENT); + assert(symlink(persistent_path, character_path) == 0); + /* Start the guest with the viewport size, including nonstandard aspect ratios. */ int width, height; assert(emscripten_get_canvas_element_size("#canvas", &width, &height) == EMSCRIPTEN_RESULT_SUCCESS); diff --git a/client/runtime/browser_host.js b/client/runtime/browser_host.js @@ -1,4 +1,6 @@ addToLibrary({ + browser_icon_positions_directory__deps: ['$UTF8ToString', '$stringToNewUTF8'], + browser_icon_positions_directory: base => stringToNewUTF8('/persistent/' + encodeURIComponent(UTF8ToString(base))), $AOAssetRequest: async (url, options) => { let delay = 1000; for (;;) { diff --git a/client/tests/browser_storage.c b/client/tests/browser_storage.c @@ -2,6 +2,7 @@ #include <stdio.h> #include <string.h> #include <sys/stat.h> +#include <emscripten.h> #include <emscripten/html5.h> void browser_initialize(unsigned character_id); @@ -85,17 +86,52 @@ static void preferences_use_canvas_dimensions(void) assert(configured_width == width && configured_height == height); } +static void icon_positions_survive_reload(unsigned character_id) +{ + int resume = MAIN_THREAD_EM_ASM_INT({ + return new URLSearchParams(location.search).has('resume'); + }); + char path[128]; + snprintf(path, sizeof(path), "Prefs/localtest/Char%u/IconPositions.bin", character_id); + const char *expected = character_id == 42 ? "icon positions" : "other position"; + if (!resume) { + if (character_id == 43) { + FILE *file = fopen(path, "rb"); + assert(file && fgetc(file) == EOF); + assert(fclose(file) == 0); + } + FILE *file = fopen(path, "wb"); + assert(file); + assert(fwrite(expected, 1, 14, file) == 14); + assert(fclose(file) == 0); + return; + } + + FILE *file = fopen(path, "rb"); + assert(file); + char content[15] = {0}; + assert(fread(content, 1, sizeof(content), file) == 14); + assert(!strcmp(content, expected)); + assert(fclose(file) == 0); +} + int main(void) { - browser_initialize(42); + unsigned character_id = MAIN_THREAD_EM_ASM_INT({ + return new URLSearchParams(location.search).has('other') ? 43 : 42; + }); + browser_initialize(character_id); - FILE *preferences = fopen("Prefs/localtest/Char42/Char.cfg", "r"); + char path[128]; + snprintf(path, sizeof(path), "Prefs/localtest/Char%u/Char.cfg", character_id); + FILE *preferences = fopen(path, "r"); assert(preferences); char content[128] = {0}; assert(fread(content, 1, sizeof(content) - 1, preferences) == 29); fclose(preferences); assert(!strcmp(content, "IsFirstTime 0\nIsTipEnabled 0\n")); preferences_use_canvas_dimensions(); + icon_positions_survive_reload(character_id); replace_occlusion_cache(); create_occlusion_cache(); read_across_asset_chunk(); @@ -103,4 +139,5 @@ int main(void) puts("PASS: asset reads preserve bytes across the 1 MiB boundary"); puts("PASS: private preferences use the canvas dimensions"); puts("PASS: visitor preferences disable first-run tutorials and tips"); + puts("PASS: native icon positions use persistent browser storage"); } diff --git a/docs/inventory.md b/docs/inventory.md @@ -58,17 +58,33 @@ and confirmation emit this packet. The server checks the session, owner, page, occupied slot and both templates, saves the deletion, then echoes the action. Repeated requests for the now-empty slot do nothing. -## Visual order remains a separate client concern +## Native visual order The shipped `cd_image/text/help/The Inventory Window.txt` documents left-click pickup/place, Shift-left-click inspection and held-right-click deletion. Manual WASM testing confirms that picking up an item and placing it elsewhere in the same window rearranges its visual cell without sending a server move request. -Its logical server slot remains unchanged. `browser_initialize` in -`client/runtime/browser_host.c` currently copies native preferences into a -private in-memory filesystem on every startup. Visual ordering therefore resets -on browser reload. The TODO's native move/reconnect milestone remains open until -that client preference persistence is implemented and verified. +Its logical server slot remains unchanged. + +`IconPositionSystem_t::Init`, `Load` and `Save` in +`client/game/ccr.dll/lifted-0006.c` (`sub_27013000`, `sub_27013200` and +`sub_27013060`) use `Prefs/localtest/Char<id>/IconPositions.bin`. The native +`IconGrid_t::CheckGrid` (`sub_27011590` in +`client/game/ccr.dll/lifted-0005.c`) commits the cell/identity map after a grid +change. Browser builds save that completed map immediately, since page reloads +skip the native shutdown destructor that ordinarily saves it. + +`browser_initialize` in `client/runtime/browser_host.c` links only this native +binary file to the browser's origin-private filesystem. The directory is keyed +by the server asset URL and the filename by character ID. Other preferences and +occlusion caches keep their existing in-memory behavior. Existing icon-position +bytes are preserved; a missing file starts empty. Native loads and saves use the +same file format and the same logical item identities. + +The native fixture moved one pistol from visual cell 0 to cell 8, then verified +cell 8 after page reload and a server restart. Its server ID, QL, logical slot +and item count stayed unchanged. A second visitor moved its own pistol without +changing the first visitor's cell. Idle frames did not rewrite the saved file. ## Verification @@ -86,3 +102,10 @@ The isolated fixture grants one pistol before native world initialization. Deletion was checked after reload, and a second simultaneous WASM visitor showed only its own pistol while the first visitor's inventory stayed empty. The browser driver closes its contexts and temporary server in `finally`. + +The production-initializer storage regression in `client/tests/browser_storage.c` +has initial, resume and second-character passes. Run `storage-test.html`, +`storage-test.html?resume`, `storage-test.html?other`, +`storage-test.html?resume` and `storage-test.html?other&resume` in one browser +context to check persistence and isolation. Chromium and Firefox pass those +checks alongside the existing cache and viewport cases.