added jsb support for AssetsMAnager

This commit is contained in:
성낙호 2013-08-07 14:07:01 +09:00
parent 0883723c71
commit 1c91e73a8c
4 changed files with 60 additions and 7 deletions

View File

@ -88,6 +88,10 @@ AssetsManager::AssetsManager(const char* packageUrl/* =NULL */, const char* vers
AssetsManager::~AssetsManager() AssetsManager::~AssetsManager()
{ {
if (_delegate)
{
_delegate->release();
}
if (_schedule) if (_schedule)
{ {
_schedule->release(); _schedule->release();
@ -479,7 +483,17 @@ void AssetsManager::deleteVersion()
void AssetsManager::setDelegate(AssetsManagerDelegateProtocol *delegate) void AssetsManager::setDelegate(AssetsManagerDelegateProtocol *delegate)
{ {
if (_delegate)
{
_delegate->release();
}
_delegate = delegate; _delegate = delegate;
if (_delegate)
{
_delegate->retain();
}
} }
void AssetsManager::setConnectionTimeout(unsigned int timeout) void AssetsManager::setConnectionTimeout(unsigned int timeout)
@ -604,4 +618,30 @@ void AssetsManager::Helper::handleUpdateSucceed(Message *msg)
if (manager) manager->_delegate->onSuccess(); 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; NS_CC_EXT_END;

View File

@ -26,7 +26,7 @@
#define __AssetsManager__ #define __AssetsManager__
#include <string> #include <string>
#include <curl/curl.h>
#include <mutex> #include <mutex>
#include "cocos2d.h" #include "cocos2d.h"
@ -41,7 +41,7 @@ class AssetsManagerDelegateProtocol;
* The updated package should be a zip file. And there should be a file named * The updated package should be a zip file. And there should be a file named
* version in the server, which contains version code. * version in the server, which contains version code.
*/ */
class AssetsManager class AssetsManager : public Node
{ {
public: public:
enum class ErrorCode enum class ErrorCode
@ -77,6 +77,14 @@ public:
virtual ~AssetsManager(); virtual ~AssetsManager();
typedef std::function<void(int)> ErrorCallback;
typedef std::function<void(int)> ProgressCallback;
typedef std::function<void(void)> 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. /* @brief Check out if there is a new version resource.
* You may use this method before updating, then let user determine whether * You may use this method before updating, then let user determine whether
* he wants to update resources. * he wants to update resources.
@ -173,6 +181,7 @@ private:
std::mutex _messageQueueMutex; std::mutex _messageQueueMutex;
}; };
private: private:
//! The path to store downloaded resources. //! The path to store downloaded resources.
std::string _storagePath; std::string _storagePath;
@ -185,16 +194,17 @@ private:
std::string _downloadedVersion; std::string _downloadedVersion;
CURL *_curl; void *_curl;
Helper *_schedule; Helper *_schedule;
unsigned int _connectionTimeout; unsigned int _connectionTimeout;
AssetsManagerDelegateProtocol *_delegate; // weak reference AssetsManagerDelegateProtocol *_delegate;
bool _isDownloading; bool _isDownloading;
}; };
class AssetsManagerDelegateProtocol class AssetsManagerDelegateProtocol : public Object
{ {
public: public:
/* @brief Call back function for error /* @brief Call back function for error

View File

@ -56,4 +56,6 @@
#include "CCDeprecated-ext.h" #include "CCDeprecated-ext.h"
#include "AssetsManager/AssetsManager.h"
#endif /* __COCOS2D_EXT_H__ */ #endif /* __COCOS2D_EXT_H__ */

View File

@ -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 # 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*$". # 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] # 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 # 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], *::[^visit$ copyWith.* onEnter.* onExit.* ^description$ getObjectType],
EditBox::[(g|s)etDelegate ^keyboard.* touchDownAction getScriptEditBoxHandler registerScriptEditBoxHandler unregisterScriptEditBoxHandler], EditBox::[(g|s)etDelegate ^keyboard.* touchDownAction getScriptEditBoxHandler registerScriptEditBoxHandler unregisterScriptEditBoxHandler],
TableView::[create (g|s)etDataSource$ (g|s)etDelegate], TableView::[create (g|s)etDataSource$ (g|s)etDelegate],
AssetsManager::[setDelegate],
Control::[removeHandleOfControlEvent addHandleOfControlEvent] Control::[removeHandleOfControlEvent addHandleOfControlEvent]
rename_functions = CCBReader::[getAnimationManager=getActionManager setAnimationManager=setActionManager], rename_functions = CCBReader::[getAnimationManager=getActionManager setAnimationManager=setActionManager],