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

@ -146,6 +146,10 @@ CApplication::CApplication()
m_kmodState = 0; m_kmodState = 0;
m_mouseButtonsState = 0; m_mouseButtonsState = 0;
m_trackedKeys = 0; m_trackedKeys = 0;
m_dataPath = GetSystemUtils()->GetDataPath();
m_langPath = GetSystemUtils()->GetLangPath();
m_savePath = GetSystemUtils()->GetSaveDir();
m_runSceneName = ""; m_runSceneName = "";
m_runSceneRank = 0; m_runSceneRank = 0;
@ -208,8 +212,10 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
OPT_LOGLEVEL, OPT_LOGLEVEL,
OPT_LANGUAGE, OPT_LANGUAGE,
OPT_LANGDIR, OPT_LANGDIR,
OPT_VBO, OPT_DATADIR,
OPT_MOD OPT_SAVEDIR,
OPT_MOD,
OPT_VBO
}; };
option options[] = option options[] =
@ -221,8 +227,10 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
{ "loglevel", required_argument, nullptr, OPT_LOGLEVEL }, { "loglevel", required_argument, nullptr, OPT_LOGLEVEL },
{ "language", required_argument, nullptr, OPT_LANGUAGE }, { "language", required_argument, nullptr, OPT_LANGUAGE },
{ "langdir", required_argument, nullptr, OPT_LANGDIR }, { "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 }, { "mod", required_argument, nullptr, OPT_MOD },
{ "vbo", required_argument, nullptr, OPT_VBO },
{ nullptr, 0, nullptr, 0} { 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(" -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(" -language lang set language (one of: en, de, fr, pl, ru)\n");
GetLogger()->Message(" -langdir path set custom language directory path\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(" -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; return PARSE_ARGS_HELP;
} }
case OPT_DEBUG: case OPT_DEBUG:
@ -341,11 +351,28 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
break; 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: case OPT_MOD:
{ {
GetLogger()->Info("Loading mod from \"%s\"\n", optarg); GetLogger()->Info("Loading mod: '%s'\n", optarg);
CResourceManager::AddLocation(optarg, true); CResourceManager::AddLocation(optarg, true);
break; break;
} }
default: default:
@ -362,6 +389,22 @@ bool CApplication::Create()
bool defaultValues = false; bool defaultValues = false;
GetLogger()->Info("Creating CApplication\n"); 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()) if (!GetProfile().InitCurrentDirectory())
{ {
@ -369,17 +412,6 @@ bool CApplication::Create()
defaultValues = true; 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)) { if (GetProfile().GetLocalProfileString("Language", "Lang", path)) {
Language language; Language language;
if (ParseLanguage(path, language)) { if (ParseLanguage(path, language)) {
@ -1696,7 +1728,7 @@ void CApplication::SetLanguage(Language language)
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
bindtextdomain("colobot", CResourceManager::GetLanguageLocation().c_str()); bindtextdomain("colobot", m_langPath.c_str());
bind_textdomain_codeset("colobot", "UTF-8"); bind_textdomain_codeset("colobot", "UTF-8");
textdomain("colobot"); textdomain("colobot");

View File

@ -464,6 +464,15 @@ protected:
std::vector<int> m_joyAxeState; std::vector<int> m_joyAxeState;
//! Current state of joystick buttons; may be updated from another thread //! Current state of joystick buttons; may be updated from another thread
std::vector<bool> m_joyButtonState; 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 //! Scene to run on startup

View File

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

View File

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

View File

@ -130,6 +130,12 @@ public:
/** The difference is \a after - \a before. */ /** The difference is \a after - \a before. */
virtual long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after) = 0; 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 //! Returns the save dir location
virtual std::string GetSaveDir(); virtual std::string GetSaveDir();
}; };

View File

@ -87,10 +87,19 @@ void CSystemUtilsMacOSX::Init()
m_dataPath += "/Contents/Resources"; 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 CSystemUtilsMacOSX::GetSaveDir()
{ {
std::string savegameDir = m_ASPath; 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()); GetLogger()->Trace("Saved game files are going to %s\n", savegameDir.c_str());
return savegameDir; return savegameDir;

View File

@ -28,6 +28,8 @@ class CSystemUtilsMacOSX : public CSystemUtilsOther
public: public:
virtual void Init() override; virtual void Init() override;
virtual std::string GetDataPath() override;
virtual std::string GetLangPath() override;
virtual std::string GetSaveDir() override; virtual std::string GetSaveDir() override;
private: private:
std::string m_ASPath; 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* CResourceManager::GetSDLFileHandler(const std::string &filename)
{ {
SDL_RWops *handler = SDL_AllocRW(); SDL_RWops *handler = SDL_AllocRW();

View File

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

View File

@ -52,18 +52,17 @@ CSNDFile::~CSNDFile()
if (m_file) if (m_file)
{ {
PHYSFS_close(m_file); PHYSFS_close(m_file);
} if (m_snd_file)
{
if (m_snd_file) sf_close(m_snd_file);
{ }
sf_close(m_snd_file);
} }
} }
bool CSNDFile::IsOpen() 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 = 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) if (m_lastCachedFont->font == nullptr)
m_error = std::string("TTF_OpenFont error ") + std::string(TTF_GetError()); m_error = std::string("TTF_OpenFont error ") + std::string(TTF_GetError());