diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index 244c458f46..0ea8451774 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().empty()) + { + 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/configurationmanager.cpp b/components/files/configurationmanager.cpp index 7b4cbac864..49fdd996a7 100644 --- a/components/files/configurationmanager.cpp +++ b/components/files/configurationmanager.cpp @@ -18,37 +18,33 @@ 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?"; + using GetPath = const std::filesystem::path& (Files::FixedPath<>::*)() const; + constexpr std::array, 4> sTokenMappings = { + std::make_pair(u8"?local?", &FixedPath<>::getLocalPath), + std::make_pair(u8"?userconfig?", &FixedPath<>::getUserConfigPath), + std::make_pair(u8"?userdata?", &FixedPath<>::getUserDataPath), + std::make_pair(u8"?global?", &FixedPath<>::getGlobalDataPath), + }; + } 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,7 +70,7 @@ namespace Files std::optional config = loadConfig(mFixedPath.getLocalPath(), description); if (config) mActiveConfigPaths.push_back(mFixedPath.getLocalPath()); - else + else if (!mFixedPath.getGlobalConfigPath().empty()) { mActiveConfigPaths.push_back(mFixedPath.getGlobalConfigPath()); config = loadConfig(mFixedPath.getGlobalConfigPath(), description); @@ -305,15 +301,18 @@ 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); + auto found = std::find_if( + sTokenMappings.begin(), sTokenMappings.end(), [&](const auto& item) { return item.first == token; }); + if (found != sTokenMappings.end()) { - auto tempPath(((mFixedPath).*(tokenIt->second))()); - if (pos < str.length() - 1) + auto tempPath(((mFixedPath).*(found->second))()); + if (!tempPath.empty() && 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); diff --git a/components/files/configurationmanager.hpp b/components/files/configurationmanager.hpp index 2e10f21252..184c6ebb82 100644 --- a/components/files/configurationmanager.hpp +++ b/components/files/configurationmanager.hpp @@ -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/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..60ac5e265c 100644 --- a/components/files/windowspath.cpp +++ b/components/files/windowspath.cpp @@ -54,19 +54,7 @@ namespace Files { // 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 diff --git a/components/files/windowspath.hpp b/components/files/windowspath.hpp index 380e831b20..ed2bbdfc2e 100644 --- a/components/files/windowspath.hpp +++ b/components/files/windowspath.hpp @@ -34,7 +34,7 @@ namespace Files std::filesystem::path getUserDataPath() const; /** - * \brief Returns "X:\Program Files\" + * \brief Returns an empty path * * \return std::filesystem::path */