- fix for issue 772

- fixed some issues in zstream that prevented to use "getline" in zipped streams 
- added a "pooled buffers" system to keep a cache of file descriptors. This is potentially dangerous though, but shaves several seconds of loading time on the PSP. If problems arise on other platforms I'll make it a compilation parameter
- gracefully fail when calling manacost information on a not properly initialized mana cost
This commit is contained in:
wagic.the.homebrew
2011-12-28 16:14:08 +00:00
parent 9e80314f6d
commit 591f0e0d30
12 changed files with 276 additions and 61 deletions

View File

@@ -77,6 +77,8 @@ public:
static void Destroy();
// closes temporary files when the machine goes to sleep
void Pause();
//////////////////////////////////////////////////////////////////////////
/// Open file for reading.
///

View File

@@ -41,6 +41,11 @@ JZipCache::~JZipCache()
dir.clear();
}
void JFileSystem::Pause()
{
filesystem::closeTempFiles();
}
void JFileSystem::preloadZip(const string& filename)
{
map<string,JZipCache *>::iterator it = mZipCache.find(filename);
@@ -202,6 +207,7 @@ bool JFileSystem::MakeDir(const string & dir)
JFileSystem::~JFileSystem()
{
clearZipCache();
filesystem::closeTempFiles();
SAFE_DELETE(mUserFS);
SAFE_DELETE(mSystemFS);
}
@@ -286,7 +292,12 @@ bool JFileSystem::readIntoString(const string & FilePath, string & target)
int fileSize = GetFileSize(file);
target.resize((std::string::size_type) fileSize);
try {
target.resize((std::string::size_type) fileSize);
} catch (bad_alloc&) {
return false;
}
if (fileSize)
file.read(&target[0], fileSize);

View File

@@ -556,6 +556,8 @@ void JGE::Pause()
mPaused = true;
if (mApp != NULL) mApp->Pause();
JFileSystem::GetInstance()->Pause();
}
void JGE::Resume()

View File

