diff --git a/extensions/AssetsManager/AssetsManager.cpp b/extensions/AssetsManager/AssetsManager.cpp index ac0ca37dac..e4bc2e721d 100644 --- a/extensions/AssetsManager/AssetsManager.cpp +++ b/extensions/AssetsManager/AssetsManager.cpp @@ -88,6 +88,10 @@ AssetsManager::AssetsManager(const char* packageUrl/* =NULL */, const char* vers AssetsManager::~AssetsManager() { + if (_delegate) + { + _delegate->release(); + } if (_schedule) { _schedule->release(); @@ -479,7 +483,17 @@ void AssetsManager::deleteVersion() void AssetsManager::setDelegate(AssetsManagerDelegateProtocol *delegate) { + if (_delegate) + { + _delegate->release(); + } + _delegate = delegate; + + if (_delegate) + { + _delegate->retain(); + } } void AssetsManager::setConnectionTimeout(unsigned int timeout) @@ -604,4 +618,30 @@ void AssetsManager::Helper::handleUpdateSucceed(Message *msg) if (manager) manager->_delegate->onSuccess(); } +AssetsManager* AssetsManager::create(const char* packageUrl, const char* versionFileUrl, const char* storagePath, ErrorCallback errorCallback, ProgressCallback progressCallback, SuccessCallback successCallback ) +{ + class DelegateProtocolImpl : public AssetsManagerDelegateProtocol + { + public : + DelegateProtocolImpl(ErrorCallback errorCallback, ProgressCallback progressCallback, SuccessCallback successCallback) + : errorCallback(errorCallback), progressCallback(progressCallback), successCallback(successCallback) + {} + + virtual void onError(AssetsManager::ErrorCode errorCode) { errorCallback(int(errorCode)); } + virtual void onProgress(int percent) { progressCallback(percent); } + virtual void onSuccess() { successCallback(); } + + private : + ErrorCallback errorCallback; + ProgressCallback progressCallback; + SuccessCallback successCallback; + }; + + auto* manager = new AssetsManager(packageUrl,versionFileUrl,storagePath); + auto* delegate = new DelegateProtocolImpl(errorCallback,progressCallback,successCallback); + delegate->autorelease(); + manager->setDelegate(delegate); + return manager; +} + NS_CC_EXT_END; diff --git a/extensions/AssetsManager/AssetsManager.h b/extensions/AssetsManager/AssetsManager.h index b150e073e0..c9603f0188 100644 --- a/extensions/AssetsManager/AssetsManager.h +++ b/extensions/AssetsManager/AssetsManager.h @@ -26,7 +26,7 @@ #define __AssetsManager__ #include -#include + #include #include "cocos2d.h" @@ -41,7 +41,7 @@ class AssetsManagerDelegateProtocol; * The updated package should be a zip file. And there should be a file named * version in the server, which contains version code. */ -class AssetsManager +class AssetsManager : public Node { public: enum class ErrorCode @@ -77,6 +77,14 @@ public: virtual ~AssetsManager(); + typedef std::function ErrorCallback; + typedef std::function ProgressCallback; + typedef std::function SuccessCallback; + + /* @brief To access within scripting environment + */ + static AssetsManager* create(const char* packageUrl, const char* versionFileUrl, const char* storagePath, ErrorCallback errorCallback, ProgressCallback progressCallback, SuccessCallback successCallback ); + /* @brief Check out if there is a new version resource. * You may use this method before updating, then let user determine whether * he wants to update resources. @@ -172,6 +180,7 @@ private: std::list *_messageQueue; std::mutex _messageQueueMutex; }; + private: //! The path to store downloaded resources. @@ -185,16 +194,17 @@ private: std::string _downloadedVersion; - CURL *_curl; + void *_curl; + Helper *_schedule; unsigned int _connectionTimeout; - AssetsManagerDelegateProtocol *_delegate; // weak reference + AssetsManagerDelegateProtocol *_delegate; bool _isDownloading; }; -class AssetsManagerDelegateProtocol +class AssetsManagerDelegateProtocol : public Object { public: /* @brief Call back function for error diff --git a/extensions/cocos-ext.h b/extensions/cocos-ext.h index 716f269065..13f5d25ee5 100644 --- a/extensions/cocos-ext.h +++ b/extensions/cocos-ext.h @@ -56,4 +56,6 @@ #include "CCDeprecated-ext.h" +#include "AssetsManager/AssetsManager.h" + #endif /* __COCOS2D_EXT_H__ */ diff --git a/tools/tojs/cocos2dx_extension.ini b/tools/tojs/cocos2dx_extension.ini index 6a31b61f3c..6c812165bc 100644 --- a/tools/tojs/cocos2dx_extension.ini +++ b/tools/tojs/cocos2dx_extension.ini @@ -13,7 +13,7 @@ android_flags = -D_SIZE_T_DEFINED_ clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include clang_flags = -nostdinc -x c++ -std=c++11 -cocos_headers = -I%(cocosdir)s/cocos2dx/include -I%(cocosdir)s/cocos2dx/platform -I%(cocosdir)s/cocos2dx/platform/android -I%(cocosdir)s/cocos2dx -I%(cocosdir)s/cocos2dx/kazmath/include -I%(cocosdir)s/extensions +cocos_headers = -I%(cocosdir)s/cocos2dx/include -I%(cocosdir)s/cocos2dx/platform -I%(cocosdir)s/cocos2dx/platform/android -I%(cocosdir)s/cocos2dx -I%(cocosdir)s/cocos2dx/kazmath/include -I%(cocosdir)s/extensions cocos_flags = -DANDROID -DCOCOS2D_JAVASCRIPT cxxgenerator_headers = -I%(cxxgeneratordir)s/targets/spidermonkey/common @@ -26,7 +26,7 @@ headers = %(cocosdir)s/extensions/cocos-ext.h # what classes to produce code for. You can use regular expressions here. When testing the regular # expression, it will be enclosed in "^$", like this: "^Menu*$". -classes = CCBReader.* CCBAnimationManager.* Scale9Sprite Control$ ControlButton.* ScrollView$ TableView$ TableViewCell$ EditBox$ +classes = AssetsManager.* CCBReader.* CCBAnimationManager.* Scale9Sprite Control$ ControlButton.* ScrollView$ TableView$ TableViewCell$ EditBox$ # what should we skip? in the format ClassName::[function function] # ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also @@ -43,6 +43,7 @@ skip = CCBReader::[^CCBReader$ addOwnerCallbackName isJSControlled readByte getC *::[^visit$ copyWith.* onEnter.* onExit.* ^description$ getObjectType], EditBox::[(g|s)etDelegate ^keyboard.* touchDownAction getScriptEditBoxHandler registerScriptEditBoxHandler unregisterScriptEditBoxHandler], TableView::[create (g|s)etDataSource$ (g|s)etDelegate], + AssetsManager::[setDelegate], Control::[removeHandleOfControlEvent addHandleOfControlEvent] rename_functions = CCBReader::[getAnimationManager=getActionManager setAnimationManager=setActionManager],