commit 3cfc15c0c77cb8fba7fb8962b9428a63301c79f2
parent 3b7bc2041ce6babc41a106a4e52257d595e02da9
Author: anon <anon@example.com>
Date: Tue, 20 Oct 2026 23:13:11 +0000
Show WASM client download progress with published bundle sizes
Diffstat:
4 files changed, 74 insertions(+), 10 deletions(-)
diff --git a/src/browser/index.html b/src/browser/index.html
@@ -29,8 +29,10 @@
#chat-button:active, #menu-button:active, #touch-actions button:active { opacity: .8; }
#chat-compose { position: absolute; }
#chat-text { display: block; box-sizing: border-box; width: 100%; height: 100%; min-width: 0; padding: 0; border: 0; border-radius: 0; outline: none; background: #222; color: #fff; font: 12px GameVerdana, Verdana, sans-serif; }
- #loading { position: fixed; inset: 0; display: grid; place-items: center; pointer-events: none; }
- #loading[hidden] { display: none; }
+ #loading { position: fixed; inset: 0; display: grid; place-items: center; padding: 24px; text-align: center; white-space: pre-line; pointer-events: none; }
+ #download-progress { position: fixed; top: calc(50% + 36px); left: 50%; transform: translateX(-50%); width: min(320px, 80vw); height: 6px; overflow: hidden; border-radius: 3px; background: #333; pointer-events: none; }
+ #download-fill { width: 0; height: 100%; background: #9ab9cc; }
+ #loading[hidden], #download-progress[hidden] { display: none; }
#reconnect { position: fixed; top: calc(50% + 24px); left: 50%; transform: translateX(-50%); padding: 8px 16px; }
dialog { width: min(320px, calc(100vw - 64px)); padding: 24px; border: 1px solid #777; border-radius: 8px; background: #202428; color: #eee; box-shadow: 0 8px 40px #0008; }
dialog::backdrop { background: #0006; }
@@ -45,7 +47,8 @@
</style>
<div id="game-surface">
<canvas id="canvas" width="1024" height="768" tabindex="0"></canvas>
-<div id="loading" role="status">Loading...</div>
+<div id="loading" role="status">Downloading Anarchy Online client...</div>
+<div id="download-progress" role="progressbar" aria-label="Client download" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0" hidden><div id="download-fill"></div></div>
<button id="reconnect" hidden>Reconnect</button>
<div id="game-toolbar" hidden>
<div id="chat-actions" hidden>
@@ -89,6 +92,8 @@
</dialog>
<script>
const status = document.querySelector('#loading');
+ const downloadProgress = document.querySelector('#download-progress');
+ const downloadFill = document.querySelector('#download-fill');
const canvas = document.querySelector('#canvas');
const gameSurface = document.querySelector('#game-surface');
const playerMenu = document.querySelector('#player-menu');
@@ -109,6 +114,7 @@
print: log,
printErr: log,
onAbort: message => {
+ downloadProgress.hidden = true;
status.textContent = 'Stopped: ' + message;
status.hidden = false;
console.error(message);
@@ -213,15 +219,24 @@
resumeAudio();
}
- // HTTP handles decompression; count prepared bytes while compilation streams.
+ // The published sizes distinguish the compressed transfer from decoded progress.
async function instantiateClient(imports, receive) {
const response = await fetch('/wasm/game.wasm');
if (!response.ok) throw new Error(`Client download: HTTP ${response.status}`);
+ const totalMB = Number('__WASM_DOWNLOAD_MB__');
+ status.textContent = `Downloading Anarchy Online client...\n0.00 / ${totalMB.toFixed(2)} MB`;
+ downloadProgress.hidden = false;
let loaded = 0;
const progress = new TransformStream({transform(chunk, controller) {
loaded += chunk.byteLength;
- status.textContent = `Preparing client... ${(loaded / 1000000).toFixed(1)} MB`;
+ const percent = Math.min(100, loaded / Number('__WASM_DECODED_BYTES__') * 100);
+ status.textContent = `Downloading Anarchy Online client...\n${(totalMB * percent / 100).toFixed(2)} / ${totalMB.toFixed(2)} MB`;
+ downloadFill.style.width = `${percent}%`;
+ downloadProgress.setAttribute('aria-valuenow', Math.round(percent));
controller.enqueue(chunk);
+ }, flush() {
+ downloadProgress.hidden = true;
+ status.textContent = 'Starting Anarchy Online...';
}});
const decoded = response.body.pipeThrough(progress);
const result = await WebAssembly.instantiateStreaming(new Response(decoded, {
@@ -329,7 +344,7 @@ show();
for (const type of ['pointerdown', 'keydown']) {
document.addEventListener(type, resumeAudio, true);
}
- status.textContent = 'Loading...';
+ status.textContent = 'Downloading Anarchy Online client...';
const response = await fetch('/browser-launch', {method: 'POST'});
if (!response.ok) { status.textContent = await response.text(); return; }
Module.arguments = (await response.text()).trim().split(' ').slice(1);
diff --git a/src/tests/browser_loading.mjs b/src/tests/browser_loading.mjs
@@ -5,6 +5,13 @@ import vm from 'node:vm';
function fixture(response) {
const progress = [];
const loading = {hidden: false, set textContent(value) { progress.push(value); }};
+ const percentages = [], widths = [];
+ const downloadProgress = {hidden: true, setAttribute(name, value) {
+ assert.equal(name, 'aria-valuenow');
+ assert.equal(this.hidden, false);
+ percentages.push(value);
+ }};
+ const downloadFill = {style: {set width(value) { widths.push(value); }}};
const canvas = {addEventListener() {}, getContext() { return null; }};
const context = vm.createContext({
console, WebAssembly, Response, TransformStream, crossOriginIsolated: true,
@@ -12,16 +19,19 @@ function fixture(response) {
updateTouchControls() {},
localStorage: {getItem() { return null; }},
window: {addEventListener() {}},
- document: {querySelector: selector => selector === '#loading' ? loading : canvas},
+ document: {querySelector: selector => ({
+ '#loading': loading, '#download-progress': downloadProgress, '#download-fill': downloadFill
+ })[selector] ?? canvas},
fetch: async url => {
assert.equal(url, '/wasm/game.wasm');
return response;
}
});
const html = fs.readFileSync(new URL('../browser/index.html', import.meta.url), 'utf8');
- const script = html.match(/<script>([\s\S]*?)<\/script>/)[1];
+ const script = html.match(/<script>([\s\S]*?)<\/script>/)[1]
+ .replace('__WASM_DOWNLOAD_MB__', '11.57').replace('__WASM_DECODED_BYTES__', '65549');
vm.runInContext(script, context);
- return {context, progress, loading};
+ return {context, progress, loading, downloadProgress, percentages, widths};
}
async function streamsDecodedClientAndTracksProgress() {
@@ -44,10 +54,30 @@ async function streamsDecodedClientAndTracksProgress() {
assert.ok(instance instanceof WebAssembly.Instance);
assert.ok(module instanceof WebAssembly.Module);
- assert.deepEqual(test.progress, ['Preparing client... 0.0 MB', 'Preparing client... 0.1 MB']);
+ assert.deepEqual(test.progress, [
+ 'Downloading Anarchy Online client...\n0.00 / 11.57 MB',
+ 'Downloading Anarchy Online client...\n5.78 / 11.57 MB',
+ 'Downloading Anarchy Online client...\n11.57 / 11.57 MB',
+ 'Starting Anarchy Online...'
+ ]);
+ assert.deepEqual(test.percentages, [50, 100]);
+ assert.equal(Math.round(parseFloat(test.widths[0])), 50);
+ assert.equal(test.widths[1], '100%');
+ assert.equal(test.downloadProgress.hidden, true);
assert.equal(test.loading.hidden, true);
}
+function hidesProgressWhenDownloadStops() {
+ const test = fixture();
+ test.downloadProgress.hidden = false;
+ test.context.console = {error() {}};
+
+ test.context.Module.onAbort('connection lost');
+
+ assert.equal(test.downloadProgress.hidden, true);
+ assert.deepEqual(test.progress, ['Stopped: connection lost']);
+}
+
async function rejectsFailedClientDownload() {
const test = fixture(new Response('missing', {status: 404}));
@@ -119,6 +149,7 @@ async function startsWithMainThreadGraphicsWithoutOffscreenCanvas() {
}
await streamsDecodedClientAndTracksProgress();
+hidesProgressWhenDownloadStops();
await rejectsFailedClientDownload();
await reportsMissingWebGL();
await reportsUnavailableWebGLOnBothThreadsBeforeDownloading();
diff --git a/tools/client/publish_wasm.py b/tools/client/publish_wasm.py
@@ -33,6 +33,12 @@ if not compressed.exists() or not stamp.exists() or stamp.read_text().strip() !=
stamp.write_text(module_hash + '\n')
shutil.copyfile(compressed, staging / 'game.wasm.br')
+# Fill the loading page from this bundle's exact compressed and decoded sizes.
+page = (staging / 'index.html').read_text()
+page = page.replace('__WASM_DOWNLOAD_MB__', f'{compressed.stat().st_size / 1000000:.2f}')
+page = page.replace('__WASM_DECODED_BYTES__', str((source / 'game.wasm').stat().st_size))
+(staging / 'index.html').write_text(page)
+
# Record the exact inputs and published bytes, including uncommitted source edits.
inputs = subprocess.check_output(
['git', 'ls-files', 'src', 'tools/client/build_wasm_dependencies.py',
diff --git a/tools/client/test_publish_wasm.py b/tools/client/test_publish_wasm.py
@@ -65,6 +65,18 @@ class PublishWasmTests(unittest.TestCase):
self.assertEqual(brotli.decompress((self.root / 'server/assets/client/game.wasm.br').read_bytes()),
b'\0asm\1\0\0\0\0\1\0')
+ def test_fills_loading_page_with_bundle_sizes(self):
+ (self.web / 'index.html').write_text('__WASM_DOWNLOAD_MB__ MB; __WASM_DECODED_BYTES__ bytes')
+ (self.web / 'game.wasm.br').write_bytes(b'compressed fixture'.ljust(1234567, b'\0'))
+ (self.web / 'game.wasm.br.sha256').write_text(hashlib.sha256(b'\0asm\1\0\0\0').hexdigest())
+
+ self.publish()
+
+ self.assertEqual((self.root / 'server/assets/client/index.html').read_text(), '1.23 MB; 8 bytes')
+ metadata = json.loads((self.root / 'server/assets/client/build.json').read_text())
+ self.assertEqual(metadata['files']['index.html']['sha256'],
+ hashlib.sha256(b'1.23 MB; 8 bytes').hexdigest())
+
if __name__ == '__main__':
unittest.main()