mirror of
https://github.com/Leo501/CocosCreatorTutorial.git
synced 2026-06-01 06:59:21 +08:00
完善热更新组件,添加验证函数。致此热更新已十分完善,可直接使用
This commit is contained in:
@@ -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));
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user