From fde39c6dcb4a58ebcfb3f1e588dc43be91f56f6d Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 25 Jan 2013 21:52:35 +0800 Subject: [PATCH] Adding cache support for CCFileUtils. --- cocos2dx/platform/android/CCFileUtils.cpp | 25 ++++++++++++------- cocos2dx/platform/ios/CCFileUtils.mm | 17 +++++++++---- .../CocosPlayer/Classes/AppDelegate.cpp | 3 ++- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/cocos2dx/platform/android/CCFileUtils.cpp b/cocos2dx/platform/android/CCFileUtils.cpp index 555ffb8731..95503b39ae 100644 --- a/cocos2dx/platform/android/CCFileUtils.cpp +++ b/cocos2dx/platform/android/CCFileUtils.cpp @@ -37,6 +37,8 @@ static CCFileUtils* s_pFileUtils = NULL; // record the zip on the resource path static ZipFile *s_pZipFile = NULL; +static std::map 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::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(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) diff --git a/cocos2dx/platform/ios/CCFileUtils.mm b/cocos2dx/platform/ios/CCFileUtils.mm index 9111c694fd..269995e1f1 100644 --- a/cocos2dx/platform/ios/CCFileUtils.mm +++ b/cocos2dx/platform/ios/CCFileUtils.mm @@ -142,6 +142,8 @@ static void static_addValueToCCDict(id key, id value, CCDictionary* pDict) NS_CC_BEGIN +static std::map 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::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(filename, fullpath)); return fullpath; } } diff --git a/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp b/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp index 537ff70a5f..70f5e04ff5 100644 --- a/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp +++ b/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp @@ -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() {