Improve desktop app !
This commit is contained in:
+31
-10
@@ -5,17 +5,38 @@
|
||||
// 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";
|
||||
|
||||
// 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 } = JSON.parse(
|
||||
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<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 });
|
||||
|
||||
// the singleplayer game server, started the first time singleplayer connects so players who only join other servers
|
||||
@@ -40,16 +61,16 @@ function singleplayer_host(): Promise<Host> {
|
||||
return host;
|
||||
}
|
||||
|
||||
// the world gets saved before the app closes
|
||||
// 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) => {
|
||||
if (!host) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
try {
|
||||
await (await host).shutdown();
|
||||
} catch (e) {
|
||||
console.error((e as Error).message);
|
||||
if (host) {
|
||||
try {
|
||||
await (await host).shutdown();
|
||||
} catch (e) {
|
||||
console.error((e as Error).message);
|
||||
}
|
||||
}
|
||||
Deno.exit(0);
|
||||
});
|
||||
@@ -76,7 +97,7 @@ Deno.serve(async (req) => {
|
||||
return new Response((e as Error).message, { status: 500 });
|
||||
}
|
||||
}
|
||||
return serve_static(BUILD_DIR, req, url);
|
||||
return serve_static(await client_dir, req, url);
|
||||
});
|
||||
|
||||
// where the app keeps its worlds, the usual place for app data on each os
|
||||
|
||||
Reference in New Issue
Block a user