add 'def' to settings info

This commit is contained in:
MihailRis 2024-10-30 11:50:39 +03:00
parent edad594101
commit a43002cf54
4 changed files with 39 additions and 0 deletions

View File

@ -57,6 +57,10 @@ public:
return value;
}
const T& getDefault() const {
return initial;
}
T& operator*() {
return value;
}

View File

@ -102,6 +102,26 @@ dv::value SettingsHandler::getValue(const std::string& name) const {
}
}
dv::value SettingsHandler::getDefault(const std::string& name) const {
auto found = map.find(name);
if (found == map.end()) {
throw std::runtime_error("setting '" + name + "' does not exist");
}
auto setting = found->second;
if (auto number = dynamic_cast<NumberSetting*>(setting)) {
return static_cast<number_t>(number->getDefault());
} else if (auto integer = dynamic_cast<IntegerSetting*>(setting)) {
return static_cast<integer_t>(integer->getDefault());
} else if (auto flag = dynamic_cast<FlagSetting*>(setting)) {
return flag->getDefault();
} else if (auto string = dynamic_cast<StringSetting*>(setting)) {
return string->getDefault();
} else {
throw std::runtime_error("type is not implemented for '" + name + "'");
}
}
std::string SettingsHandler::toString(const std::string& name) const {
auto found = map.find(name);
if (found == map.end()) {

View File

@ -22,6 +22,7 @@ public:
SettingsHandler(EngineSettings& settings);
dv::value getValue(const std::string& name) const;
dv::value getDefault(const std::string& name) const;
void setValue(const std::string& name, const dv::value& value);
std::string toString(const std::string& name) const;
Setting* getSetting(const std::string& name) const;

View File

@ -154,6 +154,8 @@ static int l_get_setting_info(lua::State* L) {
lua::setfield(L, "min");
lua::pushnumber(L, number->getMax());
lua::setfield(L, "max");
lua::pushnumber(L, number->getDefault());
lua::setfield(L, "def");
return 1;
}
if (auto integer = dynamic_cast<IntegerSetting*>(setting)) {
@ -161,6 +163,18 @@ static int l_get_setting_info(lua::State* L) {
lua::setfield(L, "min");
lua::pushinteger(L, integer->getMax());
lua::setfield(L, "max");
lua::pushinteger(L, integer->getDefault());
lua::setfield(L, "def");
return 1;
}
if (auto boolean = dynamic_cast<FlagSetting*>(setting)) {
lua::pushboolean(L, boolean->getDefault());
lua::setfield(L, "def");
return 1;
}
if (auto string = dynamic_cast<StringSetting*>(setting)) {
lua::pushstring(L, string->getDefault());
lua::setfield(L, "def");
return 1;
}
lua::pop(L);