From 32fbced31f462f5b65d14286945ee2c3bc379c32 Mon Sep 17 00:00:00 2001 From: minggo Date: Fri, 22 Feb 2013 16:02:53 +0800 Subject: [PATCH] issue #1489:modify sample code and add readme --- extensions/AssetsManager/AssetsManager.cpp | 10 +++ extensions/AssetsManager/AssetsManager.h | 21 ++++- .../AssetsManagerTest/Classes/AppDelegate.cpp | 83 ++++++++++++++----- .../AssetsManagerTest/Classes/AppDelegate.h | 7 +- samples/Cpp/AssetsManagerTest/README.md | 31 +++++++ .../project.pbxproj.REMOVED.git-id | 2 +- 6 files changed, 127 insertions(+), 27 deletions(-) create mode 100644 samples/Cpp/AssetsManagerTest/README.md diff --git a/extensions/AssetsManager/AssetsManager.cpp b/extensions/AssetsManager/AssetsManager.cpp index ec4737dbc3..56772eab90 100644 --- a/extensions/AssetsManager/AssetsManager.cpp +++ b/extensions/AssetsManager/AssetsManager.cpp @@ -394,3 +394,13 @@ void AssetsManager::setVersionFileUrl(const char *versionFileUrl) { _versionFileUrl = versionFileUrl; } + +string AssetsManager::getVersion() +{ + return CCUserDefault::sharedUserDefault()->getStringForKey(KEY_OF_VERSION); +} + +void AssetsManager::deleteVersion() +{ + CCUserDefault::sharedUserDefault()->setStringForKey(KEY_OF_VERSION, ""); +} diff --git a/extensions/AssetsManager/AssetsManager.h b/extensions/AssetsManager/AssetsManager.h index 105f535c17..d599b3ba54 100644 --- a/extensions/AssetsManager/AssetsManager.h +++ b/extensions/AssetsManager/AssetsManager.h @@ -69,21 +69,35 @@ public: */ virtual bool checkUpdate(); + /* @brief Download new package if there is a new version, and uncompress downloaded zip file. + * Ofcourse it will set search path that stores downloaded files. + */ virtual void update(); /* @brief Gets url of package. */ const char* getPackageUrl() const; - /* @brief Sets url of package. - * - * @param packageUrl Package url. + /* @brief Sets package url. */ void setPackageUrl(const char* packageUrl); + /* @brief Gets version file url. + */ const char* getVersionFileUrl() const; + + /* @brief Gets version file url. + */ void setVersionFileUrl(const char* versionFileUrl); + /* @brief Gets current version code. + */ + std::string getVersion(); + + /* @brief Deletes recorded version code. + */ + void deleteVersion(); + /* @brief Gets storage path. */ const char* getStoragePath() const; @@ -91,6 +105,7 @@ public: /* @brief Sets storage path. * * @param storagePath The path to store downloaded resources. + * @warm The path should be a valid path. */ void setStoragePath(const char* storagePath); diff --git a/samples/Cpp/AssetsManagerTest/Classes/AppDelegate.cpp b/samples/Cpp/AssetsManagerTest/Classes/AppDelegate.cpp index 38c6ec8de6..5bbd0ec7f1 100644 --- a/samples/Cpp/AssetsManagerTest/Classes/AppDelegate.cpp +++ b/samples/Cpp/AssetsManagerTest/Classes/AppDelegate.cpp @@ -2,9 +2,6 @@ // AssetsManagerTestAppDelegate.cpp // AssetsManagerTest // -// Created by minggo on 2/5/13. -// Copyright __MyCompanyName__ 2013. All rights reserved. -// #include "AppDelegate.h" @@ -17,6 +14,11 @@ #include "js_bindings_system_registration.h" #include "js_bindings_ccbreader.h" +#if (CC_TARGET_PLATFORM != CC_PLATFORM_W32) +#include +#include +#endif + USING_NS_CC; using namespace CocosDenshion; @@ -77,47 +79,74 @@ void AppDelegate::applicationWillEnterForeground() } UpdateLayer::UpdateLayer() -: pAssetManager(NULL) -, pItemEnter(NULL) +: pItemEnter(NULL) , pItemReset(NULL) , pItemUpdate(NULL) +, isUpdateItemClicked(false) { init(); } UpdateLayer::~UpdateLayer() { - CC_SAFE_DELETE(pAssetManager); + AssetsManager *pAssetsManager = getAssetsManager(); + CC_SAFE_DELETE(pAssetsManager); } void UpdateLayer::update(cocos2d::CCObject *pSender) { // update resources - pAssetManager = new AssetsManager("http://localhost/package.zip", "http://localhost/version"); - pAssetManager->update(); - delete pAssetManager; - pAssetManager = NULL; + getAssetsManager()->update(); + + isUpdateItemClicked = true; +} + +void UpdateLayer::reset(cocos2d::CCObject *pSender) +{ + // Remove downloaded files + + string command = "rm -r "; + // Path may include space. + command += + "\"" + pathToSave + "\""; + system(command.c_str()); + + // Delete recorded version codes. + getAssetsManager()->deleteVersion(); +} + +void UpdateLayer::enter(cocos2d::CCObject *pSender) +{ + // Should set search resource path before running script if "update" is not clicked. + // Because AssetsManager will set + if (! isUpdateItemClicked) + { + vector searchPaths = CCFileUtils::sharedFileUtils()->getSearchPaths(); + searchPaths.insert(searchPaths.begin(), pathToSave); + CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths); + } - // Run new version CCScriptEngineProtocol *pEngine = ScriptingCore::getInstance(); CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine); ScriptingCore::getInstance()->runScript("main.js"); } -void UpdateLayer::reset(cocos2d::CCObject *pSender) -{ - -} - -void UpdateLayer::enter(cocos2d::CCObject *pSender) -{ - -} - bool UpdateLayer::init() { CCLayer::init(); + pathToSave = CCFileUtils::sharedFileUtils()->getWritablePath(); + pathToSave += "tmpdir"; + // Create the folder if it doesn't exist +#if (CC_TARGET_PLATFORM != CC_PLATFORM_W32) + DIR *pDir = NULL; + + pDir = opendir (pathToSave.c_str()); + if (! pDir) + { + mkdir(pathToSave.c_str(), S_IRWXU | S_IRWXG | S_IRWXO); + } +#endif + CCSize size = CCDirector::sharedDirector()->getWinSize(); pItemReset = CCMenuItemFont::create("reset", this, menu_selector(UpdateLayer::reset)); @@ -134,3 +163,15 @@ bool UpdateLayer::init() return true; } + +AssetsManager* UpdateLayer::getAssetsManager() +{ + static AssetsManager *pAssetsManager = NULL; + + if (! pAssetsManager) + { + pAssetsManager = new AssetsManager("http://localhost/package.zip", "http://localhost/version", pathToSave.c_str()); + } + + return pAssetsManager; +} diff --git a/samples/Cpp/AssetsManagerTest/Classes/AppDelegate.h b/samples/Cpp/AssetsManagerTest/Classes/AppDelegate.h index d17669a44d..37c6c19f82 100644 --- a/samples/Cpp/AssetsManagerTest/Classes/AppDelegate.h +++ b/samples/Cpp/AssetsManagerTest/Classes/AppDelegate.h @@ -11,7 +11,7 @@ #include "CCApplication.h" #include "cocos2d.h" -#include "cocos-ext.h" +#include "AssetsManager/AssetsManager.h" /** @brief The cocos2d Application. @@ -55,10 +55,13 @@ public: void update(cocos2d::CCObject *pSender); private: - AssetsManager *pAssetManager; + AssetsManager* getAssetsManager(); + cocos2d::CCMenuItemFont *pItemEnter; cocos2d::CCMenuItemFont *pItemReset; cocos2d::CCMenuItemFont *pItemUpdate; + std::string pathToSave; + bool isUpdateItemClicked; }; #endif // _APP_DELEGATE_H_ diff --git a/samples/Cpp/AssetsManagerTest/README.md b/samples/Cpp/AssetsManagerTest/README.md new file mode 100644 index 0000000000..2a7e3fb9c7 --- /dev/null +++ b/samples/Cpp/AssetsManagerTest/README.md @@ -0,0 +1,31 @@ +This sample shows how to use AssetsManager to auto-update application resources(pictures or scripts). + +In this sample, there is a scene which contains three items: +* enter +Start to run script. +* reset +Delete downloaded resources and delete recorded version code. +* update +Download new version of package if it exits. + +You can use this sample like this: +* Run original version application(refered as v1) +``` +start application +click "enter" +``` +* Run new version application(v2) +``` +start application +click "update" +click "enter" +``` +You will find the changes. +* Run v1 again after running v2 +``` +start application +click "reset" +click "enter" +``` +The application turns back to v1. Ofcourse you can run v2 again as mentioned abave. + \ No newline at end of file diff --git a/samples/Cpp/AssetsManagerTest/proj.ios/AssetsManagerTest.xcodeproj/project.pbxproj.REMOVED.git-id b/samples/Cpp/AssetsManagerTest/proj.ios/AssetsManagerTest.xcodeproj/project.pbxproj.REMOVED.git-id index 1eed59d543..9fdaf094fc 100644 --- a/samples/Cpp/AssetsManagerTest/proj.ios/AssetsManagerTest.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/samples/Cpp/AssetsManagerTest/proj.ios/AssetsManagerTest.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -b7ecc45522a1c4a589430ee428a083cdc9b06384 \ No newline at end of file +7b99f04b04c6534ba434c52ffa985fab9e89c0dd \ No newline at end of file