1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-31 14:45:35 +00:00
openmw/components/settings/settings.cpp

246 lines
7.5 KiB
C++
Raw Normal View History

2012-03-30 16:38:33 +00:00
#include "settings.hpp"
#include "parser.hpp"
2012-03-30 16:38:33 +00:00
2022-05-25 19:16:26 +00:00
#include <filesystem>
#include <sstream>
2012-03-30 16:38:33 +00:00
#include <components/files/configurationmanager.hpp>
#include <components/misc/stringops.hpp>
2012-03-30 16:38:33 +00:00
namespace Settings
{
2014-05-19 06:07:43 +00:00
CategorySettingValueMap Manager::mDefaultSettings = CategorySettingValueMap();
CategorySettingValueMap Manager::mUserSettings = CategorySettingValueMap();
CategorySettingVector Manager::mChangedSettings = CategorySettingVector();
2012-03-30 16:38:33 +00:00
void Manager::clear()
{
mDefaultSettings.clear();
mUserSettings.clear();
mChangedSettings.clear();
}
std::string Manager::load(const Files::ConfigurationManager& cfgMgr, bool loadEditorSettings)
{
SettingsFileParser parser;
const std::vector<boost::filesystem::path>& paths = cfgMgr.getActiveConfigPaths();
if (paths.empty())
throw std::runtime_error("No config dirs! ConfigurationManager::readConfiguration must be called first.");
// Create file name strings for either the engine or the editor.
2022-05-04 20:33:39 +00:00
std::string defaultSettingsFile;
std::string userSettingsFile;
if (!loadEditorSettings)
{
defaultSettingsFile = "defaults.bin";
userSettingsFile = "settings.cfg";
}
else
{
defaultSettingsFile = "defaults-cs.bin";
userSettingsFile = "openmw-cs.cfg";
}
// Create the settings manager and load default settings file.
const std::string defaultsBin = (paths.front() / defaultSettingsFile).string();
2022-05-25 19:16:26 +00:00
if (!std::filesystem::exists(defaultsBin))
throw std::runtime_error ("No default settings file found! Make sure the file \"" + defaultSettingsFile + "\" was properly installed.");
parser.loadSettingsFile(defaultsBin, mDefaultSettings, true, false);
// Load "settings.cfg" or "openmw-cs.cfg" from every config dir except the last one as additional default settings.
for (int i = 0; i < static_cast<int>(paths.size()) - 1; ++i)
{
const std::string additionalDefaults = (paths[i] / userSettingsFile).string();
2022-05-25 19:16:26 +00:00
if (std::filesystem::exists(additionalDefaults))
parser.loadSettingsFile(additionalDefaults, mDefaultSettings, false, true);
}
// Load "settings.cfg" or "openmw-cs.cfg" from the last config dir as user settings. This path will be used to save modified settings.
std::string settingspath = (paths.back() / userSettingsFile).string();
2022-05-25 19:16:26 +00:00
if (std::filesystem::exists(settingspath))
parser.loadSettingsFile(settingspath, mUserSettings, false, false);
return settingspath;
}
void Manager::saveUser(const std::string &file)
{
SettingsFileParser parser;
parser.saveSettingsFile(file, mUserSettings);
}
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);
if (it != mUserSettings.end())
return it->second;
it = mDefaultSettings.find(key);
if (it != mDefaultSettings.end())
return it->second;
std::string error("Trying to retrieve a non-existing setting: ");
error += setting;
error += ".\nMake sure the defaults.bin file was properly installed.";
throw std::runtime_error(error);
2012-03-30 16:38:33 +00:00
}
float Manager::getFloat(std::string_view setting, std::string_view category)
2012-03-30 16:38:33 +00:00
{
const std::string& value = getString(setting, category);
std::stringstream stream(value);
float number = 0.f;
stream >> number;
return number;
2012-03-30 16:38:33 +00:00
}
double Manager::getDouble(std::string_view setting, std::string_view category)
{
const std::string& value = getString(setting, category);
std::stringstream stream(value);
double number = 0.0;
stream >> number;
return number;
}
int Manager::getInt(std::string_view setting, std::string_view category)
2012-03-30 16:38:33 +00:00
{
const std::string& value = getString(setting, category);
std::stringstream stream(value);
int number = 0;
stream >> number;
return number;
2012-03-30 16:38:33 +00:00
}
std::int64_t Manager::getInt64(std::string_view setting, std::string_view category)
{
const std::string& value = getString(setting, category);
std::stringstream stream(value);
std::int64_t number = 0;
stream >> number;
return number;
}
bool Manager::getBool(std::string_view setting, std::string_view category)
2012-03-30 16:38:33 +00:00
{
2019-01-07 13:47:39 +00:00
const std::string& string = getString(setting, category);
return Misc::StringUtils::ciEqual(string, "true");
2012-03-30 16:38:33 +00:00
}
2012-03-31 11:35:40 +00:00
osg::Vec2f Manager::getVector2(std::string_view setting, std::string_view category)
2020-07-24 19:47:49 +00:00
{
const std::string& value = getString(setting, category);
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));
return {x, y};
2020-07-24 19:47:49 +00:00
}
osg::Vec3f Manager::getVector3(std::string_view setting, std::string_view category)
2020-07-24 19:47:49 +00:00
{
const std::string& value = getString(setting, category);
std::stringstream stream(value);
float x, y, z;
stream >> x >> y >> z;
if (stream.fail())
throw std::runtime_error(std::string("Can't parse 3d vector: " + value));
return {x, y, z};
2020-07-24 19:47:49 +00:00
}
void Manager::setString(std::string_view setting, std::string_view category, const std::string &value)
2012-03-31 11:35:40 +00:00
{
auto found = mUserSettings.find(std::make_pair(category, setting));
if (found != mUserSettings.end())
2012-03-31 11:35:40 +00:00
{
if (found->second == value)
return;
}
2012-03-31 11:35:40 +00:00
CategorySettingValueMap::key_type key(category, setting);
mUserSettings[key] = value;
mChangedSettings.insert(std::move(key));
2012-03-31 11:35:40 +00:00
}
void Manager::setInt(std::string_view setting, std::string_view category, const int value)
{
std::ostringstream stream;
stream << value;
setString(setting, category, stream.str());
}
void Manager::setInt64(std::string_view setting, std::string_view category, const std::int64_t value)
2012-03-31 11:35:40 +00:00
{
std::ostringstream stream;
stream << value;
setString(setting, category, stream.str());
2012-03-31 11:35:40 +00:00
}
void Manager::setFloat (std::string_view setting, std::string_view category, const float value)
2012-03-31 11:35:40 +00:00
{
std::ostringstream stream;
stream << value;
setString(setting, category, stream.str());
2012-03-31 11:35:40 +00:00
}
void Manager::setDouble (std::string_view setting, std::string_view category, const double value)
{
std::ostringstream stream;
stream << value;
setString(setting, category, stream.str());
}
void Manager::setBool(std::string_view setting, std::string_view category, const bool value)
2012-03-31 11:35:40 +00:00
{
2019-01-07 13:47:39 +00:00
setString(setting, category, value ? "true" : "false");
2012-03-31 11:35:40 +00:00
}
void Manager::setVector2 (std::string_view setting, std::string_view category, const osg::Vec2f value)
2020-07-24 19:47:49 +00:00
{
std::ostringstream stream;
stream << value.x() << " " << value.y();
setString(setting, category, stream.str());
}
void Manager::setVector3 (std::string_view setting, std::string_view category, const osg::Vec3f value)
2020-07-24 19:47:49 +00:00
{
std::ostringstream stream;
stream << value.x() << ' ' << value.y() << ' ' << value.z();
setString(setting, category, stream.str());
}
CategorySettingVector Manager::getPendingChanges()
2019-05-04 17:38:36 +00:00
{
return mChangedSettings;
}
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 17:38:36 +00:00
void Manager::resetPendingChanges()
{
mChangedSettings.clear();
}
void Manager::resetPendingChanges(const CategorySettingVector& filter)
{
for (const auto& key : filter)
{
mChangedSettings.erase(key);
}
}
}