Skip to content

Commit

Permalink
Refactor: isolate ROM & ReleaseCandidate chained processing steps (#600)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Aug 24, 2023
1 parent e29aa4d commit 7bfeb51
Showing 1 changed file with 62 additions and 39 deletions.
101 changes: 62 additions & 39 deletions src/igir.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import async from 'async';

import Logger from './console/logger.js';
import { ProgressBarSymbol } from './console/progressBar.js';
import ProgressBar, { ProgressBarSymbol } from './console/progressBar.js';
import ProgressBarCLI from './console/progressBarCLI.js';
import CandidateCombiner from './modules/candidateCombiner.js';
import CandidateGenerator from './modules/candidateGenerator.js';
Expand Down Expand Up @@ -44,26 +44,16 @@ export default class Igir {
async main(): Promise<void> {
// Scan and process input files
let dats = await this.processDATScanner();

const romScannerProgressBarName = 'Scanning for ROMs';
const romProgressBar = await this.logger.addProgressBar(romScannerProgressBarName);
const rawRomFiles = await new ROMScanner(this.options, romProgressBar).scan();
await romProgressBar.setName('Detecting ROM headers');
const romFilesWithHeaders = await new ROMHeaderProcessor(this.options, romProgressBar)
.process(rawRomFiles);
await romProgressBar.setName('Indexing ROMs');
const indexedRomFiles = await new FileIndexer(this.options, romProgressBar)
.index(romFilesWithHeaders);
await romProgressBar.setName(romScannerProgressBarName); // reset
await romProgressBar.doneItems(rawRomFiles.length, 'file', 'found');
await romProgressBar.freeze();

const indexedRoms = await this.processROMScanner();
const roms = [...indexedRoms.values()]
.flatMap((files) => files)
.filter(ArrayPoly.filterUnique);
const patches = await this.processPatchScanner();

// Set up progress bar and input for DAT processing
const datProcessProgressBar = await this.logger.addProgressBar('Processing DATs', ProgressBarSymbol.PROCESSING, dats.length);
if (!dats.length) {
dats = new DATInferrer(datProcessProgressBar).infer(romFilesWithHeaders);
dats = new DATInferrer(datProcessProgressBar).infer(roms);
}

if (this.options.getSingle() && !dats.some((dat) => dat.hasParentCloneInfo())) {
Expand All @@ -89,29 +79,19 @@ export default class Igir {
const filteredDat = await new DATFilter(this.options, progressBar).filter(dat);

// Generate and filter ROM candidates
const parentsToCandidates = await new CandidateGenerator(this.options, progressBar)
.generate(filteredDat, indexedRomFiles);
const parentsToPatchedCandidates = await new CandidatePatchGenerator(
this.options,
progressBar,
).generate(filteredDat, parentsToCandidates, patches);
romOutputDirs.push(...this.getCandidateOutputDirs(filteredDat, parentsToPatchedCandidates));
const parentsToFilteredCandidates = await new CandidatePreferer(this.options, progressBar)
.prefer(filteredDat, parentsToPatchedCandidates);
const parentsToPostProcessedCandidates = await new CandidatePostProcessor(
this.options,
progressBar,
).process(filteredDat, parentsToFilteredCandidates);
const parentsToCombinedCandidates = await new CandidateCombiner(
this.options,
const parentsToCandidates = await this.generateCandidates(
progressBar,
).combine(filteredDat, parentsToPostProcessedCandidates);
filteredDat,
indexedRoms,
patches,
);
romOutputDirs.push(...this.getCandidateOutputDirs(filteredDat, parentsToCandidates));

// Write the output files
const movedRoms = await new CandidateWriter(this.options, progressBar)
.write(filteredDat, parentsToCombinedCandidates);
.write(filteredDat, parentsToCandidates);
movedRomsToDelete.push(...movedRoms);
const writtenRoms = [...parentsToCombinedCandidates.entries()]
const writtenRoms = [...parentsToCandidates.entries()]
.reduce((map, [parent, releaseCandidates]) => {
// For each Parent, find what rom Files were written
const parentWrittenRoms = releaseCandidates
Expand All @@ -124,15 +104,15 @@ export default class Igir {

// Write a fixdat
await new FixdatCreator(this.options, progressBar)
.write(filteredDat, parentsToCombinedCandidates);
.write(filteredDat, parentsToCandidates);

// Write the output report
const datStatus = await new StatusGenerator(this.options, progressBar)
.generate(filteredDat, parentsToCombinedCandidates);
.generate(filteredDat, parentsToCandidates);
datsStatuses.push(datStatus);

// Progress bar cleanup
const totalReleaseCandidates = [...parentsToCombinedCandidates.values()]
const totalReleaseCandidates = [...parentsToCandidates.values()]
.reduce((sum, rcs) => sum + rcs.length, 0);
if (totalReleaseCandidates > 0) {
await progressBar.freeze();
Expand All @@ -149,13 +129,13 @@ export default class Igir {
datProcessProgressBar.delete();

// Delete moved ROMs
await this.deleteMovedRoms(rawRomFiles, movedRomsToDelete, datsToWrittenRoms);
await this.deleteMovedRoms(roms, movedRomsToDelete, datsToWrittenRoms);

// Clean the output directories
const cleanedOutputFiles = await this.processOutputCleaner(romOutputDirs, datsToWrittenRoms);

// Generate the report
await this.processReportGenerator(rawRomFiles, cleanedOutputFiles, datsStatuses);
await this.processReportGenerator(roms, cleanedOutputFiles, datsStatuses);

await ProgressBarCLI.stop();
}
Expand Down Expand Up @@ -188,6 +168,27 @@ export default class Igir {
return dats;
}

private async processROMScanner(): Promise<Map<string, File[]>> {
const romScannerProgressBarName = 'Scanning for ROMs';
const romProgressBar = await this.logger.addProgressBar(romScannerProgressBarName);

const rawRomFiles = await new ROMScanner(this.options, romProgressBar).scan();

await romProgressBar.setName('Detecting ROM headers');
const romFilesWithHeaders = await new ROMHeaderProcessor(this.options, romProgressBar)
.process(rawRomFiles);

await romProgressBar.setName('Indexing ROMs');
const indexedRomFiles = await new FileIndexer(this.options, romProgressBar)
.index(romFilesWithHeaders);

await romProgressBar.setName(romScannerProgressBarName); // reset
await romProgressBar.doneItems(rawRomFiles.length, 'file', 'found');
await romProgressBar.freeze();

return indexedRomFiles;
}

private async processPatchScanner(): Promise<Patch[]> {
if (!this.options.getPatchFileCount()) {
return [];
Expand All @@ -200,6 +201,28 @@ export default class Igir {
return patches;
}

private async generateCandidates(
progressBar: ProgressBar,
dat: DAT,
indexedRoms: Map<string, File[]>,
patches: Patch[],
): Promise<Map<Parent, ReleaseCandidate[]>> {
const candidates = await new CandidateGenerator(this.options, progressBar)
.generate(dat, indexedRoms);

const patchedCandidates = await new CandidatePatchGenerator(this.options, progressBar)
.generate(dat, candidates, patches);

const filteredCandidates = await new CandidatePreferer(this.options, progressBar)
.prefer(dat, patchedCandidates);

const postProcessedCandidates = await new CandidatePostProcessor(this.options, progressBar)
.process(dat, filteredCandidates);

return new CandidateCombiner(this.options, progressBar)
.combine(dat, postProcessedCandidates);
}

/**
* Find all ROM output paths for a DAT and its candidates.
*/
Expand Down

0 comments on commit 7bfeb51

Please sign in to comment.