93 lines
4.1 KiB
TypeScript
93 lines
4.1 KiB
TypeScript
/**
|
|
* Package the baked navigation data for the launcher data directory.
|
|
* node tools/navgen/package-maps.mts --maps <folder> --inventory <pois folder> --out <folder>
|
|
* --version <YYYY.MM.DD[.N]> --game-version <x.y.z.n> [--unsigned]
|
|
*
|
|
* Signed packages use the project's PATCH_SIGNING_KEY. The unpacked map-nav directory is also
|
|
* usable locally. Each release archive contains that directory, ready to extract into the data root.
|
|
*/
|
|
import { execFileSync } from 'node:child_process';
|
|
import { createHash } from 'node:crypto';
|
|
import { mkdir, mkdtemp, readFile, readdir, rename, writeFile } from 'node:fs/promises';
|
|
import { join, resolve } from 'node:path';
|
|
import { parseArgs } from 'node:util';
|
|
import { isPackVersion, packSigningPublicKey, signEd25519, verifyEd25519 } from '@wto/patcher';
|
|
import { readNavPack } from '../../packages/ground-sim/src/map/wtonav.ts';
|
|
|
|
const { values } = parseArgs({
|
|
options: {
|
|
maps: { type: 'string' },
|
|
inventory: { type: 'string' },
|
|
out: { type: 'string' },
|
|
version: { type: 'string' },
|
|
'game-version': { type: 'string' },
|
|
unsigned: { type: 'boolean', default: false },
|
|
},
|
|
});
|
|
|
|
async function main(): Promise<void> {
|
|
const { maps, inventory, out, version, 'game-version': gameVersion } = values;
|
|
if (!maps || !inventory || !out || !version || !isPackVersion(version) || !gameVersion ||
|
|
!/^\d+\.\d+\.\d+\.\d+$/.test(gameVersion)) {
|
|
throw new Error('Supply --maps, --inventory, --out, --version YYYY.MM.DD[.N] and --game-version x.y.z.n.');
|
|
}
|
|
const privateKey = values.unsigned ? undefined : process.env['PATCH_SIGNING_KEY'];
|
|
if (!privateKey && !values.unsigned) throw new Error('Set PATCH_SIGNING_KEY or use --unsigned for local use.');
|
|
const output = resolve(out);
|
|
const levels = (await readdir(inventory)).filter((file) => /^[a-z0-9_]+\.pois\.json$/.test(file))
|
|
.map((file) => file.slice(0, -'.pois.json'.length)).sort();
|
|
if (levels.length === 0) throw new Error('The mission inventory is empty.');
|
|
|
|
await mkdir(output, { recursive: true });
|
|
const staging = await mkdtemp(join(output, 'map-nav-stage-'));
|
|
const pack = join(staging, 'map-nav');
|
|
await mkdir(join(pack, 'maps'), { recursive: true });
|
|
const entries = [];
|
|
for (const level of levels) {
|
|
const filename = level + '.wtonav';
|
|
const bytes = await readFile(join(maps, filename));
|
|
const { header, tiers } = readNavPack(bytes);
|
|
if (tiers.some((tier) => tier.polyCount === 0) || /^0+$/.test(header.everSha1)) {
|
|
throw new Error('Incomplete navigation data for ' + level);
|
|
}
|
|
entries.push({
|
|
level,
|
|
everSha1: header.everSha1,
|
|
size: bytes.length,
|
|
sha256: createHash('sha256').update(bytes).digest('hex'),
|
|
covers: header.coverCount,
|
|
});
|
|
await writeFile(join(pack, 'maps', filename), bytes);
|
|
}
|
|
|
|
const app = JSON.parse(await readFile(new URL('../../apps/launcher/package.json', import.meta.url), 'utf8')) as { version: string };
|
|
const manifest = JSON.stringify({
|
|
format: 1,
|
|
version,
|
|
minAppVersion: app.version,
|
|
gameVersions: [gameVersion],
|
|
createdAt: new Date().toISOString(),
|
|
maps: entries,
|
|
}, null, 2) + '\n';
|
|
await writeFile(join(pack, 'manifest.json'), manifest);
|
|
const stem = 'wtoffline-map-nav-' + version;
|
|
const archive = join(staging, stem + '.zip');
|
|
execFileSync('tar', ['-a', '-c', '-f', archive, '-C', staging, 'map-nav'], { stdio: 'inherit', windowsHide: true });
|
|
if (privateKey) {
|
|
const bytes = await readFile(archive);
|
|
const signature = signEd25519(bytes, privateKey);
|
|
const publicKey = packSigningPublicKey();
|
|
if (!publicKey || !verifyEd25519(bytes, signature, publicKey)) {
|
|
throw new Error('The map archive signature did not verify against the project public key.');
|
|
}
|
|
await writeFile(join(staging, stem + '.zip.sig'), signature);
|
|
}
|
|
await writeFile(join(staging, stem + '.manifest.json'), manifest);
|
|
const release = join(output, version);
|
|
await rename(staging, release);
|
|
console.log('Packaged ' + entries.length + ' maps in ' + join(release, stem + '.zip'));
|
|
console.log('Unpacked maps: ' + join(release, 'map-nav'));
|
|
}
|
|
|
|
await main();
|