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.
81 lines (80 loc) • 3.74 kB
JavaScript
import { ProgressBarSymbol } from '../../console/progressBar.js';
import ArrayPoly from '../../polyfill/arrayPoly.js';
import { MergeMode } from '../../types/options.js';
import Module from '../module.js';
/**
* Validate un-merged, split, and merged ROM sets for playability after all generation and filtering
* has happened.
*/
export default class CandidateMergeSplitValidator extends Module {
options;
constructor(options, progressBar) {
super(progressBar, CandidateMergeSplitValidator.name);
this.options = options;
}
/**
* Validate the {@link WriteCandidate}s.
*/
validate(dat, candidates) {
if (candidates.length === 0) {
this.progressBar.logTrace(`${dat.getName()}: no candidates to validate merged & split ROM sets for`);
return [];
}
this.progressBar.logTrace(`${dat.getName()}: validating merged & split ROM sets`);
this.progressBar.setSymbol(ProgressBarSymbol.CANDIDATE_VALIDATING);
this.progressBar.resetProgress(candidates.length);
const datGamesIndexed = dat.getGames().reduce((map, game) => {
map.set(game.getName(), game);
return map;
}, new Map());
const candidatesIndexed = candidates
.filter((candidate) => candidate.getRomsWithFiles().length > 0)
.reduce((map, candidate) => {
map.set(candidate.getGame().getName(), candidate);
return map;
}, new Map());
/**
* For every {@link Game} that has {@link WriteCandidate}s with files
*/
const missingGames = candidates
.filter((candidate) => candidate.getRomsWithFiles().length > 0)
.map((candidate) => candidate.getGame())
.reduce(ArrayPoly.reduceUnique(), [])
.flatMap((game) => {
let missingDependencies = [];
// Validate dependent parent was found
const cloneOf = game.getCloneOf();
if (this.options.getMergeRoms() === MergeMode.SPLIT &&
cloneOf !== undefined &&
!candidatesIndexed.has(cloneOf)) {
missingDependencies = [cloneOf, ...missingDependencies];
}
// Validate dependent devices were found
if (this.options.getMergeRoms() !== MergeMode.FULLNONMERGED) {
const missingDeviceGames = game
.getDeviceRefs()
.map((deviceRef) => datGamesIndexed.get(deviceRef.getName()))
.filter((deviceGame) => deviceGame !== undefined &&
// Dependent device has ROM files
deviceGame.getRoms().length > 0)
.map((deviceGame) => {
const deviceCandidate = candidatesIndexed.get(deviceGame.getName());
if (deviceCandidate) {
// The device game has candidates, validation passed
return undefined;
}
return deviceGame.getName();
})
.filter((deviceGameName) => deviceGameName !== undefined)
.sort();
missingDependencies = [...missingDependencies, ...missingDeviceGames];
}
if (missingDependencies.length > 0) {
this.progressBar.logWarn(`${dat.getName()}: ${game.getName()}: missing dependent ROM set${missingDependencies.length === 1 ? '' : 's'}: ${missingDependencies.join(', ')}`);
}
return missingDependencies;
});
this.progressBar.logTrace(`${dat.getName()}: done validating merged & split ROM sets`);
return missingGames;
}
}