aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/client/capture_game.js (2401B)


      1 // Send this source to the Computer Use Node REPL while verify_game.py owns the client.
      2 await (async () => {
      3     const fs = await import('node:fs/promises');
      4     const path = await import('node:path');
      5     const {setTimeout} = await import('node:timers/promises');
      6     const directory = (await import('node:url')).pathToFileURL(path.resolve('local/client/game-14.9.1') + path.sep);
      7     const report = JSON.parse(await fs.readFile(new URL('verification.json', directory), 'utf8'));
      8     if (report.client_exited || !report.started_at) {
      9         throw new Error('Start verify_game.py before capturing the game.');
     10     }
     11 
     12     // Resolve a current, unique game window through Computer Use.
     13     const windows = (await sky.list_windows()).filter(window =>
     14         window.app.toLowerCase() === 'process:' + report.executable.toLowerCase());
     15     if (windows.length !== 1) {
     16         throw new Error(`Expected one generated game window, found ${windows.length}.`);
     17     }
     18     let window = await sky.get_window({id: windows[0].id, app: windows[0].app});
     19     await sky.activate_window({window});
     20 
     21     // Native F12 captures the DirectDraw surface that Windows capture misses.
     22     const screenshots = path.join(report.client, 'Screenshots');
     23     const before = new Set(await fs.readdir(screenshots));
     24     const state = await sky.get_window_state({window, include_screenshot: false, include_text: true});
     25     window = state.window;
     26     await sky.press_key({window, key: 'F12'});
     27     await sky.get_window_state({window, include_screenshot: false, include_text: true});
     28     let files = [];
     29     for (let attempt = 0; attempt < 10 && files.length === 0; attempt++) {
     30         await setTimeout(500);
     31         files = (await fs.readdir(screenshots)).filter(name => !before.has(name) && name.endsWith('.png'));
     32     }
     33     if (files.length !== 1) throw new Error(`Expected one native screenshot, found ${files.length}.`);
     34     await setTimeout(500);
     35     const elapsed = Math.round(Date.now() / 1000 - report.started_at);
     36     const filename = `capture-${report.pid}-${elapsed}.png`;
     37     await fs.copyFile(path.join(screenshots, files[0]), new URL(filename, directory));
     38     const capture = {pid: report.pid, filename, elapsed_seconds: elapsed, native_file: files[0]};
     39     await fs.writeFile(new URL(`capture-${report.pid}-${elapsed}.json`, directory),
     40         JSON.stringify(capture, null, 2));
     41     nodeRepl.write(capture);
     42 })();