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);