Files
bworld/desktop/main.ts
T

48 lines
1.8 KiB
TypeScript

// 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";
import { desktop_app } from "./app.ts";
const app = desktop_app((message) => {
console.error(message);
alert(`The singleplayer world stopped: ${message}`);
Deno.exit(1);
});
const win = new Deno.BrowserWindow({ title: "bworld", width: 1280, height: 720 });
guard_shortcuts(win);
// 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();
}
// 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);
}
// 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 });
}
return await app.handle(req, url);
});