1
0

Manifest.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /****************************************************************************
  2. Copyright (c) 2014 cocos2d-x.org
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "Manifest.h"
  21. #include "json/prettywriter.h"
  22. #include "json/stringbuffer.h"
  23. #include <fstream>
  24. #include <stdio.h>
  25. #define KEY_VERSION "version"
  26. #define KEY_PACKAGE_URL "packageUrl"
  27. #define KEY_MANIFEST_URL "remoteManifestUrl"
  28. #define KEY_VERSION_URL "remoteVersionUrl"
  29. #define KEY_GROUP_VERSIONS "groupVersions"
  30. #define KEY_ENGINE_VERSION "engineVersion"
  31. #define KEY_ASSETS "assets"
  32. #define KEY_COMPRESSED_FILES "compressedFiles"
  33. #define KEY_SEARCH_PATHS "searchPaths"
  34. #define KEY_PATH "path"
  35. #define KEY_MD5 "md5"
  36. #define KEY_GROUP "group"
  37. #define KEY_COMPRESSED "compressed"
  38. #define KEY_SIZE "size"
  39. #define KEY_COMPRESSED_FILE "compressedFile"
  40. #define KEY_DOWNLOAD_STATE "downloadState"
  41. NS_CC_EXT_BEGIN
  42. static int cmpVersion(const std::string& v1, const std::string& v2)
  43. {
  44. int i;
  45. int oct_v1[4] = {0}, oct_v2[4] = {0};
  46. int filled1 = std::sscanf(v1.c_str(), "%d.%d.%d.%d", &oct_v1[0], &oct_v1[1], &oct_v1[2], &oct_v1[3]);
  47. int filled2 = std::sscanf(v2.c_str(), "%d.%d.%d.%d", &oct_v2[0], &oct_v2[1], &oct_v2[2], &oct_v2[3]);
  48. if (filled1 == 0 || filled2 == 0)
  49. {
  50. return strcmp(v1.c_str(), v2.c_str());
  51. }
  52. for (i = 0; i < 4; i++)
  53. {
  54. if (oct_v1[i] > oct_v2[i])
  55. return 1;
  56. else if (oct_v1[i] < oct_v2[i])
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. Manifest::Manifest(const std::string& manifestUrl/* = ""*/)
  62. : _versionLoaded(false)
  63. , _loaded(false)
  64. , _manifestRoot("")
  65. , _remoteManifestUrl("")
  66. , _remoteVersionUrl("")
  67. , _version("")
  68. , _engineVer("")
  69. {
  70. // Init variables
  71. _fileUtils = FileUtils::getInstance();
  72. if (manifestUrl.size() > 0)
  73. parse(manifestUrl);
  74. }
  75. void Manifest::loadJson(const std::string& url)
  76. {
  77. clear();
  78. std::string content;
  79. if (_fileUtils->isFileExist(url))
  80. {
  81. // Load file content
  82. content = _fileUtils->getStringFromFile(url);
  83. if (content.size() == 0)
  84. {
  85. CCLOG("Fail to retrieve local file content: %s\n", url.c_str());
  86. }
  87. else
  88. {
  89. // Parse file with rapid json
  90. _json.Parse<0>(content.c_str());
  91. // Print error
  92. if (_json.HasParseError()) {
  93. size_t offset = _json.GetErrorOffset();
  94. if (offset > 0)
  95. offset--;
  96. std::string errorSnippet = content.substr(offset, 10);
  97. CCLOG("File parse error %d at <%s>\n", _json.GetParseError(), errorSnippet.c_str());
  98. }
  99. }
  100. }
  101. }
  102. void Manifest::parseVersion(const std::string& versionUrl)
  103. {
  104. loadJson(versionUrl);
  105. if (_json.IsObject())
  106. {
  107. loadVersion(_json);
  108. }
  109. }
  110. void Manifest::parse(const std::string& manifestUrl)
  111. {
  112. loadJson(manifestUrl);
  113. if (!_json.HasParseError() && _json.IsObject())
  114. {
  115. // Register the local manifest root
  116. size_t found = manifestUrl.find_last_of("/\\");
  117. if (found != std::string::npos)
  118. {
  119. _manifestRoot = manifestUrl.substr(0, found+1);
  120. }
  121. loadManifest(_json);
  122. }
  123. }
  124. bool Manifest::isVersionLoaded() const
  125. {
  126. return _versionLoaded;
  127. }
  128. bool Manifest::isLoaded() const
  129. {
  130. return _loaded;
  131. }
  132. bool Manifest::versionEquals(const Manifest *b) const
  133. {
  134. // Check manifest version
  135. if (_version != b->getVersion())
  136. {
  137. return false;
  138. }
  139. // Check group versions
  140. else
  141. {
  142. std::vector<std::string> bGroups = b->getGroups();
  143. std::unordered_map<std::string, std::string> bGroupVer = b->getGroupVerions();
  144. // Check group size
  145. if (bGroups.size() != _groups.size())
  146. return false;
  147. // Check groups version
  148. for (unsigned int i = 0; i < _groups.size(); ++i) {
  149. std::string gid =_groups[i];
  150. // Check group name
  151. if (gid != bGroups[i])
  152. return false;
  153. // Check group version
  154. if (_groupVer.at(gid) != bGroupVer.at(gid))
  155. return false;
  156. }
  157. }
  158. return true;
  159. }
  160. bool Manifest::versionGreater(const Manifest *b, const std::function<int(const std::string& versionA, const std::string& versionB)>& handle) const
  161. {
  162. std::string localVersion = getVersion();
  163. std::string bVersion = b->getVersion();
  164. bool greater;
  165. if (handle)
  166. {
  167. greater = handle(localVersion, bVersion) >= 0;
  168. }
  169. else
  170. {
  171. greater = cmpVersion(localVersion, bVersion) >= 0;
  172. }
  173. return greater;
  174. }
  175. std::unordered_map<std::string, Manifest::AssetDiff> Manifest::genDiff(const Manifest *b) const
  176. {
  177. std::unordered_map<std::string, AssetDiff> diff_map;
  178. const std::unordered_map<std::string, Asset> &bAssets = b->getAssets();
  179. std::string key;
  180. Asset valueA;
  181. Asset valueB;
  182. std::unordered_map<std::string, Asset>::const_iterator valueIt, it;
  183. for (it = _assets.begin(); it != _assets.end(); ++it)
  184. {
  185. key = it->first;
  186. valueA = it->second;
  187. // Deleted
  188. valueIt = bAssets.find(key);
  189. if (valueIt == bAssets.cend()) {
  190. AssetDiff diff;
  191. diff.asset = valueA;
  192. diff.type = DiffType::DELETED;
  193. diff_map.emplace(key, diff);
  194. continue;
  195. }
  196. // Modified
  197. valueB = valueIt->second;
  198. if (valueA.md5 != valueB.md5) {
  199. AssetDiff diff;
  200. diff.asset = valueB;
  201. diff.type = DiffType::MODIFIED;
  202. diff_map.emplace(key, diff);
  203. }
  204. }
  205. for (it = bAssets.begin(); it != bAssets.end(); ++it)
  206. {
  207. key = it->first;
  208. valueB = it->second;
  209. // Added
  210. valueIt = _assets.find(key);
  211. if (valueIt == _assets.cend()) {
  212. AssetDiff diff;
  213. diff.asset = valueB;
  214. diff.type = DiffType::ADDED;
  215. diff_map.emplace(key, diff);
  216. }
  217. }
  218. return diff_map;
  219. }
  220. void Manifest::genResumeAssetsList(DownloadUnits *units) const
  221. {
  222. for (auto it = _assets.begin(); it != _assets.end(); ++it)
  223. {
  224. Asset asset = it->second;
  225. if (asset.downloadState != DownloadState::SUCCESSED && asset.downloadState != DownloadState::UNMARKED)
  226. {
  227. DownloadUnit unit;
  228. unit.customId = it->first;
  229. unit.srcUrl = _packageUrl + asset.path;
  230. unit.storagePath = _manifestRoot + asset.path;
  231. unit.size = asset.size;
  232. units->emplace(unit.customId, unit);
  233. }
  234. }
  235. }
  236. std::vector<std::string> Manifest::getSearchPaths() const
  237. {
  238. std::vector<std::string> searchPaths;
  239. searchPaths.push_back(_manifestRoot);
  240. for (int i = (int)_searchPaths.size()-1; i >= 0; i--)
  241. {
  242. std::string path = _searchPaths[i];
  243. if (path.size() > 0 && path[path.size() - 1] != '/')
  244. path.append("/");
  245. path = _manifestRoot + path;
  246. searchPaths.push_back(path);
  247. }
  248. return searchPaths;
  249. }
  250. void Manifest::prependSearchPaths()
  251. {
  252. std::vector<std::string> searchPaths = FileUtils::getInstance()->getSearchPaths();
  253. std::vector<std::string>::iterator iter = searchPaths.begin();
  254. bool needChangeSearchPaths = false;
  255. if (std::find(searchPaths.begin(), searchPaths.end(), _manifestRoot) == searchPaths.end())
  256. {
  257. searchPaths.insert(iter, _manifestRoot);
  258. needChangeSearchPaths = true;
  259. }
  260. for (int i = (int)_searchPaths.size()-1; i >= 0; i--)
  261. {
  262. std::string path = _searchPaths[i];
  263. if (path.size() > 0 && path[path.size() - 1] != '/')
  264. path.append("/");
  265. path = _manifestRoot + path;
  266. iter = searchPaths.begin();
  267. searchPaths.insert(iter, path);
  268. needChangeSearchPaths = true;
  269. }
  270. if (needChangeSearchPaths)
  271. {
  272. FileUtils::getInstance()->setSearchPaths(searchPaths);
  273. }
  274. }
  275. const std::string& Manifest::getPackageUrl() const
  276. {
  277. return _packageUrl;
  278. }
  279. const std::string& Manifest::getManifestFileUrl() const
  280. {
  281. return _remoteManifestUrl;
  282. }
  283. const std::string& Manifest::getVersionFileUrl() const
  284. {
  285. return _remoteVersionUrl;
  286. }
  287. const std::string& Manifest::getVersion() const
  288. {
  289. return _version;
  290. }
  291. const std::vector<std::string>& Manifest::getGroups() const
  292. {
  293. return _groups;
  294. }
  295. const std::unordered_map<std::string, std::string>& Manifest::getGroupVerions() const
  296. {
  297. return _groupVer;
  298. }
  299. const std::string& Manifest::getGroupVersion(const std::string &group) const
  300. {
  301. return _groupVer.at(group);
  302. }
  303. const std::unordered_map<std::string, Manifest::Asset>& Manifest::getAssets() const
  304. {
  305. return _assets;
  306. }
  307. void Manifest::setAssetDownloadState(const std::string &key, const Manifest::DownloadState &state)
  308. {
  309. auto valueIt = _assets.find(key);
  310. if (valueIt != _assets.end())
  311. {
  312. valueIt->second.downloadState = state;
  313. // Update json object
  314. if(_json.IsObject())
  315. {
  316. if ( _json.HasMember(KEY_ASSETS) )
  317. {
  318. rapidjson::Value &assets = _json[KEY_ASSETS];
  319. if (assets.IsObject())
  320. {
  321. if (assets.HasMember(key.c_str()))
  322. {
  323. rapidjson::Value &entry = assets[key.c_str()];
  324. if (entry.HasMember(KEY_DOWNLOAD_STATE) && entry[KEY_DOWNLOAD_STATE].IsInt())
  325. {
  326. entry[KEY_DOWNLOAD_STATE].SetInt((int) state);
  327. }
  328. else
  329. {
  330. entry.AddMember<int>(KEY_DOWNLOAD_STATE, (int)state, _json.GetAllocator());
  331. }
  332. }
  333. }
  334. }
  335. }
  336. }
  337. }
  338. void Manifest::clear()
  339. {
  340. if (_versionLoaded || _loaded)
  341. {
  342. _groups.clear();
  343. _groupVer.clear();
  344. _remoteManifestUrl = "";
  345. _remoteVersionUrl = "";
  346. _version = "";
  347. _engineVer = "";
  348. _versionLoaded = false;
  349. }
  350. if (_loaded)
  351. {
  352. _assets.clear();
  353. _searchPaths.clear();
  354. _loaded = false;
  355. }
  356. }
  357. Manifest::Asset Manifest::parseAsset(const std::string &path, const rapidjson::Value &json)
  358. {
  359. Asset asset;
  360. asset.path = path;
  361. if ( json.HasMember(KEY_MD5) && json[KEY_MD5].IsString() )
  362. {
  363. asset.md5 = json[KEY_MD5].GetString();
  364. }
  365. else asset.md5 = "";
  366. if ( json.HasMember(KEY_PATH) && json[KEY_PATH].IsString() )
  367. {
  368. asset.path = json[KEY_PATH].GetString();
  369. }
  370. if ( json.HasMember(KEY_COMPRESSED) && json[KEY_COMPRESSED].IsBool() )
  371. {
  372. asset.compressed = json[KEY_COMPRESSED].GetBool();
  373. }
  374. else asset.compressed = false;
  375. if ( json.HasMember(KEY_SIZE) && json[KEY_SIZE].IsInt() )
  376. {
  377. asset.size = json[KEY_SIZE].GetInt();
  378. }
  379. else asset.size = 0;
  380. if ( json.HasMember(KEY_DOWNLOAD_STATE) && json[KEY_DOWNLOAD_STATE].IsInt() )
  381. {
  382. asset.downloadState = (json[KEY_DOWNLOAD_STATE].GetInt());
  383. }
  384. else asset.downloadState = DownloadState::UNMARKED;
  385. return asset;
  386. }
  387. void Manifest::loadVersion(const rapidjson::Document &json)
  388. {
  389. // Retrieve remote manifest url
  390. if ( json.HasMember(KEY_MANIFEST_URL) && json[KEY_MANIFEST_URL].IsString() )
  391. {
  392. _remoteManifestUrl = json[KEY_MANIFEST_URL].GetString();
  393. }
  394. // Retrieve remote version url
  395. if ( json.HasMember(KEY_VERSION_URL) && json[KEY_VERSION_URL].IsString() )
  396. {
  397. _remoteVersionUrl = json[KEY_VERSION_URL].GetString();
  398. }
  399. // Retrieve local version
  400. if ( json.HasMember(KEY_VERSION) && json[KEY_VERSION].IsString() )
  401. {
  402. _version = json[KEY_VERSION].GetString();
  403. }
  404. // Retrieve local group version
  405. if ( json.HasMember(KEY_GROUP_VERSIONS) )
  406. {
  407. const rapidjson::Value& groupVers = json[KEY_GROUP_VERSIONS];
  408. if (groupVers.IsObject())
  409. {
  410. for (rapidjson::Value::ConstMemberIterator itr = groupVers.MemberBegin(); itr != groupVers.MemberEnd(); ++itr)
  411. {
  412. std::string group = itr->name.GetString();
  413. std::string version = "0";
  414. if (itr->value.IsString())
  415. {
  416. version = itr->value.GetString();
  417. }
  418. _groups.push_back(group);
  419. _groupVer.emplace(group, version);
  420. }
  421. }
  422. }
  423. // Retrieve local engine version
  424. if ( json.HasMember(KEY_ENGINE_VERSION) && json[KEY_ENGINE_VERSION].IsString() )
  425. {
  426. _engineVer = json[KEY_ENGINE_VERSION].GetString();
  427. }
  428. _versionLoaded = true;
  429. }
  430. void Manifest::loadManifest(const rapidjson::Document &json)
  431. {
  432. loadVersion(json);
  433. // Retrieve package url
  434. if ( json.HasMember(KEY_PACKAGE_URL) && json[KEY_PACKAGE_URL].IsString() )
  435. {
  436. _packageUrl = json[KEY_PACKAGE_URL].GetString();
  437. // Append automatically "/"
  438. if (_packageUrl.size() > 0 && _packageUrl[_packageUrl.size() - 1] != '/')
  439. {
  440. _packageUrl.append("/");
  441. }
  442. }
  443. // Retrieve all assets
  444. if ( json.HasMember(KEY_ASSETS) )
  445. {
  446. const rapidjson::Value& assets = json[KEY_ASSETS];
  447. if (assets.IsObject())
  448. {
  449. for (rapidjson::Value::ConstMemberIterator itr = assets.MemberBegin(); itr != assets.MemberEnd(); ++itr)
  450. {
  451. std::string key = itr->name.GetString();
  452. Asset asset = parseAsset(key, itr->value);
  453. _assets.emplace(key, asset);
  454. }
  455. }
  456. }
  457. // Retrieve all search paths
  458. if ( json.HasMember(KEY_SEARCH_PATHS) )
  459. {
  460. const rapidjson::Value& paths = json[KEY_SEARCH_PATHS];
  461. if (paths.IsArray())
  462. {
  463. for (rapidjson::SizeType i = 0; i < paths.Size(); ++i)
  464. {
  465. if (paths[i].IsString()) {
  466. _searchPaths.push_back(paths[i].GetString());
  467. }
  468. }
  469. }
  470. }
  471. _loaded = true;
  472. }
  473. void Manifest::saveToFile(const std::string &filepath)
  474. {
  475. rapidjson::StringBuffer buffer;
  476. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
  477. _json.Accept(writer);
  478. std::ofstream output(filepath, std::ofstream::out);
  479. if(!output.bad())
  480. output << buffer.GetString() << std::endl;
  481. }
  482. NS_CC_EXT_END