Some minor reformatting, JLOG instrumentation of the zip file loading.
This commit is contained in:
+286
-276
@@ -1,277 +1,287 @@
|
|||||||
//-------------------------------------------------------------------------------------
|
//-------------------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
// JGE++ is a hardware accelerated 2D game SDK for PSP/Windows.
|
// JGE++ is a hardware accelerated 2D game SDK for PSP/Windows.
|
||||||
//
|
//
|
||||||
// Licensed under the BSD license, see LICENSE in JGE root for details.
|
// Licensed under the BSD license, see LICENSE in JGE root for details.
|
||||||
//
|
//
|
||||||
// Copyright (c) 2007 James Hui (a.k.a. Dr.Watson) <jhkhui@gmail.com>
|
// Copyright (c) 2007 James Hui (a.k.a. Dr.Watson) <jhkhui@gmail.com>
|
||||||
//
|
//
|
||||||
//-------------------------------------------------------------------------------------
|
//-------------------------------------------------------------------------------------
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#pragma warning(disable : 4786)
|
#pragma warning(disable : 4786)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "../include/JGE.h"
|
#include "../include/JGE.h"
|
||||||
#include "../include/JFileSystem.h"
|
#include "../include/JFileSystem.h"
|
||||||
#include "../include/JLogger.h"
|
#include "../include/JLogger.h"
|
||||||
#include "tinyxml/tinyxml.h"
|
#include "tinyxml/tinyxml.h"
|
||||||
#include "unzip/unzip.h"
|
#include "unzip/unzip.h"
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
JZipCache::JZipCache(){}
|
JZipCache::JZipCache(){}
|
||||||
|
|
||||||
JZipCache::~JZipCache(){
|
JZipCache::~JZipCache(){
|
||||||
map<string,unz_file_pos *>::iterator it;
|
map<string,unz_file_pos *>::iterator it;
|
||||||
for (it = dir.begin(); it != dir.end(); it++){
|
for (it = dir.begin(); it != dir.end(); it++){
|
||||||
delete(it->second);
|
delete(it->second);
|
||||||
}
|
}
|
||||||
dir.clear();
|
dir.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
JFileSystem* JFileSystem::GetInstance()
|
|
||||||
{
|
JFileSystem* JFileSystem::GetInstance()
|
||||||
if (mInstance == NULL)
|
{
|
||||||
{
|
if (mInstance == NULL)
|
||||||
mInstance = new JFileSystem();
|
{
|
||||||
}
|
mInstance = new JFileSystem();
|
||||||
|
}
|
||||||
return mInstance;
|
|
||||||
}
|
return mInstance;
|
||||||
|
}
|
||||||
|
|
||||||
void JFileSystem::Destroy()
|
|
||||||
{
|
void JFileSystem::Destroy()
|
||||||
if (mInstance)
|
{
|
||||||
{
|
if (mInstance)
|
||||||
delete mInstance;
|
{
|
||||||
mInstance = NULL;
|
delete mInstance;
|
||||||
}
|
mInstance = NULL;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
JFileSystem::JFileSystem()
|
|
||||||
{
|
JFileSystem::JFileSystem()
|
||||||
mZipAvailable = false;
|
{
|
||||||
#if defined (WIN32) || defined (LINUX) || defined (IOS)
|
mZipAvailable = false;
|
||||||
mFile = NULL;
|
#if defined (WIN32) || defined (LINUX) || defined (IOS)
|
||||||
#else
|
mFile = NULL;
|
||||||
mFile = -1;
|
#else
|
||||||
#endif
|
mFile = -1;
|
||||||
mPassword = NULL;
|
#endif
|
||||||
mZipFile = NULL;
|
mPassword = NULL;
|
||||||
mFileSize = 0;
|
mZipFile = NULL;
|
||||||
|
mFileSize = 0;
|
||||||
#ifdef RESPATH
|
|
||||||
SetResourceRoot(RESPATH"/");
|
#ifdef RESPATH
|
||||||
#else
|
SetResourceRoot(RESPATH"/");
|
||||||
SetResourceRoot("Res/"); // default root folder
|
#else
|
||||||
#endif
|
SetResourceRoot("Res/"); // default root folder
|
||||||
}
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
JFileSystem::~JFileSystem()
|
|
||||||
{
|
JFileSystem::~JFileSystem()
|
||||||
DetachZipFile();
|
{
|
||||||
|
DetachZipFile();
|
||||||
map<string,JZipCache *>::iterator it;
|
|
||||||
for (it = mZipCache.begin(); it != mZipCache.end(); it++){
|
map<string,JZipCache *>::iterator it;
|
||||||
delete(it->second);
|
for (it = mZipCache.begin(); it != mZipCache.end(); it++){
|
||||||
}
|
delete(it->second);
|
||||||
mZipCache.clear();
|
}
|
||||||
}
|
mZipCache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
bool JFileSystem::AttachZipFile(const string &zipfile, char *password /* = NULL */)
|
|
||||||
{
|
bool JFileSystem::AttachZipFile(const string &zipfile, char *password /* = NULL */)
|
||||||
if (mZipAvailable && mZipFile != NULL)
|
{
|
||||||
{
|
if (mZipAvailable && mZipFile != NULL)
|
||||||
if (mZipFileName != zipfile)
|
{
|
||||||
DetachZipFile(); // close the previous zip file
|
if (mZipFileName != zipfile)
|
||||||
else
|
DetachZipFile(); // close the previous zip file
|
||||||
return true;
|
else
|
||||||
}
|
return true;
|
||||||
|
}
|
||||||
mZipFileName = zipfile;
|
|
||||||
mPassword = password;
|
mZipFileName = zipfile;
|
||||||
|
mPassword = password;
|
||||||
mZipFile = unzOpen(mZipFileName.c_str());
|
|
||||||
|
mZipFile = unzOpen(mZipFileName.c_str());
|
||||||
if (mZipFile != NULL)
|
|
||||||
{
|
if (mZipFile != NULL)
|
||||||
mZipAvailable = true;
|
{
|
||||||
return true;
|
mZipAvailable = true;
|
||||||
}
|
return true;
|
||||||
|
}
|
||||||
return false;
|
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void JFileSystem::DetachZipFile()
|
|
||||||
{
|
void JFileSystem::DetachZipFile()
|
||||||
if (mZipAvailable && mZipFile != NULL)
|
{
|
||||||
{
|
if (mZipAvailable && mZipFile != NULL)
|
||||||
unzCloseCurrentFile(mZipFile);
|
{
|
||||||
unzClose(mZipFile);
|
JLOG("DetachZipFile");
|
||||||
}
|
int error = unzCloseCurrentFile(mZipFile);
|
||||||
|
if (error < 0 )
|
||||||
mZipFile = NULL;
|
JLOG("error calling unzCloseCurrentFile");
|
||||||
mZipAvailable = false;
|
|
||||||
}
|
error = unzClose(mZipFile);
|
||||||
|
if (error < 0)
|
||||||
|
JLOG("Error calling unzClose");
|
||||||
bool JFileSystem::OpenFile(const string &filename)
|
}
|
||||||
{
|
|
||||||
|
mZipFile = NULL;
|
||||||
string path = mResourceRoot + filename;
|
mZipAvailable = false;
|
||||||
JLOG("JFileSystem::OpenFile");
|
}
|
||||||
JLOG(path.c_str());
|
|
||||||
|
|
||||||
if (mZipAvailable && mZipFile != NULL)
|
bool JFileSystem::OpenFile(const string &filename)
|
||||||
{
|
{
|
||||||
preloadZip(mZipFileName);
|
string path = mResourceRoot + filename;
|
||||||
map<string,JZipCache *>::iterator it = mZipCache.find(mZipFileName);
|
JLOG(path.c_str());
|
||||||
if (it == mZipCache.end()){
|
|
||||||
DetachZipFile();
|
if (mZipAvailable && mZipFile != NULL)
|
||||||
return OpenFile(filename);
|
{
|
||||||
}
|
JLOG("zip available, calling preload")
|
||||||
JZipCache * zc = it->second;
|
preloadZip(mZipFileName);
|
||||||
map<string,unz_file_pos *>::iterator it2 = zc->dir.find(filename);
|
map<string,JZipCache *>::iterator it = mZipCache.find(mZipFileName);
|
||||||
if (it2 == zc->dir.end()){
|
if (it == mZipCache.end())
|
||||||
DetachZipFile();
|
{
|
||||||
return OpenFile(filename);
|
JLOG("zip cache miss, detaching & calling open again");
|
||||||
}
|
DetachZipFile();
|
||||||
unzGoToFilePos(mZipFile,it2->second);
|
return OpenFile(filename);
|
||||||
char filenameInzip[256];
|
}
|
||||||
unz_file_info fileInfo;
|
JZipCache * zc = it->second;
|
||||||
|
map<string,unz_file_pos *>::iterator it2 = zc->dir.find(filename);
|
||||||
if (unzGetCurrentFileInfo(mZipFile, &fileInfo, filenameInzip, sizeof(filenameInzip), NULL, 0, NULL, 0) == UNZ_OK)
|
if (it2 == zc->dir.end())
|
||||||
mFileSize = fileInfo.uncompressed_size;
|
{
|
||||||
else
|
JLOG("directory miss, detaching & calling open again");
|
||||||
mFileSize = 0;
|
DetachZipFile();
|
||||||
|
return OpenFile(filename);
|
||||||
return (unzOpenCurrentFilePassword(mZipFile, mPassword) == UNZ_OK);
|
}
|
||||||
}
|
unzGoToFilePos(mZipFile,it2->second);
|
||||||
else
|
char filenameInzip[256];
|
||||||
{
|
unz_file_info fileInfo;
|
||||||
#if defined (WIN32) || defined (LINUX)|| defined (IOS)
|
|
||||||
mFile = fopen(path.c_str(), "rb");
|
JLOG("calling unzGetCurrentFileInfo");
|
||||||
if (mFile != NULL)
|
if (unzGetCurrentFileInfo(mZipFile, &fileInfo, filenameInzip, sizeof(filenameInzip), NULL, 0, NULL, 0) == UNZ_OK)
|
||||||
{
|
mFileSize = fileInfo.uncompressed_size;
|
||||||
fseek(mFile, 0, SEEK_END);
|
else
|
||||||
mFileSize = ftell(mFile);
|
mFileSize = 0;
|
||||||
fseek(mFile, 0, SEEK_SET);
|
|
||||||
return true;
|
return (unzOpenCurrentFilePassword(mZipFile, mPassword) == UNZ_OK);
|
||||||
}
|
}
|
||||||
#else
|
else
|
||||||
mFile = sceIoOpen(path.c_str(), PSP_O_RDONLY, 0777);
|
{
|
||||||
if (mFile > 0)
|
#if defined (WIN32) || defined (LINUX)|| defined (IOS)
|
||||||
{
|
mFile = fopen(path.c_str(), "rb");
|
||||||
mFileSize = sceIoLseek(mFile, 0, PSP_SEEK_END);
|
if (mFile != NULL)
|
||||||
sceIoLseek(mFile, 0, PSP_SEEK_SET);
|
{
|
||||||
return true;
|
fseek(mFile, 0, SEEK_END);
|
||||||
}
|
mFileSize = ftell(mFile);
|
||||||
#endif
|
fseek(mFile, 0, SEEK_SET);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
#else
|
||||||
|
JLOG("calling sceIOpen");
|
||||||
return false;
|
mFile = sceIoOpen(path.c_str(), PSP_O_RDONLY, 0777);
|
||||||
|
if (mFile > 0)
|
||||||
}
|
{
|
||||||
|
JLOG("calling sceIoSeek");
|
||||||
|
mFileSize = sceIoLseek(mFile, 0, PSP_SEEK_END);
|
||||||
void JFileSystem::CloseFile()
|
sceIoLseek(mFile, 0, PSP_SEEK_SET);
|
||||||
{
|
return true;
|
||||||
if (mZipAvailable && mZipFile != NULL){
|
}
|
||||||
unzCloseCurrentFile(mZipFile);
|
#endif
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
return false;
|
||||||
#if defined (WIN32) || defined (LINUX) || defined (IOS)
|
}
|
||||||
if (mFile != NULL)
|
|
||||||
fclose(mFile);
|
|
||||||
#else
|
void JFileSystem::CloseFile()
|
||||||
if (mFile > 0)
|
{
|
||||||
sceIoClose(mFile);
|
if (mZipAvailable && mZipFile != NULL){
|
||||||
#endif
|
unzCloseCurrentFile(mZipFile);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
int JFileSystem::ReadFile(void *buffer, int size)
|
#if defined (WIN32) || defined (LINUX) || defined (IOS)
|
||||||
{
|
if (mFile != NULL)
|
||||||
if (mZipAvailable && mZipFile != NULL)
|
fclose(mFile);
|
||||||
{
|
#else
|
||||||
return unzReadCurrentFile(mZipFile, buffer, size);
|
if (mFile > 0)
|
||||||
}
|
sceIoClose(mFile);
|
||||||
else
|
#endif
|
||||||
{
|
}
|
||||||
#if defined (WIN32) || defined (LINUX) || defined (IOS)
|
|
||||||
return fread(buffer, 1, size, mFile);
|
|
||||||
#else
|
int JFileSystem::ReadFile(void *buffer, int size)
|
||||||
return sceIoRead(mFile, buffer, size);
|
{
|
||||||
#endif
|
if (mZipAvailable && mZipFile != NULL)
|
||||||
}
|
{
|
||||||
}
|
return unzReadCurrentFile(mZipFile, buffer, size);
|
||||||
|
}
|
||||||
|
else
|
||||||
int JFileSystem::GetFileSize()
|
{
|
||||||
{
|
#if defined (WIN32) || defined (LINUX) || defined (IOS)
|
||||||
return mFileSize;
|
return fread(buffer, 1, size, mFile);
|
||||||
}
|
#else
|
||||||
|
return sceIoRead(mFile, buffer, size);
|
||||||
|
#endif
|
||||||
void JFileSystem::SetResourceRoot(const string& resourceRoot)
|
}
|
||||||
{
|
}
|
||||||
#ifdef IOS
|
|
||||||
NSString *pathUTF8 = [NSString stringWithUTF8String: resourceRoot.c_str()];
|
|
||||||
NSString *fullpath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathUTF8];
|
int JFileSystem::GetFileSize()
|
||||||
mResourceRoot = [fullpath cStringUsingEncoding:1];
|
{
|
||||||
mResourceRoot += "/";
|
return mFileSize;
|
||||||
#else
|
}
|
||||||
mResourceRoot = resourceRoot;
|
|
||||||
#endif
|
|
||||||
}
|
void JFileSystem::SetResourceRoot(const string& resourceRoot)
|
||||||
|
{
|
||||||
string JFileSystem::GetResourceRoot()
|
#ifdef IOS
|
||||||
{
|
NSString *pathUTF8 = [NSString stringWithUTF8String: resourceRoot.c_str()];
|
||||||
return mResourceRoot;
|
NSString *fullpath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathUTF8];
|
||||||
}
|
mResourceRoot = [fullpath cStringUsingEncoding:1];
|
||||||
|
mResourceRoot += "/";
|
||||||
string JFileSystem::GetResourceFile(string filename)
|
#else
|
||||||
{
|
mResourceRoot = resourceRoot;
|
||||||
string result = mResourceRoot;
|
#endif
|
||||||
return result.append(filename);
|
}
|
||||||
|
|
||||||
|
string JFileSystem::GetResourceRoot()
|
||||||
|
{
|
||||||
|
return mResourceRoot;
|
||||||
|
}
|
||||||
|
|
||||||
|
string JFileSystem::GetResourceFile(string filename)
|
||||||
|
{
|
||||||
|
string result = mResourceRoot;
|
||||||
|
return result.append(filename);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user