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