Files
bworld/desktop/main.ts
T
2026-11-14 14:48:07 -03:00

95 lines
4.0 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 { fromFileUrl, join } from "@std/path";
import { serve_static } from "../server/host.ts";
import { PROTOCOL_VERSION } from "../common/protocol.ts";
import { current_client, update_client } from "./client_updates.ts";
import { guard_shortcuts } from "./shortcuts.ts";
import { center_mouse } from "./center_mouse.ts";
import { data_dir, install_mods } from "./saves.ts";
import { singleplayer_server } from "./singleplayer.ts";
// where the project was built from: build/ and server_mods/ (the .cubi files) are bundled into the app with --include
const ROOT = fromFileUrl(new URL("../", import.meta.url));
const BUILD_DIR = join(ROOT, "build");
const DATA_DIR = data_dir();
install_mods(join(ROOT, "server_mods"), DATA_DIR);
// the server the address field starts with. "" leaves it empty for the player to fill in
const config: { server?: string; client_updates?: string } = JSON.parse(
await Deno.readTextFile(new URL("./config.json", import.meta.url)),
);
const default_server = Deno.env.get("CUBI_SERVER") ?? config.server ?? "";
// the site the client updates from, see client_updates.ts. CUBI_CLIENT_UPDATES=off plays the client the app was
// built with (to test a local build), or it can point at a local server to test updating
const updates_from = (Deno.env.get("CUBI_CLIENT_UPDATES") ?? config.client_updates ?? "off").replace(/\/+$/, "");
// how long the first page waits for an update, a slower one is played next time
const UPDATE_WAIT_MS = 3000;
const client_options = {
source: updates_from,
bundled_dir: BUILD_DIR,
cache_dir: join(DATA_DIR, "client"),
protocol: PROTOCOL_VERSION,
};
const client_dir: Promise<string> = updates_from === "off" || updates_from === ""
? Promise.resolve(BUILD_DIR)
: Promise.race([
update_client(client_options),
new Promise<string>((resolve) => setTimeout(() => resolve(current_client(client_options)), UPDATE_WAIT_MS)),
]);
client_dir.then((dir) => console.log(`Playing the client in ${dir}`));
const win = new Deno.BrowserWindow({ title: "bworld", width: 1280, height: 720 });
guard_shortcuts(win);
const singleplayer = singleplayer_server({
build_dir: BUILD_DIR,
data_dir: DATA_DIR,
on_fatal(message) {
console.error(message);
alert(`The singleplayer world stopped: ${message}`);
Deno.exit(1);
},
});
// 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 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 === "/") {
// ?server= fills in the address field, without it the field would start with this app's own address.
// ?singleplayer shows the main menu instead of the browser's title screen
const start = new URL("/client/", url);
start.searchParams.set("server", default_server);
start.searchParams.set("singleplayer", "");
return Response.redirect(start, 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 });
}
const response = await singleplayer.handle(req, url);
if (response) return response;
return serve_static(await client_dir, req, url);
});