-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathPowerNBT.min.js
7 lines (7 loc) · 17 KB
/
PowerNBT.min.js
1
2
3
4
5
6
7
/**
* powernbt-js
* @version v1.2.0 - 2015-01-07T22:34:37.748Z
* @author DPOH-VAR ([email protected])
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
!function(t,e){"use strict";function r(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r]);t.prototype=Object.create(e.prototype,{constructor:{value:t}})}function n(t){this._buffer=t.buffer||t,this._offset=0}function a(){this._buffer=[]}var o;o=window.Long;var i={};i.readTag=function(t,r){var a;a=t instanceof n?t:new n(t);var o="";if(r==e){if(r=a.readInt8(),r==i.Type.TAG_END)return new i.NBTTagEnd;var T=a.readInt16(),s=a.read(T),p=new Int8Array(s);o=u.decode(p)}switch(r){case i.Type.TAG_BYTE:return new i.NBTTagByte(a.read(1),o);case i.Type.TAG_SHORT:return new i.NBTTagShort(a.read(2),o);case i.Type.TAG_INT:return new i.NBTTagInt(a.read(4),o);case i.Type.TAG_LONG:return new i.NBTTagLong(a.read(8),o);case i.Type.TAG_FLOAT:return new i.NBTTagFloat(a.read(4),o);case i.Type.TAG_DOUBLE:return new i.NBTTagDouble(a.read(8),o);case i.Type.TAG_BYTE_ARRAY:var f=a.readInt32();return new i.NBTTagByteArray(a.read(f),o);case i.Type.TAG_STRING:var y=a.readInt16();return new i.NBTTagString(a.read(y),o);case i.Type.TAG_LIST:for(var g=a.readInt8(),h=a.readInt32(),B=[],l=0;h>l;l++){var N=i.readTag(a,g);B.push(N)}return new i.NBTTagList(g,B,o);case i.Type.TAG_COMPOUND:for(var c={};;){var _=i.readTag(a);if(_.type==i.Type.TAG_END)break;c[_._name]=_}return new i.NBTTagCompound(c,o);case i.Type.TAG_INT_ARRAY:var w=a.readInt32();return new i.NBTTagIntArray(a.read(4*w),o);default:throw new Error("NBT parsing error: tag type: "+r)}},"function"==typeof require&&"object"==typeof module&&module&&"object"==typeof exports&&exports?module.exports=i:t.NBT=i;var u={encode:function(t){t=t.replace(/\r\n/g,"\n");for(var e=[],r=0;r<t.length;r++){var n=t.charCodeAt(r);128>n?e[e.length]=n:n>127&&2048>n?(e[e.length]=n>>6|192,e[e.length]=63&n|128):(e[e.length]=n>>12|224,e[e.length]=n>>6&63|128,e[e.length]=63&n|128)}return e},decode:function(t){for(var e=t.length||t.byteLength,r="",n=0,a=0,o=0,i=0;e>n;)a=255&t[n],128>a?(r+=String.fromCharCode(a),n++):a>191&&224>a?(o=255&t[n+1],r+=String.fromCharCode((31&a)<<6|63&o),n+=2):(o=255&t[n+1],i=255&t[n+2],r+=String.fromCharCode((15&a)<<12|(63&o)<<6|63&i),n+=3);return r}};i.Type={TAG_END:0,TAG_BYTE:1,TAG_SHORT:2,TAG_INT:3,TAG_LONG:4,TAG_FLOAT:5,TAG_DOUBLE:6,TAG_BYTE_ARRAY:7,TAG_STRING:8,TAG_LIST:9,TAG_COMPOUND:10,TAG_INT_ARRAY:11},i.NBTBase=function(t){if(this.constructor==i.NBTBase)throw new Error("NBTBase is abstract");this._name=(t||"")+""},Object.defineProperty(i.NBTBase.prototype,"name",{enumerable:!0,get:function(){return this._name}}),i.NBTBase.prototype.type=null,i.NBTBase.prototype.clone=function(){},i.NBTBase.prototype.toString=function(){},i.NBTBase.prototype.save=function(t,e){return null==t&&(t=new a),t.writeInt8(this.type),this.type==i.Type.TAG_END?t:(t.writeUtf8String(null==e?this.name:e),this._write(t),t)},Object.defineProperty(i.NBTBase.prototype,"typeName",{enumerable:!0,get:function(){return i.TypeName[this.type]}}),i.NBTBase.prototype._write=function(){},i.NBTNumber=function(t,e){if(this.constructor==i.NBTNumber)throw new Error("NBTNumber is abstract");i.NBTBase.call(this,e),this._value=new ArrayBuffer(t)},r(i.NBTNumber,i.NBTBase),i.NBTNumber.prototype.valueOf=function(){},i.NBTNumber.prototype._write=function(t){t.write(this._value)},i.NBTArray=function(t){if(this.constructor==i.NBTArray)throw new Error("NBTArray is abstract");i.NBTBase.call(this,t)},r(i.NBTArray,i.NBTBase),i.NBTArray.prototype.toArray=function(){for(var t=[],e=0;e<this.size;e++)t.push(this.get(e));return t},n.prototype.read=function(t){var e=this._buffer.slice(this._offset,this._offset+t);return this._offset+=t,e},n.prototype.readInt8=function(){var t=new DataView(this._buffer).getInt8(this._offset);return this._offset+=1,t},n.prototype.readInt16=function(){var t=new DataView(this._buffer).getInt16(this._offset);return this._offset+=2,t},n.prototype.readInt32=function(){var t=new DataView(this._buffer).getInt32(this._offset);return this._offset+=4,t},i.NBTTagByte=function(t,e){i.NBTNumber.call(this,1,e),t&&this.setValue(t)},r(i.NBTTagByte,i.NBTNumber),i.NBTTagByte.MAX_VALUE=127,i.NBTTagByte.MIN_VALUE=-128,i.NBTTagByte.prototype.type=i.Type.TAG_BYTE,i.NBTTagByte.prototype.toString=function(t){return new DataView(this._value).getInt8(0).toString(t)},i.NBTTagByte.prototype.valueOf=function(){return new DataView(this._value).getInt8(0)},i.NBTTagByte.prototype.clone=function(){return new i.NBTTagByte(this._value.slice(0),this._name)},i.NBTTagByte.prototype.setValue=function(t,e){if(t instanceof ArrayBuffer){if(1!=t.byteLength)throw new Error("NBTTagByte: byteLength must be 1");return void(this._value=t)}t instanceof i.NBTNumber&&(t=t.valueOf()),"string"==typeof t&&(t=parseInt(t,e));var r=new DataView(this._value);r.setInt8(0,t)},i.NBTTagByteArray=function(t,e){i.NBTArray.call(this,e),this.setValue(t)},r(i.NBTTagByteArray,i.NBTArray),i.NBTTagByteArray.prototype.type=i.Type.TAG_BYTE_ARRAY,i.NBTTagByteArray.prototype.toString=function(){return"NBTTagByteArray["+this._value.byteLength+"]"},i.NBTTagByteArray.prototype.clone=function(){return new i.NBTTagByteArray(this._value.slice(0),this._name)},i.NBTTagByteArray.prototype.setValue=function(t){if(null==t)return void(this._value=new ArrayBuffer(0));if(t instanceof ArrayBuffer)return void(this._value=t);"string"==typeof t&&(t=u.encode(t));var e=t.length||t.byteLength;this._value=new ArrayBuffer(e);for(var r=new DataView(this._value),n=0;e>n;n++)r.setInt8(n,t[n])},i.NBTTagByteArray.prototype.get=function(t){var e=new DataView(this._value);return e.getInt8(t)},i.NBTTagByteArray.prototype.set=function(t,e){var r=new DataView(this._value);r.setInt8(t,e)},i.NBTTagByteArray.prototype._write=function(t){t.writeInt32(this._value.byteLength),t.write(this._value)},Object.defineProperty(i.NBTTagByteArray.prototype,"size",{enumerable:!0,get:function(){return this._value.byteLength}}),i.NBTTagCompound=function(t,e){i.NBTBase.call(this,e),this._value=t||{}},r(i.NBTTagCompound,i.NBTBase),i.NBTTagCompound.prototype.type=i.Type.TAG_COMPOUND,i.NBTTagCompound.prototype.get=function(t){return this._value[t]},i.NBTTagCompound.prototype.put=function(t,e){e?(e._name=t,this._value[t]=e):this.remove(t)},i.NBTTagCompound.prototype.putClone=function(t,e){this.put(t,e&&e.clone())},i.NBTTagCompound.prototype.remove=function(t){var e=this._value[t];return delete this._value[t],e},i.NBTTagCompound.prototype.containsKey=function(t){return this._value[t]!=e},i.NBTTagCompound.prototype.clear=function(){this._value={}},i.NBTTagCompound.prototype.clone=function(){var t=new i.NBTTagCompound,e=this._value,r=t._value;for(var n in e)e.hasOwnProperty(n)&&(r[n]=e[n].clone());return t},i.NBTTagCompound.prototype.toString=function(){return"NBTTagCompound["+this.keys.length+"]"},Object.defineProperty(i.NBTTagCompound.prototype,"size",{enumerable:!0,get:function(){var t=0,e=this._value;for(var r in e)e.hasOwnProperty(r)&&t++;return t}}),Object.defineProperty(i.NBTTagCompound.prototype,"keys",{enumerable:!0,get:function(){var t=[],e=this._value;for(var r in e)e.hasOwnProperty(r)&&t.push(r);return t}}),Object.defineProperty(i.NBTTagCompound.prototype,"entries",{enumerable:!0,get:function(){var t=[],e=this._value;for(var r in e)e.hasOwnProperty(r)&&t.push({key:r,value:e[r]});return t}}),i.NBTTagCompound.prototype._write=function(t){var e=this._value;for(var r in e)if(e.hasOwnProperty(r)){var n=this._value[r];n.save(t,r)}t.writeInt8(0)},i.NBTTagDouble=function(t,e){i.NBTNumber.call(this,8,e),t&&this.setValue(t)},r(i.NBTTagDouble,i.NBTNumber),i.NBTTagDouble.prototype.type=i.Type.TAG_DOUBLE,i.NBTTagDouble.prototype.toString=function(){return new DataView(this._value).getFloat64(0).toString()},i.NBTTagDouble.prototype.valueOf=function(){return new DataView(this._value).getFloat64(0)},i.NBTTagDouble.prototype.clone=function(){return new i.NBTTagDouble(this._value.slice(0),this._name)},i.NBTTagDouble.prototype.setValue=function(t){if(t instanceof ArrayBuffer){if(8!=t.byteLength)throw new Error("NBTTagDouble: byteLength must be 8");return void(this._value=t)}"string"==typeof t&&(t=parseFloat(t));var e=new DataView(this._value);e.setFloat64(0,t)},i.NBTTagEnd=function(){i.NBTBase.call(this,"")},r(i.NBTTagEnd,i.NBTBase),i.NBTTagEnd.prototype.type=i.Type.TAG_END,i.NBTTagEnd.prototype.clone=function(){return new i.NBTTagEnd},i.NBTTagEnd.prototype.toString=function(){return""},i.NBTTagFloat=function(t,e){i.NBTNumber.call(this,4,e),t&&this.setValue(t)},r(i.NBTTagFloat,i.NBTNumber),i.NBTTagFloat.prototype.type=i.Type.TAG_FLOAT,i.NBTTagFloat.prototype.toString=function(){return new DataView(this._value).getFloat32(0).toString()},i.NBTTagFloat.prototype.valueOf=function(){return new DataView(this._value).getFloat32(0)},i.NBTTagFloat.prototype.clone=function(){return new i.NBTTagFloat(this._value.slice(0),this._name)},i.NBTTagFloat.prototype.setValue=function(t){if(t instanceof ArrayBuffer){if(4!=t.byteLength)throw new Error("NBTTagFloat: byteLength must be 4");return void(this._value=t)}"string"==typeof t&&(t=parseFloat(t));var e=new DataView(this._value);e.setFloat32(0,t)},i.NBTTagInt=function(t,e){i.NBTNumber.call(this,4,e),t&&this.setValue(t)},r(i.NBTTagInt,i.NBTNumber),i.NBTTagInt.MAX_VALUE=2147483647,i.NBTTagInt.MIN_VALUE=-2147483648,i.NBTTagInt.prototype.type=i.Type.TAG_INT,i.NBTTagInt.prototype.toString=function(t){return new DataView(this._value).getInt32(0).toString(t)},i.NBTTagInt.prototype.valueOf=function(){return new DataView(this._value).getInt32(0)},i.NBTTagInt.prototype.clone=function(){return new i.NBTTagInt(this._value.slice(0),this._name)},i.NBTTagInt.prototype.setValue=function(t,e){if(t instanceof ArrayBuffer){if(4!=t.byteLength)throw new Error("NBTTagInt: byteLength must be 4");return void(this._value=t)}t instanceof i.NBTNumber&&(t=t.valueOf()),"string"==typeof t&&(t=parseInt(t,e));var r=new DataView(this._value);r.setInt32(0,t)},i.NBTTagIntArray=function(t,e){i.NBTArray.call(this,e),this.setValue(t)},r(i.NBTTagIntArray,i.NBTArray),i.NBTTagIntArray.prototype.type=i.Type.TAG_INT_ARRAY,i.NBTTagIntArray.prototype.toString=function(){return"NBTTagIntArray["+Math.round(this._value.byteLength/4)+"]"},i.NBTTagIntArray.prototype.clone=function(){return new i.NBTTagIntArray(this._value.slice(0),this._name)},i.NBTTagIntArray.prototype.setValue=function(t){if(null==t)return void(this._value=new ArrayBuffer(0));if(t instanceof ArrayBuffer)return void(this._value=t);"string"==typeof t&&(t=u.encode(t));var e=t.length||t.byteLength;this._value=new ArrayBuffer(4*e);for(var r=new DataView(this._value),n=0;e>n;n++)r.setInt32(4*n,t[n])},i.NBTTagIntArray.prototype.get=function(t){var e=new DataView(this._value);return e.getInt32(4*t)},i.NBTTagIntArray.prototype.set=function(t,e){var r=new DataView(this._value);r.setInt32(4*t,e)},i.NBTTagIntArray.prototype._write=function(t){t.writeInt32(this._value.byteLength/4),t.write(this._value)},Object.defineProperty(i.NBTTagIntArray.prototype,"size",{enumerable:!0,get:function(){return this._value.byteLength/4}}),i.NBTTagList=function(t,e,r){this._listType=t||i.Type.TAG_END,i.NBTBase.call(this,r),this._value=e||[]},r(i.NBTTagList,i.NBTBase),i.NBTTagList.prototype.type=i.Type.TAG_LIST,i.NBTTagList.prototype.get=function(t){return this._value[t]},i.NBTTagList.prototype.set=function(t,e){if(this._listType==i.Type.TAG_END)this._listType=e.type;else if(this._listType!=e.type)throw new Error("NBT tag has wrong type: "+e.type+"; expected: "+this._listType);e._name="",this._value[t]=e},i.NBTTagList.prototype.add=function(){for(var t=0;t<arguments.length;t++){var e=arguments[t];if(this._listType==i.Type.TAG_END)this._listType=e.type;else if(this._listType!=e.type)throw new Error("NBT tag has wrong type: "+e.type+"; expected: "+this._listType);this._value.push(e)}},i.NBTTagList.prototype.remove=function(t){this._value.splice(t,1)},i.NBTTagList.prototype.clear=function(){this._value.length=0},Object.defineProperty(i.NBTTagList.prototype,"listType",{enumerable:!0,get:function(){return this._listType}}),Object.defineProperty(i.NBTTagList.prototype,"size",{enumerable:!0,get:function(){return this._value.length}}),Object.defineProperty(i.NBTTagList.prototype,"list",{enumerable:!0,get:function(){return this._value.slice(0)}}),Object.defineProperty(i.NBTTagList.prototype,"listTypeName",{enumerable:!0,get:function(){return i.TypeName[this.listType]}}),i.NBTTagList.prototype.toString=function(){return"NBTTagList["+this._value.length+"]"},i.NBTTagList.prototype._write=function(t){t.writeInt8(this._listType),t.writeInt32(this._value.length),this._value.forEach(function(e){e._write(t)})},i.NBTTagLong=function(t,e){i.NBTNumber.call(this,8,e),t&&this.setValue(t)},r(i.NBTTagLong,i.NBTNumber),i.NBTTagLong.MAX_VALUE=9007199254740992,i.NBTTagLong.MIN_VALUE=-9007199254740992,i.NBTTagLong.MAX_VALUE_STR="9223372036854775807",i.NBTTagLong.MIN_VALUE_STR="-9223372036854775808",i.NBTTagLong.prototype.type=i.Type.TAG_LONG,i.NBTTagLong.prototype.toString=function(t){return this._long().toString(t)},i.NBTTagLong.prototype.getHigh=function(){return new DataView(this._value).getInt32(0)},i.NBTTagLong.prototype.setHigh=function(t){return new DataView(this._value).setInt32(0,t),this},i.NBTTagLong.prototype.getLow=function(){return new DataView(this._value).getInt32(4)},i.NBTTagLong.prototype.setLow=function(t){return new DataView(this._value).setInt32(4,t),this},i.NBTTagLong.prototype._long=function(){return new o(this.getLow(),this.getHigh())},i.NBTTagLong.prototype.setValue=function(t,e){if(t instanceof ArrayBuffer){if(8!=t.byteLength)throw new Error("NBTTagLong: byteLength must be 8");return void(this._value=t)}var r=new DataView(this._value);return t instanceof i.NBTTagLong?(r.setInt32(0,t.getHigh()),void r.setInt32(4,t.getLow())):(t instanceof i.NBTNumber&&(t=t.valueOf()),"number"==typeof t?t=o.fromNumber(t):"string"==typeof t&&(t=t.match(/^0[xX][0-9a-fA-F]+$/)?o.fromString(t.substr(2),16):t.match(/^-0[xX][0-9a-fA-F]+$/)?o.fromString("-"+t.substr(3),16):t.match(/^-?0[0-7]+$/)?o.fromString(t.substr(1),!1,8):o.fromString(t,!1,e||10)),void(t instanceof o?(r.setInt32(0,t.high),r.setInt32(4,t.low)):(r.setInt32(0,0),r.setInt32(4,0))))},i.NBTTagLong.prototype.valueOf=function(){return this._long().toNumber()},i.NBTTagLong.prototype.clone=function(){return new i.NBTTagLong(this._value.slice(0),this._name)},i.NBTTagShort=function(t,e){i.NBTNumber.call(this,2,e),t&&this.setValue(t)},r(i.NBTTagShort,i.NBTNumber),i.NBTTagShort.MAX_VALUE=32767,i.NBTTagShort.MIN_VALUE=-32768,i.NBTTagShort.prototype.type=i.Type.TAG_SHORT,i.NBTTagShort.prototype.toString=function(t){return new DataView(this._value).getInt16(0).toString(t)},i.NBTTagShort.prototype.valueOf=function(){return new DataView(this._value).getInt16(0)},i.NBTTagShort.prototype.clone=function(){return new i.NBTTagShort(this._value.slice(0),this._name)},i.NBTTagShort.prototype.setValue=function(t,e){if(t instanceof ArrayBuffer){if(2!=t.byteLength)throw new Error("NBTTagShort: byteLength must be 2");return void(this._value=t)}t instanceof i.NBTNumber&&(t=t.valueOf()),"string"==typeof t&&(t=parseInt(t,e));var r=new DataView(this._value);r.setInt16(0,t)},i.NBTTagString=function(t,e){i.NBTBase.call(this,e),this._value=new ArrayBuffer(0),t&&this.setValue(t)},r(i.NBTTagString,i.NBTBase),i.NBTTagString.prototype.type=i.Type.TAG_STRING,i.NBTTagString.prototype.toString=function(){var t=new Int8Array(this._value);return u.decode(t)},i.NBTTagString.prototype.clone=function(){return new i.NBTTagString(this._value.slice(0),this._name)},i.NBTTagString.prototype.setValue=function(t){if(t instanceof ArrayBuffer)return void(this._value=t);var e=u.encode(t+"");this._value=new ArrayBuffer(e.length);for(var r=new DataView(this._value),n=0;n<e.length;n++)r.setInt8(n,e[n])},i.NBTTagString.prototype._write=function(t){t.writeInt16(this._value.byteLength),t.write(this._value)},a.prototype.write=function(t){this._buffer.push(t)},a.prototype.writeInt8=function(t){var e=new ArrayBuffer(1);new DataView(e).setInt8(0,t),this._buffer.push(e)},a.prototype.writeInt16=function(t){var e=new ArrayBuffer(2);new DataView(e).setInt16(0,t),this._buffer.push(e)},a.prototype.writeInt32=function(t){var e=new ArrayBuffer(4);new DataView(e).setInt32(0,t),this._buffer.push(e)},a.prototype.writeFloat32=function(t){var e=new ArrayBuffer(4);new DataView(e).setFloat32(0,t),this._buffer.push(e)},a.prototype.writeFloat64=function(t){var e=new ArrayBuffer(4);new DataView(e).setFloat64(0,t),this._buffer.push(e)},a.prototype.writeUtf8String=function(t){var e=u.encode(t.toString()),r=new ArrayBuffer(e.length||e.byteLength||0);this.writeInt16(r.byteLength);for(var n=new DataView(r),a=0;a<r.byteLength;a++)n.setInt8(a,e[a]);this._buffer.push(r)},a.prototype.toBlob=function(){return new Blob(this._buffer)},a.prototype.toArrayBuffer=function(){if(0==this._buffer.length)return new ArrayBuffer(0);if(1==this._buffer.length)return this._buffer[0];var t=this._buffer.reduce(function(t,e){return t+e.byteLength||e.length},0),e=new ArrayBuffer(t),r=new DataView(e),n=0;return this._buffer.map(function(t){return new DataView(t)}).forEach(function(t){for(var e=0;e<t.byteLength;e++)r.setInt8(n++,t.getInt8(e))}),this._buffer=[e],e},i.TypeName={0:"end",1:"byte",2:"short",3:"int",4:"long",5:"float",6:"double",7:"byteArray",8:"string",9:"list",10:"compound",11:"intArray"}}(this);