aorecomp

Anarchy Online classic in the browser

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


commit e8fab899f2cc4de50ca3468f7311ff2c4d83f7b2
parent 28733a29b9b410c4e875dbf9a6440ca43696c9b5
Author: anon <anon@example.com>
Date:   Fri,  6 Nov 2026 07:46:07 +0000

Allow browser startup when private mode denies persistent storage

Diffstat:
MDEVELOPERS.md | 3+++
MTODO.md | 12++++++++++++
Mclient/runtime/browser_host.c | 12++++++++++--
Mclient/runtime/browser_host.js | 13+++++++++++++
Atests/e2e/storage.test.mjs | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 91 insertions(+), 2 deletions(-)

diff --git a/DEVELOPERS.md b/DEVELOPERS.md @@ -570,6 +570,9 @@ The snapshot's `cursor` records the native shape, visibility, hotspot and drawn size. `node --test tests/e2e/cursor.test.mjs` checks the browser pointer through low-FPS input, canvas re-entry, contextual selection, camera controls, inventory transfers, resizing and mobile touch input using the isolated test server. +`node --test tests/e2e/storage.test.mjs` checks startup and native inventory +layout writes/reload with persistent storage available or denied, preserving +server-owned items in both cases. Short key/click helpers release on the fresh consumed SDL event, then read the next full snapshot. Reading a large snapshot while holding a skill button can trigger native autorepeat and buy more points than intended. diff --git a/TODO.md b/TODO.md @@ -35,6 +35,18 @@ research separately. More screenshots and test cases do not count as new gamepla ## P0: first experience +- [x] **Load the game when private browsing denies local storage.** Firefox + private windows stop at "Starting Anarchy Online..." because OPFS + `getDirectory()` rejects with `SecurityError`. Use temporary local preferences + when storage is denied. Completion: startup and inventory interaction work + without OPFS, server-owned items survive reload, and ordinary browser + preferences still persist when storage is available. + November 5: storage denial now selects writable memory for local preferences. + Both denied-storage and persistent-storage browser regressions pass, including + native inventory layout writes and server item preservation after reload. + Firefox private-profile checks reach the player, and the user confirms that + private-window loading now works. + - [x] **A1: Smooth NPC and enemy movement.** Reopen the November 4 report of walking followed by multi-metre jumps. Earlier Wardroid/Organic Waste Collector checks passed; they do not dismiss the current report. Reproduce in Omni Trade diff --git a/client/runtime/browser_host.c b/client/runtime/browser_host.c @@ -16,6 +16,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_preferences_directory(const char *base); +void browser_storage_available(em_proxying_ctx *context, void *available); static char *ao_asset_manifest(const char *base) { @@ -114,8 +115,15 @@ void browser_initialize(unsigned character_id) } } free(manifest); - /* Keep the native character directory in origin-private browser storage. */ - backend_t persistent = wasmfs_create_opfs_backend(); + /* Private browsing can deny OPFS; native preferences still need writable files. */ + int storage_available = -1; + em_proxying_queue *queue = em_proxying_queue_create(); + assert(queue); + assert(emscripten_proxy_sync_with_ctx(queue, emscripten_main_runtime_thread_id(), + browser_storage_available, &storage_available)); + em_proxying_queue_destroy(queue); + assert(storage_available >= 0); + backend_t persistent = storage_available ? wasmfs_create_opfs_backend() : wasmfs_create_memory_backend(); assert(wasmfs_create_directory("/persistent", 0755, persistent) == 0); char *directory = browser_preferences_directory(base); free(base); diff --git a/client/runtime/browser_host.js b/client/runtime/browser_host.js @@ -40,6 +40,19 @@ addToLibrary({ }, browser_preferences_directory__deps: ['$UTF8ToString', '$stringToNewUTF8'], browser_preferences_directory: base => stringToNewUTF8('/persistent/' + encodeURIComponent(UTF8ToString(base))), + browser_storage_available__deps: ['emscripten_proxy_finish'], + browser_storage_available: async (context, available) => { + try { + await navigator.storage.getDirectory(); + HEAP32[available >> 2] = 1; + } catch (error) { + if (error.name !== 'SecurityError') throw error; + HEAP32[available >> 2] = 0; + console.warn('Browser storage denied; local preferences will last only for this page.'); + } finally { + _emscripten_proxy_finish(context); + } + }, $AOAssetRequest: async (url, options) => { let delay = 1000; for (;;) { diff --git a/tests/e2e/storage.test.mjs b/tests/e2e/storage.test.mjs @@ -0,0 +1,53 @@ +import assert from 'node:assert/strict'; +import {before, test} from 'node:test'; +import {buildServer, withFixture} from './fixture.mjs'; +import {GameTest} from '../../tools/client/game_test.mjs'; + +before(buildServer); + +for (const denied of [true, false]) { + test(denied ? 'denied browser storage uses temporary preferences' : 'available browser storage retains preferences', + {timeout: 120_000}, async () => { + await withFixture(denied ? 'storage-denied' : 'storage-persistent', async fixture => { + const context = await fixture.visitor(); + await fixture.setup(context, 'grant-item', {template: 121567}); + const items = (await fixture.state(context)).items; + if (denied) { + // Firefox private browsing rejects this call on both pages and workers. + const rejectStorage = `if (navigator.storage) navigator.storage.getDirectory = async () => { + throw new DOMException('Security error when calling GetDirectory', 'SecurityError'); + };`; + await context.addInitScript(rejectStorage); + await context.route('**/wasm/game.js', async route => { + const response = await route.fetch(); + await route.fulfill({response, body: rejectStorage + await response.text()}); + }); + } + const page = await context.newPage(); + const game = await GameTest.attach(page); + fixture.games.push(game); + await page.goto(fixture.url); + await page.waitForFunction(() => typeof gameReady !== 'undefined' && gameReady, null, {timeout: 30_000}); + await game.ready(); + await game.key('i'); + const grid = state => state.grids.find(grid => grid.cells.some(cell => cell.identity[0] === 104)); + const weapon = state => grid(state).cells.find(cell => cell.identity[0] === 104 && cell.identity[1] === 48 && !cell.dummy); + const click = cell => game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2); + assert.equal(weapon(game.state).index, 0); + + // Exercise native preference writes by moving an item to a sparse cell. + await click(weapon(game.state)); + await game.wait('item picked up', state => state.dragged[0] === 104); + await click(grid(game.state).cells.find(cell => cell.index === 8)); + await game.wait('item layout saved', state => state.dragged[0] === 0 && weapon(state).index === 8); + await page.reload(); + await page.waitForFunction(() => typeof gameReady !== 'undefined' && gameReady, null, {timeout: 30_000}); + await game.ready(); + if (!game.state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 104))) await game.key('i'); + + assert.equal(weapon(game.state).index, denied ? 0 : 8); + assert.deepEqual((await fixture.state(context)).items, items); + assert.deepEqual(game.errors, []); + }); + }); +}