/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/@egret/ts-binary-serializer@1.2.6/dist/BinarySerializer.umd.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t["ts-binary-serializer"]={})}(this,(function(t){"use strict";var e,r=function(t,e,r,i){void 0===r&&(r=!1),this.isArray=!1,this.key=t,this.datatype=e,this.isArray=r,this.pclass=i},i=function(){function t(){this.m_needSort=!0}return Object.defineProperty(t.prototype,"prototype",{get:function(){return this.m_prototype},set:function(t){this.m_prototype=t,this.pname=this.protoName},enumerable:!0,configurable:!0}),t.prototype.addProperty=function(t,e,i){void 0===i&&(i=!1);for(var n=this.properties,o=0,a=n.length;o<a;o++)if(n[o].key==t)return;n.push(new r(t,e,i)),this.m_needSort=!0},Object.defineProperty(t.prototype,"protoName",{get:function(){return this.prototype.constructor.name},enumerable:!0,configurable:!0}),t.prototype.sortProperty=function(){if(this.m_needSort)return this.m_needSort=!1,this},t}();(e=t.DataType||(t.DataType={}))[e.Null=0]="Null",e[e.Float32=1]="Float32",e[e.Float64=2]="Float64",e[e.Int32=3]="Int32",e[e.Int16=4]="Int16",e[e.Int8=5]="Int8",e[e.Uint32=6]="Uint32",e[e.Uint16=7]="Uint16",e[e.Uint8=8]="Uint8",e[e.String=9]="String",e[e.Bool=10]="Bool",e[e.Object=11]="Object",e[e.Float16=12]="Float16",e[e.Map=13]="Map";var n=function(){function e(){}return e.register=function(n,o,a,s,p){void 0===s&&(s=!1);var h=e.getMetaClass(n);null==h&&((h=new i).prototype=n,h.properties=[],e.meta.push(h));var u=new r(o,a,s);if(a==t.DataType.Object||a==t.DataType.Map)if("number"==typeof p){if(p==t.DataType.Null||p==t.DataType.Object||p==t.DataType.Map)throw new Error("invalid pclass for object/map");u.pclass=p}else u.pclass=e.getMetaClass(p.prototype);h.properties.push(u)},e.getMetaClass=function(t){for(var r=e.meta,i=0,n=r.length;i<n;i++){var o=r[i];if(o.prototype===t)return o}return null},e.meta=[],e}(),o=function(){function t(){}return t.ByteToFloat16=function(t){var e=!!(t>>15&1),r=t>>10&31,i=1023&t;if(0==r&&0==i)return e?-0:0;if(31==r)return 0==i?e?-1/0:1/0:NaN;var n=0;return n=0==r?i/512:1+i/1024,(e?-1:1)*Math.pow(2,r-15)*n},t.Float16ToByte=function(t){var e=t;if(isNaN(e))return 31745;if(1/e==-1/0)return 32768;if(0===e)return 0;if(e===-1/0)return 64512;if(e===1/0)return 31744;var r=e<0;e=Math.abs(e);var i=Math.floor(e),n=0,o=0;if(i>0){for(;i>0;)n++,i>>=1;n+=14;var a=e/Math.pow(2,n-15)-1;o=Math.round(1024*a)}else{var s=32768*e;if((i=Math.floor(s))>0){for(;i>0;)n++,i>>=1;n--}if(0==n)o=Math.round(512*s);else{a=s/(1<<n)-1;o=Math.round(1024*a)}}return(n<<10)+o+(r?32768:0)},t}(),a=function(){function e(){this.m_arrayBufferCurrentSize=e.DEFAULT_BUFFER_SIZE,this.m_pos=0}return e.initialize=function(){for(var e in t.DataType)if(isNaN(Number(e))){var r=t.DataType[e];this.WriteFuncMap[r]="write"+e,this.ReadFuncMap[r]="read"+e}},Object.defineProperty(e.prototype,"pos",{get:function(){return this.m_pos},enumerable:!0,configurable:!0}),e.create=function(){var t=new e,r=new Uint8Array(e.DEFAULT_BUFFER_SIZE);return t.m_arrayBuffer=r,t.m_view=new DataView(r.buffer),t},e.prototype.checkBufferExpand=function(t){void 0===t&&(t=8);var e=this.m_arrayBufferCurrentSize;if(this.m_pos+t>=e){for(var r=e+t;e<r;)e<<=1;var i=this.m_arrayBuffer,n=new Uint8Array(e);n.set(i,0),this.m_arrayBuffer=n,this.m_arrayBufferCurrentSize=e,this.m_view=new DataView(n.buffer,0,e)}},e.createWithView=function(t,r,i){var n=new e;return n.m_arrayBuffer=new Uint8Array(t),n.m_view=new DataView(t,r,i),n.m_pos=r,n},e.prototype.pushProperty=function(r,i,n,o){if(void 0===n&&(n=!1),null!=i){this.writeType(r);var a=this[e.WriteFuncMap[r]],s=r==t.DataType.Object,p=r==t.DataType.Map;if(!n)return this.checkBufferExpand(8),void(s?Reflect.apply(a,this,[o]):p?this.writeMap(i,o):Reflect.apply(a,this,[i]));if(!Array.isArray(i))throw new Error("target property: "+i+" is not an array.");var h=i,u=h.length;if(u>65535)throw new Error("array length exceeded.");if(this.checkBufferExpand(8*u+4),this.writeUint16(u),s)for(var f=0;f<u;f++)Reflect.apply(a,this,[h[f],o]);else if(p)for(f=0;f<u;f++)this.writeMap(h[f],o);else for(f=0;f<u;f++)a.call(this,h[f])}else this.writeType(t.DataType.Null)},e.prototype.readProperty=function(r,i,n){void 0===i&&(i=!1);var o=this.readType();if(o==t.DataType.Null)return null;if(o!=r)throw new Error("data type mismatch "+o+" "+r);var a=this[e.ReadFuncMap[r]],s=r==t.DataType.Object,p=r==t.DataType.Map;if(!i){if(s){if(null==n)throw new Error("tmc is null");return a.call(this,n)}if(p){if(null==n)throw new Error("read property tmc missing: "+r);return this.readMap(n)}return a.call(this,null)}var h=this.readUint16();if(0==h)return[];var u=[];if(s)for(var f=0;f<h;f++)u.push(a.call(this,n));else if(p){if(null==n)throw new Error("read property tmc missing: "+r);for(f=0;f<h;f++)u.push(this.readMap(n))}else for(f=0;f<h;f++)u.push(a.call(this,null));return u},e.prototype.writeMap=function(t,r){if(null!=t){var n=Object.getOwnPropertyNames(t);if(n.sort(),null==r)throw new Error("type not found for:"+t);var o=n.length;if(o>65535)throw new Error("map size exceed!");this.writeUint16(o);for(var a=0,s=n.length;a<s;a++){var p=n[a];this.writeString(p);var h=t[p];if(null==h)this.writeBool(!1);else if(this.writeBool(!0),r instanceof i)this.writeObject(h,r);else this[e.WriteFuncMap[r]].call(this,h)}}else this.writeUint16(0)},e.prototype.readMap=function(t){var r=this.readUint16();if(0==r)return null;for(var n={},o=0;o<r;o++){var a=this.readString();if(null==a)throw new Error("key is null");if(this.readBool())if(t instanceof i)n[a]=this.readObject(t);else{var s=this[e.ReadFuncMap[t]];n[a]=s.call(this,null)}else n[a]=null}return n},e.prototype.writeFloat16=function(t){var e=this.m_view,r=this.m_pos,i=o.Float16ToByte(t);e.setUint16(r,i),this.m_pos+=2},e.prototype.readFloat16=function(){var t=this.m_view.getUint16(this.m_pos),e=o.ByteToFloat16(t);return this.m_pos+=2,e},e.prototype.writeFloat32=function(t){var e=this.m_view,r=this.m_pos;e.setFloat32(r,t),this.m_pos+=4},e.prototype.readFloat32=function(){var t=this.m_view.getFloat32(this.m_pos);return this.m_pos+=4,t},e.prototype.writeFloat64=function(t){var e=this.m_view,r=this.m_pos;try{e.setFloat64(r,t)}catch(t){throw console.log(r,this.m_arrayBufferCurrentSize),t}this.m_pos+=8},e.prototype.readFloat64=function(){var t=this.m_view.getFloat64(this.m_pos);return this.m_pos+=8,t},e.prototype.writeInt8=function(t){var e=this.m_view,r=this.m_pos;e.setInt8(r,t),this.m_pos++},e.prototype.readInt8=function(){var t=this.m_view.getInt8(this.m_pos);return this.m_pos+=1,t},e.prototype.writeUint8=function(t){var e=this.m_view,r=this.m_pos;e.setUint8(r,t),this.m_pos++},e.prototype.readUint8=function(){var t=this.m_view.getUint8(this.m_pos);return this.m_pos+=1,t},e.prototype.writeInt16=function(t){var e=this.m_view,r=this.m_pos;e.setInt16(r,t),this.m_pos+=2},e.prototype.readInt16=function(){var t=this.m_view.getInt16(this.m_pos);return this.m_pos+=2,t},e.prototype.writeUint16=function(t){var e=this.m_view,r=this.m_pos;e.setUint16(r,t),this.m_pos+=2},e.prototype.readUint16=function(){var t=this.m_view.getUint16(this.m_pos);return this.m_pos+=2,t},e.prototype.writeInt32=function(t){var e=this.m_view,r=this.m_pos;e.setInt32(r,t),this.m_pos+=4},e.prototype.readInt32=function(){var t=this.m_view.getInt32(this.m_pos);return this.m_pos+=4,t},e.prototype.writeUint32=function(t){var e=this.m_view,r=this.m_pos;e.setUint32(r,t),this.m_pos+=4},e.prototype.readUint32=function(){var t=this.m_view.getUint32(this.m_pos);return this.m_pos+=4,t},e.prototype.writeBool=function(t){var e=this.m_view,r=this.m_pos;e.setUint8(r,t?1:0),this.m_pos++},e.prototype.readBool=function(){var t=this.m_view.getUint8(this.m_pos);return this.m_pos++,1==t},e.prototype.writeString=function(t){this.writeUTF8StrFast(t)},e.prototype.readString=function(){return this.readUTF8StrFast()},e.prototype.readUTF8StrFast=function(){var t=this.readInt32();if(-1==t)return null;for(var e=new Array(t),r=this.m_pos,i=this.m_arrayBuffer,n=0;n<t;n++)e[n]=i[r++];var o=String.fromCharCode.apply(String,e);return this.m_pos=r,decodeURIComponent(escape(o))},e.prototype.writeUTF8StrFast=function(t){if(null!=t){var e=unescape(encodeURIComponent(t)),r=e.length;this.writeInt32(r),this.checkBufferExpand(r);for(var i=this.m_view,n=this.m_pos,o=0;o<r;o++)i.setUint8(n++,e.charCodeAt(o));this.m_pos=n}else this.writeInt32(-1)},e.prototype.writeUTF8Str=function(t){if(null!=t){var e=t.length;this.writeInt32(e),this.checkBufferExpand(4*e);for(var r=this.m_view,i=this.m_pos,n=0;n<e;n++){var o=t.charCodeAt(n);o<128?r.setUint8(i++,o):o<2048?(r.setUint8(i++,o>>6|192),r.setUint8(i++,128|63&o)):o<65536?(r.setUint8(i++,224|o>>12),r.setUint8(i++,128|o>>6&63),r.setUint8(i++,128|63&o)):(r.setUint8(i++,240|o>>18),r.setUint8(i++,128|o>>12&63),r.setUint8(i++,128|o>>6&63),r.setUint8(i++,128|63&o))}this.m_pos=i}else this.writeInt32(-1)},e.prototype.readUTF8Str=function(){var t=this.readInt32();if(-1==t)return null;for(var e=new Array(t),r=0;r<t;r++){var i=this.readUint8();if(i>>7)if(i>>5==6){var n=this.readUint8();e[r]=(31&i)<<6|63&n}else if(i>>4==14){n=this.readUint8();var o=this.readUint8();e[r]=(15&i)<<12|(63&n)<<6|63&o}else{n=this.readUint8(),o=this.readUint8();var a=this.readUint8();e[r]=(7&i)<<18|(63&n)<<12|(63&o)<<6|63&a}else e[r]=i}return String.fromCharCode.apply(String,e)},e.prototype.writeType=function(t){this.writeUint8(t)},e.prototype.readType=function(){return this.readUint8()},e.prototype.writeObject=function(t,e){this.serialize(e,t)},e.prototype.readObject=function(t){var e=Object.create(t.prototype);return this.deserialize(e,t),e},e.prototype.serialize=function(t,e){for(var r=t.properties,i=0,n=r.length;i<n;i++){var o=r[i];this.pushProperty(o.datatype,e[o.key],o.isArray,o.pclass)}},e.prototype.deserialize=function(t,e){if(null==e)throw new Error("typeMetaClass is null");e.sortProperty();for(var r=e.properties,i=0,n=r.length;i<n;i++){var o=r[i],a=this.readProperty(o.datatype,o.isArray,o.pclass);t[o.key]=a}return t},e.DEFAULT_BUFFER_SIZE=512,e.WriteFuncMap={},e.ReadFuncMap={},e}();a.initialize();var s=function(){this.includeEntryInfo=!1};t.BinarySerializeOptions=s,t.BinarySerialize=function(t,e,r){var i=Object.getPrototypeOf(t);if(null==i||"Object"==i.constructor.name){if(null==e)throw new Error("param type is required.");i=e.prototype}var o=n.getMetaClass(i);if(null==o){var s="reflect class: "+i.name+" invalid";throw new Error(s)}o.sortProperty();var p=a.create();return p.serialize(o,t),p.m_arrayBuffer.buffer.slice(0,p.pos)},t.BinaryDeserialize=function(t,e){var r=Object.create(t.prototype),i=n.getMetaClass(t.prototype);if(null==i){var o="reflect class "+t.prototype+" invalid.";throw new Error(o)}return i.sortProperty(),a.createWithView(e,0,e.byteLength).deserialize(r,i)},t.SerializeField=function(t,e,r){return void 0===e&&(e=!1),function(i,o){n.register(i,o,t,e,r)}},t.TypeReflector=n,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=/sm/4d32091a3851423994b22a9a51ad4230549d1e2221cd725cf171f872576d11ad.map