UNPKG

igir

Version:

🕹 A zero-setup ROM collection manager that sorts, filters, extracts or archives, patches, and reports on collections of any size on any OS.

94 lines (93 loc) • 3.58 kB
import { ProgressBarSymbol } from "../../console/progressBar.js"; import ArchiveEntry from "../../models/files/archives/archiveEntry.js"; import ROMHeader from "../../models/files/romHeader.js"; import IntlUtil from "../../utils/intlUtil.js"; import Module from "../module.js"; class ROMHeaderProcessor extends Module { options; fileFactory; mappableSemaphore; constructor(options, progressBar, fileFactory, mappableSemaphore) { super(progressBar, ROMHeaderProcessor.name); this.options = options; this.fileFactory = fileFactory; this.mappableSemaphore = mappableSemaphore; } /** * Process each {@link File}, finding any {@link Header} present. */ async process(inputRomFiles) { if (inputRomFiles.length === 0) { return inputRomFiles; } const filesThatNeedProcessing = inputRomFiles.filter( (inputFile) => this.fileNeedsProcessing(inputFile) ).length; if (filesThatNeedProcessing === 0) { this.prefixedLogger.trace("no ROMs need their header processed"); return inputRomFiles; } this.prefixedLogger.trace( `processing headers in ${IntlUtil.toLocaleString(filesThatNeedProcessing)} ROM${filesThatNeedProcessing === 1 ? "" : "s"}` ); this.progressBar.setSymbol(ProgressBarSymbol.ROM_HEADER_DETECTION); this.progressBar.resetProgress(filesThatNeedProcessing); const parsedFiles = await this.mappableSemaphore.map(inputRomFiles, async (inputFile) => { if (!this.fileNeedsProcessing(inputFile)) { return inputFile; } this.progressBar.incrementInProgress(); const childBar = this.progressBar.addChildBar({ name: inputFile.toString() }); let fileWithHeader; try { fileWithHeader = await this.getFileWithHeader(inputFile); } catch (error) { this.prefixedLogger.error( `${inputFile.toString()}: failed to process ROM header: ${error}` ); fileWithHeader = inputFile; } finally { childBar.delete(); } this.progressBar.incrementCompleted(); return fileWithHeader; }); const headeredRomsCount = parsedFiles.filter( (romFile) => romFile.getFileHeader() !== void 0 ).length; this.prefixedLogger.trace( `found headers in ${IntlUtil.toLocaleString(headeredRomsCount)} ROM${headeredRomsCount === 1 ? "" : "s"}` ); this.prefixedLogger.trace("done processing file headers"); return parsedFiles; } fileNeedsProcessing(inputFile) { if (inputFile instanceof ArchiveEntry && !this.options.shouldZip() && !this.options.shouldExtract()) { return false; } if (inputFile.getSize() === 0) { return false; } return ROMHeader.headerFromFilename(inputFile.getExtractedFilePath()) !== void 0 || this.options.shouldReadFileForHeader(inputFile.getExtractedFilePath()); } async getFileWithHeader(inputFile) { this.prefixedLogger.trace( `${inputFile.toString()}: reading potentially headered file by file contents` ); const headerForFileStream = await this.fileFactory.headerFrom(inputFile); if (headerForFileStream) { this.prefixedLogger.trace( `${inputFile.toString()}: found header by file contents: ${headerForFileStream.getHeaderedFileExtension()}` ); return await inputFile.withFileHeader(headerForFileStream); } this.prefixedLogger.trace(`${inputFile.toString()}: didn't find header by file contents`); return inputFile; } } export { ROMHeaderProcessor as default }; //# sourceMappingURL=romHeaderProcessor.js.map