2012-03-30 18:38:33 +02:00
|
|
|
#include "settings.hpp"
|
2019-09-29 16:16:19 +02:00
|
|
|
#include "parser.hpp"
|
2012-03-30 18:38:33 +02:00
|
|
|
|
2022-10-03 23:30:16 +02:00
|
|
|
#include <cerrno>
|
|
|
|
#include <charconv>
|
2022-05-25 21:16:26 +02:00
|
|
|
#include <filesystem>
|
2019-09-30 18:51:11 +03:00
|
|
|
#include <sstream>
|
2022-10-03 23:30:16 +02:00
|
|
|
#include <system_error>
|
2012-03-30 18:38:33 +02:00
|
|
|
|
2022-01-16 01:59:20 +01:00
|
|
|
#include <components/files/configurationmanager.hpp>
|
2022-08-03 00:00:54 +02:00
|
|
|
#include <components/misc/strings/algorithm.hpp>
|
2012-03-30 18:38:33 +02:00
|
|
|
|
2015-02-03 00:53:30 +01:00
|
|
|
namespace Settings
|
|
|
|
{
|
2022-10-03 23:30:16 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
template <class T>
|
|
|
|
auto parseIntegralNumber(const std::string& value, std::string_view setting, std::string_view category)
|
|
|
|
{
|
|
|
|
T number{};
|
|
|
|
const auto result = std::from_chars(value.data(), value.data() + value.size(), number);
|
|
|
|
if (result.ec != std::errc())
|
|
|
|
throw std::system_error(std::make_error_code(result.ec),
|
|
|
|
"Failed to parse number from setting [" + std::string(category) + "] " + std::string(setting)
|
|
|
|
+ " value \"" + value + "\"");
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
auto parseFloatingPointNumber(const std::string& value, std::string_view setting, std::string_view category)
|
|
|
|
{
|
|
|
|
std::stringstream stream(value);
|
|
|
|
T number{};
|
|
|
|
stream >> number;
|
|
|
|
if (stream.bad())
|
|
|
|
throw std::system_error(errno, std::generic_category(),
|
|
|
|
"Failed to parse number from setting [" + std::string(category) + "] " + std::string(setting)
|
|
|
|
+ " value \"" + value + "\"");
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
}
|
2014-05-19 10:07:43 +04:00
|
|
|
|
2015-02-03 00:53:30 +01:00
|
|
|
CategorySettingValueMap Manager::mDefaultSettings = CategorySettingValueMap();
|
|
|
|
CategorySettingValueMap Manager::mUserSettings = CategorySettingValueMap();
|
2012-04-01 18:48:37 +02:00
|
|
|
CategorySettingVector Manager::mChangedSettings = CategorySettingVector();
|
2012-03-30 18:38:33 +02:00
|
|
|
|
2015-11-27 20:32:45 +01:00
|
|
|
void Manager::clear()
|
|
|
|
{
|
|
|
|
mDefaultSettings.clear();
|
|
|
|
mUserSettings.clear();
|
|
|
|
mChangedSettings.clear();
|
|
|
|
}
|
|
|
|
|
2022-06-19 13:28:33 +02:00
|
|
|
std::filesystem::path Manager::load(const Files::ConfigurationManager& cfgMgr, bool loadEditorSettings)
|
2015-02-03 00:53:30 +01:00
|
|
|
{
|
|
|
|
SettingsFileParser parser;
|
2022-06-08 23:25:50 +02:00
|
|
|
const std::vector<std::filesystem::path>& paths = cfgMgr.getActiveConfigPaths();
|
2022-01-16 01:59:20 +01:00
|
|
|
if (paths.empty())
|
|
|
|
throw std::runtime_error("No config dirs! ConfigurationManager::readConfiguration must be called first.");
|
2022-09-22 21:26:05 +03:00
|
|
|
|
2022-03-04 09:44:52 +00:00
|
|
|
// Create file name strings for either the engine or the editor.
|
2022-05-04 22:33:39 +02:00
|
|
|
std::string defaultSettingsFile;
|
|
|
|
std::string userSettingsFile;
|
2022-09-22 21:26:05 +03:00
|
|
|
|
2022-03-04 09:44:52 +00:00
|
|
|
if (!loadEditorSettings)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2022-03-04 09:44:52 +00:00
|
|
|
defaultSettingsFile = "defaults.bin";
|
|
|
|
userSettingsFile = "settings.cfg";
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-04 09:44:52 +00:00
|
|
|
defaultSettingsFile = "defaults-cs.bin";
|
|
|
|
userSettingsFile = "openmw-cs.cfg";
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
|
|
|
|
2022-01-16 01:59:20 +01:00
|
|
|
// Create the settings manager and load default settings file.
|
2022-06-19 13:28:33 +02:00
|
|
|
const auto defaultsBin = paths.front() / defaultSettingsFile;
|
2022-05-25 21:16:26 +02:00
|
|
|
if (!std::filesystem::exists(defaultsBin))
|
2022-03-04 09:44:52 +00:00
|
|
|
throw std::runtime_error("No default settings file found! Make sure the file \"" + defaultSettingsFile
|
|
|
|
+ "\" was properly installed.");
|
2022-01-16 01:59:20 +01:00
|
|
|
parser.loadSettingsFile(defaultsBin, mDefaultSettings, true, false);
|
2022-09-22 21:26:05 +03:00
|
|
|
|
2022-03-04 09:44:52 +00:00
|
|
|
// Load "settings.cfg" or "openmw-cs.cfg" from every config dir except the last one as additional default
|
|
|
|
// settings.
|
2022-01-16 01:59:20 +01:00
|
|
|
for (int i = 0; i < static_cast<int>(paths.size()) - 1; ++i)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2022-06-19 13:28:33 +02:00
|
|
|
const auto additionalDefaults = paths[i] / userSettingsFile;
|
2022-05-25 21:16:26 +02:00
|
|
|
if (std::filesystem::exists(additionalDefaults))
|
2022-01-16 01:59:20 +01:00
|
|
|
parser.loadSettingsFile(additionalDefaults, mDefaultSettings, false, true);
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
|
|
|
|
2022-03-04 09:44:52 +00:00
|
|
|
// Load "settings.cfg" or "openmw-cs.cfg" from the last config dir as user settings. This path will be used to
|
|
|
|
// save modified settings.
|
2022-06-19 13:28:33 +02:00
|
|
|
auto settingspath = paths.back() / userSettingsFile;
|
2022-05-25 21:16:26 +02:00
|
|
|
if (std::filesystem::exists(settingspath))
|
2022-01-16 01:59:20 +01:00
|
|
|
parser.loadSettingsFile(settingspath, mUserSettings, false, false);
|
2022-09-22 21:26:05 +03:00
|
|
|
|
2022-01-16 01:59:20 +01:00
|
|
|
return settingspath;
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
2022-01-16 01:59:20 +01:00
|
|
|
|
2022-03-04 09:44:52 +00:00
|
|
|
void Manager::saveUser(const std::filesystem::path& file)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2022-05-04 22:33:39 +02:00
|
|
|
SettingsFileParser parser;
|
|
|
|
parser.saveSettingsFile(file, mUserSettings);
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
2022-03-04 09:44:52 +00:00
|
|
|
|
|
|
|
const std::string& Manager::getString(std::string_view setting, std::string_view category)
|
|
|
|
{
|
|
|
|
const auto key = std::make_pair(category, setting);
|
|
|
|
CategorySettingValueMap::iterator it = mUserSettings.find(key);
|
2015-02-03 00:53:30 +01:00
|
|
|
if (it != mUserSettings.end())
|
|
|
|
return it->second;
|
2022-09-22 21:26:05 +03:00
|
|
|
|
2015-02-03 00:53:30 +01:00
|
|
|
it = mDefaultSettings.find(key);
|
2022-03-04 09:44:52 +00:00
|
|
|
if (it != mDefaultSettings.end())
|
2015-02-03 00:53:30 +01:00
|
|
|
return it->second;
|
2022-09-22 21:26:05 +03:00
|
|
|
|
2022-06-18 02:44:56 +02:00
|
|
|
std::string error("Trying to retrieve a non-existing setting: ");
|
|
|
|
error += setting;
|
2022-03-04 09:44:52 +00:00
|
|
|
error += ".\nMake sure the defaults.bin file was properly installed.";
|
2022-06-18 02:44:56 +02:00
|
|
|
throw std::runtime_error(error);
|
2022-03-04 09:44:52 +00:00
|
|
|
}
|
2022-09-22 21:26:05 +03:00
|
|
|
|
2022-03-04 09:44:52 +00:00
|
|
|
std::vector<std::string> Manager::getStringArray(std::string_view setting, std::string_view category)
|
|
|
|
{
|
|
|
|
// TODO: it is unclear how to handle empty value -
|
|
|
|
// there is no difference between empty serialized array
|
2022-07-25 08:19:01 +04:00
|
|
|
// and a serialized array which has one empty value
|
|
|
|
std::vector<std::string> values;
|
2022-03-04 09:44:52 +00:00
|
|
|
const std::string& value = getString(setting, category);
|
2022-07-25 08:19:01 +04:00
|
|
|
if (value.empty())
|
2022-03-04 09:44:52 +00:00
|
|
|
return values;
|
2022-09-22 21:26:05 +03:00
|
|
|
|
2022-03-04 09:44:52 +00:00
|
|
|
Misc::StringUtils::split(value, values, ",");
|
2022-07-25 08:19:01 +04:00
|
|
|
for (auto& item : values)
|
|
|
|
Misc::StringUtils::trim(item);
|
|
|
|
return values;
|
2022-03-04 09:44:52 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 01:59:20 +01:00
|
|
|
float Manager::getFloat(std::string_view setting, std::string_view category)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2022-10-03 23:30:16 +02:00
|
|
|
return parseFloatingPointNumber<float>(getString(setting, category), setting, category);
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
2022-01-16 01:59:20 +01:00
|
|
|
|
2022-03-04 09:44:52 +00:00
|
|
|
double Manager::getDouble(std::string_view setting, std::string_view category)
|
2022-01-16 01:59:20 +01:00
|
|
|
{
|
2022-10-03 23:30:16 +02:00
|
|
|
return parseFloatingPointNumber<double>(getString(setting, category), setting, category);
|
2022-01-16 01:59:20 +01:00
|
|
|
}
|
|
|
|
|
2022-03-04 09:44:52 +00:00
|
|
|
int Manager::getInt(std::string_view setting, std::string_view category)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2022-10-03 23:30:16 +02:00
|
|
|
return parseIntegralNumber<int>(getString(setting, category), setting, category);
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
2022-01-16 01:59:20 +01:00
|
|
|
|
2022-10-04 00:02:53 +02:00
|
|
|
std::uint64_t Manager::getUInt64(std::string_view setting, std::string_view category)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2022-10-04 00:02:53 +02:00
|
|
|
return parseIntegralNumber<std::uint64_t>(getString(setting, category), setting, category);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t Manager::getSize(std::string_view setting, std::string_view category)
|
|
|
|
{
|
|
|
|
return parseIntegralNumber<std::size_t>(getString(setting, category), setting, category);
|
2015-02-03 00:53:30 +01:00
|
|
|
}
|
|
|
|
|
2022-06-19 13:28:33 +02:00
|
|
|
bool Manager::getBool(std::string_view setting, std::string_view category)
|
2015-02-03 00:53:30 +01:00
|
|
|
{
|
2015-11-23 18:10:33 -05:00
|
|
|
const std::string& string = getString(setting, category);
|
|
|
|
return Misc::StringUtils::ciEqual(string, "true");
|
2015-02-03 00:53:30 +01:00
|
|
|
}
|
|
|
|
|
2022-08-28 16:38:11 +02:00
|
|
|
osg::Vec2f Manager::getVector2(std::string_view setting, std::string_view category)
|
2015-02-03 00:53:30 +01:00
|
|
|
{
|
|
|
|
const std::string& value = getString(setting, category);
|
2022-06-18 02:44:56 +02:00
|
|
|
std::stringstream stream(value);
|
|
|
|
float x, y;
|
|
|
|
stream >> x >> y;
|
|
|
|
if (stream.fail())
|
|
|
|
throw std::runtime_error(std::string("Can't parse 2d vector: " + value));
|
2021-11-30 16:00:30 +00:00
|
|
|
return { x, y };
|
2012-03-30 18:38:33 +02:00
|
|
|
}
|
|
|
|
|
2022-07-25 08:19:01 +04:00
|
|
|
osg::Vec3f Manager::getVector3(std::string_view setting, std::string_view category)
|
|
|
|
{
|
|
|
|
const std::string& value = getString(setting, category);
|
|
|
|
std::stringstream stream(value);
|
2020-07-24 21:47:49 +02:00
|
|
|
float x, y, z;
|
|
|
|
stream >> x >> y >> z;
|
2022-07-25 08:19:01 +04:00
|
|
|
if (stream.fail())
|
|
|
|
throw std::runtime_error(std::string("Can't parse 3d vector: " + value));
|
|
|
|
return { x, y, z };
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
2022-07-25 08:19:01 +04:00
|
|
|
|
|
|
|
void Manager::setString(std::string_view setting, std::string_view category, const std::string& value)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2022-07-25 08:19:01 +04:00
|
|
|
auto found = mUserSettings.find(std::make_pair(category, setting));
|
2015-02-03 00:53:30 +01:00
|
|
|
if (found != mUserSettings.end())
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2022-07-25 08:19:01 +04:00
|
|
|
if (found->second == value)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-30 18:51:11 +03:00
|
|
|
CategorySettingValueMap::key_type key(category, setting);
|
2012-03-30 18:38:33 +02:00
|
|
|
|
2021-05-29 16:27:23 -05:00
|
|
|
mUserSettings[key] = value;
|
|
|
|
|
2022-06-18 02:44:56 +02:00
|
|
|
mChangedSettings.insert(std::move(key));
|
2012-03-30 18:38:33 +02:00
|
|
|
}
|
|
|
|
|
2022-06-18 02:44:56 +02:00
|
|
|
void Manager::setStringArray(
|
|
|
|
std::string_view setting, std::string_view category, const std::vector<std::string>& value)
|
2021-11-06 13:46:43 +01:00
|
|
|
{
|
|
|
|
std::stringstream stream;
|
|
|
|
|
2022-06-18 02:44:56 +02:00
|
|
|
// TODO: escape delimeters, new line characters, etc.
|
2019-01-07 17:47:39 +04:00
|
|
|
for (size_t i = 0; i < value.size(); ++i)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2019-01-07 17:47:39 +04:00
|
|
|
std::string item = value[i];
|
|
|
|
Misc::StringUtils::trim(item);
|
|
|
|
stream << item;
|
2012-03-31 13:35:40 +02:00
|
|
|
|
2020-07-24 21:47:49 +02:00
|
|
|
if (i < value.size() - 1)
|
|
|
|
stream << ",";
|
|
|
|
}
|
|
|
|
|
|
|
|
setString(setting, category, stream.str());
|
|
|
|
}
|
|
|
|
|
2022-06-18 02:44:56 +02:00
|
|
|
void Manager::setInt(std::string_view setting, std::string_view category, const int value)
|
2012-03-31 13:35:40 +02:00
|
|
|
{
|
2019-05-30 07:10:46 +03:00
|
|
|
std::ostringstream stream;
|
2015-02-03 00:53:30 +01:00
|
|
|
stream << value;
|
|
|
|
setString(setting, category, stream.str());
|
2012-04-01 18:48:37 +02:00
|
|
|
}
|
2012-03-31 13:35:40 +02:00
|
|
|
|
2022-10-04 00:02:53 +02:00
|
|
|
void Manager::setUInt64(std::string_view setting, std::string_view category, const std::uint64_t value)
|
2022-07-25 08:19:01 +04:00
|
|
|
{
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << value;
|
|
|
|
setString(setting, category, stream.str());
|
|
|
|
}
|
|
|
|
|
2022-06-18 02:44:56 +02:00
|
|
|
void Manager::setFloat(std::string_view setting, std::string_view category, const float value)
|
2022-04-07 16:35:18 +02:00
|
|
|
{
|
2019-05-30 07:10:46 +03:00
|
|
|
std::ostringstream stream;
|
|
|
|
stream << value;
|
|
|
|
setString(setting, category, stream.str());
|
2012-03-31 13:35:40 +02:00
|
|
|
}
|
|
|
|
|
2022-06-18 02:44:56 +02:00
|
|
|
void Manager::setDouble(std::string_view setting, std::string_view category, const double value)
|
2021-05-29 16:27:23 -05:00
|
|
|
{
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << value;
|
|
|
|
setString(setting, category, stream.str());
|
|
|
|
}
|
|
|
|
|
2022-06-18 02:44:56 +02:00
|
|
|
void Manager::setBool(std::string_view setting, std::string_view category, const bool value)
|
2012-03-31 13:35:40 +02:00
|
|
|
{
|
2019-01-07 17:47:39 +04:00
|
|
|
setString(setting, category, value ? "true" : "false");
|
2012-03-31 13:35:40 +02:00
|
|
|
}
|
2012-04-01 16:26:42 +02:00
|
|
|
|
2022-06-18 02:44:56 +02:00
|
|
|
void Manager::setVector2(std::string_view setting, std::string_view category, const osg::Vec2f value)
|
2020-07-24 21:47:49 +02:00
|
|
|
{
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << value.x() << " " << value.y();
|
|
|
|
setString(setting, category, stream.str());
|
|
|
|
}
|
|
|
|
|
2022-06-18 02:44:56 +02:00
|
|
|
void Manager::setVector3(std::string_view setting, std::string_view category, const osg::Vec3f value)
|
2020-07-24 21:47:49 +02:00
|
|
|
{
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << value.x() << ' ' << value.y() << ' ' << value.z();
|
|
|
|
setString(setting, category, stream.str());
|
|
|
|
}
|
|
|
|
|
2021-11-30 16:00:30 +00:00
|
|
|
CategorySettingVector Manager::getPendingChanges()
|
2019-05-04 21:38:36 +04:00
|
|
|
{
|
|
|
|
return mChangedSettings;
|
|
|
|
}
|
|
|
|
|
2021-11-30 16:00:30 +00:00
|
|
|
CategorySettingVector Manager::getPendingChanges(const CategorySettingVector& filter)
|
|
|
|
{
|
|
|
|
CategorySettingVector intersection;
|
|
|
|
std::set_intersection(mChangedSettings.begin(), mChangedSettings.end(), filter.begin(), filter.end(),
|
|
|
|
std::inserter(intersection, intersection.begin()));
|
|
|
|
return intersection;
|
|
|
|
}
|
|
|
|
|
2019-05-04 21:38:36 +04:00
|
|
|
void Manager::resetPendingChanges()
|
2012-04-01 16:26:42 +02:00
|
|
|
{
|
|
|
|
mChangedSettings.clear();
|
|
|
|
}
|
2015-02-03 00:53:30 +01:00
|
|
|
|
2021-11-30 16:00:30 +00:00
|
|
|
void Manager::resetPendingChanges(const CategorySettingVector& filter)
|
|
|
|
{
|
|
|
|
for (const auto& key : filter)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2021-11-30 16:00:30 +00:00
|
|
|
mChangedSettings.erase(key);
|
2022-09-22 21:26:05 +03:00
|
|
|
}
|
2021-11-30 16:00:30 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 00:53:30 +01:00
|
|
|
}
|