Manifest.h 7.1 KB

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