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.

14.4 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file  minigameHost.mjs — the page that boots a mini-game package outside its vendor.
 *
 * A WeChat/Douyin package is CommonJS plus a `wx`/`tt` global, so no browser
 * opens one and CI never launched a mini-game build at all. This supplies both,
 * over http, from the export directory as it shipped.
 *
 * What it is NOT: WeChat. It cannot answer how the real base library behaves.
 * What it does answer is everything between "the exporter returned ok" and "the
 * game runs" — a file the entry requires and the package does not carry, a wasm
 * the loader cannot locate, an entry wired to the wrong boot call.
 *
 * The shim implements `MiniGameGlobal` (sdk/src/platform/minigame/api.ts), which
 * the engine documents as "only the subset the engine actually uses" — so it is
 * written against a declared contract rather than guessed, and check-minigame-host
 * fails when the contract grows a member the shim has not got.
 */

/** Forward real canvas touch events through the vendor callback contract. */
export function miniGameTouchBridge(canvas) {
  const api = {};
  const list = touches => Array.from(touches ?? [], touch => ({
    identifier: touch.identifier, clientX: touch.clientX, clientY: touch.clientY,
  }));
  for (const phase of ['Start', 'Move', 'End', 'Cancel']) {
    const kind = 'touch' + phase.toLowerCase();
    const listeners = new Map();
    api['onTouch' + phase] = callback => {
      if (listeners.has(callback)) return;
      const listener = event => callback({ touches: list(event.touches), changedTouches: list(event.changedTouches) });
      listeners.set(callback, listener);
      canvas.addEventListener(kind, listener, { passive: false });
    };
    api['offTouch' + phase] = callback => {
      const listener = listeners.get(callback);
      if (!listener) return;
      canvas.removeEventListener(kind, listener);
      listeners.delete(callback);
    };
  }
  return api;
}

/** Keep keyboard callback identity so a platform unbind removes its DOM listener. */
export function miniGameKeyboardBridge(target) {
  const api = {};
  for (const phase of ['Down', 'Up']) {
    const kind = 'key' + phase.toLowerCase();
    const listeners = new Map();
    api['onKey' + phase] = callback => {
      if (listeners.has(callback)) return;
      const listener = event => callback({ code: event.code, key: event.key });
      listeners.set(callback, listener);
      target.addEventListener(kind, listener);
    };
    api['offKey' + phase] = callback => {
      const listener = listeners.get(callback);
      if (!listener) return;
      target.removeEventListener(kind, listener);
      listeners.delete(callback);
    };
  }
  return api;
}

/** Forward page visibility with the same callback identity as vendor lifecycle hooks. */
export function miniGameVisibilityBridge(target) {
  const api = {};
  for (const phase of ['Show', 'Hide']) {
    const listeners = new Map();
    api['on' + phase] = callback => {
      if (listeners.has(callback)) return;
      const listener = () => { if (target.hidden === (phase === 'Hide')) callback(); };
      listeners.set(callback, listener);
      target.addEventListener('visibilitychange', listener);
    };
    api['off' + phase] = callback => {
      const listener = listeners.get(callback);
      if (!listener) return;
      target.removeEventListener('visibilitychange', listener);
      listeners.delete(callback);
    };
  }
  return api;
}

/**
 * The page source; `BASE` is substituted with the served root.
 *
 * `failSubpackage` makes `loadSubpackage` REFUSE that root: a 分包 that will not
 * come down is a failure a game has to survive, and a stand-in that always
 * succeeds proves only the happy half.
 */
