Export schematics

This commit is contained in:
2026-11-15 18:41:22 -03:00
parent 2fa109026c
commit 59bfed58bf
18 changed files with 511 additions and 26 deletions
+32 -17
View File
@@ -19,22 +19,43 @@ const CHUNK_READ_TIMEOUT_MS = 30_000;
let game: GameServer | undefined;
let starting: Promise<void> | undefined;
// downloads waiting for the host, by id
const downloads = new Map<number, { resolve: (bytes: Uint8Array) => void; reject: (error: Error) => void }>();
let next_download = 1;
// what the game asked the host for and waits on, downloads and schematic files, by id
const requests = new Map<number, { resolve: (value: never) => void; reject: (error: Error) => void }>();
let next_request = 1;
// where the host puts saved chunks, grown when one doesn't fit
let chunk_buffer = new SharedArrayBuffer(READ_HEADER + 256 * 1024);
function post(message: GameToHost) {
// the chunks are moved to the host, not copied
// the chunks and other bytes are moved to the host, not copied
const transfer = message.type === "save"
? message.save.chunks.map((chunk) => chunk.data.buffer)
: message.type === "send" && typeof message.data !== "string"
? [message.data.buffer]
: message.type === "write_schematic"
? [message.bytes.buffer]
: [];
self.postMessage(message, transfer);
}
function request<T>(message: (id: number) => GameToHost): Promise<T> {
return new Promise((resolve, reject) => {
const id = next_request++;
requests.set(id, { resolve: resolve as (value: never) => void, reject });
post(message(id));
});
}
// the host's answer to a request, with what was asked for, or undefined and why not
function answer(id: number, value: unknown, error: string) {
const pending = requests.get(id);
requests.delete(id);
if (value !== undefined) {
pending?.resolve(value as never);
} else {
pending?.reject(new Error(error));
}
}
// a saved chunk from the host. the game waits for it, like minecraft's server waits for a chunk it needs right away,
// which takes much less than generating it
function read_chunk(x: number, z: number): Uint8Array | undefined {
@@ -69,13 +90,11 @@ self.onmessage = async (event: MessageEvent<HostToGame>) => {
return;
}
if (message.type === "downloaded") {
const pending = downloads.get(message.id);
downloads.delete(message.id);
if (message.bytes) {
pending?.resolve(message.bytes);
} else {
pending?.reject(new Error(message.error ?? "Couldn't download it"));
}
answer(message.id, message.bytes, message.error ?? "Couldn't download it");
return;
}
if (message.type === "wrote_schematic") {
answer(message.id, message.path, message.error ?? "Couldn't write it");
return;
}
@@ -107,12 +126,8 @@ async function init(message: Extract<HostToGame, { type: "init" }>) {
{
send: (conn, data) => post({ type: "send", conn, data }),
close: (conn) => post({ type: "close", conn }),
download: (url, max_bytes) =>
new Promise((resolve, reject) => {
const id = next_download++;
downloads.set(id, { resolve, reject });
post({ type: "download", id, url, max_bytes });
}),
download: (url, max_bytes) => request((id) => ({ type: "download", id, url, max_bytes })),
write_schematic: (bytes) => request((id) => ({ type: "write_schematic", id, bytes })),
},
{ level: message.save.level, chunks: { list: message.save.chunks, read: read_chunk } },
message.default_seed,