/** * The one-byte format label at the start of a binary BLK, and what follows it. * * Source: DagorEngine prog/engine/ioSys/dataBlock/blk_shared.h (dblk::BBF_*) and * blk_parser.cpp (DataBlock::loadFromStream). Any other first byte means a text BLK, * which this module does not read. */ import { zstdDecompressSync } from 'node:zlib'; import type { BlkBlock } from '../model.ts'; import { BlkFormatError } from '../errors.ts'; import { ByteReader } from './byte-reader.ts'; import { ByteWriter } from './byte-writer.ts'; import { decodeDump } from './dump-decoder.ts'; import { encodeDump } from './dump-encoder.ts'; export const BlkLabel = { /** Self-contained: own name table, body follows uncompressed. */ SelfContained: 1, /** 3-byte compressed size, then zstd of a SelfContained document (label included). */ SelfContainedZstd: 2, /** Names come from the data pack's shared name map; body follows uncompressed. */ SharedNames: 3, /** As SharedNames, body is zstd-compressed to the end of the file. */ SharedNamesZstd: 4, /** As SharedNamesZstd, compressed with the data pack's zstd dictionary. */ SharedNamesZstdDictionary: 5, /** As SelfContained, block records carry explicit parameter offsets. */ SelfContainedDeduplicated: 6, } as const; /** What a document stored with a shared name map needs from its data pack. */ export interface SharedNameContext { readonly names: readonly string[]; /** The pack's zstd dictionary, needed only for SharedNamesZstdDictionary documents. */ readonly dictionary?: Uint8Array | undefined; } export function decodeBlk(bytes: Uint8Array, shared?: SharedNameContext): BlkBlock { const reader = new ByteReader(bytes); const label = reader.u8(); switch (label) { case BlkLabel.SelfContained: return decodeDump(reader, { sharedNames: [], deduplicated: false }); case BlkLabel.SelfContainedDeduplicated: return decodeDump(reader, { sharedNames: [], deduplicated: true }); case BlkLabel.SelfContainedZstd: { const size = reader.u8() | (reader.u8() << 8) | (reader.u8() << 16); const inner = decompress(reader.take(size)); if (inner[0] !== BlkLabel.SelfContained) throw new BlkFormatError('compressed BLK does not contain a self-contained document'); return decodeBlk(inner); } case BlkLabel.SharedNames: return decodeDump(reader, { sharedNames: requireShared(shared).names, deduplicated: false }); case BlkLabel.SharedNamesZstd: { const names = requireShared(shared).names; return decodeDump(new ByteReader(decompress(reader.take(reader.remaining))), { sharedNames: names, deduplicated: false }); } case BlkLabel.SharedNamesZstdDictionary: { const context = requireShared(shared); if (!context.dictionary) throw new BlkFormatError('this BLK needs the data pack\'s zstd dictionary'); const body = decompress(reader.take(reader.remaining), context.dictionary); return decodeDump(new ByteReader(body), { sharedNames: context.names, deduplicated: false }); } default: throw new BlkFormatError(`format label ${label} is not a binary BLK (text BLK is not supported)`); } } /** Encode as a self-contained binary BLK, which the game accepts in every loading mode. */ export function encodeBlk(root: BlkBlock): Uint8Array { const writer = new ByteWriter(); writer.u8(BlkLabel.SelfContained); encodeDump(writer, root); return writer.toBytes(); } export function decompress(data: Uint8Array, dictionary?: Uint8Array): Uint8Array { try { return dictionary ? zstdDecompressSync(data, { dictionary }) : zstdDecompressSync(data); } catch (error) { throw new BlkFormatError(`zstd decompression failed: ${(error as Error).message}`); } } function requireShared(shared: SharedNameContext | undefined): SharedNameContext { if (!shared) throw new BlkFormatError('this BLK uses the data pack\'s shared name map, which was not supplied'); return shared; }