From c0b230b742f946d7148d8cdc7e521b9ed8f3ebdc Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Mon, 7 Jul 2025 16:32:42 +0200 Subject: [PATCH] Remove global config on Windows --- apps/openmw/engine.cpp | 11 +++-- components/files/androidpath.cpp | 4 +- components/files/androidpath.hpp | 5 +- components/files/configurationmanager.cpp | 59 +++++++++++++---------- components/files/configurationmanager.hpp | 9 +--- components/files/fixedpath.hpp | 8 +-- components/files/linuxpath.cpp | 4 +- components/files/linuxpath.hpp | 5 +- components/files/macospath.cpp | 4 +- components/files/macospath.hpp | 5 +- components/files/qtconfigpath.hpp | 10 ---- components/files/windowspath.cpp | 18 ++----- components/files/windowspath.hpp | 7 +-- 13 files changed, 67 insertions(+), 82 deletions(-) diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index 244c458f46..b1f2457935 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -781,7 +781,6 @@ void OMW::Engine::prepareEngine() const auto userdefault = mCfgMgr.getUserConfigPath() / "gamecontrollerdb.txt"; const auto localdefault = mCfgMgr.getLocalPath() / "gamecontrollerdb.txt"; - const auto globaldefault = mCfgMgr.getGlobalPath() / "gamecontrollerdb.txt"; std::filesystem::path userGameControllerdb; if (std::filesystem::exists(userdefault)) @@ -790,9 +789,13 @@ void OMW::Engine::prepareEngine() std::filesystem::path gameControllerdb; if (std::filesystem::exists(localdefault)) gameControllerdb = localdefault; - else if (std::filesystem::exists(globaldefault)) - gameControllerdb = globaldefault; - // else if it doesn't exist, pass in an empty string + else if (mCfgMgr.getGlobalPath()) + { + const auto globaldefault = *mCfgMgr.getGlobalPath() / "gamecontrollerdb.txt"; + if (std::filesystem::exists(globaldefault)) + gameControllerdb = globaldefault; + } + // else if it doesn't exist, pass in an empty path // gui needs our shaders path before everything else mResourceSystem->getSceneManager()->setShaderPath(mResDir / "shaders"); diff --git a/components/files/androidpath.cpp b/components/files/androidpath.cpp index 0a6cc161f3..a1cbd99fe8 100644 --- a/components/files/androidpath.cpp +++ b/components/files/androidpath.cpp @@ -49,7 +49,7 @@ namespace Files // /data/data/com.libopenmw.openmw/files/config // (note the addition of "files") - std::filesystem::path AndroidPath::getGlobalConfigPath() const + std::optional AndroidPath::getGlobalConfigPath() const { return std::filesystem::path(g_path_global) / "files" / "config"; } @@ -61,7 +61,7 @@ namespace Files // /sdcard/Android/data/com.libopenmw.openmw // (so that the data is at /sdcard/Android/data/com.libopenmw.openmw/data) - std::filesystem::path AndroidPath::getGlobalDataPath() const + std::optional AndroidPath::getGlobalDataPath() const { return std::filesystem::path(g_path_user); } diff --git a/components/files/androidpath.hpp b/components/files/androidpath.hpp index 5a10efe798..d1ac1289be 100644 --- a/components/files/androidpath.hpp +++ b/components/files/androidpath.hpp @@ -4,6 +4,7 @@ #if defined(__ANDROID__) #include +#include /** * \namespace Files */ @@ -25,7 +26,7 @@ namespace Files /** * \brief Return path to the global (system) directory where config files can be placed. */ - std::filesystem::path getGlobalConfigPath() const; + std::optional getGlobalConfigPath() const; /** * \brief Return path to the runtime configuration directory which is the @@ -36,7 +37,7 @@ namespace Files /** * \brief Return path to the global (system) directory where game files can be placed. */ - std::filesystem::path getGlobalDataPath() const; + std::optional getGlobalDataPath() const; /** * \brief diff --git a/components/files/configurationmanager.cpp b/components/files/configurationmanager.cpp index 7b4cbac864..f13a2bc748 100644 --- a/components/files/configurationmanager.cpp +++ b/components/files/configurationmanager.cpp @@ -18,37 +18,43 @@ namespace Files namespace bpo = boost::program_options; + namespace + { #if defined(_WIN32) || defined(__WINDOWS__) - static const char* const applicationName = "OpenMW"; + constexpr auto applicationName = "OpenMW"; #else - static const char* const applicationName = "openmw"; + constexpr auto applicationName = "openmw"; #endif - static constexpr auto localToken = u8"?local?"; - static constexpr auto userConfigToken = u8"?userconfig?"; - static constexpr auto userDataToken = u8"?userdata?"; - static constexpr auto globalToken = u8"?global?"; + constexpr std::u8string_view localToken = u8"?local?"; + constexpr std::u8string_view userConfigToken = u8"?userconfig?"; + constexpr std::u8string_view userDataToken = u8"?userdata?"; + constexpr std::u8string_view globalToken = u8"?global?"; + + const std::filesystem::path* getTokenPath(std::u8string_view token, const Files::FixedPath<>& fixedPath) + { + if (token == localToken) + return &fixedPath.getLocalPath(); + else if (token == userConfigToken) + return &fixedPath.getUserConfigPath(); + else if (token == userDataToken) + return &fixedPath.getUserDataPath(); + else if (token == globalToken && fixedPath.getGlobalDataPath()) + return &*fixedPath.getGlobalDataPath(); + return nullptr; + } + } ConfigurationManager::ConfigurationManager(bool silent) : mFixedPath(applicationName) , mSilent(silent) { - setupTokensMapping(); - // Initialize with fixed paths, will be overridden in `readConfiguration`. mUserDataPath = mFixedPath.getUserDataPath(); mScreenshotPath = mFixedPath.getUserDataPath() / "screenshots"; } - ConfigurationManager::~ConfigurationManager() {} - - void ConfigurationManager::setupTokensMapping() - { - mTokensMapping.insert(std::make_pair(localToken, &FixedPath<>::getLocalPath)); - mTokensMapping.insert(std::make_pair(userConfigToken, &FixedPath<>::getUserConfigPath)); - mTokensMapping.insert(std::make_pair(userDataToken, &FixedPath<>::getUserDataPath)); - mTokensMapping.insert(std::make_pair(globalToken, &FixedPath<>::getGlobalDataPath)); - } + ConfigurationManager::~ConfigurationManager() = default; static bool hasReplaceConfig(const bpo::variables_map& variables) { @@ -74,10 +80,10 @@ namespace Files std::optional config = loadConfig(mFixedPath.getLocalPath(), description); if (config) mActiveConfigPaths.push_back(mFixedPath.getLocalPath()); - else + else if (mFixedPath.getGlobalConfigPath()) { - mActiveConfigPaths.push_back(mFixedPath.getGlobalConfigPath()); - config = loadConfig(mFixedPath.getGlobalConfigPath(), description); + mActiveConfigPaths.push_back(*mFixedPath.getGlobalConfigPath()); + config = loadConfig(*mFixedPath.getGlobalConfigPath(), description); } if (!config) { @@ -305,15 +311,16 @@ namespace Files const auto pos = str.find('?', 1); if (pos != std::u8string::npos && pos != 0) { - auto tokenIt = mTokensMapping.find(str.substr(0, pos + 1)); - if (tokenIt != mTokensMapping.end()) + std::u8string_view view(str); + auto token = view.substr(0, pos + 1); + if (const std::filesystem::path* tokenPath = getTokenPath(token, mFixedPath)) { - auto tempPath(((mFixedPath).*(tokenIt->second))()); - if (pos < str.length() - 1) + auto tempPath(*tokenPath); + if (pos < view.length() - 1) { // There is something after the token, so we should // append it to the path - tempPath /= str.substr(pos + 1, str.length() - pos); + tempPath /= view.substr(pos + 1, view.length() - pos); } path = std::move(tempPath); @@ -391,7 +398,7 @@ namespace Files return std::nullopt; } - const std::filesystem::path& ConfigurationManager::getGlobalPath() const + const std::optional& ConfigurationManager::getGlobalPath() const { return mFixedPath.getGlobalConfigPath(); } diff --git a/components/files/configurationmanager.hpp b/components/files/configurationmanager.hpp index 2e10f21252..54bdbac3f4 100644 --- a/components/files/configurationmanager.hpp +++ b/components/files/configurationmanager.hpp @@ -44,7 +44,7 @@ namespace Files boost::program_options::variables_map& variables, const std::filesystem::path& basePath) const; /**< Fixed paths */ - const std::filesystem::path& getGlobalPath() const; + const std::optional& getGlobalPath() const; const std::filesystem::path& getLocalPath() const; const std::filesystem::path& getUserConfigPath() const; @@ -62,17 +62,12 @@ namespace Files private: typedef Files::FixedPath<> FixedPathType; - typedef const std::filesystem::path& (FixedPathType::*path_type_f)() const; - typedef std::map TokensMappingContainer; - std::optional loadConfig( const std::filesystem::path& path, const boost::program_options::options_description& description) const; void addExtraConfigDirs( std::stack& dirs, const boost::program_options::variables_map& variables) const; - void setupTokensMapping(); - std::vector mActiveConfigPaths; FixedPathType mFixedPath; @@ -80,8 +75,6 @@ namespace Files std::filesystem::path mUserDataPath; std::filesystem::path mScreenshotPath; - TokensMappingContainer mTokensMapping; - bool mSilent; }; diff --git a/components/files/fixedpath.hpp b/components/files/fixedpath.hpp index 76e8574fa7..7eef1486ea 100644 --- a/components/files/fixedpath.hpp +++ b/components/files/fixedpath.hpp @@ -80,7 +80,7 @@ namespace Files /** * \brief Return path pointing to the global (system) configuration directory. */ - const std::filesystem::path& getGlobalConfigPath() const { return mGlobalConfigPath; } + const std::optional& getGlobalConfigPath() const { return mGlobalConfigPath; } /** * \brief Return path pointing to the directory where application was started. @@ -89,7 +89,7 @@ namespace Files const std::filesystem::path& getInstallPath() const { return mInstallPath; } - const std::filesystem::path& getGlobalDataPath() const { return mGlobalDataPath; } + const std::optional& getGlobalDataPath() const { return mGlobalDataPath; } const std::filesystem::path& getCachePath() const { return mCachePath; } @@ -98,10 +98,10 @@ namespace Files std::filesystem::path mUserConfigPath; /**< User path */ std::filesystem::path mUserDataPath; - std::filesystem::path mGlobalConfigPath; /**< Global path */ + std::optional mGlobalConfigPath; /**< Global path */ std::filesystem::path mLocalPath; /**< It is the same directory where application was run */ - std::filesystem::path mGlobalDataPath; /**< Global application data path */ + std::optional mGlobalDataPath; /**< Global application data path */ std::filesystem::path mCachePath; diff --git a/components/files/linuxpath.cpp b/components/files/linuxpath.cpp index 2e74948fff..3a22f505d9 100644 --- a/components/files/linuxpath.cpp +++ b/components/files/linuxpath.cpp @@ -67,7 +67,7 @@ namespace Files return getEnv("XDG_CACHE_HOME", getUserHome() / ".cache") / mName; } - std::filesystem::path LinuxPath::getGlobalConfigPath() const + std::optional LinuxPath::getGlobalConfigPath() const { std::filesystem::path globalPath(GLOBAL_CONFIG_PATH); return globalPath / mName; @@ -94,7 +94,7 @@ namespace Files return localPath; } - std::filesystem::path LinuxPath::getGlobalDataPath() const + std::optional LinuxPath::getGlobalDataPath() const { std::filesystem::path globalDataPath(GLOBAL_DATA_PATH); return globalDataPath / mName; diff --git a/components/files/linuxpath.hpp b/components/files/linuxpath.hpp index 36bbb47bf2..b1ca290e4d 100644 --- a/components/files/linuxpath.hpp +++ b/components/files/linuxpath.hpp @@ -4,6 +4,7 @@ #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) #include +#include /** * \namespace Files @@ -28,7 +29,7 @@ namespace Files /** * \brief Return path to the global (system) directory where config files can be placed. */ - std::filesystem::path getGlobalConfigPath() const; + std::optional getGlobalConfigPath() const; /** * \brief Return path to the runtime configuration directory which is the @@ -39,7 +40,7 @@ namespace Files /** * \brief Return path to the global (system) directory where game files can be placed. */ - std::filesystem::path getGlobalDataPath() const; + std::optional getGlobalDataPath() const; /** * \brief diff --git a/components/files/macospath.cpp b/components/files/macospath.cpp index 191f3b15a6..855a94f8bc 100644 --- a/components/files/macospath.cpp +++ b/components/files/macospath.cpp @@ -82,7 +82,7 @@ namespace Files return userPath / mName; } - std::filesystem::path MacOsPath::getGlobalConfigPath() const + std::optional MacOsPath::getGlobalConfigPath() const { std::filesystem::path globalPath("/Library/Preferences/"); return globalPath / mName; @@ -100,7 +100,7 @@ namespace Files return getBinaryPath().parent_path().parent_path() / "Resources"; } - std::filesystem::path MacOsPath::getGlobalDataPath() const + std::optional MacOsPath::getGlobalDataPath() const { std::filesystem::path globalDataPath("/Library/Application Support/"); return globalDataPath / mName; diff --git a/components/files/macospath.hpp b/components/files/macospath.hpp index c1eeaf1dab..ebe8584fb8 100644 --- a/components/files/macospath.hpp +++ b/components/files/macospath.hpp @@ -4,6 +4,7 @@ #if defined(macintosh) || defined(Macintosh) || defined(__APPLE__) || defined(__MACH__) #include +#include /** * \namespace Files @@ -32,7 +33,7 @@ namespace Files * * \return std::filesystem::path */ - std::filesystem::path getGlobalConfigPath() const; + std::optional getGlobalConfigPath() const; /** * \brief Return path to the runtime directory which is the @@ -54,7 +55,7 @@ namespace Files * * \return std::filesystem::path */ - std::filesystem::path getGlobalDataPath() const; + std::optional getGlobalDataPath() const; std::filesystem::path getInstallPath() const; diff --git a/components/files/qtconfigpath.hpp b/components/files/qtconfigpath.hpp index 16e0499cd5..a2154ce110 100644 --- a/components/files/qtconfigpath.hpp +++ b/components/files/qtconfigpath.hpp @@ -8,21 +8,11 @@ namespace Files { - inline QString getLocalConfigPathQString(const Files::ConfigurationManager& cfgMgr) - { - return Files::pathToQString(cfgMgr.getLocalPath() / openmwCfgFile); - } - inline QString getUserConfigPathQString(const Files::ConfigurationManager& cfgMgr) { return Files::pathToQString(cfgMgr.getUserConfigPath() / openmwCfgFile); } - inline QString getGlobalConfigPathQString(const Files::ConfigurationManager& cfgMgr) - { - return Files::pathToQString(cfgMgr.getGlobalPath() / openmwCfgFile); - } - inline QStringList getActiveConfigPathsQString(const Files::ConfigurationManager& cfgMgr) { const auto& activePaths = cfgMgr.getActiveConfigPaths(); diff --git a/components/files/windowspath.cpp b/components/files/windowspath.cpp index 77faa23131..2c4215fffb 100644 --- a/components/files/windowspath.cpp +++ b/components/files/windowspath.cpp @@ -50,23 +50,11 @@ namespace Files return getUserConfigPath(); } - std::filesystem::path WindowsPath::getGlobalConfigPath() const + std::optional WindowsPath::getGlobalConfigPath() const { // The concept of a global config path is absurd on Windows. // Always use local config instead. - // The virtual base class requires that we provide this, though. - std::filesystem::path globalPath = std::filesystem::current_path(); - - PWSTR cString; - HRESULT result = SHGetKnownFolderPath(FOLDERID_ProgramFiles, 0, nullptr, &cString); - if (SUCCEEDED(result)) - globalPath = std::filesystem::path(cString); - else - Log(Debug::Error) << "Error " << result << " when getting Program Files path"; - - CoTaskMemFree(cString); - - return globalPath / mName; + return {}; } std::filesystem::path WindowsPath::getLocalPath() const @@ -84,7 +72,7 @@ namespace Files return localPath; } - std::filesystem::path WindowsPath::getGlobalDataPath() const + std::optional WindowsPath::getGlobalDataPath() const { return getGlobalConfigPath(); } diff --git a/components/files/windowspath.hpp b/components/files/windowspath.hpp index 380e831b20..624d1c071d 100644 --- a/components/files/windowspath.hpp +++ b/components/files/windowspath.hpp @@ -4,6 +4,7 @@ #if defined(_WIN32) || defined(__WINDOWS__) #include +#include /** * \namespace Files @@ -34,11 +35,11 @@ namespace Files std::filesystem::path getUserDataPath() const; /** - * \brief Returns "X:\Program Files\" + * \brief Returns nothing * * \return std::filesystem::path */ - std::filesystem::path getGlobalConfigPath() const; + std::optional getGlobalConfigPath() const; /** * \brief Return local path which is a location where @@ -60,7 +61,7 @@ namespace Files * * \return std::filesystem::path */ - std::filesystem::path getGlobalDataPath() const; + std::optional getGlobalDataPath() const; /** * \brief Gets the path of the installed Morrowind version if there is one.