Manifest.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __Manifest__
  22. #define __Manifest__
  23. #include <string>
  24. #include <unordered_map>
  25. #include <vector>
  26. #include "extensions/ExtensionMacros.h"
  27. #include "extensions/ExtensionExport.h"
  28. #include "network/CCDownloader.h"
  29. #include "platform/CCFileUtils.h"
  30. #include "json/document-wrapper.h"
  31. NS_CC_EXT_BEGIN
  32. struct DownloadUnit
  33. {
  34. std::string srcUrl;
  35. std::string storagePath;
  36. std::string customId;
  37. float size;
  38. };
  39. struct ManifestAsset {
  40. std::string md5;
  41. std::string path;
  42. bool compressed;
  43. float size;
  44. int downloadState;
  45. };
  46. //typedef std::unordered_map<std::string, DownloadUnit> DownloadUnits;
  47. typedef std::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. enum DownloadType
  65. {
  66. PURE_MD5,
  67. MIXTURE_MD5
  68. };
  69. //! Asset object
  70. typedef ManifestAsset Asset;
  71. //! Object indicate the difference between two Assets
  72. struct AssetDiff {
  73. Asset asset;
  74. DiffType type;
  75. };
  76. /** @brief Check whether the version informations have been fully loaded
  77. */
  78. bool isVersionLoaded() const;
  79. /** @brief Check whether the manifest have been fully loaded
  80. */
  81. bool isLoaded() const;
  82. /** @brief Gets remote package url.
  83. */
  84. const std::string& getPackageUrl() const;
  85. /** @brief Gets remote manifest file url.
  86. */
  87. const std::string& getManifestFileUrl() const;
  88. /** @brief Gets remote version file url.
  89. */
  90. const std::string& getVersionFileUrl() const;
  91. /** @brief Gets manifest version.
  92. */
  93. const std::string& getVersion() const;
  94. /** @brief Get the search paths list related to the Manifest.
  95. */
  96. std::vector<std::string> getSearchPaths() const;
  97. void setCustomDownloadType(Manifest::DownloadType bCustomDownloadType);
  98. Manifest::DownloadType getCustomDownloadType();
  99. bool is_force_download() { return _is_force; };
  100. int get_pop_size() { return _pop_size; };
  101. std::string replace_string(std::string s1) const;
  102. bool is_download_half(const Manifest *b);
  103. protected:
  104. /** @brief Constructor for Manifest class
  105. * @param manifestUrl Url of the local manifest
  106. */
  107. Manifest(const std::string& manifestUrl = "");
  108. /** @brief Load the json file into local json object
  109. * @param url Url of the json file
  110. */
  111. void loadJson(const std::string& url);
  112. /** @brief Parse the version file information into this manifest
  113. * @param versionUrl Url of the local version file
  114. */
  115. void parseVersion(const std::string& versionUrl);
  116. /** @brief Parse the manifest file information into this manifest
  117. * @param manifestUrl Url of the local manifest
  118. */
  119. void parse(const std::string& manifestUrl);
  120. /** @brief Check whether the version of this manifest equals to another.
  121. * @param b The other manifest
  122. * @return Equal or not
  123. */
  124. bool versionEquals(const Manifest *b) const;
  125. /** @brief Check whether the version of this manifest is greater than another.
  126. * @param b The other manifest
  127. * @param [handle] Customized comparasion handle function
  128. * @return Greater or not
  129. */
  130. bool versionGreater(const Manifest *b, const std::function<int(const std::string& versionA, const std::string& versionB)>& handle) const;
  131. /** @brief Generate difference between this Manifest and another.
  132. * @param b The other manifest
  133. */
  134. std::unordered_map<std::string, AssetDiff> genDiff(const Manifest *b, bool is_all = false) const;
  135. /** @brief Generate resuming download assets list
  136. * @param units The download units reference to be modified by the generation result
  137. */
  138. void genResumeAssetsList(DownloadUnits *units) const;
  139. /** @brief Prepend all search paths to the FileUtils.
  140. */
  141. void prependSearchPaths();
  142. void loadVersion(const rapidjson::Document &json);
  143. void loadManifest(const rapidjson::Document &json);
  144. void saveToFile(const std::string &filepath);
  145. Asset parseAsset(const std::string &path, const rapidjson::Value &json);
  146. void clear();
  147. /** @brief Gets all groups.
  148. */
  149. const std::vector<std::string>& getGroups() const;
  150. /** @brief Gets all groups version.
  151. */
  152. const std::unordered_map<std::string, std::string>& getGroupVerions() const;
  153. /** @brief Gets version for the given group.
  154. * @param group Key of the requested group
  155. */
  156. const std::string& getGroupVersion(const std::string &group) const;
  157. /**
  158. * @brief Gets assets.
  159. * @lua NA
  160. */
  161. const std::unordered_map<std::string, Asset>& getAssets() const;
  162. /** @brief Set the download state for an asset
  163. * @param key Key of the asset to set
  164. * @param state The current download state of the asset
  165. */
  166. void setAssetDownloadState(const std::string &key, const DownloadState &state);
  167. void setManifestRoot(const std::string &root) {_manifestRoot = root;};
  168. const std::string& getPlatFormUrl() const;
  169. const std::string& getResourceUrl() const;
  170. void setAppVersion(const std::string & appVersion) {_appVersion = appVersion;}
  171. private:
  172. //! Indicate whether the version informations have been fully loaded
  173. bool _versionLoaded;
  174. //! Indicate whether the manifest have been fully loaded
  175. bool _loaded;
  176. bool _is_force;
  177. //! Reference to the global file utils
  178. FileUtils *_fileUtils;
  179. //! The local manifest root
  180. std::string _manifestRoot;
  181. //! The remote package url
  182. std::string _packageUrl;
  183. //! The remote path of manifest file
  184. std::string _remoteManifestUrl;
  185. //! The remote path of version file [Optional]
  186. std::string _remoteVersionUrl;
  187. //! The version of local manifest
  188. std::string _version;
  189. //! All groups exist in manifest [Optional]
  190. std::vector<std::string> _groups;
  191. //! The versions of all local group [Optional]
  192. std::unordered_map<std::string, std::string> _groupVer;
  193. //! The version of local engine
  194. std::string _engineVer;
  195. //! Full assets list
  196. std::unordered_map<std::string, Asset> _assets;
  197. //! All search paths
  198. std::vector<std::string> _searchPaths;
  199. //ios:ipa android:apk
  200. std::string platform_full_url;
  201. //��Դ��ַ
  202. std::string resource_full_url;
  203. rapidjson::Document _json;
  204. std::string _appVersion;
  205. //是否自定义下载地址标识
  206. Manifest::DownloadType _nCustomDownloadType;
  207. int _pop_size;
  208. };
  209. NS_CC_EXT_END
  210. #endif /* defined(__Manifest__) */