Files
MineGeo/scripts/render-map.mjs

30 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { readFile, mkdir, mkdtemp, copyFile, rm } from "node:fs/promises";
import { dirname, resolve, sep } from "node:path";
import { gunzipSync } from "node:zlib";
import { renderSnapshot } from "../renderer/render.mjs";
const [input, output] = process.argv.slice(2);
if (!input || !output)
throw new Error(
"Usage: npm run render:map -- <terrain.json.gz> <output.webp>",
);
const terrain = JSON.parse(
gunzipSync(await readFile(input), {
maxOutputLength: 256 * 1024 * 1024,
}).toString(),
);
const parent = dirname(resolve(output));
await mkdir(parent, { recursive: true });
const temporary = await mkdtemp(resolve(parent, ".minegeo-map-"));
try {
const report = await renderSnapshot(terrain, [], temporary);
await copyFile(resolve(temporary, "map.webp"), resolve(output));
console.log(
`Rendered ${report.map.width} × ${report.map.height} aerial map from Y=${report.map.cameraY}: ${output}`,
);
} finally {
if (!temporary.startsWith(parent + sep))
throw new Error("Refusing to remove a map directory outside its parent");
await rm(temporary, { recursive: true, force: true });
}