From 2c399de28e2a5ebe083c434e4a6ace8a629ba5ef Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 25 Jan 2013 22:55:20 +0800 Subject: [PATCH] issue #1683: Moved CCFileUtils::setResourceDirectory. And fix a compatible issue for android. --- cocos2dx/platform/CCFileUtilsCommon_cpp.h | 10 -- cocos2dx/platform/android/CCFileUtils.cpp | 11 ++ cocos2dx/platform/blackberry/CCFileUtils.cpp | 10 ++ cocos2dx/platform/linux/CCFileUtils.cpp | 10 ++ cocos2dx/platform/marmalade/CCFileUtils.cpp | 10 ++ cocos2dx/platform/win32/CCFileUtils.cpp | 169 ++++++++++--------- 6 files changed, 131 insertions(+), 89 deletions(-) diff --git a/cocos2dx/platform/CCFileUtilsCommon_cpp.h b/cocos2dx/platform/CCFileUtilsCommon_cpp.h index 94491ca8e8..8accc6aa90 100644 --- a/cocos2dx/platform/CCFileUtilsCommon_cpp.h +++ b/cocos2dx/platform/CCFileUtilsCommon_cpp.h @@ -410,16 +410,6 @@ CCArray* CCFileUtils::getSearchPath() return m_pSearchPathArray; } -void CCFileUtils::setResourceDirectory(const char* pszResourceDirectory) -{ - m_obDirectory = pszResourceDirectory; - if (m_obDirectory.size() > 0 && m_obDirectory[m_obDirectory.size() - 1] != '/') - { - m_obDirectory.append("/"); - } - m_pSearchPathArray->insertObject(CCString::create(m_obDirectory.c_str()), 0); -} - const char* CCFileUtils::getResourceDirectory() { return m_obDirectory.c_str(); diff --git a/cocos2dx/platform/android/CCFileUtils.cpp b/cocos2dx/platform/android/CCFileUtils.cpp index 95503b39ae..d3c07ebf9a 100644 --- a/cocos2dx/platform/android/CCFileUtils.cpp +++ b/cocos2dx/platform/android/CCFileUtils.cpp @@ -234,6 +234,17 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz return pData; } +void CCFileUtils::setResourceDirectory(const char* pszResourceDirectory) +{ + m_obDirectory = pszResourceDirectory; + if (m_obDirectory.size() > 0 && m_obDirectory[m_obDirectory.size() - 1] != '/') + { + m_obDirectory.append("/"); + } + m_obDirectory += "assets/"; + m_pSearchPathArray->insertObject(CCString::create(m_obDirectory.c_str()), 0); +} + string CCFileUtils::getWriteablePath() { // Fix for Nexus 10 (Android 4.2 multi-user environment) diff --git a/cocos2dx/platform/blackberry/CCFileUtils.cpp b/cocos2dx/platform/blackberry/CCFileUtils.cpp index ecbdc5f136..791bcf6969 100644 --- a/cocos2dx/platform/blackberry/CCFileUtils.cpp +++ b/cocos2dx/platform/blackberry/CCFileUtils.cpp @@ -240,6 +240,16 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz return buffer; } +void CCFileUtils::setResourceDirectory(const char* pszResourceDirectory) +{ + m_obDirectory = pszResourceDirectory; + if (m_obDirectory.size() > 0 && m_obDirectory[m_obDirectory.size() - 1] != '/') + { + m_obDirectory.append("/"); + } + m_pSearchPathArray->insertObject(CCString::create(m_obDirectory.c_str()), 0); +} + std::string CCFileUtils::getWriteablePath() { // Let's write it in the current working directory's data folder diff --git a/cocos2dx/platform/linux/CCFileUtils.cpp b/cocos2dx/platform/linux/CCFileUtils.cpp index d82c013a06..85c3667906 100644 --- a/cocos2dx/platform/linux/CCFileUtils.cpp +++ b/cocos2dx/platform/linux/CCFileUtils.cpp @@ -222,6 +222,16 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz } +void CCFileUtils::setResourceDirectory(const char* pszResourceDirectory) +{ + m_obDirectory = pszResourceDirectory; + if (m_obDirectory.size() > 0 && m_obDirectory[m_obDirectory.size() - 1] != '/') + { + m_obDirectory.append("/"); + } + m_pSearchPathArray->insertObject(CCString::create(m_obDirectory.c_str()), 0); +} + string CCFileUtils::getWriteablePath() { //return current resource path diff --git a/cocos2dx/platform/marmalade/CCFileUtils.cpp b/cocos2dx/platform/marmalade/CCFileUtils.cpp index de95db8ee3..170f99ef79 100644 --- a/cocos2dx/platform/marmalade/CCFileUtils.cpp +++ b/cocos2dx/platform/marmalade/CCFileUtils.cpp @@ -212,6 +212,16 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz return (unsigned char*)pDataToBeReadBinary; } +void CCFileUtils::setResourceDirectory(const char* pszResourceDirectory) +{ + m_obDirectory = pszResourceDirectory; + if (m_obDirectory.size() > 0 && m_obDirectory[m_obDirectory.size() - 1] != '/') + { + m_obDirectory.append("/"); + } + m_pSearchPathArray->insertObject(CCString::create(m_obDirectory.c_str()), 0); +} + std::string CCFileUtils::getWriteablePath() { return std::string("ram://"); diff --git a/cocos2dx/platform/win32/CCFileUtils.cpp b/cocos2dx/platform/win32/CCFileUtils.cpp index 1e75e8db25..9e6a1aa4d1 100644 --- a/cocos2dx/platform/win32/CCFileUtils.cpp +++ b/cocos2dx/platform/win32/CCFileUtils.cpp @@ -64,8 +64,8 @@ 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_pFilenameLookupDict); + CC_SAFE_RELEASE(s_pFileUtils->m_pSearchPathArray); CC_SAFE_RELEASE(s_pFileUtils->m_pSearchResolutionsOrderArray); } @@ -77,15 +77,15 @@ void CCFileUtils::purgeCachedEntries() } -bool CCFileUtils::init() -{ - m_pSearchPathArray = new CCArray(); - m_pSearchPathArray->addObject(CCString::create("")); - - m_pSearchResolutionsOrderArray = new CCArray(); - m_pSearchResolutionsOrderArray->addObject(CCString::create("")); - - return true; +bool CCFileUtils::init() +{ + m_pSearchPathArray = new CCArray(); + m_pSearchPathArray->addObject(CCString::create("")); + + m_pSearchResolutionsOrderArray = new CCArray(); + m_pSearchResolutionsOrderArray->addObject(CCString::create("")); + + return true; } const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath) @@ -93,11 +93,11 @@ const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath) return CCString::create(fullPathForFilename(pszRelativePath))->getCString(); } -std::string CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resourceDirectory, const std::string& searchPath) -{ - std::string ret; - const std::string& resourceRootPath = CCApplication::sharedApplication()->getResourceRootPath(); - +std::string CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resourceDirectory, const std::string& searchPath) +{ + std::string ret; + const std::string& resourceRootPath = CCApplication::sharedApplication()->getResourceRootPath(); + if (filename.length() > 0 && ('/' == filename[0] || '\\' == filename[0])) { @@ -105,46 +105,46 @@ std::string CCFileUtils::getPathForFilename(const std::string& filename, const s char szDriver[3] = {s_pszResourcePath[0], s_pszResourcePath[1], 0}; ret = szDriver; ret += "/"; - } - else if (resourceRootPath.length() > 0) - { - ret = resourceRootPath; - if (ret[ret.length()-1] != '\\' && ret[ret.length()-1] != '/') - { - ret += "/"; - } - } - else - { - ret = s_pszResourcePath; - } - - 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; + } + else if (resourceRootPath.length() > 0) + { + ret = resourceRootPath; + if (ret[ret.length()-1] != '\\' && ret[ret.length()-1] != '/') + { + ret += "/"; + } + } + else + { + ret = s_pszResourcePath; + } + + 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; } std::string CCFileUtils::fullPathForFilename(const char* pszFileName) @@ -161,29 +161,29 @@ std::string CCFileUtils::fullPathForFilename(const char* pszFileName) // path start with "x:", is absolute path, return directly return newFileName; } - - 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()); - - if (GetFileAttributesA(fullpath.c_str()) != -1) - { - bFound = true; - break; - } - } - if (bFound) - { - break; - } + + 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()); + + if (GetFileAttributesA(fullpath.c_str()) != -1) + { + bFound = true; + break; + } + } + if (bFound) + { + break; + } } }while(false); @@ -230,6 +230,17 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz return pBuffer; } +void CCFileUtils::setResourceDirectory(const char* pszResourceDirectory) +{ + m_obDirectory = pszResourceDirectory; + if (m_obDirectory.size() > 0 && m_obDirectory[m_obDirectory.size() - 1] != '/') + { + m_obDirectory.append("/"); + } + + m_pSearchPathArray->insertObject(CCString::create(m_obDirectory.c_str()), 0); +} + string CCFileUtils::getWriteablePath() { // Get full path of executable, e.g. c:\Program Files (x86)\My Game Folder\MyGame.exe