//Update.cpp
#if(CC_TARGET_PLATFORM!=CC_PLATFORM_WIN32)
#include<dirent.h>
#include<sys/stat.h>
#endif
USING_NS_CC;
USING_NS_CC_EXT;
#defineDOWNLOAD_FILE"download"
Update::Update():
_pathToSave(
""
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
_showDownloadInfo(NULL)
{
}
Update::~Update()
{
AssetsManager*assetManager=getAssetsManager();
CC_SAFE_DELETE(assetManager);
}
Update::init()
{
(!Layer::init()){
return
false
;
}
SizewinSize=Director::getInstance()->getWinSize();
initDownloadDir();
_showDownloadInfo=Label::createWithSystemFont(
"Arial"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,20);
_showDownloadInfo->setPosition(Vec2(winSize.width/2,winSize.height/2-20));
->addChild(_showDownloadInfo);
autoitemLabel1=MenuItemLabel::create(
Label::createWithSystemFont(
"Reset"
"arail"
));
autoitemLabel2=MenuItemLabel::create(
"Update"
));
automenu=Menu::create(itemLabel1,itemLabel2,NULL);
->addChild(menu);
itemLabel1->setPosition(Vec2(winSize.width/2,winSize.height/2+20));
itemLabel2->setPosition(Vec2(winSize.width/2,winSize.height/2));
menu->setPosition(Vec2::ZERO);
true
;
}
Update::onError(AssetsManager::ErrorCodecode)
{
switch
(code){
case
cocos2d::extension::AssetsManager::ErrorCode::NO_NEW_VERSION:
_showDownloadInfo->setString(
"nonewversion"
);
break
;
cocos2d::extension::AssetsManager::ErrorCode::NETWORK:
@H_502_1677@
);
;
cocos2d::extension::AssetsManager::ErrorCode::CREATE_FILE:
"createfileerror"
);
;
default
:
;
}
}
Update::onProgress(
percent)
{
(percent<0){
;
}
char
progress[20];
snprintf(progress,monospace!important; font-size:1em!important; min-height:inherit!important; color:blue!important; background:none!important">"download%d%%"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,percent);
_showDownloadInfo->setString(progress);
}
Update::onSuccess()
{
"downloadsuccess"
);
);
std::stringpath=FileUtils::getInstance()->getWritablePath()+DOWNLOAD_FILE;
LuaEngine*pEngine=LuaEngine::getInstance();
pEngine->addSearchPath(_pathToSave.c_str());
"/src/main.lua"
;
pEngine->executeScriptFile(path.c_str());
}
AssetsManager*Update::getAssetsManager()
{
AssetsManager*assetManager=NULL;
(!assetManager){
/*创建AssetsManager对象
*/
AssetsManager(
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
//设置AssetsManager对象的回调对象
);
//设置AssetsManager对象的timeout时间
assetManager->setConnectionTimeout(3);
}
assetManager;
}
Update::initDownloadDir()
{
"initDownloadDir"
);
_pathToSave=FileUtils::getInstance()->getWritablePath();
_pathToSave+=DOWNLOAD_FILE;
"Path:%s"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,_pathToSave.c_str());
#if(CC_TARGET_PLATFORM!=CC_PLATFORM_WIN32)
DIR*pDir=NULL;
pDir=opendir(_pathToSave.c_str());
(!pDir){
mkdir(_pathToSave.c_str(),S_IRWXU|S_IRWXG|S_IRWXO);
}
#else
((GetFileAttributes(_pathToSave.c_str()))=INVALID_FILE_ATTRIBUTES){
CreateDirectoryA(_pathToSave.c_str(),0);
}
#endif
"initDownloadDirend"
);
}
Update::reset(Ref*pSender)
{
);
//Removedownloadedfiles
#if(CC_TARGET_PLATFORM!=CC_PLATFORM_WIN32)
std::stringcommand=
"rm-r"
;
//Pathmayincludespace.
command+=
"\""
+_pathToSave+
;
#else
"rd/s/q"
;
//Pathmayincludespace.
;
#endif
getAssetsManager()->deleteVersion();
initDownloadDir();
}
@H_404_2153@
Update::update(Ref*pSender)
{
);
getAssetsManager()->update();
}
可以点击下载下来网络资源压缩包,看下压缩包内包含的内容。
可以点击下载下来源代码压缩包,对应一起的。
大家最好可以在本地搭建一个apache服务器,做一下练习。
转自:http://www.cnblogs.com/daxiaxiaohao/p/4162400.html
|