Save all generated chunks instead of only chunks that were changed
This commit is contained in:
+50
-15
@@ -2,11 +2,11 @@
|
||||
// server/main.ts runs it as a dedicated server, and the desktop app runs it for singleplayer
|
||||
import { serveDir, serveFile } from "@std/http/file-server";
|
||||
import { join } from "@std/path";
|
||||
import { GameToHost, HostToGame } from "./game/host_protocol.ts";
|
||||
import { GameToHost, HostToGame, READ_DONE, READ_HEADER, READ_TOO_SMALL } from "./game/host_protocol.ts";
|
||||
import { load_bmods } from "./load_bmods.ts";
|
||||
import { read_regions, write_chunks } from "./regions.ts";
|
||||
import { list_chunks, read_chunk, write_chunks } from "./regions.ts";
|
||||
import { default_worldgen_workers, start_worldgen_workers } from "./worldgen_workers.ts";
|
||||
import type { SavedChunk, WorldSave } from "./game/saves.ts";
|
||||
import type { SavedChunk } from "./game/saves.ts";
|
||||
|
||||
const MAX_MESSAGE_SIZE = 4096;
|
||||
// in build/, see build.ts
|
||||
@@ -62,8 +62,9 @@ export async function start_host(options: HostOptions): Promise<Host> {
|
||||
let on_final_save: ((error?: Error) => void) | undefined;
|
||||
// saves are written one after another, in order
|
||||
let saving = Promise.resolve();
|
||||
// chunks from saves that couldn't be written, tried again with the next one
|
||||
let unwritten = new Map<string, SavedChunk>();
|
||||
// chunks from saves that aren't written yet, by "x,z": the game reads them from here. ones that couldn't be written
|
||||
// are tried again with the next save
|
||||
const unwritten = new Map<string, SavedChunk>();
|
||||
|
||||
game.onmessage = (event: MessageEvent<GameToHost>) => {
|
||||
const message = event.data;
|
||||
@@ -86,14 +87,16 @@ export async function start_host(options: HostOptions): Promise<Host> {
|
||||
break;
|
||||
case "save": {
|
||||
const { save, final } = message;
|
||||
for (const chunk of save.chunks) unwritten.set(`${chunk.x},${chunk.z}`, chunk);
|
||||
saving = saving.then(async () => {
|
||||
const chunks = new Map(unwritten);
|
||||
for (const chunk of save.chunks) chunks.set(`${chunk.x},${chunk.z}`, chunk);
|
||||
try {
|
||||
await write_world(options.world_dir, { level: save.level, chunks: [...chunks.values()] });
|
||||
unwritten = new Map();
|
||||
// unless a later save has them again
|
||||
for (const [key, chunk] of chunks) {
|
||||
if (unwritten.get(key) === chunk) unwritten.delete(key);
|
||||
}
|
||||
} catch (e) {
|
||||
unwritten = chunks;
|
||||
console.error(`Couldn't save the world: ${(e as Error).message}`);
|
||||
if (final) on_final_save?.(e as Error);
|
||||
return;
|
||||
@@ -105,6 +108,19 @@ export async function start_host(options: HostOptions): Promise<Host> {
|
||||
});
|
||||
break;
|
||||
}
|
||||
case "read_chunk": {
|
||||
const { x, z, buffer } = message;
|
||||
const unwritten_chunk = unwritten.get(`${x},${z}`);
|
||||
(unwritten_chunk
|
||||
? Promise.resolve(unwritten_chunk.data)
|
||||
: read_chunk(region_dir(options.world_dir), x, z))
|
||||
.catch((e) => {
|
||||
console.error(`Couldn't read chunk ${x}, ${z}: ${(e as Error).message}`);
|
||||
return undefined;
|
||||
})
|
||||
.then((data) => answer_chunk_read(buffer, data));
|
||||
break;
|
||||
}
|
||||
case "download":
|
||||
download(message.url, message.max_bytes).then(
|
||||
(bytes) => post({ type: "downloaded", id: message.id, bytes }),
|
||||
@@ -120,6 +136,7 @@ export async function start_host(options: HostOptions): Promise<Host> {
|
||||
};
|
||||
|
||||
const save = await read_world(options.world_dir);
|
||||
if (save.chunks.length) console.log(`${save.chunks.length} chunks saved`);
|
||||
game.postMessage(
|
||||
{
|
||||
type: "init",
|
||||
@@ -134,7 +151,7 @@ export async function start_host(options: HostOptions): Promise<Host> {
|
||||
})),
|
||||
worldgen_ports: worldgen.ports,
|
||||
} satisfies HostToGame,
|
||||
[...save.chunks.map((chunk) => chunk.data.buffer), ...worldgen.ports],
|
||||
worldgen.ports,
|
||||
);
|
||||
|
||||
function handle_socket(socket: WebSocket) {
|
||||
@@ -271,20 +288,38 @@ async function download(url: string, max_bytes: number): Promise<Uint8Array> {
|
||||
const LEVEL_FILE = "level.json";
|
||||
const REGION_DIR = "region";
|
||||
|
||||
// a folder that isn't there is a new world
|
||||
async function read_world(dir: string): Promise<WorldSave> {
|
||||
function region_dir(world_dir: string) {
|
||||
return join(world_dir, REGION_DIR);
|
||||
}
|
||||
|
||||
// level.json and where the saved chunks are. a folder that isn't there is a new world
|
||||
async function read_world(dir: string): Promise<{ level?: string; chunks: [number, number][] }> {
|
||||
let level: string | undefined;
|
||||
try {
|
||||
level = Deno.readTextFileSync(join(dir, LEVEL_FILE));
|
||||
} catch (e) {
|
||||
if (!(e instanceof Deno.errors.NotFound)) throw e;
|
||||
}
|
||||
return { level, chunks: level ? await read_regions(join(dir, REGION_DIR)) : [] };
|
||||
return { level, chunks: level ? await list_chunks(region_dir(dir)) : [] };
|
||||
}
|
||||
|
||||
// the chunks first, level.json says which world generation they were saved with
|
||||
async function write_world(dir: string, save: Required<WorldSave>) {
|
||||
await write_chunks(join(dir, REGION_DIR), save.chunks);
|
||||
// gives the game waiting in read_chunk the chunk, see READ_WAITING
|
||||
function answer_chunk_read(buffer: SharedArrayBuffer, data: Uint8Array | undefined) {
|
||||
const header = new Int32Array(buffer, 0, 2);
|
||||
if (data && READ_HEADER + data.length > buffer.byteLength) {
|
||||
header[1] = data.length;
|
||||
Atomics.store(header, 0, READ_TOO_SMALL);
|
||||
} else {
|
||||
if (data) new Uint8Array(buffer, READ_HEADER).set(data);
|
||||
header[1] = data ? data.length : -1;
|
||||
Atomics.store(header, 0, READ_DONE);
|
||||
}
|
||||
Atomics.notify(header, 0);
|
||||
}
|
||||
|
||||
// the chunks first, then level.json
|
||||
async function write_world(dir: string, save: { level: string; chunks: SavedChunk[] }) {
|
||||
await write_chunks(region_dir(dir), save.chunks);
|
||||
Deno.mkdirSync(dir, { recursive: true });
|
||||
// write then rename so a crash mid write doesnt eat the world
|
||||
const level = join(dir, LEVEL_FILE);
|
||||
|
||||
Reference in New Issue
Block a user