1
0
Fork 0
mirror of https://github.com/marcrobledo/RomPatcher.js.git synced 2025-09-25 17:36:56 +00:00

Merge pull request #92 from skyfloogle/bsdiff

BSDIFF support
This commit is contained in:
Marc Robledo 2025-09-22 09:28:54 +02:00 committed by GitHub
commit 6cd3e81cd2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 110 additions and 3 deletions

View file

@ -11,6 +11,7 @@ A ROM patcher made in Javascript.
* PPF
* Paper Mario Star Rod (.mod)
* VCDiff (.xdelta, .vcdiff)
* BSDiff (.bdf, .bspatch) (patch only)
* can patch and create patches
* shows ROM CRC32, MD5 and SHA-1 before patching
* can remove headers before patching

View file

@ -19,6 +19,7 @@ var PRECACHE_URLS = [
'/RomPatcher.js/rom-patcher-js/modules/BinFile.js',
'/RomPatcher.js/rom-patcher-js/modules/HashCalculator.js',
'/RomPatcher.js/rom-patcher-js/modules/RomPatcher.format.ips.js',
'/RomPatcher.js/rom-patcher-js/modules/RomPatcher.format.bdf.js',
'/RomPatcher.js/rom-patcher-js/modules/RomPatcher.format.bps.js',
'/RomPatcher.js/rom-patcher-js/modules/RomPatcher.format.ups.js',
'/RomPatcher.js/rom-patcher-js/modules/RomPatcher.format.aps_n64.js',
@ -30,6 +31,7 @@ var PRECACHE_URLS = [
'/RomPatcher.js/rom-patcher-js/modules/zip.js/z-worker.js',
'/RomPatcher.js/rom-patcher-js/modules/zip.js/zip.min.js',
'/RomPatcher.js/rom-patcher-js/modules/zip.js/inflate.js',
'/RomPatcher.js/rom-patcher-js/modules/bz2/bz2.js',
/* Rom Patcher JS core (web assets) */
'/RomPatcher.js/rom-patcher-js/assets/icon_alert_orange.svg',
'/RomPatcher.js/rom-patcher-js/assets/icon_check_circle_green.svg',

View file

@ -97,7 +97,7 @@ const RomPatcher = (function () {
patchFile.littleEndian = false;
patchFile.seek(0);
var header = patchFile.readString(6);
var header = patchFile.readString(8);
var patch = null;
if (header.startsWith(IPS.MAGIC)) {
patch = IPS.fromFile(patchFile);
@ -117,6 +117,8 @@ const RomPatcher = (function () {
patch = PMSR.fromFile(patchFile);
} else if (header.startsWith(VCDIFF.MAGIC)) {
patch = VCDIFF.fromFile(patchFile);
} else if (header.startsWith(BDF.MAGIC)) {
patch = BDF.fromFile(patchFile);
}
if (patch)
@ -407,4 +409,5 @@ if (typeof module !== 'undefined' && module.exports) {
PPF = require('./modules/RomPatcher.format.ppf');
PMSR = require('./modules/RomPatcher.format.pmsr');
VCDIFF = require('./modules/RomPatcher.format.vcdiff');
BDF = require('./modules/RomPatcher.format.bdf');
}

View file

@ -50,6 +50,7 @@ const RomPatcherWeb = (function () {
'modules/RomPatcher.format.ppf.js',
'modules/RomPatcher.format.pmsr.js',
'modules/RomPatcher.format.vcdiff.js',
'modules/RomPatcher.format.bdf.js',
'modules/zip.js/zip.min.js',
'RomPatcher.js'
];
@ -1175,7 +1176,7 @@ const ZIPManager = (function (romPatcherWeb) {
const ZIP_MAGIC = '\x50\x4b\x03\x04';
const FILTER_PATCHES = /\.(ips|ups|bps|aps|rup|ppf|mod|ebp|xdelta|vcdiff)$/i;
const FILTER_PATCHES = /\.(ips|ups|bps|aps|rup|ppf|mod|ebp|xdelta|vcdiff|bdf|bspatch)$/i;
//const FILTER_ROMS=/(?<!\.(txt|diz|rtf|docx?|xlsx?|html?|pdf|jpe?g|gif|png|bmp|webp|zip|rar|7z))$/i; //negative lookbehind is not compatible with Safari https://stackoverflow.com/a/51568859
const FILTER_NON_ROMS = /(\.(txt|diz|rtf|docx?|xlsx?|html?|pdf|jpe?g|gif|png|bmp|webp|zip|rar|7z))$/i;

View file

@ -12,7 +12,8 @@ self.importScripts(
'./modules/RomPatcher.format.rup.js',
'./modules/RomPatcher.format.ppf.js',
'./modules/RomPatcher.format.pmsr.js',
'./modules/RomPatcher.format.vcdiff.js'
'./modules/RomPatcher.format.vcdiff.js',
'./modules/RomPatcher.format.bdf.js'
);

View file

@ -303,6 +303,14 @@ BinFile.prototype.readU32 = function () {
this.offset += 4;
return this._lastRead >>> 0
}
BinFile.prototype.readU64 = function () {
if (this.littleEndian)
this._lastRead = this._u8array[this.offset] + (this._u8array[this.offset + 1] << 8) + (this._u8array[this.offset + 2] << 16) + (this._u8array[this.offset + 3] << 24) + (this._u8array[this.offset + 4] << 32) + (this._u8array[this.offset + 5] << 40) + (this._u8array[this.offset + 6] << 48) + (this._u8array[this.offset + 7] << 56);
else
this._lastRead = (this._u8array[this.offset] << 56) + (this._u8array[this.offset + 1] << 48) + (this._u8array[this.offset + 2] << 40) + (this._u8array[this.offset + 3] << 32) + (this._u8array[this.offset + 4] << 24) + (this._u8array[this.offset + 5] << 16) + (this._u8array[this.offset + 6] << 8) + this._u8array[this.offset + 7];
this.offset += 8;
return this._lastRead >>> 0
}

View file

@ -0,0 +1,62 @@
/* BDF module for Rom Patcher JS v20250914 - Marc Robledo 2025 - http://www.marcrobledo.com/license */
/* File format specification: https://www.daemonology.net/bsdiff/ */
const bz2 = require('./bz2/bz2');
const BDF_MAGIC='BSDIFF40';
if(typeof module !== "undefined" && module.exports){
module.exports = BDF;
}
function BDF(){
this.records=[];
this.patchedSize=0;
}
BDF.prototype.apply=function(file){
var tempFile=new BinFile(this.patchedSize);
for (const record of this.records) {
for (const b of record.diff) {
tempFile.writeU8(file.readU8() + b);
}
tempFile.writeBytes(record.extra);
file.seek(file.offset + record.skip);
}
return tempFile;
}
BDF.MAGIC = BDF_MAGIC;
BDF.fromFile=function(file){
var patch=new BDF();
file.seek(8);
file.littleEndian=true;
var controlSize=file.readU64();
var diffSize=file.readU64();
patch.patchedSize=file.readU64();
var controlCompressed=file.readBytes(controlSize);
var diffCompressed=file.readBytes(diffSize);
var extraCompressed=file.readBytes(file.fileSize-file.offset);
var controlFile=new BinFile(bz2.decompress(controlCompressed));
controlFile.littleEndian=true;
var diffFile=new BinFile(bz2.decompress(diffCompressed));
var extraFile=new BinFile(bz2.decompress(extraCompressed));
while(!controlFile.isEOF()){
var diffLen=controlFile.readU64();
var extraLen=controlFile.readU64();
var skip=controlFile.readU64();
if(skip&(1<<63))
skip=-(skip&~(1<<63));
var diff=diffFile.readBytes(diffLen);
var extra=extraFile.readBytes(extraLen);
patch.records.push({diff, extra, skip});
}
return patch;
}

View file

@ -0,0 +1,19 @@
Copyright 2019 SheetJS LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

File diff suppressed because one or more lines are too long

View file

@ -113,6 +113,14 @@ const TEST_PATCHES = [
patchCrc32: 0xa211f97c,
patchDownload: 'https://www.romhacking.net/hacks/2871/',
outputCrc32: 0x9cecd976
}, {
title: 'Tetris - Rosy Retrospection',
romFile: 'Tetris (World) (Rev 1).gb',
romCrc32: 0x46df91ad,
patchFile: 'rosy_retrospection.bdf',
patchCrc32: 0xcc61564a,
patchDownload: 'https://www.romhacking.net/hacks/5813/',
outputCrc32: 0x3d400209
}
];