这个是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::