From f207627b498b2b4ec6bcad692abc32e02cddfc1f Mon Sep 17 00:00:00 2001 From: Christopher Monsanto Date: Sun, 8 Nov 2026 22:07:13 -0500 Subject: [PATCH] Record each sprite's size in its set's manifest Co-Authored-By: Claude Opus 5.5 --- rules/publish.ts | 19 ++++++----- tools/deploy/api.ts | 35 ++++++++++++++++++++ tools/deploy/test/api.test.ts | 17 ++++++++++ tools/deploy/test/src/300x200-animated.webp | Bin 0 -> 156 bytes tools/deploy/test/src/7x5.gif | Bin 0 -> 46 bytes tools/deploy/test/src/9x4-lossless.webp | Bin 0 -> 36 bytes tools/deploy/test/src/9x4-lossy.webp | Bin 0 -> 64 bytes 7 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 tools/deploy/test/src/300x200-animated.webp create mode 100644 tools/deploy/test/src/7x5.gif create mode 100644 tools/deploy/test/src/9x4-lossless.webp create mode 100644 tools/deploy/test/src/9x4-lossy.webp diff --git a/rules/publish.ts b/rules/publish.ts index 0baa700c9..8316687fd 100644 --- a/rules/publish.ts +++ b/rules/publish.ts @@ -4,7 +4,7 @@ import * as nodePath from 'node:path'; import * as spritedata from '@smogon/sprite-data/index.ts'; import type {Artifact} from '../tools/build/artifact.ts'; -import type {DeployCtx, SrcFile} from '../tools/deploy/api.ts'; +import type {DeployCtx, Size, SrcFile} from '../tools/deploy/api.ts'; // Anything with a sprite-id nominal name and an extension: a built artifact // or a raw source file. @@ -21,16 +21,17 @@ export type Tree = { }; // A record of one copy this manifest made: where under the tree root the copy -// landed, and the un-stamped filename it answers to. +// landed, the un-stamped filename it answers to, and the picture's size. type Entry = { dir: string, stamped: string, filename: string, + size: Size, }; -// Holds the unhashed name -> served url mapping published beside a stamped set. -// Each entry's url is the tree's url plus the copy's place in it, so no -// consumer needs a prefix of its own. +// Holds the unhashed name -> served url and size mapping published beside a +// stamped set. Each entry's url is the tree's url plus the copy's place in it, +// so no consumer needs a prefix of its own. export class Manifest { #ctx: DeployCtx; #tree: Tree; @@ -54,7 +55,7 @@ export class Manifest { throw new Error(`duplicate sprite name ${name}`); } let stamped = `${name}-${h}.${e}`; - this.#entries.set(name, {dir, stamped, filename: `${name}.${e}`}); + this.#entries.set(name, {dir, stamped, filename: `${name}.${e}`, size: await this.#ctx.size(f)}); this.#ctx.copy(f, nodePath.posix.join(this.#tree.root, dir, stamped)); } @@ -69,9 +70,9 @@ export class Manifest { } write(dst: string): void { - let sorted: Record = {}; - for (let [k] of this.#sorted()) { - sorted[k] = this.url(k); + let sorted: Record = {}; + for (let [k, e] of this.#sorted()) { + sorted[k] = {url: this.url(k), ...e.size}; } this.#ctx.write(dst, JSON.stringify(sorted, null, 4) + '\n'); } diff --git a/tools/deploy/api.ts b/tools/deploy/api.ts index 17eb605ae..bb5a41f45 100644 --- a/tools/deploy/api.ts +++ b/tools/deploy/api.ts @@ -36,8 +36,37 @@ export type DeployCtx = { // of its bytes, byte-compatible with artifact hashes; for several, a digest // of the sorted per-source digests (order-insensitive). hash(...srcs: CopySource[]): Promise, + size(src: CopySource): Promise, }; +export type Size = {width: number, height: number}; + +// Reads the size a browser lays a PNG, GIF or WebP out at from its header: a +// GIF's is its logical screen, not its first frame's. +export function imageSize(head: Buffer, what: string): Size { + let ascii = (start: number, end: number) => head.toString('latin1', start, end); + if (ascii(1, 4) === 'PNG') { + return {width: head.readUInt32BE(16), height: head.readUInt32BE(20)}; + } + if (ascii(0, 3) === 'GIF') { + return {width: head.readUInt16LE(6), height: head.readUInt16LE(8)}; + } + if (ascii(0, 4) === 'RIFF' && ascii(8, 12) === 'WEBP') { + switch (ascii(12, 16)) { + case 'VP8 ': + return {width: head.readUInt16LE(26) & 0x3fff, height: head.readUInt16LE(28) & 0x3fff}; + case 'VP8L': { + let bits = head.readUInt32LE(21); + return {width: (bits & 0x3fff) + 1, height: ((bits >>> 14) & 0x3fff) + 1}; + } + case 'VP8X': + return {width: head.readUIntLE(24, 3) + 1, height: head.readUIntLE(27, 3) + 1}; + } + } + throw new Error(`${what} is not a PNG, GIF or WebP`); +} + + export type DeployFn = (ctx: DeployCtx) => void | Promise; // Like rule(): a buildFile registers free-floating deploy blocks next to the @@ -114,5 +143,11 @@ export function makeCtx(casDir: string, queue: ActionQueue): DeployCtx { } return shortHash(h.digest()); }, + async size(src: CopySource): Promise { + let path = srcPath(src); + await using f = await fs.open(path); + let {buffer} = await f.read({buffer: Buffer.alloc(30), position: 0}); + return imageSize(buffer, path); + }, }; } diff --git a/tools/deploy/test/api.test.ts b/tools/deploy/test/api.test.ts index a6e65caad..8d63a877d 100644 --- a/tools/deploy/test/api.test.ts +++ b/tools/deploy/test/api.test.ts @@ -250,3 +250,20 @@ test('publish refuses links in the tree, links to what it does not carry, and __ reserved.write('a', '__meta/__files'); await assert.rejects(() => reserved.publish(tree, meta, 'd'), /publish's own/); }); + +test('ctx.size reads each format the sets publish', async () => { + let ctx = makeCtx(pathlib.join(tmpdir(), 'cas'), new ActionQueue()); + let src = pathlib.join(import.meta.dirname, 'src'); + let sizes = await Promise.all(['32.png', '7x5.gif', '9x4-lossy.webp', '9x4-lossless.webp', '300x200-animated.webp'] + .map(name => ctx.size(pathlib.join(src, name)))); + assert.deepEqual(sizes, [ + {width: 20, height: 26}, + {width: 7, height: 5}, + {width: 9, height: 4}, + {width: 9, height: 4}, + {width: 300, height: 200}, + ]); + let other = pathlib.join(tmpdir(), 'x.png'); + fs.writeFileSync(other, ''); + await assert.rejects(() => ctx.size(other), /not a PNG, GIF or WebP/); +}); diff --git a/tools/deploy/test/src/300x200-animated.webp b/tools/deploy/test/src/300x200-animated.webp new file mode 100644 index 0000000000000000000000000000000000000000..86c3bc77b82cf277e01ddb224459a62e7c854a0f GIT binary patch literal 156 zcmWIYbaR`+z`zjh>J$(bU=hIuWHSM=HY3Av1_nnzPhU15kAdO;e-O{t%>c{;5fBwA tK;=MvKH@+TeeHvW4D14b`~R^sh`jt?-}R#Y)&J`_^)aC7yZ9eX9{}P3CZYfU literal 0 HcmV?d00001 diff --git a/tools/deploy/test/src/7x5.gif b/tools/deploy/test/src/7x5.gif new file mode 100644 index 0000000000000000000000000000000000000000..8cc836a5a4375a9d0f11b29015d7fe3960722fef GIT binary patch literal 46 rcmZ?wbhEHbWM^Py_`tyMp8*6Ef3kpS9S{kUVPIly>0h~Sr!U1`yOMg{