Download for desktop app
This commit is contained in:
@@ -38,6 +38,10 @@ export class TitleScreen extends GuiScreen {
|
||||
? new Button("Singleplayer", WIDTH, ROW_HEIGHT, () => this.#join_singleplayer())
|
||||
: undefined;
|
||||
credits_button = new Button("Credits", WIDTH, ROW_HEIGHT, () => this.#open_credits());
|
||||
// in the browser, the desktop app for this computer's os
|
||||
download_button = has_singleplayer()
|
||||
? undefined
|
||||
: new Button(`Download for ${download_os().name}`, WIDTH, ROW_HEIGHT, download_desktop_app);
|
||||
// open over the title screen, it gets the input while it's there
|
||||
#credits: CreditsScreen | undefined;
|
||||
|
||||
@@ -82,6 +86,7 @@ export class TitleScreen extends GuiScreen {
|
||||
this.singleplayer_button?.handle_input();
|
||||
this.join_button.handle_input();
|
||||
this.credits_button.handle_input();
|
||||
this.download_button?.handle_input();
|
||||
}
|
||||
|
||||
on_render(): void {
|
||||
@@ -101,11 +106,12 @@ export class TitleScreen extends GuiScreen {
|
||||
this.server.render();
|
||||
this.join_button.render();
|
||||
this.credits_button.render();
|
||||
this.download_button?.render();
|
||||
|
||||
if (this.status) {
|
||||
const width = measure_text(this.status, TEXT_SCALE);
|
||||
const x = (canvas.width - width) / 2;
|
||||
const y = this.credits_button.y + ROW_HEIGHT + GAP;
|
||||
const y = (this.download_button ?? this.credits_button).y + ROW_HEIGHT + GAP;
|
||||
draw_rect(x - 8, y - 4, width + 16, TEXT_HEIGHT * TEXT_SCALE + 8, [0, 0, 0, 0.5]);
|
||||
draw_text(this.status, x, y, TEXT_SCALE, this.status_is_error ? [1, 0.45, 0.45, 1] : [1, 1, 1, 1]);
|
||||
}
|
||||
@@ -128,7 +134,9 @@ export class TitleScreen extends GuiScreen {
|
||||
#layout() {
|
||||
const x = (canvas.width - WIDTH) / 2;
|
||||
const singleplayer_height = this.singleplayer_button ? ROW_HEIGHT + GAP : 0;
|
||||
const total = 2 * (LABEL_GAP + ROW_HEIGHT + GAP) + singleplayer_height + 2 * ROW_HEIGHT + GAP;
|
||||
const download_height = this.download_button ? ROW_HEIGHT + GAP : 0;
|
||||
const total = 2 * (LABEL_GAP + ROW_HEIGHT + GAP) + singleplayer_height + 2 * ROW_HEIGHT + GAP +
|
||||
download_height;
|
||||
let y = (canvas.height - total) / 2 + 40;
|
||||
for (const field of [this.name, this.server]) {
|
||||
field.x = x;
|
||||
@@ -144,6 +152,10 @@ export class TitleScreen extends GuiScreen {
|
||||
this.join_button.y = y;
|
||||
this.credits_button.x = x;
|
||||
this.credits_button.y = y + ROW_HEIGHT + GAP;
|
||||
if (this.download_button) {
|
||||
this.download_button.x = x;
|
||||
this.download_button.y = this.credits_button.y + ROW_HEIGHT + GAP;
|
||||
}
|
||||
}
|
||||
|
||||
#label(text: string, field: EditBox) {
|
||||
@@ -232,6 +244,21 @@ function has_singleplayer() {
|
||||
return new URL(location.href).searchParams.has("singleplayer");
|
||||
}
|
||||
|
||||
// the desktop app's build for the os the browser is on, next to the page in dist/. everything that isn't windows
|
||||
// gets linux's
|
||||
function download_os() {
|
||||
return /windows/i.test(navigator.userAgent)
|
||||
? { name: "Windows", file: "dist/windows.zip" }
|
||||
: { name: "Linux", file: "dist/linux.zip" };
|
||||
}
|
||||
|
||||
function download_desktop_app() {
|
||||
const link = document.createElement("a");
|
||||
link.href = download_os().file;
|
||||
link.download = "";
|
||||
link.click();
|
||||
}
|
||||
|
||||
// a server's mods can't be unloaded, so going back to the title screen starts the page over. without ?server=
|
||||
// or ?name=, so the fields show what was used last. the desktop app keeps its ?singleplayer and ?server=, its saved
|
||||
// fields don't last between launches and the page's own address isn't a server to join
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
"check-mods": "deno run --allow-read --allow-run tools/check_mods.ts",
|
||||
"pack-mod": "deno run --allow-read --allow-write --allow-run --allow-env tools/pack_mod.ts",
|
||||
"test": "deno test --allow-read --allow-write --allow-run tests/",
|
||||
"desktop": "deno run -A build.ts --once && deno desktop --allow-read --allow-write --allow-net --allow-ffi --allow-env=BWORLD_SERVER,BWORLD_CLIENT_UPDATES,HOME,USERPROFILE,APPDATA,XDG_DATA_HOME,DEBUG --include build --include server_mods --include server/game/worker.ts --include desktop/config.json desktop/main.ts && deno run --allow-read --allow-write desktop/linux_launcher.ts dist/bworld"
|
||||
"desktop": "deno run -A build.ts --once && deno desktop --allow-read --allow-write --allow-net --allow-ffi --allow-env=BWORLD_SERVER,BWORLD_CLIENT_UPDATES,HOME,USERPROFILE,APPDATA,XDG_DATA_HOME,DEBUG --include build --include server_mods --include server/game/worker.ts --include desktop/config.json desktop/main.ts && deno run --allow-read --allow-write desktop/linux_launcher.ts dist/bworld",
|
||||
"desktop-all": "deno run -A tools/desktop_all.ts"
|
||||
},
|
||||
"compilerOptions": {
|
||||
"lib": ["dom", "dom.asynciterable", "dom.iterable", "deno.ns", "deno.unstable", "deno.webgpu"]
|
||||
|
||||
+42
-29
@@ -3,10 +3,6 @@
|
||||
// so this moves the launcher to bworld-bin and puts a script in its place that starts it with the switches webgpu
|
||||
// needs on some linux setups. run it on the app folder after deno desktop builds it; running it again does nothing
|
||||
|
||||
if (Deno.build.os !== "linux") {
|
||||
Deno.exit(0);
|
||||
}
|
||||
|
||||
const SWITCHES = [
|
||||
"--enable-unsafe-webgpu",
|
||||
"--ozone-platform=x11",
|
||||
@@ -14,30 +10,36 @@ const SWITCHES = [
|
||||
"--enable-features=Vulkan,VulkanFromANGLE",
|
||||
];
|
||||
|
||||
const dir = Deno.args[0];
|
||||
if (!dir) {
|
||||
console.error("usage: linux_launcher.ts <app folder>, like dist/bworld");
|
||||
Deno.exit(1);
|
||||
}
|
||||
const name = dir.replace(/\/+$/, "").split("/").pop()!;
|
||||
const launcher = `${dir}/${name}`;
|
||||
const binary = `${launcher}-bin`;
|
||||
|
||||
// already wrapped: the launcher is a script now
|
||||
const head = new Uint8Array(4);
|
||||
const file = Deno.openSync(launcher);
|
||||
file.readSync(head);
|
||||
file.close();
|
||||
const is_elf = head[0] === 0x7f && head[1] === 0x45 && head[2] === 0x4c && head[3] === 0x46;
|
||||
if (!is_elf) {
|
||||
console.log(`${launcher} is already wrapped`);
|
||||
Deno.exit(0);
|
||||
if (import.meta.main) {
|
||||
// on its own it's run after building for the os it's on, which only needs wrapping on linux
|
||||
if (Deno.build.os !== "linux") {
|
||||
Deno.exit(0);
|
||||
}
|
||||
const dir = Deno.args[0];
|
||||
if (!dir) {
|
||||
console.error("usage: linux_launcher.ts <app folder>, like dist/bworld");
|
||||
Deno.exit(1);
|
||||
}
|
||||
wrap_linux_launcher(dir);
|
||||
}
|
||||
|
||||
Deno.renameSync(launcher, binary);
|
||||
Deno.writeTextFileSync(
|
||||
launcher,
|
||||
`#!/bin/sh
|
||||
// wraps the launcher of a linux build in dir. it works from any os, see tools/desktop_all.ts, but the script is only
|
||||
// made executable on unix: zipping it from windows has to set that itself
|
||||
export function wrap_linux_launcher(dir: string) {
|
||||
const name = dir.replace(/[\/\\]+$/, "").split(/[\/\\]/).pop()!;
|
||||
const launcher = `${dir}/${name}`;
|
||||
const binary = `${launcher}-bin`;
|
||||
|
||||
// already wrapped: the launcher is a script now
|
||||
if (!is_elf(launcher)) {
|
||||
console.log(`${launcher} is already wrapped`);
|
||||
return;
|
||||
}
|
||||
|
||||
Deno.renameSync(launcher, binary);
|
||||
Deno.writeTextFileSync(
|
||||
launcher,
|
||||
`#!/bin/sh
|
||||
# starts ${name} with the chromium switches webgpu needs on some linux setups, see desktop/linux_launcher.ts.
|
||||
# BWORLD_CHROMIUM_FLAGS replaces them, set it to "" to start without any
|
||||
here="$(dirname "$(readlink -f "$0")")"
|
||||
@@ -45,6 +47,17 @@ here="$(dirname "$(readlink -f "$0")")"
|
||||
export LAUFEY_RUNTIME_PATH="$here/${name}.so"
|
||||
exec "$here/${name}-bin" \${BWORLD_CHROMIUM_FLAGS-${SWITCHES.join(" ")}} "$@"
|
||||
`,
|
||||
);
|
||||
Deno.chmodSync(launcher, 0o755);
|
||||
console.log(`Wrapped ${launcher}: ${SWITCHES.join(" ")}`);
|
||||
);
|
||||
if (Deno.build.os !== "windows") {
|
||||
Deno.chmodSync(launcher, 0o755);
|
||||
}
|
||||
console.log(`Wrapped ${launcher}: ${SWITCHES.join(" ")}`);
|
||||
}
|
||||
|
||||
export function is_elf(path: string) {
|
||||
const head = new Uint8Array(4);
|
||||
const file = Deno.openSync(path);
|
||||
file.readSync(head);
|
||||
file.close();
|
||||
return head[0] === 0x7f && head[1] === 0x45 && head[2] === 0x4c && head[3] === 0x46;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
// deno task desktop-all
|
||||
// builds the desktop app for windows and linux, from whichever of them it runs on, and zips each into
|
||||
// dist/windows.zip and dist/linux.zip for the title screen's download button. each zip has the app's bworld/ folder
|
||||
import { walk } from "@std/fs";
|
||||
import { relative } from "@std/path";
|
||||
import { Zip, ZipDeflate } from "fflate";
|
||||
import { is_elf, wrap_linux_launcher } from "../desktop/linux_launcher.ts";
|
||||
|
||||
const APP_NAME = "bworld";
|
||||
const TARGETS = [
|
||||
{ name: "windows", target: "x86_64-pc-windows-msvc" },
|
||||
{ name: "linux", target: "x86_64-unknown-linux-gnu" },
|
||||
];
|
||||
// same as deno task desktop
|
||||
const DESKTOP_ARGS = [
|
||||
"--allow-read",
|
||||
"--allow-write",
|
||||
"--allow-net",
|
||||
"--allow-ffi",
|
||||
"--allow-env=BWORLD_SERVER,BWORLD_CLIENT_UPDATES,HOME,USERPROFILE,APPDATA,XDG_DATA_HOME,DEBUG",
|
||||
"--include",
|
||||
"build",
|
||||
"--include",
|
||||
"server_mods",
|
||||
"--include",
|
||||
"server/game/worker.ts",
|
||||
"--include",
|
||||
"desktop/config.json",
|
||||
"desktop/main.ts",
|
||||
];
|
||||
|
||||
async function run(args: string[]) {
|
||||
const { success } = await new Deno.Command(Deno.execPath(), { args, stdout: "inherit", stderr: "inherit" })
|
||||
.output();
|
||||
if (!success) {
|
||||
throw new Error(`deno ${args.join(" ")} failed`);
|
||||
}
|
||||
}
|
||||
|
||||
function remove(path: string) {
|
||||
try {
|
||||
Deno.removeSync(path, { recursive: true });
|
||||
} catch (e) {
|
||||
if (!(e instanceof Deno.errors.NotFound)) throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// zip is built on any os, so linux's executables are found by what they start with: elf binaries and scripts
|
||||
function is_executable(path: string) {
|
||||
if (is_elf(path)) return true;
|
||||
const head = new Uint8Array(2);
|
||||
const file = Deno.openSync(path);
|
||||
file.readSync(head);
|
||||
file.close();
|
||||
return head[0] === 0x23 && head[1] === 0x21; // #!
|
||||
}
|
||||
|
||||
// zips folder into out with the folder itself at its root. unix_modes keeps linux's executables executable
|
||||
async function zip_folder(folder: string, out: string, unix_modes: boolean) {
|
||||
const parent = `${folder}/..`;
|
||||
const output = await Deno.open(out, { write: true, create: true, truncate: true });
|
||||
// the zip's chunks are written one after another, in order
|
||||
let writes = Promise.resolve();
|
||||
const done = Promise.withResolvers<void>();
|
||||
const zip = new Zip((error, chunk, final) => {
|
||||
if (error) {
|
||||
done.reject(error);
|
||||
return;
|
||||
}
|
||||
writes = writes.then(() => write_all(output, chunk));
|
||||
if (final) done.resolve();
|
||||
});
|
||||
|
||||
for await (const entry of walk(folder, { includeDirs: false })) {
|
||||
const file = new ZipDeflate(relative(parent, entry.path).replaceAll("\\", "/"), { level: 6 });
|
||||
if (unix_modes) {
|
||||
file.os = 3;
|
||||
file.attrs = (is_executable(entry.path) ? 0o100755 : 0o100644) << 16;
|
||||
}
|
||||
zip.add(file);
|
||||
file.push(await Deno.readFile(entry.path), true);
|
||||
}
|
||||
zip.end();
|
||||
try {
|
||||
await done.promise;
|
||||
await writes;
|
||||
} finally {
|
||||
output.close();
|
||||
}
|
||||
}
|
||||
|
||||
async function write_all(file: Deno.FsFile, data: Uint8Array) {
|
||||
for (let written = 0; written < data.length;) {
|
||||
written += await file.write(data.subarray(written));
|
||||
}
|
||||
}
|
||||
|
||||
await run(["run", "-A", "build.ts", "--once"]);
|
||||
for (const { name, target } of TARGETS) {
|
||||
const dir = `dist/${name}`;
|
||||
const app = `${dir}/${APP_NAME}`;
|
||||
remove(dir);
|
||||
console.log(`Building ${name} (${target})`);
|
||||
await run(["desktop", "--target", target, "--output", app, ...DESKTOP_ARGS]);
|
||||
if (name === "linux") {
|
||||
wrap_linux_launcher(app);
|
||||
}
|
||||
const zip = `dist/${name}.zip`;
|
||||
await zip_folder(app, zip, name === "linux");
|
||||
console.log(`Zipped ${zip}`);
|
||||
}
|
||||
Reference in New Issue
Block a user