Read rank thresholds and available avatar artwork from the selected copy. Persist avatar selections in the player profile and preserve free research points when a level changes.
54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
/**
|
|
* Read BLK documents out of one of the player's game data packs (the
|
|
* game's own files are the source of every static fact; nothing is shipped with us).
|
|
*/
|
|
import { createHash } from 'node:crypto';
|
|
import { readFileSync } from 'node:fs';
|
|
import { decodeBlk, parseSharedNameMap, type BlkBlock, type SharedNameContext } from '@wto/blk';
|
|
import { getFile, NAME_MAP_PATH, readArchive, type Archive } from '@wto/vromfs';
|
|
|
|
export class GamePack {
|
|
readonly #archive: Archive;
|
|
readonly #shared: SharedNameContext;
|
|
|
|
private constructor(archive: Archive) {
|
|
this.#archive = archive;
|
|
const dictionary = findDictionary(archive);
|
|
const names = archive.files.has(NAME_MAP_PATH) ? parseSharedNameMap(getFile(archive, NAME_MAP_PATH), dictionary).names : [];
|
|
this.#shared = { names, dictionary };
|
|
}
|
|
|
|
static open(packPath: string): GamePack {
|
|
return new GamePack(readArchive(readFileSync(packPath)));
|
|
}
|
|
|
|
readBlk(pathInPack: string): BlkBlock {
|
|
return decodeBlk(getFile(this.#archive, pathInPack), this.#shared);
|
|
}
|
|
|
|
readText(pathInPack: string): string {
|
|
return Buffer.from(this.readBytes(pathInPack)).toString('utf8');
|
|
}
|
|
|
|
readBytes(pathInPack: string): Uint8Array {
|
|
return getFile(this.#archive, pathInPack);
|
|
}
|
|
|
|
/** Every file path in the pack, sorted. */
|
|
paths(): string[] {
|
|
return [...this.#archive.files.keys()].sort();
|
|
}
|
|
|
|
/** SHA-1 of one file's content, hex; undefined when the pack has no such file. */
|
|
fileDigest(pathInPack: string): string | undefined {
|
|
const content = this.#archive.files.get(pathInPack);
|
|
return content ? createHash('sha1').update(content).digest('hex') : undefined;
|
|
}
|
|
}
|
|
|
|
function findDictionary(archive: Archive): Uint8Array | undefined {
|
|
const dictionaries = [...archive.files.keys()].filter((path) => path.endsWith('.dict'));
|
|
if (dictionaries.length > 1) throw new Error(`the data pack has several zstd dictionaries: ${dictionaries.join(', ')}`);
|
|
return dictionaries.length ? archive.files.get(dictionaries[0]!) : undefined;
|
|
}
|