能走 更新检查回调 checkcb 成功对比出版本号,并且能下载远程文件,也可以检查下载的文件 setVerifyCallback。
代码如下:
const {ccclass, property} = cc._decorator;
@ccclass
export default class Helloworld extends cc.Component {
@property(cc.RichText)
label: cc.RichText = null;
@property(cc.RichText)
updateting: cc.RichText = null;
@property
text: string = 'hello';
_storagePath:string = "";
_am:jsb.AssetsManager = null;
_updating:boolean = false;
@property(cc.Asset)
mainifestUrl:cc.Asset = null;
start () {
// init logic
this.label.string = this.text;
}
onLoad() {
var self = this;
this._storagePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + 'blackjack-remote-asset');
cc.log('Storage path for remote asset : ' + this._storagePath);
this._am = new jsb.AssetsManager('', this._storagePath, this.versionCompareHanle);
this._am.setVerifyCallback(function (path, asset) {
var compressed = asset.compressed;
var expectedMD5 = asset.md5;
var relativePath = asset.path;
var size = asset.size;
if (compressed) {
self .label.string = "Verification passed : " + relativePath;
return true;
}
else {
self .label.string = "Verification passed : " + relativePath + ' (' + expectedMD5 + ')';
return true;
}
});
}
/**比较版本 */
versionCompareHanle( versionA : string , versionB : string ){
console.log(`当前版本 : ${versionA} , 远程版本 : ${versionB}`);
let vA = versionA.split('.');
let vB = versionB.split('.');
for( let i = 0 ; i < vA.length && i < vB.length ; ++i ){
let a = parseInt(vA[i]);
let b = parseInt(vB[i]);
if ( a === b ){
continue;
}
else{
return a - b;
}
}
if ( vB.length > vA.length){
return -1;
}
return 0;
}
hotUpdate() {
this.updateting.string = this._updating+"";
if (this._am && !this._updating) {
this._am.setEventCallback(this.updateCb);
if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
let url = this.mainifestUrl.nativeUrl;
if (cc.loader.md5Pipe) {
url = cc.loader.md5Pipe.transformURL(url);
}
this._am.loadLocalManifest(url);
}
this._am.update();
this._updating = true;
}
}
checkUpdate() {
if (this._updating) {
this.label.string = 'Checking or updating ...';
return;
}
let url = this.mainifestUrl.nativeUrl;
if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
if (cc.loader.md5Pipe) {
url = cc.loader.md5Pipe.transformURL(url);
}
this._am.loadLocalManifest(url);
}
if (!this._am.getLocalManifest() || !this._am.getLocalManifest().isLoaded()) {
this.label.string = 'Failed to load local manifest ...';
return;
}
this._am.setEventCallback(this.checkCb.bind(this));
this._am.checkUpdate();
this._updating = true;
}
changeScene() {
cc.director.loadScene('helloworld');
}
checkCb(event) {
cc.log('Code: ' + event.getEventCode());
switch (event.getEventCode()) {
case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
this.label.string = "No local manifest file found, hot update skipped.";
break;
case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
this.label.string = "Fail to download manifest file, hot update skipped.";
break;
case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
this.label.string = "Already up to date with the latest remote version.";
this.changeScene();
break;
case jsb.EventAssetsManager.NEW_VERSION_FOUND:
this.label.string = 'New version found, please try to update.';
this.hotUpdate();
break;
default:
return;
}
this._am.setEventCallback(null);
this._updating = false;
}
updateCb(event) {
console.log("updatecb")
var needRestart = false;
var failed = false;
let mm = event.getMessage();
if (mm) {
this.label.string = 'Updated file: ' + mm;
}
switch (event.getEventCode()) {
case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
this.label.string = 'No local manifest file found, hot update skipped.';
failed = true;
break;
case jsb.EventAssetsManager.UPDATE_PROGRESSION:
// this.byteProgress.progress = event.getPercent();
// this.fileProgress.progress = event.getPercentByFile();
// this.fileLabel.string = event.getDownloadedFiles() + ' / ' + event.getTotalFiles();
// this.byteLabel.string = event.getDownloadedBytes() + ' / ' + event.getTotalBytes();
console.log("curfile",event.getDownloadedFiles())
console.log("totalbyt",event.getTotalFiles())
var msg = event.getMessage();
if (msg) {
this.label.string = 'Updated file: ' + msg;
}
break;
case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
this.label.string = 'Fail to download manifest file, hot update skipped.';
failed = true;
break;
case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
this.label.string = 'Already up to date with the latest remote version.';
failed = true;
break;
case jsb.EventAssetsManager.UPDATE_FINISHED:
this.label.string = 'Update finished. ' + event.getMessage();
needRestart = true;
break;
case jsb.EventAssetsManager.UPDATE_FAILED:
this.label.string = 'Update failed. ' + event.getMessage();
this._updating = false;
// this._canRetry = true;
break;
case jsb.EventAssetsManager.ERROR_UPDATING:
this.label.string = 'Asset update error: ' + event.getAssetId() + ', ' + event.getMessage();
break;
case jsb.EventAssetsManager.ERROR_DECOMPRESS:
this.label.string = event.getMessage();
break;
default:
break;
}
if (failed) {
this._updating = false;
}
if (needRestart) {
this._am.setEventCallback(null);
var searchPaths = jsb.fileUtils.getSearchPaths();
var newPaths = this._am.getLocalManifest().getSearchPaths();
Array.prototype.unshift(searchPaths, newPaths);
cc.sys.localStorage.setItem('HotUpdateSearchPaths', JSON.stringify(searchPaths));
jsb.fileUtils.setSearchPaths(searchPaths);
cc.game.restart();
}
}
}