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 // record the zip on the resource path
static ZipFile *s_pZipFile = NULL; static ZipFile *s_pZipFile = NULL;
static std::map<std::string, std::string> s_fullPathCache;
CCFileUtils* CCFileUtils::sharedFileUtils() CCFileUtils* CCFileUtils::sharedFileUtils()
{ {
if (s_pFileUtils == NULL) if (s_pFileUtils == NULL)
@ -53,7 +55,6 @@ bool CCFileUtils::init()
{ {
m_pSearchPathArray = new CCArray(); m_pSearchPathArray = new CCArray();
m_pSearchPathArray->addObject(CCString::create("assets/")); m_pSearchPathArray->addObject(CCString::create("assets/"));
m_pSearchPathArray->addObject(CCString::create(""));
m_pSearchResolutionsOrderArray = new CCArray(); m_pSearchResolutionsOrderArray = new CCArray();
m_pSearchResolutionsOrderArray->addObject(CCString::create("")); m_pSearchResolutionsOrderArray->addObject(CCString::create(""));
@ -77,14 +78,12 @@ void CCFileUtils::purgeFileUtils()
void CCFileUtils::purgeCachedEntries() void CCFileUtils::purgeCachedEntries()
{ {
s_fullPathCache.clear();
} }
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath) const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
{ {
CCString* pRet = CCString::create(""); return CCString::create(fullPathForFilename(pszRelativePath))->getCString();
pRet->m_sString = fullPathForFilename(pszRelativePath);
return pRet->getCString();
} }
std::string CCFileUtils::fullPathForFilename(const char* pszFileName) 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] == '/') { if (pszFileName == NULL || pszFileName[0] == '\0' || pszFileName[0] == '/') {
return pszFileName; 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. // Get the new file name.
std::string newFilename = getNewFilename(pszFileName); std::string newFilename = getNewFilename(pszFileName);
@ -128,6 +136,7 @@ std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
} }
if (bFound) if (bFound)
{ {
s_fullPathCache.insert(std::pair<std::string, std::string>(pszFileName, fullpath));
CCLOG("Returning path: %s", fullpath.c_str()); CCLOG("Returning path: %s", fullpath.c_str());
return fullpath; 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 CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resourceDirectory, const std::string& searchPath)
{ {
std::string ret = "";
std::string file = filename; std::string file = filename;
std::string file_path = ""; std::string file_path = "";
size_t pos = filename.find_last_of("/"); size_t pos = filename.find_last_of("/");
@ -172,10 +180,9 @@ std::string CCFileUtils::getPathForFilename(const std::string& filename, const s
path += "/"; path += "/";
} }
path += file; path += file;
ret += path;
CCLOG("getPathForFilename, fullPath = %s", ret.c_str()); CCLOG("getPathForFilename, fullPath = %s", path.c_str());
return ret; return path;
} }
unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize) 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 NS_CC_BEGIN
static std::map<std::string, std::string> s_fullPathCache;
static CCFileUtils* s_pFileUtils = NULL; static CCFileUtils* s_pFileUtils = NULL;
CCFileUtils* CCFileUtils::sharedFileUtils() CCFileUtils* CCFileUtils::sharedFileUtils()
@ -169,7 +171,7 @@ void CCFileUtils::purgeFileUtils()
void CCFileUtils::purgeCachedEntries() void CCFileUtils::purgeCachedEntries()
{ {
s_fullPathCache.clear();
} }
bool CCFileUtils::init() bool CCFileUtils::init()
@ -224,9 +226,7 @@ const char* CCFileUtils::getResourceDirectory()
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath) const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
{ {
CCString* pRet = CCString::create(""); return CCString::create(fullPathForFilename(pszRelativePath))->getCString();
pRet->m_sString = fullPathForFilename(pszRelativePath);
return pRet->getCString();
} }
std::string CCFileUtils::getNewFilename(const char* pszFileName) std::string CCFileUtils::getNewFilename(const char* pszFileName)
@ -282,9 +282,15 @@ std::string CCFileUtils::fullPathForFilename(const char* filename)
{ {
CCAssert(filename != NULL, "CCFileUtils: Invalid path"); 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 = ""; std::string fullpath = "";
NSString *relPath = [NSString stringWithUTF8String:filename]; NSString *relPath = [NSString stringWithUTF8String:filename];
BOOL found = NO;
// only if it is not an absolute path // only if it is not an absolute path
if( ! [relPath isAbsolutePath] ) { if( ! [relPath isAbsolutePath] ) {
@ -306,6 +312,7 @@ std::string CCFileUtils::fullPathForFilename(const char* filename)
if (fullpath.length() > 0) if (fullpath.length() > 0)
{ {
s_fullPathCache.insert(std::pair<std::string, std::string>(filename, fullpath));
return fullpath; return fullpath;
} }
} }

View File

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