diff --git a/cocos2dx/platform/CCFileUtils.h b/cocos2dx/platform/CCFileUtils.h index dac367d81c..5650686d9f 100644 --- a/cocos2dx/platform/CCFileUtils.h +++ b/cocos2dx/platform/CCFileUtils.h @@ -255,16 +255,20 @@ public: /** * Checks whether a file exists. * - * @param strFullpathOfFile The full path of file. + * @note If a relative path was passed in, it will be inserted a default root path at the beginning. + * @param strFilePath The path of the file, it could be a relative or absolute path. + * @return true if the file exists, otherwise it will return false. */ - virtual bool isFileExist(const std::string& strFullpathOfFile) = 0; + virtual bool isFileExist(const std::string& strFilePath) = 0; /** * Checks whether the path is an absolute path. + * * @note On Android, if the parameter passed in is relative to "assets/", this method will treat it as an absolute path. * Also on Blackberry, path starts with "app/native/Resources/" is treated as an absolute path. * * @param strPath The path that needs to be checked. + * @return true if it's an absolute path, otherwise it will return false. */ virtual bool isAbsolutePath(const std::string& strPath); @@ -305,8 +309,7 @@ protected: * @param filename The file name. * @param resolutionDirectory The resolution directory. * @param searchPath The search path. - * @param retFullPath The return value of full path. - * @return Whether the file exists. + * @return The full path of the file. It will return an empty string if the full path of the file doesn't exist. */ virtual std::string getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath); @@ -335,11 +338,11 @@ protected: virtual CCArray* createCCArrayWithContentsOfFile(const std::string& filename); /** Dictionary used to lookup filenames based on a key. - It is used internally by the following methods: - - std::string fullPathForFilename(const char*); - - @since v2.1 + * It is used internally by the following methods: + * + * std::string fullPathForFilename(const char*); + * + * @since v2.1 */ CCDictionary* m_pFilenameLookupDict; diff --git a/cocos2dx/platform/android/CCFileUtilsAndroid.cpp b/cocos2dx/platform/android/CCFileUtilsAndroid.cpp index 189389f39e..fec413c5e7 100644 --- a/cocos2dx/platform/android/CCFileUtilsAndroid.cpp +++ b/cocos2dx/platform/android/CCFileUtilsAndroid.cpp @@ -60,17 +60,27 @@ bool CCFileUtilsAndroid::init() return CCFileUtils::init(); } -bool CCFileUtilsAndroid::isFileExist(const std::string& strFullpathOfFile) +bool CCFileUtilsAndroid::isFileExist(const std::string& strFilePath) { bool bFound = false; + // Check whether file exists in apk. - if (s_pZipFile->fileExists(strFullpathOfFile)) + if (strFilePath[0] != '/') { - bFound = true; - } + std::string strPath = strFilePath; + if (strPath.find(m_strDefaultResRootPath) != 0) + {// Didn't find "assets/" at the beginning of the path, adding it. + strPath.insert(0, m_strDefaultResRootPath); + } + + if (s_pZipFile->fileExists(strPath)) + { + bFound = true; + } + } else { - FILE *fp = fopen(strFullpathOfFile.c_str(), "r"); + FILE *fp = fopen(strFilePath.c_str(), "r"); if(fp) { bFound = true; @@ -105,7 +115,7 @@ unsigned char* CCFileUtilsAndroid::getFileData(const char* pszFileName, const ch if (pszFileName[0] != '/') { - CCLOG("GETTING FILE RELATIVE DATA: %s", pszFileName); + //CCLOG("GETTING FILE RELATIVE DATA: %s", pszFileName); string fullPath = fullPathForFilename(pszFileName); pData = s_pZipFile->getFileData(fullPath.c_str(), pSize); } @@ -114,7 +124,7 @@ unsigned char* CCFileUtilsAndroid::getFileData(const char* pszFileName, const ch do { // read rrom other path than user set it - CCLOG("GETTING FILE ABSOLUTE DATA: %s", pszFileName); + //CCLOG("GETTING FILE ABSOLUTE DATA: %s", pszFileName); FILE *fp = fopen(pszFileName, pszMode); CC_BREAK_IF(!fp); diff --git a/cocos2dx/platform/android/CCFileUtilsAndroid.h b/cocos2dx/platform/android/CCFileUtilsAndroid.h index 518045442a..39faecd604 100644 --- a/cocos2dx/platform/android/CCFileUtilsAndroid.h +++ b/cocos2dx/platform/android/CCFileUtilsAndroid.h @@ -33,8 +33,6 @@ NS_CC_BEGIN -class CCDictionary; -class CCArray; /** * @addtogroup platform * @{ @@ -47,28 +45,12 @@ class CC_DLL CCFileUtilsAndroid : public CCFileUtils CCFileUtilsAndroid(); public: virtual ~CCFileUtilsAndroid(); + /* override funtions */ bool init(); - virtual unsigned char* getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize); - /** - * Gets the writeable path - * @return The path that can write/read file - */ virtual std::string getWriteablePath(); - - /** - * Checks whether file exists. - * - * @param strFullpathOfFile The full path of file. - */ - virtual bool isFileExist(const std::string& strFullpathOfFile); - - /** - * Checks whether the path is an absolute path. - * - * @param strPath The path that needs to be checked. - */ + virtual bool isFileExist(const std::string& strFilePath); virtual bool isAbsolutePath(const std::string& strPath); }; diff --git a/cocos2dx/platform/blackberry/CCFileUtilsBlackberry.cpp b/cocos2dx/platform/blackberry/CCFileUtilsBlackberry.cpp index cb5267a937..e24b4b78e3 100644 --- a/cocos2dx/platform/blackberry/CCFileUtilsBlackberry.cpp +++ b/cocos2dx/platform/blackberry/CCFileUtilsBlackberry.cpp @@ -51,9 +51,18 @@ bool CCFileUtilsBlackberry::isAbsolutePath(const std::string& strPath) return false; } -bool CCFileUtilsBlackberry::isFileExist(const std::string& strFullpathOfFile) +bool CCFileUtilsBlackberry::isFileExist(const std::string& strFilePath) { - return access(strFullpathOfFile.c_str(), F_OK) != -1 ? true : false; + std::string strPath = strFilePath; + if (strPath[0] != '/') + { // Not absolute path, add the default root path at the beginning. + if (strPath.find(m_strDefaultResRootPath) != 0) + {// Didn't find "assets/" at the beginning of the path, adding it. + strPath.insert(0, m_strDefaultResRootPath); + } + } + + return access(strPath.c_str(), F_OK) != -1 ? true : false; } NS_CC_END diff --git a/cocos2dx/platform/blackberry/CCFileUtilsBlackberry.h b/cocos2dx/platform/blackberry/CCFileUtilsBlackberry.h index a1b9cd0b18..b760f27121 100644 --- a/cocos2dx/platform/blackberry/CCFileUtilsBlackberry.h +++ b/cocos2dx/platform/blackberry/CCFileUtilsBlackberry.h @@ -33,8 +33,6 @@ NS_CC_BEGIN -class CCDictionary; -class CCArray; /** * @addtogroup platform * @{ @@ -48,20 +46,8 @@ class CC_DLL CCFileUtilsBlackberry : public CCFileUtils public: /* override funtions */ bool init(); - - /** - * Gets the writeable path - * @return The path that can write/read file - */ virtual std::string getWriteablePath(); - - /** - * Checks whether file exists. - * - * @param strFullpathOfFile The full path of file. - */ - virtual bool isFileExist(const std::string& strFullpathOfFile); - + virtual bool isFileExist(const std::string& strFilePath); virtual bool isAbsolutePath(const std::string& strPath); }; diff --git a/cocos2dx/platform/ios/CCFileUtilsIOS.h b/cocos2dx/platform/ios/CCFileUtilsIOS.h index 9333d79af1..7ba85e5d77 100644 --- a/cocos2dx/platform/ios/CCFileUtilsIOS.h +++ b/cocos2dx/platform/ios/CCFileUtilsIOS.h @@ -33,8 +33,6 @@ NS_CC_BEGIN -class CCDictionary; -class CCArray; /** * @addtogroup platform * @{ @@ -45,37 +43,9 @@ class CC_DLL CCFileUtilsIOS : public CCFileUtils { public: /* override funtions */ - - /** - * Gets the writeable path - * @return The path that can write/read file - */ virtual std::string getWriteablePath(); - - /** - * Checks whether file exists. - * - * @param strFullpathOfFile The full path of file. - */ - virtual bool isFileExist(const std::string& strFullpathOfFile); - - /** - * Checks whether the path is an absolute path. - * - * @param strPath The path that needs to be checked. - */ + virtual bool isFileExist(const std::string& strFilePath); virtual bool isAbsolutePath(const std::string& strPath); - - /** - * Gets full path for the directory and the filename. - * - * @note Only iOS and Mac need to override this method since they are using - * `[[NSBundle mainBundle] pathForResource: ofType: inDirectory:]` to make a full path. - * Other platforms will use the default implementation of this method. - * @param strDirectory The directory contains the file we are looking for. - * @param strFilename The name of the file. - * @return The full path of the file, if the file can't be found, it will return an empty string. - */ virtual std::string getFullPathForDirectoryAndFilename(const std::string& strDirectory, const std::string& strFilename); virtual CCDictionary* createCCDictionaryWithContentsOfFile(const std::string& filename); diff --git a/cocos2dx/platform/ios/CCFileUtilsIOS.mm b/cocos2dx/platform/ios/CCFileUtilsIOS.mm index 849f1759d9..06e747a4f8 100644 --- a/cocos2dx/platform/ios/CCFileUtilsIOS.mm +++ b/cocos2dx/platform/ios/CCFileUtilsIOS.mm @@ -62,13 +62,13 @@ std::string CCFileUtilsIOS::getWriteablePath() return strRet; } -bool CCFileUtilsIOS::isFileExist(const std::string& strFullpathOfFile) +bool CCFileUtilsIOS::isFileExist(const std::string& strFilePath) { bool bRet = false; - if (strFullpathOfFile[0] != '/') + if (strFilePath[0] != '/') { - std::string path = strFullpathOfFile; + std::string path = strFilePath; std::string file; size_t pos = path.find_last_of("/"); if (pos != std::string::npos) @@ -86,7 +86,7 @@ bool CCFileUtilsIOS::isFileExist(const std::string& strFullpathOfFile) else { // Search path is an absolute path. - if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:strFullpathOfFile.c_str()]]) { + if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:strFilePath.c_str()]]) { bRet = true; } } diff --git a/cocos2dx/platform/linux/CCFileUtilsLinux.cpp b/cocos2dx/platform/linux/CCFileUtilsLinux.cpp index e7bcd5c4af..5a6c392034 100644 --- a/cocos2dx/platform/linux/CCFileUtilsLinux.cpp +++ b/cocos2dx/platform/linux/CCFileUtilsLinux.cpp @@ -53,10 +53,16 @@ string CCFileUtilsLinux::getWriteablePath() return m_strDefaultResRootPath; } -bool CCFileUtilsLinux::isFileExist(const std::string& strFullpathOfFile) +bool CCFileUtilsLinux::isFileExist(const std::string& strFilePath) { + std::string strPath = strFilePath; + if (!isAbsolutePath(strPath)) + { // Not absolute path, add the default root path at the beginning. + strPath.insert(0, m_strDefaultResRootPath); + } + struct stat sts; - return (stat(strFullpathOfFile.c_str(), &sts) != -1) ? true : false; + return (stat(strPath.c_str(), &sts) != -1) ? true : false; } NS_CC_END diff --git a/cocos2dx/platform/linux/CCFileUtilsLinux.h b/cocos2dx/platform/linux/CCFileUtilsLinux.h index 816b5d2787..da83a087da 100644 --- a/cocos2dx/platform/linux/CCFileUtilsLinux.h +++ b/cocos2dx/platform/linux/CCFileUtilsLinux.h @@ -33,8 +33,6 @@ NS_CC_BEGIN -class CCDictionary; -class CCArray; /** * @addtogroup platform * @{ @@ -48,19 +46,8 @@ class CC_DLL CCFileUtilsLinux : public CCFileUtils public: /* override funtions */ bool init(); - - /** - * Gets the writeable path - * @return The path that can write/read file - */ virtual std::string getWriteablePath(); - - /** - * Checks whether file exists. - * - * @param strFullpathOfFile The full path of file. - */ - virtual bool isFileExist(const std::string& strFullpathOfFile); + virtual bool isFileExist(const std::string& strFilePath); }; // end of platform group diff --git a/cocos2dx/platform/mac/CCFileUtilsMac.h b/cocos2dx/platform/mac/CCFileUtilsMac.h index eccd216f93..1b5e8252ec 100644 --- a/cocos2dx/platform/mac/CCFileUtilsMac.h +++ b/cocos2dx/platform/mac/CCFileUtilsMac.h @@ -32,9 +32,6 @@ #include "ccTypeInfo.h" NS_CC_BEGIN - -class CCDictionary; -class CCArray; /** * @addtogroup platform * @{ @@ -45,30 +42,9 @@ class CC_DLL CCFileUtilsMac : public CCFileUtils { public: /* override funtions */ - - /** - * Gets the writeable path - * @return The path that can write/read file - */ virtual std::string getWriteablePath(); - - /** - * Checks whether file exists. - * - * @param strFullpathOfFile The full path of file. - */ - virtual bool isFileExist(const std::string& strFullpathOfFile); - - /** - * Checks whether the path is an absolute path. - * - * @param strPath The path that needs to be checked. - */ + virtual bool isFileExist(const std::string& strFilePath); virtual bool isAbsolutePath(const std::string& strPath); - - /** - * - */ virtual std::string getFullPathForDirectoryAndFilename(const std::string& strDirectory, const std::string& strFilename); virtual CCDictionary* createCCDictionaryWithContentsOfFile(const std::string& filename); diff --git a/cocos2dx/platform/mac/CCFileUtilsMac.mm b/cocos2dx/platform/mac/CCFileUtilsMac.mm index 25d19d3eef..6dd10a885f 100644 --- a/cocos2dx/platform/mac/CCFileUtilsMac.mm +++ b/cocos2dx/platform/mac/CCFileUtilsMac.mm @@ -59,13 +59,13 @@ std::string CCFileUtilsMac::getWriteablePath() return strRet; } -bool CCFileUtilsMac::isFileExist(const std::string& strFullpathOfFile) +bool CCFileUtilsMac::isFileExist(const std::string& strFilePath) { bool bRet = false; - if (strFullpathOfFile[0] != '/') + if (strFilePath[0] != '/') { - std::string path = strFullpathOfFile; + std::string path = strFilePath; std::string file; size_t pos = path.find_last_of("/"); if (pos != std::string::npos) @@ -83,7 +83,7 @@ bool CCFileUtilsMac::isFileExist(const std::string& strFullpathOfFile) else { // Search path is an absolute path. - if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:strFullpathOfFile.c_str()]]) { + if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:strFilePath.c_str()]]) { bRet = true; } } diff --git a/cocos2dx/platform/marmalade/CCFileUtilsMarmalade.cpp b/cocos2dx/platform/marmalade/CCFileUtilsMarmalade.cpp index fb8712b4d7..b4e506e75b 100644 --- a/cocos2dx/platform/marmalade/CCFileUtilsMarmalade.cpp +++ b/cocos2dx/platform/marmalade/CCFileUtilsMarmalade.cpp @@ -28,9 +28,9 @@ string CCFileUtilsMarmalade::getWriteablePath() return std::string("ram://"); } -bool CCFileUtilsMarmalade::isFileExist(const std::string& strFullpathOfFile) +bool CCFileUtilsMarmalade::isFileExist(const std::string& strFilePath) { - return s3eFileCheckExists(strFullpathOfFile.c_str()) == S3E_TRUE ? true : false; + return s3eFileCheckExists(strFilePath.c_str()) == S3E_TRUE ? true : false; } unsigned char* CCFileUtilsMarmalade::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize) diff --git a/cocos2dx/platform/marmalade/CCFileUtilsMarmalade.h b/cocos2dx/platform/marmalade/CCFileUtilsMarmalade.h index 47839244ad..3371797850 100644 --- a/cocos2dx/platform/marmalade/CCFileUtilsMarmalade.h +++ b/cocos2dx/platform/marmalade/CCFileUtilsMarmalade.h @@ -33,8 +33,6 @@ NS_CC_BEGIN -class CCDictionary; -class CCArray; /** * @addtogroup platform * @{ @@ -47,22 +45,9 @@ class CC_DLL CCFileUtilsMarmalade : public CCFileUtils CCFileUtilsMarmalade(); public: /* override funtions */ - - /** - * Gets the writeable path - * @return The path that can write/read file - */ virtual std::string getWriteablePath(); - - /** - * Checks whether file exists. - * - * @param strFullpathOfFile The full path of file. - */ - virtual bool isFileExist(const std::string& strFullpathOfFile); - + virtual bool isFileExist(const std::string& strFilePath); virtual bool isAbsolutePath(const std::string& strPath); - virtual unsigned char* getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize); }; diff --git a/cocos2dx/platform/win32/CCFileUtilsWin32.cpp b/cocos2dx/platform/win32/CCFileUtilsWin32.cpp index eddc723253..9993fbb5de 100644 --- a/cocos2dx/platform/win32/CCFileUtilsWin32.cpp +++ b/cocos2dx/platform/win32/CCFileUtilsWin32.cpp @@ -64,9 +64,14 @@ bool CCFileUtilsWin32::init() return CCFileUtils::init(); } -bool CCFileUtilsWin32::isFileExist(const std::string& strFullpathOfFile) +bool CCFileUtilsWin32::isFileExist(const std::string& strFilePath) { - return GetFileAttributesA(strFullpathOfFile.c_str()) != -1 ? true : false; + std::string strPath = strFilePath; + if (!isAbsolutePath(strPath)) + { // Not absolute path, add the default root path at the beginning. + strPath.insert(0, m_strDefaultResRootPath); + } + return GetFileAttributesA(strPath.c_str()) != -1 ? true : false; } bool CCFileUtilsWin32::isAbsolutePath(const std::string& strPath) diff --git a/cocos2dx/platform/win32/CCFileUtilsWin32.h b/cocos2dx/platform/win32/CCFileUtilsWin32.h index 463f35ceb3..e8946f4ef7 100644 --- a/cocos2dx/platform/win32/CCFileUtilsWin32.h +++ b/cocos2dx/platform/win32/CCFileUtilsWin32.h @@ -33,8 +33,6 @@ NS_CC_BEGIN -class CCDictionary; -class CCArray; /** * @addtogroup platform * @{ @@ -48,25 +46,8 @@ class CC_DLL CCFileUtilsWin32 : public CCFileUtils public: /* override funtions */ bool init(); - - /** - * Gets the writeable path - * @return The path that can write/read file - */ virtual std::string getWriteablePath(); - - /** - * Checks whether file exists. - * - * @param strFullpathOfFile The full path of file. - */ - virtual bool isFileExist(const std::string& strFullpathOfFile); - - /** - * Checks whether the path is an absolute path. - * - * @param strPath The path that needs to be checked. - */ + virtual bool isFileExist(const std::string& strFilePath); virtual bool isAbsolutePath(const std::string& strPath); };