[√]cocos creator 热更新源码剖析(3)

简介: [√]cocos creator 热更新源码剖析

Downloader

几个比较重要的下载回调

  • onDataTaskSuccess
  • onFileTaskSuccess
  • onTaskProgress
  • onTaskError

开启了一个线程进行下载,使用到了curl,每个下载文件都会创建一个DownloadTask

manifest下载失败的处理

void AssetsManagerEx::onError(const network::DownloadTask& task,
                              int errorCode,
                              int errorCodeInternal,
                              const std::string& errorStr)
{
    if (task.identifier == VERSION_ID)
    {
        // 当收到下载version.manifest失败后,会尝试下载project.manifest
        CCLOG("AssetsManagerEx : Fail to download version file, step skipped\n");
        _updateState = State::PREDOWNLOAD_MANIFEST;
        downloadManifest();
    }
    else if (task.identifier == MANIFEST_ID)
    {
       // 当收到project.manifest也下载失败了,就会抛出异常 
        dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ERROR_DOWNLOAD_MANIFEST, task.identifier, errorStr, errorCode, errorCodeInternal);
        _updateState = State::FAIL_TO_UPDATE;
    }
    else
    {
        if (_downloadingTask.find(task.identifier) != _downloadingTask.end()) {
            _downloadingTask.erase(task.identifier);
        }
        // 热更过程中下载失败
        fileError(task.identifier, errorStr, errorCode, errorCodeInternal);
    }
}

update

开始更新下载需要的文件

void AssetsManagerEx::update()
{
    // .. 一些错误检查
    _updateEntry = UpdateEntry::DO_UPDATE;
    switch (_updateState) {
        case State::FAIL_TO_UPDATE:
        case State::READY_TO_UPDATE:
        case State::NEED_UPDATE:
        {
            if (_updateEntry == UpdateEntry::DO_UPDATE)
            {
                // 一般正常流程都会命中这个逻辑
                startUpdate();
            }
        }
            break;
    }
}
void AssetsManagerEx::startUpdate()
{
    if (_updateState == State::READY_TO_UPDATE)
    {
        // Start to update 6 files from remote package.
        msg = StringUtils::format("Start to update %d files from remote package.", _totalToDownload);
        dispatchUpdateEvent(EventAssetsManagerEx::EventCode::UPDATE_PROGRESSION, "", msg);
        batchDownload(); // 批量创建下载任务队列
    }
}

更新成功

触发事件UPDATE_FINISHED,js层收到后软重启即可

cc.audioEngine.stopAll();
cc.game.restart();
Uncaught TypeError: Cannot read property 'emit' of null, location: src/cocos2d-jsb.js:0:0
STACK:
[0]_onTouchEnded@src/cocos2d-jsb.js:25740
[1]253.proto.emit@src/cocos2d-jsb.js:41837
[2]_doDispatchEvent@src/cocos2d-jsb.js:15311
[3]dispatchEvent@src/cocos2d-jsb.js:16193
[4]_touchEndHandler@src/cocos2d-jsb.js:15200
[5]_onTouchEventCallback@src/cocos2d-jsb.js:32750
[6]_dispatchEventToListeners@src/cocos2d-jsb.js:32836
[7]_dispatchTouchEvent@src/cocos2d-jsb.js:32785
[8]dispatchEvent@src/cocos2d-jsb.js:33037
[9]handleTouchesEnd@src/cocos2d-jsb.js:39827
[10]_mouseEventsOnElement@src/cocos2d-jsb.js:39986
[11]anonymous@src/cocos2d-jsb.js:40005
[12]dispatchEvent@jsb-adapter/jsb-builtin.js:3077
[13]anonymous@jsb-adapter/jsb-builtin.js:3149
_onTouchEnded: function _onTouchEnded(event) {
    if (!this.interactable || !this.enabledInHierarchy) return;
    if (this._pressed) {
      cc.Component.EventHandler.emitEvents(this.clickEvents, event);
      this.node.emit("click", this); // 这里报的错
    }
    this._pressed = false;
    this._updateState();
    event.stopPropagation();
},
 ERROR: Uncaught TypeError: Cannot read property 'length' of undefined, location: src/cocos2d-jsb.js:0:0
STACK:
[0]lookupClasses@src/cocos2d-jsb.js:42198
[1]unpackJSONs@src/cocos2d-jsb.js:42274
[2]unpackJson@src/cocos2d-jsb.js:19647
[3]unpack@src/cocos2d-jsb.js:19680
[4]anonymous@src/cocos2d-jsb.js:19703
[5]finale@src/cocos2d-jsb.js:18985
[6]anonymous@src/cocos2d-jsb.js:20636
[7]anonymous@src/cocos2d-jsb.js:18962
[8]anonymous@jsb-adapter/jsb-engine.js:3183
[9]readFile@jsb-adapter/jsb-engine.js:3149
[10]readJson@jsb-adapter/jsb-engine.js:3171
[11]parseJson@jsb-adapter/jsb-engine.js:3468
[12]download@jsb-adapter/jsb-engine.js:3354
[13]downloadJson@jsb-adapter/jsb-engine.js:3480
[14]invoke@src/cocos2d-jsb.js:18956
[15]process@src/cocos2d-jsb.js:18966
[16]retry@src/cocos2d-jsb.js:20634
[17]download@src/cocos2d-jsb.js:18990
[18]load@src/cocos2d-jsb.js:19700
[19]fetch@src/cocos2d-jsb.js:19514

事件ID

enum class EventCode
{
    ERROR_NO_LOCAL_MANIFEST,
    ERROR_DOWNLOAD_MANIFEST,
    ERROR_PARSE_MANIFEST,
    NEW_VERSION_FOUND,
    ALREADY_UP_TO_DATE,
    UPDATE_PROGRESSION,
    ASSET_UPDATED,
    ERROR_UPDATING,
    UPDATE_FINISHED,
    UPDATE_FAILED,
    ERROR_DECOMPRESS
};

image.png

目录
相关文章
|
6月前
|
XML 存储 JSON
CocosCreator 面试题(十五)Cocos Creator如何内置protobuf JS版本?
CocosCreator 面试题(十五)Cocos Creator如何内置protobuf JS版本?
177 0
|
缓存 数据格式 异构计算
creator源码阅读系列之第三篇
creator源码阅读系列之第三篇
111 0
|
6月前
|
图形学
cocos creator DragonBones 源码阅读
cocos creator DragonBones 源码阅读
68 0
|
JavaScript C++
[√]cocos creator 热更新源码剖析(1)
[√]cocos creator 热更新源码剖析
155 1
|
JavaScript C++
[√]cocos creator 热更新源码剖析(2)
[√]cocos creator 热更新源码剖析(2)
100 1
|
6月前
|
JavaScript 前端开发 Java
CocosCreator 面试题(十)Cocos Creator 内存管理
CocosCreator 面试题(十)Cocos Creator 内存管理
390 0
|
6月前
|
编解码 前端开发 UED
CocosCreator 面试题(十一)Cocos Creator 屏幕适配
CocosCreator 面试题(十一)Cocos Creator 屏幕适配
265 0
|
C语言 C++
[√]luahelper为什么无法调试cocos2dx lua
[√]luahelper为什么无法调试cocos2dx lua
113 0
Cocos Creator3.8 项目实战(二)cocos creator编辑器中绑定事件引发的bug解决
Cocos Creator3.8 项目实战(二)cocos creator编辑器中绑定事件引发的bug解决
138 0
Cocos Creator 解决热更新资源md5比较引发卡顿问题(二)
Cocos Creator 解决热更新资源md5比较引发卡顿问题