summaryrefslogtreecommitdiff
path: root/src/settings.cpp
diff options
context:
space:
mode:
authorSmallJoker <SmallJoker@users.noreply.github.com>2020-01-25 16:56:54 +0100
committerGitHub <noreply@github.com>2020-01-25 16:56:54 +0100
commitcde2a7f6f24f638421238ead4e61b155322fefc8 (patch)
tree96b6b1dd00a0c14660eea6f3f518b5d832dc1bd8 /src/settings.cpp
parent9cb3219f34be983e1b84a62a64c25e137d587365 (diff)
downloadminetest-cde2a7f6f24f638421238ead4e61b155322fefc8.tar.gz
minetest-cde2a7f6f24f638421238ead4e61b155322fefc8.tar.bz2
minetest-cde2a7f6f24f638421238ead4e61b155322fefc8.zip
Settings: Add get_flags API for mapgen flags (mg_flags, mgv6_spflags, ...) (#9284)
Unified flags handling in C++ and Lua Settings API -> Reading only, for now. Writing can be implemented later, if needed. API function to read the currently active flags -> was impossible from Lua Co-authored-by: Wuzzy <wuzzy2@mail.ru>
Diffstat (limited to 'src/settings.cpp')
-rw-r--r--src/settings.cpp65
1 files changed, 50 insertions, 15 deletions
diff --git a/src/settings.cpp b/src/settings.cpp
index c1fe41fa3..3728fabf2 100644
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -484,12 +484,33 @@ v3f Settings::getV3F(const std::string &name) const
u32 Settings::getFlagStr(const std::string &name, const FlagDesc *flagdesc,
u32 *flagmask) const
{
- std::string val = get(name);
- return std::isdigit(val[0])
- ? stoi(val)
- : readFlagString(val, flagdesc, flagmask);
-}
+ u32 flags = 0;
+ u32 mask_default = 0;
+
+ std::string value;
+ // Read default value (if there is any)
+ if (getDefaultNoEx(name, value)) {
+ flags = std::isdigit(value[0])
+ ? stoi(value)
+ : readFlagString(value, flagdesc, &mask_default);
+ }
+
+ // Apply custom flags "on top"
+ value = get(name);
+ u32 flags_user;
+ u32 mask_user = U32_MAX;
+ flags_user = std::isdigit(value[0])
+ ? stoi(value) // Override default
+ : readFlagString(value, flagdesc, &mask_user);
+ flags &= ~mask_user;
+ flags |= flags_user;
+
+ if (flagmask)
+ *flagmask = mask_default | mask_user;
+
+ return flags;
+}
// N.B. if getStruct() is used to read a non-POD aggregate type,
// the behavior is undefined.
@@ -736,19 +757,16 @@ bool Settings::getV3FNoEx(const std::string &name, v3f &val) const
}
-// N.B. getFlagStrNoEx() does not set val, but merely modifies it. Thus,
-// val must be initialized before using getFlagStrNoEx(). The intention of
-// this is to simplify modifying a flags field from a default value.
bool Settings::getFlagStrNoEx(const std::string &name, u32 &val,
- FlagDesc *flagdesc) const
+ const FlagDesc *flagdesc) const
{
- try {
- u32 flags, flagmask;
-
- flags = getFlagStr(name, flagdesc, &flagmask);
+ if (!flagdesc) {
+ if (!(flagdesc = getFlagDescFallback(name)))
+ return false; // Not found
+ }
- val &= ~flagmask;
- val |= flags;
+ try {
+ val = getFlagStr(name, flagdesc, nullptr);
return true;
} catch (SettingNotFoundException &e) {
@@ -873,6 +891,11 @@ bool Settings::setV3F(const std::string &name, v3f value)
bool Settings::setFlagStr(const std::string &name, u32 flags,
const FlagDesc *flagdesc, u32 flagmask)
{
+ if (!flagdesc) {
+ if (!(flagdesc = getFlagDescFallback(name)))
+ return false; // Not found
+ }
+
return set(name, writeFlagString(flags, flagdesc, flagmask));
}
@@ -1018,6 +1041,18 @@ void Settings::clearDefaultsNoLock()
m_defaults.clear();
}
+void Settings::setDefault(const std::string &name, const FlagDesc *flagdesc,
+ u32 flags)
+{
+ m_flags[name] = flagdesc;
+ setDefault(name, writeFlagString(flags, flagdesc, U32_MAX));
+}
+
+const FlagDesc *Settings::getFlagDescFallback(const std::string &name) const
+{
+ auto it = m_flags.find(name);
+ return it == m_flags.end() ? nullptr : it->second;
+}
void Settings::registerChangedCallback(const std::string &name,
SettingsChangedCallback cbf, void *userdata)