From 8e487c283c26d3f9cc4630a5c613554ee243950e Mon Sep 17 00:00:00 2001 From: elsid Date: Sat, 22 Apr 2023 20:40:17 +0200 Subject: [PATCH 1/2] Store default settings values --- components/settings/settings.cpp | 3 +++ components/settings/settingvalue.hpp | 22 ++++++++++++++++++++-- components/settings/values.cpp | 14 +++++++++++++- components/settings/values.hpp | 3 ++- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/components/settings/settings.cpp b/components/settings/settings.cpp index 04c3425de2..aa44734562 100644 --- a/components/settings/settings.cpp +++ b/components/settings/settings.cpp @@ -141,6 +141,9 @@ namespace Settings parser.loadSettingsFile(additionalDefaults, mDefaultSettings, false, true); } + if (!loadEditorSettings) + Settings::Values::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; diff --git a/components/settings/settingvalue.hpp b/components/settings/settingvalue.hpp index e84f014c9b..bdc91c4a21 100644 --- a/components/settings/settingvalue.hpp +++ b/components/settings/settingvalue.hpp @@ -23,10 +23,25 @@ namespace Settings : mCategory(category) , mName(name) , mSanitizer(std::move(sanitizer)) - , mValue(sanitize(Settings::Manager::get(name, category))) + , mValue(sanitize(Settings::Manager::get(mName, mCategory))) { } + SettingValue(SettingValue&& other) + : mCategory(other.mCategory) + , mName(other.mName) + , mSanitizer(std::move(other.mSanitizer)) + , mDefaultValue(std::move(other.mValue)) + , mValue(sanitize(Settings::Manager::get(mName, mCategory))) + { + } + + SettingValue(const SettingValue& other) = delete; + + SettingValue& operator=(const SettingValue& other) = delete; + + SettingValue& operator=(SettingValue&& other) = delete; + const T& get() const { return mValue; } operator const T&() const { return mValue; } @@ -39,10 +54,13 @@ namespace Settings Settings::Manager::set(mName, mCategory, mValue); } + void reset() { set(mDefaultValue); } + private: const std::string_view mCategory; const std::string_view mName; - const std::unique_ptr> mSanitizer; + std::unique_ptr> mSanitizer; + T mDefaultValue{}; T mValue{}; T sanitize(const T& value) const diff --git a/components/settings/values.cpp b/components/settings/values.cpp index 668a85c8fd..99ca0bcb9b 100644 --- a/components/settings/values.cpp +++ b/components/settings/values.cpp @@ -1,12 +1,24 @@ #include "values.hpp" +#include + namespace Settings { Values* Values::sValues = nullptr; - void Values::init() + void Values::initDefaults() { + if (Values::sValues != nullptr) + throw std::logic_error("Default settings already initialized"); static Values values; Values::sValues = &values; } + + void Values::init() + { + if (Values::sValues == nullptr) + throw std::logic_error("Default settings are not initialized"); + static Values values(std::move(*Values::sValues)); + Values::sValues = &values; + } } diff --git a/components/settings/values.hpp b/components/settings/values.hpp index c6badbaa7c..b95cc1885d 100644 --- a/components/settings/values.hpp +++ b/components/settings/values.hpp @@ -58,6 +58,8 @@ namespace Settings StereoViewCategory mStereoView; PostProcessingCategory mPostProcessing; + static void initDefaults(); + static void init(); private: @@ -195,7 +197,6 @@ namespace Settings { return values().mPostProcessing; } - } #endif From 39e867781efe90760a2dfe4a84b20423420a97a1 Mon Sep 17 00:00:00 2001 From: elsid Date: Mon, 10 Apr 2023 17:55:41 +0200 Subject: [PATCH 2/2] Avoid using Settings::Manager::mDefaultSettings directly --- apps/openmw/mwgui/settingswindow.cpp | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/apps/openmw/mwgui/settingswindow.cpp b/apps/openmw/mwgui/settingswindow.cpp index b787ce59e6..7d81c81a8d 100644 --- a/apps/openmw/mwgui/settingswindow.cpp +++ b/apps/openmw/mwgui/settingswindow.cpp @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include @@ -620,21 +620,16 @@ namespace MWGui if (selectedButton == 1 || selectedButton == -1) return; - constexpr std::array settings = { - "light bounds multiplier", - "maximum light distance", - "light fade start", - "minimum interior brightness", - "max lights", - "lighting method", - }; - for (const auto& setting : settings) - Settings::Manager::setString( - setting, "Shaders", Settings::Manager::mDefaultSettings[{ "Shaders", setting }]); + Settings::shaders().mLightBoundsMultiplier.reset(); + Settings::shaders().mMaximumLightDistance.reset(); + Settings::shaders().mLightFadeStart.reset(); + Settings::shaders().mMinimumInteriorBrightness.reset(); + Settings::shaders().mMaxLights.reset(); + Settings::shaders().mLightingMethod.reset(); - auto lightingMethod = SceneUtil::LightManager::getLightingMethodFromString( - Settings::Manager::mDefaultSettings[{ "Shaders", "lighting method" }]); - auto lightIndex = mLightingMethodButton->findItemIndexWith(lightingMethodToStr(lightingMethod)); + const SceneUtil::LightingMethod lightingMethod + = SceneUtil::LightManager::getLightingMethodFromString(Settings::shaders().mLightingMethod); + const std::size_t lightIndex = mLightingMethodButton->findItemIndexWith(lightingMethodToStr(lightingMethod)); mLightingMethodButton->setIndexSelected(lightIndex); updateMaxLightsComboBox(mMaxLights);