1
0
Fork 0
mirror of https://github.com/marcrobledo/RomPatcher.js.git synced 2025-06-27 16:25:54 +00:00

Merge branch 'marcrobledo:master' into main

This commit is contained in:
xenophile 2024-09-03 00:23:10 -07:00 committed by GitHub
commit ee347e044c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 121 additions and 60 deletions

View file

@ -6,7 +6,7 @@
*/ */
var PRECACHE_ID = 'rom-patcher-js'; var PRECACHE_ID = 'rom-patcher-js';
var PRECACHE_VERSION = 'v30rc4'; var PRECACHE_VERSION = 'v30rc4b';
var PRECACHE_URLS = [ var PRECACHE_URLS = [
'/RomPatcher.js/', '/RomPatcher.js/index.html', '/RomPatcher.js/', '/RomPatcher.js/index.html',
'/RomPatcher.js/manifest.json', '/RomPatcher.js/manifest.json',

View file

@ -37,7 +37,7 @@
const ROM_PATCHER_JS_PATH = 'https://xenophile127.github.io/RomPatcher.js/rom-patcher-js/'; const ROM_PATCHER_JS_PATH = 'https://xenophile127.github.io/RomPatcher.js/rom-patcher-js/';
var RomPatcherWeb = (function () { const RomPatcherWeb = (function () {
const SCRIPT_DEPENDENCIES = [ const SCRIPT_DEPENDENCIES = [
'modules/BinFile.js', 'modules/BinFile.js',
'modules/HashCalculator.js', 'modules/HashCalculator.js',
@ -103,25 +103,54 @@ var RomPatcherWeb = (function () {
parsedPatch.outputExtension = embededPatchInfo.outputExtension; parsedPatch.outputExtension = embededPatchInfo.outputExtension;
} }
if (typeof embededPatchInfo.inputMd5 !== 'undefined') {
if (!Array.isArray(embededPatchInfo.inputMd5))
embededPatchInfo.inputMd5 = [embededPatchInfo.inputMd5];
const validMd5s = embededPatchInfo.inputMd5.reduce(function (acc, md5) {
if (typeof md5 === 'string' && /^(0x)?[0-9a-fA-F]{32}$/i.test(md5.trim())) {
acc.push(md5.trim().toLowerCase());
} else {
console.warn('Rom Patcher JS: inputMd5 must be a string (32 characters long, characters allowed: 0-9, a-f)');
}
return acc;
}, []);
if (validMd5s.length) {
parsedPatch.inputValidation = {
'type': 'MD5',
'value': validMd5s
};
} else {
console.warn('Rom Patcher JS: invalid inputMd5 for embeded patch', embededPatchInfo.inputMd5);
}
}
if (typeof embededPatchInfo.inputCrc32 !== 'undefined') { if (typeof embededPatchInfo.inputCrc32 !== 'undefined') {
if (!parsedPatch.inputValidation) {
if (!Array.isArray(embededPatchInfo.inputCrc32)) if (!Array.isArray(embededPatchInfo.inputCrc32))
embededPatchInfo.inputCrc32 = [embededPatchInfo.inputCrc32]; embededPatchInfo.inputCrc32 = [embededPatchInfo.inputCrc32];
const validCrcs = embededPatchInfo.inputCrc32.map(function (crc32) { const validCrcs = embededPatchInfo.inputCrc32.reduce(function (acc, crc32) {
if (typeof crc32 === 'string' && /^(0x)?[0-9a-fA-F]{8}$/i.test(crc32.trim())) { if (typeof crc32 === 'string' && /^(0x)?[0-9a-fA-F]{8}$/i.test(crc32.trim())) {
return parseInt(crc32.replace('0x', ''), 16); acc.push(parseInt(crc32.trim().replace('0x', ''), 16));
} else if (typeof crc32 === 'number') { } else if (typeof crc32 === 'number') {
return crc32 >>> 0; acc.push((crc32 >>> 0) & 0xffffffff);
} else { } else {
return null; console.warn('Rom Patcher JS: invalid inputCrc32 value');
} }
}).filter(function (crc32) { return acc;
return typeof crc32 === 'number'; }, []);
});
if (validCrcs.length) { if (validCrcs.length) {
parsedPatch.inputCrc32 = validCrcs; parsedPatch.inputValidation = {
'type': 'CRC32',
'value': validCrcs
};
} else { } else {
console.warn('Invalid inputCrc32 for embeded patch', embededPatchInfo); console.warn('Rom Patcher JS: invalid inputCrc32 for embeded patch', embededPatchInfo.inputCrc32);
}
} else {
console.warn('Rom Patcher JS: a valid inputMd5 was provided, inputCrc32 will be ignored', embededPatchInfo);
} }
} }
@ -521,6 +550,9 @@ var RomPatcherWeb = (function () {
const _initialize = function (newSettings, embededPatchInfo) { const _initialize = function (newSettings, embededPatchInfo) {
/* embeded patches */ /* embeded patches */
var validEmbededPatch = _checkEmbededPatchParameter(embededPatchInfo); var validEmbededPatch = _checkEmbededPatchParameter(embededPatchInfo);
if (newSettings && typeof newSettings.file === 'string') {
console.warn('Rom Patcher JS: embeded patch info was provided in settings and will be ignored, must be passed as second parameter');
}
@ -745,20 +777,29 @@ var RomPatcherWeb = (function () {
const embededPatchInfo = _getEmbededPatchInfo(binFile.fileName); const embededPatchInfo = _getEmbededPatchInfo(binFile.fileName);
if (embededPatchInfo) { if (embededPatchInfo) {
/* custom crc32s validation */ /* custom input validation */
if (embededPatchInfo.inputCrc32) { if (embededPatchInfo.inputValidation) {
if (embededPatchInfo.inputValidation.type === 'CRC32') {
patch.validateSource = function (romFile, headerSize) { patch.validateSource = function (romFile, headerSize) {
for (var i = 0; i < embededPatchInfo.inputCrc32.length; i++) { for (var i = 0; i < embededPatchInfo.inputValidation.value.length; i++) {
if (embededPatchInfo.inputCrc32[i] === romFile.hashCRC32(headerSize)) if (embededPatchInfo.inputValidation.value[i] === romFile.hashCRC32(headerSize))
return true; return true;
} }
return false; return false;
} }
patch.getValidationInfo = function () { } else if (embededPatchInfo.inputValidation.type === 'MD5') {
return { patch.validateSource = function (romFile, headerSize) {
'type': 'CRC32', for (var i = 0; i < embededPatchInfo.inputValidation.value.length; i++) {
'value': embededPatchInfo.inputCrc32 if (embededPatchInfo.inputValidation.value[i] === romFile.hashMD5(headerSize))
return true;
} }
return false;
}
} else {
throw new Error('Rom Patcher JS: Invalid inputValidation type');
}
patch.getValidationInfo = function () {
return embededPatchInfo.inputValidation
}; };
} }
@ -1489,7 +1530,39 @@ const PatchBuilderWeb = (function (romPatcherWeb) {
} }
}; };
const webWorkerCreate = new Worker(ROM_PATCHER_JS_PATH + 'RomPatcher.webworker.create.js'); var webWorkerCreate;
var initialized = false;
return {
isInitialized: function () {
return initialized;
},
initialize: function () {
if (initialized)
throw new Error('Patch Builder JS was already initialized');
else if (!romPatcherWeb.isInitialized())
throw new Error('Rom Patcher JS must be initialized before Patch Builder JS');
if (!document.getElementById('patch-builder-button-create') || document.getElementById('patch-builder-button-create').tagName !== 'BUTTON') {
console.error('Patch Builder JS: button#patch-builder-button-create not found');
throw new Error('Patch Builder JS: button#patch-builder-button-create not found');
}
if (!document.getElementById('patch-builder-select-patch-type') || document.getElementById('patch-builder-select-patch-type').tagName !== 'SELECT') {
console.error('Patch Builder JS: select#patch-builder-select-patch-type not found');
throw new Error('Patch Builder JS: select#patch-builder-select-patch-type not found');
}
if (!document.getElementById('patch-builder-input-file-original') || document.getElementById('patch-builder-input-file-original').tagName !== 'INPUT' || document.getElementById('patch-builder-input-file-original').type !== 'file') {
console.error('Patch Builder JS: input[type=file]#patch-builder-input-file-original not found');
throw new Error('Patch Builder JS: input[type=file]#patch-builder-input-file-original not found');
}
if (!document.getElementById('patch-builder-input-file-modified') || document.getElementById('patch-builder-input-file-modified').tagName !== 'INPUT' || document.getElementById('patch-builder-input-file-modified').type !== 'file') {
console.error('Patch Builder JS: input[type=file]#patch-builder-input-file-modified not found');
throw new Error('Patch Builder JS: input[type=file]#patch-builder-input-file-modified not found');
}
webWorkerCreate = new Worker(ROM_PATCHER_JS_PATH + 'RomPatcher.webworker.create.js');
webWorkerCreate.onmessage = event => { // listen for events from the worker webWorkerCreate.onmessage = event => { // listen for events from the worker
//retrieve arraybuffers back from webworker //retrieve arraybuffers back from webworker
originalRom._u8array = event.data.originalRomU8Array; originalRom._u8array = event.data.originalRomU8Array;
@ -1510,18 +1583,6 @@ const PatchBuilderWeb = (function (romPatcherWeb) {
_setToastError('webWorkerCreate error: ' + event.message); _setToastError('webWorkerCreate error: ' + event.message);
}; };
var initialized = false;
return {
isInitialized: function () {
return initialized;
},
initialize: function () {
if (initialized)
throw new Error('Patch Builder JS was already initialized');
else if (!romPatcherWeb.isInitialized())
throw new Error('Rom Patcher JS must be initialized before Patch Builder JS');
document.getElementById('patch-builder-button-create').disabled = true; document.getElementById('patch-builder-button-create').disabled = true;
document.getElementById('patch-builder-input-file-original').addEventListener('change', function () { document.getElementById('patch-builder-input-file-original').addEventListener('change', function () {