@drincs/pixi-vn-ink
Version:
Pixi'VN gives you the ability to write your own narrative using Ink
1 lines • 7.53 kB
JavaScript
var d;(t=>(t.log=(o,...a)=>console.log(`[Pixi\u2019VN Ink] ${o}`,...a),t.warn=(o,...a)=>console.warn(`[Pixi\u2019VN Ink] ${o}`,...a),t.error=(o,...a)=>console.error(`[Pixi\u2019VN Ink] ${o}`,...a),t.info=(o,...a)=>console.info(`[Pixi\u2019VN Ink] ${o}`,...a)))(d||={});var h="\xA7SPACE\xA7",y="\xA7DOUBLE_QUOTES\xA7",x="\xA7QUOTES\xA7",O="SPECIAL_\xA7QUOTES\xA7",v="\xA7CURLY_BRACKETS1\xA7",S="\xA7CURLY_BRACKETS2\xA7",T=["add","play","pause","resume","remove","volume"],f=class s{static _customHashtagScript=e=>false;static runCustomHashtagScript(e,i){return s._customHashtagScript(e,i,s.convertListStringToObj)}static set customHashtagScript(e){s._customHashtagScript=e;}static async generateOrRunOperationFromHashtagScript(e,i,n){try{e=e.replaceAll('\\"',y),e=e.replaceAll("\\'",x),e=e.replaceAll("\\`",O),e=e.replaceAll("\\{",v),e=e.replaceAll("\\}",S),e=e.replaceAll("{"," { "),e=e.replaceAll("}"," } "),e=e.replaceAll(v,"{"),e=e.replaceAll(S,"}");let t=[],o,a="";for(let r=0;r<e.length;r++){let p=e[r];p==='"'||p==="'"||p==="`"?o===void 0?(t.push(a),a="",o=p,a+=p):o===p?(o=void 0,a+=p,t.push(a),a=""):a+=p:a+=p;}a!==""&&t.push(a),t.forEach((r,p)=>{p%2===1&&(t[p]=r.replaceAll(" ",h));}),e=t.join(""),t=e.split(" ").filter(r=>r!==""),t=t.map(r=>r.replaceAll(h," ").replaceAll(y,'"').replaceAll(x,"'").replaceAll(O,"`"));let u=s.runCustomHashtagScript(t,n);if(u===!0)return;if(typeof u=="string")return u.startsWith("#")&&(u=u.substring(1)),s.generateOrRunOperationFromHashtagScript(u,i,n);let c=t.length>1?s.removeExtraDoubleQuotes(t[1]):"",l=t.length>0?s.removeExtraDoubleQuotes(t[0]):"";switch(c){case "image":case "imagecontainer":case "canvaselement":case "video":return c==="video"&&(l==="pause"||l==="resume")?{type:"video",operationType:l,alias:s.removeExtraDoubleQuotes(t[2])}:s.getCanvasOperationFromComment(t,c);case "sound":return s.getSoundOperationFromComment(t);case "input":if(l==="request"){let r={type:"input",operationType:"request"};if(t.length>2)try{let p=t.slice(2),g=s.convertListStringToObj(p);"type"in g&&typeof g.type=="string"&&(r.valueType=g.type),"default"in g&&(r.defaultValue=g.default);}catch{}return r}break;case "assets":case "bundle":switch(l){case "load":case "lazyload":return {type:c,operationType:l,aliases:t.slice(2)}}break;default:if(c)switch(l){case "call":case "jump":i.labelToOpen={label:c,type:l},i.goNextStep=void 0;return;case "fade":case "move":case "rotate":case "zoom":case "shake":let r={};if(t.length>2)try{r=s.convertListStringToObj(t.slice(2));}catch{}if(l=="move"&&"x"in r&&"y"in r&&(r.destination={x:r.x,y:r.y,type:"pixel"},delete r.x,delete r.y),l=="move"&&"xAlign"in r&&"yAlign"in r&&(r.destination={x:r.xAlign,y:r.yAlign,type:"align"},delete r.xAlign,delete r.yAlign),l=="move"&&"xPercentage"in r&&"yPercentage"in r&&(r.destination={x:r.xPercentage,y:r.yPercentage,type:"percentage"},delete r.xPercentage,delete r.yPercentage),l=="move"&&!("destination"in r)){d.error("The move operation don't have a destination or destination is not valid",r);return}return {alias:c,type:l,props:r}}else switch(l){case "pause":"dialogue"in i&&delete i.dialogue,"goNextStep"in i&&delete i.goNextStep;break}}}catch(t){throw d.error("Error parsing ink hashtag-script",e),t}d.error("The operation is not valid",e);}static getCanvasOperationFromComment(e,i){let n=s.removeExtraDoubleQuotes(e[0]),t=s.removeExtraDoubleQuotes(e[2]),o=s.convertListStringToPropList(e.slice(3));switch(n){case "show":switch(i){case "image":case "video":return s.getImageOperationFromComment(i,t,o);case "imagecontainer":return s.getContainerOperationFromComment(i,t,o);case "canvaselement":default:d.error("This show operation is not valid for this type of element",i);}case "edit":return {type:i,operationType:"edit",alias:t,props:s.convertPropListStringToObj(o)};case "remove":let u={type:i,operationType:"remove",alias:t};if(o.length>1&&o[0]==="with"){let c=e[e.indexOf("with")+1],l=e.slice(e.indexOf("with")+2),r=s.getTransition(c,l);r!==void 0&&(u.transition=r);}return u;default:d.error("The operation type is not valid",n);}}static getImageOperationFromComment(e,i,n){let t,o;n.length%2===0?(t=i,o=n):(t=s.removeExtraDoubleQuotes(n[0]),o=s.convertListStringToPropList(n.slice(1)));let a={type:e,operationType:"show",alias:i,url:t};return s.setShowProps(a,o)}static getContainerOperationFromComment(e,i,n){let t=[],o=n.findIndex(l=>l.startsWith("[")),a=n.findIndex(l=>l.endsWith("]"));if(o===-1||a===-1){d.error("Show imagecontainer must have a list of image ulrs",n);return}if(t=n.slice(o,a+1),t.length<2){d.error("Show imagecontainer must have a list of image ulrs",n);return}t[0]==="["?t.shift():t[0]=t[0].substring(1),t[t.length-1]==="]"?t.pop():t[t.length-1]=t[t.length-1].substring(0,t[t.length-1].length-1);let u={type:e,operationType:"show",alias:i,urls:t.map(l=>s.removeExtraDoubleQuotes(l))},c=s.convertListStringToPropList(n.slice(a+1));return s.setShowProps(u,c)}static getSoundOperationFromComment(e){let i=s.removeExtraDoubleQuotes(e[0]);if(!T.includes(i))return;let n=s.removeExtraDoubleQuotes(e[2]);switch(i){case "play":let t={type:"sound",operationType:"play",alias:n};if(e.length>3){let l=s.getSoundPlayOptions(e.slice(3));l!==void 0&&(t.props=l);}return t;case "pause":case "resume":return {type:"sound",operationType:i,alias:n};case "stop":case "remove":return {type:"sound",operationType:"stop",alias:n};case "volume":let u=parseFloat(e[3]);return {type:"sound",operationType:"volume",alias:n,value:u}}}static setShowProps(e,i){if(i.length>0){if(i.includes("with")&&i.length>i.indexOf("with")+1){let n=i[i.indexOf("with")+1],t=i.slice(i.indexOf("with")+2);i=i.slice(0,i.indexOf("with"));let o=s.getTransition(n,t);o!==void 0&&(e.transition=o);}if(i.length>0){let n=s.convertListStringToObj(i);e.props=n;}}return e}static getTransition(e,i){switch(e){case "dissolve":case "fade":case "movein":case "moveout":case "zoomin":case "zoomout":case "pushin":case "pushout":break;default:return}let n={type:e};if(i.length>0)try{let t=s.convertPropListStringToObj(i);n.props=t;}catch{}return n}static getSoundOption(e){try{return s.convertListStringToObj(e)}catch{return}}static getSoundPlayOptions(e){try{return s.convertListStringToObj(e)}catch{return}}static convertListStringToObj(e){let i=s.convertListStringToPropList(e);return s.convertPropListStringToObj(i)}static convertListStringToPropList(e){let i=[],n=0,t="";for(let o=0;o<e.length;o++){let a=e[o];a.startsWith("{")?(n++,t+=a):a.endsWith("}")&&n>0?(n--,t+=a,n===0&&(i.push(t),t="")):n>0?t+=a:i.push(a);}return i}static convertPropListStringToObj(e){if(e.length%2!==0)throw d.error("The props list must have a pair number of elements",e),new Error("The props list must have a pair number of elements");let i="{";e.forEach((n,t)=>{if(t%2===0)i+=`"${n}": `;else {switch(n){case "null":case "undefined":case "true":case "false":i+=`${n}`;break;default:s.containExtraDoubleQuotes(n)?(n=s.removeExtraDoubleQuotes(n),i+=`"${n}"`):n.startsWith("{")&&n.endsWith("}")?i+=`${n}`:n.startsWith('"')&&n.endsWith('"')?i+=`${n}`:isNaN(parseFloat(n))?i+=`"${n}"`:i+=`${n}`;}t<e.length-1&&(i+=", ");}}),i+="}";try{return JSON.parse(i)}catch(n){throw d.error("Error parsing ink json",i),n}}static removeExtraDoubleQuotes(e){return e.startsWith('"')&&e.endsWith('"')||e.startsWith("'")&&e.endsWith("'")||e.startsWith("`")&&e.endsWith("`")?e.substring(1,e.length-1):e}static containExtraDoubleQuotes(e){return !!(e.startsWith('"')&&e.endsWith('"')||e.startsWith("'")&&e.endsWith("'")||e.startsWith("`")&&e.endsWith("`"))}};export{f as default};