Files
WT-Offline/tools/navgen/package-maps.mts
T

99 lines
4.6 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 { installerDigest } from '../../apps/launcher/src/main/services/app-update/installer-integrity.ts';
import {
MAP_NAV_FORMAT, MapNavManifestSchema, MapNavReleaseManifestSchema, mapNavReleaseFiles, mapNavSignaturePayload,
} from '../../apps/launcher/src/shared/schemas/map-nav-pack.ts';
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 = MapNavManifestSchema.parse({
format: MAP_NAV_FORMAT,
version,
minAppVersion: app.version,
gameVersions: [gameVersion],
createdAt: new Date().toISOString(),
maps: entries,
});
await writeFile(join(pack, 'manifest.json'), JSON.stringify(manifest, null, 2) + '\n');
const files = mapNavReleaseFiles(version);
const archive = join(staging, files.zip);
execFileSync('tar', ['-a', '-c', '-f', archive, '-C', staging, 'map-nav'], { stdio: 'inherit', windowsHide: true });
const releaseManifest = MapNavReleaseManifestSchema.parse({ ...manifest, archive: await installerDigest(archive) });
const bytes = Buffer.from(JSON.stringify(releaseManifest, null, 2) + '\n', 'utf8');
if (privateKey) {
const payload = mapNavSignaturePayload(bytes);
const signature = signEd25519(payload, privateKey);
const publicKey = packSigningPublicKey();
if (!publicKey || !verifyEd25519(payload, signature, publicKey)) {
throw new Error('The map release signature did not verify against the project public key.');
}
await writeFile(join(staging, files.signature), signature);
}
await writeFile(join(staging, files.manifest), bytes);
const release = join(output, version);
await rename(staging, release);
console.log('Packaged ' + entries.length + ' maps in ' + join(release, files.zip));
console.log('Unpacked maps: ' + join(release, 'map-nav'));
}
await main();