@drincs/pixi-vn-json
Version:
Pixi'VN can be integrated with JSON files to create a visual novel.
1 lines • 20.2 kB
JavaScript
;var narration=require('@drincs/pixi-vn/narration'),storage=require('@drincs/pixi-vn/storage');var Y=Object.create;var B=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var Z=Object.getOwnPropertyNames;var U=Object.getPrototypeOf,ee=Object.prototype.hasOwnProperty;var ie=(a,e,i)=>e in a?B(a,e,{enumerable:true,configurable:true,writable:true,value:i}):a[e]=i;var R=(a=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(a,{get:(e,i)=>(typeof require<"u"?require:e)[i]}):a)(function(a){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+a+'" is not supported')});var K=(a,e)=>()=>(e||a((e={exports:{}}).exports,e),e.exports);var te=(a,e,i,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of Z(e))!ee.call(a,n)&&n!==i&&B(a,n,{get:()=>e[n],enumerable:!(o=Q(e,n))||o.enumerable});return a};var ne=(a,e,i)=>(i=a!=null?Y(U(a)):{},te(B(i,"default",{value:a,enumerable:true}),a));var N=(a,e,i)=>ie(a,typeof e!="symbol"?e+"":e,i);var M=K((L,G)=>{(function(a,e){typeof L=="object"?G.exports=L=e():typeof define=="function"&&define.amd?define([],e):a.CryptoJS=e();})(L,function(){var a=a||(function(e,i){var o;if(typeof window<"u"&&window.crypto&&(o=window.crypto),typeof self<"u"&&self.crypto&&(o=self.crypto),typeof globalThis<"u"&&globalThis.crypto&&(o=globalThis.crypto),!o&&typeof window<"u"&&window.msCrypto&&(o=window.msCrypto),!o&&typeof global<"u"&&global.crypto&&(o=global.crypto),!o&&typeof R=="function")try{o=R("crypto");}catch{}var n=function(){if(o){if(typeof o.getRandomValues=="function")try{return o.getRandomValues(new Uint32Array(1))[0]}catch{}if(typeof o.randomBytes=="function")try{return o.randomBytes(4).readInt32LE()}catch{}}throw new Error("Native crypto module could not be used to get secure random number.")},s=Object.create||(function(){function t(){}return function(r){var x;return t.prototype=r,x=new t,t.prototype=null,x}})(),c={},f=c.lib={},g=f.Base=(function(){return {extend:function(t){var r=s(this);return t&&r.mixIn(t),(!r.hasOwnProperty("init")||this.init===r.init)&&(r.init=function(){r.$super.init.apply(this,arguments);}),r.init.prototype=r,r.$super=this,r},create:function(){var t=this.extend();return t.init.apply(t,arguments),t},init:function(){},mixIn:function(t){for(var r in t)t.hasOwnProperty(r)&&(this[r]=t[r]);t.hasOwnProperty("toString")&&(this.toString=t.toString);},clone:function(){return this.init.prototype.extend(this)}}})(),m=f.WordArray=g.extend({init:function(t,r){t=this.words=t||[],r!=i?this.sigBytes=r:this.sigBytes=t.length*4;},toString:function(t){return (t||h).stringify(this)},concat:function(t){var r=this.words,x=t.words,d=this.sigBytes,C=t.sigBytes;if(this.clamp(),d%4)for(var b=0;b<C;b++){var D=x[b>>>2]>>>24-b%4*8&255;r[d+b>>>2]|=D<<24-(d+b)%4*8;}else for(var T=0;T<C;T+=4)r[d+T>>>2]=x[T>>>2];return this.sigBytes+=C,this},clamp:function(){var t=this.words,r=this.sigBytes;t[r>>>2]&=4294967295<<32-r%4*8,t.length=e.ceil(r/4);},clone:function(){var t=g.clone.call(this);return t.words=this.words.slice(0),t},random:function(t){for(var r=[],x=0;x<t;x+=4)r.push(n());return new m.init(r,t)}}),u=c.enc={},h=u.Hex={stringify:function(t){for(var r=t.words,x=t.sigBytes,d=[],C=0;C<x;C++){var b=r[C>>>2]>>>24-C%4*8&255;d.push((b>>>4).toString(16)),d.push((b&15).toString(16));}return d.join("")},parse:function(t){for(var r=t.length,x=[],d=0;d<r;d+=2)x[d>>>3]|=parseInt(t.substr(d,2),16)<<24-d%8*4;return new m.init(x,r/2)}},P=u.Latin1={stringify:function(t){for(var r=t.words,x=t.sigBytes,d=[],C=0;C<x;C++){var b=r[C>>>2]>>>24-C%4*8&255;d.push(String.fromCharCode(b));}return d.join("")},parse:function(t){for(var r=t.length,x=[],d=0;d<r;d++)x[d>>>2]|=(t.charCodeAt(d)&255)<<24-d%4*8;return new m.init(x,r)}},S=u.Utf8={stringify:function(t){try{return decodeURIComponent(escape(P.stringify(t)))}catch{throw new Error("Malformed UTF-8 data")}},parse:function(t){return P.parse(unescape(encodeURIComponent(t)))}},V=f.BufferedBlockAlgorithm=g.extend({reset:function(){this._data=new m.init,this._nDataBytes=0;},_append:function(t){typeof t=="string"&&(t=S.parse(t)),this._data.concat(t),this._nDataBytes+=t.sigBytes;},_process:function(t){var r,x=this._data,d=x.words,C=x.sigBytes,b=this.blockSize,D=b*4,T=C/D;t?T=e.ceil(T):T=e.max((T|0)-this._minBufferSize,0);var _=T*b,$=e.min(_*4,C);if(_){for(var z=0;z<_;z+=b)this._doProcessBlock(d,z);r=d.splice(0,_),x.sigBytes-=$;}return new m.init(r,$)},clone:function(){var t=g.clone.call(this);return t._data=this._data.clone(),t},_minBufferSize:0});f.Hasher=V.extend({cfg:g.extend(),init:function(t){this.cfg=this.cfg.extend(t),this.reset();},reset:function(){V.reset.call(this),this._doReset();},update:function(t){return this._append(t),this._process(),this},finalize:function(t){t&&this._append(t);var r=this._doFinalize();return r},blockSize:512/32,_createHelper:function(t){return function(r,x){return new t.init(x).finalize(r)}},_createHmacHelper:function(t){return function(r,x){return new v.HMAC.init(t,x).finalize(r)}}});var v=c.algo={};return c})(Math);return a});});var F=K((I,W)=>{(function(a,e){typeof I=="object"?W.exports=I=e(M()):typeof define=="function"&&define.amd?define(["./core"],e):e(a.CryptoJS);})(I,function(a){return (function(){var e=a,i=e.lib,o=i.WordArray,n=i.Hasher,s=e.algo,c=[],f=s.SHA1=n.extend({_doReset:function(){this._hash=new o.init([1732584193,4023233417,2562383102,271733878,3285377520]);},_doProcessBlock:function(g,m){for(var u=this._hash.words,h=u[0],P=u[1],S=u[2],V=u[3],y=u[4],v=0;v<80;v++){if(v<16)c[v]=g[m+v]|0;else {var t=c[v-3]^c[v-8]^c[v-14]^c[v-16];c[v]=t<<1|t>>>31;}var r=(h<<5|h>>>27)+y+c[v];v<20?r+=(P&S|~P&V)+1518500249:v<40?r+=(P^S^V)+1859775393:v<60?r+=(P&S|P&V|S&V)-1894007588:r+=(P^S^V)-899497514,y=V,V=S,S=P<<30|P>>>2,P=h,h=r;}u[0]=u[0]+h|0,u[1]=u[1]+P|0,u[2]=u[2]+S|0,u[3]=u[3]+V|0,u[4]=u[4]+y|0;},_doFinalize:function(){var g=this._data,m=g.words,u=this._nDataBytes*8,h=g.sigBytes*8;return m[h>>>5]|=128<<24-h%32,m[(h+64>>>9<<4)+14]=Math.floor(u/4294967296),m[(h+64>>>9<<4)+15]=u,g.sigBytes=m.length*4,this._process(),this._hash},clone:function(){var g=n.clone.call(this);return g._hash=this._hash.clone(),g}});e.SHA1=n._createHelper(f),e.HmacSHA1=n._createHmacHelper(f);})(),a.SHA1});});var J;(n=>(n.log=(s,...c)=>console.log(`[Pixi\u2019VN Json] ${s}`,...c),n.warn=(s,...c)=>console.warn(`[Pixi\u2019VN Json] ${s}`,...c),n.error=(s,...c)=>console.error(`[Pixi\u2019VN Json] ${s}`,...c),n.info=(s,...c)=>console.info(`[Pixi\u2019VN Json] ${s}`,...c)))(J||(J={}));var l=class{static init(e){this._loadAssets=e.loadAssets,this._soundOperation=e.soundOperation,this._imageOperation=e.imageOperation,this._videoOperation=e.videoOperation,this._imageContainerOperation=e.imageContainerOperation,this._canvasElementOperation=e.canvasElementOperation,this._setStorageValue=e.setStorageValue,this._inputOperation=e.inputOperation,this._tickerOperation=e.tickerOperation,this._effectOperation=e.effectOperation,this._animateOperation=e.animateOperation,this._getLogichValue=e.getLogichValue,this._getConditionalStep=e.getConditionalStep;}static get loadAssets(){return this._loadAssets}static get soundOperation(){return this._soundOperation}static get imageOperation(){return this._imageOperation}static get videoOperation(){return this._videoOperation}static get imageContainerOperation(){return this._imageContainerOperation}static get canvasElementOperation(){return this._canvasElementOperation}static get setStorageValue(){return this._setStorageValue}static get inputOperation(){return this._inputOperation}static get tickerOperation(){return this._tickerOperation}static get effectOperation(){return this._effectOperation}static get animateOperation(){return this._animateOperation}static get getLogichValue(){return this._getLogichValue}static get getConditionalStep(){return this._getConditionalStep}};N(l,"_loadAssets",()=>{throw J.error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue."),new Error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue.")}),N(l,"_soundOperation",()=>{throw J.error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue."),new Error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue.")}),N(l,"_imageOperation",()=>{throw J.error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue."),new Error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue.")}),N(l,"_videoOperation",()=>{throw J.error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue."),new Error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue.")}),N(l,"_imageContainerOperation",()=>{throw J.error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue."),new Error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue.")}),N(l,"_canvasElementOperation",()=>{throw J.error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue."),new Error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue.")}),N(l,"_setStorageValue",()=>{throw J.error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue."),new Error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue.")}),N(l,"_inputOperation",()=>{throw J.error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue."),new Error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue.")}),N(l,"_tickerOperation",()=>{throw J.error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue."),new Error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue.")}),N(l,"_effectOperation",()=>{throw J.error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue."),new Error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue.")}),N(l,"_animateOperation",()=>{throw J.error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue."),new Error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue.")}),N(l,"_getLogichValue",()=>{throw J.error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue."),new Error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue.")}),N(l,"_getConditionalStep",()=>{throw J.error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue."),new Error("An error occurred! pixi-vn-json was not initialized. Please contact the Pixi'VN team to report the issue.")});async function A(a,e){let i=l.getLogichValue(a);if(i)switch(i.type){case "sound":l.soundOperation(i);break;case "assets":case "bundle":await l.loadAssets(i);break;case "image":await l.imageOperation(i);break;case "video":await l.videoOperation(i);break;case "imagecontainer":await l.imageContainerOperation(i);break;case "canvaselement":await l.canvasElementOperation(i);break;case "value":l.setStorageValue(i);break;case "operationtoconvert":if(e){let o="";i.values.forEach(s=>{if(typeof s=="string")o+=s;else {let c=l.getLogichValue(s);o+=`${c}`;}});let n=await e(o);n&&await A(n,e);}break;case "input":l.inputOperation(i);break;case "fade":case "move":case "rotate":case "zoom":l.tickerOperation(i);break;case "shake":await l.effectOperation(i);break;case "animate":case "animate-sequence":l.animateOperation(i);break}}async function re(a,e={}){let{operationStringConvert:i,skipEmptyDialogs:o}=e;if(!Array.isArray(a))if(typeof a=="object"||typeof a=="string")a=[a];else {J.error("Error parsing imported Pixi'VN JSON: data is not an object");return}let n=a.map(async f=>{if(typeof f=="string")try{f=JSON.parse(f);}catch(g){J.error("Error parsing imported Pixi'VN JSON",g),f={};}return f}),c=(await Promise.all(n)).map(async f=>{let g=[];if(f.initialOperations){f.initialOperations.forEach(u=>{let h=A(u,i?P=>i(P,{},{}):void 0);g.push(h);});let m={};[...storage.storage.storage.keys()].forEach((u,h)=>{m[u]=storage.storage.storage.get(u);}),storage.storage.startingStorage=m;}if(f.labels){let m=f.labels;for(let u in m)try{let h=m[u],P=new O(u,h,void 0,e);narration.RegisteredLabels.add(P);}catch(h){J.error(`Error creating JSON label ${u}`,h);}}return Promise.all(g)});await Promise.all(c);}var q=ne(F());var H="___param___";var p=class p{static t(e){return Array.isArray(e)?e.map(i=>p.translate(`${i}`)):p.translate(`${e}`)}static set beforeToTranslate(e){p._beforeToTranslate=e;}static set translate(e){p._translate=e;}static get translate(){return e=>{let i=e;return p._beforeToTranslate&&(i=p._beforeToTranslate(i)),i=p._translate(i),p._afterToTranslate&&(i=p._afterToTranslate(i)),i}}static set afterToTranslate(e){p._afterToTranslate=e;}static addKey(e,i,o){let n=o.defaultValue||"empty_string";typeof i=="string"&&(i=[i]),Array.isArray(i)&&i.forEach(s=>{e[s]===void 0&&(n==="empty_string"?e[s]="":n==="copy_key"&&(p._beforeToTranslate?e[s]=p._beforeToTranslate(s):e[s]=s));});}static getConditionalsThenElse(e,i=[]){if(typeof e=="object"&&e&&"type"in e)if(e.type==="ifelse")e.then&&p.getConditionalsThenElse(e.then,i),e.else&&p.getConditionalsThenElse(e.else,i);else if(e.type==="stepswitch"){if(e.elements)if(Array.isArray(e.elements))e.elements.forEach(o=>{p.getConditionalsThenElse(o,i);});else if(e.elements.type==="ifelse"){let o=[];p.getConditionalsThenElse(e.elements,o),o.forEach(n=>{i.push(...n);});}else if(e.elements.type==="stepswitch"){let o=[];p.getConditionalsThenElse(e.elements,o),o.forEach(n=>{i.push(...n);});}else p.getConditionalsThenElse(e.elements,i);}else e.type==="resulttocombine"?(e.firstItem&&p.getConditionalsThenElse(e.firstItem,i),e.secondConditionalItem&&e.secondConditionalItem.forEach(o=>{p.getConditionalsThenElse(o,i);})):i.push(e);else e&&i.push(e);return i}static generateJsonTranslation(e,i={},o={}){let{defaultValue:n="copy_key"}=o;return e.forEach(s=>{if(s.choices){let c=[];Array.isArray(s.choices)?c=[s.choices]:c=p.getConditionalsThenElse(s.choices),c.forEach(f=>f.forEach(g=>{if("type"in g){let m=[];g.type==="ifelse"||g.type==="stepswitch"?p.getConditionalsThenElse(g,m):m=[g],m.map(h=>p.getConditionalsThenElse(h.text)).forEach(h=>{Array.isArray(h)&&h.forEach(P=>{Array.isArray(P)?P.forEach(S=>{typeof S=="string"?p.addKey(i,S,{defaultValue:n}):p.getConditionalsThenElse(S).forEach(V=>{Array.isArray(V)?V.forEach(y=>{typeof y=="string"&&p.addKey(i,y,{defaultValue:n});}):typeof V=="string"&&p.addKey(i,V,{defaultValue:n});});}):typeof P=="string"&&p.addKey(i,P,{defaultValue:n});});});}}));}if(s.dialogue){let c=[];Array.isArray(s.dialogue)?c=[s.dialogue]:c=p.getConditionalsThenElse(s.dialogue),c.forEach(f=>{if(typeof f=="string")p.addKey(i,f,{defaultValue:n});else if("text"in f){let g=p.getConditionalsThenElse(f.text);typeof g=="string"&&(g=[g]),g.forEach(m=>{typeof m=="string"?p.addKey(i,m,{defaultValue:n}):p.getConditionalsThenElse(m).forEach(h=>{typeof h=="string"?p.addKey(i,h,{defaultValue:n}):Array.isArray(h)&&h.forEach(P=>{typeof P=="string"?p.addKey(i,P,{defaultValue:n}):p.getConditionalsThenElse(P).forEach(S=>{typeof S=="string"&&p.addKey(i,S,{defaultValue:n});});});});});}});}s.conditionalStep&&p.getConditionalsThenElse(s.conditionalStep).forEach(f=>{Array.isArray(f)?p.generateJsonTranslation(f,i,o):p.generateJsonTranslation([f],i,o);});}),i}};N(p,"_beforeToTranslate"),N(p,"_translate",e=>e),N(p,"_afterToTranslate");var E=p;function se(a){try{if(typeof a>"u")return a;let e=JSON.stringify(a);return JSON.parse(e)}catch(e){throw J.error("Error creating exportable element",a,e),new Error("[Pixi\u2019VN] Error creating exportable element")}}var O=class extends narration.LabelAbstract{constructor(i,o,n,s={}){n||(n={}),n.onLoadingLabel=async()=>{for(let c of o){let f;if(typeof c=="function"?f=c():f=c,f=l.getConditionalStep(f),f.operations){let g=f.operations.map(m=>l.loadAssets(m));await Promise.all(g);}}};super(i,n);N(this,"_steps");N(this,"operationStringConvert");N(this,"skipEmptyDialogs",false);this._steps=o,this.operationStringConvert=s.operationStringConvert,this.skipEmptyDialogs=s.skipEmptyDialogs||false;}get steps(){return this._steps.map(i=>this.stepConverter(i))}get stepCount(){return this._steps.length}getStepById(i){if(i<0||i>=this._steps.length)return;let o=this._steps[i];return this.stepConverter(o)}getStepSha(i){if(i<0||i>=this.steps.length)return;let o=this._steps[i];return (0, q.default)(o.toString().toLocaleLowerCase()).toString()}getDialogueText(i){let o="";if(Array.isArray(i)){let n=[];i.forEach(s=>{if(typeof s=="string")n.push(s);else if(s&&typeof s=="object"){let c=l.getLogichValue(s);c?(c&&!Array.isArray(c)&&typeof c=="object"&&(c=l.getLogichValue(c)||""),Array.isArray(c)?n=n.concat(c):n.push(`${c}`)):n.push(`${s}`);}}),o=n;}else {let n=l.getLogichValue(i)||"";n&&!Array.isArray(n)&&typeof n=="object"&&(n=l.getLogichValue(n)||""),o=n;}return `${o}`}getDialogue(i){if(i==null)return;let o=l.getLogichValue(i);return o==null?"":typeof o=="object"&&"character"in o&&"text"in o?{character:o.character,text:E.t(this.getDialogueText(o.text))}:E.t(this.getDialogueText(o))}getChoices(i){return l.getLogichValue(i)?.map(s=>l.getLogichValue(s)).filter(s=>s!==void 0)}stepConverter(i){return async o=>{let n=typeof i=="function"?i():i;n=se(n),n=l.getConditionalStep(n);let s=this.operationStringConvert?V=>this.operationStringConvert(V,n,o):void 0,{operations:c=[]}=n;for(let V of c)await A(V,s);let{labelToOpen:f=[]}=n,g=this.getChoices(n.choices),m=l.getLogichValue(n.glueEnabled),u=this.getDialogue(n.dialogue),h=[];Array.isArray(f)||(f=[f]),f.forEach(V=>{let y=l.getLogichValue(V);y&&h.push(y);});let P=l.getLogichValue(n.goNextStep),S=l.getLogichValue(n.end);if(g){let V=g.map(y=>{let v="";if(Array.isArray(y.text)){let r=[];y.text.forEach(x=>{if(typeof x=="string")r.push(E.t(x));else if(x&&typeof x=="object"){let d=l.getLogichValue(x);d&&!Array.isArray(d)&&typeof d=="object"&&(d=l.getLogichValue(d)||""),d&&(Array.isArray(d)?r=r.concat(E.t(d)):r.push(E.t(d)));}}),v=r;}else typeof y.text=="string"&&(v=E.t(y.text));return {label:y.label,text:v,props:y.props,type:y.type,oneTime:y.oneTime,onlyHaveNoChoice:y.onlyHaveNoChoice,autoSelect:y.autoSelect}});narration.narration.choiceMenuOptions=V;}else narration.narration.choiceMenuOptions=void 0;u!==void 0&&(narration.narration.dialogue=u,this.skipEmptyDialogs&&typeof u=="string"&&(u===""||RegExp(/^\s+$/).test(u))&&(P=true)),m?storage.storage.setFlag(storage.SYSTEM_RESERVED_STORAGE_KEYS.ADD_NEXT_DIALOG_TEXT_INTO_THE_CURRENT_DIALOG_FLAG_KEY,true):m===false&&storage.storage.setFlag(storage.SYSTEM_RESERVED_STORAGE_KEYS.ADD_NEXT_DIALOG_TEXT_INTO_THE_CURRENT_DIALOG_FLAG_KEY,false);for(let V of h){let y=V.label;typeof y=="object"&&(y=l.getLogichValue(y)||"");let v=V.params?.map(t=>l.getLogichValue(t));o={...o,...V.props},V.type==="jump"?(narration.narration.openedLabels.length>0&&narration.narration.closeCurrentLabel(),storage.storage.setTempVariable(`${H}${narration.narration.openedLabels.length}`,v),await narration.narration.callLabel(y,o)):(storage.storage.setTempVariable(`${H}${narration.narration.openedLabels.length}`,v),await narration.narration.callLabel(y,o));}S==="game_end"?(narration.narration.closeAllLabels(),await narration.narration.goNext(o,{runNow:true})):S==="label_end"&&narration.narration.closeCurrentLabel(),P&&await narration.narration.goNext(o,{runNow:true});}}};exports.LabelJson=O;exports.importPixiVNJson=re;