AssertManager---热更新

这个是cocos官方的流程图,拿来借用一下:

AssertManager 下载更新流程:

1,AssetsManagerEx()构造函数

   _storagePath = storagePath;           ----文件下载路径
    adjustPath(_storagePath);
    _fileUtils->createDirectory(_storagePath);
    
    _tempStoragePath = _storagePath; 
    _tempStoragePath.insert(_storagePath.size() - 1, TEMP_PACKAGE_SUFFIX);
    _fileUtils->createDirectory(_tempStoragePath);
    _tempVersionPath = _tempStoragePath + VERSION_FILENAME; ---下载服务器中version.manifest,并保存
    _cacheManifestPath = _storagePath + MANIFEST_FILENAME;
    _tempManifestPath = _tempStoragePath + TEMP_MANIFEST_FILENAME;---下载服务器中project.manifest,并保存

创建了相应的资源下载目录,临时文件目录

2,loadLocalManifest(manifestUrl);------manifestUrl 为本地保留的一份manifest文件

{ 
    "packageUrl" : "http://192.168.1.11:8000/res", 
    "remoteManifestUrl" : "http://192.168.1.11:8000/res/project.manifest", 
    "remoteVersionUrl" : "http://192.168.1.11:8000/res/version.manifest", 
    "version" : "1.0.1", 
    "engineVersion" : "3.0 rc0", 
    "assets" : {

    },

    "searchPaths" : [ 
    ] 
}

 // Load local manifest in app package
    _localManifest->parse(_manifestUrl); ----------解析本地manifest中的数据,并保存。

3,AssetsManagerEx::downloadVersion();---下载version.manifest

通过_localManifest解析到的remoteVersionUrl下载服务器端配置的version.manifest.到_tempVersionPath,再拿这个进行版本对比,是否要更新下载。_remoteManifest->parseVersion(_tempVersionPath);解析version.manifest文件,

_downloader->createDownloadFileTask(versionUrl, _tempVersionPath, VERSION_ID);

if (_localManifest->versionGreater(_remoteManifest, _versionCompareHandle))
        {
            _updateState = State::UP_TO_DATE;
            _fileUtils->removeDirectory(_tempStoragePath);
            dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ALREADY_UP_TO_DATE);
        }
        else
        {
            _updateState = State::NEED_UPDATE;

            // Wait to update so continue the process
            if (_updateEntry == UpdateEntry::DO_UPDATE)
            {
                // dispatch after checking update entry because event dispatching may modify the update entry
                dispatchUpdateEvent(EventAssetsManagerEx::EventCode::NEW_VERSION_FOUND);
                _updateState = State::PREDOWNLOAD_MANIFEST;
                downloadManifest();
            }
            else
            {
                dispatchUpdateEvent(EventAssetsManagerEx::EventCode::NEW_VERSION_FOUND);
            }
        }
    }

4,AssetsManagerEx::downloadManifest()-------下载project.manifest

通过_remoteManifest-解析到的remoteVersionUrl,下载project.manifest,并保存在_tempManifestPath中。

_downloader->createDownloadFileTask(manifestUrl, _tempManifestPath, MANIFEST_ID);

 _remoteManifest->parse(_tempManifestPath);---解析project.manifest

 if (_localManifest->versionGreater(_remoteManifest, _versionCompareHandle))
        {
            _updateState = State::UP_TO_DATE;
            _fileUtils->removeDirectory(_tempStoragePath);
            dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ALREADY_UP_TO_DATE);
        }
        else
        {
            _updateState = State::NEED_UPDATE;
            dispatchUpdateEvent(EventAssetsManagerEx::EventCode::NEW_VERSION_FOUND);

            if (_updateEntry == UpdateEntry::DO_UPDATE)
            {
                startUpdate();
            }
        }

5,开始下载任务

  // Check difference between local manifest and remote manifest

==========客户端做差异增量,也可以放到服务器中做,

对比本地localmanifest和服务器下载 到的_remoteManifest中,asset资源的md5值,获取不同的资源diff_map 。
 std::unordered_map<std::string, Manifest::AssetDiff> diff_map = _localManifest->genDiff(_remoteManifest);

对新增差异的文件提供下载地址下载

       for (auto it = diff_map.begin(); it != diff_map.end(); ++it)
            {
                Manifest::AssetDiff diff = it->second;
                if (diff.type != Manifest::DiffType::DELETED)
                {
                    std::string path = diff.asset.path;
                    DownloadUnit unit;
                    unit.customId = it->first;
                    unit.srcUrl = packageUrl + path;
                    unit.storagePath = _tempStoragePath + path;
                    unit.size = diff.asset.size;
                    _downloadUnits.emplace(unit.customId, unit);
                    _tempManifest->setAssetDownloadState(it->first, Manifest::

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值