From 0e3280e5dfe0d4d0b1b1369f43fdfd23bf68e8e7 Mon Sep 17 00:00:00 2001 From: xiaoxiao921 <837334+xiaoxiao921@users.noreply.github.com> Date: Tue, 28 May 2024 21:01:08 +0200 Subject: [PATCH] lua keybinds: stuff --- src/gui/gui.cpp | 47 ++++++++++-- src/lua_extensions/bindings/hades/inputs.cpp | 81 ++++++++++++-------- src/lua_extensions/bindings/hades/inputs.hpp | 2 +- src/lua_extensions/lua_manager_extension.cpp | 2 +- src/lua_extensions/lua_module_ext.hpp | 2 +- 5 files changed, 93 insertions(+), 41 deletions(-) diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 3f10f31..b8e50ff 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -4,6 +4,7 @@ #include "hooks/hooking.hpp" #include "lua/bindings/imgui_window.hpp" #include "lua_extensions/lua_manager_extension.hpp" +#include "lua_extensions/lua_module_ext.hpp" #include #include @@ -215,6 +216,45 @@ namespace big ImGui::EndMenu(); } + if (ImGui::BeginMenu("Keybinds")) + { + ImGui::Checkbox("Enable Vanilla Debug Keybinds", &lua::hades::inputs::enable_vanilla_debug_keybinds); + if (ImGui::TreeNode(lua::hades::inputs::enable_vanilla_debug_keybinds ? "Vanilla" : "Vanilla (Disabled)")) + { + for (const auto& [keybind, cbs] : lua::hades::inputs::vanilla_key_callbacks) + { + if (cbs.size()) + { + ImGui::Text("%s (%llu)", keybind.c_str(), cbs.size()); + } + } + + ImGui::TreePop(); + } + + std::scoped_lock guard(g_lua_manager->m_module_lock); + for (const auto& mod_ : g_lua_manager->m_modules) + { + auto mod = (big::lua_module_ext*)mod_.get(); + + if (mod->m_data_ext.m_keybinds.size()) + { + if (ImGui::TreeNode(mod->guid().c_str())) + { + for (const auto& [keybind, cbs] : mod->m_data_ext.m_keybinds) + { + if (cbs.size()) + { + ImGui::Text("%s (%llu)", keybind.c_str(), cbs.size()); + } + } + } + } + } + + ImGui::EndMenu(); + } + if (ImGui::BeginMenu("Windows")) { for (auto& [mod_guid, windows] : lua::window::is_open) @@ -259,13 +299,6 @@ namespace big ImGui::EndMenu(); } - if (ImGui::BeginMenu("Debug")) - { - ImGui::Checkbox("Enable Vanilla Debug Keybinds", &lua::hades::inputs::enable_vanilla_debug_keybinds); - - ImGui::EndMenu(); - } - ImGui::EndMainMenuBar(); } diff --git a/src/lua_extensions/bindings/hades/inputs.cpp b/src/lua_extensions/bindings/hades/inputs.cpp index 955ffca..2cc173c 100644 --- a/src/lua_extensions/bindings/hades/inputs.cpp +++ b/src/lua_extensions/bindings/hades/inputs.cpp @@ -5,6 +5,7 @@ #include "string/string.hpp" #include +#include #include #include @@ -305,41 +306,50 @@ namespace lua::hades::inputs bool enable_vanilla_debug_keybinds = false; bool let_game_input_go_through_gui_layer = true; - std::unordered_map key_callbacks; + std::map> vanilla_key_callbacks; static gmAddress RegisterDebugKey{}; - static void invoke_key_callback(uintptr_t mCallback) + static void invoke_debug_key_callback(uintptr_t mCallback) { // offset to get mName from DebugAction eastl_basic_string_view_char *mName = (eastl_basic_string_view_char *)(mCallback - 0x18); - const auto it_callback = key_callbacks.find(mName->get_text()); - if (it_callback != key_callbacks.end()) - { - LOG(DEBUG) << it_callback->first; - it_callback->second(); - } - } - static void invoke_vanilla_debug_key_callback(uintptr_t mCallback) - { - if (!enable_vanilla_debug_keybinds) + if (enable_vanilla_debug_keybinds) { - return; + const auto it_callback = vanilla_key_callbacks.find(mName->get_text()); + if (it_callback != vanilla_key_callbacks.end()) + { + LOG(DEBUG) << it_callback->first << " (Vanilla)"; + + for (auto &cb : it_callback->second) + { + cb(); + } + } } - invoke_key_callback(mCallback); - } + std::scoped_lock guard(big::g_lua_manager->m_module_lock); + for (const auto &mod_ : big::g_lua_manager->m_modules) + { + auto mod = (big::lua_module_ext *)mod_.get(); + const auto it_callback = mod->m_data_ext.m_keybinds.find(mName->get_text()); + if (it_callback != mod->m_data_ext.m_keybinds.end()) + { + LOG(DEBUG) << it_callback->first << " (" << mod->guid() << ")"; - static void invoke_plugin_debug_key_callback(uintptr_t mCallback) - { - invoke_key_callback(mCallback); + for (auto &cb : it_callback->second) + { + cb(); + } + } + } } - static void parse_and_register_keybind(const std::string &keybind, const sol::coroutine &callback, auto &RegisterDebugKey, void *invoke_callback) + static void parse_and_register_keybind(std::string &keybind, const sol::coroutine &callback, auto &RegisterDebugKey, bool is_vanilla, big::lua_module_ext *mod) { eastl::function funcy; funcy.mMgrFuncPtr = nullptr; - funcy.mInvokeFuncPtr = (decltype(funcy.mInvokeFuncPtr))invoke_callback; + funcy.mInvokeFuncPtr = invoke_debug_key_callback; eastl_basic_string_view_char callback_name{}; callback_name.mRemainingSizeField = (char)129; // TODO: this is leaking @@ -355,8 +365,6 @@ namespace lua::hades::inputs int32_t key = 0; if (keybind.size()) { - LOG(INFO) << "keybind: " << keybind; - if (keybind.contains("Control")) { key_modifier |= sgg::KeyModifier::Ctrl; @@ -370,12 +378,27 @@ namespace lua::hades::inputs key_modifier |= sgg::KeyModifier::Alt; } + if (!keybind.contains(' ')) + { + keybind = std::format(" {}", keybind); + } + std::string key_str = big::string::split(keybind, ' ')[1]; auto it_key = sgg::key_map.find(key_str); if (it_key != sgg::key_map.end()) { - key = it_key->second; - key_callbacks[keybind] = callback; + key = it_key->second; + + if (is_vanilla) + { + LOG(DEBUG) << "Vanilla Keybind Registered: " << keybind; + vanilla_key_callbacks[keybind].push_back(callback); + } + else if (mod) + { + LOG(INFO) << mod->guid() << " Keybind Registered: " << keybind; + mod->m_data_ext.m_keybinds[keybind].push_back(callback); + } } } @@ -387,19 +410,15 @@ namespace lua::hades::inputs // Name: on_key_pressed // Param: keybind: string: The key binding string representing the key that, when pressed, will trigger the callback function. The format used is the one used by the vanilla game, please check the vanilla scripts using "OnKeyPressed". // Param: callback: function: The function to be called when the specified keybind is pressed. - static void on_key_pressed(const std::string &keybind, sol::coroutine cb, sol::this_environment env) + static void on_key_pressed(std::string keybind, sol::coroutine cb, sol::this_environment env) { auto mod = (big::lua_module_ext *)big::lua_module::this_from(env); if (mod) { - mod->m_data_ext.m_keybinds.emplace(keybind, cb); - - key_callbacks[keybind] = cb; - if (RegisterDebugKey) { auto RegisterDebugKey_good_type = RegisterDebugKey.as_func *, eastl_basic_string_view_char *, void *, void *, bool, eastl_basic_string_view_char *, eastl_basic_string_view_char *, bool)>(); - parse_and_register_keybind(keybind, cb, RegisterDebugKey_good_type, invoke_plugin_debug_key_callback); + parse_and_register_keybind(keybind, cb, RegisterDebugKey_good_type, false, mod); } } } @@ -424,7 +443,7 @@ namespace lua::hades::inputs if (keybind_opt.has_value() && keybind_opt->size() && callback_opt.has_value() && callback_opt->valid()) { auto RegisterDebugKey_good_type = RegisterDebugKey.as_func *, eastl_basic_string_view_char *, void *, void *, bool, eastl_basic_string_view_char *, eastl_basic_string_view_char *, bool)>(); - parse_and_register_keybind(*keybind_opt, *callback_opt, RegisterDebugKey_good_type, invoke_vanilla_debug_key_callback); + parse_and_register_keybind(*keybind_opt, *callback_opt, RegisterDebugKey_good_type, true, nullptr); } } }; diff --git a/src/lua_extensions/bindings/hades/inputs.hpp b/src/lua_extensions/bindings/hades/inputs.hpp index a149b37..915cb11 100644 --- a/src/lua_extensions/bindings/hades/inputs.hpp +++ b/src/lua_extensions/bindings/hades/inputs.hpp @@ -5,7 +5,7 @@ namespace lua::hades::inputs extern bool enable_vanilla_debug_keybinds; extern bool let_game_input_go_through_gui_layer; - extern std::unordered_map key_callbacks; + extern std::map> vanilla_key_callbacks; void bind(sol::state_view &state, sol::table &lua_ext); } // namespace lua::hades::inputs diff --git a/src/lua_extensions/lua_manager_extension.cpp b/src/lua_extensions/lua_manager_extension.cpp index 25402d6..c8c6b15 100644 --- a/src/lua_extensions/lua_manager_extension.cpp +++ b/src/lua_extensions/lua_manager_extension.cpp @@ -17,7 +17,7 @@ namespace big::lua_manager_extension { std::scoped_lock l(g_manager_mutex); - lua::hades::inputs::key_callbacks.clear(); + lua::hades::inputs::vanilla_key_callbacks.clear(); g_is_lua_state_valid = false; diff --git a/src/lua_extensions/lua_module_ext.hpp b/src/lua_extensions/lua_module_ext.hpp index eb62e97..e82f36e 100644 --- a/src/lua_extensions/lua_module_ext.hpp +++ b/src/lua_extensions/lua_module_ext.hpp @@ -20,7 +20,7 @@ namespace big std::vector m_on_sjson_game_data_read; - std::unordered_map m_keybinds; + std::map> m_keybinds; }; class lua_module_ext : public lua_module