Manifest.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  3. Copyright (c) 2014-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. #ifndef __Manifest__
  23. #define __Manifest__
  24. #include <string>
  25. #include <unordered_map>
  26. #include <vector>
  27. #include "extensions/ExtensionMacros.h"
  28. #include "extensions/ExtensionExport.h"
  29. #include "network/CCDownloader.h"
  30. #include "platform/CCFileUtils.h"
  31. #include "json/document-wrapper.h"
  32. NS_CC_EXT_BEGIN
  33. struct DownloadUnit
  34. {
  35. std::string srcUrl;
  36. std::string storagePath;
  37. std::string customId;
  38. float size;
  39. };
  40. struct ManifestAsset {
  41. std::string md5;
  42. std::string path;
  43. bool compressed;
  44. float size;
  45. int downloadState;
  46. };
  47. typedef std::unordered_map<std::string, DownloadUnit> DownloadUnits;
  48. class CC_EX_DLL Manifest : public Ref
  49. {
  50. public:
  51. friend class AssetsManagerEx;
  52. //! The type of difference
  53. enum class DiffType {
  54. ADDED,
  55. DELETED,
  56. MODIFIED
  57. };
  58. enum DownloadState {
  59. UNSTARTED,
  60. DOWNLOADING,
  61. SUCCESSED,
  62. UNMARKED
  63. };
  64. //! Asset object
  65. typedef ManifestAsset Asset;
  66. //! Object indicate the difference between two Assets
  67. struct AssetDiff {
  68. Asset asset;
  69. DiffType type;
  70. };
  71. /** @brief Check whether the version informations have been fully loaded
  72. */
  73. bool isVersionLoaded() const;
  74. /** @brief Check whether the manifest have been fully loaded
  75. */
  76. bool isLoaded() const;
  77. /** @brief Gets remote package url.
  78. */
  79. const std::string& getPackageUrl() const;
  80. /** @brief Gets remote manifest file url.
  81. */
  82. const std::string& getManifestFileUrl() const;
  83. /** @brief Gets remote version file url.
  84. */
  85. const std::string& getVersionFileUrl() const;
  86. /** @brief Gets manifest version.
  87. */
  88. const std::string& getVersion() const;
  89. /** @brief Get the search paths list related to the Manifest.
  90. */
  91. std::vector<std::string> getSearchPaths() const;
  92. /** @brief Get the manifest root path, normally it should also be the local storage path.
  93. */
  94. const std::string& getManifestRoot() const { return _manifestRoot; };
  95. /** @brief Constructor for Manifest class, create manifest by parsing a json file
  96. * @param manifestUrl Url of the local manifest
  97. */
  98. Manifest(const std::string& manifestUrl = "");
  99. /** @brief Constructor for Manifest class, create manifest by parsing a json string
  100. * @param content Json string content
  101. * @param manifestRoot The root path of the manifest file (It should be local path, so that we can find assets path relative to the root path)
  102. */
  103. Manifest(const std::string& content, const std::string& manifestRoot);
  104. /** @brief Parse the manifest file information into this manifest
  105. * @param manifestUrl Url of the local manifest
  106. */
  107. void parseFile(const std::string& manifestUrl);
  108. /** @brief Parse the manifest from json string into this manifest
  109. * @param content Json string content
  110. * @param manifestRoot The root path of the manifest file (It should be local path, so that we can find assets path relative to the root path)
  111. */
  112. void parseJSONString(const std::string& content, const std::string& manifestRoot);
  113. /** @brief Get whether the manifest is being updating
  114. * @return Updating or not
  115. */
  116. bool isUpdating() const { return _updating; };
  117. /** @brief Set whether the manifest is being updating
  118. * @param updating Updating or not
  119. */
  120. void setUpdating(bool updating);
  121. protected:
  122. /** @brief Load the json file into local json object
  123. * @param url Url of the json file
  124. */
  125. void loadJson(const std::string& url);
  126. /** @brief Load the json from a string into local json object
  127. * @param content The json content string
  128. */
  129. void loadJsonFromString(const std::string& content);
  130. /** @brief Parse the version file information into this manifest
  131. * @param versionUrl Url of the local version file
  132. */
  133. void parseVersion(const std::string& versionUrl);
  134. /** @brief Check whether the version of this manifest equals to another.
  135. * @param b The other manifest
  136. * @return Equal or not
  137. */
  138. bool versionEquals(const Manifest *b) const;
  139. /** @brief Check whether the version of this manifest is greater or equals than another.
  140. * @param b The other manifest
  141. * @param [handle] Customized comparasion handle function
  142. * @return Greater or not
  143. */
  144. bool versionGreaterOrEquals(const Manifest *b, const std::function<int(const std::string& versionA, const std::string& versionB)>& handle) const;
  145. /** @brief Check whether the version of this manifest is greater or equals than another.
  146. * @param b The other manifest
  147. * @param [handle] Customized comparasion handle function
  148. * @return Greater or not
  149. */
  150. bool versionGreater(const Manifest *b, const std::function<int(const std::string& versionA, const std::string& versionB)>& handle) const;
  151. /** @brief Generate difference between this Manifest and another.
  152. * @param b The other manifest
  153. */
  154. std::unordered_map<std::string, AssetDiff> genDiff(const Manifest *b) const;
  155. /** @brief Generate resuming download assets list
  156. * @param units The download units reference to be modified by the generation result
  157. */
  158. void genResumeAssetsList(DownloadUnits *units) const;
  159. /** @brief Prepend all search paths to the FileUtils.
  160. */
  161. void prependSearchPaths();
  162. void loadVersion(const rapidjson::Document &json);
  163. void loadManifest(const rapidjson::Document &json);
  164. void saveToFile(const std::string &filepath);
  165. Asset parseAsset(const std::string &path, const rapidjson::Value &json);
  166. void clear();
  167. /** @brief Gets all groups.
  168. */
  169. const std::vector<std::string>& getGroups() const;
  170. /** @brief Gets all groups version.
  171. */
  172. const std::unordered_map<std::string, std::string>& getGroupVerions() const;
  173. /** @brief Gets version for the given group.
  174. * @param group Key of the requested group
  175. */
  176. const std::string& getGroupVersion(const std::string &group) const;
  177. /**
  178. * @brief Gets assets.
  179. * @lua NA
  180. */
  181. const std::unordered_map<std::string, Asset>& getAssets() const;
  182. /** @brief Set the download state for an asset
  183. * @param key Key of the asset to set
  184. * @param state The current download state of the asset
  185. */
  186. void setAssetDownloadState(const std::string &key, const DownloadState &state);
  187. void setManifestRoot(const std::string &root) {_manifestRoot = root;};
  188. private:
  189. //! Indicate whether the version informations have been fully loaded
  190. bool _versionLoaded;
  191. //! Indicate whether the manifest have been fully loaded
  192. bool _loaded;
  193. //! Indicate whether the manifest is updating and can be resumed in the future
  194. bool _updating;
  195. //! Reference to the global file utils
  196. FileUtils *_fileUtils;
  197. //! The local manifest root
  198. std::string _manifestRoot;
  199. //! The remote package url
  200. std::string _packageUrl;
  201. //! The remote path of manifest file
  202. std::string _remoteManifestUrl;
  203. //! The remote path of version file [Optional]
  204. std::string _remoteVersionUrl;
  205. //! The version of local manifest
  206. std::string _version;
  207. //! All groups exist in manifest [Optional]
  208. std::vector<std::string> _groups;
  209. //! The versions of all local group [Optional]
  210. std::unordered_map<std::string, std::string> _groupVer;
  211. //! The version of local engine
  212. std::string _engineVer;
  213. //! Full assets list
  214. std::unordered_map<std::string, Asset> _assets;
  215. //! All search paths
  216. std::vector<std::string> _searchPaths;
  217. rapidjson::Document _json;
  218. };
  219. NS_CC_EXT_END
  220. #endif /* defined(__Manifest__) */