Skip to content

Commit df456bd

Browse files
committed
fix RUP overflow data not being processed as it should
fix RUP patch creation with different file sizes
1 parent 1ee279b commit df456bd

File tree

4 files changed

+67
-21
lines changed

4 files changed

+67
-21
lines changed

_cache_service_worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
var PRECACHE_ID = 'rom-patcher-js';
9-
var PRECACHE_VERSION = 'v30finalfix2';
9+
var PRECACHE_VERSION = 'v31';
1010
var PRECACHE_URLS = [
1111
'/RomPatcher.js/', '/RomPatcher.js/index.html',
1212
'/RomPatcher.js/manifest.json',

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
<button id="button-settings" class="btn-transparent"><img src="./webapp/icon_settings.svg" loading="lazy" class="icon settings" /> <span data-localize="yes">Settings</span></button>
152152
</div>
153153

154-
Rom Patcher JS <small><a href="legacy/" rel="nofollow">v3.0</a></small> by <a href="/">Marc Robledo</a>
154+
Rom Patcher JS <small><a href="legacy/" rel="nofollow">v3.1</a></small> by <a href="/">Marc Robledo</a>
155155
<br />
156156
<img src="./webapp/icon_github.svg" loading="lazy" class="icon github" /> <a href="https://github.com/marcrobledo/RomPatcher.js/" target="_blank">See on GitHub</a>
157157
<img src="./webapp/icon_heart.svg" loading="lazy" class="icon heart" /> <a href="https://www.paypal.me/marcrobledo/5" target="_blank" rel="nofollow">Donate</a>

rom-patcher-js/RomPatcher.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ const RomPatcher = (function () {
219219

220220
if (options.outputSuffix) {
221221
patchedRom.fileName = romFile.fileName.replace(/\.([^\.]*?)$/, ' (patched).$1');
222+
if(patchedRom.unpatched)
223+
patchedRom.fileName = patchedRom.fileName.replace(' (patched)', ' (unpatched)');
222224
} else if (patch._originalPatchFile) {
223225
patchedRom.fileName = patch._originalPatchFile.fileName.replace(/\.\w+$/i, (/\.\w+$/i.test(romFile.fileName) ? romFile.fileName.match(/\.\w+$/i)[0] : ''));
224226
} else {

rom-patcher-js/modules/RomPatcher.format.rup.js

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* RUP module for Rom Patcher JS v20240721 - Marc Robledo 2018-2024 - http://www.marcrobledo.com/license */
1+
/* RUP module for Rom Patcher JS v20241102 - Marc Robledo 2018-2024 - http://www.marcrobledo.com/license */
22
/* File format specification: http://www.romhacking.net/documents/288/ */
33

44
const RUP_MAGIC='NINJA2';
@@ -40,7 +40,11 @@ RUP.prototype.toString=function(){
4040
s+='\nTarget file size: '+file.targetFileSize;
4141
s+='\nSource MD5: '+file.sourceMD5;
4242
s+='\nTarget MD5: '+file.targetMD5;
43-
s+='\nOverflow text: '+file.overflowText;
43+
if(file.overflowMode==='A'){
44+
s+='\nOverflow mode: Append ' + file.overflowData.length + ' bytes';
45+
}else if(file.overflowMode==='M'){
46+
s+='\nOverflow mode: Minify ' + file.overflowData.length + ' bytes';
47+
}
4448
s+='\n#records: '+file.records.length;
4549
}
4650
return s
@@ -50,8 +54,11 @@ RUP.prototype.toString=function(){
5054
RUP.prototype.validateSource=function(romFile,headerSize){
5155
var md5string=romFile.hashMD5(headerSize);
5256
for(var i=0; i<this.files.length; i++){
53-
if(this.files[i].sourceMD5===md5string){
54-
return this.files[i];
57+
if(this.files[i].sourceMD5===md5string || this.files[i].targetMD5===md5string){
58+
return {
59+
file:this.files[i],
60+
undo:this.files[i].targetMD5===md5string
61+
};
5562
}
5663
}
5764
return false;
@@ -77,32 +84,54 @@ RUP.prototype.apply=function(romFile, validate){
7784
if(!validFile)
7885
throw new Error('Source ROM checksum mismatch');
7986
}else{
80-
validFile=this.files[0];
87+
validFile={
88+
file:this.files[0],
89+
undo:this.files[0].targetMD5===romFile.hashMD5()
90+
};
8191
}
8292

93+
var undo=validFile.undo;
94+
var patch=validFile.file;
8395

84-
85-
tempFile=new BinFile(validFile.targetFileSize);
96+
tempFile=new BinFile(!undo? patch.targetFileSize : patch.sourceFileSize);
8697
/* copy original file */
8798
romFile.copyTo(tempFile, 0);
8899

89100

90-
for(var i=0; i<validFile.records.length; i++){
91-
var offset=validFile.records[i].offset;
101+
for(var i=0; i<patch.records.length; i++){
102+
var offset=patch.records[i].offset;
92103
romFile.seek(offset);
93104
tempFile.seek(offset);
94-
for(var j=0; j<validFile.records[i].xor.length; j++){
105+
for(var j=0; j<patch.records[i].xor.length; j++){
95106
tempFile.writeU8(
96-
(romFile.isEOF()?0x00:romFile.readU8()) ^ validFile.records[i].xor[j]
107+
(romFile.isEOF()?0x00:romFile.readU8()) ^ patch.records[i].xor[j]
97108
);
98109
}
99110
}
100111

112+
/* add overflow data if needed */
113+
if(patch.overflowMode==='A' && !undo){ /* append */
114+
tempFile.seek(patch.sourceFileSize);
115+
tempFile.writeBytes(patch.overflowData.map((byte) => byte ^ 0xff));
116+
}else if(patch.overflowMode==='M' && undo){ /* minify */
117+
tempFile.seek(patch.targetFileSize);
118+
tempFile.writeBytes(patch.overflowData.map((byte) => byte ^ 0xff));
119+
}
101120

102-
if(validate && tempFile.hashMD5()!==validFile.targetMD5){
121+
122+
if(
123+
validate &&
124+
(
125+
(!undo && tempFile.hashMD5()!==patch.targetMD5) ||
126+
(undo && tempFile.hashMD5()!==patch.sourceMD5)
127+
)
128+
){
103129
throw new Error('Target ROM checksum mismatch');
104130
}
105131

132+
if(undo)
133+
tempFile.unpatched=true;
134+
106135
return tempFile
107136
}
108137

@@ -161,8 +190,10 @@ RUP.fromFile=function(file){
161190

162191

163192
if(nextFile.sourceFileSize!==nextFile.targetFileSize){
164-
file.skip(1); //skip 'M' (source>target) or 'A' (source<target)
165-
nextFile.overflowText=file.readString(file.readVLV());
193+
nextFile.overflowMode=file.readString(1); // 'M' (source>target) or 'A' (source<target)
194+
if(nextFile.overflowMode!=='M' && nextFile.overflowMode!=='A')
195+
throw new Error('RUP: invalid overflow mode');
196+
nextFile.overflowData=file.readBytes(file.readVLV());
166197
}
167198

168199
}else if(command===RUP_COMMAND_XOR_RECORD){
@@ -229,8 +260,8 @@ RUP.prototype.export=function(fileName){
229260

230261
if(file.sourceFileSize!==file.targetFileSize){
231262
patchFileSize++; // M or A
232-
patchFileSize+=RUP_getVLVLen(file.overflowText);
233-
patchFileSize+=file.overflowText;
263+
patchFileSize+=RUP_getVLVLen(file.overflowData.length);
264+
patchFileSize+=file.overflowData.length;
234265
}
235266
for(var j=0; j<file.records.length; j++){
236267
patchFileSize++; //command 0x01
@@ -279,8 +310,8 @@ RUP.prototype.export=function(fileName){
279310

280311
if(file.sourceFileSize!==file.targetFileSize){
281312
patchFile.writeString(file.sourceFileSize>file.targetFileSize?'M':'A');
282-
patchFile.writeVLV(file.overflowText.length);
283-
patchFile.writeString(file.overflowText);
313+
patchFile.writeVLV(file.overflowData.length);
314+
patchFile.writeBytes(file.overflowData);
284315
}
285316

286317
for(var j=0; j<file.records.length; j++){
@@ -314,10 +345,23 @@ RUP.buildFromRoms=function(original, modified){
314345
targetFileSize:modified.fileSize,
315346
sourceMD5:original.hashMD5(),
316347
targetMD5:modified.hashMD5(),
317-
overflowText:'',
348+
overflowMode:null,
349+
overflowData:[],
318350
records:[]
319351
};
320352

353+
if(file.sourceFileSize<file.targetFileSize){
354+
modified.seek(file.sourceFileSize);
355+
file.overflowMode='A';
356+
file.overflowData=modified.readBytes(file.targetFileSize-file.sourceFileSize).map((byte) => byte ^ 0xff);
357+
modified=modified.slice(0, file.sourceFileSize);
358+
}else if(file.sourceFileSize>file.targetFileSize){
359+
original.seek(file.targetFileSize);
360+
file.overflowMode='M';
361+
file.overflowData=original.readBytes(file.sourceFileSize-file.targetFileSize).map((byte) => byte ^ 0xff);
362+
original=original.slice(0, file.targetFileSize);
363+
}
364+
321365

322366
original.seek(0);
323367
modified.seek(0);

0 commit comments

Comments
 (0)