Restored -datadir and -langdir arguments, added -savedir

Also, fixed some crashes when unable to open file
dev-mp
krzys-h 2014-08-06 12:27:17 +02:00
parent 9a3cd67c3b
commit 0b2f25a6e3
11 changed files with 99 additions and 38 deletions

View File

@ -147,6 +147,10 @@ CApplication::CApplication()
m_mouseButtonsState = 0;
m_trackedKeys = 0;
m_dataPath = GetSystemUtils()->GetDataPath();
m_langPath = GetSystemUtils()->GetLangPath();
m_savePath = GetSystemUtils()->GetSaveDir();
m_runSceneName = "";
m_runSceneRank = 0;
@ -208,8 +212,10 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
OPT_LOGLEVEL,
OPT_LANGUAGE,
OPT_LANGDIR,
OPT_VBO,
OPT_MOD
OPT_DATADIR,
OPT_SAVEDIR,
OPT_MOD,
OPT_VBO
};
option options[] =
@ -221,8 +227,10 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
{ "loglevel", required_argument, nullptr, OPT_LOGLEVEL },
{ "language", required_argument, nullptr, OPT_LANGUAGE },
{ "langdir", required_argument, nullptr, OPT_LANGDIR },
{ "vbo", required_argument, nullptr, OPT_VBO },
{ "datadir", required_argument, nullptr, OPT_DATADIR },
{ "savedir", required_argument, nullptr, OPT_SAVEDIR },
{ "mod", required_argument, nullptr, OPT_MOD },
{ "vbo", required_argument, nullptr, OPT_VBO },
{ nullptr, 0, nullptr, 0}
};
@ -260,8 +268,10 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
GetLogger()->Message(" -loglevel level set log level to level (one of: trace, debug, info, warn, error, none)\n");
GetLogger()->Message(" -language lang set language (one of: en, de, fr, pl, ru)\n");
GetLogger()->Message(" -langdir path set custom language directory path\n");
GetLogger()->Message(" -vbo mode set OpenGL VBO mode (one of: auto, enable, disable)\n");
GetLogger()->Message(" -datadir path set custom data directory path\n");
GetLogger()->Message(" -savedir path set custom save directory path (must be writable)\n");
GetLogger()->Message(" -mod path load datadir mod from given path\n");
GetLogger()->Message(" -vbo mode set OpenGL VBO mode (one of: auto, enable, disable)\n");
return PARSE_ARGS_HELP;
}
case OPT_DEBUG:
@ -341,11 +351,28 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
break;
}
case OPT_DATADIR:
{
m_dataPath = optarg;
GetLogger()->Info("Using data dir: '%s'\n", optarg);
break;
}
case OPT_LANGDIR:
{
m_langPath = optarg;
GetLogger()->Info("Using language dir: '%s'\n", optarg);
break;
}
case OPT_SAVEDIR:
{
m_savePath = optarg;
GetLogger()->Info("Using save dir: '%s'\n", optarg);
break;
}
case OPT_MOD:
{
GetLogger()->Info("Loading mod from \"%s\"\n", optarg);
GetLogger()->Info("Loading mod: '%s'\n", optarg);
CResourceManager::AddLocation(optarg, true);
break;
}
default:
@ -363,23 +390,28 @@ bool CApplication::Create()
GetLogger()->Info("Creating CApplication\n");
boost::filesystem::path dataPath(m_dataPath);
if (! (boost::filesystem::exists(dataPath) && boost::filesystem::is_directory(dataPath)) )
{
GetLogger()->Error("Data directory '%s' doesn't exist or is not a directory\n", m_dataPath.c_str());
m_errorMessage = std::string("Could not read from data directory:\n") +
std::string("'") + m_dataPath + std::string("'\n") +
std::string("Please check your installation, or supply a valid data directory by -datadir option.");
m_exitCode = 1;
return false;
}
CResourceManager::AddLocation(m_dataPath, false);
boost::filesystem::create_directories(m_savePath);
CResourceManager::SetSaveLocation(m_savePath);
CResourceManager::AddLocation(m_savePath, true);
if (!GetProfile().InitCurrentDirectory())
{
GetLogger()->Warn("Config not found. Default values will be used!\n");
defaultValues = true;
}
boost::filesystem::path dataPath(COLOBOT_DEFAULT_DATADIR);
if (! (boost::filesystem::exists(dataPath) && boost::filesystem::is_directory(dataPath)) )
{
GetLogger()->Error("Data directory '%s' doesn't exist or is not a directory\n", COLOBOT_DEFAULT_DATADIR);
m_errorMessage = std::string("Could not read from data directory:\n") +
std::string("'") + COLOBOT_DEFAULT_DATADIR + std::string("'\n") +
std::string("Please check your installation, or supply a valid data directory by -datadir option.");
m_exitCode = 1;
return false;
}
if (GetProfile().GetLocalProfileString("Language", "Lang", path)) {
Language language;
if (ParseLanguage(path, language)) {
@ -1696,7 +1728,7 @@ void CApplication::SetLanguage(Language language)
setlocale(LC_ALL, "");
bindtextdomain("colobot", CResourceManager::GetLanguageLocation().c_str());
bindtextdomain("colobot", m_langPath.c_str());
bind_textdomain_codeset("colobot", "UTF-8");
textdomain("colobot");

View File

@ -465,6 +465,15 @@ protected:
//! Current state of joystick buttons; may be updated from another thread
std::vector<bool> m_joyButtonState;
//! Path to directory with data files
std::string m_dataPath;
//! Path to directory with language files
std::string m_langPath;
//! Path to directory with save files
std::string m_savePath;
//@{
//! Scene to run on startup
std::string m_runSceneName;

View File

@ -79,7 +79,6 @@ int SDL_MAIN_FUNC(int argc, char *argv[])
{
CLogger logger; // single istance of logger
CResourceManager manager(argv[0]);
manager.AddLocation(COLOBOT_DEFAULT_DATADIR, false);
// Initialize static string arrays
InitializeRestext();
@ -103,9 +102,6 @@ int SDL_MAIN_FUNC(int argc, char *argv[])
return app->GetExitCode();
}
manager.SetSaveLocation(systemUtils->GetSaveDir());
manager.AddLocation(systemUtils->GetSaveDir(), true);
int code = 0;
if (! app->Create())
@ -124,7 +120,6 @@ int SDL_MAIN_FUNC(int argc, char *argv[])
delete app;
delete systemUtils;
//delete manager;
logger.Info("Exiting with code %d\n", code);
return code;

View File

@ -192,6 +192,16 @@ float CSystemUtils::TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *afte
return result;
}
std::string CSystemUtils::GetDataPath()
{
return COLOBOT_DEFAULT_DATADIR;
}
std::string CSystemUtils::GetLangPath()
{
return COLOBOT_I18N_DIR;
}
std::string CSystemUtils::GetSaveDir()
{
return std::string("save");

View File

@ -130,6 +130,12 @@ public:
/** The difference is \a after - \a before. */
virtual long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after) = 0;
//! Returns the data path (containing textures, levels, helpfiles, etc)
virtual std::string GetDataPath();
//! Returns the translations path
virtual std::string GetLangPath();
//! Returns the save dir location
virtual std::string GetSaveDir();
};

