Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm committed Oct 21, 2023
1 parent 4e797ea commit c7dc0bf
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/igir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default class Igir {
// Set up progress bar and input for DAT processing
const datProcessProgressBar = await this.logger.addProgressBar(chalk.underline('Processing DATs'), ProgressBarSymbol.NONE, dats.length);
if (dats.length === 0) {
dats = new DATGameInferrer(datProcessProgressBar).infer(roms);
dats = new DATGameInferrer(this.options, datProcessProgressBar).infer(roms);
}

const datsToWrittenFiles = new Map<DAT, File[]>();
Expand Down
28 changes: 18 additions & 10 deletions src/modules/datGameInferrer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import moment from 'moment';

import ProgressBar from '../console/progressBar.js';
import Constants from '../constants.js';
import ArrayPoly from '../polyfill/arrayPoly.js';
import DAT from '../types/dats/dat.js';
import Game from '../types/dats/game.js';
import Header from '../types/dats/logiqx/header.js';
Expand All @@ -22,6 +23,8 @@ import Module from './module.js';
* This class will not be run concurrently with any other class.
*/
export default class DATGameInferrer extends Module {
private static readonly DEFAULT_DAT_NAME = moment().format('YYYYMMDD-HHmmss');

private readonly options: Options;

constructor(options: Options, progressBar: ProgressBar) {
Expand All @@ -36,14 +39,17 @@ export default class DATGameInferrer extends Module {
this.progressBar.logInfo(`inferring DATs for ${romFiles.length.toLocaleString()} ROM${romFiles.length !== 1 ? 's' : ''}`);

const normalizedInputPaths = this.options.getInputPaths()
.map((inputPath) => path.normalize(inputPath));
.map((inputPath) => path.normalize(inputPath))
// Try to strip out glob patterns
.map((inputPath) => inputPath.replace(/([\\/][?*]+)+$/, ''));

const inputPathsToRomFiles = romFiles.reduce((map, file) => {
const normalizedPath = file.getFilePath().normalize();
normalizedInputPaths
const matchedInputPaths = normalizedInputPaths
// `.filter()` rather than `.find()` because a file can be found in overlapping input paths,
// therefore it should be counted in both
.filter((inputPath) => normalizedPath.startsWith(inputPath))
.filter((inputPath) => normalizedPath.startsWith(inputPath));
(matchedInputPaths.length > 0 ? matchedInputPaths : [DATGameInferrer.DEFAULT_DAT_NAME])
.forEach((inputPath) => {
const datRomFiles = [...(map.get(inputPath) ?? []), file];
map.set(inputPath, datRomFiles);
Expand Down Expand Up @@ -84,13 +90,15 @@ export default class DATGameInferrer extends Module {
}, new Map<string, File[]>());

const games = [...gameNamesToRomFiles.entries()].map(([gameName, gameRomFiles]) => {
const roms = gameRomFiles.map((romFile) => new ROM({
name: path.basename(romFile.getExtractedFilePath()),
size: romFile.getSize(),
crc: romFile.getCrc32(),
md5: romFile.getMd5(),
sha1: romFile.getSha1(),
}));
const roms = gameRomFiles
.map((romFile) => new ROM({
name: path.basename(romFile.getExtractedFilePath()),
size: romFile.getSize(),
crc: romFile.getCrc32(),
md5: romFile.getMd5(),
sha1: romFile.getSha1(),
}))
.filter(ArrayPoly.filterUniqueMapped((rom) => rom.getName()));
return new Game({
name: gameName,
description: gameName,
Expand Down
5 changes: 3 additions & 2 deletions test/igir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,8 +787,8 @@ describe('with inferred DATs', () => {
[path.join('onetwothree', 'one.rom'), 'f817a89f'],
[path.join('onetwothree', 'three.rom'), 'ff46c5d8'],
[path.join('onetwothree', 'two.rom'), '96170874'],
['speed_test_v51.sfc', '8beffd94'],
['speed_test_v51.smc', '9adca6cc'],
[path.join('speed_test_v51', 'speed_test_v51.sfc'), '8beffd94'],
[path.join('speed_test_v51', 'speed_test_v51.smc'), '9adca6cc'],
['three.rom', 'ff46c5d8'],
['two.rom', '96170874'],
['unknown.rom', '377a7727'],
Expand Down Expand Up @@ -863,6 +863,7 @@ describe('with inferred DATs', () => {
['onetwothree.zip|one.rom', 'f817a89f'],
['onetwothree.zip|three.rom', 'ff46c5d8'],
['onetwothree.zip|two.rom', '96170874'],
['speed_test_v51.zip|speed_test_v51.sfc', '8beffd94'],
['speed_test_v51.zip|speed_test_v51.smc', '9adca6cc'],
['three.zip|three.rom', 'ff46c5d8'],
['two.zip|two.rom', '96170874'],
Expand Down
2 changes: 1 addition & 1 deletion test/modules/candidateCombiner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function runCombinedCandidateGenerator(
romFiles: File[],
): Promise<Map<Parent, ReleaseCandidate[]>> {
// Run DATInferrer, but condense all DATs down to one
const datGames = new DATGameInferrer(new ProgressBarFake()).infer(romFiles)
const datGames = new DATGameInferrer(options, new ProgressBarFake()).infer(romFiles)
.flatMap((dat) => dat.getGames());
const dat = new LogiqxDAT(new Header(), datGames);

Expand Down
18 changes: 10 additions & 8 deletions test/modules/candidatePatchGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import ReleaseCandidate from '../../src/types/releaseCandidate.js';
import ProgressBarFake from '../console/progressBarFake.js';

// Run DATInferrer, but condense all DATs down to one
function buildInferredDat(romFiles: File[]): DAT {
const datGames = new DATGameInferrer(new ProgressBarFake()).infer(romFiles)
function buildInferredDat(options: Options, romFiles: File[]): DAT {
const datGames = new DATGameInferrer(options, new ProgressBarFake()).infer(romFiles)
.flatMap((dat) => dat.getGames());
return new LogiqxDAT(new Header(), datGames);
}
Expand Down Expand Up @@ -56,10 +56,11 @@ it('should do nothing with no parents', async () => {
describe('with inferred DATs', () => {
it('should do nothing with no relevant patches', async () => {
// Given
const romFiles = await new ROMScanner(new Options({
const options = new Options({
input: [path.join('test', 'fixtures', 'roms', 'headered')],
}), new ProgressBarFake()).scan();
const dat = buildInferredDat(romFiles);
});
const romFiles = await new ROMScanner(options, new ProgressBarFake()).scan();
const dat = buildInferredDat(options, romFiles);

// When
const parentsToCandidates = await runPatchCandidateGenerator(dat, romFiles);
Expand All @@ -72,10 +73,11 @@ describe('with inferred DATs', () => {

it('should create patch candidates with relevant patches', async () => {
// Given
const romFiles = await new ROMScanner(new Options({
const options = new Options({
input: [path.join('test', 'fixtures', 'roms', 'patchable')],
}), new ProgressBarFake()).scan();
const dat = buildInferredDat(romFiles);
});
const romFiles = await new ROMScanner(options, new ProgressBarFake()).scan();
const dat = buildInferredDat(options, romFiles);

// When
const parentsToCandidates = await runPatchCandidateGenerator(dat, romFiles);
Expand Down
9 changes: 5 additions & 4 deletions test/modules/candidateWriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ async function walkAndStat(dirPath: string): Promise<[string, Stats][]> {
);
}

function datInferrer(romFiles: File[]): DAT {
function datInferrer(options: Options, romFiles: File[]): DAT {
// Run DATInferrer, but condense all DATs down to one
const datGames = new DATGameInferrer(new ProgressBarFake()).infer(romFiles)
const datGames = new DATGameInferrer(options, new ProgressBarFake()).infer(romFiles)
.flatMap((dat) => dat.getGames());
// TODO(cemmer): filter to unique games / remove duplicates
return new LogiqxDAT(new Header({ name: 'ROMWriter Test' }), datGames);
Expand All @@ -93,7 +93,7 @@ async function romWriter(
output: outputTemp,
});
const romFiles = await new ROMScanner(options, new ProgressBarFake()).scan();
const dat = datInferrer(romFiles);
const dat = datInferrer(options, romFiles);
const romFilesWithHeaders = await new ROMHeaderProcessor(options, new ProgressBarFake())
.process(romFiles);
const indexedRomFiles = await new FileIndexer(options, new ProgressBarFake())
Expand Down Expand Up @@ -551,7 +551,8 @@ describe('zip', () => {
[`ROMWriter Test.zip|${path.join('onetwothree', 'one.rom')}`, 'f817a89f'],
[`ROMWriter Test.zip|${path.join('onetwothree', 'three.rom')}`, 'ff46c5d8'],
[`ROMWriter Test.zip|${path.join('onetwothree', 'two.rom')}`, '96170874'],
['ROMWriter Test.zip|speed_test_v51.sfc', '8beffd94'],
[`ROMWriter Test.zip|${path.join('speed_test_v51', 'speed_test_v51.sfc')}`, '8beffd94'],
[`ROMWriter Test.zip|${path.join('speed_test_v51', 'speed_test_v51.smc')}`, '9adca6cc'],
['ROMWriter Test.zip|three.rom', 'ff46c5d8'],
['ROMWriter Test.zip|two.rom', '96170874'],
['ROMWriter Test.zip|unknown.rom', '377a7727'],
Expand Down
36 changes: 19 additions & 17 deletions test/modules/datGameInferrer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@ import Options from '../../src/types/options.js';
import ProgressBarFake from '../console/progressBarFake.js';

test.each([
['test/fixtures/roms/**/*', {
'7z': 5,
// No input paths
[[], {}],
// One input path
[['test/fixtures/roms/**/*'], { roms: 27 }],
[['test/fixtures/roms/7z/*'], { '7z': 5 }],
[['test/fixtures/roms/rar/*'], { rar: 5 }],
[['test/fixtures/roms/raw/*'], { raw: 10 }],
[['test/fixtures/roms/tar/*'], { tar: 5 }],
[['test/fixtures/roms/zip/*'], { zip: 6 }],
// Multiple input paths
[[
'test/fixtures/roms/headered',
'test/fixtures/roms/patchable/*',
'test/fixtures/roms/unheadered/**/*',
], {
headered: 6,
patchable: 9,
rar: 5,
raw: 10,
roms: 5,
tar: 5,
unheadered: 1,
zip: 6,
}],
['test/fixtures/roms/7z/*', { '7z': 5 }],
['test/fixtures/roms/rar/*', { rar: 5 }],
['test/fixtures/roms/raw/*', { raw: 10 }],
['test/fixtures/roms/tar/*', { tar: 5 }],
['test/fixtures/roms/zip/*', { zip: 6 }],
])('should infer DATs: %s', async (inputGlob, expected) => {
])('should infer DATs: %s', async (input, expected) => {
// Given
const romFiles = await new ROMScanner(new Options({
input: [inputGlob],
}), new ProgressBarFake()).scan();
const options = new Options({ input });
const romFiles = await new ROMScanner(options, new ProgressBarFake()).scan();

// When
const dats = new DATGameInferrer(new ProgressBarFake()).infer(romFiles);
const dats = new DATGameInferrer(options, new ProgressBarFake()).infer(romFiles);

// Then
const datNameToGameCount = Object.fromEntries(
Expand Down

0 comments on commit c7dc0bf

Please sign in to comment.