#include "Update.h"
#if(CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
#include <dirent.h>
#include <sys/stat.h>
#endif
USING_NS_CC;
USING_NS_CC_EXT;
#define DOWNLOAD_FILE "download"
#include "CCLuaEngine.h"
Update::Update():
_pathToSave(
""
),
_showDownloadInfo(NULL)
{
}
Update::~Update()
{
AssetsManager *assetManager = getAssetsManager();
CC_SAFE_DELETE(assetManager);
}
bool
Update::init()
{
if
(!Layer::init()) {
return
false
;
}
Size winSize = Director::getInstance()->getWinSize();
initDownloadDir();
_showDownloadInfo = Label::createWithSystemFont(
""
,
"Arial"
, 20);
_showDownloadInfo->setPosition(Vec2(winSize.width / 2,winSize.height / 2 - 20));
this
->addChild(_showDownloadInfo);
auto
itemLabel1 = MenuItemLabel::create(
Label::createWithSystemFont(
"Reset"
,
"Arail"
, 20), CC_CALLBACK_1(Update::reset,
this
));
auto
itemLabel2 = MenuItemLabel::create(
Label::createWithSystemFont(
"Update"
,
"Arail"
, 20), CC_CALLBACK_1(Update::update,
this
));
auto
menu = Menu::create(itemLabel1,itemLabel2, NULL);
this
->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);
return
true
;
}
void
Update::onError(AssetsManager::ErrorCode code)
{
switch
(code) {
case
cocos2d::extension::AssetsManager::ErrorCode::NO_NEW_VERSION:
_showDownloadInfo->setString(
"no new version"
);
break
;
case
cocos2d::extension::AssetsManager::ErrorCode::NETWORK:
_showDownloadInfo->setString(
"no new version"
);
break
;
case
cocos2d::extension::AssetsManager::ErrorCode::CREATE_FILE:
_showDownloadInfo->setString(
"create file error"
);
break
;
default
:
break
;
}
}
void
Update::onProgress(
int
percent)
{
if
(percent < 0) {
return
;
}
char
progress[20];
snprintf(progress, 20,
"download %d%%"
,percent);
_showDownloadInfo->setString(progress);
}
void
Update::onSuccess()
{
CCLOG(
"download success"
);
_showDownloadInfo->setString(
"download success"
);
std::string path = FileUtils::getInstance()->getWritablePath() + DOWNLOAD_FILE;
LuaEngine* pEngine = LuaEngine::getInstance();
pEngine->addSearchPath(_pathToSave.c_str());
path +=
"/src/main.lua"
;
pEngine->executeScriptFile(path.c_str());
}
AssetsManager* Update::getAssetsManager()
{
static
AssetsManager *assetManager = NULL;
if
(!assetManager) {
assetManager =
new
AssetsManager(
_pathToSave.c_str());
assetManager->setDelegate(
this
);
assetManager->setConnectionTimeout(3);
}
return
assetManager;
}
void
Update::initDownloadDir()
{
CCLOG(
"initDownloadDir"
);
_pathToSave = FileUtils::getInstance()->getWritablePath();
_pathToSave += DOWNLOAD_FILE;
CCLOG(
"Path: %s"
,_pathToSave.c_str());
#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
((GetFileAttributes(_pathToSave.c_str())) = INVALID_FILE_ATTRIBUTES) {
CreateDirectoryA(_pathToSave.c_str(),0);
}
#endif
CCLOG(
"initDownloadDir end"
);
}
void
Update::reset(Ref *pSender)
{
_showDownloadInfo->setString(
""
);
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
std::string command =
"rm -r "
;
command +=
"\""
+ _pathToSave +
"\""
;
system
(command.c_str());
#else
std::string command =
"rd /s /q "
;
command +=
"\""
+ _pathToSave +
"\""
;
system
(command.c_str());
#endif
getAssetsManager()->deleteVersion();
initDownloadDir();
}
void
Update::update(Ref *pSender)
{
_showDownloadInfo->setString(
""
);
getAssetsManager()->update();
}