@@ -3,7 +3,7 @@
// zfsystem.cpp: implementation of the zip file system classes.
//
// Copyright (C) 2004 Tanguy Fautré.
// Copyright (C) 2004 Tanguy Fautré.
// For conditions of distribution and use,
// see copyright notice in zfsystem.h
//
@@ -39,6 +39,7 @@ filesystem * izfstream::pDefaultFS = NULL;
string filesystem::CurrentZipName = "";
ifstream filesystem::CurrentZipFile;
filesystem * filesystem::pCurrentFS = NULL;
std::vector<filesystem::pooledBuffer *> filesystem::m_Buffers;
static const int STORED = 0;
static const int DEFLATED = 8;
@@ -86,6 +87,90 @@ filesystem::filesystem(const char * BasePath, const char * FileExt, bool Default
// File System Member Functions
//////////////////////////////////////////////////////////////////////
zbuffer * filesystem::getValidBuffer(const std::string & filename, const std::string & externalFilename, std::streamoff Offset, std::streamoff Size )
{
//if exists filename in pool and is not in use, return that
for (size_t i = 0; i < m_Buffers.size(); ++i)
{
if (m_Buffers[i]->filename != filename)
continue;
zbuffer * buffer = m_Buffers[i]->buffer;
if (buffer && !buffer->is_used())
{
buffer->use(Offset, Size);
return buffer;
}
}
// if more than 3 objects in the pool, delete and close the first one that is unused
if (m_Buffers.size() > 3)
{
for (size_t i = 0; i < m_Buffers.size(); ++i)
{
zbuffer * buffer = m_Buffers[i]->buffer;
if (buffer && !buffer->is_used())
{
delete m_Buffers[i];
m_Buffers.erase(m_Buffers.begin() + i);
break;
}
}
}
//No possiblility to open more files for now
if (m_Buffers.size() > 3)
return NULL;
//create a new buffer object, add it to the pool, and return that
pooledBuffer * pb = new pooledBuffer(filename, externalFilename);
zbuffer * buffer = new zbuffer_stored();
buffer->open(filename.c_str(), Offset, Size);
pb->buffer = buffer;
m_Buffers.push_back(pb);
return pb->buffer;
}
void filesystem::closeBufferPool()
{
for (size_t i = 0; i < m_Buffers.size(); ++i)
{
if (m_Buffers[i])
{
if (m_Buffers[i]->buffer && m_Buffers[i]->buffer->is_used())
{
LOG("FATAL: File Buffer still in use but need to close");
}
delete m_Buffers[i];
}
}
m_Buffers.clear();
}
void filesystem::unuse(izfstream & File)
{
File.setstate(std::ios::badbit);
if (!File.Zipped())
{
std::streambuf * buffer = File.rdbuf(NULL);
if (buffer)
delete(buffer);
}
else
{
zbuffer * buffer = static_cast<zbuffer *>(File.rdbuf());
if (buffer)
buffer->unuse();
}
}
void filesystem::Open(izfstream & File, const char * Filename)
{
// Close the file if it was opened;
@@ -99,11 +184,11 @@ void filesystem::Open(izfstream & File, const char * Filename)
if (FileNotZipped(FullPath.c_str())) {
// Link the izfile object with an opened filebuf
filebuf * FileBuf = new filebuf;
filebuf * FileBuf = new filebuf;
FileBuf->open(FullPath.c_str(), ios::binary | ios::in);
if (FileBuf->is_open()) {
delete File.rdbuf(FileBuf);
File.rdbuf(FileBuf);
File.clear(ios::goodbit);
File.m_FilePath = Filename;
File.m_FullFilePath = FullPath;
@@ -148,14 +233,12 @@ void filesystem::Open(izfstream & File, const char * Filename)
if (DataPos != streamoff(-1)) {
string zipName = m_BasePath + CurrentZipName;
// Open the file at the right position
((izstream &) File).open(
zipName.c_str(),
streamoff(DataPos),
streamoff(FileInfo.m_CompSize),
FileInfo.m_CompMethod
);
zbuffer * buffer = getValidBuffer(zipName, Filename, streamoff(DataPos), streamoff(FileInfo.m_CompSize));
if (buffer) {
File.rdbuf(buffer);
File.SetCompMethod(FileInfo.m_CompMethod);
if (File) {
File.m_FilePath = Filename;
File.m_FullFilePath = FullPath;
File.m_Zipped = true;
@@ -163,6 +246,10 @@ void filesystem::Open(izfstream & File, const char * Filename)
File.m_CompSize = FileInfo.m_CompSize;
File.m_Offset = FileInfo.m_Offset;
}
else
{
File.setstate(ios::badbit);
}
}
}
@@ -326,7 +413,11 @@ void filesystem::InsertZip(const char * Filename, const size_t PackID)
return;
// Find the start of the central directory
if (! File.seekg(CentralDir(File))) return;
if (! File.seekg(CentralDir(File)))
{
File.close();
return;
}
LOG("open zip ok");
@@ -381,7 +472,10 @@ bool filesystem::PreloadZip(const char * Filename, map<string, limited_file_info
{
streamoff realBeginOfFile = SkipLFHdr(CurrentZipFile, File.getOffset());
if (! CurrentZipFile.seekg(CentralDirZipped(CurrentZipFile, realBeginOfFile, File.getCompSize())))
{
File.close();
return false;
}
// Check every headers within the zip file
file_header FileHdr;
@@ -409,7 +503,10 @@ bool filesystem::PreloadZip(const char * Filename, map<string, limited_file_info
else
{
if (! File.seekg(CentralDir(File)))
{
File.close();
return false;
}
// Check every headers within the zip file
file_header FileHdr;

View File

@@ -75,6 +75,7 @@ public:
void setFS(filesystem * pFS = pDefaultFS);
bool Zipped() const;
bool isBeingUsed() const;
const std::string & FilePath() const;
const std::string & FullFilePath() const;
size_t getUncompSize();
@@ -94,6 +95,7 @@ protected:
size_t m_UncompSize;
size_t m_Offset;
size_t m_CompSize;
bool m_Used;
};
@@ -131,6 +133,17 @@ public:
size_t m_Size;
};
class pooledBuffer
{
public:
pooledBuffer(std::string filename, std::string externalFilename ) : filename(filename), externalFilename(externalFilename), buffer(NULL) {}
~pooledBuffer() { if (buffer) { delete buffer; } }
std::string filename;
std::string externalFilename;
zbuffer * buffer;
};
filesystem(const char * BasePath = "", const char * FileExt = "zip", bool DefaultFS = true);
~filesystem();
@@ -142,14 +155,18 @@ public:
static std::string getCurrentZipName();
static filesystem * getCurrentFS();
static std::streamoff SkipLFHdr(std::istream & File, std::streamoff LFHdrPos);
void unuse(izfstream & File);
//Fills the vector results with a list of children of the given folder
std::vector<std::string>& scanfolder(const std::string& folderName, std::vector<std::string>& results);
friend std::ostream & operator << (std::ostream & Out, const filesystem & FS);
static void closeTempFiles();
protected:
// Zip file info class
class zipfile_info
{
@@ -181,6 +198,9 @@ protected:
const std::string & FindZip(size_t PackID) const;
void InsertZip(const char * Filename, const size_t PackID);
static zbuffer * getValidBuffer(const std::string & filename, const std::string & externalFilename, std::streamoff Offset = 0, std::streamoff Size = 0);
static void closeBufferPool();
// New type definitions
typedef std::map<size_t, zipfile_info> zipmap;
typedef std::map<size_t, zipfile_info>::iterator zipmap_iterator;
@@ -194,6 +214,7 @@ protected:
std::string m_FileExt;
zipmap m_Zips;
filemap m_Files;
static std::vector<pooledBuffer *> m_Buffers;
static std::ifstream CurrentZipFile;
static std::string CurrentZipName;
static filesystem * pCurrentFS;
@@ -242,7 +263,8 @@ inline void izfstream::open(const char * FilePath, filesystem * pFS) {
}
inline void izfstream::close() {
izstream::close();
if (m_pFS)
m_pFS->unuse( * this);
m_FilePath = m_FullFilePath = "";
m_UncompSize = 0;
}
@@ -255,6 +277,10 @@ inline bool izfstream::Zipped() const {
return m_Zipped;
}
inline bool izfstream::isBeingUsed() const {
return m_Used;
}
inline const std::string & izfstream::FilePath() const {
return m_FilePath;
}
@@ -273,14 +299,16 @@ inline filesystem::~filesystem() {
// Security mesure with izfile::pDefaultFS
if (izfstream::pDefaultFS == this)
izfstream::pDefaultFS = NULL;
}
inline void filesystem::closeTempFiles() {
if (CurrentZipName.size())
{
CurrentZipFile.close();
CurrentZipName = "";
}
closeBufferPool();
}
inline void filesystem::MakeDefault() {
izfstream::pDefaultFS = this;
}

View File

@@ -22,6 +22,7 @@ using namespace std;
// zstream Member Functions
//////////////////////////////////////////////////////////////////////
/*
void izstream::open(const char * Filename, streamoff Offset, streamoff Size, int CompMethod)
{
// Change the buffer if need
@@ -67,8 +68,33 @@ zbuffer * izstream::GetRightBuffer(int CompMethod) const
default:
return NULL;
}
}
}*/
bool zbuffer::use(std::streamoff Offset, std::streamoff Size)
{
if (! m_ZipFile)
return false;
//Don't use a buffer already used;
if (m_Used)
return false;
// adjust file position
if (! m_ZipFile.seekg(Offset, ios::beg))
return false;
setg( m_Buffer, // beginning of putback area
m_Buffer, // read position
m_Buffer); // end of buffer
m_Buffer[0] = 0;
m_Pos = -1;
m_Size = Size;
m_Used = true;
return true;
}
//////////////////////////////////////////////////////////////////////
@@ -83,13 +109,11 @@ zbuffer_stored * zbuffer_stored::open(const char * Filename, streamoff Offset, s
return NULL;
// adjust file position
if (! m_ZipFile.seekg(Offset, ios::beg))
if (! use(Offset, Size))
return NULL;
m_Opened = true;
m_Pos = -1;
m_Size = Size;
m_Filename = Filename;
return this;
}
@@ -101,6 +125,7 @@ zbuffer_stored * zbuffer_stored::close()
return NULL;
else {
m_Opened = false;
m_Used = false;
m_ZipFile.close();
}
@@ -127,15 +152,15 @@ int zbuffer_stored::underflow()
return static_cast<unsigned char>(* gptr());
// Refill de buffer.
streamoff ToRead = ((m_Size - m_Pos) < BUFFERSIZE) ? (m_Size - m_Pos) : BUFFERSIZE;
if ((ToRead == 0) || (! m_ZipFile.read(m_Buffer, BUFFERSIZE)))
return EOF;
// Set the real position of the beginning of the buffer.
if (m_Pos == streamoff(-1))
m_Pos = 0;
else
m_Pos += ToRead;
streamoff ToRead = ((m_Size - m_Pos) < BUFFERSIZE) ? (m_Size - m_Pos) : BUFFERSIZE;
if ((ToRead == 0) || (! m_ZipFile.read(m_Buffer, ToRead)))
return EOF;
m_Pos += ToRead;
// Reset buffer pointers.
setg( m_Buffer, // beginning of putback area
@@ -188,7 +213,7 @@ streampos zbuffer_stored::seekoff(streamoff off, ios::seekdir dir, ios::openmod
if (ToRead == 0)
return WantedPos;
if (! m_ZipFile.read(m_Buffer, BUFFERSIZE))
if (! m_ZipFile.read(m_Buffer, ToRead))
return streambuf::seekoff(off, dir, nMode);
// Set the buffer at the right position
@@ -225,7 +250,7 @@ zbuffer_deflated * zbuffer_deflated::open(const char * Filename, streamoff Offse
return NULL;
// adjust file position
if (! m_ZipFile.seekg(Offset, ios::beg))
if (! use(Offset, Size))
return NULL;
// z_stream (NULL) Initialization
@@ -247,7 +272,7 @@ zbuffer_deflated * zbuffer_deflated::open(const char * Filename, streamoff Offse
m_StreamEnd = false;
m_Pos = 0;
m_CompPos = 0;
m_Size = Size;
m_Filename = Filename;
return this;
}
@@ -260,6 +285,7 @@ zbuffer_deflated * zbuffer_deflated::close()
return NULL;
else {
m_Opened = false;
m_Used = false;
m_ZipFile.close();
// z_stream unitialization.

View File

@@ -69,17 +69,25 @@ public:
virtual zbuffer * close() = 0;
bool is_open() const { return m_Opened; }
bool is_used() const {return m_Used;}
void unuse() { m_Used = false;}
bool use(std::streamoff Offset, std::streamoff Size);
std::string getFilename() { return m_Filename; }
protected:
zbuffer() : m_Size(0), m_Opened(false) { }
zbuffer() : m_Size(0), m_Opened(false), m_Used(false) { }
static const int BUFFERSIZE = 4092;
std::string m_Filename;
std::ifstream m_ZipFile;
std::streamoff m_Pos;
std::streamoff m_Size;
char m_Buffer[BUFFERSIZE];
bool m_Opened;
bool m_Used;
};
@@ -88,7 +96,7 @@ protected:
class zbuffer_stored : public zbuffer
{
public:
virtual ~zbuffer_stored() { }
virtual ~zbuffer_stored() { close(); }
virtual zbuffer_stored * open(const char * Filename, std::streamoff Offset, std::streamoff Size);
virtual zbuffer_stored * close();
@@ -112,8 +120,7 @@ class zbuffer_deflated : public zbuffer
public:
virtual ~zbuffer_deflated() {
if (m_Opened)
inflateEnd(&m_ZStream);
close();
}
virtual zbuffer_deflated * open(const char * Filename, std::streamoff Offset, std::streamoff Size);
@@ -144,23 +151,25 @@ class izstream : public std::istream
public:
izstream() : std::istream(NULL), m_CompMethod(-1) { setstate(std::ios::badbit); }
virtual ~izstream() { delete rdbuf(); }
virtual ~izstream() { rdbuf(NULL); } //This doesn't delete the buffer, deletion is handled by zfsystem;
void open(const char * Filename, std::streamoff Offset, std::streamoff Size, int CompMethod);
void close() { SetCompMethod(-1); }
//void open(const char * Filename, std::streamoff Offset, std::streamoff Size, int CompMethod);
//void close() { SetCompMethod(-1); }
void SetCompMethod(int CompMethod) { m_CompMethod = CompMethod; };
protected:
static const int STORED = 0;
static const int DEFLATED = 8;
zbuffer * GetRightBuffer(int CompMethod) const;
//zbuffer * GetRightBuffer(int CompMethod) const;
void SetCompMethod(int CompMethod) {
/*void SetCompMethod(int CompMethod) {
delete rdbuf(GetRightBuffer(m_CompMethod = CompMethod));
if (rdbuf() == NULL)
setstate(std::ios::badbit);
}
}*/
int m_CompMethod;
};