From ef915a121e144d68f35a278b78a4cb0788a2350b Mon Sep 17 00:00:00 2001 From: andrewlu Date: Wed, 3 Feb 2021 21:34:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=89=88=E6=9C=AC=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E8=8E=B7=E5=8F=96=E5=B7=A5=E5=85=B7=E7=B1=BB.=20?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E5=9C=A8=E9=A1=B9=E7=9B=AE=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=B8=AD=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8=20import=20um=20?= =?UTF-8?q?=3D=20require('UpdateManager')=20=E7=84=B6=E5=90=8E=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/Bundle2/UI2.prefab | 15 +- assets/Bundle2/UI2.ts | 29 ++-- packages/update-manager/js/FileUploader.js | 14 +- packages/update-manager/js/FileUtils.js | 9 +- packages/update-manager/lib/UpdateManager.js | 129 ++++++++++++++++++ .../update-manager/lib/UpdateManager.js.meta | 9 ++ packages/update-manager/main.js | 7 +- packages/update-manager/package.json | 9 ++ packages/update-manager/panel/index.html | 25 ++-- packages/update-manager/templates/pre_main.js | 39 +----- 10 files changed, 201 insertions(+), 84 deletions(-) create mode 100644 packages/update-manager/lib/UpdateManager.js create mode 100644 packages/update-manager/lib/UpdateManager.js.meta diff --git a/assets/Bundle2/UI2.prefab b/assets/Bundle2/UI2.prefab index 46a1bc9..9435981 100644 --- a/assets/Bundle2/UI2.prefab +++ b/assets/Bundle2/UI2.prefab @@ -105,7 +105,7 @@ "_contentSize": { "__type__": "cc.Size", "width": 700, - "height": 255.6 + "height": 75.6 }, "_anchorPoint": { "__type__": "cc.Vec2", @@ -117,7 +117,7 @@ "ctor": "Float64Array", "array": [ 0, - 0, + 282.58, 0, 0, 0, @@ -156,9 +156,9 @@ ], "_srcBlendFactor": 770, "_dstBlendFactor": 771, - "_string": "这是Bundle2,修改此bundle 并上传更新包,然后运行查看效果.\n当前版本V4", - "_N$string": "这是Bundle2,修改此bundle 并上传更新包,然后运行查看效果.\n当前版本V4", - "_fontSize": 60, + "_string": "", + "_N$string": "", + "_fontSize": 30, "_lineHeight": 60, "_enableWrapText": true, "_N$file": null, @@ -193,8 +193,9 @@ "__id__": 1 }, "_enabled": true, - "label": null, - "text": "hello", + "label": { + "__id__": 3 + }, "_id": "" }, { diff --git a/assets/Bundle2/UI2.ts b/assets/Bundle2/UI2.ts index 367a0fa..67a8e19 100644 --- a/assets/Bundle2/UI2.ts +++ b/assets/Bundle2/UI2.ts @@ -1,28 +1,21 @@ -// Learn TypeScript: -// - https://docs.cocos.com/creator/manual/en/scripting/typescript.html -// Learn Attribute: -// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html -// Learn life-cycle callbacks: -// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html - const {ccclass, property} = cc._decorator; +//@ts-ignore +import UpdateManager = require('UpdateManager'); + @ccclass export default class NewClass extends cc.Component { @property(cc.Label) label: cc.Label = null; - @property - text: string = 'hello'; - - // LIFE-CYCLE CALLBACKS: - - // onLoad () {} - - start () { - + start() { + if (UpdateManager.getUpdateFlag()) { + this.label.string = UpdateManager.cur_ver_desc; + UpdateManager.resetUpdateFlag(); + } else { + this.label.string = "未发现新版本,\n当前版本:" + UpdateManager.cur_ver_name; + } + console.log('>>>>>>>>>>>>>>>>>>>>>', UpdateManager.cur_ver_code); } - - // update (dt) {} } diff --git a/packages/update-manager/js/FileUploader.js b/packages/update-manager/js/FileUploader.js index 1b8540b..41ed34e 100644 --- a/packages/update-manager/js/FileUploader.js +++ b/packages/update-manager/js/FileUploader.js @@ -79,11 +79,11 @@ class FileUploader { if (this.ftps[i].totalTasks && !this.ftps[i].idleState) { p = (this.ftps[i].totalTasks - this.ftps[i].queue.length) / this.ftps[i].totalTasks; } - p = Math.floor(p * 10000) / 100; + p = parseFloat((Math.floor(p * 10000) / 100).toFixed(2)); str.push({ name: i, idle: this.ftps[i].idleState, - remain: this.ftps[i].queue.length, + remain: this.ftps[i].getTaskLength(), hasError: this.ftps[i].hasError, totalTasks: this.ftps[i].totalTasks, curTaskPath: this.ftps[i].curTaskPath, @@ -240,7 +240,15 @@ class UploadThread { uploadOnce(ftp, once) { return Promise.race([new Promise(function (resolve) { - ftp.put(once.src, once.dst, false, resolve) + fs.readFile(once.src, (err, data) => { + if (err) { + logger.error('文件读取异常:', once.src, err); + resolve(err) + return + } else { + ftp.put(data, once.dst, false, resolve) + } + }); }), this.sleep(20000)]); } diff --git a/packages/update-manager/js/FileUtils.js b/packages/update-manager/js/FileUtils.js index 4461117..6992f07 100644 --- a/packages/update-manager/js/FileUtils.js +++ b/packages/update-manager/js/FileUtils.js @@ -34,15 +34,13 @@ class FileUtil { * @param src * @param dst */ - copy(src, dst) { + async copy(src, dst) { const st = fs.statSync(src); if (st.isFile()) { - var readable = fs.createReadStream(src);//创建读取流 - var writable = fs.createWriteStream(dst);//创建写入流 + const readable = fs.createReadStream(src);//创建读取流 + const writable = fs.createWriteStream(dst);//创建写入流 readable.pipe(writable); return; - } else if (!st.isDirectory()) { - return; } if (!fs.existsSync(dst)) { fs.mkdirSync(dst, {recursive: true}); @@ -56,7 +54,6 @@ class FileUtil { write(filePath, content) { const pathInfo = path.parse(filePath); - // console.log('写文件:', filePath, pathInfo.dir) if (!fs.existsSync(pathInfo.dir)) { fs.mkdirSync(pathInfo.dir, {recursive: true}); } diff --git a/packages/update-manager/lib/UpdateManager.js b/packages/update-manager/lib/UpdateManager.js new file mode 100644 index 0000000..cb5be34 --- /dev/null +++ b/packages/update-manager/lib/UpdateManager.js @@ -0,0 +1,129 @@ +/** + * 方便直接获取当前版本号等信息. + * 使用方式: + * // @ts-ignore + * import UpdateManager = require( "UpdateManager"); + * 然后在代码中直接使用即可. + */ +class UpdateManager { + + cur_ver_code = 0; + cur_ver_name = "1.0.1"; + cur_ver_desc = ""; + ver_type = "dev"; + + new_ver_code = 0; + new_ver_name = "1.0.1"; + new_ver_desc = ""; + new_ver_info = {}; + + // 记录当前版本的bundles. 如果有新版本, 则自动下载新bundles. + cur_bundles = {}; + + updateSuccess = "0"; + + constructor() { + const curVer = window.localStorage.getItem("cur_ver_info"); + if (curVer) { + const verInfo = JSON.parse(curVer); + this.new_ver_code = this.cur_ver_code = verInfo.versionCode; + this.new_ver_name = this.cur_ver_name = verInfo.versionName; + this.ver_type = verInfo.versionType; + this.new_ver_desc = this.cur_ver_desc = verInfo.versionLog; + this.new_ver_info = verInfo; + this.cur_bundles = verInfo.bundleVers; + } + + this.updateSuccess = window.localStorage.getItem("ver_updated") || 0; + this.fetchNewVerInfo(); + } + + // 此方法适用于非强制更新包. 需要设计信息界面,显示版本信息. + // 手动调用进行更新下载, 下载完成返回true, 此时需要重启游戏才能应用更新. 需要界面提示用户. + doUpdate() { + if (this.hasNewVersion()) { + window.localStorage.setItem('cur_ver_info', JSON.stringify(this.new_ver_info)); + return true; + } + return false; + } + + resetUpdateFlag() { + window.localStorage.setItem('ver_updated', '0'); + } + + getUpdateFlag() { + return this.updateSuccess == 1; + } + + // 检测是否有新版本. + hasNewVersion() { + return this.new_ver_info && this.new_ver_info.versionCode && this.new_ver_info.versionCode != this.cur_ver_code; + } + + fetchNewVerInfo() { + let url = window.updateUrl || false; + if (!url) { + return; + } + // 游戏启动后再请求更新,避免影响启动速度. + url += `?_t=${Math.random()}`; + cc.log("请求更新地址:", url); + this._get(url).then(version => { + if (!version) { + console.log("未检测到更新版本内容"); + return; + } + if (version) { + cc.log('当前版本号:', this.cur_ver_code, this.cur_ver_name); + cc.log('请求更新内容:', version); + let verInfo = version; + if (typeof version === 'string') { + verInfo = JSON.parse(version); + } + this.new_ver_code = verInfo.versionCode; + this.new_ver_name = verInfo.versionName; + this.new_ver_desc = verInfo.versionLog; + + this.new_ver_info = verInfo; + + // 如果首次运行,本地未缓存版本信息,则进行缓存. + if (!this.cur_ver_code) { + cc.log("当前首次运行,记录版本信息"); + window.localStorage.setItem('cur_ver_info', JSON.stringify(this.new_ver_info)); + + this.new_ver_code = this.cur_ver_code = verInfo.versionCode; + this.new_ver_name = this.cur_ver_name = verInfo.versionName; + this.ver_type = verInfo.versionType; + this.new_ver_desc = this.cur_ver_desc = verInfo.versionLog; + + } else if (this.hasNewVersion() && verInfo.forceUpdate) { + window.localStorage.setItem('cur_ver_info', JSON.stringify(this.new_ver_info)); + window.localStorage.setItem('ver_updated', '1'); + cc.log("有新版本,且默认强制更新"); + } + } + }); + } + + // ajax 请求. + _get(url) { + return new Promise(resolve => { + const ajax = new XMLHttpRequest(); + ajax.open("get", url, true); + ajax.setRequestHeader("Content-Type", "application/json;charset=utf-8"); + ajax.onreadystatechange = function () { + if (ajax.readyState == 4) { + if (ajax.status == 200) { + resolve(ajax.responseText); + } else { + resolve(null); + } + } + } + ajax.send(null); + }); + } +} + +module.exports = new UpdateManager(); \ No newline at end of file diff --git a/packages/update-manager/lib/UpdateManager.js.meta b/packages/update-manager/lib/UpdateManager.js.meta new file mode 100644 index 0000000..7104b18 --- /dev/null +++ b/packages/update-manager/lib/UpdateManager.js.meta @@ -0,0 +1,9 @@ +{ + "ver": "1.0.8", + "uuid": "aa7483f4-ac0c-4809-9c5e-4e5734a26db6", + "isPlugin": false, + "loadPluginInWeb": true, + "loadPluginInNative": true, + "loadPluginInEditor": false, + "subMetas": {} +} \ No newline at end of file diff --git a/packages/update-manager/main.js b/packages/update-manager/main.js index ea739fb..b0c3d9c 100644 --- a/packages/update-manager/main.js +++ b/packages/update-manager/main.js @@ -45,9 +45,12 @@ const saveNewVersionInfo = function (opt, bundleVers, configJson) { const data = { versionCode: configJson.versionCfg.versionCode, versionName: configJson.versionCfg.versionName, + versionType: configJson.versionCfg.versionType, + versionLog: configJson.versionCfg.versionLog, + forceUpdate: configJson.versionCfg.forceUpdate, server: `${configJson.versionCfg.baseUrl}${configJson.ftpCfg.rootPath}${configJson.versionCfg.versionType}/${configJson.versionCfg.versionCode}` }; - const updateInfo = Object.assign(data, {bundles: bundleVers}) + const updateInfo = Object.assign(data, {bundles: bundleVers}); // 将版本更新信息写入文件 let targetUpdatePath = join(opt.dest, `remote/update-${configJson.versionCfg.versionType}.json`) FileUtil.write(targetUpdatePath, JSON.stringify(updateInfo, null, 2)); @@ -84,7 +87,7 @@ module.exports = { Editor.Builder.removeListener('build-finished', doMakeUpdatePackage); if (this.ftp) { this.ftp.destroy(); - this.ftp = null; + this.ftp = null; } }, messages: { diff --git a/packages/update-manager/package.json b/packages/update-manager/package.json index 5e13ce5..88d56e3 100644 --- a/packages/update-manager/package.json +++ b/packages/update-manager/package.json @@ -9,6 +9,15 @@ "message": "update-manager:open" } }, + "runtime-resource": { + "path": "./lib", + "name": "lib" + }, + "reload": { + "ignore": [ + "lib/**/*" + ] + }, "panel": { "main": "panel/index.html", "type": "simple", diff --git a/packages/update-manager/panel/index.html b/packages/update-manager/panel/index.html index 5f7aee3..be79522 100644 --- a/packages/update-manager/panel/index.html +++ b/packages/update-manager/panel/index.html @@ -28,21 +28,14 @@ - - - - - - - - - - - - - - - + + + + +