Widget:Template decoder/templates.js
From Guild Wars Wiki 中文站
Jump to navigationJump to search
/*
* buildwars/gw-templates
* @copyright 2024 smiley
* @license MIT
* @link https://github.com/build-wars/gw-templates
*/
function t(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=Array(e);n<e;n++)r[n]=t[n];return r}function e(t,e,n){if('function'==typeof t?t===e:t.has(e))return arguments.length<3?e:n;throw new TypeError('Private element is not present on this object')}function n(t,e,n){return e=d(e),function(t,e){if(e&&('object'==typeof e||'function'==typeof e))return e;if(void 0!==e)throw new TypeError('Derived constructors may only return object or undefined');return function(t){if(void 0===t)throw new ReferenceError('this hasn\'t been initialised - super() hasn\'t been called');return t}(t)}(t,v()?Reflect.construct(e,n||[],d(t).constructor):e.apply(t,n))}function r(t,e){if(e.has(t))throw new TypeError('Cannot initialize the same private elements twice on an object')}function i(t,e){if(!(t instanceof e))throw new TypeError('Cannot call a class as a function')}function o(t,n){return t.get(e(t,n))}function a(t,e,n){r(t,e),e.set(t,n)}function u(t,n,r){return t.set(e(t,n),r),r}function s(t,e){r(t,e),e.add(t)}function l(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,'value'in r&&(r.writable=!0),Object.defineProperty(t,b(r.key),r)}}function c(t,e,n){return e&&l(t.prototype,e),n&&l(t,n),Object.defineProperty(t,'prototype',{writable:!1}),t}function f(t,e){var n='undefined'!=typeof Symbol&&t[Symbol.iterator]||t['@@iterator'];if(!n){if(Array.isArray(t)||(n=E(t))||e){n&&(t=n);var r=0,i=function(){};return{s:i,n:function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:i}}throw new TypeError('Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.')}var o,a=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return a=t.done,t},e:function(t){u=!0,o=t},f:function(){try{a||null==n.return||n.return()}finally{if(u)throw o}}}}function h(t,e,n){return(e=b(e))in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function d(t){return d=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},d(t)}function p(t,e){if('function'!=typeof e&&null!==e)throw new TypeError('Super expression must either be null or a function');t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,'prototype',{writable:!1}),e&&_(t,e)}function v(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(v=function(){return!!t})()}function _(t,e){return _=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},_(t,e)}function y(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=null==t?null:'undefined'!=typeof Symbol&&t[Symbol.iterator]||t['@@iterator'];if(null!=n){var r,i,o,a,u=[],s=!0,l=!1;try{if(o=(n=n.call(t)).next,0===e);else for(;!(s=(r=o.call(n)).done)&&(u.push(r.value),u.length!==e);s=!0);}catch(t){l=!0,i=t}finally{try{if(!s&&null!=n.return&&(a=n.return(),Object(a)!==a))return}finally{if(l)throw i}}return u}}(t,e)||E(t,e)||function(){throw new TypeError('Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.')}()}function b(t){var e=function(t,e){if('object'!=typeof t||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e);if('object'!=typeof r)return r;throw new TypeError('@@toPrimitive must return a primitive value.')}return String(t)}(t,'string');return'symbol'==typeof e?e:e+''}function g(t){return g='function'==typeof Symbol&&'symbol'==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&'function'==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?'symbol':typeof t},g(t)}function E(e,n){if(e){if('string'==typeof e)return t(e,n);var r={}.toString.call(e).slice(8,-1);return'Object'===r&&e.constructor&&(r=e.constructor.name),'Map'===r||'Set'===r?Array.from(e):'Arguments'===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?t(e,n):void 0}}function m(){m=function(t,e){return new n(t,void 0,e)};var t=RegExp.prototype,e=new WeakMap;function n(t,r,i){var o=RegExp(t,r);return e.set(o,i||e.get(t)),_(o,n.prototype)}function r(t,n){var r=e.get(n);return Object.keys(r).reduce(function(e,n){var i=r[n];if('number'==typeof i)e[n]=t[i];else{for(var o=0;void 0===t[i[o]]&&o+1<i.length;)o++;e[n]=t[i[o]]}return e},Object.create(null))}return p(n,RegExp),n.prototype.exec=function(e){var n=t.exec.call(this,e);if(n){n.groups=r(n,this);var i=n.indices;i&&(i.groups=r(i,this))}return n},n.prototype[Symbol.replace]=function(n,i){if('string'==typeof i){var o=e.get(this);return t[Symbol.replace].call(this,n,i.replace(/\$<([^>]+)(>|$)/g,function(t,e,n){if(''===n)return t;var r=o[e];return Array.isArray(r)?'$'+r.join('$'):'number'==typeof r?'$'+r:''}))}if('function'==typeof i){var a=this;return t[Symbol.replace].call(this,n,function(){var t=arguments;return'object'!=typeof t[t.length-1]&&(t=[].slice.call(t)).push(r(t,a)),i.apply(this,t)})}return t[Symbol.replace].call(this,n,i)},m.apply(this,arguments)}var w=function(){return c(function t(){i(this,t)},null,[{key:'array_fill',value:function(t,e){for(var n=[],r=0;r<t;r++)n[r]=structuredClone(e);return n}},{key:'array_sum',value:function(t){var e=0;if(null===t)return 0;'object'!==g(t)&&(t={});for(var n=0,r=Object.values(t);n<r.length;n++){var i=r[n],o=i;'boolean'==typeof i?o=i?1:0:null==i?o=0:'number'!=typeof i&&(o=parseFloat(String(i))),isNaN(o)||(e+=o)}return e}},{key:'intval',value:function(t){var e,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,r=g(t);return'boolean'===r?+t:'string'===r?(e=parseInt(t,n||10),isNaN(e)||!isFinite(e)?0:e):'number'===r&&isFinite(t)?0|t:0}},{key:'str_split',value:function(t,e){var n=g(t);if('number'===n||'bigint'===n)t+='';else if('string'!==n)throw new Error('invalid string');if(void 0===e&&(e=1),'number'!=typeof e||e<1)throw new Error('invalid split length');for(var r=[],i=0;i<t.length;)r.push(t.slice(i,i+=e));return r}}])}(),k=function(){return c(function t(){i(this,t),h(this,'_offset',0),h(this,'_string','')},[{key:'TEMPLATE_SKILL_OLD',get:function(){return 0}},{key:'TEMPLATE_SKILL_NEW',get:function(){return 14}},{key:'TEMPLATE_EQUIPMENT_OLD',get:function(){return 1}},{key:'TEMPLATE_EQUIPMENT_NEW',get:function(){return 15}},{key:'decode',value:function(t){throw new Error('abstract nethod')}},{key:'_bindec_flip',value:function(t){return w.intval(t.split('').reverse().join(''),2)}},{key:'_decbin_flip',value:function(t){return(t>>>0).toString(2).split('').reverse().join('')}},{key:'_decbin_pad',value:function(t,e){return this._decbin_flip(t).padEnd(e,'0')}},{key:'_checkCharacterSet',value:function(t){if(''===(t=t.trim().replaceAll(' ','+').replaceAll('=','')))return'';if(null===t.match(/^[A-Za-z0-9\+\/]*$/))throw new Error('Base64 must match RFC3548 character set');return t}},{key:'_getPadSize',value:function(t,e){var n,r=f(t);try{for(r.s();!(n=r.n()).done;){var i=n.value;w.intval(i)>=Math.pow(2,e)&&e++}}catch(t){r.e(t)}finally{r.f()}return e}},{key:'_base64decode',value:function(t){for(t=this._checkCharacterSet(t);t.length%8!=0;)t+='A';return atob(t)}},{key:'_base64encode',value:function(t){return(t instanceof Uint8Array?t.toBase64():btoa(t)).replaceAll('=','')}},{key:'_read',value:function(t){return this._bindec_flip(this._string.substring(this._offset,this._offset+=t))}},{key:'_decodeTemplate',value:function(t){if(''===t)throw new Error('invalid base64 template');var e=this._base64decode(t),n=this._decodeBinaryToBase2(e);switch(this._bindec_flip(n.substring(0,4))){case this.TEMPLATE_SKILL_NEW:case this.TEMPLATE_EQUIPMENT_NEW:return n.substring(8);case this.TEMPLATE_SKILL_OLD:case this.TEMPLATE_EQUIPMENT_OLD:return n.substring(4)}throw new Error('invalid template')}},{key:'_encodeTemplate',value:function(t){if(''===t)throw new Error('invalid binary template');var e=this._encodeBase2ToBinary(t);return this._base64encode(e)}},{key:'_decodeBinaryToBase2',value:function(t){var e=t.split('').map(function(t){return t.charCodeAt(0)}).map(function(t){return t.toString(2).padStart(8,'0')}).join('');return w.str_split(e,6).map(function(t){return t.split('').reverse().join('')}).join('')}},{key:'_encodeBase2ToBinary',value:function(t){for(;t.length%8!=0||t.length%6!=0;)t+='0';var e=w.str_split(t,6).map(function(t){return t.split('').reverse().join('')});return w.str_split(e.join(''),8).map(function(t){return w.intval(t,2)}).map(function(t){return String.fromCharCode(t)}).join('')}}],[{key:'fromTemplate',value:function(t){throw new Error('not dealing with this shit here')}},{key:'fromChatCode',value:function(t){throw new Error('not dealing with this shit here')}}])}(),N=new WeakMap,S=new WeakMap,A=new WeakMap,C=new WeakSet,O=function(){function t(){var e;i(this,t);for(var r=arguments.length,o=new Array(r),u=0;u<r;u++)o[u]=arguments[u];return s(e=n(this,t,[].concat(o)),C),a(e,N,{1:5,2:5,3:5,4:5,5:5,6:5,8:5,9:5,10:5,11:5,12:5,13:5,14:5,15:5,16:5,17:5,18:5,19:5,20:5,21:5,22:2,23:2,24:2,25:2,26:2,27:2,28:2,29:2,30:2,31:2,32:2,33:2,34:2,35:2,36:2,37:2,38:2,39:2,40:2,41:2,42:6,43:6,44:6,45:6,46:6,47:6,48:6,49:6,50:6,51:6,52:6,53:6,54:6,55:6,56:6,57:6,59:6,60:6,61:6,62:6,63:4,64:4,65:4,66:4,67:4,68:4,69:4,70:4,71:4,72:4,73:4,74:4,75:4,76:4,77:4,78:4,79:4,80:4,81:4,83:4,84:4,85:4,86:4,87:4,88:4,89:4,90:3,91:3,92:3,93:3,94:3,95:3,96:3,97:3,98:3,99:3,100:3,101:3,102:3,103:3,104:3,105:3,106:3,107:3,108:3,109:3,110:0,111:0,112:0,113:0,114:0,115:0,116:1,117:1,118:1,119:1,120:1,121:1,122:1,123:1,124:1,125:1,126:1,127:1,128:1,129:1,130:1,131:1,132:1,133:0,134:0,135:0,136:0,137:0,138:0,139:0,140:0,141:0,142:0,143:0,144:0,145:1,146:1,147:0,148:0,149:0,150:0,151:0,152:0,153:0,154:0,155:0,156:0,157:0,158:0,159:5,160:2,161:6,162:3,163:5,164:2,165:6,166:3,167:5,168:2,169:6,170:3,171:5,172:2,173:6,174:3,175:5,176:2,177:6,178:3,179:5,180:2,181:6,182:3,183:5,184:2,185:6,186:3,187:5,188:2,189:6,190:3,191:5,192:2,193:6,194:3,195:5,196:2,197:6,198:3,199:5,200:2,201:6,202:3,203:5,204:2,205:6,206:3,207:5,208:2,209:6,210:3,211:5,212:2,213:6,214:3,215:5,216:2,217:6,218:3,219:5,220:2,221:6,222:3,223:5,224:2,225:6,226:3,227:5,228:2,229:6,230:3,231:5,232:2,233:6,234:3,235:5,236:2,237:6,238:3,239:5,240:2,241:6,242:3,243:5,244:2,245:6,246:3,247:5,248:2,249:6,250:3,251:5,252:2,253:6,254:3,255:5,256:2,257:6,258:3,259:5,260:2,261:6,262:3,263:5,264:2,265:6,266:3,267:5,268:2,269:6,270:3,271:4,272:4,273:4,274:4,275:4,276:4,277:4,278:4,279:0,280:1,281:1,282:1,283:1,284:0,285:0,286:0,287:0,288:0,289:0,290:4,291:4,292:4,293:4,294:5,295:2,296:6,297:3,298:5,299:2,300:6,301:3,302:5,303:2,304:6,305:3,306:4,307:4,308:4,309:4,310:5,311:2,312:6,313:3,314:5,315:2,316:6,317:3,318:5,319:2,320:6,321:3,322:0,323:1,324:1,325:0,326:0,327:0,328:0,329:0,330:0,331:0,332:0,333:0,334:0,335:0,336:0,337:0,338:0,339:0}),a(e,S,{0:'default',2:'blue',3:'green',4:'purple',5:'red',6:'yellow',7:'brown',8:'orange',9:'grey'}),a(e,A,{}),e}return p(t,k),c(t,[{key:'decode',value:function(t){this._string=this._decodeTemplate(t),this._offset=0,u(A,this,{});for(var e=this._read(4),n=this._read(4),r=this._read(3),i=0;i<r;i++){for(var a=this._read(3),s=this._read(e),l=this._read(2),c=this._read(4),f=[],h=0;h<l;h++)f.push(this._read(n));o(A,this)[String(a)]={id:s,slot:a,color:c,mods:f}}return o(A,this)}},{key:'encode',value:function(){var t=this._decbin_pad(this.TEMPLATE_EQUIPMENT_NEW,4);t+=this._decbin_pad(0,4);var e=[],n=[];for(var r in o(A,this))e.push(o(A,this)[r].id),n=n.concat(o(A,this)[r].mods);var i=this._getPadSize(e,8),a=this._getPadSize(n,8);for(var u in t+=this._decbin_pad(i,4),t+=this._decbin_pad(a,4),t+=this._decbin_pad(Object.keys(o(A,this)).length,3),o(A,this)){t+=this._decbin_pad(o(A,this)[u].slot,3),t+=this._decbin_pad(o(A,this)[u].id,i),t+=this._decbin_pad(o(A,this)[u].mods.length,2),t+=this._decbin_pad(o(A,this)[u].color,4);var s,l=f(o(A,this)[u].mods);try{for(l.s();!(s=l.n()).done;){var c=s.value;t+=this._decbin_pad(c,a)}}catch(t){l.e(t)}finally{l.f()}}return this._encodeTemplate(t)}},{key:'addItem',value:function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[];if(void 0===o(N,this)[String(t)])throw new Error('invalid item id');if(void 0===o(S,this)[String(n)])throw new Error('invalid color id');var i=o(N,this)[String(t)];return o(A,this)[String(i)]={id:t,slot:i,color:n,mods:e(C,this,T).call(this,r)},this}},{key:'clearItems',value:function(){return u(A,this,{}),this}}],[{key:'fromTemplate',value:function(e){return(new t).decode(e)}},{key:'fromChatCode',value:function(e){var n=e.trim().match(m(/^\[([^;]*);([A-Za-z0-9\+\/ ]+)\]$/,{name:1,code:2}));if(null===n||!n.groups)throw new Error('invalid chat code');return t.fromTemplate(n.groups.code)}}])}();function T(t){var e,n=[],r=f(t);try{for(r.s();!(e=r.n()).done;){var i=e.value;'number'==typeof i&&((i=w.intval(i))<1||i>=512||n.push(i))}}catch(t){r.e(t)}finally{r.f()}return n}const I=new Map([[129,1],[141,13],[143,15],[144,16],[157,29],[160,32],[161,33],[162,34],[163,35],[164,36],[165,37],[166,38],[167,39],[168,40],[169,41],[170,42],[171,43],[172,44],[173,45],[174,46],[175,47],[176,48],[177,49],[178,50],[179,51],[180,52],[181,53],[182,54],[183,55],[184,56],[185,57],[186,58],[187,59],[188,60],[189,61],[190,62],[191,63],[192,64],[193,65],[194,66],[195,67],[196,68],[197,69],[198,70],[199,71],[200,72],[201,73],[202,74],[203,75],[204,76],[205,77],[206,78],[207,79],[208,80],[209,81],[210,82],[211,83],[212,84],[213,85],[214,86],[215,87],[216,88],[217,89],[218,90],[219,91],[220,92],[221,93],[222,94],[223,95],[224,96],[225,97],[226,98],[227,99],[228,100],[229,101],[230,102],[231,103],[232,104],[233,105],[234,106],[235,107],[236,108],[237,109],[238,110],[239,111],[240,112],[241,113],[242,114],[243,115],[244,116],[245,117],[246,118],[247,119],[248,120],[249,121],[250,122],[251,123],[252,124],[253,125],[254,126],[255,127],[338,12],[339,28],[352,10],[353,26],[376,31],[381,14],[382,30],[402,3],[710,8],[732,24],[8211,22],[8212,23],[8216,17],[8217,18],[8218,2],[8220,19],[8221,20],[8222,4],[8224,6],[8225,7],[8226,21],[8230,5],[8240,9],[8249,11],[8250,27],[8364,0],[8482,25]]),P=t=>{if('replacement'===t)return 65533;throw new Error};var D,j=new WeakMap,M=new WeakMap,L=new WeakMap,W=new WeakMap,R=new WeakMap,U=new WeakSet,F=function(){function t(){var e,r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t.PAWNED_CHARSET_UTF8;return i(this,t),s(e=n(this,t),U),a(e,j,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'),a(e,M,'pwnd'),a(e,L,[]),a(e,W,void 0),a(e,R,void 0),u(W,e,t.getEncoding(r)),u(R,e,r),e}return p(t,k),c(t,[{key:'decode',value:function(n){var r=t.parseHeader(n),i=t.getEncoding(r[3]),o=(n=n.trim().replace(/[\r\n]/g,'')).indexOf('>'),a=n.indexOf('<',o);if(-1===o||-1===a||a<=o)throw new Error('invalid paw·ned² template');this._string=this._checkCharacterSet(n.substring(o+1,a)),this._offset=0;for(var u=[];this._offset<this._string.length;){for(var s=e(U,this,V).call(this),l=e(U,this,V).call(this),c=[],f=0;f<3;f++)c[f]=e(U,this,V).call(this);var h=e(U,this,V).call(this),d=e(U,this,V).call(this),p=e(U,this,V).call(this,!0),v=y(e(U,this,z).call(this,p,i),2),_=v[0],b=v[1],g=y(e(U,this,K).call(this,h),2),E=g[0],m=g[1];u.push({skills:s,equipment:l,weaponsets:c,templatename:_,description:b,player:e(U,this,B).call(this,d,i),attributes:E,flags:m})}return u}},{key:'encode',value:function(){var n,r='>',i=f(o(L,this));try{for(i.s();!(n=i.n()).done;){var a=n.value;r+=e(U,this,J).call(this,a.skills),r+=e(U,this,J).call(this,a.equipment);var u,s=f(a.weaponsets);try{for(s.s();!(u=s.n()).done;){var l=u.value;r+=e(U,this,J).call(this,l)}}catch(t){s.e(t)}finally{s.f()}r+=e(U,this,J).call(this,a.flags),r+=e(U,this,J).call(this,a.player),r+=e(U,this,J).call(this,a.description,!0)}}catch(t){i.e(t)}finally{i.f()}return r+='<',r=w.str_split(r,80).join('\n'),''.concat(o(M,this)).concat(e(U,this,X).call(this),'?').concat(t.PWND_HEADER_COMMENT,'\n').concat(r)}},{key:'addBuild',value:function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:'',r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[],i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:'',a=arguments.length>4&&void 0!==arguments[4]?arguments[4]:'',u=arguments.length>5&&void 0!==arguments[5]?arguments[5]:'',s=arguments.length>6&&void 0!==arguments[6]?arguments[6]:[],l=arguments.length>7&&void 0!==arguments[7]?arguments[7]:[];return o(L,this).push({skills:this._checkCharacterSet(t),equipment:this._checkCharacterSet(n),weaponsets:e(U,this,G).call(this,r),flags:e(U,this,$).call(this,s,l),player:e(U,this,x).call(this,u,o(W,this)),description:e(U,this,H).call(this,i,a,o(W,this))}),this}},{key:'clearBuilds',value:function(){return u(L,this,[]),this}},{key:'_read',value:function(t){return e(U,this,q).call(this,this._string.substring(this._offset,this._offset+=t))}}],[{key:'PWND_HEADER_COMMENT',get:function(){return'pwnd-encoder by @codemasher: https://github.com/build-wars/gw-templates'}},{key:'PAWNED_CHARSET_UNDEFINED',get:function(){return 0}},{key:'PAWNED_CHARSET_WINDOWS1252',get:function(){return 1}},{key:'PAWNED_CHARSET_UTF8',get:function(){return 2}},{key:'ENCODING_UNDEFINED',get:function(){return'undefined'}},{key:'ENCODING_ASCII',get:function(){return'ascii'}},{key:'ENCODING_WINDOWS1252',get:function(){return'windows-1252'}},{key:'ENCODING_UTF8',get:function(){return'utf-8'}},{key:'CHARSETS',get:function(){return[t.ENCODING_UNDEFINED,t.ENCODING_WINDOWS1252,t.ENCODING_UTF8]}},{key:'CON_LUNAR_FORTUNE',get:function(){return 0}},{key:'CON_CANDY_CORN',get:function(){return 1}},{key:'CON_GOLDEN_EGG',get:function(){return 2}},{key:'CON_BDAY_CUPCAKE',get:function(){return 3}},{key:'CON_PUMPKIN_PIE',get:function(){return 4}},{key:'CON_CANDY_APPLE',get:function(){return 5}},{key:'CON_WAR_SUPPLIES',get:function(){return 6}},{key:'CON_DRAKE_KABOB',get:function(){return 7}},{key:'CON_SKALEFIN_SOUP',get:function(){return 8}},{key:'CON_PAHNAI_SALAD',get:function(){return 9}},{key:'CON_GREEN_CANDY',get:function(){return 10}},{key:'CON_BLUE_CANDY',get:function(){return 11}},{key:'CON_RED_CANDY',get:function(){return 12}},{key:'CON_ESSENCE_OF_CELERITY',get:function(){return 13}},{key:'CON_ARMOR_OF_SALVATION',get:function(){return 14}},{key:'CON_GRAIL_OF_MIGHT',get:function(){return 15}},{key:'FLAGS',get:function(){return['buLunarFortune','buCandyCorn','buGoldenEgg','buBirthdayCupcake','buSliceOfPumpkinPie','buCandyApple','buWarSupplies','buDrakeKabob','buBowlOfSkalefinSoup','buPahnaiSalad','buGreenRockCandy','buBlueRockCandy','buRedRockCandy','buEssenceOfCelerity','buArmorOfSalvation','buGrailOfMight']}},{key:'fromTemplate',value:function(e){return(new t).decode(e)}},{key:'fromChatCode',value:function(t){throw new Error('not supported')}},{key:'getEncoding',value:function(e){if(!t.CHARSETS[e])throw new Error('invalid encoding flag');return t.CHARSETS[e]}},{key:'parseHeader',value:function(t){var e=t.substring(0,8);if('pwnd'!==e.substring(0,4))throw new Error('invalid paw-ned² template');var n=e.substring(4,8).split('').map(w.intval);if(0!==n[0])throw new Error('paw-ned² breaking change detected');return n}}])}();function G(t){var e,n=['','',''],r=0,i=f(t);try{for(i.s();!(e=i.n()).done;){var o=e.value;if(r>2)break;if('string'==typeof o&&''!==(o=o.trim())){try{o=this._checkCharacterSet(o)}catch(t){continue}n[r]=o,r++}}}catch(t){i.e(t)}finally{i.f()}return n}function B(t,n){var r=this._base64decode(t);return e(U,this,Y).call(this,r,n)}function x(t,n){if(''===(t=t.trim()))return'';var r=e(U,this,Q).call(this,t,n);if(o(R,this)!==D.PAWNED_CHARSET_UTF8&&r.length>32)throw new Error('player name cannot be longer than 32 bytes');if(r.length>48)throw new Error('player name cannot be longer than 48 bytes in UTF-8 mode');return this._base64encode(r)}function z(t,n){var r=this._base64decode(t);return r.length<=1?['','']:(r=e(U,this,Y).call(this,r,n)).includes('\n')?r.split('\n',2):[r,'']}function H(t,n,r){if(t+n==='')return'Cg';var i=e(U,this,Q).call(this,t+'\n'+n,r);if(i.length>258)throw new Error('template name and description combined cannot be longer than 255 bytes');return this._base64encode(i)}function K(t){if(''===t)return[w.array_fill(5,0),w.array_fill(D.FLAGS.length,!1)];for(var n=[],r='',i=0;i<6;i++){var o,a=e(U,this,q).call(this,null!==(o=t[i])&&void 0!==o?o:'A');i<3?(n.push((56&a)>>3),n.push(7&a)):r+=this._decbin_pad(a,6)}n.splice(5,1);var u,s=r.split(''),l=[],c=f(D.FLAGS.keys());try{for(c.s();!(u=c.n()).done;){var h=u.value;l[h]='1'===s[h]}}catch(t){c.e(t)}finally{c.f()}return[n,l]}function $(t,n){t=Object.values(t),n=Object.values(n);var r=w.array_sum(n);if(0===w.array_sum(t)&&0===r)return'';for(;t.length<6;)t.push(0);for(var i='',o=0;o<6;o+=2)i+=e(U,this,Z).call(this,t[o]<<3|t[o+1]);if(r>0){for(var a='',u=0,s=Object.keys(D.FLAGS);u<s.length;u++){a+=n[s[u]]?1:0}var l=this._encodeBase2ToBinary(a);i+=this._base64encode(l)}return i.replace(/[A]+$/,'')}function Y(t,e){e===D.ENCODING_UNDEFINED&&(e=D.ENCODING_ASCII);var n=t.replaceAll('\0','').split('').map(function(t){return t.charCodeAt(0)});return new TextDecoder(e,{fatal:!0}).decode(new Uint8Array(n)).trim()}function Q(t,e){if(''===t)return new Uint8Array([]);if(e===D.ENCODING_UTF8)return(new TextEncoder).encode(t);if([D.ENCODING_UNDEFINED,D.ENCODING_ASCII,D.ENCODING_WINDOWS1252].includes(e))return new Uint8Array((t=>{let e;'fatal'!==e&&'replacement'!==e&&(e='fatal');const n=t.length,r=new Uint16Array(n);for(let i=0;i<n;i++){const n=t.charCodeAt(i);if(0<=n&&n<=127)r[i]=n;else if(I.has(n)){const t=I.get(n);r[i]=t+128}else r[i]=P(e)}return r})(t));throw new Error('invalid $to_encoding')}function q(t){var e=o(j,this).indexOf(t);if(-1===e)throw new Error('invalid base64 character');return e}function Z(t){if(t<0||t>63)throw new Error('invalid base64 ordinal');return o(j,this).substring(t,t+1)}function V(){var t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],e=this._read(1);return t&&(e*=64,e+=this._read(1)),this._string.substring(this._offset,this._offset+=e)}function J(t){if(arguments.length>1&&void 0!==arguments[1]&&arguments[1]){var n=e(U,this,Z).call(this,Math.floor(t.length/64));return(n+=e(U,this,Z).call(this,t.length%64))+t}return e(U,this,Z).call(this,t.length)+t}function X(){return[0,0,0,o(R,this)].join('')}D=F;var tt=new WeakMap,et=new WeakMap,nt=function(){function t(){var e;i(this,t);for(var r=arguments.length,o=new Array(r),u=0;u<r;u++)o[u]=arguments[u];return a(e=n(this,t,[].concat(o)),tt,{1:17,2:23,3:16,4:6,5:0,6:12,7:35,8:36,9:40,10:44}),a(e,et,{0:5,1:5,2:5,3:5,4:4,5:4,6:4,7:4,8:6,9:6,10:6,11:6,12:6,13:3,14:3,15:3,16:3,17:1,18:1,19:1,20:1,21:1,22:2,23:2,24:2,25:2,29:7,30:7,31:7,32:8,33:8,34:8,35:7,36:8,37:9,38:9,39:9,40:9,41:10,42:10,43:10,44:10}),e}return p(t,k),c(t,[{key:'decode',value:function(t){var e=this;this._string=this._decodeTemplate(t),this._offset=0,this._read(2);for(var n=this._read(4),r=this._read(4),i=this._read(4),o=this._read(4)+4,a={},u=0;u<i;u++)a[this._read(o)]=this._read(4);var s=this._read(4)+8;return{code:t,prof_pri:n,prof_sec:r,attributes:a,skills:[0,0,0,0,0,0,0,0].map(function(){return e._read(s)})}}},{key:'encode',value:function(t,e,n,r){var i=y(this.normalizeProfessions(t,e),2);t=i[0],e=i[1],n=this.normalizeAttributes(n,t,e),r=this.normalizeSkills(r);var o=this._decbin_pad(this.TEMPLATE_SKILL_NEW,4);o+=this._decbin_pad(0,4),o+=this._decbin_pad(0,2),o+=this._decbin_pad(t,4),o+=this._decbin_pad(e,4);var a=Object.keys(n);o+=this._decbin_pad(a.length,4);var u=this._getPadSize(a,5);for(var s in o+=this._decbin_pad(u-4,4),n)o+=this._decbin_pad(w.intval(s),u),o+=this._decbin_pad(n[s],4);var l=this._getPadSize(r,10);o+=this._decbin_pad(l-8,4);var c,h=f(r);try{for(h.s();!(c=h.n()).done;){var d=c.value;o+=this._decbin_pad(d,l)}}catch(t){h.e(t)}finally{h.f()}return this._encodeTemplate(o)}},{key:'normalizeProfessions',value:function(t,e){return void 0===o(tt,this)[String(t)]&&(t=0),void 0!==o(tt,this)[String(e)]&&e!==t||(e=0),[t,e]}},{key:'normalizeAttributes',value:function(t,e,n){var r={};for(var i in t)if(void 0!==o(et,this)[i]){var a=o(et,this)[i];a!==e&&a!==n||void 0!==o(tt,this)[String(n)]&&a===o(tt,this)[String(n)]||(r[i]=Math.max(0,Math.min(t[i],12)))}return r}},{key:'normalizeSkills',value:function(t){var e,n=[0,0,0,0,0,0,0,0],r=0,i=f(t);try{for(i.s();!(e=i.n()).done;){var o=e.value;if(r>7)break;'number'==typeof o&&((o=w.intval(o))>0&&o<4095&&(n[r]=o),r++)}}catch(t){i.e(t)}finally{i.f()}return n}}],[{key:'fromTemplate',value:function(e){return(new t).decode(e)}},{key:'fromChatCode',value:function(e){var n=e.trim().match(m(/^\[([^;]*);([A-Za-z0-9\+\/ ]+)\]$/,{name:1,code:2}));if(null===n||!n.groups)throw new Error('invalid chat code');return t.fromTemplate(n.groups.code)}}])}();export{O as EquipmentTemplate,F as PwndTemplate,nt as SkillTemplate};
//# sourceMappingURL=gw-templates-es6.js.map