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.

82 lines (81 loc) • 2.44 kB
import path from 'node:path'; import IgirException from '../exceptions/igirException.js'; export default class Patch { file; crcBefore; crcAfter; sizeAfter; constructor(file, crcBefore, crcAfter, sizeAfter) { this.file = file; this.crcBefore = crcBefore; this.crcAfter = crcAfter; this.sizeAfter = sizeAfter; } static getCrcFromPath(fileBasename) { const matches = /(^|[^a-z0-9])([a-f0-9]{8})([^a-z0-9]|$)/i.exec(fileBasename); if (matches && matches.length >= 3) { return matches[2].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(); } static async readUpsUint(fp) { let data = 0; let shift = 1; while (!fp.isEOF()) { const x = (await fp.readNext(1)).readUInt8(); data += (x & 0x7f) * shift; // drop the left-most bit if (x & 0x80) { // left-most bit is telling us this is the end break; } shift <<= 7; data += shift; } return data; } static async readVcdiffUintFromFile(fp) { let num = 0; while (!fp.isEOF()) { const bits = (await fp.readNext(1)).readUInt8(); num = (num << 7) + (bits & 0x7f); if (!(bits & 0x80)) { // left-most bit is telling us to keep going break; } } return num; } 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 & 0x7f); if (!(bits & 0x80)) { // left-most bit is telling us to keep going break; } } return [num, lastOffset]; } }