1.version.manifest
版本配置主要是用于快速确定是否需要更新,如果project.manifest很大的话这个是很有必要的。
{ // assets manager从这里下载所有的assets "packageUrl" : "http://example.com/assets_manager/TestScene/",// version.manifest "remoteVersionUrl" : "http://example.com/assets_manager/TestScene/version.manifest",// project.manifest "remoteManifestUrl" : "http://example.com/assets_manager/TestScene/project.manifest",// 本mani文件的版本 "version" : "1.0.0",// 组版本 "groupVersions": { "1" : "1.0.22" } "engineVersion" : "Cocos2d-JS v3.0 RC0" }比较版本的方式是:version字段必须相等,然后比较groupVersions的所有字段(字段总数及对应的值)必须等。具体代码是:
bool Manifest::versionEquals(const Manifest *b) const { // Check manifest version if (_version != b->getVersion()) { return false; } // Check group versions else { std::vector<std::string> bGroups = b->getGroups(); std::unordered_map<std::string,std::string> bGroupVer = b->getGroupVerions(); // Check group size if (bGroups.size() != _groups.size()) return false; // Check groups version for (int i = 0; i < _groups.size(); ++i) { std::string gid =_groups[i]; // Check group name if (gid != bGroups[i]) return false; // Check group version if (_groupVer.at(gid) != bGroupVer.at(gid)) return false; } } return true; }
2.project.manifest
apk里的project.manifest会和下载的project.manifest(如果更新过的话会cache在下载目录的)比较version,选择version大的版本。所以这里介绍服务器的project.manifest配置。
其他的和上面的一样,只是新加了assets和searchPaths字段,用于配置更新的文件及要设置的搜索目录。
{ "packageUrl" : "http://192.168.1.103/bw","remoteManifestUrl" : "http://192.168.1.103/bw/project.manifest","remoteVersionUrl" : "http://192.168.1.103/bw/version.manifest","version" : "1.0.22","groupVersions" : { "1" : "1.0.22" },"engineVersion" : "3.0 rc0","assets" : { "app.zip" : { "md5" : "69361FEDE857D55A627B997828D63A0E","compressed" : true,"group" : "1" } },"searchPaths" : [ ] }md5表示asset的版本信息,compressed可选,如果压缩了则下载后会被解压。group值在现在的cocos2dx 3.3里也没有用到(可能后边会用吧),所以暂且可以不配置。
而客户端更新文件的方式是,比较本地(或cache过的)和从服务器下载的project.manifest文件的差值,然后进行更新。具体算法是比较assets的所有值,减少的就删除,
新增了就添加,md5变了就修改。所以目前没有用到group字段,当然你可以用来自己配置的时候区分各个组。具体比较代码:
std::unordered_map<std::string,Manifest::AssetDiff> Manifest::genDiff(const Manifest *b) const { std::unordered_map<std::string,AssetDiff> diff_map; std::unordered_map<std::string,Asset> bAssets = b->getAssets(); std::string key; Asset valueA; Asset valueB; std::unordered_map<std::string,Asset>::const_iterator valueIt,it; for (it = _assets.begin(); it != _assets.end(); ++it) { key = it->first; valueA = it->second; // Deleted valueIt = bAssets.find(key); if (valueIt == bAssets.cend()) { AssetDiff diff; diff.asset = valueA; diff.type = DiffType::DELETED; diff_map.emplace(key,diff); continue; } // Modified valueB = valueIt->second; if (valueA.md5 != valueB.md5) { AssetDiff diff; diff.asset = valueB; diff.type = DiffType::MODIFIED; diff_map.emplace(key,diff); } } for (it = bAssets.begin(); it != bAssets.end(); ++it) { key = it->first; valueB = it->second; // Added valueIt = _assets.find(key); if (valueIt == _assets.cend()) { AssetDiff diff; diff.asset = valueB; diff.type = DiffType::ADDED; diff_map.emplace(key,diff); } } return diff_map; }