Files
bworld/server/game/host_protocol.ts
T
2026-10-12 15:09:22 -03:00

56 lines
2.4 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import type { ModData, ModListing } from "$/common/mod_loader.ts";
import type { WorldSave } from "./saves.ts";
// messages between server/main.ts (the host) and the game server worker
// read_chunk's buffer: an i32 the game waits on while it's READ_WAITING, then an i32 the chunk's length, then the
// chunk. a length of -1 is no chunk. READ_TOO_SMALL says the buffer is too small and the length is how big the chunk is
export const READ_WAITING = 0;
export const READ_DONE = 1;
export const READ_TOO_SMALL = 2;
export const READ_HEADER = 8;
export type HostToGame =
| {
type: "init";
// level.json, and where the saved chunks are. the game reads them with read_chunk when it needs them
save: { level?: string; chunks: [number, number][] };
default_seed: string;
// in load order, with the scripts' code since the worker can't read files
mods: {
listing: ModListing;
data: ModData;
server_code?: string;
worldgen_code?: string;
shared_code?: string;
}[];
// the game's ends of channels to the worldgen workers the host started, see worldgen_pool.ts. none means
// generating on the game's thread
worldgen_ports: MessagePort[];
}
// a worldgen worker crashed and the host started it again, this is the new one's port
| { type: "worldgen_port"; index: number; port: MessagePort }
| { type: "connect"; conn: number }
| { type: "message"; conn: number; data: string }
| { type: "disconnect"; conn: number }
// save now and say when it's done, for shutting down
| { type: "shutdown" }
// answers download, with the bytes or why it failed
| { type: "downloaded"; id: number; bytes?: Uint8Array; error?: string };
export type GameToHost =
| { type: "ready"; seed: string }
| { type: "failed"; error: string }
// text is a json message, bytes a binary one
| { type: "send"; conn: number; data: string | Uint8Array<ArrayBuffer> }
| { type: "close"; conn: number }
// level.json and the chunks generated or changed since the last save
| { type: "save"; save: Required<WorldSave>; final: boolean }
// the game waits on buffer for a saved chunk, see READ_WAITING
| { type: "read_chunk"; x: number; z: number; buffer: SharedArrayBuffer }
// the worker can't use the network, so the host fetches urls for it, like skins
| { type: "download"; id: number; url: string; max_bytes: number };