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.
104 lines (103 loc) • 4.47 kB
JavaScript
import IgirException from "../../exceptions/igirException.js";
import IOFile from "../../models/files/ioFile.js";
import FsUtil from "../../utils/fsUtil.js";
import FileChecksums, { ChecksumBitmask } from "../files/fileChecksums.js";
import Patch from "./patch.js";
class UPSPatch extends Patch {
static SUPPORTED_EXTENSIONS = [".ups"];
static FILE_SIGNATURE = Buffer.from("UPS1");
/**
* Parse a .ups patch file and return a {@link UPSPatch}.
*/
static async patchFrom(file) {
let crcBefore = "";
let crcAfter = "";
let targetSize = 0;
await file.extractToTempIOFile("r", async (patchFile) => {
patchFile.seek(this.FILE_SIGNATURE.length);
await super.readUpsUint(patchFile);
targetSize = await super.readUpsUint(patchFile);
patchFile.seek(patchFile.getSize() - 12);
crcBefore = (await patchFile.readNext(4)).reverse().toString("hex");
crcAfter = (await patchFile.readNext(4)).reverse().toString("hex");
const patchChecksumExpected = (await patchFile.readNext(4)).reverse().toString("hex");
patchFile.seek(0);
const patchData = await patchFile.readNext(patchFile.getSize() - 4);
const patchChecksumsActual = await FileChecksums.hashData(patchData, ChecksumBitmask.CRC32);
if (patchChecksumsActual.crc32 !== patchChecksumExpected) {
throw new IgirException(
`UPS patch is invalid, CRC of contents (${patchChecksumsActual.crc32}) doesn't match expected (${patchChecksumExpected}): ${file.toString()}`
);
}
});
if (crcBefore.length !== 8 || crcAfter.length !== 8) {
throw new IgirException(`couldn't parse base file CRC for patch: ${file.toString()}`);
}
return new UPSPatch(file, crcBefore, crcAfter, targetSize);
}
/**
* Apply this patch to the input ROM file and write the patched result to the output path.
*/
async createPatchedFile(inputRomFile, outputRomPath, callback) {
await this.getFile().extractToTempIOFile("r", async (patchFile) => {
const header = await patchFile.readNext(4);
if (!header.equals(UPSPatch.FILE_SIGNATURE)) {
throw new IgirException(`UPS patch header is invalid: ${this.getFile().toString()}`);
}
const sourceSize = await Patch.readUpsUint(patchFile);
if (inputRomFile.getSize() !== sourceSize) {
throw new IgirException(
`UPS patch expected ROM size of ${FsUtil.sizeReadable(sourceSize)}: ${patchFile.getPathLike().toString()}`
);
}
await Patch.readUpsUint(patchFile);
await UPSPatch.writeOutputFile(inputRomFile, outputRomPath, patchFile, callback);
});
}
static async writeOutputFile(inputRomFile, outputRomPath, patchFile, callback) {
await inputRomFile.extractToTempFile(async (tempRomFile) => {
const sourceFile = await IOFile.fileFrom(tempRomFile, "r");
await FsUtil.copyFile(tempRomFile, outputRomPath);
const targetFile = await IOFile.fileFrom(outputRomPath, "r+");
try {
await this.applyPatch(patchFile, sourceFile, targetFile, callback);
} finally {
await targetFile.close();
await sourceFile.close();
}
});
}
static async applyPatch(patchFile, sourceFile, targetFile, callback) {
while (patchFile.getPosition() < patchFile.getSize() - 12) {
const relativeOffset = await super.readUpsUint(patchFile);
sourceFile.skipNext(relativeOffset);
targetFile.skipNext(relativeOffset);
const data = await this.readPatchBlock(patchFile, sourceFile);
await targetFile.write(data);
sourceFile.skipNext(1);
targetFile.skipNext(1);
if (callback !== void 0) {
const progressPercentage = patchFile.getPosition() / patchFile.getSize();
callback(Math.floor(progressPercentage * targetFile.getSize()));
}
}
}
static async readPatchBlock(patchFile, sourceFile) {
const buffer = [];
while (patchFile.getPosition() < patchFile.getSize() - 12) {
const xorByte = (await patchFile.readNext(1)).readUInt8();
if (!xorByte) {
return Buffer.concat(buffer);
}
const sourceByte = sourceFile.isEOF() ? 0 : (await sourceFile.readNext(1)).readUInt8();
buffer.push(Buffer.of(sourceByte ^ xorByte));
}
throw new IgirException(
`UPS patch failed to read 0x00 block termination: ${patchFile.getPathLike().toString()}`
);
}
}
export {
UPSPatch as default
};
//# sourceMappingURL=upsPatch.js.map