Some minor reformatting, JLOG instrumentation of the zip file loading.

This commit is contained in:
wrenczes@gmail.com
2011-02-01 05:53:19 +00:00
parent 4d15756f76
commit 9085b2a0e1
+83 -73
View File
@@ -36,27 +36,28 @@ JZipCache::~JZipCache(){
} }
void JFileSystem::preloadZip(string filename){ void JFileSystem::preloadZip(string filename){
map<string,JZipCache *>::iterator it = mZipCache.find(filename); map<string,JZipCache *>::iterator it = mZipCache.find(filename);
if (it != mZipCache.end()) return; if (it != mZipCache.end()) return;
JZipCache * cache = new JZipCache(); JZipCache * cache = new JZipCache();
mZipCache[filename] = cache; mZipCache[filename] = cache;
if (!mZipAvailable || !mZipFile) { if (!mZipAvailable || !mZipFile)
AttachZipFile(filename); {
if (!mZipAvailable || !mZipFile) return; AttachZipFile(filename);
} if (!mZipAvailable || !mZipFile) return;
int err = unzGoToFirstFile (mZipFile); }
while (err == UNZ_OK){ int err = unzGoToFirstFile (mZipFile);
unz_file_pos* filePos = new unz_file_pos(); while (err == UNZ_OK){
char filenameInzip[4096]; unz_file_pos* filePos = new unz_file_pos();
if (unzGetCurrentFileInfo(mZipFile, NULL, filenameInzip, sizeof(filenameInzip), NULL, 0, NULL, 0) == UNZ_OK){ char filenameInzip[4096];
unzGetFilePos(mZipFile, filePos); if (unzGetCurrentFileInfo(mZipFile, NULL, filenameInzip, sizeof(filenameInzip), NULL, 0, NULL, 0) == UNZ_OK){
string name = filenameInzip; unzGetFilePos(mZipFile, filePos);
cache->dir[name] = filePos; string name = filenameInzip;
} cache->dir[name] = filePos;
err = unzGoToNextFile(mZipFile); }
} err = unzGoToNextFile(mZipFile);
}
} }
JFileSystem* JFileSystem::mInstance = NULL; JFileSystem* JFileSystem::mInstance = NULL;
@@ -143,8 +144,14 @@ void JFileSystem::DetachZipFile()
{ {
if (mZipAvailable && mZipFile != NULL) if (mZipAvailable && mZipFile != NULL)
{ {
unzCloseCurrentFile(mZipFile); JLOG("DetachZipFile");
unzClose(mZipFile); int error = unzCloseCurrentFile(mZipFile);
if (error < 0 )
JLOG("error calling unzCloseCurrentFile");
error = unzClose(mZipFile);
if (error < 0)
JLOG("Error calling unzClose");
} }
mZipFile = NULL; mZipFile = NULL;
@@ -154,62 +161,65 @@ void JFileSystem::DetachZipFile()
bool JFileSystem::OpenFile(const string &filename) bool JFileSystem::OpenFile(const string &filename)
{ {
string path = mResourceRoot + filename;
JLOG(path.c_str());
string path = mResourceRoot + filename; if (mZipAvailable && mZipFile != NULL)
JLOG("JFileSystem::OpenFile"); {
JLOG(path.c_str()); JLOG("zip available, calling preload")
preloadZip(mZipFileName);
map<string,JZipCache *>::iterator it = mZipCache.find(mZipFileName);
if (it == mZipCache.end())
{
JLOG("zip cache miss, detaching & calling open again");
DetachZipFile();
return OpenFile(filename);
}
JZipCache * zc = it->second;
map<string,unz_file_pos *>::iterator it2 = zc->dir.find(filename);
if (it2 == zc->dir.end())
{
JLOG("directory miss, detaching & calling open again");
DetachZipFile();
return OpenFile(filename);
}
unzGoToFilePos(mZipFile,it2->second);
char filenameInzip[256];
unz_file_info fileInfo;
if (mZipAvailable && mZipFile != NULL) JLOG("calling unzGetCurrentFileInfo");
{ if (unzGetCurrentFileInfo(mZipFile, &fileInfo, filenameInzip, sizeof(filenameInzip), NULL, 0, NULL, 0) == UNZ_OK)
preloadZip(mZipFileName); mFileSize = fileInfo.uncompressed_size;
map<string,JZipCache *>::iterator it = mZipCache.find(mZipFileName); else
if (it == mZipCache.end()){ mFileSize = 0;
DetachZipFile();
return OpenFile(filename);
}
JZipCache * zc = it->second;
map<string,unz_file_pos *>::iterator it2 = zc->dir.find(filename);
if (it2 == zc->dir.end()){
DetachZipFile();
return OpenFile(filename);
}
unzGoToFilePos(mZipFile,it2->second);
char filenameInzip[256];
unz_file_info fileInfo;
if (unzGetCurrentFileInfo(mZipFile, &fileInfo, filenameInzip, sizeof(filenameInzip), NULL, 0, NULL, 0) == UNZ_OK) return (unzOpenCurrentFilePassword(mZipFile, mPassword) == UNZ_OK);
mFileSize = fileInfo.uncompressed_size; }
else else
mFileSize = 0; {
#if defined (WIN32) || defined (LINUX)|| defined (IOS)
return (unzOpenCurrentFilePassword(mZipFile, mPassword) == UNZ_OK); mFile = fopen(path.c_str(), "rb");
} if (mFile != NULL)
else {
{ fseek(mFile, 0, SEEK_END);
#if defined (WIN32) || defined (LINUX)|| defined (IOS) mFileSize = ftell(mFile);
mFile = fopen(path.c_str(), "rb"); fseek(mFile, 0, SEEK_SET);
if (mFile != NULL) return true;
{ }
fseek(mFile, 0, SEEK_END); #else
mFileSize = ftell(mFile); JLOG("calling sceIOpen");
fseek(mFile, 0, SEEK_SET); mFile = sceIoOpen(path.c_str(), PSP_O_RDONLY, 0777);
return true; if (mFile > 0)
} {
#else JLOG("calling sceIoSeek");
mFile = sceIoOpen(path.c_str(), PSP_O_RDONLY, 0777); mFileSize = sceIoLseek(mFile, 0, PSP_SEEK_END);
if (mFile > 0) sceIoLseek(mFile, 0, PSP_SEEK_SET);
{ return true;
mFileSize = sceIoLseek(mFile, 0, PSP_SEEK_END); }
sceIoLseek(mFile, 0, PSP_SEEK_SET); #endif
return true; }
}
#endif
}
return false;
return false;
} }