issue #1489:modify sample code and add readme

This commit is contained in:
minggo 2013-02-22 16:02:53 +08:00
parent ed1cbb94c2
commit 32fbced31f
6 changed files with 127 additions and 27 deletions

View File

@ -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, "");
}

View File

@ -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);

View File

@ -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 <dirent.h>
#include <sys/stat.h>
#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<string> 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;
}

View File

@ -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_

View File

@ -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.

View File

@ -1 +1 @@
b7ecc45522a1c4a589430ee428a083cdc9b06384
7b99f04b04c6534ba434c52ffa985fab9e89c0dd