From b4a753812e38f9e75253868b12c9e437605f7d26 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Sat, 26 Jul 2025 10:23:19 +0200 Subject: [PATCH] Use empty paths instead of optionals --- apps/openmw/engine.cpp | 4 +-- components/files/androidpath.cpp | 4 +-- components/files/androidpath.hpp | 5 ++- components/files/configurationmanager.cpp | 40 +++++++++-------------- components/files/configurationmanager.hpp | 2 +- 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/windowspath.cpp | 4 +-- components/files/windowspath.hpp | 7 ++-- 12 files changed, 40 insertions(+), 52 deletions(-) diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index b1f2457935..0ea8451774 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -789,9 +789,9 @@ void OMW::Engine::prepareEngine() std::filesystem::path gameControllerdb; if (std::filesystem::exists(localdefault)) gameControllerdb = localdefault; - else if (mCfgMgr.getGlobalPath()) + else if (!mCfgMgr.getGlobalPath().empty()) { - const auto globaldefault = *mCfgMgr.getGlobalPath() / "gamecontrollerdb.txt"; + const auto globaldefault = mCfgMgr.getGlobalPath() / "gamecontrollerdb.txt"; if (std::filesystem::exists(globaldefault)) gameControllerdb = globaldefault; } diff --git a/components/files/androidpath.cpp b/components/files/androidpath.cpp index a1cbd99fe8..0a6cc161f3 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::optional AndroidPath::getGlobalConfigPath() const + std::filesystem::path 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::optional AndroidPath::getGlobalDataPath() const + std::filesystem::path AndroidPath::getGlobalDataPath() const { return std::filesystem::path(g_path_user); } diff --git a/components/files/androidpath.hpp b/components/files/androidpath.hpp index d1ac1289be..5a10efe798 100644 --- a/components/files/androidpath.hpp +++ b/components/files/androidpath.hpp @@ -4,7 +4,6 @@ #if defined(__ANDROID__) #include -#include /** * \namespace Files */ @@ -26,7 +25,7 @@ namespace Files /** * \brief Return path to the global (system) directory where config files can be placed. */ - std::optional getGlobalConfigPath() const; + std::filesystem::path getGlobalConfigPath() const; /** * \brief Return path to the runtime configuration directory which is the @@ -37,7 +36,7 @@ namespace Files /** * \brief Return path to the global (system) directory where game files can be placed. */ - std::optional getGlobalDataPath() const; + std::filesystem::path getGlobalDataPath() const; /** * \brief diff --git a/components/files/configurationmanager.cpp b/components/files/configurationmanager.cpp index f13a2bc748..49fdd996a7 100644 --- a/components/files/configurationmanager.cpp +++ b/components/files/configurationmanager.cpp @@ -26,23 +26,13 @@ namespace Files constexpr auto applicationName = "openmw"; #endif - 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; - } + 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) @@ -80,10 +70,10 @@ namespace Files std::optional config = loadConfig(mFixedPath.getLocalPath(), description); if (config) mActiveConfigPaths.push_back(mFixedPath.getLocalPath()); - else if (mFixedPath.getGlobalConfigPath()) + else if (!mFixedPath.getGlobalConfigPath().empty()) { - mActiveConfigPaths.push_back(*mFixedPath.getGlobalConfigPath()); - config = loadConfig(*mFixedPath.getGlobalConfigPath(), description); + mActiveConfigPaths.push_back(mFixedPath.getGlobalConfigPath()); + config = loadConfig(mFixedPath.getGlobalConfigPath(), description); } if (!config) { @@ -313,10 +303,12 @@ namespace Files { std::u8string_view view(str); auto token = view.substr(0, pos + 1); - if (const std::filesystem::path* tokenPath = getTokenPath(token, mFixedPath)) + auto found = std::find_if( + sTokenMappings.begin(), sTokenMappings.end(), [&](const auto& item) { return item.first == token; }); + if (found != sTokenMappings.end()) { - auto tempPath(*tokenPath); - if (pos < view.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 @@ -398,7 +390,7 @@ namespace Files return std::nullopt; } - const std::optional& ConfigurationManager::getGlobalPath() const + const std::filesystem::path& ConfigurationManager::getGlobalPath() const { return mFixedPath.getGlobalConfigPath(); } diff --git a/components/files/configurationmanager.hpp b/components/files/configurationmanager.hpp index 54bdbac3f4..184c6ebb82 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::optional& getGlobalPath() const; + const std::filesystem::path& getGlobalPath() const; const std::filesystem::path& getLocalPath() const; const std::filesystem::path& getUserConfigPath() const; diff --git a/components/files/fixedpath.hpp b/components/files/fixedpath.hpp index 7eef1486ea..76e8574fa7 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::optional& getGlobalConfigPath() const { return mGlobalConfigPath; } + const std::filesystem::path& 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::optional& getGlobalDataPath() const { return mGlobalDataPath; } + const std::filesystem::path& 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::optional mGlobalConfigPath; /**< Global path */ + std::filesystem::path mGlobalConfigPath; /**< Global path */ std::filesystem::path mLocalPath; /**< It is the same directory where application was run */ - std::optional mGlobalDataPath; /**< Global application data path */ + std::filesystem::path mGlobalDataPath; /**< Global application data path */ std::filesystem::path mCachePath; diff --git a/components/files/linuxpath.cpp b/components/files/linuxpath.cpp index 3a22f505d9..2e74948fff 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::optional LinuxPath::getGlobalConfigPath() const + std::filesystem::path LinuxPath::getGlobalConfigPath() const { std::filesystem::path globalPath(GLOBAL_CONFIG_PATH); return globalPath / mName; @@ -94,7 +94,7 @@ namespace Files return localPath; } - std::optional LinuxPath::getGlobalDataPath() const + std::filesystem::path 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 b1ca290e4d..36bbb47bf2 100644 --- a/components/files/linuxpath.hpp +++ b/components/files/linuxpath.hpp @@ -4,7 +4,6 @@ #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) #include -#include /** * \namespace Files @@ -29,7 +28,7 @@ namespace Files /** * \brief Return path to the global (system) directory where config files can be placed. */ - std::optional getGlobalConfigPath() const; + std::filesystem::path getGlobalConfigPath() const; /** * \brief Return path to the runtime configuration directory which is the @@ -40,7 +39,7 @@ namespace Files /** * \brief Return path to the global (system) directory where game files can be placed. */ - std::optional getGlobalDataPath() const; + std::filesystem::path getGlobalDataPath() const; /** * \brief diff --git a/components/files/macospath.cpp b/components/files/macospath.cpp index 855a94f8bc..191f3b15a6 100644 --- a/components/files/macospath.cpp +++ b/components/files/macospath.cpp @@ -82,7 +82,7 @@ namespace Files return userPath / mName; } - std::optional MacOsPath::getGlobalConfigPath() const + std::filesystem::path 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::optional MacOsPath::getGlobalDataPath() const + std::filesystem::path 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 ebe8584fb8..c1eeaf1dab 100644 --- a/components/files/macospath.hpp +++ b/components/files/macospath.hpp @@ -4,7 +4,6 @@ #if defined(macintosh) || defined(Macintosh) || defined(__APPLE__) || defined(__MACH__) #include -#include /** * \namespace Files @@ -33,7 +32,7 @@ namespace Files * * \return std::filesystem::path */ - std::optional getGlobalConfigPath() const; + std::filesystem::path getGlobalConfigPath() const; /** * \brief Return path to the runtime directory which is the @@ -55,7 +54,7 @@ namespace Files * * \return std::filesystem::path */ - std::optional getGlobalDataPath() const; + std::filesystem::path getGlobalDataPath() const; std::filesystem::path getInstallPath() const; diff --git a/components/files/windowspath.cpp b/components/files/windowspath.cpp index 2c4215fffb..60ac5e265c 100644 --- a/components/files/windowspath.cpp +++ b/components/files/windowspath.cpp @@ -50,7 +50,7 @@ namespace Files return getUserConfigPath(); } - std::optional WindowsPath::getGlobalConfigPath() const + std::filesystem::path WindowsPath::getGlobalConfigPath() const { // The concept of a global config path is absurd on Windows. // Always use local config instead. @@ -72,7 +72,7 @@ namespace Files return localPath; } - std::optional WindowsPath::getGlobalDataPath() const + std::filesystem::path WindowsPath::getGlobalDataPath() const { return getGlobalConfigPath(); } diff --git a/components/files/windowspath.hpp b/components/files/windowspath.hpp index 624d1c071d..ed2bbdfc2e 100644 --- a/components/files/windowspath.hpp +++ b/components/files/windowspath.hpp @@ -4,7 +4,6 @@ #if defined(_WIN32) || defined(__WINDOWS__) #include -#include /** * \namespace Files @@ -35,11 +34,11 @@ namespace Files std::filesystem::path getUserDataPath() const; /** - * \brief Returns nothing + * \brief Returns an empty path * * \return std::filesystem::path */ - std::optional getGlobalConfigPath() const; + std::filesystem::path getGlobalConfigPath() const; /** * \brief Return local path which is a location where @@ -61,7 +60,7 @@ namespace Files * * \return std::filesystem::path */ - std::optional getGlobalDataPath() const; + std::filesystem::path getGlobalDataPath() const; /** * \brief Gets the path of the installed Morrowind version if there is one.