tools: test nginx, generated tile fixtures and the tile group (#24)
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
// Fixture map tiles, generated at test time (DESIGN 13 M3 "fixture tiles at 32 px generated at test
|
||||
// time by a helper (not committed)", slice 9 / #24). Zero npm deps: the PNG writer uses node:zlib.
|
||||
//
|
||||
// The fixture is deliberately boring art: each 32 px tile is one solid colour derived from its
|
||||
// coordinates, so a test can tell two tiles apart by their bytes and no image work is needed. Tiles
|
||||
// land under <stateDir>/tiles/<scene>/v<art_version>/, the tree the test nginx aliases for
|
||||
// /_tiles/ (3.3, 11.1).
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import zlib from 'node:zlib'
|
||||
|
||||
const CRC_TABLE = (() => {
|
||||
const table = new Int32Array(256)
|
||||
for (let n = 0; n < 256; n++) {
|
||||
let c = n
|
||||
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
|
||||
table[n] = c
|
||||
}
|
||||
return table
|
||||
})()
|
||||
|
||||
function crc32(buf) {
|
||||
let c = 0xffffffff
|
||||
for (let i = 0; i < buf.length; i++) c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8)
|
||||
return (c ^ 0xffffffff) >>> 0
|
||||
}
|
||||
|
||||
function chunk(type, data) {
|
||||
const head = Buffer.alloc(4)
|
||||
head.writeUInt32BE(data.length)
|
||||
const body = Buffer.concat([Buffer.from(type, 'latin1'), data])
|
||||
const crc = Buffer.alloc(4)
|
||||
crc.writeUInt32BE(crc32(body))
|
||||
return Buffer.concat([head, body, crc])
|
||||
}
|
||||
|
||||
// A solid-colour 8-bit RGB PNG of `size` x `size` (filter 0 on every scanline).
|
||||
export function pngBytes(size, rgb) {
|
||||
const ihdr = Buffer.alloc(13)
|
||||
ihdr.writeUInt32BE(size, 0)
|
||||
ihdr.writeUInt32BE(size, 4)
|
||||
ihdr[8] = 8 // bit depth
|
||||
ihdr[9] = 2 // colour type: truecolour (RGB)
|
||||
ihdr[10] = 0 // compression
|
||||
ihdr[11] = 0 // filter
|
||||
ihdr[12] = 0 // interlace
|
||||
const row = Buffer.alloc(1 + size * 3)
|
||||
for (let x = 0; x < size; x++) {
|
||||
row[1 + x * 3] = rgb[0]
|
||||
row[2 + x * 3] = rgb[1]
|
||||
row[3 + x * 3] = rgb[2]
|
||||
}
|
||||
const raw = Buffer.concat(Array.from({ length: size }, () => row))
|
||||
return Buffer.concat([
|
||||
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
||||
chunk('IHDR', ihdr),
|
||||
chunk('IDAT', zlib.deflateSync(raw, { level: 6 })),
|
||||
chunk('IEND', Buffer.alloc(0)),
|
||||
])
|
||||
}
|
||||
|
||||
export function isPng(buf) {
|
||||
return Buffer.isBuffer(buf) && buf.length > 8 &&
|
||||
buf.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))
|
||||
}
|
||||
|
||||
export function tileDir(stateDir, scene, artVersion) {
|
||||
return path.join(stateDir, 'tiles', String(scene), `v${artVersion}`)
|
||||
}
|
||||
|
||||
// Deterministic colour of a tile: distinct neighbours, stable across runs.
|
||||
function colourOf(x, y) {
|
||||
return [(x * 37 + y * 11) & 0xff, (x * 17 + y * 53) & 0xff, (x * 97 + y * 5) & 0xff]
|
||||
}
|
||||
|
||||
// Writes the given [x, y] tiles (default size 32 px) and returns the paths.
|
||||
export function writeTiles(stateDir, { scene = 1, artVersion = 1, size = 32, tiles }) {
|
||||
const dir = tileDir(stateDir, scene, artVersion)
|
||||
fs.mkdirSync(dir, { recursive: true })
|
||||
const files = []
|
||||
for (const [x, y] of tiles) {
|
||||
const file = path.join(dir, `${x}_${y}.png`)
|
||||
fs.writeFileSync(file, pngBytes(size, colourOf(x, y)))
|
||||
files.push(file)
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
// Writes every tile of the rectangle x0..x1 / y0..y1 (inclusive). The leak check enumerates the
|
||||
// same rectangle, so every tile it expects to be allowed has real bytes behind the internal alias.
|
||||
export function writeTileGrid(stateDir, { scene = 1, artVersion = 1, size = 32, x0, x1, y0, y1 }) {
|
||||
const tiles = []
|
||||
for (let y = y0; y <= y1; y++) for (let x = x0; x <= x1; x++) tiles.push([x, y])
|
||||
return writeTiles(stateDir, { scene, artVersion, size, tiles })
|
||||
}
|
||||
Reference in New Issue
Block a user