A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(ci): drive a package by pointer, not only by keyboard
esenginebf30f3833fe4
diff --git a/desktop/scripts/inputScript.mjs b/desktop/scripts/inputScript.mjsnew file mode 100644index 000000000..79698443c--- /dev/null+++ b/desktop/scripts/inputScript.mjs@@ -0,0 +1,67 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file inputScript.mjs — the gesture a launcher plays into a packaged game.+ *+ * Shared by launch-export and launch-minigame so a golden project's declared+ * input means one thing whichever package it is driving. Events are real DOM+ * events on the page the build owns: a package has no automation hook, and the+ * point is the path a player's hardware takes, not a way around it.+ *+ * Pointer positions are FRACTIONS of the surface, because the surface size is+ * chosen per project (it follows the editor's play panel) and a pixel target+ * would land somewhere else on every game.+ */++/**+ * JS source that plays `spec` and then lets the game run on.+ * `{ keys?: string[], pointer?: { x, y, action? }, frames?: number }`+ */+export function inputScript(spec) {+ const keys = JSON.stringify(spec.keys ?? []);+ const pointer = JSON.stringify(spec.pointer ?? null);+ const frames = Number(spec.frames ?? 40);+ return `+ (async () => {+ const raf = () => new Promise((r) => requestAnimationFrame(r));+ const keys = ${keys};+ const pointer = ${pointer};+ const fireKey = (type, code) => {+ const e = new KeyboardEvent(type, { key: code, code, bubbles: true, cancelable: true });+ window.dispatchEvent(e);+ document.dispatchEvent(e);+ };+ const canvas = document.querySelector('canvas');+ // Pointer AND mouse: engines bind one or the other depending on what the+ // host advertises, and a package driven through only half of that pair+ // looks unresponsive for a reason that is the harness's fault.+ const firePointer = (type, x, y) => {+ if (!canvas) return;+ const r = canvas.getBoundingClientRect();+ const clientX = r.left + r.width * x;+ const clientY = r.top + r.height * y;+ const init = { clientX, clientY, bubbles: true, cancelable: true, button: 0, buttons: type === 'up' ? 0 : 1, pointerId: 1, isPrimary: true, pointerType: 'mouse' };+ const names = { down: ['pointerdown', 'mousedown'], move: ['pointermove', 'mousemove'], up: ['pointerup', 'mouseup'] };+ for (const n of names[type]) {+ canvas.dispatchEvent(n.startsWith('pointer') ? new PointerEvent(n, init) : new MouseEvent(n, init));+ }+ if (type === 'up') canvas.dispatchEvent(new MouseEvent('click', init));+ };++ for (const k of keys) fireKey('keydown', k);+ if (pointer) {+ firePointer('move', pointer.x, pointer.y);+ await raf();+ firePointer('down', pointer.x, pointer.y);+ // Held across a few frames: a press sampled once per frame can land+ // between two of them and never be seen as a press at all.+ for (let i = 0; i < 4; i++) await raf();+ firePointer('up', pointer.x, pointer.y);+ }+ for (let i = 0; i < ${frames}; i++) await raf();+ for (const k of keys) fireKey('keyup', k);+ await raf();+ return keys.length + (pointer ? 1 : 0);+ })()+ `;+}diff --git a/desktop/scripts/launch-export.mjs b/desktop/scripts/launch-export.mjsindex 3a17044fb..fa6f87f1f 100644--- a/desktop/scripts/launch-export.mjs+++ b/desktop/scripts/launch-export.mjs@@ -20,7 +20,7 @@* --settle <n> frames to let run before capturing (default 30)* --timeout <ms> how long to wait for the first frame (default 30000)* --allow-flat accept a single-colour frame (a deliberately blank scene)- * --input <json> hold keys and run on: {"keys":["ArrowRight"],"frames":40}+ * --input <json> drive it: {"keys":["ArrowRight"]} or {"pointer":{"x":.5,"y":.5}}*/import { app, BrowserWindow } from 'electron';import http from 'node:http';@@ -29,6 +29,7 @@ import { existsSync } from 'node:fs';import path from 'node:path';import { fileURLToPath } from 'node:url';import { onRendererConsole } from './rendererConsole.mjs';+import { inputScript } from './inputScript.mjs';// capturePage pixels go through the OS display profile; pin sRGB so a colour// judgement reads the same on a wide-gamut machine as on a CI runner.@@ -136,28 +137,12 @@ async function main() {})`).catch((e) => { errors.push(String(e)); return false; });- // Drive the game, then let it run on. Real DOM key events on the page the build- // owns: a package has no automation hook, and the point is to exercise the path- // a player's keyboard takes, not a back door around it.+ // Drive the game, then let it run on.if (painted && INPUT) {const spec = JSON.parse(INPUT);- const ran = await win.webContents.executeJavaScript(`- (async () => {- const raf = () => new Promise((r) => requestAnimationFrame(r));- const keys = ${JSON.stringify(spec.keys ?? [])};- const fire = (type, code) => {- const e = new KeyboardEvent(type, { key: code, code, bubbles: true, cancelable: true });- window.dispatchEvent(e);- document.dispatchEvent(e);- };- for (const k of keys) fire('keydown', k);- for (let i = 0; i < ${Number(spec.frames ?? 40)}; i++) await raf();- for (const k of keys) fire('keyup', k);- await raf();- return keys.length;- })()- `).catch((e) => { errors.push(`input: ${e}`); return -1; });- console.log(` input: ${ran} key(s) held for ${Number(spec.frames ?? 40)} frames`);+ const ran = await win.webContents.executeJavaScript(inputScript(spec))+ .catch((e) => { errors.push(`input: ${e}`); return -1; });+ console.log(` input: ${ran} source(s) over ${Number(spec.frames ?? 40)} frames`);}const image = await win.webContents.capturePage();diff --git a/desktop/scripts/launch-minigame.mjs b/desktop/scripts/launch-minigame.mjsindex a983caf5c..e41187e81 100644--- a/desktop/scripts/launch-minigame.mjs+++ b/desktop/scripts/launch-minigame.mjs@@ -15,6 +15,7 @@* --w / --h surface size (default 640x360)* --settle <n> frames to let run before capturing (default 30)* --timeout <ms> how long to wait for the first frame (default 30000)+ * --input <json> drive it, same spec as launch-export*/import { app, BrowserWindow } from 'electron';import http from 'node:http';@@ -24,6 +25,7 @@ import path from 'node:path';import { fileURLToPath } from 'node:url';import { onRendererConsole } from './rendererConsole.mjs';import { HOST_PAGE } from './minigameHost.mjs';+import { inputScript } from './inputScript.mjs';app.commandLine.appendSwitch('force-color-profile', 'srgb');@@ -38,6 +40,7 @@ const W = Number(flag('w', '640'));const H = Number(flag('h', '360'));const SETTLE = Number(flag('settle', '30'));const TIMEOUT = Number(flag('timeout', '30000'));+const INPUT = flag('input', '');const MIME = {'.js': 'text/javascript', '.json': 'application/json', '.wasm': 'application/wasm',@@ -127,6 +130,13 @@ async function main() {})`).catch((e) => { errors.push(String(e)); return false; });+ if (painted && INPUT) {+ const spec = JSON.parse(INPUT);+ const ran = await win.webContents.executeJavaScript(inputScript(spec))+ .catch((e) => { errors.push(`input: ${e}`); return -1; });+ console.log(` input: ${ran} source(s) over ${Number(spec.frames ?? 40)} frames`);+ }+const image = await win.webContents.capturePage();if (OUT) await writeFile(OUT, image.toPNG());stop();diff --git a/tools/check-golden.mjs b/tools/check-golden.mjsindex 44631a0f0..091c2d8cb 100644--- a/tools/check-golden.mjs+++ b/tools/check-golden.mjs@@ -69,7 +69,13 @@ for (const g of GOLDEN) {if (!g.interact && !g.interactGap) {fail(`"${g.id}" neither declares input nor says why it has none — the chain has no interact step for it`);}- if (g.interact && !(g.interact.keys?.length > 0)) fail(`"${g.id}" declares input with no keys to hold`);+ if (g.interact && !(g.interact.keys?.length > 0) && !g.interact.pointer) {+ fail(`"${g.id}" declares input with neither keys nor a pointer target`);+ }+ const pt = g.interact?.pointer;+ if (pt && !(pt.x >= 0 && pt.x <= 1 && pt.y >= 0 && pt.y <= 1)) {+ fail(`"${g.id}" has a pointer target outside the surface — x/y are fractions, not pixels`);+ }if (!g.certifies?.length) fail(`"${g.id}" certifies nothing — say what it is in the suite for`);for (const c of g.certifies ?? []) {if (!CAPABILITIES.includes(c)) fail(`"${g.id}" certifies "${c}", which is not a declared capability`);diff --git a/tools/goldenProjects.mjs b/tools/goldenProjects.mjsindex 625e09d56..824b3af1a 100644--- a/tools/goldenProjects.mjs+++ b/tools/goldenProjects.mjs@@ -84,6 +84,8 @@ export const KNOWN_GAPS = {* `parity` overrides {@link DEFAULT_PARITY} for a game whose opening seconds* move too much to compare that tightly; `parityGap` opts out with a reason.* `interact` is the input a package must visibly answer; `interactGap` opts out.+ * A pointer target is a FRACTION of the surface and therefore tied to the layout+ * it was aimed at: when one moves, the check fails loudly and names both frames.*/export const GOLDEN = [{@@ -105,7 +107,8 @@ export const GOLDEN = [certifies: ['ui-layout', 'text'],targets: ['web', 'desktop'],tier: 'pr',- interactGap: 'widgets are pointer-driven; no keyboard path to hold',+ // Opens the modal — a whole-panel change, so the response is unmistakable.+ interact: { pointer: { x: 0.625, y: 0.675 }, frames: 40 },},{id: 'tilemap-demo',@@ -129,7 +132,7 @@ export const GOLDEN = [certifies: ['persistence', 'save-versioning'],targets: ['web', 'desktop'],tier: 'nightly',- interactGap: 'save slots are driven from the UI, not the keyboard',+ interactGap: 'pointer-driven; no stable slot target pinned yet',},{id: 'hot-update-demo',@@ -150,7 +153,7 @@ export const GOLDEN = [certifies: ['single-file'],targets: ['playable', 'web'],tier: 'nightly',- interactGap: 'tiles are pointer-driven; no keyboard path',+ interactGap: 'pointer-driven; no stable tile target pinned yet',},{id: 'input-actions',@@ -201,7 +204,12 @@ export function parityFor(g) {/** The input a project's package must answer, or null when it opted out. */export function interactFor(g) {if (g.interactGap || !g.interact) return null;- return { keys: g.interact.keys ?? [], frames: g.interact.frames ?? 40, responds: g.interact.responds ?? DEFAULT_RESPONDS };+ return {+ keys: g.interact.keys ?? [],+ pointer: g.interact.pointer ?? null,+ frames: g.interact.frames ?? 40,+ responds: g.interact.responds ?? DEFAULT_RESPONDS,+ };}/** Absolute path to a golden project's directory. */diff --git a/tools/verify-golden.mjs b/tools/verify-golden.mjsindex 4c0622092..c25f65376 100644--- a/tools/verify-golden.mjs+++ b/tools/verify-golden.mjs@@ -170,7 +170,7 @@ for (const { id, target } of pairs) {'electron', path.join('scripts', LAUNCHER(target)),'--dir', out, '--out', drivenPng,'--w', String(editor.w), '--h', String(editor.h),- '--input', JSON.stringify({ keys: input.keys, frames: input.frames }),+ '--input', JSON.stringify({ keys: input.keys, pointer: input.pointer, frames: input.frames }),], { encoding: 'utf8', cwd: DESKTOP });if (drive.status !== 0) {results.push({ id, target, stage: 'interact', ok: false, why: 'the driven launch failed' });@@ -188,7 +188,9 @@ for (const { id, target } of pairs) {}const answered = response >= input.responds;results.push({ id, target, stage: 'interact', ok: answered, why: answered ? '' : `response ${response.toFixed(4)} < ${input.responds}` });- console.log(`${answered ? '✓' : '✗'} ${id} ${target} — parity ${distance.toFixed(4)}, responds ${response.toFixed(4)} to ${input.keys.join('+')}`);+ const gesture = [input.keys.join('+'), input.pointer && `tap ${input.pointer.x}×${input.pointer.y}`]+ .filter(Boolean).join(' + ');+ console.log(`${answered ? '✓' : '✗'} ${id} ${target} — parity ${distance.toFixed(4)}, responds ${response.toFixed(4)} to ${gesture}`);if (!answered) console.log(` the package did not visibly answer the key; compare ${packagePng} and ${drivenPng}`);}