// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // the desktop app without a browser: an sdl window (sdl.ts) that the client draws into with deno's own webgpu. the // client runs in a worker (client/native/main.ts) loaded from the client's files like a page, and this thread passes // it the window's events and does what it asks, like grabbing the mouse. the client's files and singleplayer are // served on a local address like main.ts's chromium window has them, see app.ts import { join, toFileUrl } from "@std/path"; import { desktop_app } from "./app.ts"; import { SdlWindow } from "./sdl.ts"; import type { ClientMessage, WindowMessage } from "../client/native/messages.ts"; // how often the window's events are looked at, in milliseconds const POLL_MS = 2; const game_window = new SdlWindow("bworld", 1280, 720); const app = desktop_app((message) => { console.error(message); game_window.show_error("bworld", `The singleplayer world stopped: ${message}`); Deno.exit(1); }); const server = Deno.serve( { hostname: "127.0.0.1", port: 0, onListen() {} }, (req) => app.handle(req, new URL(req.url)), ); const start_page = app.start_page(`http://127.0.0.1:${server.addr.port}`); // what the client remembers, see Platform.storage const STORAGE_FILE = join(app.data_dir, "client_storage.json"); let storage: Record = {}; try { storage = JSON.parse(Deno.readTextFileSync(STORAGE_FILE)); } catch { // nothing saved yet } let client: Worker | undefined; // a fresh client, like loading the page again. the old one is gone with everything it loaded async function start_client(page: URL) { client?.terminate(); game_window.set_grabbed(false); const client_dir = await app.client_dir; const worker = new Worker(toFileUrl(join(client_dir, "client", "native.js")), { type: "module", deno: { // the servers it plays on, and its own files. ffi is for drawing into the window, it gives it up after permissions: { net: true, read: [client_dir], ffi: true, write: false, run: false, env: false, sys: false }, }, }); worker.onmessage = (event: MessageEvent) => handle(event.data); worker.onerror = (event) => { console.error(`The client stopped: ${event.message}`); }; client = worker; send({ type: "start", page: page.href, surface: game_window.surface_handles(), ...game_window.pixel_size(), storage, }); } function send(message: WindowMessage) { client?.postMessage(message); } function handle(message: ClientMessage) { switch (message.type) { case "grab": game_window.set_grabbed(message.grab); break; case "fullscreen": game_window.toggle_fullscreen(); break; case "restart": { const page = new URL(message.page); // only its own pages start_client(page.origin === start_page.origin ? page : start_page); break; } // the client can't run at all, like without webgpu case "fatal": game_window.set_grabbed(false); game_window.show_error("bworld", message.message); close(); break; case "ask": game_window.set_grabbed(false); send({ type: "answer", id: message.id, yes: game_window.ask(message.title, message.text, message.yes, message.no), }); break; case "read_clipboard": send({ type: "clipboard", id: message.id, text: game_window.read_clipboard() }); break; case "write_clipboard": game_window.write_clipboard(message.text); break; case "store": storage[message.key] = message.value; Deno.writeTextFileSync(STORAGE_FILE, JSON.stringify(storage, null, "\t")); break; } } let closing = false; // the world gets saved before the app closes async function close() { if (closing) return; closing = true; clearInterval(polling); client?.terminate(); await app.singleplayer.stop(); game_window.close(); Deno.exit(0); } const polling = setInterval(() => game_window.poll(send, close), POLL_MS); start_client(start_page);