From d4b2f23c4aae83b83d3bda66ebf4eee2339e3ac5 Mon Sep 17 00:00:00 2001 From: MrSimbax Date: Fri, 3 Apr 2020 20:15:24 +0200 Subject: [PATCH] Add handling of env variables --- src/app/app.cpp | 29 +++++++++++++++++++++++++++- src/app/app.h | 4 +++- src/app/main.cpp | 2 ++ src/app/pathman.cpp | 6 +++++- src/common/system/system.cpp | 5 +++++ src/common/system/system.h | 3 +++ src/common/system/system_linux.cpp | 12 ++++++++++++ src/common/system/system_linux.h | 2 ++ src/common/system/system_macosx.cpp | 6 ++++++ src/common/system/system_macosx.h | 2 ++ src/common/system/system_windows.cpp | 16 +++++++++++++++ src/common/system/system_windows.h | 2 ++ 12 files changed, 86 insertions(+), 3 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index 6f864572..94b6319e 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -220,6 +220,30 @@ CSoundInterface* CApplication::GetSound() return m_sound.get(); } +void CApplication::LoadEnvironmentVariables() +{ + auto dataDir = m_systemUtils->GetEnvVar("COLOBOT_DATA_DIR"); + if (!dataDir.empty()) + { + m_pathManager->SetDataPath(dataDir); + GetLogger()->Info("Using data dir (based on environment variable): '%s'\n", dataDir.c_str()); + } + + auto langDir = m_systemUtils->GetEnvVar("COLOBOT_LANG_DIR"); + if (!langDir.empty()) + { + m_pathManager->SetLangPath(langDir); + GetLogger()->Info("Using lang dir (based on environment variable): '%s'\n", langDir.c_str()); + } + + auto saveDir = m_systemUtils->GetEnvVar("COLOBOT_SAVE_DIR"); + if (!saveDir.empty()) + { + m_pathManager->SetSavePath(saveDir); + GetLogger()->Info("Using save dir (based on environment variable): '%s'\n", saveDir.c_str()); + } +} + ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[]) { enum OptionType @@ -286,15 +310,18 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[]) GetLogger()->Message("\n"); GetLogger()->Message("%s\n", COLOBOT_FULLNAME); GetLogger()->Message("\n"); - GetLogger()->Message("List of available options:\n"); + GetLogger()->Message("List of available options and environment variables:\n"); GetLogger()->Message(" -help this help\n"); GetLogger()->Message(" -debug modes enable debug modes (more info printed in logs; see code for reference of modes)\n"); GetLogger()->Message(" -runscene sceneNNN run given scene on start\n"); GetLogger()->Message(" -scenetest win every mission right after it's loaded\n"); GetLogger()->Message(" -loglevel level set log level to level (one of: trace, debug, info, warn, error, none)\n"); GetLogger()->Message(" -langdir path set custom language directory path\n"); + GetLogger()->Message(" environment variable: COLOBOT_LANG_DIR\n"); GetLogger()->Message(" -datadir path set custom data directory path\n"); + GetLogger()->Message(" environment variable: COLOBOT_DATA_DIR\n"); GetLogger()->Message(" -savedir path set custom save directory path (must be writable)\n"); + GetLogger()->Message(" environment variable: COLOBOT_SAVE_DIR\n"); GetLogger()->Message(" -mod path load datadir mod from given path\n"); GetLogger()->Message(" -resolution WxH set resolution\n"); GetLogger()->Message(" -headless headless mode - disables graphics, sound and user interaction\n"); diff --git a/src/app/app.h b/src/app/app.h index ccae3a5c..0c7f6388 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -164,7 +164,9 @@ public: CSoundInterface* GetSound(); public: - //! Parses commandline arguments + //! Loads some data from environment variables + void LoadEnvironmentVariables(); + //! Parses commandline arguments (they take priority) ParseArgsStatus ParseArguments(int argc, char *argv[]); //! Initializes the application bool Create(); diff --git a/src/app/main.cpp b/src/app/main.cpp index 9fdb89a3..af0fc99a 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -172,6 +172,8 @@ int main(int argc, char *argv[]) int code = 0; CApplication app(systemUtils.get()); // single instance of the application + app.LoadEnvironmentVariables(); + ParseArgsStatus status = app.ParseArguments(argc, argv); if (status == PARSE_ARGS_FAIL) { diff --git a/src/app/pathman.cpp b/src/app/pathman.cpp index 60715ac1..954fcc4e 100644 --- a/src/app/pathman.cpp +++ b/src/app/pathman.cpp @@ -41,7 +41,7 @@ CPathManager::CPathManager(CSystemUtils* systemUtils) : m_dataPath(systemUtils->GetDataPath()) , m_langPath(systemUtils->GetLangPath()) , m_savePath(systemUtils->GetSaveDir()) - , m_modAutoloadDir{ m_dataPath + "/mods", m_savePath + "/mods" } + , m_modAutoloadDir{} , m_mods{} { } @@ -130,6 +130,10 @@ void CPathManager::InitPaths() { GetLogger()->Info("Data path: %s\n", m_dataPath.c_str()); GetLogger()->Info("Save path: %s\n", m_savePath.c_str()); + + m_modAutoloadDir.push_back(m_dataPath + "/mods"); + m_modAutoloadDir.push_back(m_savePath + "/mods"); + if (!m_modAutoloadDir.empty()) { GetLogger()->Info("Mod autoload dirs:\n"); diff --git a/src/common/system/system.cpp b/src/common/system/system.cpp index 0b8001a1..b22d0771 100644 --- a/src/common/system/system.cpp +++ b/src/common/system/system.cpp @@ -210,3 +210,8 @@ std::string CSystemUtils::GetSaveDir() { return "./saves"; } + +std::string CSystemUtils::GetEnvVar(const std::string& str) +{ + return std::string(); +} diff --git a/src/common/system/system.h b/src/common/system/system.h index 7912f27a..431ddd41 100644 --- a/src/common/system/system.h +++ b/src/common/system/system.h @@ -139,6 +139,9 @@ public: //! Returns the save dir location virtual std::string GetSaveDir(); + //! Returns environment variable + virtual std::string GetEnvVar(const std::string &str); + //! Sleep for given amount of microseconds virtual void Usleep(int usecs) = 0; diff --git a/src/common/system/system_linux.cpp b/src/common/system/system_linux.cpp index 9bfe431a..b71befe5 100644 --- a/src/common/system/system_linux.cpp +++ b/src/common/system/system_linux.cpp @@ -126,6 +126,18 @@ std::string CSystemUtilsLinux::GetSaveDir() #endif } +std::string CSystemUtilsLinux::GetEnvVar(const std::string& name) +{ + char* envVar = getenv(name.c_str()); + if (envVar != nullptr) + { + + GetLogger()->Trace("Detected environment variable %s = %s\n", name.c_str(), envVar); + return std::string(envVar); + } + return ""; +} + void CSystemUtilsLinux::Usleep(int usec) { usleep(usec); diff --git a/src/common/system/system_linux.h b/src/common/system/system_linux.h index f1576f31..c4de2c7f 100644 --- a/src/common/system/system_linux.h +++ b/src/common/system/system_linux.h @@ -45,6 +45,8 @@ public: std::string GetSaveDir() override; + std::string GetEnvVar(const std::string& name) override; + void Usleep(int usec) override; private: diff --git a/src/common/system/system_macosx.cpp b/src/common/system/system_macosx.cpp index 9ef7ce1d..8a537e11 100644 --- a/src/common/system/system_macosx.cpp +++ b/src/common/system/system_macosx.cpp @@ -113,6 +113,12 @@ std::string CSystemUtilsMacOSX::GetSaveDir() #endif } +std::string CSystemUtilsMacOSX::GetEnvVar(const std::string& str) +{ + // TODO: I have no Mac + return std::string(); +} + void CSystemUtilsMacOSX::Usleep(int usec) { usleep(usec); diff --git a/src/common/system/system_macosx.h b/src/common/system/system_macosx.h index 5b572ec4..708d26fc 100644 --- a/src/common/system/system_macosx.h +++ b/src/common/system/system_macosx.h @@ -36,6 +36,8 @@ public: std::string GetLangPath() override; std::string GetSaveDir() override; + std::string GetEnvVar(const std::string& str) override; + void Usleep(int usec) override; private: diff --git a/src/common/system/system_windows.cpp b/src/common/system/system_windows.cpp index a20bfcf1..5254db83 100644 --- a/src/common/system/system_windows.cpp +++ b/src/common/system/system_windows.cpp @@ -131,6 +131,22 @@ std::string CSystemUtilsWindows::GetSaveDir() #endif } +std::string CSystemUtilsWindows::GetEnvVar(const std::string& name) +{ + std::wstring wname(name.begin(), name.end()); + wchar_t* envVar = _wgetenv(wname.c_str()); + if (envVar == nullptr) + { + return ""; + } + else + { + std::string var = UTF8_Encode(std::wstring(envVar)); + GetLogger()->Trace("Detected environment variable %s = %s\n", name.c_str(), var.c_str()); + return var; + } +} + void CSystemUtilsWindows::Usleep(int usec) { LARGE_INTEGER ft; diff --git a/src/common/system/system_windows.h b/src/common/system/system_windows.h index 74f02455..05f3c910 100644 --- a/src/common/system/system_windows.h +++ b/src/common/system/system_windows.h @@ -43,6 +43,8 @@ public: std::string GetSaveDir() override; + std::string GetEnvVar(const std::string& name) override; + void Usleep(int usec) override; public: