/* savegame-editor.js v20180408 A library that lets you create easily a savegame editor. Made with vanilla JS. by Marc Robledo 2016-2018 http://www.marcrobledo.com/license */ /* LIBRARIES */ /* MarcBinFile.js v2016 */ function MarcBinFile(a,b){if("function"!=typeof window.FileReader)throw console.error("MarcBinFile.js: Browser doesn't support FileReader."),"Invalid browser";if("object"==typeof a&&a.name&&a.size)this.file=a,this.fileName=this.file.name,this.fileSize=this.file.size,this.fileType=a.type;else if("object"==typeof a&&a.files){if(1!=a.files.length){for(var c=[],d=a.files.length,e=function(){d--,0==d&&b&&b.call()},f=0;f0;e++)d+=String.fromCharCode(c[e]);return d},MarcBinFile.prototype.writeByte=function(a,b){this.fileReader.dataView.setUint8(a,b,this.littleEndian)},MarcBinFile.prototype.writeByteSigned=function(a,b){this.fileReader.dataView.setInt8(a,b,this.littleEndian)},MarcBinFile.prototype.writeBytes=function(a,b){for(var c=0;c=0){ SavegameEditor.save(); tempFile.save(); }else{ MarcDialogs.open('warning'); } } function closeFileConfirm(){ MarcDialogs.confirm('All changes will be lost.', function(){ closeFile(); MarcDialogs.close() }); } function closeFile(){ show('dragzone'); hide('the-editor'); hide('toolbar'); } function getSavegameDefaultName(){ if(typeof SavegameEditor.Filename==='string') return SavegameEditor.Filename; return SavegameEditor.Filename[0] } function getSavegameAllNames(){ if(typeof SavegameEditor.Filename==='string') return SavegameEditor.Filename; else{ var s=''; for(var i=0; i Browse '+getSavegameAllNames()+' or drop it here'; var inputFile=document.createElement('input'); inputFile.type='file'; inputFile.className='hidden'; inputFile.id='file-load'; inputFile.addEventListener('change', function(){ loadSavegameFromInput(this); }, false); var demoMessage=document.createElement('a'); demoMessage.id='demo'; demoMessage.href=SavegameEditor.Filename; demoMessage.download=SavegameEditor.Filename; demoMessage.innerHTML='Do you want to try it out? Download an example savegame'; dragZone.appendChild(dragMessage); dragZone.appendChild(inputFile); dragZone.appendChild(demoMessage); document.body.appendChild(dragZone); MarcDragAndDrop.add('dragzone', function(droppedFiles){ tempFile=new MarcBinFile(droppedFiles[0], _tempFileLoadFunction); }); var warningDialog=document.createElement('div'); warningDialog.className='dialog'; warningDialog.id='dialog-warning'; warningDialog.innerHTML='Use this tool at your own risk. By using it, you are responsible of any data lost.'; var divButtons=document.createElement('div'); divButtons.className='buttons'; var understandButton=document.createElement('button'); understandButton.innerHTML='I understand'; understandButton.addEventListener('click',function(){ var EXPIRE_DAYS=3; var d=new Date(); d.setTime(d.getTime()+(EXPIRE_DAYS*24*60*60*1000)); document.cookie="hideWarningMessage=1;expires="+d.toUTCString();//+";path=./"; MarcDialogs.close(); saveChanges(); }, false); divButtons.appendChild(understandButton); warningDialog.appendChild(divButtons); document.body.appendChild(warningDialog); }, false); /* binary and other helpers */ function compareBytes(offset,a2){ var a1=tempFile.readBytes(offset, a2.length); for(var i=0;i field.maxValue){ val=field.maxValue; } field.value=val; } function fixNumericFieldValueFromEvent(){fixNumericFieldValue(this)} function inputNumber(id,min,max,def){ var input=document.createElement('input'); input.id='number-'+id; input.className='full-width text-right'; input.type='text'; /* type='number' validation breaks getting input value when it's not valid */ input.minValue=min; input.maxValue=max; input.value=def; input.addEventListener('change', fixNumericFieldValueFromEvent, false); return input; } function inputFloat(id,min,max,def){ var input=document.createElement('input'); input.id='float-'+id; input.className='full-width text-right'; input.type='text'; input.minValue=min; input.maxValue=max; input.value=def; input.addEventListener('change', fixNumericFieldValueFromEvent, false); return input } function input(id,def){ var input=document.createElement('input'); input.id='input-'+id; input.className='full-width'; input.type='text'; input.value=def; return input } function checkbox(id,val){ var input=document.createElement('input'); input.id='checkbox-'+id; input.type='checkbox'; if(val) input.value=val; return input } function select(id,options,func){ var select; if(document.getElementById('select-'+id)){ select=document.getElementById('select-'+id); }else{ select=document.createElement('select'); select.id='select-'+id; select.className='full-width'; } if(options){ for(var i=0; i