Android: Access to files from APK is boosted - 1st review update

This commit is contained in:
Denis Mingulov 2012-11-09 09:53:40 +02:00
parent 071d264cc5
commit 83cf8aaf29
2 changed files with 23 additions and 20 deletions

View File

@ -33,10 +33,8 @@ NS_CC_BEGIN
#include "platform/CCCommon.h" #include "platform/CCCommon.h"
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h" #include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
// record the resource path
static string s_strResourcePath = "";
static CCFileUtils* s_pFileUtils = NULL; static CCFileUtils* s_pFileUtils = NULL;
// record the zip on the resource path
static ZipFile *s_pZipFile = NULL; static ZipFile *s_pZipFile = NULL;
CCFileUtils* CCFileUtils::sharedFileUtils() CCFileUtils* CCFileUtils::sharedFileUtils()
@ -44,8 +42,8 @@ CCFileUtils* CCFileUtils::sharedFileUtils()
if (s_pFileUtils == NULL) if (s_pFileUtils == NULL)
{ {
s_pFileUtils = new CCFileUtils(); s_pFileUtils = new CCFileUtils();
s_strResourcePath = getApkPath(); std::string resourcePath = getApkPath();
s_pZipFile = new ZipFile(s_strResourcePath, "assets/"); s_pZipFile = new ZipFile(resourcePath, "assets/");
} }
return s_pFileUtils; return s_pFileUtils;
} }
@ -57,6 +55,7 @@ void CCFileUtils::purgeFileUtils()
s_pFileUtils->purgeCachedEntries(); s_pFileUtils->purgeCachedEntries();
} }
CC_SAFE_DELETE(s_pZipFile);
CC_SAFE_DELETE(s_pFileUtils); CC_SAFE_DELETE(s_pFileUtils);
} }

View File

@ -288,13 +288,19 @@ int ZipUtils::ccInflateCCZFile(const char *path, unsigned char **out)
// from unzip.cpp // from unzip.cpp
#define UNZ_MAXFILENAMEINZIP 256 #define UNZ_MAXFILENAMEINZIP 256
struct ZipEntryInfo
{
unz_file_pos pos;
uLong uncompressed_size;
};
class ZipFilePrivate class ZipFilePrivate
{ {
public: public:
unzFile zipFile; unzFile zipFile;
// std::unordered_map is faster if available on the platform // std::unordered_map is faster if available on the platform
typedef std::map<std::string, unz_file_pos> FileListContainer; typedef std::map<std::string, ZipEntryInfo> FileListContainer;
FileListContainer fileList; FileListContainer fileList;
}; };
@ -338,7 +344,8 @@ bool ZipFile::setFilter(const std::string &filter)
{ {
// UNZ_MAXFILENAMEINZIP + 1 - it is done so in unzLocateFile // UNZ_MAXFILENAMEINZIP + 1 - it is done so in unzLocateFile
char szCurrentFileName[UNZ_MAXFILENAMEINZIP + 1]; 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, szCurrentFileName, sizeof(szCurrentFileName) - 1,
NULL, 0, NULL, 0); NULL, 0, NULL, 0);
std::string currentFileName = szCurrentFileName; std::string currentFileName = szCurrentFileName;
@ -348,7 +355,10 @@ bool ZipFile::setFilter(const std::string &filter)
if (filter.empty() if (filter.empty()
|| currentFileName.substr(0, filter.length()) == filter) || 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); ZipFilePrivate::FileListContainer::const_iterator it = m_data->fileList.find(fileName);
CC_BREAK_IF(it == m_data->fileList.end()); 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); int nRet = unzGoToFilePos(m_data->zipFile, &fileInfo.pos);
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);
CC_BREAK_IF(UNZ_OK != nRet); CC_BREAK_IF(UNZ_OK != nRet);
nRet = unzOpenCurrentFile(m_data->zipFile); nRet = unzOpenCurrentFile(m_data->zipFile);
CC_BREAK_IF(UNZ_OK != nRet); CC_BREAK_IF(UNZ_OK != nRet);
pBuffer = new unsigned char[FileInfo.uncompressed_size]; pBuffer = new unsigned char[fileInfo.uncompressed_size];
int nSize = 0; int nSize = 0;
nSize = unzReadCurrentFile(m_data->zipFile, pBuffer, FileInfo.uncompressed_size); nSize = unzReadCurrentFile(m_data->zipFile, pBuffer, fileInfo.uncompressed_size);
CCAssert(nSize == 0 || nSize == (int)FileInfo.uncompressed_size, "the file size is wrong"); CCAssert(nSize == 0 || nSize == (int)fileInfo.uncompressed_size, "the file size is wrong");
if (pSize) if (pSize)
{ {
*pSize = FileInfo.uncompressed_size; *pSize = fileInfo.uncompressed_size;
} }
unzCloseCurrentFile(m_data->zipFile); unzCloseCurrentFile(m_data->zipFile);
} while (0); } while (0);