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.
This commit is contained in:
wagic.jeck
2009-10-28 04:06:45 +00:00
parent 6279cc894c
commit 3347fd020b
4 changed files with 110 additions and 85 deletions
+1 -1
View File
@@ -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! 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" 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!
+7 -5
View File
@@ -162,12 +162,13 @@ public:
JQuad* GetQuad(int id); JQuad* GetQuad(int id);
//Our file redirect system. //Our file redirect system.
string graphicsFile(const string filename, const string specific = ""); string graphicsFile(const string filename);
string avatarFile(const string filename, const string specific = ""); string avatarFile(const string filename);
string cardFile(const string filename, const string specific = ""); string cardFile(const string filename);
string musicFile(const string filename, const string specific = ""); string musicFile(const string filename);
string sfxFile(const string filename, const string specific = ""); string sfxFile(const string filename);
int fileOK(string filename, bool relative = false); int fileOK(string filename, bool relative = false);
int dirOK(string dirname);
//For backwards compatibility with JResourceManager. Avoid using these, they're not optimal. //For backwards compatibility with JResourceManager. Avoid using these, they're not optimal.
int CreateTexture(const string &textureName); int CreateTexture(const string &textureName);
@@ -190,6 +191,7 @@ public:
#endif #endif
private: private:
bool bThemedCards; //Does the theme have a "sets" directory for overwriting cards?
void FlattenTimes(); //To prevent bad cache timing on int overflow void FlattenTimes(); //To prevent bad cache timing on int overflow
//For cached stuff //For cached stuff
+2 -2
View File
@@ -273,8 +273,8 @@ bool WCachedTexture::Attempt(string filename, int submode, int & error){
if(submode & TEXTURE_SUB_5551) if(submode & TEXTURE_SUB_5551)
format = GU_PSM_5551; format = GU_PSM_5551;
if(!realname.size()){ //realname should not be empty, even if file 404s. if(!realname.size()){
error = CACHE_ERROR_BAD; error = CACHE_ERROR_404;
return false; return false;
} }
+98 -75
View File
@@ -9,6 +9,10 @@
#include <JFileSystem.h> #include <JFileSystem.h>
#include <assert.h> #include <assert.h>
#include "../include/WResourceManager.h" #include "../include/WResourceManager.h"
#if defined (WIN32)
#include <sys/types.h>
#include <sys/stat.h>
#endif
int idCounter = OTHERS_OFFSET; int idCounter = OTHERS_OFFSET;
@@ -171,6 +175,8 @@ WResourceManager::WResourceManager(){
textureWCache.Resize(TEXTURES_CACHE_MINSIZE,MAX_CACHE_OBJECTS); textureWCache.Resize(TEXTURES_CACHE_MINSIZE,MAX_CACHE_OBJECTS);
lastTime = 0; lastTime = 0;
lastError = CACHE_ERROR_NONE; lastError = CACHE_ERROR_NONE;
bThemedCards = false;
} }
WResourceManager::~WResourceManager(){ WResourceManager::~WResourceManager(){
LOG("==Destroying WResourceManager=="); LOG("==Destroying WResourceManager==");
@@ -495,16 +501,9 @@ JSample * WResourceManager::RetrieveSample(string filename, int style, int submo
return NULL; return NULL;
} }
string WResourceManager::graphicsFile(const string filename, const string specific){ string WResourceManager::graphicsFile(const string filename){
char buf[512]; 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. //Check the theme folder.
string theme = options[Options::ACTIVE_THEME].str; string theme = options[Options::ACTIVE_THEME].str;
@@ -513,7 +512,8 @@ string WResourceManager::graphicsFile(const string filename, const string specif
if(fileOK(buf,true)) if(fileOK(buf,true))
return buf; return buf;
} }
/*
//FIXME Put back when we're using modes.
//Failure. Check mode graphics //Failure. Check mode graphics
string mode = options[Options::ACTIVE_MODE].str; string mode = options[Options::ACTIVE_MODE].str;
@@ -522,7 +522,7 @@ string WResourceManager::graphicsFile(const string filename, const string specif
if(fileOK(buf,true)) if(fileOK(buf,true))
return buf; return buf;
} }
*/
//Failure. Check graphics //Failure. Check graphics
char graphdir[512]; char graphdir[512];
sprintf(graphdir,"graphics/%s",filename.c_str()); 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]; 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. //Check the profile folder.
string profile = options[Options::ACTIVE_PROFILE].str; string profile = options[Options::ACTIVE_PROFILE].str;
@@ -570,7 +563,8 @@ string WResourceManager::avatarFile(const string filename, const string specific
if(fileOK(buf,true)) if(fileOK(buf,true))
return buf; return buf;
} }
/*
//FIXME Put back when we're using modes.
//Failure. Check mode graphics //Failure. Check mode graphics
string mode = options[Options::ACTIVE_MODE].str; string mode = options[Options::ACTIVE_MODE].str;
@@ -579,6 +573,7 @@ string WResourceManager::avatarFile(const string filename, const string specific
if(fileOK(buf,true)) if(fileOK(buf,true))
return buf; return buf;
} }
*/
//Failure. Check Baka //Failure. Check Baka
sprintf(buf,"ai/baka/avatars/%s",filename.c_str()); sprintf(buf,"ai/baka/avatars/%s",filename.c_str());
@@ -595,30 +590,42 @@ string WResourceManager::avatarFile(const string filename, const string specific
return graphdir; return graphdir;
} }
string WResourceManager::cardFile(const string filename, const string specific){ string WResourceManager::cardFile(const string filename){
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
/*
char buf[512]; char buf[512];
//Check the specific location, if any. string::size_type i;
if(specific != ""){ string set;
sprintf(buf,"%s/sets/%s",specific.c_str(),filename.c_str()); JFileSystem* fs = JFileSystem::GetInstance();
if(fileOK(buf,true))
return buf;
}
//Check the theme folder. //Check the theme folder.
string theme = options[Options::ACTIVE_THEME].str; string theme = options[Options::ACTIVE_THEME].str;
if(theme != "" && theme != "Default"){ if(theme != "" && theme != "Default"){
sprintf(buf,"themes/%s/sets/%s",theme.c_str(),filename.c_str()); //Does this theme use custom cards?
if(fileOK(buf,true)) if(bThemedCards){
return buf; //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 //Failure. Check mode
string mode = options[Options::ACTIVE_MODE].str; string mode = options[Options::ACTIVE_MODE].str;
@@ -627,48 +634,38 @@ string WResourceManager::cardFile(const string filename, const string specific){
if(fileOK(buf,true)) if(fileOK(buf,true))
return buf; 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]; char defdir[512];
sprintf(defdir,"sets/%s",filename.c_str()); sprintf(defdir,"sets/%s",filename.c_str());
if(fileOK(defdir,true)) if(fileOK(defdir,true))
return defdir; 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. //Complete failure.
return ""; return "";
} }
string WResourceManager::musicFile(const string filename, const string specific){ string WResourceManager::musicFile(const string filename){
char buf[512]; 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. //Check the theme folder.
string theme = options[Options::ACTIVE_THEME].str; string theme = options[Options::ACTIVE_THEME].str;
@@ -678,6 +675,8 @@ string WResourceManager::musicFile(const string filename, const string specific)
return buf; return buf;
} }
/*
//FIXME Put back when we're using modes.
//Failure. Check mode //Failure. Check mode
string mode = options[Options::ACTIVE_MODE].str; 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()); sprintf(buf,"modes/%s/sound/%s",mode.c_str(),filename.c_str());
if(fileOK(buf,true)) if(fileOK(buf,true))
return buf; return buf;
} }*/
//Failure. Check sound //Failure. Check sound
char defdir[512]; char defdir[512];
@@ -697,16 +696,9 @@ string WResourceManager::musicFile(const string filename, const string specific)
return defdir; return defdir;
} }
string WResourceManager::sfxFile(const string filename, const string specific){ string WResourceManager::sfxFile(const string filename){
char buf[512]; 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. //Check the theme folder.
string theme = options[Options::ACTIVE_THEME].str; string theme = options[Options::ACTIVE_THEME].str;
@@ -716,6 +708,8 @@ string WResourceManager::sfxFile(const string filename, const string specific){
return buf; return buf;
} }
/*
//FIXME: Put back when we're using modes.
//Failure. Check mode //Failure. Check mode
string mode = options[Options::ACTIVE_MODE].str; string mode = options[Options::ACTIVE_MODE].str;
if(mode != "" && mode != "Default"){ if(mode != "" && mode != "Default"){
@@ -723,7 +717,7 @@ string WResourceManager::sfxFile(const string filename, const string specific){
if(fileOK(buf,true)) if(fileOK(buf,true))
return buf; return buf;
} }
*/
//Failure. Check sound //Failure. Check sound
char defdir[512]; char defdir[512];
sprintf(defdir,"sound/sfx/%s",filename.c_str()); sprintf(defdir,"sound/sfx/%s",filename.c_str());
@@ -734,6 +728,25 @@ string WResourceManager::sfxFile(const string filename, const string specific){
return ""; 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){ int WResourceManager::fileOK(string filename, bool relative){
char fname[512]; char fname[512];
std::ifstream * fp = NULL; std::ifstream * fp = NULL;
@@ -863,6 +876,16 @@ void WResourceManager::Refresh(){
SAFE_DELETE(oldtex); 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 //WCache