From 4fa8af43a1aa951bec4877f1c03ad374c9d085c2 Mon Sep 17 00:00:00 2001 From: leo <907600065@qq.com> Date: Fri, 11 Jan 2019 10:37:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=83=AD=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E3=80=82=E8=87=B4=E6=AD=A4=E7=83=AD=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=B7=B2=E5=8D=81=E5=88=86=E5=AE=8C=E5=96=84=EF=BC=8C?= =?UTF-8?q?=E5=8F=AF=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HotUpdateDemo/assets/Script/HotUpdate.js | 23 +++++++++++++++++++++-- HotUpdateDemo/version_generator.js | 12 ++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/HotUpdateDemo/assets/Script/HotUpdate.js b/HotUpdateDemo/assets/Script/HotUpdate.js index bae01d0..097704b 100644 --- a/HotUpdateDemo/assets/Script/HotUpdate.js +++ b/HotUpdateDemo/assets/Script/HotUpdate.js @@ -1,7 +1,12 @@ +/** + * 参考文章 + * https://www.cnblogs.com/pixs-union/p/9435882.html + */ const ErrCode = cc.Enum({ laodManifestFailed: 0, //下载manifest失败 updateFailed: 1, //更新失败, }); +const MD5 = require("Uint8ArrayMD5"); cc.Class({ extends: cc.Component, @@ -56,11 +61,26 @@ cc.Class({ } this._am.setVerifyCallback(function (path, asset) { + console.log('path', path, 'setVerifyCallback' + JSON.stringify(asset)); var compressed = asset.compressed; + /** + * 计算md5 + * @param {*} filePath + */ + let calMD5OfFile = function (filePath) { + return MD5(jsb.fileUtils.getDataFromFile(filePath)); + }; + if (compressed) { return true; } else { - return true; + var resMD5 = calMD5OfFile(path); + console.log('resMD%=', resMD5, 'asset md5=', asset.md5); + if (asset.md5 == resMD5) { + return true; + } + jsb.fileUtils.removeFile(path); + return false; } }); @@ -134,7 +154,6 @@ cc.Class({ if (!this._am.getLocalManifest() || !this._am.getLocalManifest().isLoaded()) { this.onFailure(ErrCode.laodManifestFailed); - // this.panel.info.string = 'Failed to load local manifest ...'; return; } this._checkListener = new jsb.EventListenerAssetsManager(this._am, this.checkCb.bind(this)); diff --git a/HotUpdateDemo/version_generator.js b/HotUpdateDemo/version_generator.js index dd789c4..144b715 100644 --- a/HotUpdateDemo/version_generator.js +++ b/HotUpdateDemo/version_generator.js @@ -31,6 +31,13 @@ while (i < process.argv.length) { case '--version': case '-v': manifest.version = process.argv[i + 1]; + var versionPath = "./config.json"; + var data = fs.readFileSync(versionPath); + let param = JSON.parse(data.toString()); + if (param) { + manifest.version = param.game_version; + }; + console.log('version=', manifest.version); i += 2; break; case '--src': @@ -38,7 +45,6 @@ while (i < process.argv.length) { src = process.argv[i + 1]; hotDir = src + 'hotUpdate'; console.log('hotDir=', hotDir); - i += 2; break; case '--dest': @@ -75,7 +81,9 @@ function readDir(dir, obj) { } else if (stat.isFile()) { // Size in Bytes size = stat['size']; - md5 = crypto.createHash('md5').update(fs.readFileSync(subpath, 'binary')).digest('hex'); + console.log('md5 ',subpath); + // md5 = crypto.createHash('md5').update(fs.readFileSync(subpath, 'binary')).digest('hex');//返回的并非二进制类型,而是String。这会导致非文本文件md5计算错误 + md5 = crypto.createHash('md5').update(fs.readFileSync(subpath)).digest('hex');// compressed = path.extname(subpath).toLowerCase() === '.zip'; relative = path.relative(src, subpath);