Erwan
- Zip file Directory cache for faster access - Preload card images in player's hand before the game starts
This commit is contained in:
@@ -35,6 +35,14 @@ using namespace std;
|
|||||||
///
|
///
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
class JZipCache {
|
||||||
|
public:
|
||||||
|
JZipCache();
|
||||||
|
~JZipCache();
|
||||||
|
map<string,unz_file_pos *> dir;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
class JFileSystem
|
class JFileSystem
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -109,10 +117,12 @@ protected:
|
|||||||
private:
|
private:
|
||||||
static JFileSystem* mInstance;
|
static JFileSystem* mInstance;
|
||||||
|
|
||||||
|
map<string,JZipCache *>mZipCache;
|
||||||
string mResourceRoot;
|
string mResourceRoot;
|
||||||
string mZipFileName;
|
string mZipFileName;
|
||||||
char *mPassword;
|
char *mPassword;
|
||||||
bool mZipAvailable;
|
bool mZipAvailable;
|
||||||
|
void preloadZip(string filename);
|
||||||
#if defined (WIN32) || defined (LINUX)
|
#if defined (WIN32) || defined (LINUX)
|
||||||
FILE *mFile;
|
FILE *mFile;
|
||||||
#else
|
#else
|
||||||
|
|||||||
@@ -23,6 +23,41 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
JZipCache::JZipCache(){}
|
||||||
|
|
||||||
|
JZipCache::~JZipCache(){
|
||||||
|
map<string,unz_file_pos *>::iterator it;
|
||||||
|
for (it = dir.begin(); it != dir.end(); it++){
|
||||||
|
delete(it->second);
|
||||||
|
}
|
||||||
|
dir.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void JFileSystem::preloadZip(string filename){
|
||||||
|
map<string,JZipCache *>::iterator it = mZipCache.find(filename);
|
||||||
|
if (it != mZipCache.end()) return;
|
||||||
|
|
||||||
|
JZipCache * cache = new JZipCache();
|
||||||
|
mZipCache[filename] = cache;
|
||||||
|
|
||||||
|
if (!mZipAvailable || !mZipFile) {
|
||||||
|
AttachZipFile(filename);
|
||||||
|
if (!mZipAvailable || !mZipFile) return;
|
||||||
|
}
|
||||||
|
int err = unzGoToFirstFile (mZipFile);
|
||||||
|
while (err == UNZ_OK){
|
||||||
|
unz_file_pos* filePos = new unz_file_pos();
|
||||||
|
char filenameInzip[4096];
|
||||||
|
if (unzGetCurrentFileInfo(mZipFile, NULL, filenameInzip, sizeof(filenameInzip), NULL, 0, NULL, 0) == UNZ_OK){
|
||||||
|
unzGetFilePos(mZipFile, filePos);
|
||||||
|
string name = filenameInzip;
|
||||||
|
cache->dir[name] = filePos;
|
||||||
|
}
|
||||||
|
err = unzGoToNextFile(mZipFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
JFileSystem* JFileSystem::mInstance = NULL;
|
JFileSystem* JFileSystem::mInstance = NULL;
|
||||||
|
|
||||||
JFileSystem* JFileSystem::GetInstance()
|
JFileSystem* JFileSystem::GetInstance()
|
||||||
@@ -69,6 +104,12 @@ JFileSystem::JFileSystem()
|
|||||||
JFileSystem::~JFileSystem()
|
JFileSystem::~JFileSystem()
|
||||||
{
|
{
|
||||||
DetachZipFile();
|
DetachZipFile();
|
||||||
|
|
||||||
|
map<string,JZipCache *>::iterator it;
|
||||||
|
for (it = mZipCache.begin(); it != mZipCache.end(); it++){
|
||||||
|
delete(it->second);
|
||||||
|
}
|
||||||
|
mZipCache.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -117,10 +158,19 @@ bool JFileSystem::OpenFile(const string &filename)
|
|||||||
|
|
||||||
if (mZipAvailable && mZipFile != NULL)
|
if (mZipAvailable && mZipFile != NULL)
|
||||||
{
|
{
|
||||||
if (unzLocateFile(mZipFile, filename.c_str(), 0) != UNZ_OK){
|
preloadZip(mZipFileName);
|
||||||
|
map<string,JZipCache *>::iterator it = mZipCache.find(mZipFileName);
|
||||||
|
if (it == mZipCache.end()){
|
||||||
DetachZipFile();
|
DetachZipFile();
|
||||||
return OpenFile(filename);
|
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];
|
char filenameInzip[256];
|
||||||
unz_file_info fileInfo;
|
unz_file_info fileInfo;
|
||||||
|
|
||||||
|
|||||||
@@ -155,6 +155,14 @@ void GameObserver::startGame(int shuffle, int draw){
|
|||||||
for (i=0; i<nbPlayers; i++){
|
for (i=0; i<nbPlayers; i++){
|
||||||
players[i]->game->initGame(shuffle, draw);
|
players[i]->game->initGame(shuffle, draw);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Preload images from hand
|
||||||
|
if (!players[0]->isAI()){
|
||||||
|
for (i=0; i< players[0]->game->hand->nb_cards; i++){
|
||||||
|
players[0]->game->hand->cards[i]->getThumb();
|
||||||
|
players[0]->game->hand->cards[i]->getQuad();
|
||||||
|
}
|
||||||
|
}
|
||||||
turn = 0;
|
turn = 0;
|
||||||
phaseRing->goToPhase(Constants::MTG_PHASE_FIRSTMAIN, players[0]);
|
phaseRing->goToPhase(Constants::MTG_PHASE_FIRSTMAIN, players[0]);
|
||||||
currentGamePhase = Constants::MTG_PHASE_FIRSTMAIN;
|
currentGamePhase = Constants::MTG_PHASE_FIRSTMAIN;
|
||||||
|
|||||||
Reference in New Issue
Block a user