From 83cf8aaf29581c01be1ef43611531dfcd77026d9 Mon Sep 17 00:00:00 2001 From: Denis Mingulov Date: Fri, 9 Nov 2012 09:53:40 +0200 Subject: [PATCH] Android: Access to files from APK is boosted - 1st review update --- cocos2dx/platform/android/CCFileUtils.cpp | 9 +++--- cocos2dx/support/zip_support/ZipUtils.cpp | 34 +++++++++++++---------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/cocos2dx/platform/android/CCFileUtils.cpp b/cocos2dx/platform/android/CCFileUtils.cpp index abe8a20188..3a4a946ab2 100644 --- a/cocos2dx/platform/android/CCFileUtils.cpp +++ b/cocos2dx/platform/android/CCFileUtils.cpp @@ -33,10 +33,8 @@ NS_CC_BEGIN #include "platform/CCCommon.h" #include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h" -// record the resource path -static string s_strResourcePath = ""; - static CCFileUtils* s_pFileUtils = NULL; +// record the zip on the resource path static ZipFile *s_pZipFile = NULL; CCFileUtils* CCFileUtils::sharedFileUtils() @@ -44,8 +42,8 @@ CCFileUtils* CCFileUtils::sharedFileUtils() if (s_pFileUtils == NULL) { s_pFileUtils = new CCFileUtils(); - s_strResourcePath = getApkPath(); - s_pZipFile = new ZipFile(s_strResourcePath, "assets/"); + std::string resourcePath = getApkPath(); + s_pZipFile = new ZipFile(resourcePath, "assets/"); } return s_pFileUtils; } @@ -57,6 +55,7 @@ void CCFileUtils::purgeFileUtils() s_pFileUtils->purgeCachedEntries(); } + CC_SAFE_DELETE(s_pZipFile); CC_SAFE_DELETE(s_pFileUtils); } diff --git a/cocos2dx/support/zip_support/ZipUtils.cpp b/cocos2dx/support/zip_support/ZipUtils.cpp index 05e00891bb..28fd7d8b8d 100644 --- a/cocos2dx/support/zip_support/ZipUtils.cpp +++ b/cocos2dx/support/zip_support/ZipUtils.cpp @@ -288,13 +288,19 @@ int ZipUtils::ccInflateCCZFile(const char *path, unsigned char **out) // from unzip.cpp #define UNZ_MAXFILENAMEINZIP 256 +struct ZipEntryInfo +{ + unz_file_pos pos; + uLong uncompressed_size; +}; + class ZipFilePrivate { public: unzFile zipFile; // std::unordered_map is faster if available on the platform - typedef std::map FileListContainer; + typedef std::map FileListContainer; FileListContainer fileList; }; @@ -338,7 +344,8 @@ bool ZipFile::setFilter(const std::string &filter) { // UNZ_MAXFILENAMEINZIP + 1 - it is done so in unzLocateFile char szCurrentFileName[UNZ_MAXFILENAMEINZIP + 1]; - int nameErr = unzGetCurrentFileInfo64(m_data->zipFile, NULL, + unz_file_info64 fileInfo; + int nameErr = unzGetCurrentFileInfo64(m_data->zipFile, &fileInfo, szCurrentFileName, sizeof(szCurrentFileName) - 1, NULL, 0, NULL, 0); std::string currentFileName = szCurrentFileName; @@ -348,7 +355,10 @@ bool ZipFile::setFilter(const std::string &filter) if (filter.empty() || currentFileName.substr(0, filter.length()) == filter) { - m_data->fileList[currentFileName] = posInfo; + ZipEntryInfo entry; + entry.pos = posInfo; + entry.uncompressed_size = (uLong)fileInfo.uncompressed_size; + m_data->fileList[currentFileName] = entry; } } } @@ -390,28 +400,22 @@ unsigned char *ZipFile::getFileData(const std::string &fileName, unsigned long * ZipFilePrivate::FileListContainer::const_iterator it = m_data->fileList.find(fileName); CC_BREAK_IF(it == m_data->fileList.end()); - unz_file_pos filePos = it->second; + ZipEntryInfo fileInfo = it->second; - int nRet = unzGoToFilePos(m_data->zipFile, &filePos); - CC_BREAK_IF(UNZ_OK != nRet); - - char szFilePathA[UNZ_MAXFILENAMEINZIP + 1]; - unz_file_info FileInfo; - nRet = unzGetCurrentFileInfo(m_data->zipFile, &FileInfo, - szFilePathA, sizeof(szFilePathA) - 1, NULL, 0, NULL, 0); + int nRet = unzGoToFilePos(m_data->zipFile, &fileInfo.pos); CC_BREAK_IF(UNZ_OK != nRet); nRet = unzOpenCurrentFile(m_data->zipFile); CC_BREAK_IF(UNZ_OK != nRet); - pBuffer = new unsigned char[FileInfo.uncompressed_size]; + pBuffer = new unsigned char[fileInfo.uncompressed_size]; int nSize = 0; - nSize = unzReadCurrentFile(m_data->zipFile, pBuffer, FileInfo.uncompressed_size); - CCAssert(nSize == 0 || nSize == (int)FileInfo.uncompressed_size, "the file size is wrong"); + nSize = unzReadCurrentFile(m_data->zipFile, pBuffer, fileInfo.uncompressed_size); + CCAssert(nSize == 0 || nSize == (int)fileInfo.uncompressed_size, "the file size is wrong"); if (pSize) { - *pSize = FileInfo.uncompressed_size; + *pSize = fileInfo.uncompressed_size; } unzCloseCurrentFile(m_data->zipFile); } while (0);