Skip to content

Commit

Permalink
lua keybinds: stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiao921 committed May 28, 2024
1 parent 86ff7b6 commit 0e3280e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 41 deletions.
47 changes: 40 additions & 7 deletions src/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <codecvt>
#include <gui/widgets/imgui_hotkey.hpp>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}

Expand Down
81 changes: 50 additions & 31 deletions src/lua_extensions/bindings/hades/inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "string/string.hpp"

#include <hooks/hooking.hpp>
#include <lua/lua_manager.hpp>
#include <lua_extensions/lua_module_ext.hpp>
#include <memory/gm_address.hpp>

Expand Down Expand Up @@ -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<std::string, sol::coroutine> key_callbacks;
std::map<std::string, std::vector<sol::coroutine>> 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<void(uintptr_t)> 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
Expand All @@ -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;
Expand All @@ -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);
}
}
}

Expand All @@ -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<void(int32_t a1, int32_t, eastl::function<void(uintptr_t)> *, 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);
}
}
}
Expand All @@ -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<void(int32_t a1, int32_t, eastl::function<void(uintptr_t)> *, 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);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/lua_extensions/bindings/hades/inputs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, sol::coroutine> key_callbacks;
extern std::map<std::string, std::vector<sol::coroutine>> vanilla_key_callbacks;

void bind(sol::state_view &state, sol::table &lua_ext);
} // namespace lua::hades::inputs
2 changes: 1 addition & 1 deletion src/lua_extensions/lua_manager_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/lua_extensions/lua_module_ext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace big

std::vector<on_sjson_game_data_read_t> m_on_sjson_game_data_read;

std::unordered_map<std::string, sol::coroutine> m_keybinds;
std::map<std::string, std::vector<sol::coroutine>> m_keybinds;
};

class lua_module_ext : public lua_module
Expand Down

0 comments on commit 0e3280e

Please sign in to comment.