Use empty paths instead of optionals

This commit is contained in:
Evil Eye 2025-07-26 10:23:19 +02:00
parent c0b230b742
commit b4a753812e
12 changed files with 40 additions and 52 deletions

View File

@ -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;
}

View File

@ -49,7 +49,7 @@ namespace Files
// /data/data/com.libopenmw.openmw/files/config
// (note the addition of "files")
std::optional<std::filesystem::path> 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<std::filesystem::path> AndroidPath::getGlobalDataPath() const
std::filesystem::path AndroidPath::getGlobalDataPath() const
{
return std::filesystem::path(g_path_user);
}

View File

@ -4,7 +4,6 @@
#if defined(__ANDROID__)
#include <filesystem>
#include <optional>
/**
* \namespace Files
*/
@ -26,7 +25,7 @@ namespace Files
/**
* \brief Return path to the global (system) directory where config files can be placed.
*/
std::optional<std::filesystem::path> 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<std::filesystem::path> getGlobalDataPath() const;
std::filesystem::path getGlobalDataPath() const;
/**
* \brief

View File

@ -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<std::pair<std::u8string_view, GetPath>, 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<bpo::variables_map> 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<std::filesystem::path>& ConfigurationManager::getGlobalPath() const
const std::filesystem::path& ConfigurationManager::getGlobalPath() const
{
return mFixedPath.getGlobalConfigPath();
}

View File

@ -44,7 +44,7 @@ namespace Files
boost::program_options::variables_map& variables, const std::filesystem::path& basePath) const;
/**< Fixed paths */
const std::optional<std::filesystem::path>& getGlobalPath() const;
const std::filesystem::path& getGlobalPath() const;
const std::filesystem::path& getLocalPath() const;
const std::filesystem::path& getUserConfigPath() const;

View File

@ -80,7 +80,7 @@ namespace Files
/**
* \brief Return path pointing to the global (system) configuration directory.
*/
const std::optional<std::filesystem::path>& 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<std::filesystem::path>& 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<std::filesystem::path> mGlobalConfigPath; /**< Global path */
std::filesystem::path mGlobalConfigPath; /**< Global path */
std::filesystem::path mLocalPath; /**< It is the same directory where application was run */
std::optional<std::filesystem::path> mGlobalDataPath; /**< Global application data path */
std::filesystem::path mGlobalDataPath; /**< Global application data path */
std::filesystem::path mCachePath;

View File

@ -67,7 +67,7 @@ namespace Files
return getEnv("XDG_CACHE_HOME", getUserHome() / ".cache") / mName;
}
std::optional<std::filesystem::path> 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<std::filesystem::path> LinuxPath::getGlobalDataPath() const
std::filesystem::path LinuxPath::getGlobalDataPath() const
{
std::filesystem::path globalDataPath(GLOBAL_DATA_PATH);
return globalDataPath / mName;

View File

@ -4,7 +4,6 @@
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
#include <filesystem>
#include <optional>
/**
* \namespace Files
@ -29,7 +28,7 @@ namespace Files
/**
* \brief Return path to the global (system) directory where config files can be placed.
*/
std::optional<std::filesystem::path> 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<std::filesystem::path> getGlobalDataPath() const;
std::filesystem::path getGlobalDataPath() const;
/**
* \brief

View File

@ -82,7 +82,7 @@ namespace Files
return userPath / mName;
}
std::optional<std::filesystem::path> 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<std::filesystem::path> MacOsPath::getGlobalDataPath() const
std::filesystem::path MacOsPath::getGlobalDataPath() const
{
std::filesystem::path globalDataPath("/Library/Application Support/");
return globalDataPath / mName;

View File

@ -4,7 +4,6 @@
#if defined(macintosh) || defined(Macintosh) || defined(__APPLE__) || defined(__MACH__)
#include <filesystem>
#include <optional>
/**
* \namespace Files
@ -33,7 +32,7 @@ namespace Files
*
* \return std::filesystem::path
*/
std::optional<std::filesystem::path> 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<std::filesystem::path> getGlobalDataPath() const;
std::filesystem::path getGlobalDataPath() const;
std::filesystem::path getInstallPath() const;

View File

@ -50,7 +50,7 @@ namespace Files
return getUserConfigPath();
}
std::optional<std::filesystem::path> 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<std::filesystem::path> WindowsPath::getGlobalDataPath() const
std::filesystem::path WindowsPath::getGlobalDataPath() const
{
return getGlobalConfigPath();
}

View File

@ -4,7 +4,6 @@
#if defined(_WIN32) || defined(__WINDOWS__)
#include <filesystem>
#include <optional>
/**
* \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<std::filesystem::path> 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<std::filesystem::path> getGlobalDataPath() const;
std::filesystem::path getGlobalDataPath() const;
/**
* \brief Gets the path of the installed Morrowind version if there is one.