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.
86 lines (85 loc) • 3.44 kB
JavaScript
import { ProgressBarSymbol } from "../console/progressBar.js";
import PatchFactory from "../factories/patchFactory.js";
import { ChecksumBitmask } from "../models/files/fileChecksums.js";
import FsUtil from "../utils/fsUtil.js";
import IntlUtil from "../utils/intlUtil.js";
import Scanner from "./scanner.js";
class PatchScanner extends Scanner {
constructor(options, progressBar, fileFactory, mappableSemaphore) {
super(options, progressBar, fileFactory, mappableSemaphore, PatchScanner.name);
}
/**
* Scan & process {@link Patch}es.
*/
async scan() {
this.prefixedLogger.trace("scanning patch files");
this.progressBar.setSymbol(ProgressBarSymbol.FILE_SCANNING);
this.progressBar.resetProgress(0);
const patchFilePaths = await this.options.scanPatchFilesWithoutExclusions((increment) => {
this.progressBar.incrementTotal(increment);
});
this.prefixedLogger.trace(
`found ${IntlUtil.toLocaleString(patchFilePaths.length)} patch file${patchFilePaths.length === 1 ? "" : "s"}`
);
this.progressBar.resetProgress(patchFilePaths.length);
const patchFiles = await this.getUniqueFilesFromPaths(patchFilePaths, ChecksumBitmask.CRC32);
this.progressBar.resetProgress(patchFiles.length);
const patches = await this.parsePatchFiles(patchFiles);
for (const patch of patches) {
if (patch.getCrcBefore() === "00000000") {
this.prefixedLogger.warn(`${patch.toString()}: couldn't parse base file CRC`);
}
}
this.prefixedLogger.trace("done scanning patch files");
return patches;
}
async parsePatchFiles(patchFiles) {
this.prefixedLogger.trace(
`parsing ${IntlUtil.toLocaleString(patchFiles.length)} patch file${patchFiles.length === 1 ? "" : "s"}`
);
if (patchFiles.length === 0) {
return [];
}
this.progressBar.setName("Parsing patches");
this.progressBar.setSymbol(ProgressBarSymbol.PATCH_PARSING);
return (await this.mappableSemaphore.map(patchFiles, async (patchFile) => {
this.progressBar.incrementInProgress();
const childBar = this.progressBar.addChildBar({
name: patchFile.toString(),
total: patchFile.getSize(),
progressFormatter: FsUtil.sizeReadable.bind(FsUtil)
});
try {
return await this.patchFromFile(patchFile);
} catch (error) {
this.prefixedLogger.warn(`${patchFile.toString()}: failed to parse patch: ${error}`);
return void 0;
} finally {
childBar.delete();
this.progressBar.incrementCompleted();
}
})).filter((patch) => patch !== void 0);
}
async patchFromFile(file) {
const patchForFilename = await PatchFactory.patchFromFilename(file);
if (patchForFilename) {
this.prefixedLogger.trace(
`${patchForFilename.toString()}: found ${patchForFilename.constructor.name} from patch filename extension`
);
return patchForFilename;
}
const patchForFileContents = await PatchFactory.patchFromFileContents(file);
if (patchForFileContents) {
this.prefixedLogger.trace(
`${patchForFileContents.toString()}: found ${patchForFileContents.constructor.name} from patch file contents`
);
return patchForFileContents;
}
this.prefixedLogger.trace(`${file.toString()}: is not a known patch file`);
return void 0;
}
}
export {
PatchScanner as default
};
//# sourceMappingURL=patchScanner.js.map