Manic EMU is an all-in-one retro game emulator for iOS. It packs powerful features while keeping a clean, sleek UI and delivering buttery-smooth gameplay.
Swift
const IPS_MAGIC="PATCH",IPS_MAX_ROM_SIZE=16777216,IPS_RECORD_RLE=0,IPS_RECORD_SIMPLE=1;function IPS(){this.records=[],this.truncate=!1,this.EBPmetadata=null}"undefined"!=typeof module&&module.exports&&(module.exports=IPS),IPS.prototype.addSimpleRecord=function(e,t){this.records.push({offset:e,type:1,length:t.length,data:t})},IPS.prototype.addRLERecord=function(e,t,r){this.records.push({offset:e,type:0,length:t,byte:r})},IPS.prototype.setEBPMetadata=function(e){if("object"!=typeof e)throw new TypeError("metadataObject must be an object");for(var t in e)if("string"!=typeof e[t])throw new TypeError("metadataObject values must be strings");this.EBPmetadata={patcher:"EBPatcher",...e}},IPS.prototype.getDescription=function(){if(this.EBPmetadata){var e="";for(var t in this.EBPmetadata){if("patcher"===t)continue;e+=t.charAt(0).toUpperCase()+t.slice(1)+": "+this.EBPmetadata[t]+"\n"}return e.trim()}return null},IPS.prototype.toString=function(){nSimpleRecords=0,nRLERecords=0;for(var e=0;e<this.records.length;e++)0===this.records[e].type?nRLERecords++:nSimpleRecords++;var t="Simple records: "+nSimpleRecords;return t+="\nRLE records: "+nRLERecords,t+="\nTotal records: "+this.records.length,this.truncate&&!this.EBPmetadata?t+="\nTruncate at: 0x"+this.truncate.toString(16):this.EBPmetadata&&(t+="\nEBP Metadata: "+JSON.stringify(this.EBPmetadata)),t},IPS.prototype.export=function(e){for(var t=5,r=0;r<this.records.length;r++)0===this.records[r].type?t+=8:t+=5+this.records[r].data.length;t+=3,this.truncate&&!this.EBPmetadata?t+=3:this.EBPmetadata&&(t+=JSON.stringify(this.EBPmetadata).length),tempFile=new BinFile(t),tempFile.fileName=e+(this.EBPmetadata?".ebp":".ips"),tempFile.writeString("PATCH");for(r=0;r<this.records.length;r++){var i=this.records[r];tempFile.writeU24(i.offset),0===i.type?(tempFile.writeU16(0),tempFile.writeU16(i.length),tempFile.writeU8(i.byte)):(tempFile.writeU16(i.data.length),tempFile.writeBytes(i.data))}return tempFile.writeString("EOF"),this.truncate&&!this.EBPmetadata?tempFile.writeU24(this.truncate):this.EBPmetadata&&tempFile.writeString(JSON.stringify(this.EBPmetadata)),tempFile},IPS.prototype.apply=function(e){if(this.truncate&&!this.EBPmetadata)this.truncate>e.fileSize?(tempFile=new BinFile(this.truncate),e.copyTo(tempFile,0,e.fileSize,0)):tempFile=e.slice(0,this.truncate);else{for(var t=e.fileSize,r=0;r<this.records.length;r++){var i=this.records[r];0===i.type?i.offset+i.length>t&&(t=i.offset+i.length):i.offset+i.data.length>t&&(t=i.offset+i.data.length)}t===e.fileSize?tempFile=e.slice(0,e.fileSize):(tempFile=new BinFile(t),e.copyTo(tempFile,0))}e.seek(0);for(r=0;r<this.records.length;r++)if(tempFile.seek(this.records[r].offset),0===this.records[r].type)for(var a=0;a<this.records[r].length;a++)tempFile.writeU8(this.records[r].byte);else tempFile.writeBytes(this.records[r].data);return tempFile},IPS.MAGIC="PATCH",IPS.fromFile=function(e){var t=new IPS;for(e.seek(5);!e.isEOF();){var r=e.readU24();if(4542278===r){if(e.isEOF())break;if(e.offset+3===e.fileSize){t.truncate=e.readU24();break}if(e.readU8()==="{".charCodeAt(0)){e.skip(-1),t.setEBPMetadata(JSON.parse(e.readString(e.fileSize-e.offset)));break}}var i=e.readU16();0===i?t.addRLERecord(r,e.readU16(),e.readU8()):t.addSimpleRecord(r,e.readBytes(i))}return t},IPS.buildFromRoms=function(e,t,r=!1){var i=new IPS;!r&&t.fileSize<e.fileSize?i.truncate=t.fileSize:r&&i.setEBPMetadata("object"==typeof r?r:{Author:"Unknown",Title:"Untitled",Description:"No description"});for(var a={type:3735928559,startOffset:0,length:0};!t.isEOF();){var s=e.isEOF()?0:e.readU8(),o=t.readU8();if(s!==o){for(var n=!0,d=[],l=t.offset-1;s!==o&&d.length<65535&&(d.push(o),o!==d[0]&&(n=!1),!t.isEOF()&&65535!==d.length);)s=e.isEOF()?0:e.readU8(),o=t.readU8();var f=l-(a.offset+a.length);if(1===a.type&&f<6&&a.length+f+d.length<65535)if(n&&d.length>6)e.seek(l),t.seek(l),a={type:3735928559,startOffset:0,length:0};else{for(;f--;)a.data.push(t._u8array[a.offset+a.length]),a.length++;a.data=a.data.concat(d),a.length=a.data.length}else{if(l>=16777216)throw new Error(`Files are too big for ${i.EBPmetadata?"EBP":"IPS"} format`);n&&d.length>2?i.addRLERecord(l,d.length,d[0]):i.addSimpleRecord(l,d),a=i.records[i.records.length-1]}}}if(t.fileSize>e.fileSize){var h=i.records[i.records.length-1];h.offset+h.length<t.fileSize&&i.addSimpleRecord(t.fileSize-1,[0])}return i};