From cefef60e11727a9f772640f2ecba4b40c96bf9a2 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 24 Jan 2013 17:38:43 +0800 Subject: [PATCH] Refactored CCFileUtils for Marmalade port. --- cocos2dx/platform/marmalade/CCFileUtils.cpp | 173 +++++++++++++------- 1 file changed, 111 insertions(+), 62 deletions(-) diff --git a/cocos2dx/platform/marmalade/CCFileUtils.cpp b/cocos2dx/platform/marmalade/CCFileUtils.cpp index 4caef46641..de95db8ee3 100644 --- a/cocos2dx/platform/marmalade/CCFileUtils.cpp +++ b/cocos2dx/platform/marmalade/CCFileUtils.cpp @@ -36,7 +36,6 @@ NS_CC_BEGIN; -static char s_pszResourcePath[S3E_FILE_MAX_PATH] = {0}; static CCFileUtils* s_pFileUtils = NULL; @@ -45,15 +44,30 @@ CCFileUtils* CCFileUtils::sharedFileUtils() if (s_pFileUtils == NULL) { s_pFileUtils = new CCFileUtils(); + s_pFileUtils->init(); } return s_pFileUtils; } +bool CCFileUtils::init() +{ + m_pSearchPathArray = new CCArray(); + m_pSearchPathArray->addObject(CCString::create("")); + + m_pSearchResolutionsOrderArray = new CCArray(); + m_pSearchResolutionsOrderArray->addObject(CCString::create("")); + + return true; +} + void CCFileUtils::purgeFileUtils() { if (s_pFileUtils != NULL) { s_pFileUtils->purgeCachedEntries(); + CC_SAFE_RELEASE(s_pFileUtils->m_pFilenameLookupDict); + CC_SAFE_RELEASE(s_pFileUtils->m_pSearchPathArray); + CC_SAFE_RELEASE(s_pFileUtils->m_pSearchResolutionsOrderArray); } CC_SAFE_DELETE(s_pFileUtils); @@ -64,74 +78,109 @@ void CCFileUtils::purgeCachedEntries() } +std::string CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resourceDirectory, const std::string& searchPath) +{ + std::string ret = ""; + + if (ret[ret.length()-1] != '\\' && ret[ret.length()-1] != '/') + { + ret += "/"; + } + + std::string file = filename; + std::string file_path = ""; + size_t pos = filename.find_last_of("/"); + if (pos != std::string::npos) + { + file_path = filename.substr(0, pos+1); + file = filename.substr(pos+1); + } + + // searchPath + file_path + resourceDirectory + std::string path = searchPath; + if (path.size() > 0 && path[path.length()-1] != '/') + { + path += "/"; + } + path += file_path; + path += resourceDirectory; + + if (path.size() > 0 && path[path.length()-1] != '/') + { + path += "/"; + } + path += file; + ret += path; + + return ret; +} + +const char* CCFileUtils::fullPathForFilename(const char* pszFileName) +{ + // TODO HOW ARE WE SUPPOSED TO WRITE BACK TO THE "ignore" REFERENCE? + IwAssert(GAME, pszFileName); + + if (pszFileName && pszFileName[0] == '/') + { + return pszFileName; + } + + bool bFound = false; + CCString* pRet = CCString::create(""); + + std::string newFileName = getNewFilename(pszFileName); + std::string fullpath; + + do + { + CCObject* pSearchObj = NULL; + CCARRAY_FOREACH(m_pSearchPathArray, pSearchObj) + { + CCString* pSearchPath = (CCString*)pSearchObj; + + CCObject* pResourceDirObj = NULL; + CCARRAY_FOREACH(m_pSearchResolutionsOrderArray, pResourceDirObj) + { + CCString* pResourceDirectory = (CCString*)pResourceDirObj; + // Search in subdirectories + fullpath = this->getPathForFilename(newFileName, pResourceDirectory->getCString(), pSearchPath->getCString()); + + // check if file or path exist + if (s3eFileCheckExists(fullpath.c_str()) == S3E_TRUE) + { + pRet->m_sString = fullpath; + bFound = true; + break; + } + } + if (bFound) + { + break; + } + } + + }while(false); + + if (!bFound) + { // Can't find the file, return the relative path. + pRet->m_sString = newFileName; + } + + return pRet->getCString(); +} + const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath) { - // TODO HOW ARE WE SUPPOSED TO WRITE BACK TO THE "ignore" REFERENCE? - IwAssert(GAME, pszRelativePath); - - bool bFileExist = true; - const char* resDir = m_obDirectory.c_str(); - CCString * pRet = new CCString(); - - pRet->autorelease(); - if ((strlen(pszRelativePath) > 1 && pszRelativePath[1] == ':')) - { - pRet->m_sString = resDir; - pRet->m_sString += pszRelativePath; - } - else if (strlen(pszRelativePath) > 0 && pszRelativePath[0] == '/') - { - char szDriver[3] = {s_pszResourcePath[0], s_pszResourcePath[1], 0}; - pRet->m_sString = szDriver; - pRet->m_sString += resDir; - pRet->m_sString += pszRelativePath; - } - else - { - pRet->m_sString = s_pszResourcePath; - pRet->m_sString += resDir; - pRet->m_sString += pszRelativePath; - } - - bool exists = s3eFileCheckExists(pRet->getCString()) == S3E_TRUE; - if (!exists) - { - if ((strlen(pszRelativePath) > 1 && pszRelativePath[1] == ':')) - { - pRet->m_sString = pszRelativePath; - } - else if (strlen(pszRelativePath) > 0 && pszRelativePath[0] == '/') - { - char szDriver[3] = {s_pszResourcePath[0], s_pszResourcePath[1], 0}; - pRet->m_sString = szDriver; - pRet->m_sString += pszRelativePath; - } - else - { - pRet->m_sString = s_pszResourcePath; - pRet->m_sString += pszRelativePath; - } - - bFileExist = s3eFileCheckExists(pRet->getCString()) == S3E_TRUE; - } - - if (!bFileExist) - { - pRet->m_sString = pszRelativePath; - } - - return pRet->m_sString.c_str(); + return fullPathForFilename(pszRelativePath); } + const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile) { - - std::string relativeFile = fullPathFromRelativePath(pszRelativeFile); - - CCString *pRet = new CCString(); - pRet->autorelease(); + std::string relativeFile = pszRelativeFile; + CCString *pRet = CCString::create(""); pRet->m_sString = relativeFile.substr(0, relativeFile.rfind('/')+1); - pRet->m_sString += pszFilename; + pRet->m_sString += getNewFilename(pszFilename); return pRet->m_sString.c_str(); }