"use strict";window.packageRoot="packages://hot-update-tools/";const fs=require("fire-fs"),path=require("fire-path"),Electron=require("electron"),{remote:remote}=require("electron"),CfgUtil=Editor.require("packages://hot-update-tools/core/CfgUtil.js"),FileUtil=Editor.require("packages://hot-update-tools/core/FileUtil.js"),Mail=Editor.require("packages://hot-update-tools/mail/Mail.js"),OSS=Editor.require("packages://hot-update-tools/node_modules/ali-oss"),CO=Editor.require("packages://hot-update-tools/node_modules/co"),CocosAnalytics=Editor.require("packages://hot-update-tools/core/cocosAnalytics.min.js"),GoogleAnalytics=Editor.require("packages://hot-update-tools/core/GoogleAnalytics.js");Editor.Panel.extend({style:fs.readFileSync(Editor.url("packages://hot-update-tools/panel/index.css","utf8"))+"",template:fs.readFileSync(Editor.url("packages://hot-update-tools/panel/index.html","utf8"))+"",$:{logTextArea:"#logTextArea",hotAddressSelect:"#hotAddressSelect",testEnvSelect:"#testEnvSelect"},ready(){GoogleAnalytics.init(),GoogleAnalytics.eventOpen();let e=this.$logTextArea,t=this.$hotAddressSelect;window.hotAddressSelectCtrl=t,window.plugin=new window.Vue({el:this.shadowRoot,created:function(){this._initPluginCfg()},init:function(){},data:{testHttpUrl:null,srcDirPath:"",resDirPath:"",projManifestPath:"",verManifestPath:"",version:"",remoteServerVersion:"",isShowRemoteServerVersion:!1,genManifestDir:"",serverRootDir:"",resourceRootDir:"",localServerPath:"",logView:"",copyProgress:0,totalNum:0,curNum:0,serverVersion:"-",serverPackageUrl:"",localGameVersion:"-",localGamePackageUrl:"",localGameProjectManifest:"",localGameVersionManifest:"",localGameProjectManifestUrl:"",localGameVersionManifestUrl:"",testEnvLocal:!0,testEnvALi:!1,testEnvEmail:!1,testEnvSelect:0,isShowUseAddrBtn:!1,isShowDelAddrBtn:!1,hotAddressArray:[],emailContent:"邮件内容!",addMailPeople:"",emailPeopleArray:["xu_yanfeng@126.com"]},computed:{},methods:{onTest(){},onBtnClickOpenTestHttpServer(){require("http").createServer(function(e,t){t.writeHead(200,{"Content-Type":"text-plain"}),t.end("Hello World\n")}).listen(9800),this.testHttpUrl="http://127.0.0.1:9800"},onBtnClickTestHttp(){this.testHttpUrl&&Electron.shell.openExternal(this.testHttpUrl)},onStopTouchEvent(e){e.preventDefault(),e.stopPropagation()},onBtnClickHelpDoc(){GoogleAnalytics.eventDoc();Electron.shell.openExternal("https://tidys.github.io/plugin-docs-oneself/docs/hot-update-tools/")},onBtnClickTellMe(){GoogleAnalytics.eventQQ();Electron.shell.openExternal("http://wpa.qq.com/msgrd?v=3&uin=774177933&site=qq&menu=yes")},onBtnClickAliTest(){let e=new OSS({region:"oss-cn-beijing",accessKeyId:"LTAIOxxDqJpJbzfy",accessKeySecret:"kZRbbX3nNtxWlx5XWsR8uRrJzj4X5C",bucket:"happycars"});CO(function*(){e.useBucket("happycars"),yield function*(t){let i=yield e.list({prefix:t,delimiter:"/"});i.prefixes.forEach(function(e){}),i.objects.forEach(function(e){})}("hot")}).catch(function(e){})},onBtnClickSendMail(){Mail.sendMail(this.remoteServerVersion,this.emailContent,null,function(){this._addLog("发送邮件完毕!")}.bind(this))},onInputMailPeopleOver(){!1===this.isPeopleExist()&&this.emailPeopleArray.push(this.addMailPeople)},isPeopleExist(){if(null===this.addMailPeople||""===this.addMailPeople)return!1;for(let e=0;e0){let t=!1;for(let i=0;i0);else{let e=CfgUtil.getPackZipDir();fs.existsSync(e)||fs.mkdirSync(e);let t=path.join(e,"server");fs.existsSync(t)||fs.mkdirSync(t),this.localServerPath=t}},selectProjectManifestDir(){let e=Editor.Dialog.openFile({title:"选择导入manifest的目录",defaultPath:path.join(Editor.projectInfo.path,"assets"),properties:["openDirectory"],callback:function(e){}});if(-1!==e){let t=e[0],i=Editor.assetdb.remote.fspathToUrl(t);this.localGameProjectManifestUrl=i,this.localGameVersionManifestUrl=i,this.importManifestToGame(),this._addLog("导入完成,请检查项目目录:"+i)}},importManifestToGame(){let e=path.join(this.genManifestDir,"project.manifest"),t=path.join(this.genManifestDir,"version.manifest");if(!fs.existsSync(e))return void this._addLog("文件不存在: "+e);if(!fs.existsSync(t))return void this._addLog("文件不存在: "+t);let i=this.localGameProjectManifestUrl.split("project.manifest")[0];Editor.assetdb.import([e,t],i,function(e,t){t.forEach(function(e){})}.bind(this)),this.initLocalGameVersion()},initLocalGameVersion(){Editor.assetdb.queryAssets("db://assets/**/*",null,function(e,t){let i="",s="";if(t.forEach(function(e){"version.manifest"===path.basename(e.path)?(i=e.path,this.localGameVersionManifestUrl=e.url):"project.manifest"===path.basename(e.path)&&(s=e.path,this.localGameProjectManifestUrl=e.url)}.bind(this)),0===i.length)return void this._addLog("项目中没有配置文件: version.manifest");if(0===s.length)return void this._addLog("项目中没有配置文件: project.manifest");this.localGameVersionManifest=i,this.localGameProjectManifest=s;let r="",o="";fs.readFile(i,"utf-8",function(e,t){if(e)this._addLog("读取项目中的配置文件失败: "+i);else{let e=JSON.parse(t);r=e.version,fs.readFile(s,"utf-8",function(e,t){if(e)this._addLog("读取项目中的配置文件失败: "+s);else{let e=JSON.parse(t);(o=e.version)===r?(this.localGameVersion=o,this.localGamePackageUrl=e.packageUrl):this._addLog("游戏中的 project.manifest 和 version.manifest 中的version字段值不一致,请检查配置文件")}}.bind(this))}}.bind(this))}.bind(this))},_initResourceBuild(){if(0===this.resourceRootDir.length){let e=path.join(Editor.assetdb.library,"../"),t=path.join(e,"local/builder.json");FileUtil.isFileExit(t)?fs.readFile(t,"utf-8",function(t,i){if(!t){let t=JSON.parse(i).buildPath,s=path.join(e,t),r=path.join(s,"jsb-default");this._checkResourceRootDir(r)}}.bind(this)):this._addLog("发现没有构建项目, 使用前请先构建项目!")}},_checkResourceRootDir(e){if(FileUtil.isFileExit(e)){let t=path.join(e,"src"),i=path.join(e,"res");return!1===FileUtil.isFileExit(t)?(this._addLog("没有发现 "+t+", 请先构建项目."),!1):!1===FileUtil.isFileExit(i)?(this._addLog("没有发现 "+i+", 请先构建项目."),!1):(this.resourceRootDir=e,!0)}return this._addLog("没有发现 "+e+", 请先构建项目."),!1},onClickGenCfg(e){GoogleAnalytics.eventCustom("GenManifest");let t=CfgUtil.getBuildTimeGenTime();t.genTime===t.buildTime?(CfgUtil.updateGenTime((new Date).getTime(),this.version),!this.version||this.version.length<=0?this._addLog("[生成] 版本号未填写"):!this.serverRootDir||this.serverRootDir.length<=0?this._addLog("[生成] 服务器地址未填写"):0!==this.resourceRootDir.length?!1!==this._checkResourceRootDir(this.resourceRootDir)&&(!this.genManifestDir||this.genManifestDir.length<=0?this._addLog("[生成] manifest文件生成地址未填写"):fs.existsSync(this.genManifestDir)?(this._saveConfig(),this._genVersion(this.version,this.serverRootDir,this.resourceRootDir,this.genManifestDir)):this._addLog("[生成] manifest存储目录不存在: "+this.genManifestDir)):this._addLog("[生成] 请先指定 ")):this._addLog("[生成] 你需要重新构建项目,因为上次构建已经和版本关联:"+CfgUtil.cfgData.genVersion)},_genVersion(e,t,i,s){this._addLog("[Build] 开始生成manifest配置文件....");let r={version:e,packageUrl:t,remoteManifestUrl:"",remoteVersionUrl:"",assets:{},searchPaths:[]};"/"===t[t.length-1]?(r.remoteManifestUrl=t+"project.manifest",r.remoteVersionUrl=t+"version.manifest"):(r.remoteManifestUrl=t+"/project.manifest",r.remoteVersionUrl=t+"/version.manifest");let o=s,n=i,a=function(e,t){let i=fs.statSync(e);if(!i.isDirectory())return;let s,r,o,l,h,d=fs.readdirSync(e);for(let c=0;c0&&fs.existsSync(this.localServerPath)&&(t=this.localServerPath);let i=Editor.Dialog.openFile({title:"选择本地测试服务器目录",defaultPath:t,properties:["openDirectory"]});-1!==i&&(this.localServerPath=i[0],this._saveConfig(),this._updateServerVersion())},onCopyFileToLocalServer(){if(GoogleAnalytics.eventCustom("copyFileToLocalServer"),!fs.existsSync(this.localServerPath))return void this._addLog("本地测试服务器目录不存在:"+this.localServerPath);let e=path.join(this.resourceRootDir,"src"),t=path.join(this.resourceRootDir,"res");if(!fs.existsSync(this.resourceRootDir))return void this._addLog("资源目录不存在: "+this.resourceRootDir+", 请先构建项目");if(!fs.existsSync(e))return void this._addLog(this.resourceRootDir+"不存在src目录, 无法拷贝文件");if(!fs.existsSync(t))return void this._addLog(this.resourceRootDir+"不存在res目录, 无法拷贝文件");let i=path.join(this.genManifestDir,"project.manifest"),s=path.join(this.genManifestDir,"version.manifest");if(!this.genManifestDir||this.genManifestDir.length<=0)return void this._addLog("manifest文件生成地址未填写");if(!this._getFileIsExist(i))return void this._addLog(i+"不存在, 请点击生成配置");if(!this._getFileIsExist(s))return void this._addLog(s+"不存在, 请点击生成配置");this._addLog("[部署] 开始拷贝文件到:"+this.localServerPath),this.curNum=0,this.copyProgress=0,this._addLog("[部署] 删除目录路径: "+this.localServerPath);let r=this._getFileNum(this.localServerPath);this._addLog("[部署] 删除文件个数:"+r),this._delDir(this.localServerPath),this.totalNum=this._getTotalCopyFileNum(),this._addLog("[部署] 复制文件个数:"+this.totalNum),this._copySourceDirToDesDir(e,path.join(this.localServerPath,"src")),this._copySourceDirToDesDir(t,path.join(this.localServerPath,"res")),this._copyFileToDesDir(i,this.localServerPath),this._copyFileToDesDir(s,this.localServerPath)},_getTotalCopyFileNum(){return this._getFileNum(path.join(this.resourceRootDir,"src"))+this._getFileNum(path.join(this.resourceRootDir,"res"))+2+2},addProgress(){this.curNum++;let e=this.curNum/this.totalNum;e=e||0,this.copyProgress=100*e,e>=1&&(this._addLog("[部署] 部署到指定目录成功:"+this.localServerPath),this._updateServerVersion())},refreshLocalServerVersion(){this._updateServerVersion()},_updateServerVersion(){if(this.localServerPath.length>0){let e=require("fire-path"),t=require("fire-fs"),i=e.join(this.localServerPath,"version.manifest");t.readFile(i,"utf-8",function(i,s){if(i){let i=e.join(this.localServerPath,"project.manifest");t.readFile(i,"utf-8",function(e,t){if(e)this._addLog("无法获取到本地测试服务器版本号");else{let e=JSON.parse(t);this.serverVersion=e.version,this.serverPackageUrl=e.packageUrl}}.bind(this))}else{let e=JSON.parse(s);this.serverVersion=e.version,this.serverPackageUrl=e.packageUrl}}.bind(this))}else this._addLog("请选择本机server物理路径")},_getFileNum(e){let t=0,i=function(e){let s=fs.readdirSync(e);for(let r in s){t++;let o=path.join(e,s[r]);fs.statSync(o).isDirectory()&&i(o)}};return i(e),t},_delDir(e){let t=function(e){let i=fs.readdirSync(e);for(let s in i){let r=path.join(e,i[s]);fs.statSync(r).isDirectory()?t(r):fs.unlinkSync(r)}},i=function(t){let s=fs.readdirSync(t);if(s.length>0){for(let e in s){let r=path.join(t,s[e]);i(r)}t!==e&&fs.rmdirSync(t)}else t!==e&&fs.rmdirSync(t)};t(e),i(e)},_copyFileToDesDir(e,t){if(this._getFileIsExist(e)){let i=fs.createReadStream(e),s=path.basename(e),r=path.join(t,s),o=fs.createWriteStream(r);i.pipe(o),this.addProgress()}},_copySourceDirToDesDir(e,t){let i=this,s=function(e,t,s){fs.exists(t,function(r){r?s(e,t):fs.mkdir(t,function(){i.addProgress(),s(e,t)})})},r=function(e,t){fs.readdir(e,function(o,n){if(o)throw o;n.forEach(function(o){let n,a,l=e+"/"+o,h=t+"/"+o;fs.stat(l,function(e,t){if(e)throw e;t.isFile()?(n=fs.createReadStream(l),a=fs.createWriteStream(h),n.pipe(a),i.addProgress()):t.isDirectory()&&s(l,h,r)})})})};s(e,t,r)},_isVersionPass(e,t){if(void 0===e||null===e||void 0===t||null===t)return!1;let i=e.split("."),s=t.split("."),r=i.length>s.length?i.length:s.length;for(let e=0;eparseInt(r))return!0}return!1},onInputVersionOver(){let e=CfgUtil.cfgData.genVersion,t=(CfgUtil.cfgData.buildTime,CfgUtil.cfgData.genTime,this.remoteServerVersion);null!==t&&void 0!==t&&(this._isVersionPass(this.version,t)?(this._addLog("上次构建时版本号: "+e),this._isVersionPass(this.version,e)?this._addLog("版本通过验证!"):this._addLog("[Warning] 要构建的版本低于上次构建版本: "+this.version+"<="+e)):this._addLog("[Warning] version 填写的版本低于远程版本")),this._saveConfig()},onInPutUrlOver(e){let t=this.serverRootDir;if("http://"===t||"https://"===t||"http"===t||"https"===t||"http:"===t||"https:"===t)return;let i=t.indexOf("http://"),s=t.indexOf("https://");if(-1===i&&-1===s){/^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/.test(t)||(this._addLog(t+" 不是以http://https://开头,或者不是网址, 已经自动修改"),this.serverRootDir="http://"+this.serverRootDir,this._getRemoteServerVersion())}else this._getRemoteServerVersion();this._addHotAddress(this.serverRootDir),this._updateShowUseAddrBtn(),this._saveConfig()},_getRemoteServerVersion(){if(this.serverRootDir.length<=0)return;this.isShowRemoteServerVersion=!1,this.remoteServerVersion=null;let e=this.serverRootDir+"/version.manifest",t=new XMLHttpRequest;t.onreadystatechange=function(){if(4===t.readyState&&t.status>=200&&t.status<400){let e=t.responseText,i=null;try{i=JSON.parse(e)}catch(e){return void this._addLog("获取远程版本号失败!")}this.isShowRemoteServerVersion=!0,this.remoteServerVersion=i.version}else t.status}.bind(this),t.open("get",e,!0),t.setRequestHeader("If-Modified-Since","0"),t.send()},onTestUrl(){let e=this.serverRootDir;e.length>0?this._isUrlAvilable(e,function(t){this._addLog(e+" 响应: "+t)}.bind(this)):this._addLog("请填写 [资源服务器url] ")},_isUrlAvilable(e,t){let i=require("http");try{i.get(e,function(e){t&&t(e.statusCode)}.bind(this))}catch(e){t(-1)}},onOpenUrl(e){let t=this.serverRootDir;t.length>0?Electron.shell.openExternal(t):this._addLog("未填写参数")},userLocalIP(){GoogleAnalytics.eventCustom("useLocalIP");let e="",t=require("os").networkInterfaces();Object.keys(t).forEach(function(i){t[i].forEach(function(t){"IPv4"===t.family&&!1===t.internal&&(e=t.address)})}),e.length>0&&(this.serverRootDir="http://"+e,this.onInPutUrlOver(null))},onClickOpenVersionDir(){let e=CfgUtil.getPackZipDir();fs.existsSync(e)?(Electron.shell.showItemInFolder(e),Electron.shell.beep()):this._addLog("目录不存在:"+e)},onSelectSrcDir(e){let t=Editor.Dialog.openFile({title:"选择Src目录",defaultPath:Editor.projectInfo.path,properties:["openDirectory"],callback:function(e){}});-1!==t&&(this.srcDirPath=t[0],this._saveConfig())},onSelectResDir(){let e=Editor.Dialog.openFile({title:"选择Res目录",defaultPath:Editor.projectInfo.path,properties:["openDirectory"]});-1!==e&&(this.resDirPath=e[0],this._saveConfig())},onOpenResourceDir(){fs.existsSync(this.resourceRootDir)?(Electron.shell.showItemInFolder(this.resourceRootDir),Electron.shell.beep()):this._addLog("目录不存在:"+this.resourceRootDir)},onOpenManifestDir(){fs.existsSync(this.genManifestDir)?(Electron.shell.showItemInFolder(this.genManifestDir),Electron.shell.beep()):this._addLog("目录不存在:"+this.genManifestDir)},onOpenLocalServer(){fs.existsSync(this.genManifestDir)?(Electron.shell.showItemInFolder(this.localServerPath),Electron.shell.beep()):this._addLog("目录不存在:"+this.localServerPath)},onSelectGenManifestDir(){let e=Editor.Dialog.openFile({title:"选择生成Manifest目录",defaultPath:Editor.projectInfo.path,properties:["openDirectory"]});-1!==e&&(this.genManifestDir=e[0],this._saveConfig())},onSelectGenServerRootDir(){let e=Editor.Dialog.openFile({title:"选择部署的服务器根目录",defaultPath:Editor.projectInfo.path,properties:["openDirectory"]});-1!==e&&(this.serverRootDir=e[0],this._saveConfig())},onSelectResourceRootDir(){let e=Editor.Dialog.openFile({title:"选择构建后的根目录",defaultPath:Editor.projectInfo.path,properties:["openDirectory"]});if(-1!==e){let t=e[0];this._checkResourceRootDir(t)&&(this.resourceRootDir=t,this._saveConfig())}},onCleanSimRemoteRes(){let e=require("fire-path"),t=cc.sys.os,i=null;if("Windows"===t){let t=e.join(__dirname,"../cocos2d-x/simulator/");i=e.join(t,"win32/remote-asset")}else if("OS X"===t){let t=e.join(__dirname,"../cocos2d-x/simulator/");i=e.join(t,"mac/Simulator.app/Contents/Resources/remote-asset")}i&&(fs.existsSync(i)?(FileUtil.emptyDir(i),this._addLog("[清理热更缓存] 清空目录 "+i+" 成功.")):this._addLog("[清理热更缓存] 目录不存在: "+i))},onOpenLocalGameManifestDir(){let e=this.localGameProjectManifest.split("project.manifest")[0];fs.existsSync(e)?(Electron.shell.showItemInFolder(e),Electron.shell.beep()):this._addLog("目录不存在:"+e)},onTestZip(){require("node-native-zip")},onTestSelect(){let e=100*Math.random();var i;this.hotAddressArray.push(e.toFixed(2)),i=this.hotAddressArray.length-1,setTimeout(function(){t.selectedIndex=i},10)},onIpSelectChange(e){},onLogIp(){Editor.Ipc.sendToMain("hot-update-tools:test","Hello, this is simple panel")},onTestEnvChange(e){let t=e.target.value;this.testEnvALi=!1,this.testEnvLocal=!1,this.testEnvEmail=!1,"0"===t?this.testEnvLocal=!0:"1"===t?this.testEnvALi=!0:"2"===t&&(this.testEnvEmail=!0)}}})},messages:{"hot-update-tools:onBuildFinished"(e,t){window.plugin.onBuildFinished(t)}}});