From 8cce192dabffbdfe3421ced739552bb1afdbff1e Mon Sep 17 00:00:00 2001 From: Lukasz Papierkowski Date: Tue, 24 Dec 2024 10:52:52 +0100 Subject: [PATCH] Lua: Fix multilevel tree traversal in connectHooks function Previously, only leaf nodes from the first branch of the tree were processed. * Handled: root.branch1.leafA, root.branch1.leafB * Skipped: root.branch2.leafC, etc. This change ensures all branches are traversed so no leaf nodes are skipped. The fix iterates over each key in the table and, for sub-tables, recursively calls connectHooks instead of returning early. Change-Id: I17182f4000e83e5d4747e71573d5ac2d6558d7e7 Reviewed-by: Marcus Tillmanns --- src/plugins/lua/luaengine.cpp | 44 +++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/plugins/lua/luaengine.cpp b/src/plugins/lua/luaengine.cpp index a241ed428fe..9fb707cb9ca 100644 --- a/src/plugins/lua/luaengine.cpp +++ b/src/plugins/lua/luaengine.cpp @@ -230,18 +230,38 @@ expected_str connectHooks( qCDebug(logLuaEngine) << "connectHooks called with path: " << path; for (const auto &[k, v] : table) { - qCDebug(logLuaEngine) << "Processing key: " << k.as(); - if (v.get_type() == sol::type::table) { - return connectHooks( - lua, v.as(), QStringList{path, k.as()}.join("."), guard); - } else if (v.get_type() == sol::type::function) { - QString hookName = QStringList{path, k.as()}.join("."); - qCDebug(logLuaEngine) << "Connecting function to hook: " << hookName; - auto it = d->m_hooks.find(hookName); - if (it == d->m_hooks.end()) - return make_unexpected(Tr::tr("No hook with the name \"%1\" found.").arg(hookName)); - else - it.value()(v.as(), guard); + if (k.get_type() != sol::type::string) + return make_unexpected( + Tr::tr("Non-string key encountered in Lua table at path \"%1\"").arg(path)); + + const auto keyName = k.as(); + const auto currentPath = QStringList{path, keyName}.join("."); + qCDebug(logLuaEngine) << "Processing path:" << currentPath; + + switch (v.get_type()) { + case sol::type::table: { + auto result = connectHooks(lua, v.as(), currentPath, guard); + if (!result) + return result; + break; + } + case sol::type::function: { + qCDebug(logLuaEngine) << "Connecting function to hook:" << currentPath; + + auto it = d->m_hooks.find(currentPath); + if (it == d->m_hooks.end()) { + return make_unexpected( + Tr::tr("No hook with the name \"%1\" found.").arg(currentPath)); + } + + it.value()(v.as(), guard); + break; + } + default: { + return make_unexpected(Tr::tr("Unsupported value type \"%1\" at path \"%2\".") + .arg(static_cast(v.get_type())) + .arg(currentPath)); + } } }