//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"
#include"CCLuaEngine.h"
Update::Update():
_pathToSave(
""
_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"
_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:
);
;
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%%"
_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对象
*@param资源包的下载路径
*@param资源包的当前版本
*@param资源包下载后的存储路径
*/
AssetsManager(
_pathToSave.c_str());
//设置AssetsManager对象的回调对象
);
//设置AssetsManager对象的timeout时间
assetManager->setConnectionTimeout(3);
}
assetManager;
}
Update::initDownloadDir()
{
"initDownloadDir"
);
_pathToSave=FileUtils::getInstance()->getWritablePath();
_pathToSave+=DOWNLOAD_FILE;
"Path:%s"
#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+
;
system
(command.c_str());
#else
"rd/s/q"
;
//Pathmayincludespace.
;
(command.c_str());
#endif
getAssetsManager()->deleteVersion();
initDownloadDir();
}
Update::update(Ref*pSender)
{
);
getAssetsManager()->update();
}
可以点击下载下来网络资源压缩包,看下压缩包内包含的内容。
可以点击下载下来源代码压缩包,对应一起的。
大家最好可以在本地搭建一个apache服务器,做一下练习。
转自:http://www.cnblogs.com/daxiaxiaohao/p/4162400.html
|