Most (all?) directory handling is now into the loader.
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@1593 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
parent
29ca63e4f9
commit
2546c88019
@ -55,22 +55,14 @@ void HighscoreManager::SetFilename()
|
||||
if ( getenv("SUPERTUXKART_HIGHSCOREDIR") != NULL )
|
||||
{
|
||||
m_filename = getenv("SUPERTUXKART_HIGHSCOREDIR")
|
||||
+ std::string("/highscores/highscores.data");
|
||||
+ std::string("/highscore.data");
|
||||
}
|
||||
else
|
||||
{
|
||||
m_filename=user_config->getConfigDir();
|
||||
m_filename=loader->getHighscoreFile("highscore.data");
|
||||
}
|
||||
m_filename += "/highscore.data";
|
||||
|
||||
// Set the correct directory separator
|
||||
for(unsigned int i=0; i<m_filename.size(); i++)
|
||||
{
|
||||
if(m_filename[i]=='\\' || m_filename[i]=='/')
|
||||
m_filename[i]=Loader::DIR_SEPARATOR;
|
||||
}
|
||||
return;
|
||||
|
||||
} // SetFilename
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
@ -68,8 +68,8 @@ void KartPropertiesManager::loadKartData(bool dont_load_models)
|
||||
if (StringUtils::has_suffix(*i, ".tkkf"))
|
||||
{
|
||||
KartProperties* kp = new KartProperties();
|
||||
std::string tmp= std::string("data")+Loader::DIR_SEPARATOR + *i;
|
||||
kp->load(tmp.c_str(), "tuxkart-kart", dont_load_models);
|
||||
std::string filename=loader->getKartFile(*i);
|
||||
kp->load(filename.c_str(), "tuxkart-kart", dont_load_models);
|
||||
m_karts_properties.push_back(kp);
|
||||
if(kp->getMaxSteerAngle() > m_max_steer_angle)
|
||||
{
|
||||
|
103
src/loader.cpp
103
src/loader.cpp
@ -115,7 +115,7 @@ Loader::~Loader()
|
||||
{} // ~Loader
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void Loader::makePath(char* path, const char* dir, const char* fname) const
|
||||
void Loader::makePath(std::string& path, const std::string& dir, const std::string& fname) const
|
||||
{
|
||||
|
||||
struct stat mystat;
|
||||
@ -123,23 +123,13 @@ void Loader::makePath(char* path, const char* dir, const char* fname) const
|
||||
for(std::vector<std::string>::const_iterator i = m_search_path.begin();
|
||||
i != m_search_path.end(); ++i)
|
||||
{
|
||||
sprintf(path, "%s%c%s%c%s", i->c_str(), DIR_SEPARATOR, dir,
|
||||
DIR_SEPARATOR, fname);
|
||||
// convert backslashes and slashes to the native form
|
||||
const size_t LEN = strlen(path);
|
||||
for(size_t i = 0; i < LEN; ++i)
|
||||
if(path[i] == '\\' || path[i] == '/')
|
||||
path[i] = DIR_SEPARATOR;
|
||||
|
||||
if(stat(path, &mystat) < 0)
|
||||
continue;
|
||||
|
||||
return;
|
||||
path=(*i)+DIR_SEPARATOR+dir+DIR_SEPARATOR+fname;
|
||||
if(stat(path.c_str(), &mystat) >= 0) return;
|
||||
}
|
||||
|
||||
// error case...
|
||||
char msg[MAX_ERROR_MESSAGE_LENGTH];
|
||||
snprintf(msg, sizeof(msg), "Could not find path for '%s'.", fname);
|
||||
snprintf(msg, sizeof(msg), "Could not find path for '%s'.", fname.c_str());
|
||||
|
||||
throw std::runtime_error(msg);
|
||||
|
||||
@ -148,17 +138,87 @@ void Loader::makePath(char* path, const char* dir, const char* fname) const
|
||||
//-----------------------------------------------------------------------------
|
||||
void Loader::makeModelPath(char* path, const char* FNAME) const
|
||||
{
|
||||
makePath(path, getModelDir(), FNAME);
|
||||
std::string p(path);
|
||||
makePath(p, std::string(getModelDir()), FNAME);
|
||||
strcpy(path, p.c_str());
|
||||
} // makeModelPath
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
std::string Loader::getTexture(const std::string& FNAME) const
|
||||
std::string Loader::getTextureFile(const std::string& FNAME) const
|
||||
{
|
||||
char path[1024];
|
||||
std::string path;
|
||||
makePath(path, getTextureDir(), FNAME.c_str());
|
||||
return std::string(path);
|
||||
return path;
|
||||
} // makeTexturePath
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
std::string Loader::getKartFile(const std::string& fname) const
|
||||
{
|
||||
std::string path;
|
||||
makePath(path, "data", fname.c_str());
|
||||
return path;
|
||||
} // getKartFile
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
std::string Loader::getTrackFile(const std::string& fname) const
|
||||
{
|
||||
std::string path;
|
||||
makePath(path, "data", fname.c_str());
|
||||
return path;
|
||||
} // getTrackFile
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
std::string Loader::getConfigFile(const std::string& fname) const
|
||||
{
|
||||
std::string path;
|
||||
makePath(path, "data", fname.c_str());
|
||||
return path;
|
||||
} // getConfigFile
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
std::string Loader::getHomeDir() const
|
||||
{
|
||||
std::string DIRNAME;
|
||||
#ifdef WIN32
|
||||
// For now the old windows config way is used: store a config file
|
||||
// in the current directory (in other OS a special subdirectory is created)
|
||||
DIRNAME=".";
|
||||
#else
|
||||
if(getenv("HOME")!=NULL)
|
||||
{
|
||||
DIRNAME = getenv("HOME");
|
||||
}
|
||||
else
|
||||
{
|
||||
DIRNAME = ".";
|
||||
}
|
||||
DIRNAME += DIR_SEPARATOR+CONFIGDIR;
|
||||
#endif
|
||||
return DIRNAME;
|
||||
} // getHomeDir
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
std::string Loader::getLogFile(const std::string& fname) const
|
||||
{
|
||||
return getHomeDir()+DIR_SEPARATOR+fname;
|
||||
} // getConfigFile
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
std::string Loader::getHighscoreFile(const std::string& fname) const
|
||||
{
|
||||
return getHomeDir()+DIR_SEPARATOR+fname;
|
||||
} // getHighscoreFile
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifdef HAVE_GHOST_REPLAY
|
||||
std::string Loader::getReplayFile(const std::string& fname) const
|
||||
{
|
||||
std::string path;
|
||||
makePath(path, "replay", fname.c_str());
|
||||
return path;
|
||||
|
||||
} // getReplayFile
|
||||
#endif
|
||||
//-----------------------------------------------------------------------------
|
||||
void Loader::addSearchPath(const std::string& PATH)
|
||||
{
|
||||
@ -219,26 +279,21 @@ void Loader::listFiles(std::set<std::string>& result, const std::string& dir)
|
||||
{
|
||||
struct stat mystat;
|
||||
|
||||
#ifdef DEBUG
|
||||
// don't list directories with a slash on the end, it'll fail on win32
|
||||
assert(dir[dir.size()-1] != '/');
|
||||
#endif
|
||||
|
||||
result.clear();
|
||||
|
||||
for(std::vector<std::string>::const_iterator i = m_search_path.begin();
|
||||
i != m_search_path.end(); ++i)
|
||||
{
|
||||
std::string path = *i;
|
||||
path += DIR_SEPARATOR;
|
||||
path += dir;
|
||||
std::string path = *i+DIR_SEPARATOR+dir;
|
||||
|
||||
if(stat(path.c_str(), &mystat) < 0)
|
||||
continue;
|
||||
if(! S_ISDIR(mystat.st_mode))
|
||||
continue;
|
||||
|
||||
|
||||
ulDir* mydir = ulOpenDir(path.c_str());
|
||||
if(!mydir) continue;
|
||||
|
||||
|
@ -26,20 +26,31 @@
|
||||
#include <set>
|
||||
#include "callback_manager.hpp"
|
||||
|
||||
|
||||
class Loader : public ssgLoaderOptions
|
||||
{
|
||||
public:
|
||||
#ifdef _MSC_VER
|
||||
static const char DIR_SEPARATOR='\\';
|
||||
# define CONFIGDIR "."
|
||||
#else
|
||||
static const char DIR_SEPARATOR='/';
|
||||
# define CONFIGDIR ".supertuxkart"
|
||||
#endif
|
||||
|
||||
public:
|
||||
Loader();
|
||||
~Loader();
|
||||
|
||||
virtual void makeModelPath(char* path, const char* fname) const;
|
||||
std::string getTexture(const std::string& fname) const;
|
||||
virtual void makeModelPath (char* path, const char* fname) const;
|
||||
std::string getTextureFile (const std::string& fname) const;
|
||||
std::string getKartFile (const std::string& fname) const;
|
||||
std::string getTrackFile (const std::string& fname) const;
|
||||
std::string getConfigFile (const std::string& fname) const;
|
||||
std::string getHighscoreFile(const std::string& fname) const;
|
||||
std::string getLogFile (const std::string& fname) const;
|
||||
std::string getHomeDir () const;
|
||||
#ifdef HAVE_GHOST_REPLAY
|
||||
std::string getReplayFile(const std::string& fname) const;
|
||||
#endif
|
||||
|
||||
std::string getPath(const char* name) const;
|
||||
std::string getPath(const std::string name) const {return getPath(name.c_str());}
|
||||
@ -55,7 +66,8 @@ private:
|
||||
std::vector<std::string> m_search_path;
|
||||
CallbackType m_current_callback_type;
|
||||
|
||||
void makePath(char* path, const char* dir, const char* fname) const;
|
||||
void makePath(std::string& path, const std::string& dir,
|
||||
const std::string& fname) const;
|
||||
ssgBranch *createBranch(char *data) const;
|
||||
void preProcessObj ( ssgEntity *n, bool mirror );
|
||||
ssgBranch *animInit (char *data) const;
|
||||
|
16
src/main.cpp
16
src/main.cpp
@ -145,8 +145,7 @@ int handleCmdLine(int argc, char **argv)
|
||||
}
|
||||
else if( (!strcmp(argv[i], "--kart") && i+1<argc ))
|
||||
{
|
||||
std::string filename=std::string("data")+
|
||||
Loader::DIR_SEPARATOR+argv[i+1]+".tkkf";
|
||||
std::string filename=loader->getKartFile(std::string(argv[i+1])+".tkkf");
|
||||
try
|
||||
{
|
||||
std::string s=loader->getPath(filename);
|
||||
@ -182,7 +181,7 @@ int handleCmdLine(int argc, char **argv)
|
||||
}
|
||||
else if( (!strcmp(argv[i], "--stk-config")) && i+1<argc )
|
||||
{
|
||||
stk_config->load(std::string("data")+Loader::DIR_SEPARATOR+argv[i+1]);
|
||||
stk_config->load(loader->getConfigFile(argv[i+1]));
|
||||
fprintf ( stdout, _("STK config will be read from %s.\n"), argv[i+1] ) ;
|
||||
}
|
||||
else if( (!strcmp(argv[i], "--numkarts") || !strcmp(argv[i], "-k")) &&
|
||||
@ -399,7 +398,7 @@ void InitTuxkart()
|
||||
race_manager->setNumLaps (3);
|
||||
race_manager->setRaceMode (RaceSetup::RM_QUICK_RACE);
|
||||
race_manager->setDifficulty(RD_MEDIUM);
|
||||
stk_config->load(std::string("data")+Loader::DIR_SEPARATOR+ "stk_config.data");
|
||||
stk_config->load(loader->getConfigFile("stk_config.data"));
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
@ -416,13 +415,10 @@ int main(int argc, char *argv[] )
|
||||
|
||||
if (user_config->m_log_errors) //Enable logging of stdout and stderr to logfile
|
||||
{
|
||||
std::string logoutfile = user_config->getConfigDir();
|
||||
logoutfile += Loader::DIR_SEPARATOR;
|
||||
std::string logoutfile = loader->getLogFile("stdout.log");
|
||||
std::string logerrfile = loader->getLogFile("stderr.log");
|
||||
std::cout << "Error messages and other text output will be logged to " ;
|
||||
std::cout << logoutfile << "stderr.log and stdout.log" << std::endl;
|
||||
std::string logerrfile = logoutfile;
|
||||
logoutfile += "stdout.log";
|
||||
logerrfile += "stderr.log";
|
||||
std::cout << logoutfile << " and "<<logerrfile;
|
||||
if(freopen (logoutfile.c_str(),"w",stdout)!=stdout)
|
||||
{
|
||||
fprintf(stderr, "Can not open log file '%s'. Writing to stdout instead.\n",
|
||||
|
@ -154,7 +154,7 @@ void Material::install ()
|
||||
m_state -> setExternalPropertyIndex ( m_index ) ;
|
||||
if ( m_texname.size()>0 )
|
||||
{
|
||||
std::string fn=loader->getTexture(m_texname);
|
||||
std::string fn=loader->getTextureFile(m_texname);
|
||||
m_state -> setTexture ( fn.c_str(), !(m_clamp_tex & UCLAMP),
|
||||
!(m_clamp_tex & VCLAMP) );
|
||||
m_state -> enable ( GL_TEXTURE_2D ) ;
|
||||
|
@ -940,12 +940,7 @@ Track::loadDriveline()
|
||||
void
|
||||
Track::readDrivelineFromFile(std::vector<sgVec3Wrapper>& line, const std::string& file_ext)
|
||||
{
|
||||
std::string path = "data";
|
||||
path += Loader::DIR_SEPARATOR;
|
||||
path += m_ident;
|
||||
path += file_ext;
|
||||
path = loader->getPath(path.c_str());
|
||||
|
||||
std::string path = loader->getTrackFile(m_ident+file_ext);
|
||||
FILE *fd = fopen ( path.c_str(), "r" ) ;
|
||||
|
||||
if ( fd == NULL )
|
||||
|
@ -76,7 +76,7 @@ TrackManager::loadTrackList ()
|
||||
{
|
||||
if(StringUtils::has_suffix(*i, ".track"))
|
||||
{
|
||||
std::string track_name= std::string("data")+Loader::DIR_SEPARATOR + *i;
|
||||
std::string track_name= loader->getTrackFile(*i);
|
||||
m_tracks.push_back(new Track(track_name.c_str()));
|
||||
}
|
||||
}
|
||||
|
@ -41,6 +41,7 @@
|
||||
#include "lisp/writer.hpp"
|
||||
#include "translation.hpp"
|
||||
#include "race_manager.hpp"
|
||||
#include "loader.hpp"
|
||||
#if defined(WIN32) && !defined(__CYGWIN__)
|
||||
# define snprintf _snprintf
|
||||
#endif
|
||||
@ -68,41 +69,16 @@ UserConfig::UserConfig(const std::string& filename)
|
||||
UserConfig::~UserConfig()
|
||||
{}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
std::string UserConfig::getConfigDir()
|
||||
{
|
||||
string DIRNAME;
|
||||
#ifdef WIN32
|
||||
// For now the old windows config way is used: store a config file
|
||||
// in the current directory (in other OS a special subdirectory is created)
|
||||
DIRNAME = ".";
|
||||
#else
|
||||
if(getenv("HOME")!=NULL)
|
||||
{
|
||||
DIRNAME = getenv("HOME");
|
||||
}
|
||||
else
|
||||
{
|
||||
DIRNAME = ".";
|
||||
}
|
||||
DIRNAME += "/";
|
||||
DIRNAME += CONFIGDIR;
|
||||
#endif
|
||||
return DIRNAME;
|
||||
} // getConfigDir
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
/**
|
||||
* Set the config filename for each platform
|
||||
*/
|
||||
void UserConfig::setFilename()
|
||||
{
|
||||
filename = getConfigDir();
|
||||
filename += "/";
|
||||
#ifdef WIN32
|
||||
filename += "supertuxkart.cfg";
|
||||
m_filename = loader->getLogFile("supertuxkart.cfg");
|
||||
#else
|
||||
filename += "config";
|
||||
m_filename = loader->getLogFile("config");
|
||||
#endif
|
||||
} // setFilename
|
||||
|
||||
@ -309,7 +285,7 @@ void UserConfig::setDefaults()
|
||||
*/
|
||||
void UserConfig::loadConfig()
|
||||
{
|
||||
loadConfig(filename);
|
||||
loadConfig(m_filename);
|
||||
} // loadConfig
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
@ -322,7 +298,7 @@ void UserConfig::loadConfig()
|
||||
*/
|
||||
int UserConfig::CheckAndCreateDir()
|
||||
{
|
||||
const std::string DIRNAME = getConfigDir();
|
||||
const std::string DIRNAME = loader->getHomeDir();
|
||||
ulDir* u = ulOpenDir(DIRNAME.c_str());
|
||||
if(u)
|
||||
{ // OK, directory exists
|
||||
@ -593,7 +569,7 @@ UserConfig::readInput(const lisp::Lisp* r,
|
||||
/** Call saveConfig with the default filename for this platform. */
|
||||
void UserConfig::saveConfig()
|
||||
{
|
||||
saveConfig(filename);
|
||||
saveConfig(m_filename);
|
||||
} // saveConfig
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
@ -49,8 +49,6 @@
|
||||
#include "lisp/parser.hpp"
|
||||
#include "lisp/writer.hpp"
|
||||
|
||||
#define CONFIGDIR ".supertuxkart"
|
||||
|
||||
class ActionMap;
|
||||
|
||||
/*class for managing general tuxkart configuration data*/
|
||||
@ -63,7 +61,7 @@ private:
|
||||
Input *inputs;
|
||||
} InputMapEntry;
|
||||
|
||||
std::string filename;
|
||||
std::string m_filename;
|
||||
|
||||
/** Stores the GameAction->Input mappings in a way that is suitable for
|
||||
* quick modification of the mappings. Internally this allows multiple
|
||||
@ -165,7 +163,6 @@ public:
|
||||
UserConfig();
|
||||
UserConfig(const std::string& filename);
|
||||
~UserConfig();
|
||||
std::string getConfigDir ();
|
||||
void setDefaults();
|
||||
void setMusic(int m) { m_music = m; }
|
||||
void setSFX (int m) { m_sfx = m; }
|
||||
|
@ -335,17 +335,7 @@ void World::update(float dt)
|
||||
bool World::saveReplayHumanReadable( std::string const &filename ) const
|
||||
{
|
||||
std::string path;
|
||||
try
|
||||
{
|
||||
path = loader->getPath( ReplayBase::REPLAY_FOLDER );
|
||||
}
|
||||
catch(std::runtime_error& e)
|
||||
{
|
||||
fprintf( stderr, _("Couldn't find replay-path: '%s'\n"), ReplayBase::REPLAY_FOLDER.c_str() );
|
||||
return false;
|
||||
}
|
||||
path += DIR_SEPARATOR + filename + ".";
|
||||
path += ReplayBase::REPLAY_FILE_EXTENSION_HUMAN_READABLE;
|
||||
path = loader->getReplayFile(filename+"."+ReplayBase::REPLAY_FILE_EXTENSION_HUMAN_READABLE);
|
||||
|
||||
FILE *fd = fopen( path.c_str(), "w" );
|
||||
if( !fd )
|
||||
@ -388,9 +378,8 @@ bool World::loadReplayHumanReadable( std::string const &filename )
|
||||
assert( m_p_replay_player );
|
||||
m_p_replay_player->destroy();
|
||||
|
||||
std::string path = ReplayBase::REPLAY_FOLDER + DIR_SEPARATOR;
|
||||
path += filename + ".";
|
||||
path += ReplayBase::REPLAY_FILE_EXTENSION_HUMAN_READABLE;
|
||||
std::string path = loader->getReplayFile(filename+"."+
|
||||
ReplayBase::REPLAY_FILE_EXTENSION_HUMAN_READABLE);
|
||||
|
||||
try
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user