Skip to content

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
3.9 KB
Raw Blame
const PPF_MAGIC="PPF",PPF_IMAGETYPE_BIN=0,PPF_IMAGETYPE_GI=1,PPF_BEGIN_FILE_ID_DIZ_MAGIC="@BEG";function PPF(){this.version=3,this.imageType=0,this.blockCheck=!1,this.undoData=!1,this.records=[]}"undefined"!=typeof module&&module.exports&&(module.exports=PPF),PPF.prototype.addRecord=function(e,i,t){this.undoData?this.records.push({offset:e,data:i,undoData:t}):this.records.push({offset:e,data:i})},PPF.prototype.toString=function(){var e=this.description;return e+="\nPPF version: "+this.version,e+="\n#Records: "+this.records.length,e+="\nImage type: "+this.imageType,e+="\nBlock check: "+!!this.blockCheck,e+="\nUndo data: "+this.undoData,this.fileIdDiz&&(e+="\nFILE_ID.DIZ: "+this.fileIdDiz),e},PPF.prototype.export=function(e){for(var i=56,t=0;t<this.records.length;t++)i+=5+this.records[t].data.length,3===this.version&&(i+=4);3!==this.version&&2!==this.version||(i+=4),this.blockCheck&&(i+=1024),this.fileIdDiz&&(i+=18+this.fileIdDiz.length+16+4),tempFile=new BinFile(i),tempFile.fileName=e+".ppf",tempFile.writeString("PPF"),tempFile.writeString((10*this.version).toString()),tempFile.writeU8(parseInt(this.version)-1),tempFile.writeString(this.description,50),3===this.version?(tempFile.writeU8(this.imageType),tempFile.writeU8(this.blockCheck?1:0),tempFile.writeU8(this.undoData?1:0),tempFile.writeU8(0)):2===this.version&&tempFile.writeU32(this.inputFileSize),this.blockCheck&&tempFile.writeBytes(this.blockCheck),tempFile.littleEndian=!0;for(t=0;t<this.records.length;t++){if(tempFile.writeU32(4294967295&this.records[t].offset),3===this.version){for(var r=this.records[t].offset,s=0;s<32;s++)r=parseInt(r/2>>>0);tempFile.writeU32(r)}tempFile.writeU8(this.records[t].data.length),tempFile.writeBytes(this.records[t].data),this.undoData&&tempFile.writeBytes(this.records[t].undoData)}return this.fileIdDiz&&(tempFile.writeString("@BEGIN_FILE_ID.DIZ"),tempFile.writeString(this.fileIdDiz),tempFile.writeString("@END_FILE_ID.DIZ"),tempFile.writeU16(this.fileIdDiz.length),tempFile.writeU16(0)),tempFile},PPF.prototype.apply=function(e){for(var i=e.fileSize,t=0;t<this.records.length;t++)this.records[t].offset+this.records[t].data.length>i&&(i=this.records[t].offset+this.records[t].data.length);i===e.fileSize?tempFile=e.slice(0,e.fileSize):(tempFile=new BinFile(i),e.copyTo(tempFile,0));var r=!1;if(this.undoData){tempFile.seek(this.records[0].offset);var s=tempFile.readBytes(this.records[0].data.length),o=!1;for(t=0;t<s.length&&!o;t++)s[t]!==this.records[0].data[t]&&(o=!0);o||(r=!0)}for(t=0;t<this.records.length;t++)tempFile.seek(this.records[t].offset),r?tempFile.writeBytes(this.records[t].undoData):tempFile.writeBytes(this.records[t].data);return tempFile},PPF.MAGIC="PPF",PPF.fromFile=function(e){var i=new PPF;e.seek(3);var t=parseInt(e.readString(2))/10;if(t!==e.readU8()+1||t>3)throw new Error("invalid PPF version");for(i.version=t,i.description=e.readString(50).replace(/ +$/,""),3===i.version?(i.imageType=e.readU8(),e.readU8()&&(i.blockCheck=!0),e.readU8()&&(i.undoData=!0),e.skip(1)):2===i.version&&(i.blockCheck=!0,i.inputFileSize=e.readU32()),i.blockCheck&&(i.blockCheck=e.readBytes(1024)),e.littleEndian=!0;!e.isEOF();){if("@BEG"===e.readString(4)){e.skip(14),i.fileIdDiz=e.readString(3072),i.fileIdDiz=i.fileIdDiz.substr(0,i.fileIdDiz.indexOf("@END_FILE_ID.DIZ"));break}var r;if(e.skip(-4),3===i.version)r=e.readU32()+4294967296*e.readU32();else r=e.readU32();var s=e.readU8(),o=e.readBytes(s),d=!1;i.undoData&&(d=e.readBytes(s)),i.addRecord(r,o,d)}return i},PPF.buildFromRoms=function(e,i){var t=new PPF;if(t.description="Patch description",e.fileSize>i.fileSize){var r=new BinFile(e.fileSize);i.copyTo(r,0),i=r}for(e.seek(0),i.seek(0);!i.isEOF();){var s=e.isEOF()?0:e.readU8(),o=i.readU8();if(s!==o){for(var d=[],n=i.offset-1;s!==o&&d.length<255&&(d.push(o),!i.isEOF()&&255!==d.length);)s=e.isEOF()?0:e.readU8(),o=i.readU8();t.addRecord(n,d)}}return e.fileSize<i.fileSize&&(i.seek(i.fileSize-1),0===i.readU8()&&t.addRecord(i.fileSize-1,[0])),t};