Refactored CCFileUtils for Marmalade port.

This commit is contained in:
James Chen 2013-01-24 17:38:43 +08:00
parent 73389d68d5
commit cefef60e11
1 changed files with 111 additions and 62 deletions

View File

@ -36,7 +36,6 @@
NS_CC_BEGIN; NS_CC_BEGIN;
static char s_pszResourcePath[S3E_FILE_MAX_PATH] = {0};
static CCFileUtils* s_pFileUtils = NULL; static CCFileUtils* s_pFileUtils = NULL;
@ -45,15 +44,30 @@ CCFileUtils* CCFileUtils::sharedFileUtils()
if (s_pFileUtils == NULL) if (s_pFileUtils == NULL)
{ {
s_pFileUtils = new CCFileUtils(); s_pFileUtils = new CCFileUtils();
s_pFileUtils->init();
} }
return s_pFileUtils; 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() void CCFileUtils::purgeFileUtils()
{ {
if (s_pFileUtils != NULL) if (s_pFileUtils != NULL)
{ {
s_pFileUtils->purgeCachedEntries(); 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); CC_SAFE_DELETE(s_pFileUtils);
@ -64,74 +78,109 @@ void CCFileUtils::purgeCachedEntries()
} }
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath) 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? // TODO HOW ARE WE SUPPOSED TO WRITE BACK TO THE "ignore" REFERENCE?
IwAssert(GAME, pszRelativePath); IwAssert(GAME, pszFileName);
bool bFileExist = true; if (pszFileName && pszFileName[0] == '/')
const char* resDir = m_obDirectory.c_str();
CCString * pRet = new CCString();
pRet->autorelease();
if ((strlen(pszRelativePath) > 1 && pszRelativePath[1] == ':'))
{ {
pRet->m_sString = resDir; return pszFileName;
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; bool bFound = false;
if (!exists) CCString* pRet = CCString::create("");
std::string newFileName = getNewFilename(pszFileName);
std::string fullpath;
do
{ {
if ((strlen(pszRelativePath) > 1 && pszRelativePath[1] == ':')) CCObject* pSearchObj = NULL;
CCARRAY_FOREACH(m_pSearchPathArray, pSearchObj)
{ {
pRet->m_sString = pszRelativePath; 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;
} }
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 if (bFound)
{ {
pRet->m_sString = s_pszResourcePath; break;
pRet->m_sString += pszRelativePath; }
} }
bFileExist = s3eFileCheckExists(pRet->getCString()) == S3E_TRUE; }while(false);
if (!bFound)
{ // Can't find the file, return the relative path.
pRet->m_sString = newFileName;
} }
if (!bFileExist) return pRet->getCString();
{
pRet->m_sString = pszRelativePath;
}
return pRet->m_sString.c_str();
} }
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
{
return fullPathForFilename(pszRelativePath);
}
const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile) const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile)
{ {
std::string relativeFile = pszRelativeFile;
std::string relativeFile = fullPathFromRelativePath(pszRelativeFile); CCString *pRet = CCString::create("");
CCString *pRet = new CCString();
pRet->autorelease();
pRet->m_sString = relativeFile.substr(0, relativeFile.rfind('/')+1); pRet->m_sString = relativeFile.substr(0, relativeFile.rfind('/')+1);
pRet->m_sString += pszFilename; pRet->m_sString += getNewFilename(pszFilename);
return pRet->m_sString.c_str(); return pRet->m_sString.c_str();
} }