]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Settings: Push groups in to_table as well
authorSmallJoker <mk939@ymail.com>
Wed, 20 Jan 2021 15:58:59 +0000 (16:58 +0100)
committersfan5 <sfan5@live.de>
Mon, 1 Mar 2021 11:14:41 +0000 (12:14 +0100)
src/script/lua_api/l_settings.cpp
src/settings.h

index bcbaf15fa3e27cf2e8686f5066409c887c2fb898..85df8ab34bdcd13b8401f49766292b886e293afe 100644 (file)
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "lua_api/l_settings.h"
 #include "lua_api/l_internal.h"
 #include "cpp_api/s_security.h"
+#include "threading/mutex_auto_lock.h"
 #include "util/string.h" // FlagDesc
 #include "settings.h"
 #include "noise.h"
@@ -253,20 +254,36 @@ int LuaSettings::l_write(lua_State* L)
        return 1;
 }
 
-// to_table(self) -> {[key1]=value1,...}
-int LuaSettings::l_to_table(lua_State* L)
+static void push_settings_table(lua_State *L, const Settings *settings)
 {
-       NO_MAP_LOCK_REQUIRED;
-       LuaSettings* o = checkobject(L, 1);
-
-       std::vector<std::string> keys = o->m_settings->getNames();
-
+       std::vector<std::string> keys = settings->getNames();
        lua_newtable(L);
        for (const std::string &key : keys) {
-               lua_pushstring(L, o->m_settings->get(key).c_str());
+               std::string value;
+               Settings *group = nullptr;
+
+               if (settings->getNoEx(key, value)) {
+                       lua_pushstring(L, value.c_str());
+               } else if (settings->getGroupNoEx(key, group)) {
+                       // Recursively push tables
+                       push_settings_table(L, group);
+               } else {
+                       // Impossible case (multithreading) due to MutexAutoLock
+                       continue;
+               }
+
                lua_setfield(L, -2, key.c_str());
        }
+}
+
+// to_table(self) -> {[key1]=value1,...}
+int LuaSettings::l_to_table(lua_State* L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       LuaSettings* o = checkobject(L, 1);
 
+       MutexAutoLock(o->m_settings->m_mutex);
+       push_settings_table(L, o->m_settings);
        return 1;
 }
 
index b5e859ee0521111151812b4f62066c54138798f5..e22d949d32e9cd817c1b4eba5b73aad8ab8ad361 100644 (file)
@@ -239,6 +239,8 @@ class Settings {
 
        // Allow TestSettings to run sanity checks using private functions.
        friend class TestSettings;
+       // For sane mutex locking when iterating
+       friend class LuaSettings;
 
        void updateNoLock(const Settings &other);
        void clearNoLock();