UNPKG

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.

98 lines (97 loc) • 2.84 kB
import path from "node:path"; import IgirException from "../../exceptions/igirException.js"; import FsUtil from "../../utils/fsUtil.js"; class Patch { file; crcBefore; crcAfter; sizeAfter; constructor(file, crcBefore, crcAfter, sizeAfter) { this.file = file; this.crcBefore = crcBefore.toLowerCase().padStart(8, "0"); this.crcAfter = crcAfter?.toLowerCase().padStart(8, "0"); this.sizeAfter = sizeAfter; } static getCrcFromPath(fileBasename) { const matches = /(^|[^a-z0-9])(0x)?([a-f0-9]{8})([^a-z0-9]|$)/i.exec(fileBasename); if (matches && matches.length >= 3) { return matches[3].toLowerCase(); } throw new IgirException(`couldn't parse base file CRC for patch: ${fileBasename}`); } getFile() { return this.file; } getCrcBefore() { return this.crcBefore; } getCrcAfter() { return this.crcAfter; } getSizeAfter() { return this.sizeAfter; } getRomName() { return path.parse(this.getFile().getExtractedFilePath()).name.replaceAll(new RegExp(this.getCrcBefore(), "gi"), "").replaceAll(/ +/g, " ").trim(); } /** * Return a human-readable identifier for the patch. */ toString() { return `${this.getFile().toString()} (${this.crcBefore} \u2192 ${this.crcAfter ?? "????????"}${this.sizeAfter !== void 0 && this.sizeAfter > 0 ? `, ${FsUtil.sizeReadable(this.sizeAfter)}` : ""})`; } /** * Read a UPS-style variable-length unsigned integer from the given file handle, advancing the * read position past the encoded value. */ static async readUpsUint(fp) { let data = 0; let shift = 1; while (!fp.isEOF()) { const x = (await fp.readNext(1)).readUInt8(); data += (x & 127) * shift; if (x & 128) { break; } shift <<= 7; data += shift; } return data; } /** * Read a VCDIFF-style variable-length unsigned integer from the given file handle, advancing * the read position past the encoded value. */ static async readVcdiffUintFromFile(fp) { let num = 0; while (!fp.isEOF()) { const bits = (await fp.readNext(1)).readUInt8(); num = (num << 7) + (bits & 127); if (!(bits & 128)) { break; } } return num; } /** * Read a VCDIFF-style variable-length unsigned integer from a buffer starting at the given * offset, returning the value and the new offset past the encoded bytes. */ static readVcdiffUintFromBuffer(buffer, offset = 0) { let num = 0; let lastOffset = offset; while (lastOffset < buffer.length) { const bits = buffer.readUInt8(lastOffset); lastOffset += 1; num = (num << 7) + (bits & 127); if (!(bits & 128)) { break; } } return [num, lastOffset]; } } export { Patch as default }; //# sourceMappingURL=patch.js.map