1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
|
#include "settings.hpp"
#include "parser.hpp"
#include "values.hpp"
#include <charconv>
#include <filesystem>
#include <sstream>
#include <system_error>
#if !(defined(_MSC_VER) && (_MSC_VER >= 1924)) && !(defined(__GNUC__) && __GNUC__ >= 11) || defined(__clang__) \
|| defined(__apple_build_version__)
#include <cerrno>
#include <ios>
#include <locale>
#endif
#include <components/debug/debuglog.hpp>
#include <components/files/configurationmanager.hpp>
#include <components/misc/strings/algorithm.hpp>
#include <components/misc/strings/conversion.hpp>
namespace Settings
{
namespace
{
template <class T>
T parseNumberFromSetting(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;
}
#if !(defined(_MSC_VER) && (_MSC_VER >= 1924)) && !(defined(__GNUC__) && __GNUC__ >= 11) || defined(__clang__) \
|| defined(__apple_build_version__)
template <>
float parseNumberFromSetting<float>(
const std::string& value, std::string_view setting, std::string_view category)
{
std::istringstream iss(value);
iss.imbue(std::locale::classic());
float floatValue = 0.0f;
if (!(iss >> floatValue))
{
throw std::system_error(errno, std::generic_category(),
"Failed to parse number from setting [" + std::string(category) + "] " + std::string(setting)
+ " value \"" + value + "\"");
}
return floatValue;
}
template <>
double parseNumberFromSetting<double>(
const std::string& value, std::string_view setting, std::string_view category)
{
std::istringstream iss(value);
iss.imbue(std::locale::classic());
double doubleValue = 0.0;
if (!(iss >> doubleValue))
{
throw std::system_error(errno, std::generic_category(),
"Failed to parse number from setting [" + std::string(category) + "] " + std::string(setting)
+ " value \"" + value + "\"");
}
return doubleValue;
}
#endif
template <class T>
std::string serialize(const T& value)
{
std::ostringstream stream;
stream << value;
return stream.str();
}
std::string toString(SceneUtil::LightingMethod value)
{
switch (value)
{
case SceneUtil::LightingMethod::FFP:
return "legacy";
case SceneUtil::LightingMethod::PerObjectUniform:
return "shaders compatibility";
case SceneUtil::LightingMethod::SingleUBO:
return "shaders";
}
throw std::invalid_argument("Invalid LightingMethod value: " + std::to_string(static_cast<int>(value)));
}
int toInt(HrtfMode value)
{
switch (value)
{
case HrtfMode::Auto:
return -1;
case HrtfMode::Disable:
return 0;
case HrtfMode::Enable:
return 1;
}
Log(Debug::Warning) << "Invalid HRTF mode value: " << static_cast<int>(value) << ", fallback to auto (-1)";
return -1;
}
}
CategorySettingValueMap Manager::mDefaultSettings = CategorySettingValueMap();
CategorySettingValueMap Manager::mUserSettings = CategorySettingValueMap();
CategorySettingVector Manager::mChangedSettings = CategorySettingVector();
std::set<std::pair<std::string_view, std::string_view>> Manager::sInitialized;
void Manager::clear()
{
sInitialized.clear();
StaticValues::clear();
mDefaultSettings.clear();
mUserSettings.clear();
mChangedSettings.clear();
}
std::filesystem::path Manager::load(const Files::ConfigurationManager& cfgMgr, bool loadEditorSettings)
{
SettingsFileParser parser;
const std::vector<std::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.
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 auto defaultsBin = paths.front() / defaultSettingsFile;
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);
const CategorySettingValueMap originalDefaultSettings = mDefaultSettings;
// 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 auto additionalDefaults = paths[i] / userSettingsFile;
if (std::filesystem::exists(additionalDefaults))
parser.loadSettingsFile(additionalDefaults, mDefaultSettings, false, true);
}
if (!loadEditorSettings)
Settings::StaticValues::initDefaults();
// Load "settings.cfg" or "openmw-cs.cfg" from the last config dir as user settings. This path will be used to
// save modified settings.
auto settingspath = paths.back() / userSettingsFile;
if (std::filesystem::exists(settingspath))
parser.loadSettingsFile(settingspath, mUserSettings, false, false);
if (!loadEditorSettings)
Settings::StaticValues::init();
for (const auto& [key, value] : originalDefaultSettings)
if (!sInitialized.contains(key))
throw std::runtime_error("Default setting [" + key.first + "] " + key.second + " is not initialized");
return settingspath;
}
void Manager::saveUser(const std::filesystem::path& file)
{
SettingsFileParser parser;
parser.saveSettingsFile(file, mUserSettings);
}
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);
if (it != mUserSettings.end())
return it->second;
it = mDefaultSettings.find(key);
if (it != mDefaultSettings.end())
return it->second;
throw std::runtime_error("Trying to retrieve a non-existing setting: [" + std::string(category) + "] "
+ std::string(setting) + ".\nMake sure the defaults.bin file was properly installed.");
}
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
// and a serialized array which has one empty value
std::vector<std::string> values;
const std::string& value = getString(setting, category);
if (value.empty())
return values;
Misc::StringUtils::split(value, values, ",");
for (auto& item : values)
Misc::StringUtils::trim(item);
return values;
}
float Manager::getFloat(std::string_view setting, std::string_view category)
{
return parseNumberFromSetting<float>(getString(setting, category), setting, category);
}
double Manager::getDouble(std::string_view setting, std::string_view category)
{
return parseNumberFromSetting<double>(getString(setting, category), setting, category);
}
int Manager::getInt(std::string_view setting, std::string_view category)
{
return parseNumberFromSetting<int>(getString(setting, category), setting, category);
}
std::uint64_t Manager::getUInt64(std::string_view setting, std::string_view category)
{
return parseNumberFromSetting<uint64_t>(getString(setting, category), setting, category);
}
std::size_t Manager::getSize(std::string_view setting, std::string_view category)
{
return parseNumberFromSetting<size_t>(getString(setting, category), setting, category);
}
unsigned Manager::getUnsigned(std::string_view setting, std::string_view category)
{
return parseNumberFromSetting<unsigned>(getString(setting, category), setting, category);
}
unsigned long Manager::getUnsignedLong(std::string_view setting, std::string_view category)
{
return parseNumberFromSetting<unsigned long>(getString(setting, category), setting, category);
}
unsigned long long Manager::getUnsignedLongLong(std::string_view setting, std::string_view category)
{
return parseNumberFromSetting<unsigned long long>(getString(setting, category), setting, category);
}
bool Manager::getBool(std::string_view setting, std::string_view category)
{
const std::string& string = getString(setting, category);
return Misc::StringUtils::ciEqual(string, "true");
}
osg::Vec2f Manager::getVector2(std::string_view setting, std::string_view category)
{
const std::string& value = getString(setting, category);
std::vector<std::string> components;
Misc::StringUtils::split(value, components);
if (components.size() == 2)
{
auto x = Misc::StringUtils::toNumeric<float>(components[0]);
auto y = Misc::StringUtils::toNumeric<float>(components[1]);
if (x && y)
{
return { x.value(), y.value() };
}
}
throw std::runtime_error(std::string("Can't parse 2d vector: " + value));
}
osg::Vec3f Manager::getVector3(std::string_view setting, std::string_view category)
{
const std::string& value = getString(setting, category);
std::vector<std::string> components;
Misc::StringUtils::split(value, components);
if (components.size() == 3)
{
auto x = Misc::StringUtils::toNumeric<float>(components[0]);
auto y = Misc::StringUtils::toNumeric<float>(components[1]);
auto z = Misc::StringUtils::toNumeric<float>(components[2]);
if (x && y && z)
{
return { x.value(), y.value(), z.value() };
}
}
throw std::runtime_error(std::string("Can't parse 3d vector: " + value));
}
void Manager::setString(std::string_view setting, std::string_view category, const std::string& value)
{
auto found = mUserSettings.find(std::make_pair(category, setting));
if (found != mUserSettings.end())
{
if (found->second == value)
return;
}
CategorySettingValueMap::key_type key(category, setting);
mUserSettings[key] = value;
mChangedSettings.insert(std::move(key));
}
void Manager::setStringArray(
std::string_view setting, std::string_view category, const std::vector<std::string>& value)
{
std::stringstream stream;
// TODO: escape delimeters, new line characters, etc.
for (size_t i = 0; i < value.size(); ++i)
{
std::string item = value[i];
Misc::StringUtils::trim(item);
stream << item;
if (i < value.size() - 1)
stream << ",";
}
setString(setting, category, stream.str());
}
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::setUInt64(std::string_view setting, std::string_view category, const std::uint64_t value)
{
std::ostringstream stream;
stream << value;
setString(setting, category, stream.str());
}
void Manager::setFloat(std::string_view setting, std::string_view category, const float value)
{
std::ostringstream stream;
stream << value;
setString(setting, category, stream.str());
}
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)
{
setString(setting, category, value ? "true" : "false");
}
void Manager::setVector2(std::string_view setting, std::string_view category, const osg::Vec2f value)
{
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)
{
std::ostringstream stream;
stream << value.x() << ' ' << value.y() << ' ' << value.z();
setString(setting, category, stream.str());
}
CategorySettingVector Manager::getPendingChanges()
{
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;
}
void Manager::resetPendingChanges()
{
mChangedSettings.clear();
}
void Manager::resetPendingChanges(const CategorySettingVector& filter)
{
for (const auto& key : filter)
{
mChangedSettings.erase(key);
}
}
void Manager::set(std::string_view setting, std::string_view category, int value)
{
setInt(setting, category, value);
}
void Manager::set(std::string_view setting, std::string_view category, unsigned value)
{
setString(setting, category, serialize(value));
}
void Manager::set(std::string_view setting, std::string_view category, unsigned long value)
{
setString(setting, category, serialize(value));
}
void Manager::set(std::string_view setting, std::string_view category, unsigned long long value)
{
setString(setting, category, serialize(value));
}
void Manager::set(std::string_view setting, std::string_view category, float value)
{
setFloat(setting, category, value);
}
void Manager::set(std::string_view setting, std::string_view category, double value)
{
setDouble(setting, category, value);
}
void Manager::set(std::string_view setting, std::string_view category, const std::string& value)
{
setString(setting, category, value);
}
void Manager::set(std::string_view setting, std::string_view category, bool value)
{
setBool(setting, category, value);
}
void Manager::set(std::string_view setting, std::string_view category, const osg::Vec2f& value)
{
setVector2(setting, category, value);
}
void Manager::set(std::string_view setting, std::string_view category, const osg::Vec3f& value)
{
setVector3(setting, category, value);
}
void Manager::set(std::string_view setting, std::string_view category, DetourNavigator::CollisionShapeType value)
{
setInt(setting, category, static_cast<int>(value));
}
void Manager::set(std::string_view setting, std::string_view category, const std::vector<std::string>& value)
{
setStringArray(setting, category, value);
}
void Manager::set(std::string_view setting, std::string_view category, const MyGUI::Colour& value)
{
setString(setting, category, value.print());
}
void Manager::set(std::string_view setting, std::string_view category, SceneUtil::LightingMethod value)
{
setString(setting, category, toString(value));
}
void Manager::set(std::string_view setting, std::string_view category, HrtfMode value)
{
setInt(setting, category, toInt(value));
}
void Manager::set(std::string_view setting, std::string_view category, WindowMode value)
{
setInt(setting, category, static_cast<int>(value));
}
void Manager::set(std::string_view setting, std::string_view category, SDLUtil::VSyncMode value)
{
setInt(setting, category, static_cast<int>(value));
}
void Manager::recordInit(std::string_view setting, std::string_view category)
{
sInitialized.emplace(category, setting);
}
GyroscopeAxis parseGyroscopeAxis(std::string_view value)
{
if (value == "x")
return GyroscopeAxis::X;
else if (value == "y")
return GyroscopeAxis::Y;
else if (value == "z")
return GyroscopeAxis::Z;
else if (value == "-x")
return GyroscopeAxis::MinusX;
else if (value == "-y")
return GyroscopeAxis::MinusY;
else if (value == "-z")
return GyroscopeAxis::MinusZ;
throw std::runtime_error("Invalid gyroscope axis: " + std::string(value));
}
NavMeshRenderMode parseNavMeshRenderMode(std::string_view value)
{
if (value == "area type")
return NavMeshRenderMode::AreaType;
if (value == "update frequency")
return NavMeshRenderMode::UpdateFrequency;
throw std::invalid_argument("Invalid navigation mesh rendering mode: " + std::string(value));
}
SceneUtil::LightingMethod parseLightingMethod(std::string_view value)
{
if (value == "legacy")
return SceneUtil::LightingMethod::FFP;
if (value == "shaders compatibility")
return SceneUtil::LightingMethod::PerObjectUniform;
if (value == "shaders")
return SceneUtil::LightingMethod::SingleUBO;
constexpr const char* fallback = "shaders compatibility";
Log(Debug::Warning) << "Unknown lighting method '" << value << "', returning fallback '" << fallback << "'";
return SceneUtil::LightingMethod::PerObjectUniform;
}
}
|