Serve library thumbnails from the website

This commit is contained in:
waozi committed 2026-10-29 19:57:01 -03:00
1 parent e19fb2c761
commit 058ae8be0c
11 files changed
+538 -11

No files matched your search

+5 -2
View File
@@ -48,7 +48,7 @@ jobs:
node-version: 22
- name: Install build dependencies
working-directory: .
run: sh ziranlang/ziran/scripts/ci_packages.sh build-essential cmake pkg-config
run: sh ziranlang/ziran/scripts/ci_packages.sh build-essential cmake pkg-config python3-pil
- uses: mymindstorm/setup-emsdk@v14
with:
version: '6.0.6'
@@ -68,12 +68,15 @@ jobs:
-q '[.[] | select(.isDraft | not) | .tagName | select(startswith("node-arcade-"))][0]')
test -n "$tag"
mkdir -p build/published
gh release download "$tag" --repo waozixyz/arcade --pattern arcade.zx --dir build/published
# Its game archive holds the library art the site serves itself.
gh release download "$tag" --repo waozixyz/arcade --pattern arcade.zx \
--pattern arcade.games.tar --dir build/published
# The Apps library that release packs, by the digest its own commit trusts.
git show "$tag:apps-controller.sha256" > build/published/apps-controller.sha256
echo "version=${tag#node-arcade-}" >> "$GITHUB_OUTPUT"
- name: Build the browser edition
run: |
python3 tests/web_library_art_test.py
python3 scripts/build-entity.py
python3 scripts/build-apps.py --output build/apps-ui
python3 scripts/build-web.py --bundle build/published/arcade.zx \
+1
View File
@@ -1,5 +1,6 @@
/build/
/backup/
/work/
/ziran.local.toml
__pycache__/
*.pyc
+1
View File
@@ -3,6 +3,7 @@
## [Unreleased]
- Arcade is available as an Android app that plays every game offline, in normal, F-Droid and Google Play editions.
- On arcade.waozi.xyz the game covers appear sooner, as smaller WebP pictures from the site itself.
## 0.7.109
+1
View File
@@ -61,6 +61,7 @@ test: all
python3 tests/publisher_test.py
python3 tests/apps_controller_test.py
python3 tests/startup_art_test.py
python3 tests/web_library_art_test.py
./build/arcade --list
sh tests/chess_engine_zi_test.sh '$(ZIRAN)'
sh tests/chess_app_test.sh '$(ZIRAN)'
+139 -1
View File
@@ -12,19 +12,155 @@ Shovey download's own shovey.zx, shovey.json and entity.zx, unchanged, on the
page files --site names. Their programs, and the game and Apps controller
packed in shovey.zx, are compiled into the page, so they run as fast as a
native build of them; the page still opens those exact bytes.
The library's pictures come from the site too (stage_web_library_art), so
the covers never wait for the art bundle from api.waozi.xyz.
"""
import argparse
import gzip
import hashlib
import io
import json
import os
from pathlib import Path
import re
import shutil
import struct
import subprocess
import sys
import tarfile
ROOT = Path(__file__).resolve().parents[1]
ZIRAN = str(ROOT / 'scripts/ziran.sh')
ART_LIMIT = 64 * 1024 * 1024
# The committed library copies are at most this large (scripts/library-art.py).
PICTURE_LONGEST = 640
COVER_QUALITY = 80
def bundle_assets(data):
"""The files embedded in a ZX v26 bundle by path; never runs it."""
if data[:8] != b'ZX\0\0\x1a\0\0\0' or len(data) > ART_LIMIT:
raise ValueError('unsupported bundle')
at = 8
def take(size):
nonlocal at
if at + size > len(data):
raise ValueError('truncated bundle')
value = data[at:at + size]
at += size
return value
def number():
return struct.unpack('<I', take(4))[0]
def text():
return take(number())
text(); text()
for _ in range(number()):
text(); text()
for _ in range(number()):
for _ in range(7):
text()
take(8); text(); take(4)
for _ in range(number()):
for _ in range(3):
text()
take(number())
assets = {}
for _ in range(number()):
path = text().decode()
if path in assets:
raise ValueError('duplicate bundle file')
assets[path] = take(number())
if at != len(data):
raise ValueError('trailing bundle bytes')
return assets
def published_art(folder, digest):
"""The art bundle a collection names, from the release's game archive
beside it or a local build's arcade.games folder."""
built = folder / 'arcade.games' / f'{digest}.zx'
if built.is_file():
return built.read_bytes()
archive = folder / 'arcade.games.tar'
if archive.is_file():
with tarfile.open(archive) as games:
member = next((entry for entry in games if entry.name == f'{digest}.zx'), None)
if member is not None and member.isfile() and member.size <= ART_LIMIT:
return games.extractfile(member).read()
raise ValueError('The collection\'s art bundle is not beside it')
def web_picture(data, exact):
"""(bytes, suffix) of the page's copy of one verified library picture: a
WebP, lossless when exact (pixel-art previews), unless the original is
smaller. Transparency is kept; nothing is larger than PICTURE_LONGEST."""
from PIL import Image
image = Image.open(io.BytesIO(data))
image.load()
original = '.' + image.format.lower().replace('jpeg', 'jpg')
alpha = 'A' in image.getbands() or 'transparency' in image.info
image = image.convert('RGBA' if alpha else 'RGB')
scale = min(1.0, PICTURE_LONGEST / max(image.size))
if scale < 1.0:
size = (max(1, round(image.width * scale)), max(1, round(image.height * scale)))
image = image.resize(size, Image.Resampling.LANCZOS)
output = io.BytesIO()
if exact:
image.save(output, 'WEBP', lossless=True, method=6)
else:
image.save(output, 'WEBP', quality=COVER_QUALITY, method=6)
if scale == 1.0 and len(data) <= output.tell():
return data, original
return output.getvalue(), '.webp'
def stage_web_library_art(bundle, output):
"""Serve the collection's separate library art from the site.
The art bundle must have the exact digest and size the collection's
signed catalog names. Its pictures go to output/library as small WebP
copies, named by content and listed in library.json under that digest;
the exact bundle goes beside them for browsers that cannot draw WebP.
Returns the listing, or None when the collection packs its own art.
"""
assets = bundle_assets(bundle.read_bytes())
catalog = json.loads(assets['assets/apps/catalog.json'])
art = catalog.get('art')
if art is None:
return None
digest, size = art.get('sha256', ''), art.get('bytes')
if not re.fullmatch(r'[0-9a-f]{64}', digest) or not isinstance(size, int):
raise ValueError('Invalid art in the collection catalog')
data = published_art(bundle.parent, digest)
if len(data) != size or hashlib.sha256(data).hexdigest() != digest:
raise ValueError('The art bundle differs from the collection catalog')
pictures = bundle_assets(data)
previews = {child[key] for child in catalog['children'] for key in ('preview', 'icon') if child.get(key)}
covers = {path for child in catalog['children'] for key, path in child.get('cover', {}).items()
if key in ('portrait', 'landscape', 'logo')}
copies = {}
for path in sorted(previews | covers):
if path not in pictures:
raise ValueError(f'The art bundle lacks {path}')
copies[path] = web_picture(pictures[path], path in previews)
library = output / 'library'
if library.exists():
shutil.rmtree(library)
library.mkdir(parents=True)
(library / f'{digest}.zx').write_bytes(data)
listed = {}
for path, (contents, suffix) in copies.items():
name = hashlib.sha256(contents).hexdigest()
(library / (name + suffix)).write_bytes(contents)
listed[path] = dict(file=f'library/{name}{suffix}', sha256=name, bytes=len(contents))
manifest = dict(art=digest, bytes=size, pictures=listed)
(output / 'library.json').write_text(json.dumps(manifest, indent=2) + '\n')
return manifest
def package(name):
@@ -47,7 +183,7 @@ def version_assets(output, receipt):
if '__ARCADE_ASSET_REVISION__' not in html:
return
digest = hashlib.sha256()
for name in ('runtime.js', 'runtime.wasm', 'runtime.data', 'arcade.json', 'apps.zx'):
for name in ('runtime.js', 'runtime.wasm', 'runtime.data', 'arcade.json', 'apps.zx', 'library.json'):
path = output / name
if path.is_file():
digest.update(name.encode() + b'\0')
@@ -114,6 +250,8 @@ def main():
compress(output / 'apps.zx')
else:
print('No matching Apps controller beside the collection; it must pack its own')
if not args.shovey and stage_web_library_art(bundle, output) is None:
print('The collection packs its own library art')
version_assets(output, receipt)
print(f'{"Shovey" if args.shovey else "Arcade"} {args.version} for the browser in {output}')
+58 -4
View File
@@ -92,6 +92,58 @@
.pipeThrough(new DecompressionStream('gzip'))).arrayBuffer();
}).catch(function () { return plain(name); });
}
function hex(buffer) {
return Array.prototype.map.call(new Uint8Array(buffer), function (byte) {
return (byte < 16 ? '0' : '') + byte.toString(16);
}).join('');
}
function file(name) {
return fetch(assetUrl(name)).then(function (response) {
if (!response.ok) throw new Error(response.status);
return response.arrayBuffer();
});
}
// The library's pictures come from this site, never api.waozi.xyz:
// small copies of the collection's signed art (library.json), each
// checked against its digest, or the exact art bundle for a browser
// that cannot draw WebP or check them. The host verifies that bundle.
function library() {
return fetch(assetUrl('library.json')).then(function (response) {
return response.ok ? response.json() : null;
}).then(function (listing) {
if (!listing) return null;
var paths = Object.keys(listing.pictures);
return Promise.all(paths.map(function (path) {
var picture = listing.pictures[path];
return file(picture.file).then(function (bytes) {
return crypto.subtle.digest('SHA-256', bytes).then(function (digest) {
if (hex(digest) !== picture.sha256) throw new Error(path);
return bytes;
});
});
})).then(function (copies) {
var webp = paths.findIndex(function (path) { return /\.webp$/.test(listing.pictures[path].file); });
var drawn = webp < 0 ? Promise.resolve() : createImageBitmap(new Blob([copies[webp]])).then(function (bitmap) { bitmap.close(); });
return drawn.then(function () { return { art: listing.art, paths: paths, copies: copies }; });
}).catch(function () {
return file('library/' + listing.art + '.zx').then(function (bundle) {
return { art: listing.art, bundle: bundle };
});
});
}).catch(function () { return null; });
}
function writeLibrary(pictures) {
if (pictures.bundle) {
FS.mkdirTree('/packages');
FS.writeFile('/packages/' + pictures.art + '.zx', new Uint8Array(pictures.bundle));
return;
}
pictures.paths.forEach(function (path, index) {
var name = '/library/' + pictures.art + '/' + path;
FS.mkdirTree(name.slice(0, name.lastIndexOf('/')));
FS.writeFile(name, new Uint8Array(pictures.copies[index]));
});
}
// The build receipt names the collection's digest and its trusted Apps
// library; Arcade's own copy of that library goes in beside it.
Promise.all([
@@ -100,12 +152,14 @@
return response.text();
}),
bundle('apps.zx'),
bundle('runtime.data')
bundle('runtime.data'),
library()
]).then(function (loaded) {
Module.arcadeReceipt = loaded[0];
if (loaded[1]) {
Module.preRun = [function () { FS.writeFile('/apps.zx', new Uint8Array(loaded[1])); }];
}
Module.preRun = [function () {
if (loaded[1]) FS.writeFile('/apps.zx', new Uint8Array(loaded[1]));
if (loaded[3]) writeLibrary(loaded[3]);
}];
// The runtime's file package, already here, is not fetched again.
if (loaded[2]) {
Module.getPreloadedPackage = function () { return loaded[2]; };
+17
View File
@@ -25,6 +25,9 @@ CollectionPackage :: struct {
collection_packages: *CollectionPackage;
collection_package_result: string;
collection_package_files: *AppFiles;
// Packages the platform already holds, read-only and verified like a
// download: the website's own copy of the library art. "" for none.
collection_package_folder: string;
package_c :: #system_library "c";
PackageFree :: (memory: *void) #foreign package_c "free";
CollectionDownloadsOpen :: (files: *AppFiles) { collection_package_files = files }
@@ -61,6 +64,20 @@ CollectionPackageFor :: (entry: *AppEntry) -> *CollectionPackage {
package.bytes = ""
package.failed = true
}
if package.bytes == "" && collection_package_folder != "" {
held: Vec(u8)
BuilderPrint(*held, "%/%.zx", collection_package_folder, TextUntilNul(package.digest[:]))
path := BuilderFinish(held)
package.bytes = CollectionReadPackage(path)
AppFileFree(path)
if package.bytes != "" && CollectionPackageValid(package) {
package.failed = false
} else if package.bytes != "" {
AppFileFree(package.bytes)
package.bytes = ""
package.failed = true
}
}
package.next = collection_packages
collection_packages = package
return package
+35 -2
View File
@@ -303,14 +303,47 @@ CollectionCall :: (context: *void, node: *AppNode, module: *u8, function: *u8,
return 0
}
// The website's small copies of a collection's separate library art, under
// its signed catalog's art digest. The page writes only copies whose digests
// match its library.json (scripts/build-web.py); "" on other hosts.
collection_thumbnails: string;
CollectionThumbnail :: (node: *AppNode, path: string) -> string {
if collection_thumbnails == "" || node == null || !node.collection ||
node.catalog.art_sha256[0] == 0 || !AppAssetPathValid(path) { return "" }
copy: Vec(u8)
BuilderPrint(*copy, "%/%/%", collection_thumbnails, TextUntilNul(node.catalog.art_sha256[:]), path)
thumbnail := BuilderFinish(copy)
file := OpenRead(thumbnail)
if file.valid { unused CloseFile(file); return thumbnail }
AppFileFree(thumbnail)
return ""
}
// The website names where its page writes the library's pictures: small
// copies, or the exact art bundle for a browser that cannot draw them.
CollectionWebLibrary :: (thumbnails: string, packages: string) {
collection_thumbnails = thumbnails
collection_package_folder = packages
}
CollectionAssetAllowed :: (path: string) -> bool {
return path == "" || (collection_widget_node != null && (AppHostAsset(collection_widget_node, path).found ||
AppHostAssetPending(collection_widget_node, path)))
if path == "" { return true }
if collection_widget_node == null { return false }
// Checked first: asking for the art itself starts its download.
thumbnail := CollectionThumbnail(collection_widget_node, path)
if thumbnail != "" { AppFileFree(thumbnail); return true }
return AppHostAsset(collection_widget_node, path).found || AppHostAssetPending(collection_widget_node, path)
}
CollectionAssetPath :: (path: string) -> string {
if path == "" { return "" }
if collection_widget_node == null { return "" }
thumbnail := CollectionThumbnail(collection_widget_node, path)
if thumbnail != "" {
if !VecPush(collection_frame_paths, thumbnail) { AppFileFree(thumbnail); return "" }
return thumbnail
}
asset := AppHostAsset(collection_widget_node, path)
if !asset.found { collection_pictures_missing = true; return "" }
relative := Join(TextUntilNul(collection_widget_node.entry.sha256[:]), path)
+3
View File
@@ -82,6 +82,9 @@ main :: () -> s32 {
controller, controller_found := ReadEntireFile("/apps.zx")
if controller_found { collection_controller = controller } else { AppFileFree(controller) }
if !found || !profile_found || receipt == "" { return 1 }
// The page writes the library's pictures from this site: small copies
// under /library, or else the exact art bundle under /packages.
CollectionWebLibrary("/library", "/packages")
shovey := CollectionShoveyReceipt(receipt)
opened := ifx shovey then CollectionOpenShoveyBytes(bytes, receipt, data) else CollectionOpenBytes(bytes, receipt, data)
if !opened { return 1 }
+191
View File
@@ -0,0 +1,191 @@
"""The website serves small verified copies of the collection's library art."""
import hashlib
import importlib.util
import io
import json
from pathlib import Path
import struct
import tarfile
import tempfile
import unittest
from PIL import Image
ROOT = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location('build_web', ROOT / 'scripts/build-web.py')
web = importlib.util.module_from_spec(spec)
spec.loader.exec_module(web)
def bundle(files):
"""A minimal ZX v26 bundle: empty program sections, then its files."""
def text(value):
return struct.pack('<I', len(value)) + value
data = b'ZX\0\0\x1a\0\0\0' + text(b'art') + text(b'Main') + struct.pack('<IIII', 0, 0, 0, 0)
data += struct.pack('<I', len(files))
for path in sorted(files):
data += text(path.encode()) + text(files[path])
return data
def png(image):
output = io.BytesIO()
image.save(output, 'PNG', optimize=True)
return output.getvalue()
def jpeg(image):
output = io.BytesIO()
image.save(output, 'JPEG', quality=85)
return output.getvalue()
def gradient(width, height):
image = Image.new('RGB', (width, height))
image.putdata([(x * 255 // width, y * 255 // height, (x * y) % 256)
for y in range(height) for x in range(width)])
return image
def decoded(data):
image = Image.open(io.BytesIO(data))
image.load()
return image
class WebLibraryArtTests(unittest.TestCase):
def setUp(self):
work = ROOT / 'build/scratch'
work.mkdir(parents=True, exist_ok=True)
self.temporary = tempfile.TemporaryDirectory(prefix='web-art-', dir=work)
self.root = Path(self.temporary.name)
self.published = self.root / 'published'
self.published.mkdir()
self.site = self.root / 'site'
self.site.mkdir()
# A photographic cover, larger than any library copy should be.
self.portrait = jpeg(gradient(800, 1200))
self.landscape = jpeg(gradient(640, 427))
logo = gradient(512, 180).convert('RGBA')
logo.putalpha(Image.new('L', logo.size, 0))
logo.paste(gradient(300, 100), (100, 40))
self.logo = png(logo)
# Pixel art with a palette, which must stay exact.
pixels = Image.new('P', (48, 32))
pixels.putpalette([value for index in range(16) for value in (index * 16, 255 - index * 16, index * 8)])
pixels.putdata([(x // 4 + y // 4) % 16 for y in range(32) for x in range(48)])
self.preview = png(pixels)
self.art = bundle({
'assets/arcade/covers/wurm-portrait.jpg': self.portrait,
'assets/arcade/covers/wurm-landscape.jpg': self.landscape,
'assets/arcade/covers/wurm-logo.png': self.logo,
'assets/arcade/previews/wurm.png': self.preview,
})
def tearDown(self):
self.temporary.cleanup()
def collection(self, art=None, archive=True):
art = self.art if art is None else art
catalog = dict(schema=1, id='inbe.arcade', title='Arcade', harness='assets/apps/apps.zx',
art=dict(sha256=hashlib.sha256(self.art).hexdigest(), bytes=len(self.art)),
children=[dict(id='xyz.waozi.wurm', title='Apfelwurm',
preview='assets/arcade/previews/wurm.png',
icon='assets/arcade/previews/wurm.png',
cover=dict(portrait='assets/arcade/covers/wurm-portrait.jpg',
landscape='assets/arcade/covers/wurm-landscape.jpg',
logo='assets/arcade/covers/wurm-logo.png',
caption='Play'))])
path = self.published / 'arcade.zx'
path.write_bytes(bundle({'assets/apps/catalog.json': json.dumps(catalog).encode()}))
digest = catalog['art']['sha256']
if archive:
# The release's game archive, as the website workflow downloads it.
with tarfile.open(self.published / 'arcade.games.tar', 'w') as games:
for name, data in ((f'{digest}.zx', art), ('0' * 64 + '.zx', b'ZX\0\0game')):
entry = tarfile.TarInfo(name)
entry.size = len(data)
games.addfile(entry, io.BytesIO(data))
else:
(self.published / 'arcade.games').mkdir()
(self.published / 'arcade.games' / f'{digest}.zx').write_bytes(art)
return path, digest
def test_small_webp_copies_keep_alpha_pixels_and_the_exact_art(self):
path, digest = self.collection()
listing = web.stage_web_library_art(path, self.site)
self.assertEqual(listing['art'], digest)
self.assertEqual(json.loads((self.site / 'library.json').read_text()), listing)
# The exact signed bundle stays beside the copies, for the fallback.
self.assertEqual((self.site / 'library' / f'{digest}.zx').read_bytes(), self.art)
pictures = listing['pictures']
self.assertEqual(set(pictures), {'assets/arcade/covers/wurm-portrait.jpg',
'assets/arcade/covers/wurm-landscape.jpg',
'assets/arcade/covers/wurm-logo.png',
'assets/arcade/previews/wurm.png'})
copies = {}
for name, picture in pictures.items():
data = (self.site / picture['file']).read_bytes()
self.assertEqual(hashlib.sha256(data).hexdigest(), picture['sha256'])
self.assertEqual(len(data), picture['bytes'])
self.assertEqual(picture['file'], f'library/{picture["sha256"]}' + Path(picture['file']).suffix)
copies[name] = data
portrait = decoded(copies['assets/arcade/covers/wurm-portrait.jpg'])
self.assertEqual(portrait.format, 'WEBP')
self.assertEqual(portrait.size, (427, 640))
landscape = copies['assets/arcade/covers/wurm-landscape.jpg']
self.assertEqual(decoded(landscape).format, 'WEBP')
self.assertLess(len(landscape), len(self.landscape))
logo = decoded(copies['assets/arcade/covers/wurm-logo.png'])
self.assertEqual((logo.format, logo.mode, logo.size), ('WEBP', 'RGBA', (512, 180)))
self.assertEqual(logo.getpixel((5, 5))[3], 0)
self.assertEqual(logo.getpixel((200, 80))[3], 255)
preview = copies['assets/arcade/previews/wurm.png']
if decoded(preview).format == 'WEBP':
self.assertLess(len(preview), len(self.preview))
self.assertEqual(decoded(preview).convert('RGB').tobytes(),
decoded(self.preview).convert('RGB').tobytes())
else:
# WebP is no smaller: the verified original goes as it is.
self.assertEqual(preview, self.preview)
def test_a_local_build_folder_also_supplies_the_art(self):
path, digest = self.collection(archive=False)
self.assertEqual(web.stage_web_library_art(path, self.site)['art'], digest)
def test_corrupt_or_resized_art_is_refused_before_anything_is_written(self):
corrupt = bytearray(self.art)
corrupt[-1] ^= 1
for art in (bytes(corrupt), self.art + b'\0'):
path, _ = self.collection(art)
with self.assertRaisesRegex(ValueError, 'differs from the collection catalog'):
web.stage_web_library_art(path, self.site)
self.assertFalse((self.site / 'library.json').exists())
self.assertFalse((self.site / 'library').exists())
def test_missing_art_or_pictures_are_refused(self):
path, _ = self.collection()
(self.published / 'arcade.games.tar').unlink()
with self.assertRaisesRegex(ValueError, 'not beside it'):
web.stage_web_library_art(path, self.site)
self.art = bundle({'assets/arcade/previews/wurm.png': self.preview})
path, _ = self.collection()
with self.assertRaisesRegex(ValueError, 'lacks assets/arcade/covers'):
web.stage_web_library_art(path, self.site)
self.assertFalse((self.site / 'library.json').exists())
def test_a_collection_that_packs_its_art_needs_no_copies(self):
catalog = dict(schema=1, id='inbe.arcade', children=[])
path = self.published / 'arcade.zx'
path.write_bytes(bundle({'assets/apps/catalog.json': json.dumps(catalog).encode()}))
self.assertIsNone(web.stage_web_library_art(path, self.site))
self.assertEqual(list(self.site.iterdir()), [])
def test_truncated_or_trailing_bundles_are_refused(self):
for data in (self.art[:-3], self.art + b'x', b'PK' + self.art[2:]):
with self.assertRaises(ValueError):
web.bundle_assets(data)
if __name__ == '__main__':
unittest.main()
+87 -2
View File
@@ -1,5 +1,7 @@
// Open Arcade's browser edition in a private headless browser and require
// install, play, reopen and recover from a failed saved-record read.
// install, play, reopen and recover from a failed saved-record read. The
// library's art never comes from api.waozi.xyz: those requests are refused,
// and the covers must draw from the site's own copies or its exact art.
// Usage: node tests/web_site_test.mjs SITE_DIR
import { spawn } from 'node:child_process';
import { createServer } from 'node:http';
@@ -12,7 +14,13 @@ const site = process.argv[2];
const game = process.env.ARCADE_TEST_GAME || 'Apfelwurm';
if (!site) throw new Error('Usage: node tests/web_site_test.mjs SITE_DIR');
const types = { '.html': 'text/html', '.js': 'text/javascript', '.wasm': 'application/wasm', '.json': 'application/json',
'.svg': 'image/svg+xml', '.png': 'image/png', '.jpg': 'image/jpeg', '.webmanifest': 'application/manifest+json' };
'.svg': 'image/svg+xml', '.png': 'image/png', '.jpg': 'image/jpeg', '.webp': 'image/webp',
'.webmanifest': 'application/manifest+json' };
const library = JSON.parse(await readFile(join(site, 'library.json'), 'utf8').catch(() => 'null'));
// 'site' serves the library as built; 'corrupt' damages every small copy;
// 'missing' also withholds the exact art bundle.
let libraryMode = 'site';
const served = { pictures: 0, art: 0 }, refusedArt = [];
const server = createServer(async (request, response) => {
const url = new URL(request.url, 'http://x');
const path = normalize(decodeURIComponent(url.pathname)).replace(/^\/+/, '') || 'index.html';
@@ -25,6 +33,15 @@ const server = createServer(async (request, response) => {
return;
}
let body = await readFile(join(site, path));
if (library && path.startsWith('library/')) {
if (path.endsWith('.zx')) {
served.art++;
if (libraryMode === 'missing') throw new Error('withheld');
} else {
served.pictures++;
if (libraryMode !== 'site') body[body.length - 1] ^= 1;
}
}
if (path === 'index.html') {
// Enable only the existing widget-tree reader in this disposable page.
// Input still travels through the browser's real pointer and key events.
@@ -69,6 +86,15 @@ try {
await new Promise((resolve, reject) => { socket.onopen = resolve; socket.onerror = reject; });
socket.onmessage = event => {
const message = JSON.parse(event.data);
if (message.method === 'Fetch.requestPaused') {
const { requestId, request } = message.params;
const refuse = library && request.url.includes(library.art);
if (refuse) refusedArt.push(request.url);
socket.send(JSON.stringify(refuse
? { id: ++sequence, method: 'Fetch.failRequest', params: { requestId, errorReason: 'BlockedByClient' } }
: { id: ++sequence, method: 'Fetch.continueRequest', params: { requestId } }));
return;
}
if (message.method === 'Runtime.consoleAPICalled')
console_lines.push(message.params.args.map(arg => arg.value ?? arg.description ?? '').join(' '));
if (message.method === 'Runtime.exceptionThrown')
@@ -80,6 +106,8 @@ try {
message.error ? request.reject(new Error(JSON.stringify(message.error))) : request.resolve(message.result);
};
await command('Runtime.enable'); await command('Page.enable');
// Games still download from the API; only its art requests are refused.
await command('Fetch.enable', { patterns: [{ urlPattern: '*://api.waozi.xyz/*' }] });
await command('Emulation.setDeviceMetricsOverride', { width: 1280, height: 800, deviceScaleFactor: 1, mobile: false });
await command('Page.navigate', { url: `http://127.0.0.1:${server.address().port}/` });
let ready = false;
@@ -157,6 +185,43 @@ try {
}
throw new Error('The library did not reopen');
};
// The covers' own pixels: art has far more colours than an empty tile.
const coverColours = async () => {
const covers = (await tree()).filter(node => node.kind === 'button' && node.visible &&
node.visible_width >= 100 && node.visible_height >= 100);
return evaluate(`(() => {
const canvas = document.getElementById('canvas'), context = canvas.getContext('2d');
const scale = canvas.width / canvas.clientWidth;
return ${JSON.stringify(covers)}.map(node => {
const pixels = context.getImageData(Math.round(node.visible_x * scale), Math.round(node.visible_y * scale),
Math.round(node.visible_width * scale), Math.round(node.visible_height * scale)).data;
const colours = new Set();
for (let at = 0; at < pixels.length; at += 8) colours.add(pixels[at] << 16 | pixels[at + 1] << 8 | pixels[at + 2]);
return colours.size;
});
})()`);
};
const DRAWN = 1500;
const coversDrawn = async () => {
let colours = [];
for (const end = Date.now() + 15000; Date.now() < end;) {
colours = await coverColours();
if (colours.length && colours.every(count => count >= DRAWN)) return colours;
await delay(250);
}
throw new Error('The library covers did not draw: ' + JSON.stringify(colours));
};
const libraryFetched = async () => evaluate(`performance.getEntriesByType('resource').map(entry => entry.name)
.filter(name => /\\/library\\//.test(new URL(name).pathname) || name.includes(${JSON.stringify(library?.art || '-')}))`);
if (library) {
// The first visit draws every cover from the small copies alone.
const colours = await coversDrawn();
const fetched = await libraryFetched();
if (served.art || refusedArt.length || served.pictures !== Object.keys(library.pictures).length ||
fetched.some(name => !/\.webp\?v=/.test(name) && !Object.values(library.pictures).some(picture => name.includes(picture.file))))
throw new Error('The covers did not come from the small copies alone: ' + JSON.stringify({ served, refusedArt, fetched }));
console.log(`Covers drew from ${served.pictures} small copies (${Object.values(library.pictures).reduce((sum, picture) => sum + picture.bytes, 0)} bytes, art ${library.bytes} bytes); colours ${colours}`);
}
const cover = button(await tree(), game);
if (!cover) throw new Error('Game cover missing');
await click(cover);
@@ -240,6 +305,26 @@ try {
await waitButton('Play');
console.log('The reported ReadLevel failure showed recovery; returning reopened the installed game and retained its saved record');
}
if (library) {
// Damaged copies are refused; the exact art from the site draws instead.
libraryMode = 'corrupt'; served.pictures = served.art = 0;
await command('Page.reload');
await pageReady();
const colours = await coversDrawn();
if (served.art !== 1 || refusedArt.length)
throw new Error('Damaged copies did not fall back to the site\'s exact art: ' + JSON.stringify({ served, refusedArt }));
console.log(`Damaged copies were refused and the site's exact art drew the covers; colours ${colours}`);
// Without the site's art the covers stay empty and Arcade asks the API.
libraryMode = 'missing';
await command('Page.reload');
await pageReady();
await delay(3000);
const empty = await coverColours();
if (!refusedArt.length || !empty.length || empty.some(count => count >= DRAWN))
throw new Error('The cover check did not tell empty covers apart: ' + JSON.stringify({ empty, refusedArt }));
console.log(`Without the site's art the covers stayed empty (colours ${empty}) and the API art request was refused`);
libraryMode = 'site';
}
}
const failed = console_lines.slice(expectedDiagnostics).find(line => /frame failed|portable execution failed/.test(line));
if (!failure && failed) failure = failed;