Files
WT-Offline/packages/vromfs/src/archive.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

34 lines
1.4 KiB
TypeScript

/** A whole .vromfs.bin data pack: read it, change files, write it back. */
import { buildContainer, parseContainer, type ContainerInfo } from './container.ts';
import { VromfsFormatError } from './errors.ts';
import { buildImage, parseImage, type ImageLayout } from './image.ts';
export interface Archive {
readonly container: ContainerInfo;
readonly layout: ImageLayout;
readonly files: Map<string, Uint8Array>;
}
export function readArchive(data: Uint8Array): Archive {
const { info, image } = parseContainer(data);
const { layout, files } = parseImage(image);
return { container: info, layout, files };
}
export function writeArchive(archive: Archive): Uint8Array {
return buildContainer(archive.container, buildImage(archive.layout, archive.files));
}
/** Add or replace a file. The game lower-cases lookup paths, so stored paths must be lower case. */
export function putFile(archive: Archive, path: string, content: Uint8Array): void {
if (path !== path.toLowerCase()) throw new VromfsFormatError(`archive paths must be lower case: ${path}`);
if (path.startsWith('/') || path.includes('\\')) throw new VromfsFormatError(`archive paths are relative and use '/': ${path}`);
archive.files.set(path, content);
}
export function getFile(archive: Archive, path: string): Uint8Array {
const content = archive.files.get(path);
if (!content) throw new VromfsFormatError(`the data pack has no file ${path}`);
return content;
}