From 12ea360360bdc6b7669ba3da1ab711dddb046af2 Mon Sep 17 00:00:00 2001 From: MysterD Date: Sat, 16 Apr 2022 22:47:16 -0700 Subject: [PATCH] Display a message on screen when script errors are found --- data/dynos_mgr_actor.cpp | 2 +- src/pc/djui/djui.c | 24 ++++++++++++++++++++++++ src/pc/djui/djui.h | 1 + src/pc/lua/smlua.c | 15 +++++++++++++++ src/pc/lua/smlua.h | 5 ++++- src/pc/lua/smlua_hooks.c | 2 ++ src/pc/lua/smlua_sync_table.c | 1 + 7 files changed, 48 insertions(+), 2 deletions(-) diff --git a/data/dynos_mgr_actor.cpp b/data/dynos_mgr_actor.cpp index 629615dd0..f6ee4a366 100644 --- a/data/dynos_mgr_actor.cpp +++ b/data/dynos_mgr_actor.cpp @@ -23,7 +23,7 @@ void DynOS_Actor_AddCustom(const SysPath &aFilename, const char *aActorName) { GfxData *_GfxData = DynOS_Actor_LoadFromBinary(aFilename, actorName, aFilename); if (!_GfxData) { - Print(" ERROR: Couldn't load Actor Binary \"%s\" from \"%s\"", actorName, aPackFolder.c_str()); + Print(" ERROR: Couldn't load Actor Binary \"%s\" from \"%s\"", actorName, aFilename.c_str()); free(actorName); return; } diff --git a/src/pc/djui/djui.c b/src/pc/djui/djui.c index 3c8834c91..e63467e7d 100644 --- a/src/pc/djui/djui.c +++ b/src/pc/djui/djui.c @@ -10,6 +10,8 @@ static Gfx* sSavedDisplayListHead = NULL; struct DjuiRoot* gDjuiRoot = NULL; static struct DjuiText* sDjuiPauseOptions = NULL; +static struct DjuiText* sDjuiLuaError = NULL; +static u32 sDjuiLuaErrorTimeout = 0; bool gDjuiInMainMenu = true; bool gDjuiDisabled = false; @@ -24,6 +26,15 @@ void djui_init(void) { djui_text_set_alignment(sDjuiPauseOptions, DJUI_HALIGN_CENTER, DJUI_VALIGN_CENTER); djui_base_set_visible(&sDjuiPauseOptions->base, false); + sDjuiLuaError = djui_text_create(&gDjuiRoot->base, ""); + djui_base_set_size_type(&sDjuiLuaError->base, DJUI_SVT_RELATIVE, DJUI_SVT_ABSOLUTE); + djui_base_set_size(&sDjuiLuaError->base, 1.0f, 32); + djui_base_set_location(&sDjuiLuaError->base, 0, 64); + djui_text_set_drop_shadow(sDjuiLuaError, 0, 0, 0, 255); + djui_text_set_alignment(sDjuiLuaError, DJUI_HALIGN_CENTER, DJUI_VALIGN_CENTER); + djui_base_set_visible(&sDjuiLuaError->base, false); + djui_base_set_color(&sDjuiLuaError->base, 255, 0, 0, 255); + djui_panel_playerlist_create(NULL); if (gCLIOpts.Network != NT_SERVER) { @@ -41,6 +52,12 @@ void djui_connect_menu_open(void) { djui_panel_join_message_create(NULL); } +void djui_lua_error(char* text) { + djui_text_set_text(sDjuiLuaError, text); + djui_base_set_visible(&sDjuiLuaError->base, true); + sDjuiLuaErrorTimeout = 30 * 5; +} + void djui_render_patch(void) { // reset the head and re-render DJUI if (sSavedDisplayListHead == NULL) { return; } @@ -67,6 +84,13 @@ void djui_render(void) { djui_base_render(&gDjuiRoot->base); } + if (sDjuiLuaErrorTimeout > 0) { + sDjuiLuaErrorTimeout--; + if (sDjuiLuaErrorTimeout == 0) { + djui_base_set_visible(&sDjuiLuaError->base, false); + } + } + djui_cursor_update(); djui_interactable_update(); } diff --git a/src/pc/djui/djui.h b/src/pc/djui/djui.h index 840cc65fa..d35562552 100644 --- a/src/pc/djui/djui.h +++ b/src/pc/djui/djui.h @@ -65,5 +65,6 @@ extern bool gDjuiDisabled; void djui_init(void); void djui_connect_menu_open(void); +void djui_lua_error(char* text); void djui_render_patch(void); void djui_render(void); diff --git a/src/pc/lua/smlua.c b/src/pc/lua/smlua.c index 3f27dbae9..d72c33ef6 100644 --- a/src/pc/lua/smlua.c +++ b/src/pc/lua/smlua.c @@ -4,11 +4,22 @@ #include "pc/crash_handler.h" #include "pc/lua/utils/smlua_text_utils.h" #include "pc/lua/utils/smlua_audio_utils.h" +#include "pc/djui/djui.h" lua_State* gLuaState = NULL; u8 gLuaInitializingScript = 0; struct Mod* gLuaLoadingMod = NULL; struct Mod* gLuaActiveMod = NULL; +struct Mod* gLuaLastHookMod = NULL; + +void smlua_mod_error(void) { + struct Mod* mod = gLuaActiveMod; + if (mod == NULL) { mod = gLuaLastHookMod; } + if (mod == NULL) { return; } + char txt[255] = { 0 }; + snprintf(txt, 254, "%s has lua script errors!", mod->name); + djui_lua_error(txt); +} static void smlua_exec_file(char* path) { lua_State* L = gLuaState; @@ -124,6 +135,7 @@ void smlua_init(void) { LOG_INFO(" %s", mod->relativePath); gLuaLoadingMod = mod; gLuaActiveMod = mod; + gLuaLastHookMod = mod; gPcDebug.lastModRun = gLuaActiveMod; for (int j = 0; j < mod->fileCount; j++) { struct ModFile* file = &mod->files[j]; @@ -153,4 +165,7 @@ void smlua_shutdown(void) { lua_close(L); gLuaState = NULL; } + gLuaLoadingMod = NULL; + gLuaActiveMod = NULL; + gLuaLastHookMod = NULL; } diff --git a/src/pc/lua/smlua.h b/src/pc/lua/smlua.h index 3cc24a33c..26a6d4db9 100644 --- a/src/pc/lua/smlua.h +++ b/src/pc/lua/smlua.h @@ -19,7 +19,7 @@ #include "pc/debuglog.h" -#define LOG_LUA(...) ( _debuglog_print_log("LUA ", __FILE__), printf(__VA_ARGS__), printf("\n") ) +#define LOG_LUA(...) ( _debuglog_print_log("LUA ", __FILE__), printf(__VA_ARGS__), printf("\n"), smlua_mod_error() ) #ifdef DEVELOPMENT #define LUA_STACK_CHECK_BEGIN() int __LUA_STACK_TOP = lua_gettop(gLuaState) @@ -33,6 +33,9 @@ extern lua_State* gLuaState; extern u8 gLuaInitializingScript; extern struct Mod* gLuaLoadingMod; extern struct Mod* gLuaActiveMod; +extern struct Mod* gLuaLastHookMod; + +void smlua_mod_error(void); void smlua_init(void); void smlua_update(void); diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index a57885c9b..b69f6060e 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -19,6 +19,7 @@ static struct LuaHookedEvent sHookedEvents[HOOK_MAX] = { 0 }; static int smlua_call_hook(lua_State* L, int nargs, int nresults, int errfunc, struct Mod* activeMod) { struct Mod* prev = gLuaActiveMod; gLuaActiveMod = activeMod; + gLuaLastHookMod = activeMod; int rc = lua_pcall(L, nargs, nresults, errfunc); gLuaActiveMod = prev; return rc; @@ -440,6 +441,7 @@ bool smlua_call_action_hook(struct MarioState* m, s32* returnValue) { } // output the return value + *returnValue = false; if (lua_type(L, -1) == LUA_TBOOLEAN || lua_type(L, -1) == LUA_TNUMBER) { *returnValue = smlua_to_integer(L, -1); } diff --git a/src/pc/lua/smlua_sync_table.c b/src/pc/lua/smlua_sync_table.c index 5853ec08e..0d31f4a48 100644 --- a/src/pc/lua/smlua_sync_table.c +++ b/src/pc/lua/smlua_sync_table.c @@ -145,6 +145,7 @@ static void smlua_sync_table_call_hook(int syncTableIndex, int keyIndex, int pre // call hook struct Mod* prev = gLuaActiveMod; gLuaActiveMod = mod; + gLuaLastHookMod = mod; gPcDebug.lastModRun = gLuaActiveMod; if (0 != lua_pcall(L, 3, 0, 0)) { LOG_LUA("Failed to call the hook_on_changed callback: %s", lua_tostring(L, -1));