2013-02-22 11:04:09 +08:00
|
|
|
//
|
|
|
|
// AssetsManagerTestAppDelegate.cpp
|
|
|
|
// AssetsManagerTest
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "AppDelegate.h"
|
|
|
|
|
|
|
|
#include "cocos2d.h"
|
|
|
|
#include "SimpleAudioEngine.h"
|
|
|
|
#include "ScriptingCore.h"
|
2013-03-13 05:18:45 +08:00
|
|
|
#include "generated/jsb_cocos2dx_auto.hpp"
|
2013-02-22 11:04:09 +08:00
|
|
|
#include "cocos2d_specifics.hpp"
|
|
|
|
|
2013-02-25 18:03:38 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
|
2013-02-22 16:02:53 +08:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
USING_NS_CC;
|
2013-02-26 16:43:46 +08:00
|
|
|
USING_NS_CC_EXT;
|
2013-02-22 11:04:09 +08:00
|
|
|
using namespace CocosDenshion;
|
|
|
|
|
|
|
|
AppDelegate::AppDelegate()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
AppDelegate::~AppDelegate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AppDelegate::applicationDidFinishLaunching()
|
|
|
|
{
|
|
|
|
// initialize director
|
2013-07-23 08:25:44 +08:00
|
|
|
Director *director = Director::getInstance();
|
|
|
|
director->setOpenGLView(EGLView::getInstance());
|
2013-02-22 11:04:09 +08:00
|
|
|
|
|
|
|
// turn on display FPS
|
2013-07-23 08:25:44 +08:00
|
|
|
//director->setDisplayStats(true);
|
2013-02-22 11:04:09 +08:00
|
|
|
|
|
|
|
// set FPS. the default value is 1.0/60 if you don't call this
|
2013-07-23 08:25:44 +08:00
|
|
|
director->setAnimationInterval(1.0 / 60);
|
2013-02-22 11:04:09 +08:00
|
|
|
|
|
|
|
ScriptingCore* sc = ScriptingCore::getInstance();
|
|
|
|
sc->addRegisterCallback(register_all_cocos2dx);
|
|
|
|
sc->addRegisterCallback(register_cocos2dx_js_extensions);
|
|
|
|
|
|
|
|
sc->start();
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Scene *scene = Scene::create();
|
2013-02-22 11:04:09 +08:00
|
|
|
UpdateLayer *updateLayer = new UpdateLayer();
|
|
|
|
scene->addChild(updateLayer);
|
|
|
|
updateLayer->release();
|
|
|
|
|
2013-07-23 08:25:44 +08:00
|
|
|
director->runWithScene(scene);
|
2013-02-22 11:04:09 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
|
|
|
|
void AppDelegate::applicationDidEnterBackground()
|
|
|
|
{
|
2013-07-12 11:50:36 +08:00
|
|
|
Director::getInstance()->stopAnimation();
|
2013-07-25 15:25:11 +08:00
|
|
|
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
|
|
|
|
SimpleAudioEngine::getInstance()->pauseAllEffects();
|
2013-02-22 11:04:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// this function will be called when the app is active again
|
|
|
|
void AppDelegate::applicationWillEnterForeground()
|
|
|
|
{
|
2013-07-12 11:50:36 +08:00
|
|
|
Director::getInstance()->startAnimation();
|
2013-07-25 15:25:11 +08:00
|
|
|
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
|
|
|
|
SimpleAudioEngine::getInstance()->resumeAllEffects();
|
2013-02-22 11:04:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UpdateLayer::UpdateLayer()
|
2013-02-22 16:02:53 +08:00
|
|
|
: pItemEnter(NULL)
|
2013-02-22 11:04:09 +08:00
|
|
|
, pItemReset(NULL)
|
|
|
|
, pItemUpdate(NULL)
|
2013-05-15 17:09:28 +08:00
|
|
|
, pProgressLabel(NULL)
|
2013-02-22 16:02:53 +08:00
|
|
|
, isUpdateItemClicked(false)
|
2013-02-22 11:04:09 +08:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateLayer::~UpdateLayer()
|
|
|
|
{
|
2013-02-22 16:02:53 +08:00
|
|
|
AssetsManager *pAssetsManager = getAssetsManager();
|
|
|
|
CC_SAFE_DELETE(pAssetsManager);
|
2013-02-22 11:04:09 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void UpdateLayer::update(cocos2d::Object *pSender)
|
2013-02-22 11:04:09 +08:00
|
|
|
{
|
2013-05-15 17:09:28 +08:00
|
|
|
pProgressLabel->setString("");
|
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
// update resources
|
2013-02-22 16:02:53 +08:00
|
|
|
getAssetsManager()->update();
|
2013-02-22 11:04:09 +08:00
|
|
|
|
2013-02-22 16:02:53 +08:00
|
|
|
isUpdateItemClicked = true;
|
2013-02-22 11:04:09 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void UpdateLayer::reset(cocos2d::Object *pSender)
|
2013-02-22 11:04:09 +08:00
|
|
|
{
|
2013-05-15 17:09:28 +08:00
|
|
|
pProgressLabel->setString(" ");
|
|
|
|
|
2013-02-22 16:02:53 +08:00
|
|
|
// Remove downloaded files
|
2013-02-25 18:03:38 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
|
2013-02-22 16:02:53 +08:00
|
|
|
string command = "rm -r ";
|
|
|
|
// Path may include space.
|
2013-02-25 18:03:38 +08:00
|
|
|
command += "\"" + pathToSave + "\"";
|
2013-02-22 16:02:53 +08:00
|
|
|
system(command.c_str());
|
2013-02-25 18:03:38 +08:00
|
|
|
#else
|
|
|
|
string command = "rd /s /q ";
|
|
|
|
// Path may include space.
|
|
|
|
command += "\"" + pathToSave + "\"";
|
|
|
|
system(command.c_str());
|
|
|
|
#endif
|
2013-02-22 16:02:53 +08:00
|
|
|
// Delete recorded version codes.
|
|
|
|
getAssetsManager()->deleteVersion();
|
2013-02-26 16:29:52 +08:00
|
|
|
|
|
|
|
createDownloadedDir();
|
2013-02-22 11:04:09 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void UpdateLayer::enter(cocos2d::Object *pSender)
|
2013-02-22 11:04:09 +08:00
|
|
|
{
|
2013-02-22 16:02:53 +08:00
|
|
|
// Should set search resource path before running script if "update" is not clicked.
|
|
|
|
// Because AssetsManager will set
|
|
|
|
if (! isUpdateItemClicked)
|
|
|
|
{
|
2013-07-12 12:03:39 +08:00
|
|
|
vector<string> searchPaths = FileUtils::getInstance()->getSearchPaths();
|
2013-02-22 16:02:53 +08:00
|
|
|
searchPaths.insert(searchPaths.begin(), pathToSave);
|
2013-07-12 12:03:39 +08:00
|
|
|
FileUtils::getInstance()->setSearchPaths(searchPaths);
|
2013-02-22 16:02:53 +08:00
|
|
|
}
|
2013-02-22 11:04:09 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
ScriptEngineProtocol *pEngine = ScriptingCore::getInstance();
|
2013-07-22 17:24:54 +08:00
|
|
|
ScriptEngineManager::getInstance()->setScriptEngine(pEngine);
|
2013-02-22 16:02:53 +08:00
|
|
|
ScriptingCore::getInstance()->runScript("main.js");
|
2013-02-22 11:04:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UpdateLayer::init()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer::init();
|
2013-02-22 11:04:09 +08:00
|
|
|
|
2013-02-26 16:29:52 +08:00
|
|
|
createDownloadedDir();
|
2013-02-22 16:02:53 +08:00
|
|
|
|
2013-07-12 11:50:36 +08:00
|
|
|
Size size = Director::getInstance()->getWinSize();
|
2013-02-22 11:04:09 +08:00
|
|
|
|
2013-06-25 09:20:07 +08:00
|
|
|
pItemReset = MenuItemFont::create("reset", CC_CALLBACK_1(UpdateLayer::reset,this));
|
|
|
|
pItemEnter = MenuItemFont::create("enter", CC_CALLBACK_1(UpdateLayer::enter, this));
|
|
|
|
pItemUpdate = MenuItemFont::create("update", CC_CALLBACK_1(UpdateLayer::update, this));
|
2013-02-22 11:04:09 +08:00
|
|
|
|
2013-07-25 15:25:11 +08:00
|
|
|
pItemEnter->setPosition(Point(size.width/2, size.height/2 + 50));
|
|
|
|
pItemReset->setPosition(Point(size.width/2, size.height/2));
|
|
|
|
pItemUpdate->setPosition(Point(size.width/2, size.height/2 - 50));
|
2013-02-22 11:04:09 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Menu *menu = Menu::create(pItemUpdate, pItemEnter, pItemReset, NULL);
|
2013-07-25 15:25:11 +08:00
|
|
|
menu->setPosition(Point(0,0));
|
2013-02-22 11:04:09 +08:00
|
|
|
addChild(menu);
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
pProgressLabel = LabelTTF::create("", "Arial", 20);
|
2013-07-25 15:25:11 +08:00
|
|
|
pProgressLabel->setPosition(Point(100, 50));
|
2013-05-15 17:09:28 +08:00
|
|
|
addChild(pProgressLabel);
|
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
return true;
|
|
|
|
}
|
2013-02-22 16:02:53 +08:00
|
|
|
|
|
|
|
AssetsManager* UpdateLayer::getAssetsManager()
|
|
|
|
{
|
|
|
|
static AssetsManager *pAssetsManager = NULL;
|
|
|
|
|
|
|
|
if (! pAssetsManager)
|
|
|
|
{
|
2013-02-25 14:33:04 +08:00
|
|
|
pAssetsManager = new AssetsManager("https://raw.github.com/minggo/AssetsManagerTest/master/package.zip",
|
|
|
|
"https://raw.github.com/minggo/AssetsManagerTest/master/version",
|
|
|
|
pathToSave.c_str());
|
2013-05-15 17:09:28 +08:00
|
|
|
pAssetsManager->setDelegate(this);
|
|
|
|
pAssetsManager->setConnectionTimeout(3);
|
2013-02-22 16:02:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return pAssetsManager;
|
|
|
|
}
|
2013-02-26 16:29:52 +08:00
|
|
|
|
|
|
|
void UpdateLayer::createDownloadedDir()
|
|
|
|
{
|
2013-07-12 12:03:39 +08:00
|
|
|
pathToSave = FileUtils::getInstance()->getWritablePath();
|
2013-02-26 16:29:52 +08:00
|
|
|
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
|
|
|
|
}
|
2013-05-15 17:09:28 +08:00
|
|
|
|
|
|
|
void UpdateLayer::onError(AssetsManager::ErrorCode errorCode)
|
|
|
|
{
|
|
|
|
if (errorCode == AssetsManager::kNoNewVersion)
|
|
|
|
{
|
|
|
|
pProgressLabel->setString("no new version");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errorCode == AssetsManager::kNetwork)
|
|
|
|
{
|
|
|
|
pProgressLabel->setString("network error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateLayer::onProgress(int percent)
|
|
|
|
{
|
|
|
|
char progress[20];
|
|
|
|
snprintf(progress, 20, "downloading %d%%", percent);
|
|
|
|
pProgressLabel->setString(progress);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateLayer::onSuccess()
|
|
|
|
{
|
|
|
|
pProgressLabel->setString("download ok");
|
|
|
|
}
|