AssetsManager.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 __AssetsManager__
  21. #define __AssetsManager__
  22. #include <string>
  23. #include <mutex>
  24. #include "2d/CCNode.h"
  25. #include "extensions/ExtensionMacros.h"
  26. #include "extensions/ExtensionExport.h"
  27. namespace cocos2d { namespace network {
  28. class Downloader;
  29. }}
  30. NS_CC_EXT_BEGIN
  31. class AssetsManagerDelegateProtocol;
  32. /*
  33. * This class is used to auto update resources, such as pictures or scripts.
  34. * The updated package should be a zip file. And there should be a file named
  35. * version in the server, which contains version code.
  36. */
  37. class CC_EX_DLL AssetsManager : public Node
  38. {
  39. public:
  40. enum class ErrorCode
  41. {
  42. // Error caused by creating a file to store downloaded data
  43. CREATE_FILE,
  44. /** Error caused by network
  45. -- network unavailable
  46. -- timeout
  47. -- ...
  48. */
  49. NETWORK,
  50. /** There is not a new version
  51. */
  52. NO_NEW_VERSION,
  53. /** Error caused in uncompressing stage
  54. -- can not open zip file
  55. -- can not read file global information
  56. -- can not read file information
  57. -- can not create a directory
  58. -- ...
  59. */
  60. UNCOMPRESS,
  61. };
  62. /* @brief Creates a AssetsManager with new package url, version code url and storage path.
  63. *
  64. * @param packageUrl URL of new package, the package should be a zip file.
  65. * @param versionFileUrl URL of version file. It should contain version code of new package.
  66. * @param storagePath The path to store downloaded resources.
  67. * @js NA
  68. * @lua new
  69. */
  70. AssetsManager(const char* packageUrl = NULL, const char* versionFileUrl = NULL, const char* storagePath = NULL);
  71. /**
  72. * @js NA
  73. * @lua NA
  74. */
  75. virtual ~AssetsManager();
  76. typedef std::function<void(int)> ErrorCallback;
  77. typedef std::function<void(int)> ProgressCallback;
  78. typedef std::function<void(void)> SuccessCallback;
  79. /* @brief To access within scripting environment
  80. */
  81. static AssetsManager* create(const char* packageUrl, const char* versionFileUrl, const char* storagePath, ErrorCallback errorCallback, ProgressCallback progressCallback, SuccessCallback successCallback );
  82. /* @brief Check out if there is a new version resource.
  83. * You may use this method before updating, then let user determine whether
  84. * he wants to update resources.
  85. */
  86. virtual bool checkUpdate();
  87. virtual bool checkUpdateEx();
  88. using Node::update;
  89. /* @brief Download new package if there is a new version, and uncompress downloaded zip file.
  90. * Ofcourse it will set search path that stores downloaded files.
  91. */
  92. virtual void update();
  93. /* @brief Gets url of package.
  94. */
  95. const char* getPackageUrl() const;
  96. /* @brief Sets package url.
  97. */
  98. void setPackageUrl(const char* packageUrl);
  99. void setPackageName(const char* packageName);
  100. /* @brief Gets version file url.
  101. */
  102. const char* getVersionFileUrl() const;
  103. /* @brief Gets version file url.
  104. */
  105. void setVersionFileUrl(const char* versionFileUrl);
  106. /* @brief Gets current version code.
  107. */
  108. std::string getVersion();
  109. /* @brief Deletes recorded version code.
  110. */
  111. void deleteVersion();
  112. /* @brief Gets storage path.
  113. */
  114. const char* getStoragePath() const;
  115. /* @brief Sets storage path.
  116. *
  117. * @param storagePath The path to store downloaded resources.
  118. * @warning The path should be a valid path.
  119. */
  120. void setStoragePath(const char* storagePath);
  121. /** @brief Sets delegate, the delegate will receive messages
  122. * @js NA
  123. * @lua NA
  124. */
  125. void setDelegate(AssetsManagerDelegateProtocol *delegate);
  126. /**
  127. * @js NA
  128. * @lua NA
  129. */
  130. AssetsManagerDelegateProtocol* getDelegate() const { return _delegate ;}
  131. /** @brief Sets connection time out in seconds
  132. */
  133. void setConnectionTimeout(unsigned int timeout);
  134. /** @brief Gets connection time out in seconds
  135. */
  136. unsigned int getConnectionTimeout();
  137. // alter by yjh 2017-03-07 13:06:43 Éý¼¶ÐèÒªÐ޸ĵĴúÂë
  138. void setIsOnlyCheckVersion(bool isCheck){ _isOnlyCheckVersion = isCheck; }
  139. // end
  140. protected:
  141. std::string basename(const std::string& path);
  142. void checkStoragePath();
  143. bool uncompress();
  144. bool uncompressEx();
  145. void setSearchPath();
  146. void downloadAndUncompress();
  147. private:
  148. //! The path to store downloaded resources.
  149. std::string _storagePath;
  150. //! The version of downloaded resources.
  151. std::string _version;
  152. std::string _packageUrl;
  153. std::string _packageName;
  154. std::string _versionFileUrl;
  155. std::string _downloadedVersion;
  156. cocos2d::network::Downloader* _downloader;
  157. unsigned int _connectionTimeout;
  158. AssetsManagerDelegateProtocol *_delegate;
  159. bool _isDownloading;
  160. bool _shouldDeleteDelegateWhenExit;
  161. bool _isOnlyCheckVersion;
  162. std::string keyOfVersion() const;
  163. std::string keyOfDownloadedVersion() const;
  164. };
  165. class AssetsManagerDelegateProtocol
  166. {
  167. public:
  168. virtual ~AssetsManagerDelegateProtocol(){};
  169. public:
  170. /* @brief Call back function for error
  171. @param errorCode Type of error
  172. * @js NA
  173. * @lua NA
  174. */
  175. virtual void onError(AssetsManager::ErrorCode errorCode) {};
  176. /** @brief Call back function for recording downloading percent
  177. @param percent How much percent downloaded
  178. @warning This call back function just for recording downloading percent.
  179. AssetsManager will do some other thing after downloading, you should
  180. write code in onSuccess() after downloading.
  181. * @js NA
  182. * @lua NA
  183. */
  184. virtual void onProgress(int percent) {};
  185. /** @brief Call back function for success
  186. * @js NA
  187. * @lua NA
  188. */
  189. virtual void onSuccess() {};
  190. // alter by yjh 2017-03-07 13:16:15
  191. virtual void onAskForUpdate() {};
  192. // end
  193. };
  194. // Deprecated declaration
  195. CC_DEPRECATED_ATTRIBUTE typedef AssetsManager CCAssetsManager;
  196. CC_DEPRECATED_ATTRIBUTE typedef AssetsManagerDelegateProtocol CCAssetsManagerDelegateProtocol;
  197. NS_CC_EXT_END;
  198. #endif /* defined(__AssetsManager__) */