From a30132f85c0c27115c7d08f9c1858c2ea40db60b Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Thu, 30 Jun 2022 21:54:09 +0200 Subject: [PATCH] Fix lui keydown & keyup events --- src/client/component/input.cpp | 39 ++++++++++++++++++--------- src/client/component/ui_scripting.cpp | 11 ++++++++ src/client/game/symbols.hpp | 3 +++ 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/client/component/input.cpp b/src/client/component/input.cpp index 3f34bf2e..e1c98234 100644 --- a/src/client/component/input.cpp +++ b/src/client/component/input.cpp @@ -22,6 +22,7 @@ namespace input utils::hook::detour cl_char_event_hook; utils::hook::detour cl_key_event_hook; utils::hook::detour cl_mouse_move_hook; + utils::hook::detour lui_cod_key_event_hook; void cl_char_event_stub(const int local_client_num, const int key) { @@ -35,12 +36,6 @@ namespace input return; } - ui_scripting::notify("keypress", - { - {"keynum", key}, - {"key", game::Key_KeynumToString(key, 0, 1)}, - }); - cl_char_event_hook.invoke(local_client_num, key); } @@ -56,16 +51,22 @@ namespace input return; } - if (!(*game::keyCatchers & 1) && !(*game::keyCatchers & 0x10)) + cl_key_event_hook.invoke(local_client_num, key, down); + } + + void lui_cod_key_event_stub(const int local_client_num, const int a2, const int key, const int down) + { + const auto state = *game::hks::lua_state; + if (game::LUI_BeginCachedEvent(local_client_num, down ? 3 : 4, state)) { - ui_scripting::notify(down ? "keydown" : "keyup", - { - {"keynum", key}, - {"key", game::Key_KeynumToString(key, 0, 1)}, - }); + const auto key_str = game::Key_KeynumToString(key, 0, 1); + game::LUI_SetTableInt("keynum", key, state); + game::LUI_SetTableString("key", key_str, state); + game::LUI_SetTableString("name", down ? "keydown" : "keyup", state); + game::LUI_EndEvent(state); } - cl_key_event_hook.invoke(local_client_num, key, down); + lui_cod_key_event_hook.invoke(local_client_num, a2, key, down); } void cl_mouse_move_stub(const int local_client_num, int x, int y) @@ -84,9 +85,21 @@ namespace input public: void post_unpack() override { + static const char* lui_cached_events[5] = + { + "process_events", + "gamepad_button", + "transition_complete", + "keydown", + "keyup", + }; + + utils::hook::inject(0x14031EB8B, lui_cached_events); + cl_char_event_hook.create(0x1403D27B0, cl_char_event_stub); cl_key_event_hook.create(0x1403D2AE0, cl_key_event_stub); cl_mouse_move_hook.create(0x1403296F0, cl_mouse_move_stub); + lui_cod_key_event_hook.create(0x140328F50, lui_cod_key_event_stub); } }; } diff --git a/src/client/component/ui_scripting.cpp b/src/client/component/ui_scripting.cpp index 09cb740a..e72cfd5a 100644 --- a/src/client/component/ui_scripting.cpp +++ b/src/client/component/ui_scripting.cpp @@ -332,6 +332,17 @@ namespace ui_scripting const auto lua = get_globals(); lua["EnableGlobals"](); + table keydown_event{}; + keydown_event["key"] = ""; + keydown_event["keynum"] = 0; + + table keyup_event{}; + keyup_event["key"] = ""; + keyup_event["keynum"] = 0; + + lua["LUI"]["CachedEvents"]["keydown"] = keydown_event; + lua["LUI"]["CachedEvents"]["keyup"] = keyup_event; + setup_functions(); lua["print"] = function(reinterpret_cast(0x1402B81C0)); diff --git a/src/client/game/symbols.hpp b/src/client/game/symbols.hpp index 1ece7892..7f0c6c27 100644 --- a/src/client/game/symbols.hpp +++ b/src/client/game/symbols.hpp @@ -91,6 +91,9 @@ namespace game WEAK symbol LUI_OpenMenu{0x1405F0EE0}; WEAK symbol LUI_BeginEvent{0x1403155E0}; + WEAK symbol LUI_BeginCachedEvent{0x1403153E0}; + WEAK symbol LUI_SetTableInt{0x140320060}; + WEAK symbol LUI_SetTableString{0x1403201F0}; WEAK symbol LUI_EndEvent{0x140316890}; WEAK symbol LUI_EnterCriticalSection{0x140316980}; WEAK symbol LUI_LeaveCriticalSection{0x14031BC20};