mirror of
https://github.com/marcrobledo/RomPatcher.js.git
synced 2025-06-27 16:25:54 +00:00
fixed MD5 hashing with >512MB files, ignore txt, readmes,etc files in zips, multiple predefined patches is now a dropdown, allowing user to choose and change patch without having to refresh the app
This commit is contained in:
parent
bcc714f179
commit
458d7297e4
7 changed files with 366 additions and 251 deletions
|
@ -1,83 +1,150 @@
|
|||
/* ZIP module for Rom Patcher JS v20190531 - Marc Robledo 2016-2019 - http://www.marcrobledo.com/license */
|
||||
/* ZIP module for Rom Patcher JS v20210815 - Marc Robledo 2016-2021 - http://www.marcrobledo.com/license */
|
||||
|
||||
const ZIP_MAGIC='\x50\x4b\x03\x04';
|
||||
|
||||
function parseZIPFile(zipFile, unzipEntryName){
|
||||
var regex=(zipFile===patchFile)? /\.(ips|ups|bps|aps|rup|ppf|xdelta)$/i : /\.(\w+)$/i;
|
||||
setMessage('apply', _('unzipping'), 'loading');
|
||||
var ZIPManager=(function(){
|
||||
const FILTER_PATCHES=/\.(ips|ups|bps|aps|rup|ppf|xdelta)$/i;
|
||||
const FILTER_ROMS=/(?<!\.(txt|diz|rtf|docx?|html?|pdf|jpe?g|gif|png|bmp|zip|rar|7z))$/i;
|
||||
|
||||
var arrayBuffer=zipFile._u8array.buffer;
|
||||
zip.createReader(
|
||||
new zip.BlobReader(new Blob([arrayBuffer])),
|
||||
function(zipReader){
|
||||
zipReader.getEntries(function(zipEntries){
|
||||
var zippedFiles=[];
|
||||
for(var i=0; i<zipEntries.length; i++){
|
||||
if(typeof unzipEntryName==='string' && unzipEntryName === zipEntries[i].filename){
|
||||
zippedFiles=[zipEntries[i]];
|
||||
break;
|
||||
}else if(regex.test(zipEntries[i].filename)){
|
||||
zippedFiles.push(zipEntries[i]);
|
||||
}
|
||||
}
|
||||
var _unzipEntry=function(zippedEntry, dest, dest2, parse){
|
||||
zippedEntry.getData(new zip.BlobWriter(), function(blob){
|
||||
var fileReader=new FileReader();
|
||||
fileReader.onload=function(){
|
||||
var unzippedFile=new MarcFile(this.result);
|
||||
unzippedFile.fileName=zippedEntry.filename;
|
||||
|
||||
if(zippedFiles.length>1){
|
||||
var zipOverlay=document.createElement('div');
|
||||
zipOverlay.className='zip-overlay';
|
||||
var zipDialog=document.createElement('div');
|
||||
zipDialog.className='zip-dialog';
|
||||
var zipList=document.createElement('ul');
|
||||
zipList.className='zipped-files'
|
||||
for(var i=0; i<zippedFiles.length; i++){
|
||||
var li=document.createElement('li');
|
||||
li.zipEntry=zippedFiles[i];
|
||||
li.zipEntry.originalZipFile=zipFile;
|
||||
li.innerHTML=zippedFiles[i].filename;
|
||||
addEvent(li, 'click', _evtClickZipEntry);
|
||||
zipList.appendChild(li);
|
||||
}
|
||||
zipDialog.innerHTML=_('patch_file');
|
||||
zipDialog.appendChild(zipList);
|
||||
zipOverlay.appendChild(zipDialog);
|
||||
document.body.appendChild(zipOverlay);
|
||||
}else if(zippedFiles.length===1){
|
||||
zippedFiles[0].originalZipFile=zipFile;
|
||||
unzipEntry(zippedFiles[0]);
|
||||
}else{
|
||||
if(zipFile===romFile){
|
||||
romFile=null;
|
||||
}else{
|
||||
patchFile=null;
|
||||
}
|
||||
if(dest.patches){
|
||||
dest.patches[dest2].fetchedFile=unzippedFile;
|
||||
if(parse)
|
||||
parseCustomPatch(dest.patches[dest2]);
|
||||
}if(dest===romFile){
|
||||
romFile=unzippedFile;
|
||||
_parseROM();
|
||||
}else if(dest===patchFile){
|
||||
patchFile=unzippedFile;
|
||||
_readPatchFile();
|
||||
}
|
||||
setTabApplyEnabled(true);
|
||||
});
|
||||
},
|
||||
function(zipReader){
|
||||
setTabApplyEnabled(true);
|
||||
setMessage('apply', _('error_unzipping'), 'error');
|
||||
};
|
||||
fileReader.readAsArrayBuffer(blob);
|
||||
});
|
||||
};
|
||||
|
||||
return{
|
||||
parseFile:function(sourceFile, compressedFileIndex){
|
||||
setMessage('apply', _('unzipping'), 'loading');
|
||||
|
||||
var arrayBuffer=sourceFile._u8array.buffer;
|
||||
zip.createReader(
|
||||
new zip.BlobReader(new Blob([arrayBuffer])),
|
||||
/* success */
|
||||
function(zipReader){
|
||||
zipReader.getEntries(function(zipEntries){
|
||||
var regex=(sourceFile===romFile)? FILTER_ROMS : FILTER_PATCHES;
|
||||
var filteredEntries=[];
|
||||
for(var i=0; i<zipEntries.length; i++){
|
||||
if(regex.test(zipEntries[i].filename) && !zipEntries[i].directory){
|
||||
filteredEntries.push(zipEntries[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
var customPatch=false;
|
||||
if(isCustomPatcherEnabled()){
|
||||
for(var i=0; i<CUSTOM_PATCHER.length && !customPatch; i++){
|
||||
if(CUSTOM_PATCHER[i].fetchedFile===sourceFile)
|
||||
customPatch=CUSTOM_PATCHER[i];
|
||||
}
|
||||
}
|
||||
|
||||
if(customPatch){
|
||||
if(customPatch.patches){
|
||||
for(var i=0; i<customPatch.patches.length; i++){
|
||||
for(var j=0; j<filteredEntries.length; j++){
|
||||
if(customPatch.patches[i].file===filteredEntries[j].filename){
|
||||
_unzipEntry(filteredEntries[j], customPatch, i, i===compressedFileIndex);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
var nextOption;
|
||||
var customPatchIndex=CUSTOM_PATCHER.indexOf(customPatch);
|
||||
customPatch.patches=[];
|
||||
for(var i=0; i<filteredEntries.length; i++){
|
||||
customPatch.patches.push({
|
||||
file:filteredEntries[i].filename,
|
||||
fetchedFile:false,
|
||||
name:customPatch.name + ' - ' + filteredEntries[i].filename,
|
||||
crc:customPatch.crc
|
||||
});
|
||||
|
||||
|
||||
var option;
|
||||
if(i===0){
|
||||
option=customPatch.selectOption;
|
||||
nextOption=option.nextSibling;
|
||||
}else{
|
||||
option=document.createElement('option');
|
||||
if(nextOption)
|
||||
el('input-file-patch').insertBefore(option, nextOption);
|
||||
else
|
||||
el('input-file-patch').appendChild(option);
|
||||
}
|
||||
option.value=customPatchIndex+','+i;
|
||||
option.innerHTML=customPatch.patches[i].name;
|
||||
nextOption=option.nextSibling;
|
||||
_unzipEntry(filteredEntries[i], customPatch, i, i===0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
setTabApplyEnabled(false);
|
||||
}else{
|
||||
var _evtClickDialogEntry=function(evt){
|
||||
document.body.removeChild(this.parentElement.parentElement.parentElement);
|
||||
_unzipEntry(this.zipEntry, sourceFile);
|
||||
}
|
||||
|
||||
if(filteredEntries.length>1){
|
||||
var zipOverlay=document.createElement('div');
|
||||
zipOverlay.className='zip-overlay';
|
||||
var zipDialog=document.createElement('div');
|
||||
zipDialog.className='zip-dialog';
|
||||
var zipList=document.createElement('ul');
|
||||
zipList.className='zipped-files'
|
||||
for(var i=0; i<filteredEntries.length; i++){
|
||||
var li=document.createElement('li');
|
||||
li.zipEntry=filteredEntries[i];
|
||||
li.innerHTML=filteredEntries[i].filename;
|
||||
addEvent(li, 'click', _evtClickDialogEntry);
|
||||
zipList.appendChild(li);
|
||||
}
|
||||
zipDialog.innerHTML=_('patch_file');
|
||||
zipDialog.appendChild(zipList);
|
||||
zipOverlay.appendChild(zipDialog);
|
||||
document.body.appendChild(zipOverlay);
|
||||
}else if(filteredEntries.length===1){
|
||||
_unzipEntry(filteredEntries[0], sourceFile);
|
||||
}else{
|
||||
if(sourceFile===romFile){
|
||||
romFile=null;
|
||||
}else if(sourceFile===patchFile){
|
||||
patchFile=null;
|
||||
setMessage('apply', _('error_invalid_patch'), 'error');
|
||||
}
|
||||
}
|
||||
|
||||
setTabApplyEnabled(true);
|
||||
}
|
||||
});
|
||||
},
|
||||
/* failed */
|
||||
function(zipReader){
|
||||
setTabApplyEnabled(true);
|
||||
setMessage('apply', _('error_unzipping'), 'error');
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function unzipEntry(zipEntry){
|
||||
zipEntry.getData(new zip.BlobWriter(), function(blob){
|
||||
var fileReader=new FileReader();
|
||||
fileReader.onload=function(){
|
||||
var unzippedFile=new MarcFile(this.result);
|
||||
unzippedFile.fileName=zipEntry.filename;
|
||||
if(zipEntry.originalZipFile===romFile){
|
||||
romFile=unzippedFile;
|
||||
_parseROM();
|
||||
}else if(zipEntry.originalZipFile===patchFile){
|
||||
patchFile=unzippedFile;
|
||||
_readPatchFile();
|
||||
}
|
||||
};
|
||||
fileReader.readAsArrayBuffer(blob);
|
||||
});
|
||||
}
|
||||
|
||||
function _evtClickZipEntry(evt){
|
||||
document.body.removeChild(this.parentElement.parentElement.parentElement);
|
||||
unzipEntry(this.zipEntry);
|
||||
}
|
||||
}
|
||||
})();
|
Loading…
Add table
Add a link
Reference in a new issue