//!wrt $BSPEC:{"icn":"apps/gameing","cpr":"Copyright (C) Windows 96 Team 2023.","dsc":"Gameing Sandbox Game","frn":"Gameing Runner","ver":1,"ssy":"gui"}

const{MsgBoxSimple:MsgBoxSimple,DialogCreator:DialogCreator,Theme:Theme}=w96.ui;class GameingRunnerApplication extends WApplication{constructor(){super(),this.blobUrls=[]}async fsMkBlob(a){const e=URL.createObjectURL(await FS.toBlob(a));return this.blobUrls.push(e),e}async main(a){if(super.main(a),null==a[1])return void await DialogCreator.alert("Gameing Runner v1.0<br><br>Usage: gameing-runner &lt;version&gt;",{icon:"info",title:"Gameing"}).wait();let e="";const t=`c:/user/appdata/gameing/versions/${a[1].trim()}`;if(!await FS.exists(t))return void await DialogCreator.alert("The target version is not installed.",{icon:"error",title:"Gameing"}).wait();const i=MsgBoxSimple.idleProgress("Gameing","Loading game data...");i.dlg.getBodyContainer().querySelector(".icon").style.backgroundImage=`url(${await Theme.getIconUrl("apps/gameing")})`;try{const a={code:await this.fsMkBlob(t+"/bin/code"),data:await this.fsMkBlob(t+"/bin/data"),framework:await this.fsMkBlob(t+"/bin/framework"),URT:await this.fsMkBlob(t+"/scripts/URT.js"),bg:await this.fsMkBlob(t+"/bg.jpg")},r=[{expr:/\$DATA_URL\$/g,val:a.data},{expr:/\$WASM_URL\$/g,val:a.code},{expr:/\$FRAMEWORK_URL\$/g,val:a.framework},{expr:/\$BACKGROUND_URL\$/g,val:a.bg}];let o=await FS.readstr(t+"/build.json");for(let a of r)o=o.replace(a.expr,a.val);const n=`c:/system/temp/gameing${Math.floor(1e4*Math.random())}`;await FS.mkdir(n),await FS.writestr(n+"/build.json",o);const s=await this.fsMkBlob(n+"/build.json");let l=await FS.readstr(t+"/index.html");const g=[{expr:/\$BUILD_URL\$/g,val:s},{expr:/\$URT_JS\$/g,val:a.URT}];for(let a of g)l=l.replace(a.expr,a.val);await FS.writestr(n+"/index.html",l),e=await this.fsMkBlob(n+"/index.html"),i.closeDialog()}catch(a){return console.error(a),alert(a,{icon:"error"}),void i.closeDialog()}const r=this.createWindow({title:"Gameing",body:`\n                <iframe src="${e}"></iframe>\n            `,bodyClass:"gameing-app",taskbar:!0,icon:await Theme.getIconUrl("apps/gameing","16x16"),initialWidth:640,initialHeight:441},!0);r.getBodyContainer().querySelector("iframe").onload=()=>{},r.show()}ontermination(){super.ontermination();for(let a of this.blobUrls)URL.revokeObjectURL(a)}}

return await WApplication.execAsync(new GameingRunnerApplication(), this.boxedEnv.args, this);