orbit/dev/enum-probe.mjs

77 lines
3.7 KiB
JavaScript

import { launch, CDP } from './cdpshot.mjs';
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const { url: wsUrl, kill } = await launch('http://localhost:8080/index.html', undefined, 1280, 720, 5000);
const cdp = new CDP(wsUrl);
await cdp.open();
await cdp.send('Runtime.enable');
const ev = async (expression) => {
const r = await cdp.send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true });
if (r.exceptionDetails) throw new Error('eval: ' + (r.exceptionDetails.exception?.description ?? r.exceptionDetails.text));
return r.result?.value;
};
const toClient = (x, y) =>
`(() => { const c = window.game.canvas; const r = c.getBoundingClientRect(); const z = r.width / c.width; return [r.left + ${x} * z, r.top + ${y} * z]; })()`;
async function click(cx, cy) {
await cdp.send('Input.dispatchMouseEvent', { type: 'mouseMoved', x: cx, y: cy });
await sleep(70);
await cdp.send('Input.dispatchMouseEvent', { type: 'mousePressed', x: cx, y: cy, button: 'left', clickCount: 1 });
await sleep(100);
await cdp.send('Input.dispatchMouseEvent', { type: 'mouseReleased', x: cx, y: cy, button: 'left', clickCount: 1 });
await sleep(70);
}
let menuUp = false;
for (let i = 0; i < 120 && !menuUp; i++) { await sleep(500); menuUp = await ev(`!!(window.game && window.game.scene.isActive('MenuScene'))`); }
if (!menuUp) throw new Error('no menu');
await sleep(4600);
const mb = await ev(`(() => { const m = window.game.scene.getScene('MenuScene'); return [m.newGameBtn.x, m.newGameBtn.y]; })()`);
await click(...(await ev(toClient(mb[0], mb[1]))));
let gameUp = false;
for (let i = 0; i < 240 && !gameUp; i++) { await sleep(500); gameUp = await ev(`!!(window.game && window.game.scene.isActive('GameScene') && window.game.scene.getScene('GameScene').ship)`); }
if (!gameUp) throw new Error('no game');
await sleep(1200);
// Enumerate interactive objects of the ACTIVE scene, hit-test at the MENU slot point.
const dump = await ev(`(() => {
const gs = window.game.scene.getScene('GameScene');
const plugin = gs.sys.input;
const list = plugin._list ?? [];
const p = { x: 1143.33, y: 680 };
const out = [];
for (const o of list) {
let hit = null;
try {
if (o.input && o.input.enabled) {
const local = o.getWorldTransformMatrix ? o.getWorldTransformMatrix() : null;
// use the manager's own candidate test if available
}
} catch (e) {}
out.push({
type: o.type || o.constructor?.name,
name: o.name || '',
depth: o.depth,
pos: [o.x, o.y],
w: o.width ?? null,
h: o.height ?? null,
enabled: o.input ? o.input.enabled : null,
hasHitArea: o.input ? !!o.input.hitArea : false,
hitArea: o.input && o.input.hitArea ? { x: o.input.hitArea.x, y: o.input.hitArea.y, w: o.input.hitArea.width, h: o.input.hitArea.height } : null,
});
}
// hit-test via the plugin
let under = null;
try {
const pointer = { x: 1143.33, y: 680, worldX: 1143.33, worldY: 680 };
const hits = plugin.hitTestPointer ? plugin.hitTestPointer({ x: 1143.33, y: 680 }) : null;
under = hits ? hits.map((h) => (h.type || h.constructor?.name)) : 'no hitTestPointer';
} catch (e) { under = 'ERR ' + e.message; }
const compact = out.map((o) => [o.type, '@' + Math.round(o.pos[0]) + ',' + Math.round(o.pos[1]), 'd' + o.depth, 'en=' + o.enabled, o.hitArea ? ('ha=' + Math.round(o.hitArea.x) + ',' + Math.round(o.hitArea.y) + ',' + Math.round(o.hitArea.w) + 'x' + Math.round(o.hitArea.h)) : 'ha=-', o.name || ''].join(' '));
return { count: list.length, under, compact };
})()`);
console.log('UNDER POINTER:', JSON.stringify(dump.under));
console.log('count:', dump.count);
for (const line of dump.compact) console.log(' ', line);
cdp.close();
kill();