import path from 'node:path'; import { Readable } from 'node:stream'; import { Exclude, Expose, instanceToPlain, plainToClassFromExist } from 'class-transformer'; import FsPoly from '../../../polyfill/fsPoly.js'; import { FsReadCallback } from '../../../polyfill/fsReadTransform.js'; import Patch from '../../patches/patch.js'; import File, { FileProps } from '../file.js'; import FileChecksums, { ChecksumBitmask, ChecksumProps } from '../fileChecksums.js'; import ROMHeader from '../romHeader.js'; import ROMPadding from '../romPadding.js'; import Archive from './archive.js'; export interface ArchiveEntryProps extends Omit { readonly archive: A; readonly entryPath: string; } @Exclude() export default class ArchiveEntry extends File implements ArchiveEntryProps { readonly archive: A; @Expose() readonly entryPath: string; protected constructor(archiveEntryProps: ArchiveEntryProps) { super({ ...archiveEntryProps, filePath: archiveEntryProps.archive.getFilePath(), }); this.archive = archiveEntryProps.archive; this.entryPath = archiveEntryProps.entryPath.replaceAll(/[\\/]/g, path.sep); } static async entryOf( archiveEntryProps: ArchiveEntryProps, checksumBitmask: number = ChecksumBitmask.NONE, ): Promise> { let finalSize = archiveEntryProps.size; let finalCrcWithHeader = archiveEntryProps.crc32; let finalCrcWithoutHeader = archiveEntryProps.fileHeader ? archiveEntryProps.crc32WithoutHeader : archiveEntryProps.crc32; let finalMd5WithHeader = archiveEntryProps.md5; let finalMd5WithoutHeader = archiveEntryProps.fileHeader ? archiveEntryProps.md5WithoutHeader : archiveEntryProps.md5; let finalSha1WithHeader = archiveEntryProps.sha1; let finalSha1WithoutHeader = archiveEntryProps.fileHeader ? archiveEntryProps.sha1WithoutHeader : archiveEntryProps.sha1; let finalSha256WithHeader = archiveEntryProps.sha256; let finalSha256WithoutHeader = archiveEntryProps.fileHeader ? archiveEntryProps.sha256WithoutHeader : archiveEntryProps.sha256; let finalSymlinkSource = archiveEntryProps.symlinkSource; if (await FsPoly.exists(archiveEntryProps.archive.getFilePath())) { // Calculate size finalSize = finalSize ?? 0; // Calculate checksums if ( (!finalCrcWithHeader && checksumBitmask & ChecksumBitmask.CRC32) || (!finalMd5WithHeader && checksumBitmask & ChecksumBitmask.MD5) || (!finalSha1WithHeader && checksumBitmask & ChecksumBitmask.SHA1) || (!finalSha256WithHeader && checksumBitmask & ChecksumBitmask.SHA256) ) { /** * If any additional checksum needs to be calculated, then prefer those calculated ones * over any that were supplied in {@link archiveEntryProps} that probably came from the * archive's file table. */ const headeredChecksums = await this.calculateEntryChecksums( archiveEntryProps.archive, archiveEntryProps.entryPath, checksumBitmask, ); finalCrcWithHeader = headeredChecksums.crc32 ?? finalCrcWithHeader; finalMd5WithHeader = headeredChecksums.md5 ?? finalMd5WithHeader; finalSha1WithHeader = headeredChecksums.sha1 ?? finalSha1WithHeader; finalSha256WithHeader = headeredChecksums.sha256 ?? finalSha256WithHeader; } if (archiveEntryProps.fileHeader && checksumBitmask) { const headerlessChecksums = await this.calculateEntryChecksums( archiveEntryProps.archive, archiveEntryProps.entryPath, checksumBitmask, archiveEntryProps.fileHeader, ); finalCrcWithoutHeader = headerlessChecksums.crc32; finalMd5WithoutHeader = headerlessChecksums.md5; finalSha1WithoutHeader = headerlessChecksums.sha1; finalSha256WithoutHeader = headerlessChecksums.sha256; } if (await FsPoly.isSymlink(archiveEntryProps.archive.getFilePath())) { finalSymlinkSource = await FsPoly.readlink(archiveEntryProps.archive.getFilePath()); } } else { finalSize = finalSize ?? 0; finalCrcWithHeader = finalCrcWithHeader ?? ''; } finalCrcWithoutHeader = finalCrcWithoutHeader ?? finalCrcWithHeader; finalMd5WithoutHeader = finalMd5WithoutHeader ?? finalMd5WithHeader; finalSha1WithoutHeader = finalSha1WithoutHeader ?? finalSha1WithHeader; finalSha256WithoutHeader = finalSha256WithoutHeader ?? finalSha256WithHeader; return new ArchiveEntry({ size: finalSize, crc32: finalCrcWithHeader, crc32WithoutHeader: finalCrcWithoutHeader, md5: finalMd5WithHeader, md5WithoutHeader: finalMd5WithoutHeader, sha1: finalSha1WithHeader, sha1WithoutHeader: finalSha1WithoutHeader, sha256: finalSha256WithHeader, sha256WithoutHeader: finalSha256WithoutHeader, symlinkSource: finalSymlinkSource, fileHeader: archiveEntryProps.fileHeader, patch: archiveEntryProps.patch, archive: archiveEntryProps.archive, entryPath: archiveEntryProps.entryPath, }); } static async entryOfObject( archive: A, obj: ArchiveEntryProps, ): Promise> { const deserialized = plainToClassFromExist(new ArchiveEntry({ archive, entryPath: '' }), obj, { enableImplicitConversion: true, excludeExtraneousValues: true, }); return this.entryOf({ ...deserialized, archive }); } toEntryProps(): ArchiveEntryProps { return instanceToPlain(this, { exposeUnsetFields: false, }) as ArchiveEntryProps; } // Property getters /** * Note: we're using type `Archive` here instead of `A` because otherwise TypeScript v5.7 will * think this is an `any`: * * (File instanceof ArchiveEntry).getArchive() * */ getArchive(): Archive { return this.archive; } getExtractedFilePath(): string { /** * Note: {@link Chd} will stuff some extra metadata in the entry path, chop it out */ return this.entryPath.split('|')[0]; } getEntryPath(): string { return this.entryPath; } async extractToFile(extractedFilePath: string, callback?: FsReadCallback): Promise { return ArchiveEntry.extractEntryToFile( this.getArchive(), this.getEntryPath(), extractedFilePath, callback, ); } private static async calculateEntryChecksums( archive: Archive, entryPath: string, checksumBitmask: number, fileHeader?: ROMHeader, ): Promise { return archive.extractEntryToStream( entryPath, async (readable) => FileChecksums.hashStream(readable, checksumBitmask), fileHeader?.getDataOffsetBytes() ?? 0, ); } private static async extractEntryToFile( archive: Archive, entryPath: string, extractedFilePath: string, callback?: FsReadCallback, ): Promise { return archive.extractEntryToFile(entryPath, extractedFilePath, callback); } async extractToTempFile(callback: (tempFile: string) => T | Promise): Promise { return ArchiveEntry.extractEntryToTempFile(this.getArchive(), this.getEntryPath(), callback); } private static async extractEntryToTempFile( archive: Archive, entryPath: string, callback: (tempFile: string) => T | Promise, ): Promise { return archive.extractEntryToTempFile(entryPath, callback); } async createReadStream(callback: (stream: Readable) => T | Promise, start = 0): Promise { // Don't extract to memory if we need to manipulate the stream start point if (start > 0) { return this.extractToTempFile(async (tempFile) => File.createStreamFromFile(tempFile, callback, start), ); } return this.archive.extractEntryToStream(this.getEntryPath(), callback); } withProps(props: ArchiveEntryProps): ArchiveEntry { return new ArchiveEntry({ ...this, ...props, }); } withFilePath(filePath: string): ArchiveEntry { if (this.getArchive().getFilePath() === filePath) { return this; } return new ArchiveEntry({ ...this, archive: this.getArchive().withFilePath(filePath), }) as ArchiveEntry; } withEntryPath(entryPath: string): ArchiveEntry { if (entryPath === this.entryPath) { return this; } return new ArchiveEntry({ ...this, entryPath }); } async withFileHeader(fileHeader: ROMHeader): Promise> { if (fileHeader === this.fileHeader) { return this; } return ArchiveEntry.entryOf( { ...this, fileHeader, paddings: [], patch: undefined, }, this.getChecksumBitmask(), ); } withoutFileHeader(): ArchiveEntry { if (this.fileHeader === undefined) { return this; } return new ArchiveEntry({ ...this, fileHeader: undefined, crc32WithoutHeader: this.getCrc32(), md5WithoutHeader: this.getMd5(), sha1WithoutHeader: this.getSha1(), sha256WithoutHeader: this.getSha256(), }); } withPaddings(paddings: ROMPadding[]): ArchiveEntry { return new ArchiveEntry({ ...this, fileHeader: paddings.length > 0 ? undefined : this.getFileHeader(), paddings, patch: paddings.length > 0 ? undefined : this.getPatch(), }); } withPatch(patch: Patch): ArchiveEntry { if (patch.getCrcBefore() !== this.getCrc32()) { return this; } return new ArchiveEntry({ ...this, fileHeader: undefined, paddings: [], patch, }); } toString(): string { if (this.getSymlinkSource()) { return `${this.getFilePath()}|${this.getExtractedFilePath()} -> ${this.getSymlinkSource()}|${this.getExtractedFilePath()}`; } return `${this.getFilePath()}|${this.getExtractedFilePath()}`; } equals(other: File): boolean { if (this === other) { return true; } if (!(other instanceof ArchiveEntry)) { return false; } if (!super.equals(other)) { return false; } return this.getEntryPath() === other.getEntryPath(); } }