// 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 { OpenAl } from "./openal.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; // how often sounds are checked for having ended, in polls const AUDIO_POLLS = 10; const game_window = new SdlWindow("bworld", 1280, 720); // the game plays without sound when there's no openal let audio: OpenAl | undefined; try { audio = new OpenAl(); } catch (e) { console.error(`No sound: ${(e as Error).message}`); } 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; let closing = false; // 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); audio?.reset(); 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, audio: audio !== undefined, }); } function send(message: WindowMessage) { client?.postMessage(message); } function handle(message: ClientMessage) { // what it asks while the app closes, like sounds, would come after openal and the window are gone if (closing) return; 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 "audio": audio?.run(message.commands); break; case "store": storage[message.key] = message.value; Deno.writeTextFileSync(STORAGE_FILE, JSON.stringify(storage, null, "\t")); break; } } // the world gets saved before the app closes. the client isn't terminated first, exiting while a terminated worker is // still letting go of its webgpu surface can crash deno, exiting stops it as well async function close() { if (closing) return; closing = true; clearInterval(polling); await app.singleplayer.stop(); audio?.close(); game_window.close(); Deno.exit(0); } let polls = 0; const polling = setInterval(() => { game_window.poll(send, close); if (audio && ++polls % AUDIO_POLLS === 0) { const ended = audio.ended(); if (ended.length) send({ type: "audio_ended", sources: ended }); } }, POLL_MS); start_client(start_page);