export const HOST_PAGE = (entry, failSubpackage = null) => `<!doctype html>
<html><head><meta charset="utf-8"><style>
  html,body{margin:0;height:100%;background:#000;overflow:hidden}
  canvas{display:block}
</style></head><body>
<script>
(() => {
  const log = (m) => console.log('[minigame] ' + m);
  // Not every mini-game realm has these: WeChat DevTools' does not, nor does
  // JavaScriptCore before iOS 15. A glue that builds a view from them fails here too
  // rather than only on the host it reaches a user through. Named still, as a test
  // driver's own script asks instanceof of them: only making one is what fails.
  for (const name of ['BigInt64Array', 'BigUint64Array']) {
    const real = globalThis[name];
    const absent = function () { throw new ReferenceError(name + ' is not defined'); };
    absent.prototype = real.prototype;
    globalThis[name] = absent;
  }
  // Vendor contract: the FIRST createCanvas() hands back the on-screen canvas and
  // every call after it is offscreen scratch. Hand the engine a fresh offscreen
  // one instead and the game runs perfectly, drawing where nobody can see.
  const screenCanvas = document.createElement('canvas');
  screenCanvas.width = window.innerWidth;
  screenCanvas.height = window.innerHeight;
  document.body.appendChild(screenCanvas);
  const touchBridge = (${miniGameTouchBridge.toString()})(screenCanvas);
  const keyboardBridge = (${miniGameKeyboardBridge.toString()})(window);
  const visibilityBridge = (${miniGameVisibilityBridge.toString()})(document);
  let screenTaken = false;
  const makeCanvas = () => {
    if (!screenTaken) { screenTaken = true; return screenCanvas; }
    return document.createElement('canvas');
  };

  const readAsArrayBuffer = (p) => {
    const xhr = new XMLHttpRequest();
    xhr.open('GET', p, false); // sync: readFileSync is synchronous by contract
    xhr.overrideMimeType('text/plain; charset=x-user-defined');
    xhr.send(null);
    if (xhr.status !== 200 && xhr.status !== 0) throw new Error('no such file: ' + p);
    const s = xhr.responseText;
    const b = new Uint8Array(s.length);
    for (let i = 0; i < s.length; i++) b[i] = s.charCodeAt(i) & 0xff;
    return b.buffer;
  };
  const readAsText = (p) => {
    const xhr = new XMLHttpRequest();
    xhr.open('GET', p, false);
    xhr.send(null);
    if (xhr.status !== 200 && xhr.status !== 0) throw new Error('no such file: ' + p);
    return xhr.responseText;
  };

  const fs = {
    readFileSync: (p, enc) => (enc === 'utf8' || enc === 'utf-8' ? readAsText(p) : readAsArrayBuffer(p)),
    readFile: (o) => {
      try { o.success && o.success({ data: fs.readFileSync(o.filePath, o.encoding) }); }
      catch (e) { o.fail && o.fail({ errMsg: String(e) }); }
      finally { o.complete && o.complete(); }
    },
    accessSync: (p) => { readAsArrayBuffer(p); },
    access: (o) => {
      try { fs.accessSync(o.path); o.success && o.success(); }
      catch (e) { o.fail && o.fail({ errMsg: String(e) }); }
      finally { o.complete && o.complete(); }
    },
    writeFile: (o) => { o.fail && o.fail({ errMsg: 'read-only host' }); o.complete && o.complete(); },
    writeFileSync: () => { throw new Error('read-only host'); },
    mkdirSync: () => {},
    unlinkSync: () => {},
    rmdirSync: () => {},
    readdirSync: () => [],
    statSync: () => ({ size: 0, isDirectory: () => false }),
  };

  const noop = () => {};
  /** What the boot last said it was doing. On the window rather than a closure
   *  because the launcher reads it when a package never reaches a frame — the
   *  stage it was stuck on is the whole diagnosis. */
  globalThis.__estellaLoading = null;
  const wx = {
    createCanvas: makeCanvas,
    createImage: () => new Image(),
    getFileSystemManager: () => fs,
    getSystemInfoSync: () => ({
      pixelRatio: window.devicePixelRatio || 1,
      language: 'en',
      windowWidth: window.innerWidth,
      windowHeight: window.innerHeight,
      platform: 'devtools',
      safeArea: { top: 0, bottom: window.innerHeight, left: 0, right: window.innerWidth,
                  width: window.innerWidth, height: window.innerHeight },
    }),
    request: (o) => {
      fetch(o.url).then(async (r) => {
        const data = o.responseType === 'arraybuffer' ? await r.arrayBuffer() : await r.text();
        o.success && o.success({ data, statusCode: r.status });
      }).catch((e) => o.fail && o.fail({ errMsg: String(e) })).finally(() => o.complete && o.complete());
    },
    // Audio is a stub on purpose: a launch smoke asks whether the package starts
    // and draws, and a silent stub cannot fake either answer.
    createInnerAudioContext: () => ({
      play: noop, pause: noop, stop: noop, destroy: noop, seek: noop,
      onPlay: noop, onPause: noop, onStop: noop, onEnded: noop, onError: noop, onCanplay: noop,
      offPlay: noop, offPause: noop, offStop: noop, offEnded: noop, offError: noop, offCanplay: noop,
      src: '', loop: false, volume: 1, autoplay: false, currentTime: 0, duration: 0, paused: true,
    }),
    // A real socket, in the SocketTask shape: a development build dials the
    // editor through it, and a stub would pass that check without a byte moving.
    connectSocket: (o) => {
      const ws = new WebSocket(o.url, o.protocols);
      ws.binaryType = 'arraybuffer';
      return {
        send: (m) => { ws.send(m.data); m.success && m.success(); },
        close: (m = {}) => ws.close(m.code, m.reason),
        onOpen: (cb) => ws.addEventListener('open', () => cb({})),
        onClose: (cb) => ws.addEventListener('close', (e) => cb({ code: e.code, reason: e.reason })),
        onError: (cb) => ws.addEventListener('error', () => cb({ errMsg: 'connectSocket:fail' })),
        onMessage: (cb) => ws.addEventListener('message', (e) => cb({ data: e.data })),
        offOpen: noop, offClose: noop, offError: noop, offMessage: noop,
      };
    },
    ...touchBridge,
    ...keyboardBridge,
    ...visibilityBridge,
    onMemoryWarning: noop, offMemoryWarning: noop,
    onError: noop, offError: noop,
    onUnhandledRejection: noop, offUnhandledRejection: noop,
    onWindowResize: noop, offWindowResize: noop,
    loadSubpackage: (o) => {
      if (o && o.name === ${JSON.stringify(failSubpackage)}) {
        o.fail && o.fail({ errMsg: 'loadSubpackage:fail (injected)' });
        o.complete && o.complete();
        return;
      }
      o.success && o.success(); o.complete && o.complete();
    },
    // Storage is real (localStorage), not a stub: a game that saves on boot and
    // reads it back would otherwise take the silent path and look fine.
    getStorageSync: (k) => { const v = localStorage.getItem(k); return v === null ? '' : JSON.parse(v); },
    setStorageSync: (k, v) => localStorage.setItem(k, JSON.stringify(v)),
    removeStorageSync: (k) => localStorage.removeItem(k),
    getStorageInfoSync: () => ({ keys: Object.keys(localStorage), currentSize: 0, limitSize: 10240 }),
    // The boot's only progress surface. Stubbed rather than left absent: an
    // absent optional makes an unguarded caller look fine here and die on a
    // device, which is the direction a stand-in must never get wrong.
    showLoading: (o) => { globalThis.__estellaLoading = o && o.title; },
    hideLoading: () => { globalThis.__estellaLoading = null; },
    setPreferredFramesPerSecond: noop,
    triggerGC: noop,
    exitMiniProgram: noop,
    getLaunchOptionsSync: () => ({ scene: 1001, query: {} }),
  };
  // WeChat instantiates wasm from a PACKAGE-RELATIVE PATH, not bytes — the one
  // place the vendors genuinely diverge (sdk/src/platform/wechat/wasm.ts), and
  // the reason a browser cannot open one of these packages unaided.
  const asUrl = (p) => '/' + (String(p)[0] === '/' ? String(p).slice(1) : String(p));
  globalThis.WXWebAssembly = {
    instantiate: (pathOrBytes, imports) => (typeof pathOrBytes === 'string'
      ? fetch(asUrl(pathOrBytes)).then((r) => r.arrayBuffer())
        .then((b) => WebAssembly.instantiate(b, imports))
      : WebAssembly.instantiate(pathOrBytes, imports)),
    compile: (p) => fetch(asUrl(p)).then((r) => r.arrayBuffer()).then((b) => WebAssembly.compile(b)),
    Memory: WebAssembly.Memory,
    Table: WebAssembly.Table,
    Module: WebAssembly.Module,
    Instance: WebAssembly.Instance,
  };

  globalThis.wx = wx;
  // The same object under Douyin's name: it answers the same shape for
  // everything the engine uses, and its profile supplies no wasm override. What
  // this page cannot answer for either vendor is the real base library.
  globalThis.tt = wx;
  globalThis.TTWebAssembly = globalThis.WXWebAssembly;
  globalThis.GameGlobal = globalThis;
  globalThis.canvas = screenCanvas;

  // The package is CommonJS. A mini-game host resolves relative requires against
  // the requiring file, so the loader has to carry that directory with it.
  const modules = new Map();
  const dirOf = (p) => p.slice(0, p.lastIndexOf('/') + 1);
  const normalize = (p) => {
    const out = [];
    for (const part of p.split('/')) {
      if (part === '.' || part === '') continue;
      if (part === '..') out.pop();
      else out.push(part);
    }
    return out.join('/');
  };
  function requireFrom(dir, spec) {
    let file = spec.startsWith('.') ? normalize(dir + spec) : normalize(spec);
    if (!/\\.(js|json)$/.test(file)) file += '.js';
    if (modules.has(file)) return modules.get(file).exports;
    const src = readAsText('/' + file);
    if (file.endsWith('.json')) {
      const m = { exports: JSON.parse(src) };
      modules.set(file, m);
      return m.exports;
    }
    const mod = { exports: {} };
    modules.set(file, mod);
    const fn = new Function('module', 'exports', 'require', '__filename', '__dirname', 'wx', 'GameGlobal',
      src + '\\n//# sourceURL=' + file);
    fn(mod, mod.exports, (s) => requireFrom(dirOf(file), s), file, dirOf(file), wx, globalThis);
    return mod.exports;
  }

  try {
    log('booting ${entry}');
    requireFrom('', './${entry}');
    log('entry returned');
  } catch (e) {
    console.error('[minigame] boot failed: ' + (e && e.stack ? e.stack : e));
  }
})();
</script></body></html>`;

/**
 * Members of `MiniGameGlobal` the shim above provides. Kept beside it so
 * check-minigame-host can hold the two against the SDK's declaration.
 */
export const SHIM_MEMBERS = [
  'createCanvas', 'createImage', 'getFileSystemManager', 'request',
  'createInnerAudioContext', 'connectSocket', 'getSystemInfoSync',
  'onTouchStart', 'onTouchMove', 'onTouchEnd',
  'offTouchStart', 'offTouchMove', 'offTouchEnd',
  'onTouchCancel', 'offTouchCancel',
  'onKeyDown', 'onKeyUp', 'offKeyDown', 'offKeyUp',
  'loadSubpackage', 'showLoading', 'hideLoading', 'onMemoryWarning', 'offMemoryWarning',
  'onError', 'offError', 'onUnhandledRejection', 'offUnhandledRejection',
  'onShow', 'onHide',
  'getStorageSync', 'setStorageSync', 'removeStorageSync', 'getStorageInfoSync',
];