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.

73 lines (72 loc) • 2.62 kB
import path from "node:path"; import { ProgressBarSymbol } from "../../console/progressBar.js"; import Game from "../../models/dats/game.js"; import ArchiveEntry from "../../models/files/archives/archiveEntry.js"; import WriteCandidate from "../../models/writeCandidate.js"; import Module from "../module.js"; class CandidateCombiner extends Module { options; constructor(options, progressBar) { super(progressBar, CandidateCombiner.name); this.options = options; } /** * Combine the candidates. */ combine(dat, candidates) { if (!this.options.getZipDatName()) { return candidates; } if (candidates.length === 0) { this.prefixedLogger.trace(`${dat.getName()}: no candidates to make patched candidates for`); return candidates; } this.prefixedLogger.trace(`${dat.getName()}: generating consolidated candidate`); this.progressBar.setSymbol(ProgressBarSymbol.CANDIDATE_COMBINING); this.progressBar.resetProgress(candidates.length); const game = CandidateCombiner.buildGame(dat, candidates); const candidate = CandidateCombiner.buildCombinedCandidate(dat, game, candidates); return [candidate]; } static buildGame(dat, candidates) { const name = dat.getName(); const roms = candidates.flatMap((candidate) => candidate.getRomsWithFiles()).map((romWithFiles) => romWithFiles.getRom()); const uniqueRoms = [ ...roms.reduce((map, rom) => { const key = rom.getName(); if (!map.has(key)) { map.set(key, rom); } return map; }, /* @__PURE__ */ new Map()).values() ]; return new Game({ name, roms: uniqueRoms }); } static buildCombinedCandidate(dat, game, candidates) { const romsWithFiles = candidates.flatMap( (candidate) => candidate.getRomsWithFiles().map((romWithFiles) => { const outputFile = romWithFiles.getOutputFile(); if (!(outputFile instanceof ArchiveEntry)) { return romWithFiles; } let outputEntry = outputFile.withFilePath( path.join(path.dirname(outputFile.getFilePath()), dat.getName()) + outputFile.getArchive().getExtension() ); if (candidate.getGame().getRoms().length > 1) { outputEntry = outputEntry.withEntryPath( path.posix.join(candidate.getGame().getName(), outputEntry.getEntryPath()) ); } return romWithFiles.withOutputFile(outputEntry); }) ); return new WriteCandidate(game, romsWithFiles); } } export { CandidateCombiner as default }; //# sourceMappingURL=candidateCombiner.js.map