View File

@ -87,10 +87,19 @@ void CSystemUtilsMacOSX::Init()
m_dataPath += "/Contents/Resources";
}
std::string CSystemUtilsMacOSX::GetDataPath()
{
return m_dataPath;
}
std::string CSystemUtilsMacOSX::GetLangPath()
{
return m_dataPath + "/i18n";
}
std::string CSystemUtilsMacOSX::GetSaveDir()
{
std::string savegameDir = m_ASPath;
boost::filesystem::create_directories(savegameDir.c_str());
GetLogger()->Trace("Saved game files are going to %s\n", savegameDir.c_str());
return savegameDir;

View File

@ -28,6 +28,8 @@ class CSystemUtilsMacOSX : public CSystemUtilsOther
public:
virtual void Init() override;
virtual std::string GetDataPath() override;
virtual std::string GetLangPath() override;
virtual std::string GetSaveDir() override;
private:
std::string m_ASPath;

View File

@ -86,12 +86,6 @@ bool CResourceManager::SetSaveLocation(const std::string &location)
}
std::string CResourceManager::GetLanguageLocation()
{
return COLOBOT_I18N_DIR;
}
SDL_RWops* CResourceManager::GetSDLFileHandler(const std::string &filename)
{
SDL_RWops *handler = SDL_AllocRW();

View File

@ -30,7 +30,6 @@ public:
static bool AddLocation(const std::string &location, bool prepend = true);
static bool RemoveLocation(const std::string &location);
static bool SetSaveLocation(const std::string &location);
static std::string GetLanguageLocation();
static SDL_RWops* GetSDLFileHandler(const std::string &filename);
static CSNDFile* GetSNDFileHandler(const std::string &filename);
static bool Exists(const std::string &filename);

View File

@ -52,18 +52,17 @@ CSNDFile::~CSNDFile()
if (m_file)
{
PHYSFS_close(m_file);
}
if (m_snd_file)
{
sf_close(m_snd_file);
}
}
}
bool CSNDFile::IsOpen()
{
return m_snd_file;
return m_file && m_snd_file;
}

View File

@ -867,7 +867,13 @@ CachedFont* CText::GetOrOpenFont(FontType font, float size)
}
m_lastCachedFont = new CachedFont();
m_lastCachedFont->font = TTF_OpenFontRW(CResourceManager::GetSDLFileHandler(mf->fileName), 1, pointSize);
SDL_RWops* file = CResourceManager::GetSDLFileHandler(mf->fileName);
if(file == nullptr)
{
m_error = std::string("Unable to open file");
return nullptr;
}
m_lastCachedFont->font = TTF_OpenFontRW(file, 1, pointSize);
if (m_lastCachedFont->font == nullptr)
m_error = std::string("TTF_OpenFont error ") + std::string(TTF_GetError());