Remove global config on Windows

This commit is contained in:
Evil Eye 2025-07-07 16:32:42 +02:00
parent 730a62effc
commit c0b230b742
13 changed files with 67 additions and 82 deletions

View File

@ -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");

View File

@ -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<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::filesystem::path AndroidPath::getGlobalDataPath() const
std::optional<std::filesystem::path> AndroidPath::getGlobalDataPath() const
{
return std::filesystem::path(g_path_user);
}

View File

@ -4,6 +4,7 @@
#if defined(__ANDROID__)
#include <filesystem>
#include <optional>
/**
* \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<std::filesystem::path> 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<std::filesystem::path> getGlobalDataPath() const;
/**
* \brief

View File

@ -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<bpo::variables_map> 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<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::filesystem::path& getGlobalPath() const;
const std::optional<std::filesystem::path>& 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<std::u8string, path_type_f> TokensMappingContainer;
std::optional<boost::program_options::variables_map> loadConfig(
const std::filesystem::path& path, const boost::program_options::options_description& description) const;
void addExtraConfigDirs(
std::stack<std::filesystem::path>& dirs, const boost::program_options::variables_map& variables) const;
void setupTokensMapping();
std::vector<std::filesystem::path> mActiveConfigPaths;
FixedPathType mFixedPath;
@ -80,8 +75,6 @@ namespace Files
std::filesystem::path mUserDataPath;
std::filesystem::path mScreenshotPath;
TokensMappingContainer mTokensMapping;
bool mSilent;
};

View File

@ -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<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::filesystem::path& getGlobalDataPath() const { return mGlobalDataPath; }
const std::optional<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::filesystem::path mGlobalConfigPath; /**< Global path */
std::optional<std::filesystem::path> 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<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::filesystem::path LinuxPath::getGlobalConfigPath() const
std::optional<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::filesystem::path LinuxPath::getGlobalDataPath() const
std::optional<std::filesystem::path> LinuxPath::getGlobalDataPath() const
{
std::filesystem::path globalDataPath(GLOBAL_DATA_PATH);
return globalDataPath / mName;

View File

@ -4,6 +4,7 @@
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
#include <filesystem>
#include <optional>
/**
* \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<std::filesystem::path> 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<std::filesystem::path> getGlobalDataPath() const;
/**
* \brief

View File

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

View File

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

View File

@ -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();

View File

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

View File

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