Save all generated chunks instead of only chunks that were changed
This commit is contained in:
+50
-25
@@ -8,7 +8,8 @@
|
||||
// a chunk's place in those is (x & 31) + (z & 31) * 32. its sectors start with u32 the length of what follows, u8
|
||||
// the compression (2, zlib like minecraft's default), then the compressed chunk from server/game/saves.ts.
|
||||
// big-endian. a chunk saved again goes into sectors nothing uses and the header is written last, so a crash while
|
||||
// saving leaves the chunks as they were instead of half written
|
||||
// saving leaves the chunks as they were instead of half written. the game reads chunks one at a time, when it needs
|
||||
// them
|
||||
import { join } from "@std/path";
|
||||
import type { SavedChunk } from "./game/saves.ts";
|
||||
|
||||
@@ -26,8 +27,8 @@ export function region_file(region_x: number, region_z: number) {
|
||||
return `r.${region_x}.${region_z}.bwr`;
|
||||
}
|
||||
|
||||
// every chunk in the folder's region files, uncompressed. chunks that can't be read are skipped, saying so
|
||||
export async function read_regions(dir: string): Promise<SavedChunk[]> {
|
||||
// where every chunk in the folder's region files is, from their headers
|
||||
export async function list_chunks(dir: string): Promise<[number, number][]> {
|
||||
let entries: Deno.DirEntry[];
|
||||
try {
|
||||
entries = [...Deno.readDirSync(dir)];
|
||||
@@ -35,39 +36,62 @@ export async function read_regions(dir: string): Promise<SavedChunk[]> {
|
||||
if (e instanceof Deno.errors.NotFound) return [];
|
||||
throw e;
|
||||
}
|
||||
const chunks: SavedChunk[] = [];
|
||||
const chunks: [number, number][] = [];
|
||||
for (const entry of entries) {
|
||||
const match = entry.isFile && REGION_FILE.exec(entry.name);
|
||||
if (!match) continue;
|
||||
const [region_x, region_z] = [Number(match[1]), Number(match[2])];
|
||||
const bytes = await Deno.readFile(join(dir, entry.name));
|
||||
if (bytes.length < HEADER_SIZE) {
|
||||
using file = await Deno.open(join(dir, entry.name));
|
||||
const header = new Uint8Array(CHUNKS_PER_REGION * 4);
|
||||
if (await read_at(file, 0, header) < header.length) {
|
||||
console.error(`${entry.name} is too short to be a region file`);
|
||||
continue;
|
||||
}
|
||||
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||
const view = new DataView(header.buffer);
|
||||
for (let i = 0; i < CHUNKS_PER_REGION; i++) {
|
||||
const location = view.getUint32(i * 4);
|
||||
if (location === 0) continue;
|
||||
const x = region_x * REGION_CHUNKS + (i % REGION_CHUNKS);
|
||||
const z = region_z * REGION_CHUNKS + Math.floor(i / REGION_CHUNKS);
|
||||
try {
|
||||
const start = (location >>> 8) * SECTOR;
|
||||
const length = view.getUint32(start);
|
||||
const compression = view.getUint8(start + 4);
|
||||
if (compression !== ZLIB) throw new Error(`unknown compression ${compression}`);
|
||||
if (length < 1 || start + 4 + length > bytes.length) {
|
||||
throw new Error("it goes past the end of the file");
|
||||
}
|
||||
chunks.push({ x, z, data: await inflate(bytes.subarray(start + 5, start + 4 + length)) });
|
||||
} catch (e) {
|
||||
console.error(`Couldn't read chunk ${x}, ${z} from ${entry.name}: ${(e as Error).message}`);
|
||||
}
|
||||
if (view.getUint32(i * 4) === 0) continue;
|
||||
chunks.push([
|
||||
region_x * REGION_CHUNKS + (i % REGION_CHUNKS),
|
||||
region_z * REGION_CHUNKS + Math.floor(i / REGION_CHUNKS),
|
||||
]);
|
||||
}
|
||||
}
|
||||
return chunks;
|
||||
}
|
||||
|
||||
// a chunk from its region file, uncompressed. undefined when it isn't there. throws when it can't be read
|
||||
export async function read_chunk(dir: string, x: number, z: number): Promise<Uint8Array<ArrayBuffer> | undefined> {
|
||||
const name = region_file(Math.floor(x / REGION_CHUNKS), Math.floor(z / REGION_CHUNKS));
|
||||
using file = await open_if_there(join(dir, name));
|
||||
if (!file) return undefined;
|
||||
const location_bytes = new Uint8Array(4);
|
||||
await read_at(file, chunk_index(x, z) * 4, location_bytes);
|
||||
const location = new DataView(location_bytes.buffer).getUint32(0);
|
||||
if (location === 0) return undefined;
|
||||
const sectors = new Uint8Array((location & 0xFF) * SECTOR);
|
||||
const read = await read_at(file, (location >>> 8) * SECTOR, sectors);
|
||||
const view = new DataView(sectors.buffer);
|
||||
if (read < 5) throw new Error(`chunk ${x}, ${z} goes past the end of ${name}`);
|
||||
const length = view.getUint32(0);
|
||||
const compression = view.getUint8(4);
|
||||
if (compression !== ZLIB) throw new Error(`chunk ${x}, ${z} in ${name} has unknown compression ${compression}`);
|
||||
if (length < 1 || 4 + length > read) throw new Error(`chunk ${x}, ${z} goes past the end of ${name}`);
|
||||
return await inflate(sectors.subarray(5, 4 + length));
|
||||
}
|
||||
|
||||
async function open_if_there(path: string) {
|
||||
try {
|
||||
return await Deno.open(path);
|
||||
} catch (e) {
|
||||
if (e instanceof Deno.errors.NotFound) return undefined;
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
function chunk_index(x: number, z: number) {
|
||||
return (x & (REGION_CHUNKS - 1)) + (z & (REGION_CHUNKS - 1)) * REGION_CHUNKS;
|
||||
}
|
||||
|
||||
// saves the chunks into their region files, making them and the folder when needed
|
||||
export async function write_chunks(dir: string, chunks: SavedChunk[]) {
|
||||
const regions = new Map<string, SavedChunk[]>();
|
||||
@@ -115,7 +139,7 @@ async function write_region(path: string, chunks: SavedChunk[]) {
|
||||
bytes.set(data, 5);
|
||||
await write_at(file, first * SECTOR, bytes);
|
||||
|
||||
const index = (chunk.x & (REGION_CHUNKS - 1)) + (chunk.z & (REGION_CHUNKS - 1)) * REGION_CHUNKS;
|
||||
const index = chunk_index(chunk.x, chunk.z);
|
||||
view.setUint32(index * 4, (first << 8) | sectors);
|
||||
view.setUint32(SECTOR + index * 4, now);
|
||||
}
|
||||
@@ -138,7 +162,7 @@ function free_sectors(used: boolean[], count: number) {
|
||||
return start;
|
||||
}
|
||||
|
||||
// reads as much of bytes as the file has there, a new file reads as zeros
|
||||
// reads as much of bytes as the file has there, a new file reads as zeros. returns how much that was
|
||||
async function read_at(file: Deno.FsFile, offset: number, bytes: Uint8Array) {
|
||||
await file.seek(offset, Deno.SeekMode.Start);
|
||||
let read = 0;
|
||||
@@ -147,6 +171,7 @@ async function read_at(file: Deno.FsFile, offset: number, bytes: Uint8Array) {
|
||||
if (n === null) break;
|
||||
read += n;
|
||||
}
|
||||
return read;
|
||||
}
|
||||
|
||||
async function write_at(file: Deno.FsFile, offset: number, bytes: Uint8Array) {
|
||||
|
||||
Reference in New Issue
Block a user