mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-16 15:29:55 +00:00
Ignore absent default setting value
This commit is contained in:
parent
4c13ecea23
commit
e1a68d8cf5
5 changed files with 46 additions and 38 deletions
|
@ -463,8 +463,6 @@ CSMPrefs::IntSetting& CSMPrefs::State::declareInt(const std::string& key, const
|
||||||
if (mCurrentCategory == mCategories.end())
|
if (mCurrentCategory == mCategories.end())
|
||||||
throw std::logic_error("no category for setting");
|
throw std::logic_error("no category for setting");
|
||||||
|
|
||||||
setDefault(key, std::to_string(default_));
|
|
||||||
|
|
||||||
CSMPrefs::IntSetting* setting = new CSMPrefs::IntSetting(&mCurrentCategory->second, &mMutex, key, label, *mIndex);
|
CSMPrefs::IntSetting* setting = new CSMPrefs::IntSetting(&mCurrentCategory->second, &mMutex, key, label, *mIndex);
|
||||||
|
|
||||||
mCurrentCategory->second.addSetting(setting);
|
mCurrentCategory->second.addSetting(setting);
|
||||||
|
@ -477,10 +475,6 @@ CSMPrefs::DoubleSetting& CSMPrefs::State::declareDouble(const std::string& key,
|
||||||
if (mCurrentCategory == mCategories.end())
|
if (mCurrentCategory == mCategories.end())
|
||||||
throw std::logic_error("no category for setting");
|
throw std::logic_error("no category for setting");
|
||||||
|
|
||||||
std::ostringstream stream;
|
|
||||||
stream << default_;
|
|
||||||
setDefault(key, stream.str());
|
|
||||||
|
|
||||||
CSMPrefs::DoubleSetting* setting
|
CSMPrefs::DoubleSetting* setting
|
||||||
= new CSMPrefs::DoubleSetting(&mCurrentCategory->second, &mMutex, key, label, *mIndex);
|
= new CSMPrefs::DoubleSetting(&mCurrentCategory->second, &mMutex, key, label, *mIndex);
|
||||||
|
|
||||||
|
@ -494,8 +488,6 @@ CSMPrefs::BoolSetting& CSMPrefs::State::declareBool(const std::string& key, cons
|
||||||
if (mCurrentCategory == mCategories.end())
|
if (mCurrentCategory == mCategories.end())
|
||||||
throw std::logic_error("no category for setting");
|
throw std::logic_error("no category for setting");
|
||||||
|
|
||||||
setDefault(key, default_ ? "true" : "false");
|
|
||||||
|
|
||||||
CSMPrefs::BoolSetting* setting = new CSMPrefs::BoolSetting(&mCurrentCategory->second, &mMutex, key, label, *mIndex);
|
CSMPrefs::BoolSetting* setting = new CSMPrefs::BoolSetting(&mCurrentCategory->second, &mMutex, key, label, *mIndex);
|
||||||
|
|
||||||
mCurrentCategory->second.addSetting(setting);
|
mCurrentCategory->second.addSetting(setting);
|
||||||
|
@ -508,8 +500,6 @@ CSMPrefs::EnumSetting& CSMPrefs::State::declareEnum(const std::string& key, cons
|
||||||
if (mCurrentCategory == mCategories.end())
|
if (mCurrentCategory == mCategories.end())
|
||||||
throw std::logic_error("no category for setting");
|
throw std::logic_error("no category for setting");
|
||||||
|
|
||||||
setDefault(key, default_.mValue);
|
|
||||||
|
|
||||||
CSMPrefs::EnumSetting* setting = new CSMPrefs::EnumSetting(&mCurrentCategory->second, &mMutex, key, label, *mIndex);
|
CSMPrefs::EnumSetting* setting = new CSMPrefs::EnumSetting(&mCurrentCategory->second, &mMutex, key, label, *mIndex);
|
||||||
|
|
||||||
mCurrentCategory->second.addSetting(setting);
|
mCurrentCategory->second.addSetting(setting);
|
||||||
|
@ -522,8 +512,6 @@ CSMPrefs::ColourSetting& CSMPrefs::State::declareColour(const std::string& key,
|
||||||
if (mCurrentCategory == mCategories.end())
|
if (mCurrentCategory == mCategories.end())
|
||||||
throw std::logic_error("no category for setting");
|
throw std::logic_error("no category for setting");
|
||||||
|
|
||||||
setDefault(key, default_.name().toUtf8().data());
|
|
||||||
|
|
||||||
CSMPrefs::ColourSetting* setting
|
CSMPrefs::ColourSetting* setting
|
||||||
= new CSMPrefs::ColourSetting(&mCurrentCategory->second, &mMutex, key, label, *mIndex);
|
= new CSMPrefs::ColourSetting(&mCurrentCategory->second, &mMutex, key, label, *mIndex);
|
||||||
|
|
||||||
|
@ -538,9 +526,6 @@ CSMPrefs::ShortcutSetting& CSMPrefs::State::declareShortcut(
|
||||||
if (mCurrentCategory == mCategories.end())
|
if (mCurrentCategory == mCategories.end())
|
||||||
throw std::logic_error("no category for setting");
|
throw std::logic_error("no category for setting");
|
||||||
|
|
||||||
std::string seqStr = getShortcutManager().convertToString(default_);
|
|
||||||
setDefault(key, seqStr);
|
|
||||||
|
|
||||||
// Setup with actual data
|
// Setup with actual data
|
||||||
QKeySequence sequence;
|
QKeySequence sequence;
|
||||||
|
|
||||||
|
@ -560,8 +545,6 @@ CSMPrefs::StringSetting& CSMPrefs::State::declareString(
|
||||||
if (mCurrentCategory == mCategories.end())
|
if (mCurrentCategory == mCategories.end())
|
||||||
throw std::logic_error("no category for setting");
|
throw std::logic_error("no category for setting");
|
||||||
|
|
||||||
setDefault(key, default_);
|
|
||||||
|
|
||||||
CSMPrefs::StringSetting* setting
|
CSMPrefs::StringSetting* setting
|
||||||
= new CSMPrefs::StringSetting(&mCurrentCategory->second, &mMutex, key, label, *mIndex);
|
= new CSMPrefs::StringSetting(&mCurrentCategory->second, &mMutex, key, label, *mIndex);
|
||||||
|
|
||||||
|
@ -575,9 +558,6 @@ CSMPrefs::ModifierSetting& CSMPrefs::State::declareModifier(const std::string& k
|
||||||
if (mCurrentCategory == mCategories.end())
|
if (mCurrentCategory == mCategories.end())
|
||||||
throw std::logic_error("no category for setting");
|
throw std::logic_error("no category for setting");
|
||||||
|
|
||||||
std::string modStr = getShortcutManager().convertToString(default_);
|
|
||||||
setDefault(key, modStr);
|
|
||||||
|
|
||||||
// Setup with actual data
|
// Setup with actual data
|
||||||
int modifier;
|
int modifier;
|
||||||
|
|
||||||
|
@ -600,33 +580,20 @@ void CSMPrefs::State::declareSubcategory(const QString& label)
|
||||||
new CSMPrefs::Subcategory(&mCurrentCategory->second, &mMutex, label, *mIndex));
|
new CSMPrefs::Subcategory(&mCurrentCategory->second, &mMutex, label, *mIndex));
|
||||||
}
|
}
|
||||||
|
|
||||||
void CSMPrefs::State::setDefault(const std::string& key, const std::string& default_)
|
|
||||||
{
|
|
||||||
Settings::CategorySetting fullKey(mCurrentCategory->second.getKey(), key);
|
|
||||||
|
|
||||||
Settings::CategorySettingValueMap::iterator iter = Settings::Manager::mDefaultSettings.find(fullKey);
|
|
||||||
|
|
||||||
if (iter == Settings::Manager::mDefaultSettings.end())
|
|
||||||
Settings::Manager::mDefaultSettings.insert(std::make_pair(fullKey, default_));
|
|
||||||
}
|
|
||||||
|
|
||||||
CSMPrefs::State::State(const Files::ConfigurationManager& configurationManager)
|
CSMPrefs::State::State(const Files::ConfigurationManager& configurationManager)
|
||||||
: mConfigFile("openmw-cs.cfg")
|
: mConfigFile("openmw-cs.cfg")
|
||||||
, mDefaultConfigFile("defaults-cs.bin")
|
, mDefaultConfigFile("defaults-cs.bin")
|
||||||
, mConfigurationManager(configurationManager)
|
, mConfigurationManager(configurationManager)
|
||||||
, mCurrentCategory(mCategories.end())
|
, mCurrentCategory(mCategories.end())
|
||||||
, mIndex(std::make_unique<Settings::Index>())
|
, mIndex(std::make_unique<Settings::Index>())
|
||||||
|
, mValues(std::make_unique<Values>(*mIndex))
|
||||||
{
|
{
|
||||||
if (sThis)
|
if (sThis)
|
||||||
throw std::logic_error("An instance of CSMPRefs::State already exists");
|
throw std::logic_error("An instance of CSMPRefs::State already exists");
|
||||||
|
|
||||||
sThis = this;
|
sThis = this;
|
||||||
|
|
||||||
Values values(*mIndex);
|
|
||||||
|
|
||||||
declare();
|
declare();
|
||||||
|
|
||||||
mValues = std::make_unique<Values>(std::move(values));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CSMPrefs::State::~State()
|
CSMPrefs::State::~State()
|
||||||
|
|
|
@ -80,8 +80,6 @@ namespace CSMPrefs
|
||||||
|
|
||||||
void declareSubcategory(const QString& label);
|
void declareSubcategory(const QString& label);
|
||||||
|
|
||||||
void setDefault(const std::string& key, const std::string& default_);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
State(const Files::ConfigurationManager& configurationManager);
|
State(const Files::ConfigurationManager& configurationManager);
|
||||||
|
|
||||||
|
|
|
@ -59,6 +59,33 @@ namespace Settings
|
||||||
EXPECT_EQ(values.mCamera.mFieldOfView.get(), 1);
|
EXPECT_EQ(values.mCamera.mFieldOfView.get(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(SettingsValuesTest, constructorWithDefaultShouldDoLookup)
|
||||||
|
{
|
||||||
|
Manager::mUserSettings[std::make_pair("category", "value")] = "13";
|
||||||
|
Index index;
|
||||||
|
SettingValue<int> value{ index, "category", "value", 42 };
|
||||||
|
EXPECT_EQ(value.get(), 13);
|
||||||
|
value.reset();
|
||||||
|
EXPECT_EQ(value.get(), 42);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(SettingsValuesTest, constructorWithDefaultShouldSanitize)
|
||||||
|
{
|
||||||
|
Manager::mUserSettings[std::make_pair("category", "value")] = "2";
|
||||||
|
Index index;
|
||||||
|
SettingValue<int> value{ index, "category", "value", -1, Settings::makeClampSanitizerInt(0, 1) };
|
||||||
|
EXPECT_EQ(value.get(), 1);
|
||||||
|
value.reset();
|
||||||
|
EXPECT_EQ(value.get(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(SettingsValuesTest, constructorWithDefaultShouldFallbackToDefault)
|
||||||
|
{
|
||||||
|
Index index;
|
||||||
|
const SettingValue<int> value{ index, "category", "value", 42 };
|
||||||
|
EXPECT_EQ(value.get(), 42);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(SettingsValuesTest, moveConstructorShouldSetDefaults)
|
TEST_F(SettingsValuesTest, moveConstructorShouldSetDefaults)
|
||||||
{
|
{
|
||||||
Index index;
|
Index index;
|
||||||
|
@ -79,6 +106,13 @@ namespace Settings
|
||||||
EXPECT_EQ(values.mCamera.mFieldOfView.get(), 1);
|
EXPECT_EQ(values.mCamera.mFieldOfView.get(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(SettingsValuesTest, moveConstructorShouldThrowOnMissingSetting)
|
||||||
|
{
|
||||||
|
Index index;
|
||||||
|
SettingValue<int> defaultValue{ index, "category", "value", 42 };
|
||||||
|
EXPECT_THROW([&] { SettingValue<int> value(std::move(defaultValue)); }(), std::runtime_error);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(SettingsValuesTest, findShouldThrowExceptionOnTypeMismatch)
|
TEST_F(SettingsValuesTest, findShouldThrowExceptionOnTypeMismatch)
|
||||||
{
|
{
|
||||||
Index index;
|
Index index;
|
||||||
|
|
|
@ -77,6 +77,15 @@ namespace Settings
|
||||||
static osg::Vec2f getVector2(std::string_view setting, std::string_view category);
|
static osg::Vec2f getVector2(std::string_view setting, std::string_view category);
|
||||||
static osg::Vec3f getVector3(std::string_view setting, std::string_view category);
|
static osg::Vec3f getVector3(std::string_view setting, std::string_view category);
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
static T getOrDefault(std::string_view setting, std::string_view category, const T& defaultValue)
|
||||||
|
{
|
||||||
|
const auto key = std::make_pair(category, setting);
|
||||||
|
if (!mUserSettings.contains(key) && !mDefaultSettings.contains(key))
|
||||||
|
return defaultValue;
|
||||||
|
return get<T>(setting, category);
|
||||||
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
static T get(std::string_view setting, std::string_view category)
|
static T get(std::string_view setting, std::string_view category)
|
||||||
{
|
{
|
||||||
|
|
|
@ -339,8 +339,8 @@ namespace Settings
|
||||||
std::unique_ptr<const Sanitizer<T>>&& sanitizer = nullptr)
|
std::unique_ptr<const Sanitizer<T>>&& sanitizer = nullptr)
|
||||||
: BaseSettingValue(getSettingValueType<T>(), category, name, index)
|
: BaseSettingValue(getSettingValueType<T>(), category, name, index)
|
||||||
, mSanitizer(std::move(sanitizer))
|
, mSanitizer(std::move(sanitizer))
|
||||||
, mDefaultValue(defaultValue)
|
, mDefaultValue(sanitize(defaultValue))
|
||||||
, mValue(defaultValue)
|
, mValue(sanitize(Settings::Manager::getOrDefault<T>(mName, mCategory, mDefaultValue)))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue