From e967e2cd1ee7b0423af876600ad2e2e605aa2be2 Mon Sep 17 00:00:00 2001 From: Motoo Chhotoo Chintoo <72434298+motoo-tobbler@users.noreply.github.com> Date: Tue, 22 Oct 2024 12:15:19 +0530 Subject: [PATCH] Functions to update Mod Menu elements (#385) Three new functions have been added, to be able to change the respective mod menu elements' values: - update_mod_menu_element_checkbox - update_mod_menu_element_slider - update_mod_menu_element_inputbox --- autogen/lua_definitions/manual.lua | 24 +++++++++ src/pc/lua/smlua_hooks.c | 78 ++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/autogen/lua_definitions/manual.lua b/autogen/lua_definitions/manual.lua index e501fc7d8..5f4478243 100644 --- a/autogen/lua_definitions/manual.lua +++ b/autogen/lua_definitions/manual.lua @@ -192,6 +192,30 @@ function update_mod_menu_element_name(index, name) -- ... end +--- @param index integer The index of the element in the order in which they were hooked +--- @param value boolean The boolean value to change to +--- Updates a mod menu checkbox element's boolean value +--- - NOTE: `index` is zero-indexed +function update_mod_menu_element_checkbox(index, value) + -- ... +end + +--- @param index integer The index of the element in the order in which they were hooked +--- @param value number The number value to change to +--- Updates a mod menu slider element's numerical value +--- - NOTE: `index` is zero-indexed +function update_mod_menu_element_slider(index, value) + -- ... +end + +--- @param index integer The index of the element in the order in which they were hooked +--- @param value string The text to change to +--- Updates a mod menu inputbox element's string value +--- - NOTE: `index` is zero-indexed +function update_mod_menu_element_inputbox(index, value) + -- ... +end + --------------- -- functions -- --------------- diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index 52e30a5b3..b6b6887f6 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -2180,6 +2180,81 @@ int smlua_update_mod_menu_element_name(lua_State* L) { return 1; } +int smlua_update_mod_menu_element_checkbox(lua_State* L) { + if (L == NULL) { return 0; } + if (!smlua_functions_valid_param_count(L, 2)) { return 0; } + + int index = smlua_to_integer(L, 1); + if (index >= gHookedModMenuElementsCount || !gSmLuaConvertSuccess) { + LOG_LUA_LINE("Update mod menu element: tried to update invalid element"); + return 0; + } + + if (gHookedModMenuElements[index].element != MOD_MENU_ELEMENT_CHECKBOX) { + LOG_LUA_LINE("Update mod menu element: element is not a checkbox."); + return 0; + } + + bool boolValue = smlua_to_boolean(L, 2); + if (!gSmLuaConvertSuccess) { + LOG_LUA_LINE("Update mod menu element: tried to update invalid element"); + return 0; + } + + gHookedModMenuElements[index].boolValue = boolValue; + return 1; +} + +int smlua_update_mod_menu_element_slider(lua_State* L) { + if (L == NULL) { return 0; } + if (!smlua_functions_valid_param_count(L, 2)) { return 0; } + + int index = smlua_to_integer(L, 1); + if (index >= gHookedModMenuElementsCount || !gSmLuaConvertSuccess) { + LOG_LUA_LINE("Update mod menu element: tried to update invalid element"); + return 0; + } + + if (gHookedModMenuElements[index].element != MOD_MENU_ELEMENT_SLIDER) { + LOG_LUA_LINE("Update mod menu element: element is not a slider."); + return 0; + } + + u32 uintValue = smlua_to_integer(L, 2); + if (!gSmLuaConvertSuccess) { + LOG_LUA_LINE("Update mod menu element: tried to update invalid element"); + return 0; + } + + gHookedModMenuElements[index].uintValue = uintValue; + return 1; +} + +int smlua_update_mod_menu_element_inputbox(lua_State* L) { + if (L == NULL) { return 0; } + if (!smlua_functions_valid_param_count(L, 2)) { return 0; } + + int index = smlua_to_integer(L, 1); + if (index >= gHookedModMenuElementsCount || !gSmLuaConvertSuccess) { + LOG_LUA_LINE("Update mod menu element: tried to update invalid element"); + return 0; + } + + if (gHookedModMenuElements[index].element != MOD_MENU_ELEMENT_INPUTBOX) { + LOG_LUA_LINE("Update mod menu element: element is not an inputbox."); + return 0; + } + + const char* stringValue = smlua_to_string(L, 2); + if (stringValue == NULL || strlen(stringValue) == 0 || !gSmLuaConvertSuccess) { + LOG_LUA_LINE("Update mod menu element: tried to update invalid element string"); + return 0; + } + + snprintf(gHookedModMenuElements[index].stringValue, gHookedModMenuElements[index].length, "%s", stringValue); + return 1; +} + void smlua_call_mod_menu_element_hook(struct LuaHookedModMenuElement* hooked, int index) { lua_State* L = gLuaState; if (L == NULL) { return; } @@ -2310,4 +2385,7 @@ void smlua_bind_hooks(void) { smlua_bind_function(L, "hook_mod_menu_inputbox", smlua_hook_mod_menu_inputbox); smlua_bind_function(L, "update_chat_command_description", smlua_update_chat_command_description); smlua_bind_function(L, "update_mod_menu_element_name", smlua_update_mod_menu_element_name); + smlua_bind_function(L, "update_mod_menu_element_checkbox", smlua_update_mod_menu_element_checkbox); + smlua_bind_function(L, "update_mod_menu_element_slider", smlua_update_mod_menu_element_slider); + smlua_bind_function(L, "update_mod_menu_element_inputbox", smlua_update_mod_menu_element_inputbox); }