-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
234 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Symbols } from '../console/progressBar.js'; | ||
import Constants from '../constants.js'; | ||
import Patch from '../types/patches/patch.js'; | ||
import PatchFactory from '../types/patches/patchFactory.js'; | ||
import Scanner from './scanner.js'; | ||
|
||
export default class PatchScanner extends Scanner { | ||
async scan(): Promise<Patch[]> { | ||
await this.progressBar.logInfo('Scanning patch files'); | ||
|
||
await this.progressBar.setSymbol(Symbols.SEARCHING); | ||
await this.progressBar.reset(this.options.getPatchFileCount()); | ||
|
||
const patchFilePaths = await this.options.scanPatchFiles(); | ||
await this.progressBar.logInfo(`Found ${patchFilePaths.length} patch file${patchFilePaths.length !== 1 ? 's' : ''}`); | ||
await this.progressBar.reset(patchFilePaths.length); | ||
|
||
const files = await this.getFilesFromPaths( | ||
patchFilePaths, | ||
Constants.PATCH_SCANNER_THREADS, | ||
); | ||
const patches = files.map((file) => PatchFactory.patchFrom(file)); | ||
|
||
await this.progressBar.doneItems(patches.length, 'unique patch', 'found'); | ||
|
||
return patches; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 10 additions & 2 deletions
12
src/types/archives/archiveFactory.ts → src/types/archives/fileFactory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import path from 'path'; | ||
|
||
import File from '../files/file.js'; | ||
import Patch from './patch.js'; | ||
|
||
export default class IPSPatch extends Patch { | ||
constructor(file: File) { | ||
const crcBefore = IPSPatch.getCrcFromPath(file.getExtractedFilePath()); | ||
super(file, crcBefore); | ||
} | ||
|
||
private static getCrcFromPath(filePath: string): string { | ||
const { name } = path.parse(filePath); | ||
|
||
const beforeMatches = name.match(/^([a-f0-9]{8})[^a-z0-9]/i); | ||
if (beforeMatches && beforeMatches?.length >= 2) { | ||
return beforeMatches[1].toUpperCase(); | ||
} | ||
|
||
const afterMatches = name.match(/[^a-z0-9]([a-f0-9]{8})$/i); | ||
if (afterMatches && afterMatches?.length >= 2) { | ||
return afterMatches[1].toUpperCase(); | ||
} | ||
|
||
throw new Error(`Couldn't parse base file CRC for patch: ${filePath}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import File from '../files/file.js'; | ||
|
||
export default abstract class Patch { | ||
private readonly file: File; | ||
|
||
private readonly crcBefore: string; | ||
|
||
private readonly crcAfter?: string; | ||
|
||
protected constructor(file: File, crcBefore: string, crcAfter?: string) { | ||
this.file = file; | ||
this.crcBefore = crcBefore; | ||
this.crcAfter = crcAfter; | ||
} | ||
|
||
getFile(): File { | ||
return this.file; | ||
} | ||
|
||
getCrcBefore(): string { | ||
return this.crcBefore; | ||
} | ||
|
||
getCrcAfter(): string | undefined { | ||
return this.crcAfter; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import File from '../files/file.js'; | ||
import IPSPatch from './ipsPatch.js'; | ||
import Patch from './patch.js'; | ||
|
||
export default class PatchFactory { | ||
static patchFrom(file: File): Patch { | ||
if (file.getExtractedFilePath().toLowerCase().endsWith('.ips')) { | ||
return new IPSPatch(file); | ||
} | ||
|
||
throw new Error(`Unknown patch type: ${file.toString()}`); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import os from 'os'; | ||
|
||
import PatchScanner from '../../src/modules/patchScanner.js'; | ||
import Options from '../../src/types/options.js'; | ||
import ProgressBarFake from '../console/progressBarFake.js'; | ||
|
||
function createPatchScanner(patch: string[]): PatchScanner { | ||
return new PatchScanner(new Options({ patch }), new ProgressBarFake()); | ||
} | ||
|
||
it('should throw on nonexistent paths', async () => { | ||
await expect(createPatchScanner(['/completely/invalid/path']).scan()).rejects.toThrow(/path doesn't exist/i); | ||
await expect(createPatchScanner(['/completely/invalid/path', os.devNull]).scan()).rejects.toThrow(/path doesn't exist/i); | ||
await expect(createPatchScanner(['/completely/invalid/path', 'test/fixtures/roms']).scan()).rejects.toThrow(/path doesn't exist/i); | ||
await expect(createPatchScanner(['test/fixtures/**/*.tmp']).scan()).rejects.toThrow(/path doesn't exist/i); | ||
await expect(createPatchScanner(['test/fixtures/roms/*foo*/*bar*']).scan()).rejects.toThrow(/path doesn't exist/i); | ||
}); | ||
|
||
it('should return empty list on no results', async () => { | ||
await expect(createPatchScanner([]).scan()).resolves.toEqual([]); | ||
await expect(createPatchScanner(['']).scan()).resolves.toEqual([]); | ||
await expect(createPatchScanner([os.devNull]).scan()).resolves.toEqual([]); | ||
}); | ||
|
||
it('should scan multiple files', async () => { | ||
const expectedPatchFiles = 1; | ||
await expect(createPatchScanner(['test/fixtures/patches/*']).scan()).resolves.toHaveLength(expectedPatchFiles); | ||
await expect(createPatchScanner(['test/fixtures/patches/**/*']).scan()).resolves.toHaveLength(expectedPatchFiles); | ||
await expect(createPatchScanner(['test/fixtures/*/*.{bps,ips}']).scan()).resolves.toHaveLength(expectedPatchFiles); | ||
}); | ||
|
||
it('should scan single files', async () => { | ||
await expect(createPatchScanner(['test/fixtures/patches/after*.ips']).scan()).resolves.toHaveLength(1); | ||
await expect(createPatchScanner(['test/fixtures/*/after*.ips']).scan()).resolves.toHaveLength(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.