Skip to content

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.

9.4 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file  launch-minigame.mjs — a packaged mini-game starts, and draws.
 *
 * The WeChat/Douyin counterpart of launch-export: same two questions (did it
 * reach a frame, is that frame more than one flat colour), asked of a package
 * no browser can open on its own. minigameHost supplies the CommonJS loader and
 * the `wx` global; this serves the export and judges the result.
 *
 * It stands in for the vendor, it does not impersonate it — see minigameHost.
 *
 *   electron desktop/scripts/launch-minigame.mjs --dir <exportDir> [options]
 *     --out <file.png>   write the captured frame
 *     --w / --h          surface size (default 640x360)
 *     --settle <n>       frames to let run before capturing (default 30)
 *     --frame-ms <n>     simulated time per render frame (default 1000/60; 0 = real time)
 *     --timeout <ms>     how long to wait for the first frame (default 30000)
 *     --input <json>     drive it, same spec as launch-export
 *     --fail-subpackage <name>  the host refuses that 分包, to see what it does
 */
import { app, BrowserWindow } from 'electron';
import http from 'node:http';
import { readFile, writeFile } from 'node:fs/promises';
import { existsSync, readFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { onRendererConsole } from '../lib/rendererConsole.mjs';
import { HOST_PAGE } from './minigameHost.mjs';
import { inputScript } from './inputScript.mjs';
import { frameClockScript } from './frameClock.mjs';

// Headless / GPU-less (CI) WebGL2 falls back to SwiftShader; harmless with a GPU.
app.commandLine.appendSwitch('enable-unsafe-swiftshader');
// The GPU process keeps a sandbox of its own, which ELECTRON_DISABLE_SANDBOX
// does not reach: on a runner with no device it dies before the first frame.
app.commandLine.appendSwitch('disable-gpu-sandbox');
// ...and on Linux there is no GPU process at all — one launch per check races
// the service that answers for WebGL2, and in-process SwiftShader draws the same.
if (process.platform === 'linux') app.commandLine.appendSwitch('in-process-gpu');
app.commandLine.appendSwitch('force-color-profile', 'srgb');
// Both of the above pin a machine's answer to the code's; so does this. A capture
// is in DEVICE pixels, so a scaled display returns a frame the editor's own
// capture cannot be compared against. Its twin launcher pins it for the same reason.
app.commandLine.appendSwitch('force-device-scale-factor', '1');

const argv = process.argv.slice(2);
const flag = (n, d) => {
  const i = argv.indexOf(`--${n}`);
  return i >= 0 && argv[i + 1] && !argv[i + 1].startsWith('--') ? argv[i + 1] : d;
};
const DIR = path.resolve(flag('dir', ''));
const OUT = flag('out', '');
const W = Number(flag('w', '640'));
const H = Number(flag('h', '360'));
const SETTLE = Number(flag('settle', '30'));
const FRAME_MS = Number(flag('frame-ms', String(1000 / 60)));
const TIMEOUT = Number(flag('timeout', '30000'));
const INPUT = flag('input', '');
/**
 * Make the host refuse this 分包 — and with it, ask a different question.
 *
 * The package is not supposed to start. A 分包 that will not come down is a
 * failure a shipped game meets on a bad network, and all it can still do is SAY
 * so, which is what the verdict flips to: silence is the failure here.
 */
const FAIL_SUBPACKAGE = flag('fail-subpackage', '');

const MIME = {
  '.js': 'text/javascript', '.json': 'application/json', '.wasm': 'application/wasm',
  '.png': 'image/png', '.jpg': 'image/jpeg', '.webp': 'image/webp', '.ktx2': 'image/ktx2',
  '.mp3': 'audio/mpeg', '.ogg': 'audio/ogg', '.wav': 'audio/wav', '.m4a': 'audio/mp4',
  '.atlas': 'text/plain', '.skel': 'application/octet-stream', '.txt': 'text/plain',
};

function serve(root, page) {
  const server = http.createServer(async (req, res) => {
    try {
      let rel = decodeURIComponent(new URL(req.url, 'http://x').pathname).replace(/^\/+/, '');
      if (rel === '' || rel === 'index.html') {
        res.writeHead(200, { 'content-type': 'text/html' }).end(page);
        return;
      }
      const abs = path.join(root, rel);
      if (!abs.startsWith(root)) { res.writeHead(403).end(); return; }
      // The vendor's loader takes a `.wasm.br` path and decompresses inside
      // itself; nothing in a page can (Chromium's DecompressionStream has no
      // brotli), so the transport does — the stand-in host sees raw wasm either way.
      const brotli = abs.endsWith('.br');
      const ext = path.extname(brotli ? abs.slice(0, -3) : abs).toLowerCase();
      const body = await readFile(abs);
      res.writeHead(200, {
        'content-type': MIME[ext] ?? 'application/octet-stream',
        ...(brotli ? { 'content-encoding': 'br' } : {}),
      }).end(body);
    } catch {
      res.writeHead(404).end();
    }
  });
  return new Promise((resolve) => server.listen(0, '127.0.0.1', () => resolve(server)));
}

function frameIsLive(image) {
  const { width, height } = image.getSize();
  const buf = image.toBitmap();
  const first = [buf[0], buf[1], buf[2]];
  const step = Math.max(1, Math.floor((width * height) / 4000));
  for (let p = 0; p < width * height; p += step) {
    const i = p * 4;
    if (Math.abs(buf[i] - first[0]) > 8 || Math.abs(buf[i + 1] - first[1]) > 8 || Math.abs(buf[i + 2] - first[2]) > 8) {
      return true;
    }
  }
  return false;
}

async function main() {
  const manifest = path.join(DIR, 'game.json');
  if (!existsSync(manifest)) {
    console.error(`✗ no game.json under ${DIR || '(--dir not given)'} — not a mini-game package`);
    app.exit(2);
    return;
  }
  // The vendor reads the entry from game.json's convention (game.js); assert it
  // is there rather than letting the loader 404 into a confusing stack.
  const entry = 'game.js';
  if (!existsSync(path.join(DIR, entry))) {
    console.error(`✗ ${entry} is missing — the package has a manifest and no entry`);
    app.exit(1);
    return;
  }
  JSON.parse(readFileSync(manifest, 'utf8')); // a manifest the vendor cannot parse is a dead package

  const page = HOST_PAGE(entry, FAIL_SUBPACKAGE || null).replace('<head>', '<head>' + frameClockScript(FRAME_MS));
  const server = await serve(DIR, page);
  const base = `http://127.0.0.1:${server.address().port}/`;

  const win = new BrowserWindow({
    width: W, height: H, useContentSize: true, show: false,
    webPreferences: { backgroundThrottling: false },
  });

  const errors = [];
  const stop = onRendererConsole(win.webContents, (msg, level) => {
    // `[estella]` is the package's own voice (why a 分包 did not come down). Any
    // logged error counts too: a system that throws every frame still paints.
    if (level === 'error' || /\[minigame\] boot failed|\[estella\]|uncaught|is not a function|no such file/i.test(msg)) {
      errors.push(msg.slice(0, 400));
    }
    // The engine names the GL path it took; a check about EmscriptenGLX reads it.
    if (level === 'warning' || level === 'error' || /^\[(minigame|engine|estella)\]|rendering through /.test(msg)) {
      console.log(`  ${msg.slice(0, 400)}`);
    }
  });
  win.webContents.on('render-process-gone', (_e, d) => errors.push(`render process gone: ${d.reason}`));

  await win.loadURL(base);
  const painted = await win.webContents.executeJavaScript(`
    new Promise((resolve) => {
      const deadline = Date.now() + ${TIMEOUT};
      let frames = 0;
      const tick = () => {
        const c = document.querySelector('canvas');
        if (c && c.width > 0 && c.height > 0) frames++;
        if (frames >= ${SETTLE}) return resolve(true);
        if (Date.now() > deadline) return resolve(false);
        requestAnimationFrame(tick);
      };
      requestAnimationFrame(tick);
    })
  `).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();
  server.close();

  const live = frameIsLive(image);
  const said = errors.find((e) => e.includes(`subpackage ${FAIL_SUBPACKAGE} did not load`));
  const ok = FAIL_SUBPACKAGE
    ? said !== undefined
    : painted && live && errors.length === 0;
  console.log(`${ok ? '✓' : '✗'} ${path.basename(DIR)} (mini-game) — `
    + (FAIL_SUBPACKAGE
      ? `${FAIL_SUBPACKAGE} refused: the package ${said ? 'said why' : 'said nothing'}`
      : `painted=${painted} live=${live} errors=${errors.length}`));
  for (const e of errors.slice(0, 5)) console.log(`    ${e}`);
  if (!FAIL_SUBPACKAGE && painted && !live) console.log('    one flat colour — it started and drew nothing');
  if (!ok) {
    // What the boot last announced through the host indicator. A package stuck
    // fetching its engine 分包 and one stuck on its first scene look identical
    // from outside, and only one of them sends you to the right place.
    const stage = await win.webContents.executeJavaScript('globalThis.__estellaLoading ?? null')
      .catch(() => null);
    if (stage) console.log(`    the boot was still saying: ${stage}`);
  }
  app.exit(ok ? 0 : 1);
}

app.whenReady().then(main).catch((e) => {
  console.error('✗ launch-minigame failed:', e);
  app.exit(2);
});