Adding cache support for CCFileUtils.

This commit is contained in:
James Chen 2013-01-25 21:52:35 +08:00
parent 55af1fd3bf
commit fde39c6dcb
3 changed files with 30 additions and 15 deletions

View File

@ -37,6 +37,8 @@ static CCFileUtils* s_pFileUtils = NULL;
// record the zip on the resource path
static ZipFile *s_pZipFile = NULL;
static std::map<std::string, std::string> s_fullPathCache;
CCFileUtils* CCFileUtils::sharedFileUtils()
{
if (s_pFileUtils == NULL)
@ -53,7 +55,6 @@ bool CCFileUtils::init()
{
m_pSearchPathArray = new CCArray();
m_pSearchPathArray->addObject(CCString::create("assets/"));
m_pSearchPathArray->addObject(CCString::create(""));
m_pSearchResolutionsOrderArray = new CCArray();
m_pSearchResolutionsOrderArray->addObject(CCString::create(""));
@ -77,14 +78,12 @@ void CCFileUtils::purgeFileUtils()
void CCFileUtils::purgeCachedEntries()
{
s_fullPathCache.clear();
}
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
{
CCString* pRet = CCString::create("");
pRet->m_sString = fullPathForFilename(pszRelativePath);
return pRet->getCString();
return CCString::create(fullPathForFilename(pszRelativePath))->getCString();
}
std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
@ -92,6 +91,15 @@ std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
if (pszFileName == NULL || pszFileName[0] == '\0' || pszFileName[0] == '/') {
return pszFileName;
}
// Already Cached ?
std::map<std::string, std::string>::iterator cacheIter = s_fullPathCache.find(pszFileName);
if (cacheIter != s_fullPathCache.end())
{
CCLOG("Return full path from cache: %s", cacheIter->second.c_str());
return cacheIter->second;
}
// Get the new file name.
std::string newFilename = getNewFilename(pszFileName);
@ -128,6 +136,7 @@ std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
}
if (bFound)
{
s_fullPathCache.insert(std::pair<std::string, std::string>(pszFileName, fullpath));
CCLOG("Returning path: %s", fullpath.c_str());
return fullpath;
}
@ -148,7 +157,6 @@ const char* CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const
std::string CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resourceDirectory, const std::string& searchPath)
{
std::string ret = "";
std::string file = filename;
std::string file_path = "";
size_t pos = filename.find_last_of("/");
@ -172,10 +180,9 @@ std::string CCFileUtils::getPathForFilename(const std::string& filename, const s
path += "/";
}
path += file;
ret += path;
CCLOG("getPathForFilename, fullPath = %s", ret.c_str());
return ret;
CCLOG("getPathForFilename, fullPath = %s", path.c_str());
return path;
}
unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)

View File

@ -142,6 +142,8 @@ static void static_addValueToCCDict(id key, id value, CCDictionary* pDict)
NS_CC_BEGIN
static std::map<std::string, std::string> s_fullPathCache;
static CCFileUtils* s_pFileUtils = NULL;
CCFileUtils* CCFileUtils::sharedFileUtils()
@ -169,7 +171,7 @@ void CCFileUtils::purgeFileUtils()
void CCFileUtils::purgeCachedEntries()
{
s_fullPathCache.clear();
}
bool CCFileUtils::init()
@ -224,9 +226,7 @@ const char* CCFileUtils::getResourceDirectory()
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
{
CCString* pRet = CCString::create("");
pRet->m_sString = fullPathForFilename(pszRelativePath);
return pRet->getCString();
return CCString::create(fullPathForFilename(pszRelativePath))->getCString();
}
std::string CCFileUtils::getNewFilename(const char* pszFileName)
@ -282,9 +282,15 @@ std::string CCFileUtils::fullPathForFilename(const char* filename)
{
CCAssert(filename != NULL, "CCFileUtils: Invalid path");
// Already Cached ?
std::map<std::string, std::string>::iterator cacheIter = s_fullPathCache.find(filename);
if (cacheIter != s_fullPathCache.end())
{
return cacheIter->second;
}
std::string fullpath = "";
NSString *relPath = [NSString stringWithUTF8String:filename];
BOOL found = NO;
// only if it is not an absolute path
if( ! [relPath isAbsolutePath] ) {
@ -306,6 +312,7 @@ std::string CCFileUtils::fullPathForFilename(const char* filename)
if (fullpath.length() > 0)
{
s_fullPathCache.insert(std::pair<std::string, std::string>(filename, fullpath));
return fullpath;
}
}

View File

@ -29,7 +29,8 @@ AppDelegate::~AppDelegate()
}
void handle_ccb_run() {
ScriptingCore::getInstance()->runScript("main.js");
CCFileUtils::sharedFileUtils()->purgeCachedEntries();
ScriptingCore::getInstance()->runScript("main.js");
}
void handle_connected() {