2013-01-28 15:17:20 +08:00
|
|
|
#include "FileUtilsTest.h"
|
|
|
|
|
|
|
|
|
|
|
|
TESTLAYER_CREATE_FUNC(TestResolutionDirectories);
|
|
|
|
TESTLAYER_CREATE_FUNC(TestSearchPath);
|
|
|
|
TESTLAYER_CREATE_FUNC(TestFilenameLookup);
|
2013-02-01 22:18:43 +08:00
|
|
|
TESTLAYER_CREATE_FUNC(TestIsFileExist);
|
2013-01-28 15:17:20 +08:00
|
|
|
|
|
|
|
static NEWTESTFUNC createFunctions[] = {
|
|
|
|
CF(TestResolutionDirectories),
|
|
|
|
CF(TestSearchPath),
|
2013-02-01 22:18:43 +08:00
|
|
|
CF(TestFilenameLookup),
|
|
|
|
CF(TestIsFileExist)
|
2013-01-28 15:17:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int sceneIdx=-1;
|
|
|
|
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
|
|
|
|
|
|
|
|
static CCLayer* nextAction()
|
|
|
|
{
|
|
|
|
sceneIdx++;
|
|
|
|
sceneIdx = sceneIdx % MAX_LAYER;
|
|
|
|
|
|
|
|
CCLayer* pLayer = (createFunctions[sceneIdx])();
|
|
|
|
pLayer->init();
|
|
|
|
pLayer->autorelease();
|
|
|
|
|
|
|
|
return pLayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CCLayer* backAction()
|
|
|
|
{
|
|
|
|
sceneIdx--;
|
|
|
|
int total = MAX_LAYER;
|
|
|
|
if( sceneIdx < 0 )
|
|
|
|
sceneIdx += total;
|
|
|
|
|
|
|
|
CCLayer* pLayer = (createFunctions[sceneIdx])();
|
|
|
|
pLayer->init();
|
|
|
|
pLayer->autorelease();
|
|
|
|
|
|
|
|
return pLayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CCLayer* restartAction()
|
|
|
|
{
|
|
|
|
CCLayer* pLayer = (createFunctions[sceneIdx])();
|
|
|
|
pLayer->init();
|
|
|
|
pLayer->autorelease();
|
|
|
|
|
|
|
|
return pLayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileUtilsTestScene::runThisTest()
|
|
|
|
{
|
|
|
|
CCLayer* pLayer = nextAction();
|
|
|
|
addChild(pLayer);
|
|
|
|
|
|
|
|
CCDirector::sharedDirector()->replaceScene(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// #pragma mark - FileUtilsDemo
|
|
|
|
|
|
|
|
void FileUtilsDemo::onEnter()
|
|
|
|
{
|
|
|
|
CCLayer::onEnter();
|
|
|
|
|
|
|
|
CCLabelTTF* label = CCLabelTTF::create(title().c_str(), "Arial", 32);
|
|
|
|
addChild(label);
|
|
|
|
label->setPosition(ccp(VisibleRect::center().x, VisibleRect::top().y-50));
|
|
|
|
|
|
|
|
std::string subTitle = subtitle();
|
|
|
|
if(! subTitle.empty())
|
|
|
|
{
|
|
|
|
CCLabelTTF* l = CCLabelTTF::create(subTitle.c_str(), "Thonburi", 16);
|
|
|
|
addChild(l, 1);
|
|
|
|
l->setPosition(ccp(VisibleRect::center().x, VisibleRect::top().y-80));
|
|
|
|
}
|
|
|
|
|
|
|
|
CCMenuItemImage *item1 = CCMenuItemImage::create("Images/b1.png", "Images/b2.png", this, menu_selector(FileUtilsDemo::backCallback));
|
|
|
|
CCMenuItemImage *item2 = CCMenuItemImage::create("Images/r1.png","Images/r2.png", this, menu_selector(FileUtilsDemo::restartCallback) );
|
|
|
|
CCMenuItemImage *item3 = CCMenuItemImage::create("Images/f1.png", "Images/f2.png", this, menu_selector(FileUtilsDemo::nextCallback) );
|
|
|
|
|
|
|
|
CCMenu *menu = CCMenu::create(item1, item2, item3, NULL);
|
|
|
|
menu->setPosition(CCPointZero);
|
|
|
|
item1->setPosition(ccp(VisibleRect::center().x - item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2));
|
|
|
|
item2->setPosition(ccp(VisibleRect::center().x, VisibleRect::bottom().y+item2->getContentSize().height/2));
|
|
|
|
item3->setPosition(ccp(VisibleRect::center().x + item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2));
|
|
|
|
|
|
|
|
addChild(menu, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileUtilsDemo::backCallback(CCObject* pSender)
|
|
|
|
{
|
|
|
|
CCScene* pScene = new FileUtilsTestScene();
|
|
|
|
CCLayer* pLayer = backAction();
|
|
|
|
|
|
|
|
pScene->addChild(pLayer);
|
|
|
|
CCDirector::sharedDirector()->replaceScene(pScene);
|
|
|
|
pScene->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileUtilsDemo::nextCallback(CCObject* pSender)
|
|
|
|
{
|
|
|
|
CCScene* pScene = new FileUtilsTestScene();
|
|
|
|
CCLayer* pLayer = nextAction();
|
|
|
|
|
|
|
|
pScene->addChild(pLayer);
|
|
|
|
CCDirector::sharedDirector()->replaceScene(pScene);
|
|
|
|
pScene->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileUtilsDemo::restartCallback(CCObject* pSender)
|
|
|
|
{
|
|
|
|
CCScene* pScene = new FileUtilsTestScene();
|
|
|
|
CCLayer* pLayer = restartAction();
|
|
|
|
|
|
|
|
pScene->addChild(pLayer);
|
|
|
|
CCDirector::sharedDirector()->replaceScene(pScene);
|
|
|
|
pScene->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
string FileUtilsDemo::title()
|
|
|
|
{
|
|
|
|
return "No title";
|
|
|
|
}
|
|
|
|
|
|
|
|
string FileUtilsDemo::subtitle()
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
//#pragma mark - TestResolutionDirectories
|
|
|
|
|
|
|
|
void TestResolutionDirectories::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils();
|
|
|
|
|
|
|
|
string ret;
|
|
|
|
|
|
|
|
sharedFileUtils->purgeCachedEntries();
|
2013-01-29 09:56:38 +08:00
|
|
|
m_defaultSearchPathArray = sharedFileUtils->getSearchPaths();
|
2013-01-28 15:17:20 +08:00
|
|
|
vector<string> searchPaths = m_defaultSearchPathArray;
|
|
|
|
searchPaths.insert(searchPaths.begin(), "Misc");
|
2013-01-29 09:56:38 +08:00
|
|
|
sharedFileUtils->setSearchPaths(searchPaths);
|
2013-01-28 15:17:20 +08:00
|
|
|
|
|
|
|
m_defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder();
|
2013-01-28 17:43:53 +08:00
|
|
|
vector<string> resolutionsOrder = m_defaultResolutionsOrderArray;
|
2013-01-28 15:17:20 +08:00
|
|
|
|
|
|
|
resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipadhd");
|
|
|
|
resolutionsOrder.insert(resolutionsOrder.begin()+1, "resources-ipad");
|
|
|
|
resolutionsOrder.insert(resolutionsOrder.begin()+2, "resources-widehd");
|
|
|
|
resolutionsOrder.insert(resolutionsOrder.begin()+3, "resources-wide");
|
|
|
|
resolutionsOrder.insert(resolutionsOrder.begin()+4, "resources-hd");
|
|
|
|
resolutionsOrder.insert(resolutionsOrder.begin()+5, "resources-iphone");
|
|
|
|
|
|
|
|
sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder);
|
|
|
|
|
|
|
|
for( int i=1; i<7; i++) {
|
|
|
|
CCString *filename = CCString::createWithFormat("test%d.txt", i);
|
|
|
|
ret = sharedFileUtils->fullPathForFilename(filename->getCString());
|
|
|
|
CCLog("%s -> %s", filename->getCString(), ret.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestResolutionDirectories::onExit()
|
|
|
|
{
|
|
|
|
CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils();
|
|
|
|
|
|
|
|
// reset search path
|
2013-01-29 09:56:38 +08:00
|
|
|
sharedFileUtils->setSearchPaths(m_defaultSearchPathArray);
|
2013-01-28 15:17:20 +08:00
|
|
|
sharedFileUtils->setSearchResolutionsOrder(m_defaultResolutionsOrderArray);
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
string TestResolutionDirectories::title()
|
|
|
|
{
|
|
|
|
return "FileUtils: resolutions in directories";
|
|
|
|
}
|
|
|
|
|
|
|
|
string TestResolutionDirectories::subtitle()
|
|
|
|
{
|
|
|
|
return "See the console";
|
|
|
|
}
|
|
|
|
|
|
|
|
//#pragma mark - TestSearchPath
|
|
|
|
|
|
|
|
void TestSearchPath::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils();
|
|
|
|
|
|
|
|
string ret;
|
|
|
|
|
|
|
|
sharedFileUtils->purgeCachedEntries();
|
2013-01-29 09:56:38 +08:00
|
|
|
m_defaultSearchPathArray = sharedFileUtils->getSearchPaths();
|
2013-01-28 15:17:20 +08:00
|
|
|
vector<string> searchPaths = m_defaultSearchPathArray;
|
2013-02-06 18:04:40 +08:00
|
|
|
string writablePath = sharedFileUtils->getWritablePath();
|
2013-01-28 17:43:53 +08:00
|
|
|
string fileName = writablePath+"external.txt";
|
|
|
|
char szBuf[100] = "Hello Cocos2d-x!";
|
|
|
|
FILE* fp = fopen(fileName.c_str(), "wb");
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
fwrite(szBuf, 1, strlen(szBuf), fp);
|
|
|
|
fclose(fp);
|
|
|
|
CCLog("Writing file to writable path succeed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
searchPaths.insert(searchPaths.begin(), writablePath);
|
|
|
|
searchPaths.insert(searchPaths.begin()+1, "Misc/searchpath1");
|
|
|
|
searchPaths.insert(searchPaths.begin()+2, "Misc/searchpath2");
|
2013-01-29 09:56:38 +08:00
|
|
|
sharedFileUtils->setSearchPaths(searchPaths);
|
2013-01-28 17:43:53 +08:00
|
|
|
|
2013-01-28 15:17:20 +08:00
|
|
|
m_defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder();
|
2013-01-28 17:43:53 +08:00
|
|
|
vector<string> resolutionsOrder = m_defaultResolutionsOrderArray;
|
2013-01-28 15:17:20 +08:00
|
|
|
|
|
|
|
resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipad");
|
|
|
|
sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder);
|
|
|
|
|
|
|
|
for( int i=1; i<3; i++) {
|
|
|
|
CCString *filename = CCString::createWithFormat("file%d.txt", i);
|
|
|
|
ret = sharedFileUtils->fullPathForFilename(filename->getCString());
|
|
|
|
CCLog("%s -> %s", filename->getCString(), ret.c_str());
|
|
|
|
}
|
2013-01-28 17:43:53 +08:00
|
|
|
|
|
|
|
// Gets external.txt from writable path
|
|
|
|
string fullPath = sharedFileUtils->fullPathForFilename("external.txt");
|
2013-03-16 23:56:03 +08:00
|
|
|
CCLog("external file path = %s", fullPath.c_str());
|
2013-03-03 10:32:09 +08:00
|
|
|
if (fullPath.length() > 0)
|
|
|
|
{
|
2013-01-28 17:43:53 +08:00
|
|
|
fp = fopen(fullPath.c_str(), "rb");
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
char szReadBuf[100] = {0};
|
2013-03-03 10:32:09 +08:00
|
|
|
int read = fread(szReadBuf, 1, strlen(szBuf), fp);
|
|
|
|
if (read > 0)
|
|
|
|
CCLog("The content of file from writable path: %s", szReadBuf);
|
2013-01-28 17:43:53 +08:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
2013-01-28 15:17:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestSearchPath::onExit()
|
|
|
|
{
|
|
|
|
CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils();
|
|
|
|
|
|
|
|
// reset search path
|
2013-01-29 09:56:38 +08:00
|
|
|
sharedFileUtils->setSearchPaths(m_defaultSearchPathArray);
|
2013-01-28 15:17:20 +08:00
|
|
|
sharedFileUtils->setSearchResolutionsOrder(m_defaultResolutionsOrderArray);
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
string TestSearchPath::title()
|
|
|
|
{
|
|
|
|
return "FileUtils: search path";
|
|
|
|
}
|
|
|
|
|
|
|
|
string TestSearchPath::subtitle()
|
|
|
|
{
|
|
|
|
return "See the console";
|
|
|
|
}
|
|
|
|
|
|
|
|
//#pragma mark - TestFilenameLookup
|
|
|
|
|
|
|
|
void TestFilenameLookup::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
|
|
|
|
CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils();
|
|
|
|
|
|
|
|
CCDictionary *dict = CCDictionary::create();
|
|
|
|
dict->setObject(CCString::create("Images/grossini.png"), "grossini.bmp");
|
|
|
|
dict->setObject(CCString::create("Images/grossini.png"), "grossini.xcf");
|
|
|
|
|
|
|
|
sharedFileUtils->setFilenameLookupDictionary(dict);
|
|
|
|
|
|
|
|
|
|
|
|
// Instead of loading carlitos.xcf, it will load grossini.png
|
|
|
|
CCSprite *sprite = CCSprite::create("grossini.xcf");
|
|
|
|
this->addChild(sprite);
|
|
|
|
|
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
sprite->setPosition(ccp(s.width/2, s.height/2));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestFilenameLookup::onExit()
|
|
|
|
{
|
|
|
|
|
|
|
|
CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils();
|
|
|
|
|
|
|
|
// reset filename lookup
|
|
|
|
sharedFileUtils->setFilenameLookupDictionary(CCDictionary::create());
|
|
|
|
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
string TestFilenameLookup::title()
|
|
|
|
{
|
|
|
|
return "FileUtils: filename lookup";
|
|
|
|
}
|
|
|
|
|
|
|
|
string TestFilenameLookup::subtitle()
|
|
|
|
{
|
|
|
|
return "See the console";
|
|
|
|
}
|
|
|
|
|
2013-02-01 22:18:43 +08:00
|
|
|
//#pragma mark - TestIsFileExist
|
|
|
|
|
|
|
|
void TestIsFileExist::onEnter()
|
|
|
|
{
|
|
|
|
FileUtilsDemo::onEnter();
|
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils();
|
|
|
|
|
|
|
|
CCLabelTTF* pTTF = NULL;
|
|
|
|
bool isExist = false;
|
|
|
|
|
|
|
|
isExist = sharedFileUtils->isFileExist("Images/grossini.png");
|
|
|
|
|
|
|
|
pTTF = CCLabelTTF::create(isExist ? "Images/grossini.png exists" : "Images/grossini.png doesn't exist", "", 20);
|
|
|
|
pTTF->setPosition(ccp(s.width/2, s.height/3));
|
|
|
|
this->addChild(pTTF);
|
|
|
|
|
|
|
|
isExist = sharedFileUtils->isFileExist("Images/grossini.xcf");
|
|
|
|
pTTF = CCLabelTTF::create(isExist ? "Images/grossini.xcf exists" : "Images/grossini.xcf doesn't exist", "", 20);
|
|
|
|
pTTF->setPosition(ccp(s.width/2, s.height/3*2));
|
|
|
|
this->addChild(pTTF);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestIsFileExist::onExit()
|
|
|
|
{
|
|
|
|
|
|
|
|
CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils();
|
|
|
|
|
|
|
|
// reset filename lookup
|
|
|
|
sharedFileUtils->setFilenameLookupDictionary(CCDictionary::create());
|
|
|
|
|
|
|
|
FileUtilsDemo::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
string TestIsFileExist::title()
|
|
|
|
{
|
|
|
|
return "FileUtils: check whether the file exists";
|
|
|
|
}
|
|
|
|
|
|
|
|
string TestIsFileExist::subtitle()
|
|
|
|
{
|
|
|
|
return "";
|
2013-03-03 10:32:09 +08:00
|
|
|
}
|