const Path=require("path"),Fs=require("fs"),FsExtra=require("fs-extra"),OutPut=Editor.require("packages://hot-update-tools/core/OutPut.js"),{shell:shell}=require("electron");Vue.component("env-project",{template:Fs.readFileSync(Editor.url("packages://hot-update-tools/panel/env-project.html"),"utf-8"),mixins:[Editor.require("packages://hot-update-tools/panel/mixin.js")],data:()=>({localGameVersion:"-",localGamePackageUrl:"",localGameProjectManifest:"",localGameVersionManifest:""}),created(){this.initLocalGameVersion()},methods:{importManifestToGame(){let e={project:"project.manifest",version:"version.manifest"},t=Path.join(OutPut.manifestDir,e.project);if(!Fs.existsSync(t))return void this.log(`[${e.project}]文件不存在: ${t}`);let s=Path.join(OutPut.manifestDir,e.version);if(!Fs.existsSync(s))return void this.log(`[${e.version}]文件不存在: ${s}`);let i=null;i=Fs.existsSync(this.localGameProjectManifest)?Path.dirname(this.localGameProjectManifest):Editor.url("db://assets");for(let t in e){let s=Path.join(OutPut.manifestDir,e[t]),a=Path.join(i,e[t]);FsExtra.copyFileSync(s,a);let o=Editor.assetdb.remote.fspathToUrl(a);Editor.assetdb.refresh(o)}this.initLocalGameVersion()},onClickLocalPackageUrl(){let e=`${this.localGamePackageUrl}/version.manifest`;shell&&shell.openExternal(e)},initLocalGameVersion(){const e=require("globby");let t=Editor.url("db://assets"),s=Path.join(t,"**/*.manifest");s=Path.normalize(s).replace(/\\/g,"/");let i="",a="";if(e.sync([s]).forEach(e=>{"version.manifest"===Path.basename(e)?i=e:"project.manifest"===Path.basename(e)&&(a=e)}),0===i.length)return void this.log("项目中没有配置文件: version.manifest");if(0===a.length)return void this.log("项目中没有配置文件: project.manifest");this.localGameVersionManifest=i,this.localGameProjectManifest=a;let o=null,r=null;if(Fs.existsSync(i))try{o=JSON.parse(Fs.readFileSync(i,"utf-8"))}catch(e){return void this.log(`配置数据异常，不是json格式：${i}`)}else this.log("读取项目中的配置文件失败: "+i);if(Fs.existsSync(a))try{r=JSON.parse(Fs.readFileSync(a,"utf-8"))}catch(e){return void this.log(`配置数据异常，不是json格式：${a}`)}else this.log("读取项目中的配置文件失败: "+a);r&&o&&r.version&&o.version&&r.version===o.version?(this.localGameVersion=r.version,this.localGamePackageUrl=r.packageUrl):this.log("游戏中的 project.manifest 和 version.manifest 中的数据异常,请检查配置文件")}}});