/// // 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. it joins servers like the browser does, and runs its own game server for // singleplayer, like minecraft java. build it with deno task desktop, see the desktop/ docs for how deno desktop works import { fromFileUrl, join } from "@std/path"; import { type Host, serve_static, start_host } 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"; // where the project was built from: build/ and server_mods/ (the .bmod files) are bundled into the app with --include const ROOT = fromFileUrl(new URL("../", import.meta.url)); const BUILD_DIR = join(ROOT, "build"); // 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("BWORLD_SERVER") ?? config.server ?? ""; // the site the client updates from, see client_updates.ts. BWORLD_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("BWORLD_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 = updates_from === "off" || updates_from === "" ? Promise.resolve(BUILD_DIR) : Promise.race([ update_client(client_options), new Promise((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); // the singleplayer game server, started the first time singleplayer connects so players who only join other servers // don't get a world made for them. it plays the .bmod files the app was built with let host: Promise | undefined; function singleplayer_host(): Promise { host ??= (async () => { const worlds = join(data_dir(), "worlds"); Deno.mkdirSync(worlds, { recursive: true }); return await start_host({ build_dir: BUILD_DIR, server_mods_dir: join(ROOT, "server_mods"), world_dir: join(worlds, "world"), on_fatal(message) { console.error(message); alert(`The singleplayer world stopped: ${message}`); Deno.exit(1); }, }); })(); return host; } // 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(); if (host) { try { await (await host).shutdown(); } catch (e) { console.error((e as Error).message); } } 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 singleplayer button, which joins this app's own server 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 }); } // the singleplayer server's socket and its .bmod downloads if (url.pathname === "/ws" || url.pathname.startsWith("/mods/")) { try { return (await singleplayer_host()).handle(req); } catch (e) { // its mods have problems, the title screen shows why the connection failed console.error((e as Error).message); host = undefined; return new Response((e as Error).message, { status: 500 }); } } return serve_static(await client_dir, req, url); }); // where the app keeps its worlds, the usual place for app data on each os function data_dir() { const home = Deno.env.get("HOME") ?? Deno.env.get("USERPROFILE") ?? "."; switch (Deno.build.os) { case "windows": return join(Deno.env.get("APPDATA") ?? join(home, "AppData", "Roaming"), "bworld"); case "darwin": return join(home, "Library", "Application Support", "bworld"); default: return join(Deno.env.get("XDG_DATA_HOME") ?? join(home, ".local", "share"), "bworld"); } }