axmol/extensions/assets-manager/Manifest.cpp

566 lines
15 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2014 cocos2d-x.org
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
http://www.cocos2d-x.org
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "Manifest.h"
2020-05-06 15:42:25 +08:00
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"
2019-11-23 20:27:39 +08:00
#include <fstream>
#include <stdio.h>
2021-12-25 10:04:45 +08:00
#define KEY_VERSION "version"
#define KEY_PACKAGE_URL "packageUrl"
#define KEY_MANIFEST_URL "remoteManifestUrl"
#define KEY_VERSION_URL "remoteVersionUrl"
#define KEY_GROUP_VERSIONS "groupVersions"
#define KEY_ENGINE_VERSION "engineVersion"
#define KEY_ASSETS "assets"
#define KEY_COMPRESSED_FILES "compressedFiles"
#define KEY_SEARCH_PATHS "searchPaths"
#define KEY_PATH "path"
#define KEY_MD5 "md5"
#define KEY_GROUP "group"
#define KEY_COMPRESSED "compressed"
#define KEY_SIZE "size"
#define KEY_COMPRESSED_FILE "compressedFile"
#define KEY_DOWNLOAD_STATE "downloadState"
2019-11-23 20:27:39 +08:00
NS_CC_EXT_BEGIN
static int cmpVersion(const std::string& v1, const std::string& v2)
2019-11-23 20:27:39 +08:00
{
int i;
int oct_v1[4] = {0}, oct_v2[4] = {0};
int filled1 = std::sscanf(v1.c_str(), "%d.%d.%d.%d", &oct_v1[0], &oct_v1[1], &oct_v1[2], &oct_v1[3]);
int filled2 = std::sscanf(v2.c_str(), "%d.%d.%d.%d", &oct_v2[0], &oct_v2[1], &oct_v2[2], &oct_v2[3]);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (filled1 == 0 || filled2 == 0)
{
return strcmp(v1.c_str(), v2.c_str());
2019-11-23 20:27:39 +08:00
}
for (i = 0; i < 4; i++)
{
if (oct_v1[i] > oct_v2[i])
return 1;
else if (oct_v1[i] < oct_v2[i])
return -1;
}
return 0;
}
Manifest::Manifest(const std::string& manifestUrl /* = ""*/)
2021-12-25 10:04:45 +08:00
: _versionLoaded(false)
, _loaded(false)
, _manifestRoot("")
, _remoteManifestUrl("")
, _remoteVersionUrl("")
, _version("")
, _engineVer("")
2019-11-23 20:27:39 +08:00
{
// Init variables
_fileUtils = FileUtils::getInstance();
if (!manifestUrl.empty())
2019-11-23 20:27:39 +08:00
parse(manifestUrl);
}
void Manifest::loadJson(const std::string& url)
2019-11-23 20:27:39 +08:00
{
clear();
std::string content;
if (_fileUtils->isFileExist(url))
{
// Load file content
content = _fileUtils->getStringFromFile(url);
2021-12-25 10:04:45 +08:00
if (content.empty())
2019-11-23 20:27:39 +08:00
{
CCLOG("Fail to retrieve local file content: %s\n", url.c_str());
2019-11-23 20:27:39 +08:00
}
else
{
// Parse file with rapid json
_json.Parse<0>(content.c_str());
// Print error
2021-12-25 10:04:45 +08:00
if (_json.HasParseError())
{
2019-11-23 20:27:39 +08:00
size_t offset = _json.GetErrorOffset();
if (offset > 0)
offset--;
std::string errorSnippet = content.substr(offset, 10);
CCLOG("File parse error %d at <%s>\n", _json.GetParseError(), errorSnippet.c_str());
}
}
}
}
void Manifest::parseVersion(const std::string& versionUrl)
2019-11-23 20:27:39 +08:00
{
loadJson(versionUrl);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_json.IsObject())
{
loadVersion(_json);
}
}
void Manifest::parse(const std::string& manifestUrl)
2019-11-23 20:27:39 +08:00
{
loadJson(manifestUrl);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (!_json.HasParseError() && _json.IsObject())
{
// Register the local manifest root
size_t found = manifestUrl.find_last_of("/\\");
if (found != std::string::npos)
{
2021-12-25 10:04:45 +08:00
_manifestRoot = manifestUrl.substr(0, found + 1);
2019-11-23 20:27:39 +08:00
}
loadManifest(_json);
}
}
bool Manifest::isVersionLoaded() const
{
return _versionLoaded;
}
bool Manifest::isLoaded() const
{
return _loaded;
}
2021-12-25 10:04:45 +08:00
bool Manifest::versionEquals(const Manifest* b) const
2019-11-23 20:27:39 +08:00
{
// 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();
2019-11-23 20:27:39 +08:00
// Check group size
if (bGroups.size() != _groups.size())
return false;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Check groups version
2021-12-25 10:04:45 +08:00
for (unsigned int i = 0; i < _groups.size(); ++i)
{
std::string gid = _groups[i];
2019-11-23 20:27:39 +08:00
// Check group name
if (gid != bGroups[i])
return false;
// Check group version
if (_groupVer.at(gid) != bGroupVer.at(gid))
return false;
}
}
return true;
}
2021-12-25 10:04:45 +08:00
bool Manifest::versionGreater(
const Manifest* b,
const std::function<int(const std::string& versionA, const std::string& versionB)>& handle) const
2019-11-23 20:27:39 +08:00
{
std::string localVersion = getVersion();
const std::string& bVersion = b->getVersion();
2019-11-23 20:27:39 +08:00
bool greater;
if (handle)
{
2020-11-19 02:43:49 +08:00
greater = handle(localVersion, bVersion) > 0;
2019-11-23 20:27:39 +08:00
}
else
{
2020-11-19 02:43:49 +08:00
greater = cmpVersion(localVersion, bVersion) > 0;
2019-11-23 20:27:39 +08:00
}
return greater;
}
std::unordered_map<std::string, Manifest::AssetDiff> Manifest::genDiff(const Manifest* b) const
2019-11-23 20:27:39 +08:00
{
std::unordered_map<std::string, AssetDiff> diff_map;
const std::unordered_map<std::string, Asset>& bAssets = b->getAssets();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
std::string key;
Asset valueA;
Asset valueB;
2021-12-25 10:04:45 +08:00
std::unordered_map<std::string, Asset>::const_iterator valueIt, it;
2019-11-23 20:27:39 +08:00
for (it = _assets.begin(); it != _assets.end(); ++it)
{
2021-12-25 10:04:45 +08:00
key = it->first;
2019-11-23 20:27:39 +08:00
valueA = it->second;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Deleted
valueIt = bAssets.find(key);
2021-12-25 10:04:45 +08:00
if (valueIt == bAssets.cend())
{
2019-11-23 20:27:39 +08:00
AssetDiff diff;
diff.asset = valueA;
2021-12-25 10:04:45 +08:00
diff.type = DiffType::DELETED;
2019-11-23 20:27:39 +08:00
diff_map.emplace(key, diff);
continue;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Modified
valueB = valueIt->second;
2021-12-25 10:04:45 +08:00
if (valueA.md5 != valueB.md5)
{
2019-11-23 20:27:39 +08:00
AssetDiff diff;
diff.asset = valueB;
2021-12-25 10:04:45 +08:00
diff.type = DiffType::MODIFIED;
2019-11-23 20:27:39 +08:00
diff_map.emplace(key, diff);
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
for (it = bAssets.begin(); it != bAssets.end(); ++it)
{
2021-12-25 10:04:45 +08:00
key = it->first;
2019-11-23 20:27:39 +08:00
valueB = it->second;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Added
valueIt = _assets.find(key);
2021-12-25 10:04:45 +08:00
if (valueIt == _assets.cend())
{
2019-11-23 20:27:39 +08:00
AssetDiff diff;
diff.asset = valueB;
2021-12-25 10:04:45 +08:00
diff.type = DiffType::ADDED;
2019-11-23 20:27:39 +08:00
diff_map.emplace(key, diff);
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return diff_map;
}
2021-12-25 10:04:45 +08:00
void Manifest::genResumeAssetsList(DownloadUnits* units) const
2019-11-23 20:27:39 +08:00
{
for (auto it = _assets.begin(); it != _assets.end(); ++it)
{
Asset asset = it->second;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (asset.downloadState != DownloadState::SUCCESSED && asset.downloadState != DownloadState::UNMARKED)
{
DownloadUnit unit;
2021-12-25 10:04:45 +08:00
unit.customId = it->first;
unit.srcUrl = _packageUrl + asset.path;
2019-11-23 20:27:39 +08:00
unit.storagePath = _manifestRoot + asset.path;
2021-12-25 10:04:45 +08:00
unit.size = asset.size;
2019-11-23 20:27:39 +08:00
units->emplace(unit.customId, unit);
}
}
}
std::vector<std::string> Manifest::getSearchPaths() const
{
std::vector<std::string> searchPaths;
searchPaths.push_back(_manifestRoot);
2021-12-25 10:04:45 +08:00
for (int i = (int)_searchPaths.size() - 1; i >= 0; i--)
2019-11-23 20:27:39 +08:00
{
std::string path = _searchPaths[i];
if (!path.empty() && path[path.size() - 1] != '/')
path.push_back('/');
2019-11-23 20:27:39 +08:00
path = _manifestRoot + path;
searchPaths.push_back(path);
}
return searchPaths;
}
void Manifest::prependSearchPaths()
{
2021-12-25 10:04:45 +08:00
std::vector<std::string> searchPaths = FileUtils::getInstance()->getSearchPaths();
2019-11-23 20:27:39 +08:00
std::vector<std::string>::iterator iter = searchPaths.begin();
2021-12-25 10:04:45 +08:00
bool needChangeSearchPaths = false;
2019-11-23 20:27:39 +08:00
if (std::find(searchPaths.begin(), searchPaths.end(), _manifestRoot) == searchPaths.end())
{
searchPaths.insert(iter, _manifestRoot);
needChangeSearchPaths = true;
}
2021-12-25 10:04:45 +08:00
for (int i = (int)_searchPaths.size() - 1; i >= 0; i--)
2019-11-23 20:27:39 +08:00
{
std::string path = _searchPaths[i];
if (!path.empty() && path[path.size() - 1] != '/')
path.push_back('/');
2019-11-23 20:27:39 +08:00
path = _manifestRoot + path;
iter = searchPaths.begin();
searchPaths.insert(iter, path);
needChangeSearchPaths = true;
}
if (needChangeSearchPaths)
{
FileUtils::getInstance()->setSearchPaths(searchPaths);
}
}
const std::string& Manifest::getPackageUrl() const
2019-11-23 20:27:39 +08:00
{
return _packageUrl;
}
const std::string& Manifest::getManifestFileUrl() const
2019-11-23 20:27:39 +08:00
{
return _remoteManifestUrl;
}
const std::string& Manifest::getVersionFileUrl() const
2019-11-23 20:27:39 +08:00
{
return _remoteVersionUrl;
}
const std::string& Manifest::getVersion() const
2019-11-23 20:27:39 +08:00
{
return _version;
}
const std::vector<std::string>& Manifest::getGroups() const
{
return _groups;
}
const std::unordered_map<std::string, std::string>& Manifest::getGroupVerions() const
2019-11-23 20:27:39 +08:00
{
return _groupVer;
}
const std::string& Manifest::getGroupVersion(const std::string& group) const
2019-11-23 20:27:39 +08:00
{
return _groupVer.at(group);
}
const std::unordered_map<std::string, Manifest::Asset>& Manifest::getAssets() const
2019-11-23 20:27:39 +08:00
{
return _assets;
}
void Manifest::setAssetDownloadState(const std::string& key, const Manifest::DownloadState& state)
2019-11-23 20:27:39 +08:00
{
auto valueIt = _assets.find(key);
if (valueIt != _assets.end())
{
valueIt->second.downloadState = state;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Update json object
2021-12-25 10:04:45 +08:00
if (_json.IsObject())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (_json.HasMember(KEY_ASSETS))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
rapidjson::Value& assets = _json[KEY_ASSETS];
2019-11-23 20:27:39 +08:00
if (assets.IsObject())
{
if (assets.HasMember(key.c_str()))
2019-11-23 20:27:39 +08:00
{
rapidjson::Value& entry = assets[key.c_str()];
2019-11-23 20:27:39 +08:00
if (entry.HasMember(KEY_DOWNLOAD_STATE) && entry[KEY_DOWNLOAD_STATE].IsInt())
{
2021-12-25 10:04:45 +08:00
entry[KEY_DOWNLOAD_STATE].SetInt((int)state);
2019-11-23 20:27:39 +08:00
}
else
{
entry.AddMember<int>(KEY_DOWNLOAD_STATE, (int)state, _json.GetAllocator());
}
}
}
}
}
}
}
void Manifest::clear()
{
if (_versionLoaded || _loaded)
{
_groups.clear();
_groupVer.clear();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_remoteManifestUrl = "";
2021-12-25 10:04:45 +08:00
_remoteVersionUrl = "";
_version = "";
_engineVer = "";
2019-11-23 20:27:39 +08:00
_versionLoaded = false;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_loaded)
{
_assets.clear();
_searchPaths.clear();
_loaded = false;
}
}
Manifest::Asset Manifest::parseAsset(const std::string& path, const rapidjson::Value& json)
2019-11-23 20:27:39 +08:00
{
Asset asset;
asset.path = path;
2021-12-25 10:04:45 +08:00
if (json.HasMember(KEY_MD5) && json[KEY_MD5].IsString())
2019-11-23 20:27:39 +08:00
{
asset.md5 = json[KEY_MD5].GetString();
}
2021-12-25 10:04:45 +08:00
else
asset.md5 = "";
if (json.HasMember(KEY_PATH) && json[KEY_PATH].IsString())
2019-11-23 20:27:39 +08:00
{
asset.path = json[KEY_PATH].GetString();
}
2021-12-25 10:04:45 +08:00
if (json.HasMember(KEY_COMPRESSED) && json[KEY_COMPRESSED].IsBool())
2019-11-23 20:27:39 +08:00
{
asset.compressed = json[KEY_COMPRESSED].GetBool();
}
2021-12-25 10:04:45 +08:00
else
asset.compressed = false;
if (json.HasMember(KEY_SIZE) && json[KEY_SIZE].IsInt())
2019-11-23 20:27:39 +08:00
{
asset.size = json[KEY_SIZE].GetInt();
}
2021-12-25 10:04:45 +08:00
else
asset.size = 0;
if (json.HasMember(KEY_DOWNLOAD_STATE) && json[KEY_DOWNLOAD_STATE].IsInt())
2019-11-23 20:27:39 +08:00
{
asset.downloadState = (json[KEY_DOWNLOAD_STATE].GetInt());
}
2021-12-25 10:04:45 +08:00
else
asset.downloadState = DownloadState::UNMARKED;
2019-11-23 20:27:39 +08:00
return asset;
}
2021-12-25 10:04:45 +08:00
void Manifest::loadVersion(const rapidjson::Document& json)
2019-11-23 20:27:39 +08:00
{
// Retrieve remote manifest url
2021-12-25 10:04:45 +08:00
if (json.HasMember(KEY_MANIFEST_URL) && json[KEY_MANIFEST_URL].IsString())
2019-11-23 20:27:39 +08:00
{
_remoteManifestUrl = json[KEY_MANIFEST_URL].GetString();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Retrieve remote version url
2021-12-25 10:04:45 +08:00
if (json.HasMember(KEY_VERSION_URL) && json[KEY_VERSION_URL].IsString())
2019-11-23 20:27:39 +08:00
{
_remoteVersionUrl = json[KEY_VERSION_URL].GetString();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Retrieve local version
2021-12-25 10:04:45 +08:00
if (json.HasMember(KEY_VERSION) && json[KEY_VERSION].IsString())
2019-11-23 20:27:39 +08:00
{
_version = json[KEY_VERSION].GetString();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Retrieve local group version
2021-12-25 10:04:45 +08:00
if (json.HasMember(KEY_GROUP_VERSIONS))
2019-11-23 20:27:39 +08:00
{
const rapidjson::Value& groupVers = json[KEY_GROUP_VERSIONS];
if (groupVers.IsObject())
{
2021-12-25 10:04:45 +08:00
for (rapidjson::Value::ConstMemberIterator itr = groupVers.MemberBegin(); itr != groupVers.MemberEnd();
++itr)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
std::string group = itr->name.GetString();
2019-11-23 20:27:39 +08:00
std::string version = "0";
if (itr->value.IsString())
{
version = itr->value.GetString();
}
_groups.push_back(group);
_groupVer.emplace(group, version);
}
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Retrieve local engine version
2021-12-25 10:04:45 +08:00
if (json.HasMember(KEY_ENGINE_VERSION) && json[KEY_ENGINE_VERSION].IsString())
2019-11-23 20:27:39 +08:00
{
_engineVer = json[KEY_ENGINE_VERSION].GetString();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_versionLoaded = true;
}
2021-12-25 10:04:45 +08:00
void Manifest::loadManifest(const rapidjson::Document& json)
2019-11-23 20:27:39 +08:00
{
loadVersion(json);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Retrieve package url
2021-12-25 10:04:45 +08:00
if (json.HasMember(KEY_PACKAGE_URL) && json[KEY_PACKAGE_URL].IsString())
2019-11-23 20:27:39 +08:00
{
_packageUrl = json[KEY_PACKAGE_URL].GetString();
// Append automatically "/"
if (!_packageUrl.empty() && _packageUrl[_packageUrl.size() - 1] != '/')
2019-11-23 20:27:39 +08:00
{
_packageUrl.push_back('/');
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Retrieve all assets
2021-12-25 10:04:45 +08:00
if (json.HasMember(KEY_ASSETS))
2019-11-23 20:27:39 +08:00
{
const rapidjson::Value& assets = json[KEY_ASSETS];
if (assets.IsObject())
{
for (rapidjson::Value::ConstMemberIterator itr = assets.MemberBegin(); itr != assets.MemberEnd(); ++itr)
{
std::string key = itr->name.GetString();
2021-12-25 10:04:45 +08:00
Asset asset = parseAsset(key, itr->value);
2019-11-23 20:27:39 +08:00
_assets.emplace(key, asset);
}
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Retrieve all search paths
2021-12-25 10:04:45 +08:00
if (json.HasMember(KEY_SEARCH_PATHS))
2019-11-23 20:27:39 +08:00
{
const rapidjson::Value& paths = json[KEY_SEARCH_PATHS];
if (paths.IsArray())
{
for (rapidjson::SizeType i = 0; i < paths.Size(); ++i)
{
2021-12-25 10:04:45 +08:00
if (paths[i].IsString())
{
_searchPaths.emplace_back(paths[i].GetString());
2019-11-23 20:27:39 +08:00
}
}
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_loaded = true;
}
void Manifest::saveToFile(const std::string& filepath)
2019-11-23 20:27:39 +08:00
{
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
_json.Accept(writer);
FileUtils::getInstance()->writeStringToFile(buffer.GetString(), filepath);
}
NS_CC_EXT_END