2013-02-22 11:04:09 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __AssetsManager__
|
|
|
|
#define __AssetsManager__
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2013-02-26 16:29:52 +08:00
|
|
|
#include "cocos2d.h"
|
2013-02-26 16:43:46 +08:00
|
|
|
#include "ExtensionMacros.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
2013-02-22 11:04:09 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This class is used to auto update resources, such as pictures or scripts.
|
|
|
|
* 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
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum ErrorCode
|
|
|
|
{
|
|
|
|
ServerNotAvailable, /** server address error or timeout */
|
|
|
|
TimeOut,
|
|
|
|
};
|
|
|
|
|
|
|
|
//! Default constructor. You should set server address later.
|
|
|
|
AssetsManager();
|
|
|
|
|
|
|
|
/* @brief Creates a AssetsManager with new package url and version code url.
|
|
|
|
* AssetsManager will use the value returned by CCFileUtils::getWritablePath() as storage path.
|
|
|
|
*
|
|
|
|
* @param packageUrl URL of new package, the package should be a zip file.
|
|
|
|
* @param versionFileUrl URL of version file. It should contain version code of new package.
|
|
|
|
*/
|
|
|
|
AssetsManager(const char* packageUrl, const char* versionFileUrl);
|
|
|
|
|
|
|
|
/* @brief Creates a AssetsManager with new package url, version code url and storage path.
|
|
|
|
*
|
|
|
|
* @param packageUrl URL of new package, the package should be a zip file.
|
|
|
|
* @param versionFileUrl URL of version file. It should contain version code of new package.
|
|
|
|
* @param storagePath The path to store downloaded resources.
|
|
|
|
*/
|
|
|
|
AssetsManager(const char* packageUrl, const char* versionFileUrl, const char* storagePath);
|
|
|
|
|
|
|
|
/* @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.
|
|
|
|
*/
|
|
|
|
virtual bool checkUpdate();
|
|
|
|
|
2013-02-22 16:02:53 +08:00
|
|
|
/* @brief Download new package if there is a new version, and uncompress downloaded zip file.
|
|
|
|
* Ofcourse it will set search path that stores downloaded files.
|
|
|
|
*/
|
2013-02-22 11:04:09 +08:00
|
|
|
virtual void update();
|
|
|
|
|
|
|
|
/* @brief Gets url of package.
|
|
|
|
*/
|
|
|
|
const char* getPackageUrl() const;
|
|
|
|
|
2013-02-22 16:02:53 +08:00
|
|
|
/* @brief Sets package url.
|
2013-02-22 11:04:09 +08:00
|
|
|
*/
|
|
|
|
void setPackageUrl(const char* packageUrl);
|
|
|
|
|
2013-02-22 16:02:53 +08:00
|
|
|
/* @brief Gets version file url.
|
|
|
|
*/
|
2013-02-22 11:04:09 +08:00
|
|
|
const char* getVersionFileUrl() const;
|
2013-02-22 16:02:53 +08:00
|
|
|
|
|
|
|
/* @brief Gets version file url.
|
|
|
|
*/
|
2013-02-22 11:04:09 +08:00
|
|
|
void setVersionFileUrl(const char* versionFileUrl);
|
|
|
|
|
2013-02-22 16:02:53 +08:00
|
|
|
/* @brief Gets current version code.
|
|
|
|
*/
|
|
|
|
std::string getVersion();
|
|
|
|
|
|
|
|
/* @brief Deletes recorded version code.
|
|
|
|
*/
|
|
|
|
void deleteVersion();
|
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
/* @brief Gets storage path.
|
|
|
|
*/
|
|
|
|
const char* getStoragePath() const;
|
|
|
|
|
|
|
|
/* @brief Sets storage path.
|
|
|
|
*
|
|
|
|
* @param storagePath The path to store downloaded resources.
|
2013-02-22 16:02:53 +08:00
|
|
|
* @warm The path should be a valid path.
|
2013-02-22 11:04:09 +08:00
|
|
|
*/
|
|
|
|
void setStoragePath(const char* storagePath);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool downLoad();
|
|
|
|
void checkStoragePath();
|
|
|
|
bool uncompress();
|
|
|
|
bool createDirectory(const char *path);
|
|
|
|
void setSearchPath();
|
|
|
|
|
|
|
|
private:
|
|
|
|
//! The path to store downloaded resources.
|
|
|
|
std::string _storagePath;
|
|
|
|
|
|
|
|
//! The version of downloaded resources.
|
|
|
|
std::string _version;
|
|
|
|
|
|
|
|
std::string _packageUrl;
|
|
|
|
std::string _versionFileUrl;
|
|
|
|
|
|
|
|
CURL *_curl;
|
|
|
|
};
|
|
|
|
|
2013-02-26 16:43:46 +08:00
|
|
|
NS_CC_EXT_END;
|
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
#endif /* defined(__AssetsManager__) */
|