static ZipFile *s_pZipFileobb = NULL;
CCFileUtils* CCFileUtils::sharedFileUtils() { if (s_sharedFileUtils == NULL) { s_sharedFileUtils = new CCFileUtilsAndroid(); s_sharedFileUtils->init(); std::string resourcePath = getApkPath(); s_pZipFile = new ZipFile(resourcePath, "assets/"); std::string resourcePath_Obb = getObbPath(); CCLOG("LJM test CCFileUtilsAndroid::sharedFileUtils resourcePath_Obb: %s", resourcePath_Obb.c_str()); s_pZipFileobb = new ZipFile(resourcePath_Obb,"assets/"); CCLOG("LJM test CCFileUtilsAndroid::sharedFileUtils s_pZipFileobb: %p", s_pZipFileobb); } return s_sharedFileUtils; }
CCFileUtilsAndroid::~CCFileUtilsAndroid() { CC_SAFE_DELETE(s_pZipFile); CC_SAFE_DELETE(s_pZipFileobb); }
bool CCFileUtilsAndroid::isFileExist(const std::string& strFilePath) { if (0 == strFilePath.length()) { return false; }
bool bFound = false; CCLOG("LJM test CCFileUtilsAndroid::isFileExist strFilePath: %s", strFilePath.c_str()); if (strFilePath[0] != '/') { std::string strPath = strFilePath; if (strPath.find(m_strDefaultResRootPath) != 0) { strPath.insert(0, m_strDefaultResRootPath); }
if (s_pZipFile->fileExists(strPath)) { bFound = true; } CCLOG("LJM test CCFileUtilsAndroid::isFileExist bFound: %d", bFound); if(!bFound){ if (s_pZipFileobb->fileExists(strPath)) { bFound = true; } } CCLOG("LJM test CCFileUtilsAndroid::isFileExist bFound1: %d", bFound); } else { FILE *fp = fopen(strFilePath.c_str(), "r"); if(fp) { bFound = true; fclose(fp); } } return bFound; }
unsigned char* CCFileUtilsAndroid::doGetFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize, bool forAsync) { unsigned char * pData = 0; if ((! pszFileName) || (! pszMode) || 0 == strlen(pszFileName)) { return 0; } string fullPath = fullPathForFilename(pszFileName); CCLOG("liuwen test CCFileUtilsAndroid::doGetFileData fullPath: %s", fullPath.c_str());
if (fullPath[0] != '/') { CCLOG("LJM test CCFileUtilsAndroid::doGetFileData forAsync: %d", forAsync); if (forAsync) { pData = s_pZipFile->getFileData(fullPath.c_str(), pSize, s_pZipFile->_dataThread); CCLOG("LJM test CCFileUtilsAndroid::doGetFileData pData: %p", pData); if (! pData) { pData = s_pZipFileobb->getFileData(fullPath.c_str(), pSize, s_pZipFile->_dataThread); } } else { pData = s_pZipFile->getFileData(fullPath.c_str(), pSize); CCLOG("LJM test CCFileUtilsAndroid::doGetFileData else pData: %p", pData); if (! pData) { pData = s_pZipFileobb->getFileData(fullPath.c_str(), pSize); } } } else { do { FILE *fp = fopen(fullPath.c_str(), pszMode); CC_BREAK_IF(!fp); unsigned long size; fseek(fp,0,SEEK_END); size = ftell(fp); fseek(fp,0,SEEK_SET); pData = new unsigned char[size]; size = fread(pData,sizeof(unsigned char), size,fp); fclose(fp);
if (pSize) { *pSize = size; } } while (0); }
if (! pData) { std::string msg = "Get data from file("; msg.append(pszFileName).append(") failed!"); CCLOG("%s", msg.c_str()); } return pData; }
|