/** 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; } 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; }