Start maybe working on a native app
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// what the desktop app serves its client from: the client's files (updated from the site, see client_updates.ts),
|
||||
// and singleplayer, whose worlds are played on a game server of the app's own with the mods each world picked from
|
||||
// its data folder (see saves.ts). the client is shown by main.ts in chromium, or by native.ts in a window of its own
|
||||
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 { 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");
|
||||
|
||||
// how long the first page waits for an update, a slower one is played next time
|
||||
const UPDATE_WAIT_MS = 3000;
|
||||
|
||||
export function desktop_app(on_fatal: (message: string) => void) {
|
||||
const data = data_dir();
|
||||
install_mods(join(ROOT, "server_mods"), data);
|
||||
|
||||
// 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(
|
||||
Deno.readTextFileSync(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(/\/+$/, "");
|
||||
const client_options = {
|
||||
source: updates_from,
|
||||
bundled_dir: BUILD_DIR,
|
||||
cache_dir: join(data, "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 singleplayer = singleplayer_server({ build_dir: BUILD_DIR, data_dir: data, on_fatal });
|
||||
|
||||
return {
|
||||
data_dir: data,
|
||||
client_dir,
|
||||
singleplayer,
|
||||
// the client's page at origin. ?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
|
||||
start_page(origin: string) {
|
||||
const start = new URL("/client/", origin);
|
||||
start.searchParams.set("server", default_server);
|
||||
start.searchParams.set("singleplayer", "");
|
||||
return start;
|
||||
},
|
||||
// singleplayer's requests and the client's files
|
||||
async handle(req: Request, url: URL): Promise<Response> {
|
||||
const response = await singleplayer.handle(req, url);
|
||||
if (response) return response;
|
||||
return serve_static(await client_dir, req, url);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user