Files
WT-Offline/packages/blk/src/shared-name-map.ts
T
N0VAN0M4LY c3914ec0d6 feat(patcher): build a loopback client copy with TypeScript
Add the shared BLK, VROMFS, executable patch, and build packages. Preserve
typed fields and shared name tables when reading BLK data. Rewrite the
service endpoints in place so the original environment names remain
available to the client, then build the client copy.
2026-11-24 00:35:13 +08:00

31 lines
1.4 KiB
TypeScript

/**
* The shared name map of a data pack: the file named "nm" inside a .vromfs.bin.
*
* BLK documents with format labels 3, 4 and 5 refer to these names instead of carrying
* their own (format.ts). Layout, checked against the stock 2.61.0.37 char.vromfs.bin:
* 8 bytes hash of the names
* 32 bytes identifier of the pack's zstd dictionary; all zero when the pack has none
* rest zstd frame holding a name table (binary/name-table.ts)
*/
import { BlkFormatError } from './errors.ts';
import { ByteReader } from './binary/byte-reader.ts';
import { decompress } from './binary/format.ts';
import { readNameTable } from './binary/name-table.ts';
const HASH_SIZE = 8;
const DICTIONARY_ID_SIZE = 32;
export interface SharedNameMap {
readonly names: readonly string[];
/** Hex identifier of the pack's zstd dictionary, or null when the pack has none. */
readonly dictionaryId: string | null;
}
export function parseSharedNameMap(file: Uint8Array, dictionary?: Uint8Array): SharedNameMap {
if (file.length <= HASH_SIZE + DICTIONARY_ID_SIZE) throw new BlkFormatError('shared name map is too short');
const idBytes = file.subarray(HASH_SIZE, HASH_SIZE + DICTIONARY_ID_SIZE);
const dictionaryId = idBytes.some((byte) => byte !== 0) ? Buffer.from(idBytes).toString('hex') : null;
const body = decompress(file.subarray(HASH_SIZE + DICTIONARY_ID_SIZE), dictionary);
return { names: readNameTable(new ByteReader(body)), dictionaryId };
}