// Sprite delivery contract. The atlas, frame names, tile declarations and playback order say // which files ought to exist. The reader must find every one and reject unexpected files. import assert from 'node:assert/strict'; import { buildAtlas, inspectProject, renderProject, exportsStill } from '../src/core.js'; import { autotileFrames } from '../src/autotile.js'; import { createBundle } from '../src/export.js'; import { encodePNG } from '../src/png.js'; import { normalsYUp } from '../src/godot.js'; import { compileAction } from '../src/actions.js'; import { engineInputs } from './read-delivery-engines.mjs'; import { sha, still } from './example-delivery.mjs'; import { checkEdit, checkImport } from './round-trip-example.mjs'; import { spriteGIFs, numberedClip } from './example-playback.mjs'; export async function spriteDelivery(delivery, document, original, engines, id) { const compiled = document.format ? compileAction(document, { formatCheck: true, withTestMap: true }) : null; const recipe = compiled?.recipe ?? document; if (compiled?.metadata !== undefined) await delivery.put('compile/metadata.json', JSON.stringify(compiled.metadata)); else delivery.restrictions.set('compiler-metadata', 'The plain recipe or this compiler produces no metadata sidecar.'); if (document.format === 'pixelforge-autotile') { const map = compiled.testMap.sheet; await delivery.image('compile/test-map.png', encodePNG(map.data, map.width, map.height), still(map)); } else { assert.ok(!compiled?.testMap); delivery.restrictions.set('test-map', 'Only an autotile compiler draws a test map.'); } // An aligned, opaque normal pass exercises the optional normal/Y-up sheets even when the // example ships on its own. All frames have the same normal, including trimmed aliases. const normal = { version: 1, name: 'flat-normal', width: recipe.width, height: recipe.height, background: '#6080ff', frames: recipe.frames.map(f => ({ name: f.name, ops: [] })) }; const bundle = await createBundle(recipe, { normal }); const { project, atlas, files } = bundle, { width, height, name, sheet, frames } = project; const sheetName = name + '.png', pictures = { [sheetName]: still(atlas) }; const pixelSize = [width * sheet.scale, height * sheet.scale]; const samples = frames.map(f => ({ rgba: sha(f.data), scale: sheet.scale })); for (const [i, frame] of frames.entries()) pictures['frames/' + frame.name + '.png'] = { size: pixelSize, frames: [samples[i]] }; const normalProject = renderProject({ ...normal, sheet: { scale: 1 } }); const normals = buildAtlas({ ...normalProject, sheet }); pictures[name + '.normal.png'] = still(normals); pictures[name + '.normal_y_up.png'] = still({ ...normals, data: normalsYUp(normals.data) }); const tileSet = new Set(autotileFrames(recipe).map(f => f.mask)).size >= 2 || frames.some(f => f.tile); const animated = recipe.animations === undefined ? !tileSet : Object.keys(recipe.animations).length > 0; assert.equal(exportsStill(recipe, project), !animated); const expected = [sheetName, name + '.atlas.json', name + '.pixel.json', name + '.normal.png', name + '.normal_y_up.png', name + '.canvas.tres', 'preview.html', 'README.txt', ...frames.map(f => 'frames/' + f.name + '.png')]; if (animated) { expected.push(name + '.css', name + '.tres', 'player.js'); for (const [key, anim] of Object.entries(project.animations)) { const file = 'animations/' + key + '.png'; expected.push(file); pictures[file] = { size: pixelSize, frames: anim.frames.map(i => ({ ...samples[i], ms: Math.max(11, frames[i].duration) })), loop: anim.loop ? 0 : 1 }; } } else delivery.restrictions.set('animated-bundle', 'Stills and tile sets without named animations omit APNG, CSS, player and SpriteFrames.'); if (tileSet && !sheet.trim) expected.push(name + '.tileset.tres', name + '.tsx'); else delivery.restrictions.set('tile-resources', 'Engine tilesets require a tile declaration and an untrimmed grid.'); assert.deepEqual([...files.keys()].sort(), expected.sort(), name + ' bundle members'); assert.deepEqual(JSON.parse(files.get(name + '.pixel.json')), recipe); assert.deepEqual(Object.keys(JSON.parse(files.get(name + '.atlas.json')).frames), frames.map(f => f.name)); const resources = await engineInputs(engines, id, files); await delivery.bundle(files, pictures); const look = inspectProject(project).sheet; await delivery.image('look.png', encodePNG(look.data, look.width, look.height), still(look)); await delivery.interchange(original, recipe, files.get(sheetName), pictures[sheetName]); checkEdit(recipe, project, delivery.restrictions); resources.push(...await checkImport(delivery, recipe, bundle, engines, id)); await spriteGIFs(delivery, project, animated); await numberedClip(delivery, document, { project, plays: animated }); if (document.format === 'pixelforge-stack' && document.slices?.flat().every(row => typeof row === 'string' && /^\.+$/.test(row))) { assert.throws(() => compileAction(document, { slices: true }), /own slices are blank/); delivery.restrictions.set('stack-slices', 'A stack made entirely from parts has no occupied root slices.'); } else if (document.format === 'pixelforge-stack') { const slices = compileAction(document, { slices: true }); const cut = await createBundle(slices.recipe); resources.push(...await engineInputs(engines, id + '-slices', cut.files)); const file = cut.project.name + '.png'; await delivery.image('slices/' + file, cut.files.get(file), still(cut.atlas)); await delivery.put('slices/recipe.json', JSON.stringify(slices.recipe)); await delivery.put('slices/metadata.json', JSON.stringify(slices.metadata)); } else { assert.throws(() => compileAction(document, { slices: true }), /sprite stack/); delivery.restrictions.set('stack-slices', 'Only sprite stacks have slices.'); } return resources; }