Merge pull request #1897 from dumganhar/refactor-ccfileutils

issue #1683: Moved CCFileUtils::setResourceDirectory. And fixed a compatible issue for android.
This commit is contained in:
James Chen 2013-01-25 06:56:59 -08:00
commit 7eb182b506
6 changed files with 131 additions and 89 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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://");

View File

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