- 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
+2
View File
@@ -77,6 +77,8 @@ public:
static void Destroy(); static void Destroy();
// closes temporary files when the machine goes to sleep
void Pause();
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
/// Open file for reading. /// Open file for reading.
/// ///
+12 -1
View File
@@ -41,6 +41,11 @@ JZipCache::~JZipCache()
dir.clear(); dir.clear();
} }
void JFileSystem::Pause()
{
filesystem::closeTempFiles();
}
void JFileSystem::preloadZip(const string& filename) void JFileSystem::preloadZip(const string& filename)
{ {
map<string,JZipCache *>::iterator it = mZipCache.find(filename); map<string,JZipCache *>::iterator it = mZipCache.find(filename);
@@ -202,6 +207,7 @@ bool JFileSystem::MakeDir(const string & dir)
JFileSystem::~JFileSystem() JFileSystem::~JFileSystem()
{ {
clearZipCache(); clearZipCache();
filesystem::closeTempFiles();
SAFE_DELETE(mUserFS); SAFE_DELETE(mUserFS);
SAFE_DELETE(mSystemFS); SAFE_DELETE(mSystemFS);
} }
@@ -286,7 +292,12 @@ bool JFileSystem::readIntoString(const string & FilePath, string & target)
int fileSize = GetFileSize(file); 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) if (fileSize)
file.read(&target[0], fileSize); file.read(&target[0], fileSize);
+2
View File
@@ -556,6 +556,8 @@ void JGE::Pause()
mPaused = true; mPaused = true;
if (mApp != NULL) mApp->Pause(); if (mApp != NULL) mApp->Pause();
JFileSystem::GetInstance()->Pause();
} }
void JGE::Resume() void JGE::Resume()
+108 -11
View File
@@ -3,7 +3,7 @@
// zfsystem.cpp: implementation of the zip file system classes. // 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, // For conditions of distribution and use,
// see copyright notice in zfsystem.h // see copyright notice in zfsystem.h
// //
@@ -39,6 +39,7 @@ filesystem * izfstream::pDefaultFS = NULL;
string filesystem::CurrentZipName = ""; string filesystem::CurrentZipName = "";
ifstream filesystem::CurrentZipFile; ifstream filesystem::CurrentZipFile;
filesystem * filesystem::pCurrentFS = NULL; filesystem * filesystem::pCurrentFS = NULL;
std::vector<filesystem::pooledBuffer *> filesystem::m_Buffers;
static const int STORED = 0; static const int STORED = 0;
static const int DEFLATED = 8; static const int DEFLATED = 8;
@@ -86,6 +87,90 @@ filesystem::filesystem(const char * BasePath, const char * FileExt, bool Default
// File System Member Functions // 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) void filesystem::Open(izfstream & File, const char * Filename)
{ {
// Close the file if it was opened; // Close the file if it was opened;
@@ -99,11 +184,11 @@ void filesystem::Open(izfstream & File, const char * Filename)
if (FileNotZipped(FullPath.c_str())) { if (FileNotZipped(FullPath.c_str())) {
// Link the izfile object with an opened filebuf // 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); FileBuf->open(FullPath.c_str(), ios::binary | ios::in);
if (FileBuf->is_open()) { if (FileBuf->is_open()) {
delete File.rdbuf(FileBuf); File.rdbuf(FileBuf);
File.clear(ios::goodbit); File.clear(ios::goodbit);
File.m_FilePath = Filename; File.m_FilePath = Filename;
File.m_FullFilePath = FullPath; File.m_FullFilePath = FullPath;
@@ -148,14 +233,12 @@ void filesystem::Open(izfstream & File, const char * Filename)
if (DataPos != streamoff(-1)) { if (DataPos != streamoff(-1)) {
string zipName = m_BasePath + CurrentZipName; string zipName = m_BasePath + CurrentZipName;
// Open the file at the right position // Open the file at the right position
((izstream &) File).open( zbuffer * buffer = getValidBuffer(zipName, Filename, streamoff(DataPos), streamoff(FileInfo.m_CompSize));
zipName.c_str(),
streamoff(DataPos), if (buffer) {
streamoff(FileInfo.m_CompSize), File.rdbuf(buffer);
FileInfo.m_CompMethod File.SetCompMethod(FileInfo.m_CompMethod);
);
if (File) {
File.m_FilePath = Filename; File.m_FilePath = Filename;
File.m_FullFilePath = FullPath; File.m_FullFilePath = FullPath;
File.m_Zipped = true; File.m_Zipped = true;
@@ -163,6 +246,10 @@ void filesystem::Open(izfstream & File, const char * Filename)
File.m_CompSize = FileInfo.m_CompSize; File.m_CompSize = FileInfo.m_CompSize;
File.m_Offset = FileInfo.m_Offset; 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; return;
// Find the start of the central directory // 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"); 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()); streamoff realBeginOfFile = SkipLFHdr(CurrentZipFile, File.getOffset());
if (! CurrentZipFile.seekg(CentralDirZipped(CurrentZipFile, realBeginOfFile, File.getCompSize()))) if (! CurrentZipFile.seekg(CentralDirZipped(CurrentZipFile, realBeginOfFile, File.getCompSize())))
{
File.close();
return false; return false;
}
// Check every headers within the zip file // Check every headers within the zip file
file_header FileHdr; file_header FileHdr;
@@ -409,7 +503,10 @@ bool filesystem::PreloadZip(const char * Filename, map<string, limited_file_info
else else
{ {
if (! File.seekg(CentralDir(File))) if (! File.seekg(CentralDir(File)))
{
File.close();
return false; return false;
}
// Check every headers within the zip file // Check every headers within the zip file
file_header FileHdr; file_header FileHdr;
+31 -3
View File
@@ -75,6 +75,7 @@ public:
void setFS(filesystem * pFS = pDefaultFS); void setFS(filesystem * pFS = pDefaultFS);
bool Zipped() const; bool Zipped() const;
bool isBeingUsed() const;
const std::string & FilePath() const; const std::string & FilePath() const;
const std::string & FullFilePath() const; const std::string & FullFilePath() const;
size_t getUncompSize(); size_t getUncompSize();
@@ -94,6 +95,7 @@ protected:
size_t m_UncompSize; size_t m_UncompSize;
size_t m_Offset; size_t m_Offset;
size_t m_CompSize; size_t m_CompSize;
bool m_Used;
}; };
@@ -131,6 +133,17 @@ public:
size_t m_Size; 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(const char * BasePath = "", const char * FileExt = "zip", bool DefaultFS = true);
~filesystem(); ~filesystem();
@@ -142,14 +155,18 @@ public:
static std::string getCurrentZipName(); static std::string getCurrentZipName();
static filesystem * getCurrentFS(); static filesystem * getCurrentFS();
static std::streamoff SkipLFHdr(std::istream & File, std::streamoff LFHdrPos); 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 //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); std::vector<std::string>& scanfolder(const std::string& folderName, std::vector<std::string>& results);
friend std::ostream & operator << (std::ostream & Out, const filesystem & FS); friend std::ostream & operator << (std::ostream & Out, const filesystem & FS);
static void closeTempFiles();
protected: protected:
// Zip file info class // Zip file info class
class zipfile_info class zipfile_info
{ {
@@ -181,6 +198,9 @@ protected:
const std::string & FindZip(size_t PackID) const; const std::string & FindZip(size_t PackID) const;
void InsertZip(const char * Filename, const size_t PackID); 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 // New type definitions
typedef std::map<size_t, zipfile_info> zipmap; typedef std::map<size_t, zipfile_info> zipmap;
typedef std::map<size_t, zipfile_info>::iterator zipmap_iterator; typedef std::map<size_t, zipfile_info>::iterator zipmap_iterator;
@@ -194,6 +214,7 @@ protected:
std::string m_FileExt; std::string m_FileExt;
zipmap m_Zips; zipmap m_Zips;
filemap m_Files; filemap m_Files;
static std::vector<pooledBuffer *> m_Buffers;
static std::ifstream CurrentZipFile; static std::ifstream CurrentZipFile;
static std::string CurrentZipName; static std::string CurrentZipName;
static filesystem * pCurrentFS; static filesystem * pCurrentFS;
@@ -242,7 +263,8 @@ inline void izfstream::open(const char * FilePath, filesystem * pFS) {
} }
inline void izfstream::close() { inline void izfstream::close() {
izstream::close(); if (m_pFS)
m_pFS->unuse( * this);
m_FilePath = m_FullFilePath = ""; m_FilePath = m_FullFilePath = "";
m_UncompSize = 0; m_UncompSize = 0;
} }
@@ -255,6 +277,10 @@ inline bool izfstream::Zipped() const {
return m_Zipped; return m_Zipped;
} }
inline bool izfstream::isBeingUsed() const {
return m_Used;
}
inline const std::string & izfstream::FilePath() const { inline const std::string & izfstream::FilePath() const {
return m_FilePath; return m_FilePath;
} }
@@ -273,14 +299,16 @@ inline filesystem::~filesystem() {
// Security mesure with izfile::pDefaultFS // Security mesure with izfile::pDefaultFS
if (izfstream::pDefaultFS == this) if (izfstream::pDefaultFS == this)
izfstream::pDefaultFS = NULL; izfstream::pDefaultFS = NULL;
}
inline void filesystem::closeTempFiles() {
if (CurrentZipName.size()) if (CurrentZipName.size())
{ {
CurrentZipFile.close(); CurrentZipFile.close();
CurrentZipName = ""; CurrentZipName = "";
} }
closeBufferPool();
} }
inline void filesystem::MakeDefault() { inline void filesystem::MakeDefault() {
izfstream::pDefaultFS = this; izfstream::pDefaultFS = this;
} }
+40 -14
View File
@@ -22,6 +22,7 @@ using namespace std;
// zstream Member Functions // zstream Member Functions
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
/*
void izstream::open(const char * Filename, streamoff Offset, streamoff Size, int CompMethod) void izstream::open(const char * Filename, streamoff Offset, streamoff Size, int CompMethod)
{ {
// Change the buffer if need // Change the buffer if need
@@ -67,8 +68,33 @@ zbuffer * izstream::GetRightBuffer(int CompMethod) const
default: default:
return NULL; 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; return NULL;
// adjust file position // adjust file position
if (! m_ZipFile.seekg(Offset, ios::beg)) if (! use(Offset, Size))
return NULL; return NULL;
m_Opened = true; m_Opened = true;
m_Pos = -1; m_Filename = Filename;
m_Size = Size;
return this; return this;
} }
@@ -101,6 +125,7 @@ zbuffer_stored * zbuffer_stored::close()
return NULL; return NULL;
else { else {
m_Opened = false; m_Opened = false;
m_Used = false;
m_ZipFile.close(); m_ZipFile.close();
} }
@@ -127,15 +152,15 @@ int zbuffer_stored::underflow()
return static_cast<unsigned char>(* gptr()); return static_cast<unsigned char>(* gptr());
// Refill de buffer. // 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. // Set the real position of the beginning of the buffer.
if (m_Pos == streamoff(-1)) if (m_Pos == streamoff(-1))
m_Pos = 0; 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. // Reset buffer pointers.
setg( m_Buffer, // beginning of putback area 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) if (ToRead == 0)
return WantedPos; return WantedPos;
if (! m_ZipFile.read(m_Buffer, BUFFERSIZE)) if (! m_ZipFile.read(m_Buffer, ToRead))
return streambuf::seekoff(off, dir, nMode); return streambuf::seekoff(off, dir, nMode);
// Set the buffer at the right position // Set the buffer at the right position
@@ -225,7 +250,7 @@ zbuffer_deflated * zbuffer_deflated::open(const char * Filename, streamoff Offse
return NULL; return NULL;
// adjust file position // adjust file position
if (! m_ZipFile.seekg(Offset, ios::beg)) if (! use(Offset, Size))
return NULL; return NULL;
// z_stream (NULL) Initialization // z_stream (NULL) Initialization
@@ -247,7 +272,7 @@ zbuffer_deflated * zbuffer_deflated::open(const char * Filename, streamoff Offse
m_StreamEnd = false; m_StreamEnd = false;
m_Pos = 0; m_Pos = 0;
m_CompPos = 0; m_CompPos = 0;
m_Size = Size; m_Filename = Filename;
return this; return this;
} }
@@ -260,6 +285,7 @@ zbuffer_deflated * zbuffer_deflated::close()
return NULL; return NULL;
else { else {
m_Opened = false; m_Opened = false;
m_Used = false;
m_ZipFile.close(); m_ZipFile.close();
// z_stream unitialization. // z_stream unitialization.
+19 -10
View File
@@ -69,17 +69,25 @@ public:
virtual zbuffer * close() = 0; virtual zbuffer * close() = 0;
bool is_open() const { return m_Opened; } 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: protected:
zbuffer() : m_Size(0), m_Opened(false) { } zbuffer() : m_Size(0), m_Opened(false), m_Used(false) { }
static const int BUFFERSIZE = 4092; static const int BUFFERSIZE = 4092;
std::string m_Filename;
std::ifstream m_ZipFile; std::ifstream m_ZipFile;
std::streamoff m_Pos; std::streamoff m_Pos;
std::streamoff m_Size; std::streamoff m_Size;
char m_Buffer[BUFFERSIZE]; char m_Buffer[BUFFERSIZE];
bool m_Opened; bool m_Opened;
bool m_Used;
}; };
@@ -88,7 +96,7 @@ protected:
class zbuffer_stored : public zbuffer class zbuffer_stored : public zbuffer
{ {
public: public:
virtual ~zbuffer_stored() { } virtual ~zbuffer_stored() { close(); }
virtual zbuffer_stored * open(const char * Filename, std::streamoff Offset, std::streamoff Size); virtual zbuffer_stored * open(const char * Filename, std::streamoff Offset, std::streamoff Size);
virtual zbuffer_stored * close(); virtual zbuffer_stored * close();
@@ -112,8 +120,7 @@ class zbuffer_deflated : public zbuffer
public: public:
virtual ~zbuffer_deflated() { virtual ~zbuffer_deflated() {
if (m_Opened) close();
inflateEnd(&m_ZStream);
} }
virtual zbuffer_deflated * open(const char * Filename, std::streamoff Offset, std::streamoff Size); virtual zbuffer_deflated * open(const char * Filename, std::streamoff Offset, std::streamoff Size);
@@ -144,23 +151,25 @@ class izstream : public std::istream
public: public:
izstream() : std::istream(NULL), m_CompMethod(-1) { setstate(std::ios::badbit); } 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 open(const char * Filename, std::streamoff Offset, std::streamoff Size, int CompMethod);
void close() { SetCompMethod(-1); } //void close() { SetCompMethod(-1); }
void SetCompMethod(int CompMethod) { m_CompMethod = CompMethod; };
protected: protected:
static const int STORED = 0; static const int STORED = 0;
static const int DEFLATED = 8; 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)); delete rdbuf(GetRightBuffer(m_CompMethod = CompMethod));
if (rdbuf() == NULL) if (rdbuf() == NULL)
setstate(std::ios::badbit); setstate(std::ios::badbit);
} }*/
int m_CompMethod; int m_CompMethod;
}; };
+8 -5
View File
@@ -126,6 +126,7 @@ void GameApp::Create()
LOG("init Res Folder at " + foldersRoot); LOG("init Res Folder at " + foldersRoot);
JFileSystem::init(foldersRoot + "User/", foldersRoot + systemFolder); JFileSystem::init(foldersRoot + "User/", foldersRoot + systemFolder);
// Create User Folders (for write access) if they don't exist // Create User Folders (for write access) if they don't exist
{ {
const char* folders[] = { "ai", "ai/baka", "ai/baka/stats", "campaigns", "graphics", "lang", "packs", "player", "player/stats", "profiles", "rules", "sets", "settings", "sound", "sound/sfx", "themes", "test"}; const char* folders[] = { "ai", "ai/baka", "ai/baka/stats", "campaigns", "graphics", "lang", "packs", "player", "player/stats", "profiles", "rules", "sets", "settings", "sound", "sound/sfx", "themes", "test"};
@@ -160,6 +161,13 @@ void GameApp::Create()
//Test for Music files presence //Test for Music files presence
JFileSystem * jfs = JFileSystem::GetInstance(); JFileSystem * jfs = JFileSystem::GetInstance();
HasMusic = jfs->FileExists(WResourceManager::Instance()->musicFile("Track0.mp3")) && jfs->FileExists(WResourceManager::Instance()->musicFile("Track1.mp3")); HasMusic = jfs->FileExists(WResourceManager::Instance()->musicFile("Track0.mp3")) && jfs->FileExists(WResourceManager::Instance()->musicFile("Track1.mp3"));
LOG("Init Collection");
MTGAllCards::loadInstance();
LOG("Loading rules");
Rules::loadAllRules();
LOG("Loading Textures"); LOG("Loading Textures");
LOG("--Loading menuicons.png"); LOG("--Loading menuicons.png");
@@ -235,8 +243,6 @@ void GameApp::Create()
jq = WResourceManager::Instance()->RetrieveQuad("phasebar.png", 0, 0, 0, 0, "phasebar", RETRIEVE_MANAGE); jq = WResourceManager::Instance()->RetrieveQuad("phasebar.png", 0, 0, 0, 0, "phasebar", RETRIEVE_MANAGE);
LOG("Init Collection");
MTGAllCards::loadInstance();
LOG("Creating Game States"); LOG("Creating Game States");
mGameStates[GAME_STATE_DECK_VIEWER] = NEW GameStateDeckViewer(this); mGameStates[GAME_STATE_DECK_VIEWER] = NEW GameStateDeckViewer(this);
@@ -265,9 +271,6 @@ void GameApp::Create()
mCurrentState = NULL; mCurrentState = NULL;
mNextState = mGameStates[GAME_STATE_MENU]; mNextState = mGameStates[GAME_STATE_MENU];
LOG("--Load Game rules");
Rules::loadAllRules();
//Set Audio volume //Set Audio volume
JSoundSystem::GetInstance()->SetSfxVolume(options[Options::SFXVOLUME].number); JSoundSystem::GetInstance()->SetSfxVolume(options[Options::SFXVOLUME].number);
JSoundSystem::GetInstance()->SetMusicVolume(options[Options::MUSICVOLUME].number); JSoundSystem::GetInstance()->SetMusicVolume(options[Options::MUSICVOLUME].number);
+25 -7
View File
@@ -1750,17 +1750,33 @@ void GameObserver::createPlayer(const string& playerMode
#ifdef TESTSUITE #ifdef TESTSUITE
void GameObserver::loadTestSuitePlayer(int playerId, TestSuiteGame* testSuite) void GameObserver::loadTestSuitePlayer(int playerId, TestSuiteGame* testSuite)
{ {
players.push_back(new TestSuiteAI(testSuite, playerId)); loadPlayer(playerId, new TestSuiteAI(testSuite, playerId));
} }
#endif //TESTSUITE #endif //TESTSUITE
void GameObserver::loadPlayer(int playerId, Player* player) void GameObserver::loadPlayer(int playerId, Player* player)
{ {
players.push_back(player); //Because we're using a vector instead of an array (why?),
// we have to prepare the vector in order to be the right size to accomodate the playerId variable
// see http://code.google.com/p/wagic/issues/detail?id=772
if (players.size() > (size_t) playerId) {
SAFE_DELETE(players[playerId]);
players[playerId] = NULL;
} else {
while (players.size() <= (size_t) playerId)
{
players.push_back(NULL);
}
}
players[playerId] = player;
} }
void GameObserver::loadPlayer(int playerId, PlayerType playerType, int decknb, bool premadeDeck) void GameObserver::loadPlayer(int playerId, PlayerType playerType, int decknb, bool premadeDeck)
{ {
if (decknb) if (decknb)
{ {
if (playerType == PLAYER_TYPE_HUMAN) if (playerType == PLAYER_TYPE_HUMAN)
@@ -1774,7 +1790,8 @@ void GameObserver::loadPlayer(int playerId, PlayerType playerType, int decknb, b
sprintf(deckFile, "%s/deck%i.txt", options.profileFile().c_str(), decknb); sprintf(deckFile, "%s/deck%i.txt", options.profileFile().c_str(), decknb);
char deckFileSmall[255]; char deckFileSmall[255];
sprintf(deckFileSmall, "player_deck%i", decknb); sprintf(deckFileSmall, "player_deck%i", decknb);
players.push_back(NEW HumanPlayer(this, deckFile, deckFileSmall, premadeDeck));
loadPlayer(playerId, NEW HumanPlayer(this, deckFile, deckFileSmall, premadeDeck));
#ifdef NETWORK_SUPPORT #ifdef NETWORK_SUPPORT
// FIXME, this is broken // FIXME, this is broken
if(isNetwork) if(isNetwork)
@@ -1785,7 +1802,7 @@ void GameObserver::loadPlayer(int playerId, PlayerType playerType, int decknb, b
} }
else else
{ //Remote player { //Remote player
mPlayers.push_back(NEW RemotePlayer(mParent->mpNetwork)); loadPlayer(playerId, NEW RemotePlayer(mParent->mpNetwork));
#endif //NETWORK_SUPPORT #endif //NETWORK_SUPPORT
} }
} }
@@ -1794,7 +1811,8 @@ void GameObserver::loadPlayer(int playerId, PlayerType playerType, int decknb, b
AIPlayerFactory playerCreator; AIPlayerFactory playerCreator;
Player * opponent = NULL; Player * opponent = NULL;
if (playerId == 1) opponent = players[0]; if (playerId == 1) opponent = players[0];
players.push_back(playerCreator.createAIPlayer(this, MTGCollection(), opponent, decknb));
loadPlayer(playerId, playerCreator.createAIPlayer(this, MTGCollection(), opponent, decknb));
} }
} }
else else
@@ -1809,11 +1827,11 @@ void GameObserver::loadPlayer(int playerId, PlayerType playerType, int decknb, b
if (playerId == 1) opponent = players[0]; if (playerId == 1) opponent = players[0];
#ifdef AI_CHANGE_TESTING #ifdef AI_CHANGE_TESTING
if (playerType == PLAYER_TYPE_CPU_TEST) if (playerType == PLAYER_TYPE_CPU_TEST)
players.push_back(playerCreator.createAIPlayerTest(this, MTGCollection(), opponent, playerId == 0 ? "ai/bakaA/" : "ai/bakaB/")); loadPlayer(playerId, playerCreator.createAIPlayerTest(this, MTGCollection(), opponent, playerId == 0 ? "ai/bakaA/" : "ai/bakaB/"));
else else
#endif #endif
{ {
players.push_back(playerCreator.createAIPlayer(this, MTGCollection(), opponent)); loadPlayer(playerId, playerCreator.createAIPlayer(this, MTGCollection(), opponent));
} }
if (playerType == PLAYER_TYPE_CPU_TEST) if (playerType == PLAYER_TYPE_CPU_TEST)
+2 -4
View File
@@ -394,11 +394,9 @@ void GameStateMenu::listPrimitives()
{ {
string filename = "sets/primitives/"; string filename = "sets/primitives/";
filename.append(primitiveFiles[i]); filename.append(primitiveFiles[i]);
izfstream file;
if (! JFileSystem::GetInstance()->openForRead(file, filename))
continue;
file.close(); if (! JFileSystem::GetInstance()->FileExists(filename))
continue;
primitives.push_back(filename); primitives.push_back(filename);
} }
primitivesLoadCounter = 0; primitivesLoadCounter = 0;
+9 -5
View File
@@ -330,18 +330,20 @@ int MTGAllCards::load(const char * config_file, const char * set_name, int autol
int lineNumber = 0; int lineNumber = 0;
std::string contents; std::string contents;
if (!JFileSystem::GetInstance()->readIntoString(config_file, contents)) izfstream file;
if (!JFileSystem::GetInstance()->openForRead(file, config_file))
return total_cards; return total_cards;
std::stringstream stream(contents);
string s; string s;
while (true) while (getline(file,s))
{ {
if (!std::getline(stream, s)) return total_cards;
lineNumber++; lineNumber++;
if (!s.size()) continue; if (!s.size()) continue;
if (s[s.size() - 1] == '\r') s.erase(s.size() - 1); // Handle DOS files if (s[s.size() - 1] == '\r')
s.erase(s.size() - 1); //Handle DOS files
if (!s.size()) continue; if (!s.size()) continue;
if (s.find("#AUTO_DEFINE ") == 0) if (s.find("#AUTO_DEFINE ") == 0)
{ {
string toAdd = s.substr(13); string toAdd = s.substr(13);
@@ -374,6 +376,7 @@ int MTGAllCards::load(const char * config_file, const char * set_name, int autol
if (!maxGrade) maxGrade = Constants::GRADE_BORDERLINE; //Default setting for grade is borderline? if (!maxGrade) maxGrade = Constants::GRADE_BORDERLINE; //Default setting for grade is borderline?
if (fileGrade > maxGrade) if (fileGrade > maxGrade)
{ {
file.close();
return total_cards; return total_cards;
} }
} }
@@ -405,6 +408,7 @@ int MTGAllCards::load(const char * config_file, const char * set_name, int autol
continue; continue;
} }
} }
file.close();
return total_cards; return total_cards;
} }
+18 -1
View File
@@ -351,11 +351,23 @@ ManaCost::~ManaCost()
void ManaCost::x() void ManaCost::x()
{ {
if (cost.size() <= (size_t)Constants::NB_Colors)
{
DebugTrace("Seems ManaCost was not properly initialized");
return;
}
cost[Constants::NB_Colors] = 1; cost[Constants::NB_Colors] = 1;
} }
int ManaCost::hasX() int ManaCost::hasX()
{ {
if (cost.size() <= (size_t)Constants::NB_Colors)
{
DebugTrace("Seems ManaCost was not properly initialized");
return 0;
}
return cost[Constants::NB_Colors]; return cost[Constants::NB_Colors];
} }
@@ -465,6 +477,11 @@ void ManaCost::copy(ManaCost * _manaCost)
int ManaCost::getCost(int color) int ManaCost::getCost(int color)
{ {
if (cost.size() <= (size_t)color)
{
DebugTrace("Seems ManaCost was not properly initialized");
return 0;
}
return cost[color]; return cost[color];
} }
@@ -477,7 +494,7 @@ ManaCostHybrid * ManaCost::getHybridCost(unsigned int i)
int ManaCost::hasColor(int color) int ManaCost::hasColor(int color)
{ {
if (cost[color]) if (getCost(color))
return 1; return 1;
for (size_t i = 0; i < hybrids.size(); i++) for (size_t i = 0; i < hybrids.size(); i++)
{ {