Drop deno desktop
This commit is contained in:
+136
-32
@@ -1,47 +1,151 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
/// <reference lib="deno.desktop" />
|
||||
|
||||
// the desktop app: the web client in a bundled chromium (deno desktop's cef backend), so people whose browser has no
|
||||
// webgpu (firefox) can still play. its title screen is a main menu like luanti's: singleplayer worlds are played on
|
||||
// a game server of its own, with the mods each world picked from its data folder, see saves.ts. build it with deno
|
||||
// task desktop, see the desktop/ docs for how deno desktop works
|
||||
import { guard_shortcuts } from "./shortcuts.ts";
|
||||
import { center_mouse } from "./center_mouse.ts";
|
||||
// the desktop app: an sdl window (sdl.ts) that the client draws into with deno's own webgpu, so it plays without a
|
||||
// browser. 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 or playing sounds (openal.ts).
|
||||
// the client's files and singleplayer are served on a local address, 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);
|
||||
alert(`The singleplayer world stopped: ${message}`);
|
||||
game_window.show_error("bworld", `The singleplayer world stopped: ${message}`);
|
||||
Deno.exit(1);
|
||||
});
|
||||
|
||||
const win = new Deno.BrowserWindow({ title: "bworld", width: 1280, height: 720 });
|
||||
guard_shortcuts(win);
|
||||
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}`);
|
||||
|
||||
// the world gets saved before the app closes. it always exits by itself: deno desktop's own shutdown freezes on the
|
||||
// last frame when chromium's ctrl+w closed the window, and Deno.serve keeps the process alive anyway
|
||||
win.addEventListener("close", async (event) => {
|
||||
event.preventDefault();
|
||||
await app.singleplayer.stop();
|
||||
Deno.exit(0);
|
||||
});
|
||||
|
||||
if (Deno.env.get("DEBUG")) {
|
||||
win.openDevtools();
|
||||
// what the client remembers, see Platform.storage
|
||||
const STORAGE_FILE = join(app.data_dir, "client_storage.json");
|
||||
let storage: Record<string, string> = {};
|
||||
try {
|
||||
storage = JSON.parse(Deno.readTextFileSync(STORAGE_FILE));
|
||||
} catch {
|
||||
// nothing saved yet
|
||||
}
|
||||
|
||||
// deno desktop binds this to a private local address and opens the window on it, the port given here is ignored
|
||||
Deno.serve(async (req) => {
|
||||
const url = new URL(req.url);
|
||||
if (url.pathname === "/") {
|
||||
return Response.redirect(app.start_page(url.origin), 302);
|
||||
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<ClientMessage>) => 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 page let go of the mouse, see center_mouse.ts
|
||||
if (url.pathname === "/desktop/center_mouse" && req.method === "POST") {
|
||||
center_mouse();
|
||||
return new Response(null, { status: 204 });
|
||||
}
|
||||
|
||||
// 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 });
|
||||
}
|
||||
return await app.handle(req, url);
|
||||
});
|
||||
}, POLL_MS);
|
||||
start_client(start_page);
|
||||
|
||||
Reference in New Issue
Block a user