fixed #1489:sample works ok

This commit is contained in:
minggo 2013-02-26 16:29:52 +08:00
parent 7941d70a04
commit d35af7b6fe
4 changed files with 36 additions and 42 deletions

View File

@ -52,7 +52,6 @@ AssetsManager::AssetsManager()
, _versionFileUrl("") , _versionFileUrl("")
, _version("") , _version("")
, _curl(NULL) , _curl(NULL)
, _delegate(NULL)
{ {
_storagePath = CCFileUtils::sharedFileUtils()->getWritablePath(); _storagePath = CCFileUtils::sharedFileUtils()->getWritablePath();
checkStoragePath(); checkStoragePath();
@ -62,7 +61,6 @@ AssetsManager::AssetsManager(const char* packageUrl, const char* versionFileUrl)
: _packageUrl(packageUrl) : _packageUrl(packageUrl)
, _version("") , _version("")
, _versionFileUrl(versionFileUrl) , _versionFileUrl(versionFileUrl)
, _delegate(NULL)
, _curl(NULL) , _curl(NULL)
{ {
_storagePath = CCFileUtils::sharedFileUtils()->getWritablePath(); _storagePath = CCFileUtils::sharedFileUtils()->getWritablePath();
@ -74,7 +72,6 @@ AssetsManager::AssetsManager(const char* packageUrl, const char* versionFileUrl,
, _version("") , _version("")
, _versionFileUrl(versionFileUrl) , _versionFileUrl(versionFileUrl)
, _storagePath(storagePath) , _storagePath(storagePath)
, _delegate(NULL)
, _curl(NULL) , _curl(NULL)
{ {
checkStoragePath(); checkStoragePath();
@ -119,7 +116,7 @@ bool AssetsManager::checkUpdate()
if (res != 0) if (res != 0)
{ {
CCLOG("can not get version file content"); CCLOG("can not get version file content, error code is %d", res);
curl_easy_cleanup(_curl); curl_easy_cleanup(_curl);
return false; return false;
} }
@ -275,7 +272,7 @@ bool AssetsManager::uncompress()
error = unzReadCurrentFile(zipfile, readBuffer, BUFFER_SIZE); error = unzReadCurrentFile(zipfile, readBuffer, BUFFER_SIZE);
if (error < 0) if (error < 0)
{ {
CCLOG("error when read zip file %s, error code is %d", fileName, error); CCLOG("can not read zip file %s, error code is %d", fileName, error);
unzCloseCurrentFile(zipfile); unzCloseCurrentFile(zipfile);
unzClose(zipfile); unzClose(zipfile);
return false; return false;
@ -304,6 +301,8 @@ bool AssetsManager::uncompress()
} }
} }
CCLOG("end uncompressing");
return true; return true;
} }
@ -349,7 +348,8 @@ static size_t downLoadPackage(void *ptr, size_t size, size_t nmemb, void *userda
static int progressFunc(void *ptr, double totalToDownload, double nowDownloaded, double totalToUpLoad, double nowUpLoaded) static int progressFunc(void *ptr, double totalToDownload, double nowDownloaded, double totalToUpLoad, double nowUpLoaded)
{ {
cocos2d::CCLog("progress %d", int(nowDownloaded/totalToDownload)); CCLOG("downloading... %d%%", (int)(nowDownloaded/totalToDownload*100));
return 0; return 0;
} }
@ -380,6 +380,8 @@ bool AssetsManager::downLoad()
return false; return false;
} }
CCLOG("succeed downloading package %s", _packageUrl.c_str());
fclose(fp); fclose(fp);
return true; return true;
} }

View File

@ -28,7 +28,7 @@
#include <string> #include <string>
#include <curl/curl.h> #include <curl/curl.h>
class AssetsManagerDelegate; #include "cocos2d.h"
/* /*
* This class is used to auto update resources, such as pictures or scripts. * This class is used to auto update resources, such as pictures or scripts.
@ -126,25 +126,7 @@ private:
std::string _packageUrl; std::string _packageUrl;
std::string _versionFileUrl; std::string _versionFileUrl;
AssetsManagerDelegate* _delegate;
CURL *_curl; CURL *_curl;
}; };
/* @brief This class is used as base class of the delegate of AssetsManager.
*/
class AssetsManagerDelegate
{
public:
/* @brief When an error happens in updating resources, AssetsManager will invoke its delegate's onError().
*
* @param errorCode The pointer to record error code.
* The value will be set by AssetsManager.
*/
virtual void onError(AssetsManager::ErrorCode* errorCode) = 0;
virtual void onUpdate(float* percent) = 0;
};
#endif /* defined(__AssetsManager__) */ #endif /* defined(__AssetsManager__) */

View File

@ -117,6 +117,8 @@ void UpdateLayer::reset(cocos2d::CCObject *pSender)
#endif #endif
// Delete recorded version codes. // Delete recorded version codes.
getAssetsManager()->deleteVersion(); getAssetsManager()->deleteVersion();
createDownloadedDir();
} }
void UpdateLayer::enter(cocos2d::CCObject *pSender) void UpdateLayer::enter(cocos2d::CCObject *pSender)
@ -139,23 +141,7 @@ bool UpdateLayer::init()
{ {
CCLayer::init(); CCLayer::init();
pathToSave = CCFileUtils::sharedFileUtils()->getWritablePath(); createDownloadedDir();
pathToSave += "tmpdir";
// Create the folder if it doesn't exist
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
DIR *pDir = NULL;
pDir = opendir (pathToSave.c_str());
if (! pDir)
{
mkdir(pathToSave.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
}
#else
if ((GetFileAttributesA(pathToSave.c_str())) == INVALID_FILE_ATTRIBUTES)
{
CreateDirectoryA(pathToSave.c_str(), 0);
}
#endif
CCSize size = CCDirector::sharedDirector()->getWinSize(); CCSize size = CCDirector::sharedDirector()->getWinSize();
@ -187,3 +173,25 @@ AssetsManager* UpdateLayer::getAssetsManager()
return pAssetsManager; return pAssetsManager;
} }
void UpdateLayer::createDownloadedDir()
{
pathToSave = CCFileUtils::sharedFileUtils()->getWritablePath();
pathToSave += "tmpdir";
// Create the folder if it doesn't exist
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
DIR *pDir = NULL;
pDir = opendir (pathToSave.c_str());
if (! pDir)
{
mkdir(pathToSave.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
}
#else
if ((GetFileAttributesA(pathToSave.c_str())) == INVALID_FILE_ATTRIBUTES)
{
CreateDirectoryA(pathToSave.c_str(), 0);
}
#endif
}

View File

@ -50,12 +50,14 @@ public:
UpdateLayer(); UpdateLayer();
~UpdateLayer(); ~UpdateLayer();
virtual bool init(); virtual bool init();
void enter(cocos2d::CCObject *pSender); void enter(cocos2d::CCObject *pSender);
void reset(cocos2d::CCObject *pSender); void reset(cocos2d::CCObject *pSender);
void update(cocos2d::CCObject *pSender); void update(cocos2d::CCObject *pSender);
private: private:
AssetsManager* getAssetsManager(); AssetsManager* getAssetsManager();
void createDownloadedDir();
cocos2d::CCMenuItemFont *pItemEnter; cocos2d::CCMenuItemFont *pItemEnter;
cocos2d::CCMenuItemFont *pItemReset; cocos2d::CCMenuItemFont *pItemReset;