| 1 | import { readdirSync, readFileSync } from "node:fs"; |
| 2 | import { extname, join } from "node:path"; |
| 3 | import { fileURLToPath } from "node:url"; |
| 4 | import test from "node:test"; |
| 5 | import assert from "node:assert/strict"; |
| 6 | |
| 7 | const SPRITE_SIZE = 16; |
| 8 | const ASSET_ROOT = join(fileURLToPath(new URL("..", import.meta.url)), "assets"); |
| 9 | const SPRITE_FOLDERS = ["grounds", "blocks", "characters", "items", "equipment", "mobs"]; |
| 10 | |
| 11 | function pngSize (buffer) |
| 12 | { |
| 13 | if (buffer.length < 24 || buffer.toString("ascii", 1, 4) !== "PNG") |
| 14 | { throw new Error("Not a PNG"); } |
| 15 | return { |
| 16 | width: buffer.readUInt32BE(16), |
| 17 | height: buffer.readUInt32BE(20) |
| 18 | }; |
| 19 | } |
| 20 | |
| 21 | function listPngs (dir) |
| 22 | { |
| 23 | const files = []; |
| 24 | for (const entry of readdirSync(dir, { withFileTypes: true })) |
| 25 | { |
| 26 | const path = join(dir, entry.name); |
| 27 | if (entry.isDirectory()) |
| 28 | { files.push(...listPngs(path)); } |
| 29 | else if (extname(entry.name).toLowerCase() === ".png") |
| 30 | { files.push(path); } |
| 31 | } |
| 32 | return files; |
| 33 | } |
| 34 | |
| 35 | test("every world sprite is exactly 16×16", () => |
| 36 | { |
| 37 | const offenders = []; |
| 38 | for (const folder of SPRITE_FOLDERS) |
| 39 | { |
| 40 | const dir = join(ASSET_ROOT, folder); |
| 41 | let files = []; |
| 42 | try |
| 43 | { files = listPngs(dir); } |
| 44 | catch (error) |
| 45 | { |
| 46 | if (error.code === "ENOENT") |
| 47 | { continue; } |
| 48 | throw error; |
| 49 | } |
| 50 | for (const file of files) |
| 51 | { |
| 52 | const { width, height } = pngSize(readFileSync(file)); |
| 53 | if (width !== SPRITE_SIZE || height !== SPRITE_SIZE) |
| 54 | { offenders.push(`${file} is ${width}×${height}`); } |
| 55 | } |
| 56 | } |
| 57 | assert.equal(offenders.length, 0, offenders.join("\n")); |
| 58 | }); |