35 lines
1.9 KiB
JavaScript
35 lines
1.9 KiB
JavaScript
import { fileURLToPath } from "node:url";
|
|
import { build } from "esbuild";
|
|
import { parseHTML } from "linkedom";
|
|
|
|
const bundle = await build({ entryPoints: [fileURLToPath(new URL("../../src/player/engine.ts", import.meta.url))],
|
|
bundle: true, write: false, format: "esm", platform: "node", logLevel: "silent" });
|
|
const { Engine } = await import(`data:text/javascript;base64,${Buffer.from(bundle.outputFiles[0].text).toString("base64")}`);
|
|
Error.stackTraceLimit = 2; // data-URL bundle frames otherwise print the entire bundle on a failure.
|
|
|
|
export function makeEngine(runtime) {
|
|
const { window, document } = parseHTML("<!doctype html><html><body></body></html>");
|
|
const storage = new Map();
|
|
Object.assign(globalThis, {
|
|
window, document, HTMLElement: window.HTMLElement, HTMLImageElement: window.HTMLImageElement,
|
|
localStorage: { getItem: (key) => storage.get(key) ?? null, setItem: (key, value) => storage.set(key, value), removeItem: (key) => storage.delete(key) },
|
|
ResizeObserver: class { observe() {} disconnect() {} },
|
|
requestAnimationFrame: (fn) => setTimeout(() => fn(performance.now()), 2), cancelAnimationFrame: clearTimeout,
|
|
});
|
|
const tr = { lang: "en", renpyName: null, str: (s) => s, line: (_id, s) => s };
|
|
const root = document.createElement("div");
|
|
document.body.append(root);
|
|
const engine = new Engine(root, structuredClone(runtime.game), { files: {} }, structuredClone(runtime.modules),
|
|
{ list: async () => [], get: async () => null, put: async () => {}, remove: async () => {} },
|
|
"runtime-test", () => {}, tr, async () => tr);
|
|
return { engine, root, window, document };
|
|
}
|
|
|
|
export const until = async (condition) => {
|
|
const deadline = performance.now() + 3000;
|
|
while (!condition()) {
|
|
if (performance.now() > deadline) throw new Error("Ожидаемое состояние плеера не наступило");
|
|
await new Promise((resolve) => setTimeout(resolve, 2));
|
|
}
|
|
};
|