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

feat (web): show initialization errors on browser console before throwing exceptions

This commit is contained in:
Marc Robledo 2024-08-14 20:27:48 +02:00
parent c5c39a7703
commit a5f57f73ba
2 changed files with 22 additions and 17 deletions

View file

@ -6,7 +6,7 @@
*/ */
var PRECACHE_ID = 'rom-patcher-js'; var PRECACHE_ID = 'rom-patcher-js';
var PRECACHE_VERSION = 'v30rc2'; var PRECACHE_VERSION = 'v30rc2b';
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

@ -231,10 +231,10 @@ var RomPatcherWeb = (function () {
spinner.className = 'rom-patcher-spinner'; spinner.className = 'rom-patcher-spinner';
const htmlInputFile = htmlElements.get(elementId); const htmlInputFile = htmlElements.get(elementId);
if (htmlInputFile){ if (htmlInputFile) {
if(elementId === 'select-patch'){ if (elementId === 'select-patch') {
htmlInputFile.parentElement.insertBefore(spinner, htmlElements.get('span-loading-embeded-patch')); htmlInputFile.parentElement.insertBefore(spinner, htmlElements.get('span-loading-embeded-patch'));
}else{ } else {
htmlInputFile.parentElement.appendChild(spinner); htmlInputFile.parentElement.appendChild(spinner);
} }
} }
@ -390,14 +390,14 @@ var RomPatcherWeb = (function () {
var patchedRom = new BinFile(event.data.patchedRomU8Array.buffer); var patchedRom = new BinFile(event.data.patchedRomU8Array.buffer);
patchedRom.fileName = event.data.patchedRomFileName; patchedRom.fileName = event.data.patchedRomFileName;
if(currentEmbededPatches){ if (currentEmbededPatches) {
const optionalPatches = currentEmbededPatches.filter((embededPatchInfo) => embededPatchInfo.optional); const optionalPatches = currentEmbededPatches.filter((embededPatchInfo) => embededPatchInfo.optional);
if(optionalPatches.length){ if (optionalPatches.length) {
const originalFileName = patchedRom.fileName; const originalFileName = patchedRom.fileName;
for(var i=0; i<optionalPatches.length; i++){ for (var i = 0; i < optionalPatches.length; i++) {
/* could be improved by using webWorkerApply to apply optional patches */ /* could be improved by using webWorkerApply to apply optional patches */
if(optionalPatches[i].checkbox.checked) if (optionalPatches[i].checkbox.checked)
patchedRom = RomPatcher.applyPatch(patchedRom, optionalPatches[i].parsedPatch, {requireValidation:false, fixChecksum: true}); patchedRom = RomPatcher.applyPatch(patchedRom, optionalPatches[i].parsedPatch, { requireValidation: false, fixChecksum: true });
} }
patchedRom.fileName = originalFileName; patchedRom.fileName = originalFileName;
} }
@ -523,6 +523,7 @@ var RomPatcherWeb = (function () {
/* check if zip-js web worker is available */ /* check if zip-js web worker is available */
if (typeof zip !== 'object' || typeof zip.useWebWorkers !== 'boolean') { if (typeof zip !== 'object' || typeof zip.useWebWorkers !== 'boolean') {
console.error('Rom Patcher JS: zip.js web worker not found');
throw new Error('Rom Patcher JS: zip.js web worker not found'); throw new Error('Rom Patcher JS: zip.js web worker not found');
} }
zip.useWebWorkers = true; zip.useWebWorkers = true;
@ -536,6 +537,7 @@ var RomPatcherWeb = (function () {
new BinFile(this, RomPatcherWeb.provideRomFile); new BinFile(this, RomPatcherWeb.provideRomFile);
}); });
} else { } else {
console.error('Rom Patcher JS: input#rom-patcher-input-file-rom[type=file] not found');
throw new Error('Rom Patcher JS: input#rom-patcher-input-file-rom[type=file] not found'); throw new Error('Rom Patcher JS: input#rom-patcher-input-file-rom[type=file] not found');
} }
if (validEmbededPatch) { if (validEmbededPatch) {
@ -546,6 +548,7 @@ var RomPatcherWeb = (function () {
this._unzipSelectedPatch(zippedEntryIndex); this._unzipSelectedPatch(zippedEntryIndex);
}); });
} else { } else {
console.error('Rom Patcher JS: select#rom-patcher-select-patch not found');
throw new Error('Rom Patcher JS: select#rom-patcher-select-patch not found'); throw new Error('Rom Patcher JS: select#rom-patcher-select-patch not found');
} }
const loadingSpan = document.createElement('span'); const loadingSpan = document.createElement('span');
@ -564,6 +567,7 @@ var RomPatcherWeb = (function () {
new BinFile(this, RomPatcherWeb.providePatchFile); new BinFile(this, RomPatcherWeb.providePatchFile);
}); });
} else { } else {
console.error('Rom Patcher JS: input#rom-patcher-input-file-patch[type=file] not found');
throw new Error('Rom Patcher JS: input#rom-patcher-input-file-patch[type=file] not found'); throw new Error('Rom Patcher JS: input#rom-patcher-input-file-patch[type=file] not found');
} }
} }
@ -571,6 +575,7 @@ var RomPatcherWeb = (function () {
if (htmlButtonApply && htmlButtonApply.tagName === 'BUTTON') { if (htmlButtonApply && htmlButtonApply.tagName === 'BUTTON') {
htmlButtonApply.addEventListener('click', RomPatcherWeb.applyPatch); htmlButtonApply.addEventListener('click', RomPatcherWeb.applyPatch);
} else { } else {
console.error('Rom Patcher JS: button#rom-patcher-button-apply not found');
throw new Error('Rom Patcher JS: button#rom-patcher-button-apply not found'); throw new Error('Rom Patcher JS: button#rom-patcher-button-apply not found');
} }
const htmlCheckboxAlterHeader = htmlElements.get('checkbox-alter-header'); const htmlCheckboxAlterHeader = htmlElements.get('checkbox-alter-header');
@ -840,17 +845,17 @@ var RomPatcherWeb = (function () {
}, },
pickEmbededFile: function (fileName) { pickEmbededFile: function (fileName) {
if(!currentEmbededPatches) if (!currentEmbededPatches)
throw new Error('No embeded patches available'); throw new Error('No embeded patches available');
else if (typeof fileName !== 'string') else if (typeof fileName !== 'string')
throw new Error('Invalid embeded patch file name'); throw new Error('Invalid embeded patch file name');
const selectPatch = htmlElements.get('select-patch'); const selectPatch = htmlElements.get('select-patch');
for(var i=0; i<selectPatch.children.length; i++){ for (var i = 0; i < selectPatch.children.length; i++) {
if(selectPatch.children[i].patchFileName === fileName){ if (selectPatch.children[i].patchFileName === fileName) {
if(selectPatch.value != selectPatch.children[i].value){ if (selectPatch.value != selectPatch.children[i].value) {
selectPatch.value = selectPatch.children[i].value; selectPatch.value = selectPatch.children[i].value;
/* create and dispatch change event */ /* create and dispatch change event */
const evt = new Event('change'); const evt = new Event('change');
selectPatch.dispatchEvent(evt); selectPatch.dispatchEvent(evt);
@ -906,12 +911,12 @@ var RomPatcherWeb = (function () {
}); });
}, },
setEmbededPatches: function (embededPatchInfo) { setEmbededPatches: function (embededPatchInfo) {
if(!currentEmbededPatches) if (!currentEmbededPatches)
throw new Error('Rom Patcher JS: not in embeded patch mode'); throw new Error('Rom Patcher JS: not in embeded patch mode');
/* embeded patches */ /* embeded patches */
var validEmbededPatch = _checkEmbededPatchParameter(embededPatchInfo); var validEmbededPatch = _checkEmbededPatchParameter(embededPatchInfo);
if(!validEmbededPatch) if (!validEmbededPatch)
throw new Error('Rom Patcher JS: invalid embeded patch parameter'); throw new Error('Rom Patcher JS: invalid embeded patch parameter');
_fetchPatchFile(validEmbededPatch); _fetchPatchFile(validEmbededPatch);
@ -1350,7 +1355,7 @@ const ZIPManager = (function (romPatcherWeb) {
option.patchFileName = selectablePatches[i].filename; option.patchFileName = selectablePatches[i].filename;
htmlElements.get('select-patch').appendChild(option); htmlElements.get('select-patch').appendChild(option);
} }
htmlElements.get('select-patch')._unzipSelectedPatch = function(fileIndex){ htmlElements.get('select-patch')._unzipSelectedPatch = function (fileIndex) {
_unzipEntry(selectablePatches[fileIndex], romPatcherWeb.providePatchFile); _unzipEntry(selectablePatches[fileIndex], romPatcherWeb.providePatchFile);
}; };