Manifest.cpp 18 KB

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