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.

49 lines (48 loc) • 2.47 kB
import { ProgressBarSymbol } from '../../console/progressBar.js'; import OutputFactory from '../../types/outputFactory.js'; import Module from '../module.js'; /** * Perform any {@link WriteCandidate} manipulations needed after candidates have had patches * attached. */ export default class CandidatePostProcessor extends Module { options; constructor(options, progressBar) { super(progressBar, CandidatePostProcessor.name); this.options = options; } /** * Post-process the candidates. */ process(dat, candidates) { if (candidates.length === 0) { this.progressBar.logTrace(`${dat.getName()}: no candidates to post-process`); return candidates; } this.progressBar.logTrace(`${dat.getName()}: processing candidates`); this.progressBar.setSymbol(ProgressBarSymbol.CANDIDATE_GENERATING); this.progressBar.resetProgress(candidates.length); // Get the output basename of every ROM const outputFileBasenames = candidates.flatMap((candidate) => candidate.getRomsWithFiles().map((romWithFiles) => { const outputPathParsed = OutputFactory.getPath(this.options, dat, candidate.getGame(), romWithFiles.getRom(), romWithFiles.getInputFile()); return outputPathParsed.name + outputPathParsed.ext; })); const processedCandidates = candidates.map((candidate) => this.postProcessCandidate(dat, candidate, outputFileBasenames)); this.progressBar.logTrace(`${dat.getName()}: done processing candidates`); return processedCandidates; } postProcessCandidate(dat, candidate, outputFileBasenames) { const newRomsWithFiles = this.mapRomsWithFiles(dat, candidate, candidate.getRomsWithFiles(), outputFileBasenames); return candidate.withRomsWithFiles(newRomsWithFiles); } mapRomsWithFiles(dat, candidate, romsWithFiles, outputFileBasenames) { return romsWithFiles.map((romWithFiles) => { const newOutputPath = OutputFactory.getPath(this.options, dat, candidate.getGame(), romWithFiles.getRom(), romWithFiles.getInputFile(), outputFileBasenames).format(); if (newOutputPath === romWithFiles.getOutputFile().getFilePath()) { return romWithFiles; } const newOutputFile = romWithFiles.getOutputFile().withFilePath(newOutputPath); return romWithFiles.withOutputFile(newOutputFile); }); } }