From b32ab212a952e5bb50d27f8656f3d2d5d8d1848e Mon Sep 17 00:00:00 2001 From: Agent X <44549182+Agent-11@users.noreply.github.com> Date: Wed, 8 Mar 2023 18:54:06 -0500 Subject: [PATCH 1/3] smlua_exec_str (run Lua from string) Seemed useful for debug or any other purposes, why not? --- autogen/lua_definitions/functions.lua | 6 ++++++ docs/lua/functions-4.md | 20 ++++++++++++++++++++ docs/lua/functions.md | 1 + src/pc/lua/smlua.c | 3 ++- src/pc/lua/smlua_functions_autogen.c | 18 ++++++++++++++++++ src/pc/lua/utils/smlua_misc_utils.h | 2 ++ 6 files changed, 49 insertions(+), 1 deletion(-) diff --git a/autogen/lua_definitions/functions.lua b/autogen/lua_definitions/functions.lua index af7a8f1dd..50a2330f8 100644 --- a/autogen/lua_definitions/functions.lua +++ b/autogen/lua_definitions/functions.lua @@ -8418,6 +8418,12 @@ function set_override_near(near) -- ... end +--- @param str string +--- @return nil +function smlua_exec_str(str) + -- ... +end + --- @param name string --- @return integer function smlua_model_util_get_id(name) diff --git a/docs/lua/functions-4.md b/docs/lua/functions-4.md index ffe2aaa1e..3ff0ece0e 100644 --- a/docs/lua/functions-4.md +++ b/docs/lua/functions-4.md @@ -8658,6 +8658,26 @@
+## [smlua_exec_str](#smlua_exec_str) + +### Lua Example +`smlua_exec_str(str)` + +### Parameters +| Field | Type | +| ----- | ---- | +| str | `string` | + +### Returns +- None + +### C Prototype +`void smlua_exec_str(const char* str);` + +[:arrow_up_small:](#) + +
+ --- # functions from smlua_model_utils.h diff --git a/docs/lua/functions.md b/docs/lua/functions.md index 7e9ae5cac..200fe5680 100644 --- a/docs/lua/functions.md +++ b/docs/lua/functions.md @@ -1560,6 +1560,7 @@ - [set_override_far](functions-4.md#set_override_far) - [set_override_fov](functions-4.md#set_override_fov) - [set_override_near](functions-4.md#set_override_near) + - [smlua_exec_str](functions-4.md#smlua_exec_str)
diff --git a/src/pc/lua/smlua.c b/src/pc/lua/smlua.c index f8abc8972..f13c1fb8f 100644 --- a/src/pc/lua/smlua.c +++ b/src/pc/lua/smlua.c @@ -7,6 +7,7 @@ #include "pc/lua/utils/smlua_audio_utils.h" #include "pc/lua/utils/smlua_model_utils.h" #include "pc/lua/utils/smlua_level_utils.h" +#include "pc/lua/utils/smlua_misc_utils.h" #include "pc/djui/djui.h" lua_State* gLuaState = NULL; @@ -52,7 +53,7 @@ static void smlua_exec_file(char* path) { lua_pop(L, lua_gettop(L)); } -static void smlua_exec_str(char* str) { +void smlua_exec_str(const char* str) { lua_State* L = gLuaState; if (luaL_dostring(L, str) != LUA_OK) { LOG_LUA("Failed to load lua string."); diff --git a/src/pc/lua/smlua_functions_autogen.c b/src/pc/lua/smlua_functions_autogen.c index 9cb90305c..2427181fe 100644 --- a/src/pc/lua/smlua_functions_autogen.c +++ b/src/pc/lua/smlua_functions_autogen.c @@ -27411,6 +27411,23 @@ int smlua_func_set_override_near(lua_State* L) { return 1; } +int smlua_func_smlua_exec_str(lua_State* L) { + if (L == NULL) { return 0; } + + int top = lua_gettop(L); + if (top != 1) { + LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "smlua_exec_str", 1, top); + return 0; + } + + const char* str = smlua_to_string(L, 1); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "smlua_exec_str"); return 0; } + + smlua_exec_str(str); + + return 1; +} + ///////////////////////// // smlua_model_utils.h // ///////////////////////// @@ -30370,6 +30387,7 @@ void smlua_bind_functions_autogen(void) { smlua_bind_function(L, "set_override_far", smlua_func_set_override_far); smlua_bind_function(L, "set_override_fov", smlua_func_set_override_fov); smlua_bind_function(L, "set_override_near", smlua_func_set_override_near); + smlua_bind_function(L, "smlua_exec_str", smlua_func_smlua_exec_str); // smlua_model_utils.h smlua_bind_function(L, "smlua_model_util_get_id", smlua_func_smlua_model_util_get_id); diff --git a/src/pc/lua/utils/smlua_misc_utils.h b/src/pc/lua/utils/smlua_misc_utils.h index 42537f402..b157f01d9 100644 --- a/src/pc/lua/utils/smlua_misc_utils.h +++ b/src/pc/lua/utils/smlua_misc_utils.h @@ -96,4 +96,6 @@ void play_transition(s16 transType, s16 time, u8 red, u8 green, u8 blue); bool course_is_main_course(u16 levelNum); +void smlua_exec_str(const char* str); + #endif From 36bda742cd2e0837e6f9edf52995366bc37260ad Mon Sep 17 00:00:00 2001 From: Agent X <44549182+Agent-11@users.noreply.github.com> Date: Wed, 8 Mar 2023 19:04:32 -0500 Subject: [PATCH 2/3] Revert "smlua_exec_str (run Lua from string)" This reverts commit b32ab212a952e5bb50d27f8656f3d2d5d8d1848e. --- autogen/lua_definitions/functions.lua | 6 ------ docs/lua/functions-4.md | 20 -------------------- docs/lua/functions.md | 1 - src/pc/lua/smlua.c | 3 +-- src/pc/lua/smlua_functions_autogen.c | 18 ------------------ src/pc/lua/utils/smlua_misc_utils.h | 2 -- 6 files changed, 1 insertion(+), 49 deletions(-) diff --git a/autogen/lua_definitions/functions.lua b/autogen/lua_definitions/functions.lua index 50a2330f8..af7a8f1dd 100644 --- a/autogen/lua_definitions/functions.lua +++ b/autogen/lua_definitions/functions.lua @@ -8418,12 +8418,6 @@ function set_override_near(near) -- ... end ---- @param str string ---- @return nil -function smlua_exec_str(str) - -- ... -end - --- @param name string --- @return integer function smlua_model_util_get_id(name) diff --git a/docs/lua/functions-4.md b/docs/lua/functions-4.md index 3ff0ece0e..ffe2aaa1e 100644 --- a/docs/lua/functions-4.md +++ b/docs/lua/functions-4.md @@ -8658,26 +8658,6 @@
-## [smlua_exec_str](#smlua_exec_str) - -### Lua Example -`smlua_exec_str(str)` - -### Parameters -| Field | Type | -| ----- | ---- | -| str | `string` | - -### Returns -- None - -### C Prototype -`void smlua_exec_str(const char* str);` - -[:arrow_up_small:](#) - -
- --- # functions from smlua_model_utils.h diff --git a/docs/lua/functions.md b/docs/lua/functions.md index 200fe5680..7e9ae5cac 100644 --- a/docs/lua/functions.md +++ b/docs/lua/functions.md @@ -1560,7 +1560,6 @@ - [set_override_far](functions-4.md#set_override_far) - [set_override_fov](functions-4.md#set_override_fov) - [set_override_near](functions-4.md#set_override_near) - - [smlua_exec_str](functions-4.md#smlua_exec_str)
diff --git a/src/pc/lua/smlua.c b/src/pc/lua/smlua.c index f13c1fb8f..f8abc8972 100644 --- a/src/pc/lua/smlua.c +++ b/src/pc/lua/smlua.c @@ -7,7 +7,6 @@ #include "pc/lua/utils/smlua_audio_utils.h" #include "pc/lua/utils/smlua_model_utils.h" #include "pc/lua/utils/smlua_level_utils.h" -#include "pc/lua/utils/smlua_misc_utils.h" #include "pc/djui/djui.h" lua_State* gLuaState = NULL; @@ -53,7 +52,7 @@ static void smlua_exec_file(char* path) { lua_pop(L, lua_gettop(L)); } -void smlua_exec_str(const char* str) { +static void smlua_exec_str(char* str) { lua_State* L = gLuaState; if (luaL_dostring(L, str) != LUA_OK) { LOG_LUA("Failed to load lua string."); diff --git a/src/pc/lua/smlua_functions_autogen.c b/src/pc/lua/smlua_functions_autogen.c index 2427181fe..9cb90305c 100644 --- a/src/pc/lua/smlua_functions_autogen.c +++ b/src/pc/lua/smlua_functions_autogen.c @@ -27411,23 +27411,6 @@ int smlua_func_set_override_near(lua_State* L) { return 1; } -int smlua_func_smlua_exec_str(lua_State* L) { - if (L == NULL) { return 0; } - - int top = lua_gettop(L); - if (top != 1) { - LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "smlua_exec_str", 1, top); - return 0; - } - - const char* str = smlua_to_string(L, 1); - if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "smlua_exec_str"); return 0; } - - smlua_exec_str(str); - - return 1; -} - ///////////////////////// // smlua_model_utils.h // ///////////////////////// @@ -30387,7 +30370,6 @@ void smlua_bind_functions_autogen(void) { smlua_bind_function(L, "set_override_far", smlua_func_set_override_far); smlua_bind_function(L, "set_override_fov", smlua_func_set_override_fov); smlua_bind_function(L, "set_override_near", smlua_func_set_override_near); - smlua_bind_function(L, "smlua_exec_str", smlua_func_smlua_exec_str); // smlua_model_utils.h smlua_bind_function(L, "smlua_model_util_get_id", smlua_func_smlua_model_util_get_id); diff --git a/src/pc/lua/utils/smlua_misc_utils.h b/src/pc/lua/utils/smlua_misc_utils.h index b157f01d9..42537f402 100644 --- a/src/pc/lua/utils/smlua_misc_utils.h +++ b/src/pc/lua/utils/smlua_misc_utils.h @@ -96,6 +96,4 @@ void play_transition(s16 transType, s16 time, u8 red, u8 green, u8 blue); bool course_is_main_course(u16 levelNum); -void smlua_exec_str(const char* str); - #endif From 0c7de174d21ab8556cbacc49843c179a848cc348 Mon Sep 17 00:00:00 2001 From: Agent X <44549182+Agent-11@users.noreply.github.com> Date: Wed, 8 Mar 2023 19:26:03 -0500 Subject: [PATCH 3/3] Add /lua to chat commands with dev flag --- src/pc/chat_commands.c | 6 ++++++ src/pc/lua/smlua.c | 2 +- src/pc/lua/smlua.h | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/pc/chat_commands.c b/src/pc/chat_commands.c index df498d7d2..becb4b2db 100644 --- a/src/pc/chat_commands.c +++ b/src/pc/chat_commands.c @@ -312,6 +312,11 @@ bool exec_chat_command(char* command) { djui_chat_message_create(message); return true; } + + if (str_starts_with("/lua ", command)) { + smlua_exec_str(&command[5]); + return true; + } #endif return smlua_call_chat_command_hook(command); @@ -327,6 +332,7 @@ void display_chat_commands(void) { } #if defined(DEVELOPMENT) djui_chat_message_create("/warp [LEVEL] [AREA] [ACT] - Level can be either a numeric value or a shorthand name"); + djui_chat_message_create("/lua [LUA] - Execute Lua code from a string"); #endif if (sConfirming != CCC_NONE) { djui_chat_message_create("/confirm"); } smlua_display_chat_commands(); diff --git a/src/pc/lua/smlua.c b/src/pc/lua/smlua.c index f8abc8972..57c9cf4e7 100644 --- a/src/pc/lua/smlua.c +++ b/src/pc/lua/smlua.c @@ -52,7 +52,7 @@ static void smlua_exec_file(char* path) { lua_pop(L, lua_gettop(L)); } -static void smlua_exec_str(char* str) { +void smlua_exec_str(char* str) { lua_State* L = gLuaState; if (luaL_dostring(L, str) != LUA_OK) { LOG_LUA("Failed to load lua string."); diff --git a/src/pc/lua/smlua.h b/src/pc/lua/smlua.h index d757f207a..49a4517ea 100644 --- a/src/pc/lua/smlua.h +++ b/src/pc/lua/smlua.h @@ -40,6 +40,7 @@ extern struct Mod* gLuaLastHookMod; void smlua_mod_error(void); int smlua_error_handler(UNUSED lua_State* L); int smlua_pcall(lua_State* L, int nargs, int nresults, int errfunc); +void smlua_exec_str(char* str); void smlua_init(void); void smlua_update(void);