From 3347fd020b72c3e616b002fd98cbe4a54f179364 Mon Sep 17 00:00:00 2001 From: "wagic.jeck" Date: Wed, 28 Oct 2009 04:06:45 +0000 Subject: [PATCH] Jeck - Removed some unused bits of code, added zip support to themes' card overrides. @Jean, please check WResourceManager::dirOK() on Linux. This needs some impact testing, to be certain it doesn't noticeably slow anything down. In a future version I'd like to memoize checking for zip file existence in each "Res/sets/XXX/" directory once during boot, and in the themed directory whenever switching themes. --- projects/mtg/bin/Res/graphics/themeinfo.txt | 2 +- projects/mtg/include/WResourceManager.h | 12 +- projects/mtg/src/WCachedResource.cpp | 4 +- projects/mtg/src/WResourceManager.cpp | 177 +++++++++++--------- 4 files changed, 110 insertions(+), 85 deletions(-) diff --git a/projects/mtg/bin/Res/graphics/themeinfo.txt b/projects/mtg/bin/Res/graphics/themeinfo.txt index 056fccb2f..b4a46f62a 100644 --- a/projects/mtg/bin/Res/graphics/themeinfo.txt +++ b/projects/mtg/bin/Res/graphics/themeinfo.txt @@ -13,4 +13,4 @@ This is themeinfo.txt. The first line should contain theme authorship informatio To build a theme, copy any file from the res/graphics/ directory to a directory just like this one. Then comes the fun part... edit it, save it, and load it up in Wagic. Be sure to include a 227*128 pixel "preview.png" to show off your new theme in options menu! -You can also replace card images! Just place your new card picture in the same place it would be under the sets directory. Be careful, though, as you can't use zipped card images in themes. For example, to replace 10th Edition's Demystify in "My Theme", use a file like this: "themes/my theme/sets/10E/129524.jpg" \ No newline at end of file +You can also replace card images! Just place your new card picture in the same place it would be under the sets directory. For example, to replace 10th Edition's Demystify in "My Theme", use a file like this: "themes/my theme/sets/10E/129524.jpg". You can also use zipped card images, just like with the normal sets directory! \ No newline at end of file diff --git a/projects/mtg/include/WResourceManager.h b/projects/mtg/include/WResourceManager.h index 6ee0bcabb..3737b8a4d 100644 --- a/projects/mtg/include/WResourceManager.h +++ b/projects/mtg/include/WResourceManager.h @@ -162,12 +162,13 @@ public: JQuad* GetQuad(int id); //Our file redirect system. - string graphicsFile(const string filename, const string specific = ""); - string avatarFile(const string filename, const string specific = ""); - string cardFile(const string filename, const string specific = ""); - string musicFile(const string filename, const string specific = ""); - string sfxFile(const string filename, const string specific = ""); + string graphicsFile(const string filename); + string avatarFile(const string filename); + string cardFile(const string filename); + string musicFile(const string filename); + string sfxFile(const string filename); int fileOK(string filename, bool relative = false); + int dirOK(string dirname); //For backwards compatibility with JResourceManager. Avoid using these, they're not optimal. int CreateTexture(const string &textureName); @@ -190,6 +191,7 @@ public: #endif private: + bool bThemedCards; //Does the theme have a "sets" directory for overwriting cards? void FlattenTimes(); //To prevent bad cache timing on int overflow //For cached stuff diff --git a/projects/mtg/src/WCachedResource.cpp b/projects/mtg/src/WCachedResource.cpp index e0f37b652..27c442552 100644 --- a/projects/mtg/src/WCachedResource.cpp +++ b/projects/mtg/src/WCachedResource.cpp @@ -273,8 +273,8 @@ bool WCachedTexture::Attempt(string filename, int submode, int & error){ if(submode & TEXTURE_SUB_5551) format = GU_PSM_5551; - if(!realname.size()){ //realname should not be empty, even if file 404s. - error = CACHE_ERROR_BAD; + if(!realname.size()){ + error = CACHE_ERROR_404; return false; } diff --git a/projects/mtg/src/WResourceManager.cpp b/projects/mtg/src/WResourceManager.cpp index 2e704b5b1..600a11b8d 100644 --- a/projects/mtg/src/WResourceManager.cpp +++ b/projects/mtg/src/WResourceManager.cpp @@ -9,6 +9,10 @@ #include #include #include "../include/WResourceManager.h" +#if defined (WIN32) +#include +#include +#endif int idCounter = OTHERS_OFFSET; @@ -171,6 +175,8 @@ WResourceManager::WResourceManager(){ textureWCache.Resize(TEXTURES_CACHE_MINSIZE,MAX_CACHE_OBJECTS); lastTime = 0; lastError = CACHE_ERROR_NONE; + + bThemedCards = false; } WResourceManager::~WResourceManager(){ LOG("==Destroying WResourceManager=="); @@ -495,16 +501,9 @@ JSample * WResourceManager::RetrieveSample(string filename, int style, int submo return NULL; } -string WResourceManager::graphicsFile(const string filename, const string specific){ +string WResourceManager::graphicsFile(const string filename){ char buf[512]; - //Check the specific location, if any. - if(specific != ""){ - sprintf(buf,"%s/%s",specific.c_str(),filename.c_str()); - if(fileOK(buf,true)) - return buf; - } - //Check the theme folder. string theme = options[Options::ACTIVE_THEME].str; @@ -513,7 +512,8 @@ string WResourceManager::graphicsFile(const string filename, const string specif if(fileOK(buf,true)) return buf; } - +/* + //FIXME Put back when we're using modes. //Failure. Check mode graphics string mode = options[Options::ACTIVE_MODE].str; @@ -522,7 +522,7 @@ string WResourceManager::graphicsFile(const string filename, const string specif if(fileOK(buf,true)) return buf; } - +*/ //Failure. Check graphics char graphdir[512]; sprintf(graphdir,"graphics/%s",filename.c_str()); @@ -539,16 +539,9 @@ string WResourceManager::graphicsFile(const string filename, const string specif } -string WResourceManager::avatarFile(const string filename, const string specific){ +string WResourceManager::avatarFile(const string filename){ char buf[512]; - //Check the specific location, if any. - if(specific != ""){ - sprintf(buf,"%s/%s",specific.c_str(),filename.c_str()); - if(fileOK(buf,true)) - return buf; - } - //Check the profile folder. string profile = options[Options::ACTIVE_PROFILE].str; @@ -570,7 +563,8 @@ string WResourceManager::avatarFile(const string filename, const string specific if(fileOK(buf,true)) return buf; } - +/* + //FIXME Put back when we're using modes. //Failure. Check mode graphics string mode = options[Options::ACTIVE_MODE].str; @@ -579,6 +573,7 @@ string WResourceManager::avatarFile(const string filename, const string specific if(fileOK(buf,true)) return buf; } +*/ //Failure. Check Baka sprintf(buf,"ai/baka/avatars/%s",filename.c_str()); @@ -595,30 +590,42 @@ string WResourceManager::avatarFile(const string filename, const string specific return graphdir; } -string WResourceManager::cardFile(const string filename, const string specific){ - JFileSystem* fs = JFileSystem::GetInstance(); - - - //PUT Back the following when we have an actual usage for it (theme, or whatever) - //Right now I'm removing this for performance -/* +string WResourceManager::cardFile(const string filename){ char buf[512]; - //Check the specific location, if any. - if(specific != ""){ - sprintf(buf,"%s/sets/%s",specific.c_str(),filename.c_str()); - if(fileOK(buf,true)) - return buf; - } - + string::size_type i; + string set; + JFileSystem* fs = JFileSystem::GetInstance(); + //Check the theme folder. string theme = options[Options::ACTIVE_THEME].str; if(theme != "" && theme != "Default"){ - sprintf(buf,"themes/%s/sets/%s",theme.c_str(),filename.c_str()); - if(fileOK(buf,true)) - return buf; - } + //Does this theme use custom cards? + if(bThemedCards){ + //Check zipped first. Discover set name. + for(i = 0;i < filename.size();i++){ + if(filename[i] == '\\' || filename[i] == '/') + break; + } + if(i != filename.size()) + set = filename.substr(0,i); + + if(set.size()){ + char zipname[512]; + sprintf(zipname, "Res/themes/%s/sets/%s/%s.zip", theme.c_str(), set.c_str(),set.c_str()); + if (fs->AttachZipFile(zipname)) + return filename.substr(i+1); + } + + sprintf(buf,"themes/%s/sets/%s",theme.c_str(),filename.c_str()); + if(fileOK(buf,true)) + return buf; //Themed, unzipped. + } + } + +//FIXME Put back when we're using modes. +/* //Failure. Check mode string mode = options[Options::ACTIVE_MODE].str; @@ -627,48 +634,38 @@ string WResourceManager::cardFile(const string filename, const string specific){ if(fileOK(buf,true)) return buf; } - */ - //Failure. Check sets + //Failure. Assume it's in a zip file? + if(!set.size()){ //Didn't fill "set" string, so do it now. + for(i = 0;i < filename.size();i++){ + if(filename[i] == '\\' || filename[i] == '/') + break; + } + + if(i != filename.size()) + set = filename.substr(0,i); + } + + if(set.size()){ + char zipname[512]; + sprintf(zipname, "Res/sets/%s/%s.zip", set.c_str(),set.c_str()); + if (fs->AttachZipFile(zipname)) + return filename.substr(i+1); + } + + //Failure. Check for unzipped file in sets char defdir[512]; sprintf(defdir,"sets/%s",filename.c_str()); if(fileOK(defdir,true)) return defdir; - //Failure. Assume it's in a zip file? - string::size_type i; - for(i = 0;i < filename.size();i++){ - if(filename[i] == '\\' || filename[i] == '/') - break; - } - - if(i != filename.size()){ - string set = filename.substr(0,i); - - if(set.size()){ - char zipname[512]; - sprintf(zipname, "Res/sets/%s/%s.zip", set.c_str(),set.c_str()); - if (fileOK(zipname)){ - fs->AttachZipFile(zipname); - return filename.substr(i+1); - } - } - } - //Complete failure. return ""; } -string WResourceManager::musicFile(const string filename, const string specific){ +string WResourceManager::musicFile(const string filename){ char buf[512]; - //Check the specific location, if any. - if(specific != ""){ - sprintf(buf,"%s/%s",specific.c_str(),filename.c_str()); - if(fileOK(buf,true)) - return buf; - } - //Check the theme folder. string theme = options[Options::ACTIVE_THEME].str; @@ -678,6 +675,8 @@ string WResourceManager::musicFile(const string filename, const string specific) return buf; } + /* + //FIXME Put back when we're using modes. //Failure. Check mode string mode = options[Options::ACTIVE_MODE].str; @@ -685,7 +684,7 @@ string WResourceManager::musicFile(const string filename, const string specific) sprintf(buf,"modes/%s/sound/%s",mode.c_str(),filename.c_str()); if(fileOK(buf,true)) return buf; - } + }*/ //Failure. Check sound char defdir[512]; @@ -697,16 +696,9 @@ string WResourceManager::musicFile(const string filename, const string specific) return defdir; } -string WResourceManager::sfxFile(const string filename, const string specific){ +string WResourceManager::sfxFile(const string filename){ char buf[512]; - //Check the specific location, if any. - if(specific != ""){ - sprintf(buf,"%s/%s",specific.c_str(),filename.c_str()); - if(fileOK(buf,true)) - return buf; - } - //Check the theme folder. string theme = options[Options::ACTIVE_THEME].str; @@ -716,6 +708,8 @@ string WResourceManager::sfxFile(const string filename, const string specific){ return buf; } +/* + //FIXME: Put back when we're using modes. //Failure. Check mode string mode = options[Options::ACTIVE_MODE].str; if(mode != "" && mode != "Default"){ @@ -723,7 +717,7 @@ string WResourceManager::sfxFile(const string filename, const string specific){ if(fileOK(buf,true)) return buf; } - +*/ //Failure. Check sound char defdir[512]; sprintf(defdir,"sound/sfx/%s",filename.c_str()); @@ -734,6 +728,25 @@ string WResourceManager::sfxFile(const string filename, const string specific){ return ""; } +int WResourceManager::dirOK(string dirname){ +char fname[512]; + +#if defined (WIN32) + sprintf(fname,RESPATH"/%s",dirname.c_str()); + + struct _stat statBuffer; + return (_stat(fname, &statBuffer) >= 0 && // make sure it exists + statBuffer.st_mode & S_IFDIR); // and it's not a file +#else + sprintf(fname,RESPATH"/%s",dirname.c_str()); + struct stat st; + if(stat(fname,&st) == 0) + return 1; +#endif + return 0; +} + + int WResourceManager::fileOK(string filename, bool relative){ char fname[512]; std::ifstream * fp = NULL; @@ -863,6 +876,16 @@ void WResourceManager::Refresh(){ SAFE_DELETE(oldtex); } + + //Check for card images in theme. + bThemedCards = false; + if(!options[Options::ACTIVE_THEME].isDefault()){ + char buf[512]; + sprintf(buf,"themes/%s/sets",options[Options::ACTIVE_THEME].str.c_str()); + + if(dirOK(buf)) + bThemedCards = true; + } } //WCache