From b5d0804af0e63a794a0b540b005ce7212624be9b Mon Sep 17 00:00:00 2001 From: jayden <46307433+krmeet@users.noreply.github.com> Date: Tue, 26 Dec 2023 22:04:06 +0000 Subject: [PATCH 1/3] fix dumb extra space --- src/pc/lua/smlua.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pc/lua/smlua.h b/src/pc/lua/smlua.h index 0f55695b8..6cf32ca21 100644 --- a/src/pc/lua/smlua.h +++ b/src/pc/lua/smlua.h @@ -20,8 +20,8 @@ #include "pc/debuglog.h" #include "pc/djui/djui_console.h" -#define LOG_LUA(...) { if (!gSmLuaSuppressErrors) { _debuglog_print_log("LUA ", __FILE__), printf(__VA_ARGS__), printf("\n"), smlua_mod_error(), snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR); } } -#define LOG_LUA_LINE(...) { if (!gSmLuaSuppressErrors) { _debuglog_print_log("LUA ", __FILE__), printf(__VA_ARGS__), printf("\n"), smlua_mod_error(); snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR), smlua_logline(); } } +#define LOG_LUA(...) { if (!gSmLuaSuppressErrors) { _debuglog_print_log("LUA", __FILE__), printf(__VA_ARGS__), printf("\n"), smlua_mod_error(), snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR); } } +#define LOG_LUA_LINE(...) { if (!gSmLuaSuppressErrors) { _debuglog_print_log("LUA", __FILE__), printf(__VA_ARGS__), printf("\n"), smlua_mod_error(); snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR), smlua_logline(); } } #ifdef DEVELOPMENT #define LUA_STACK_CHECK_BEGIN() int __LUA_STACK_TOP = lua_gettop(gLuaState) @@ -48,4 +48,4 @@ void smlua_init(void); void smlua_update(void); void smlua_shutdown(void); -#endif \ No newline at end of file +#endif From eaba7e8010a60f60d6e9b75903f0643474dce023 Mon Sep 17 00:00:00 2001 From: jayden <46307433+krmeet@users.noreply.github.com> Date: Tue, 26 Dec 2023 23:11:02 +0000 Subject: [PATCH 2/3] fix lua error handling --- src/pc/lua/smlua.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/pc/lua/smlua.c b/src/pc/lua/smlua.c index 8216c6477..0a219f21e 100644 --- a/src/pc/lua/smlua.c +++ b/src/pc/lua/smlua.c @@ -26,11 +26,12 @@ void smlua_mod_error(void) { djui_lua_error(txt); } -int smlua_error_handler(UNUSED lua_State* L) { +int smlua_error_handler(lua_State* L) { if (lua_type(L, -1) == LUA_TSTRING) { LOG_LUA("%s", lua_tostring(L, -1)); } smlua_logline(); + smlua_dump_stack(); return 0; } @@ -176,7 +177,13 @@ static void smlua_load_script(struct Mod* mod, struct ModFile* file, u16 remoteI gSmLuaConvertSuccess = true; gLuaInitializingScript = 1; LOG_INFO("Loading lua script '%s'", file->cachedPath); - bool failed = (luaL_loadfile(L, file->cachedPath) != LUA_OK); + + if (luaL_loadfile(L, file->cachedPath) != LUA_OK) { // only run on success + LOG_LUA("Failed to load lua script '%s'.", file->cachedPath); + LOG_LUA("%s", smlua_to_string(L, lua_gettop(L))); + gLuaInitializingScript = 0; + return; + } // check if this is the first time this mod has been loaded lua_getfield(L, LUA_REGISTRYINDEX, mod->relativePath); @@ -214,22 +221,10 @@ static void smlua_load_script(struct Mod* mod, struct ModFile* file, u16 remoteI smlua_cobject_init_per_file_globals(mod->relativePath); } - // only run on success - if (failed) { - LOG_LUA("Failed to load lua script '%s'.", file->cachedPath); - LOG_LUA("%s", smlua_to_string(L, lua_gettop(L))); - gLuaInitializingScript = 0; - return; - } - // run chunks LOG_INFO("Executing '%s'", file->relativePath); if (smlua_pcall(L, 0, LUA_MULTRET, 0) != LUA_OK) { LOG_LUA("Failed to execute lua script '%s'.", file->cachedPath); - LOG_LUA("%s", smlua_to_string(L, lua_gettop(L))); - smlua_dump_stack(); - gLuaInitializingScript = 0; - return; } gLuaInitializingScript = 0; } From d4875cd1eae20506dad132c489394283baee8353 Mon Sep 17 00:00:00 2001 From: jayden <46307433+krmeet@users.noreply.github.com> Date: Tue, 26 Dec 2023 23:11:20 +0000 Subject: [PATCH 3/3] sacrifice debug info for readability --- src/pc/lua/smlua.h | 4 ++-- src/pc/lua/smlua_utils.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pc/lua/smlua.h b/src/pc/lua/smlua.h index 6cf32ca21..aad8d9a23 100644 --- a/src/pc/lua/smlua.h +++ b/src/pc/lua/smlua.h @@ -20,8 +20,8 @@ #include "pc/debuglog.h" #include "pc/djui/djui_console.h" -#define LOG_LUA(...) { if (!gSmLuaSuppressErrors) { _debuglog_print_log("LUA", __FILE__), printf(__VA_ARGS__), printf("\n"), smlua_mod_error(), snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR); } } -#define LOG_LUA_LINE(...) { if (!gSmLuaSuppressErrors) { _debuglog_print_log("LUA", __FILE__), printf(__VA_ARGS__), printf("\n"), smlua_mod_error(); snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR), smlua_logline(); } } +#define LOG_LUA(...) { if (!gSmLuaSuppressErrors) { printf("[LUA] "), printf(__VA_ARGS__), printf("\n"), smlua_mod_error(), snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR); } } +#define LOG_LUA_LINE(...) { if (!gSmLuaSuppressErrors) { printf("[LUA] "), printf(__VA_ARGS__), printf("\n"), smlua_mod_error(); snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR), smlua_logline(); } } #ifdef DEVELOPMENT #define LUA_STACK_CHECK_BEGIN() int __LUA_STACK_TOP = lua_gettop(gLuaState) diff --git a/src/pc/lua/smlua_utils.c b/src/pc/lua/smlua_utils.c index 011a809ae..56746f4d1 100644 --- a/src/pc/lua/smlua_utils.c +++ b/src/pc/lua/smlua_utils.c @@ -774,9 +774,9 @@ void smlua_logline(void) { int level = 0; while (lua_getstack(L, level, &info)) { lua_getinfo(L, "nSl", &info); - LOG_LUA(" [%d] %s:%d -- %s [%s]", + LOG_LUA("\t[%d] %s:%d -- %s [%s]", level, info.short_src, info.currentline, (info.name ? info.name : ""), info.what); ++level; } -} \ No newline at end of file +}