From 3d5d0b5306e36909b13e49016c2986f393d0d1aa Mon Sep 17 00:00:00 2001 From: MysterD Date: Sat, 29 Jan 2022 15:42:33 -0800 Subject: [PATCH] Made Lua log to console outside of DEBUG builds --- mods/faster-swimming.lua | 2 ++ src/pc/lua/smlua.c | 8 ++++---- src/pc/lua/smlua.h | 2 +- src/pc/lua/smlua_hooks.c | 33 ++++++++++++++++++++++++--------- src/pc/lua/smlua_hooks.h | 9 +++++++++ src/pc/lua/smlua_utils.c | 18 +++++++++--------- 6 files changed, 49 insertions(+), 23 deletions(-) diff --git a/mods/faster-swimming.lua b/mods/faster-swimming.lua index fe1f57ea..d8c9dd27 100644 --- a/mods/faster-swimming.lua +++ b/mods/faster-swimming.lua @@ -4,6 +4,7 @@ function mario_before_phys_step(m) local hScale = 1.0 + local vScale = 1.0 -- faster swimming if (m.action & ACT_FLAG_SWIMMING) ~= 0 then @@ -14,6 +15,7 @@ function mario_before_phys_step(m) end m.vel.x = m.vel.x * hScale + m.vel.y = m.vel.y * vScale m.vel.z = m.vel.z * hScale end diff --git a/src/pc/lua/smlua.c b/src/pc/lua/smlua.c index bc116dc1..c04c11ed 100644 --- a/src/pc/lua/smlua.c +++ b/src/pc/lua/smlua.c @@ -6,7 +6,7 @@ lua_State* gLuaState = NULL; static void smlua_exec_file(char* path) { lua_State* L = gLuaState; if (luaL_dofile(L, path) != LUA_OK) { - LOG_LUA("LUA: Failed to load lua file '%s'.", path); + LOG_LUA("Failed to load lua file '%s'.", path); puts(smlua_to_string(L, lua_gettop(L))); } lua_pop(L, lua_gettop(L)); @@ -15,7 +15,7 @@ static void smlua_exec_file(char* path) { static void smlua_exec_str(char* str) { lua_State* L = gLuaState; if (luaL_dostring(L, str) != LUA_OK) { - LOG_LUA("LUA: Failed to load lua string."); + LOG_LUA("Failed to load lua string."); puts(smlua_to_string(L, lua_gettop(L))); } lua_pop(L, lua_gettop(L)); @@ -24,7 +24,7 @@ static void smlua_exec_str(char* str) { static void smlua_load_script(char* path) { lua_State* L = gLuaState; if (luaL_loadfile(L, path) != LUA_OK) { - LOG_LUA("LUA: Failed to load lua script '%s'.", path); + LOG_LUA("Failed to load lua script '%s'.", path); puts(smlua_to_string(L, lua_gettop(L))); return; } @@ -44,7 +44,7 @@ static void smlua_load_script(char* path) { // run chunks if (lua_pcall(L, 0, LUA_MULTRET, 0) != LUA_OK) { - LOG_LUA("LUA: Failed to execute lua script '%s'.", path); + LOG_LUA("Failed to execute lua script '%s'.", path); puts(smlua_to_string(L, lua_gettop(L))); smlua_dump_stack(); return; diff --git a/src/pc/lua/smlua.h b/src/pc/lua/smlua.h index 61cc5aa6..41a29592 100644 --- a/src/pc/lua/smlua.h +++ b/src/pc/lua/smlua.h @@ -18,7 +18,7 @@ #include "pc/debuglog.h" -#define LOG_LUA LOG_INFO +#define LOG_LUA(...) ( _debuglog_print_log("LUA ", __FILE__), printf(__VA_ARGS__), printf("\n") ) extern lua_State* gLuaState; diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index 4c890600..d20ccee8 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -15,18 +15,25 @@ int smlua_hook_event(lua_State* L) { if (!gSmLuaConvertSuccess) { return 0; } if (hookType >= HOOK_MAX) { - LOG_LUA("LUA: Hook Type: %d exceeds max!", hookType); + LOG_LUA("Hook Type: %d exceeds max!", hookType); return 0; } struct LuaHookedEvent* hook = &sHookedEvents[hookType]; if (hook->count >= MAX_HOOKED_REFERENCES) { - LOG_LUA("LUA: Hook Type: %d exceeded maximum references!", hookType); + LOG_LUA("Hook Type: %s exceeded maximum references!", LuaHookedEventTypeName[hookType]); return 0; } - hook->reference[hook->count] = luaL_ref(L, LUA_REGISTRYINDEX); + int ref = luaL_ref(L, LUA_REGISTRYINDEX); + if (ref == -1) { + LOG_LUA("tried to hook undefined function to '%s'", LuaHookedEventTypeName[hookType]); + return 0; + } + + hook->reference[hook->count] = ref; hook->count++; + return 1; } @@ -40,7 +47,7 @@ void smlua_call_event_hooks(enum LuaHookedEventType hookType) { // call the callback if (0 != lua_pcall(L, 0, 0, 0)) { - LOG_LUA("LUA: Failed to call the callback: %s", lua_tostring(L, -1)); + LOG_LUA("Failed to call the callback: %s", lua_tostring(L, -1)); continue; } } @@ -62,7 +69,7 @@ void smlua_call_event_hooks_mario_param(enum LuaHookedEventType hookType, struct // call the callback if (0 != lua_pcall(L, 1, 0, 0)) { - LOG_LUA("LUA: Failed to call the callback: %s", lua_tostring(L, -1)); + LOG_LUA("Failed to call the callback: %s", lua_tostring(L, -1)); continue; } } @@ -85,13 +92,21 @@ static int sHookedMarioActionsCount = 0; int smlua_hook_mario_action(lua_State* L) { if (L == NULL) { return 0; } if (sHookedMarioActionsCount >= MAX_HOOKED_ACTIONS) { - LOG_LUA("LUA: Hooked mario actions exceeded maximum references!"); + LOG_LUA("Hooked mario actions exceeded maximum references!"); + return 0; + } + + lua_Integer action = smlua_to_integer(L, -2); + int ref = luaL_ref(L, LUA_REGISTRYINDEX); + + if (ref == -1) { + LOG_LUA("Hook Action: %lld tried to hook undefined function", action); return 0; } struct LuaHookedMarioAction* hooked = &sHookedMarioActions[sHookedMarioActionsCount]; - hooked->action = smlua_to_integer(L, -2); - hooked->reference = luaL_ref(L, LUA_REGISTRYINDEX); + hooked->action = action; + hooked->reference = ref; if (!gSmLuaConvertSuccess) { return 0; } sHookedMarioActionsCount++; @@ -114,7 +129,7 @@ bool smlua_call_action_hook(struct MarioState* m, s32* returnValue) { // call the callback if (0 != lua_pcall(L, 1, 1, 0)) { - LOG_LUA("LUA: Failed to call the callback: %s", lua_tostring(L, -1)); + LOG_LUA("Failed to call the callback: %s", lua_tostring(L, -1)); continue; } diff --git a/src/pc/lua/smlua_hooks.h b/src/pc/lua/smlua_hooks.h index 06f56c5d..8b9cb23c 100644 --- a/src/pc/lua/smlua_hooks.h +++ b/src/pc/lua/smlua_hooks.h @@ -12,6 +12,15 @@ enum LuaHookedEventType { HOOK_MAX, }; +static char* LuaHookedEventTypeName[] = { + "HOOK_UPDATE", + "HOOK_MARIO_UPDATE", + "HOOK_BEFORE_MARIO_UPDATE", + "HOOK_ON_SET_MARIO_ACTION", + "HOOK_BEFORE_PHYS_STEP", + "HOOK_MAX" +}; + void smlua_call_event_hooks(enum LuaHookedEventType hookType); void smlua_call_event_hooks_mario_param(enum LuaHookedEventType hookType, struct MarioState* m); bool smlua_call_action_hook(struct MarioState* m, s32* returnValue); diff --git a/src/pc/lua/smlua_utils.c b/src/pc/lua/smlua_utils.c index 572b93c4..be346abd 100644 --- a/src/pc/lua/smlua_utils.c +++ b/src/pc/lua/smlua_utils.c @@ -44,7 +44,7 @@ lua_Integer smlua_to_integer(lua_State* L, int index) { if (lua_type(L, index) == LUA_TBOOLEAN) { return lua_toboolean(L, index) ? 1 : 0; } else if (lua_type(L, index) != LUA_TNUMBER) { - LOG_LUA("LUA: smlua_to_integer received improper type '%d'", lua_type(L, index)); + LOG_LUA("smlua_to_integer received improper type '%d'", lua_type(L, index)); smlua_logline(); gSmLuaConvertSuccess = false; return 0; @@ -56,7 +56,7 @@ lua_Integer smlua_to_integer(lua_State* L, int index) { lua_Number smlua_to_number(lua_State* L, int index) { if (lua_type(L, index) != LUA_TNUMBER) { - LOG_LUA("LUA: smlua_to_number received improper type '%d'", lua_type(L, index)); + LOG_LUA("smlua_to_number received improper type '%d'", lua_type(L, index)); smlua_logline(); gSmLuaConvertSuccess = false; return 0; @@ -67,7 +67,7 @@ lua_Number smlua_to_number(lua_State* L, int index) { const char* smlua_to_string(lua_State* L, int index) { if (lua_type(L, index) != LUA_TSTRING) { - LOG_LUA("LUA: smlua_to_string received improper type '%d'", lua_type(L, index)); + LOG_LUA("smlua_to_string received improper type '%d'", lua_type(L, index)); smlua_logline(); gSmLuaConvertSuccess = false; return 0; @@ -78,7 +78,7 @@ const char* smlua_to_string(lua_State* L, int index) { void* smlua_to_cobject(lua_State* L, int index, u16 lot) { if (lua_type(L, index) != LUA_TTABLE) { - LOG_LUA("LUA: smlua_to_cobject received improper type '%d'", lua_type(L, index)); + LOG_LUA("smlua_to_cobject received improper type '%d'", lua_type(L, index)); smlua_logline(); gSmLuaConvertSuccess = false; return 0; @@ -91,7 +91,7 @@ void* smlua_to_cobject(lua_State* L, int index, u16 lot) { if (!gSmLuaConvertSuccess) { return NULL; } if (lot != objLot) { - LOG_LUA("LUA: smlua_to_cobject received improper LOT. Expected '%d', received '%d'", lot, objLot); + LOG_LUA("smlua_to_cobject received improper LOT. Expected '%d', received '%d'", lot, objLot); smlua_logline(); gSmLuaConvertSuccess = false; return NULL; @@ -105,13 +105,13 @@ void* smlua_to_cobject(lua_State* L, int index, u16 lot) { // check allowlist if (!smlua_cobject_allowlist_contains(lot, (u64)pointer)) { - LOG_LUA("LUA: smlua_to_cobject received a pointer not in allow list. '%u', '%llu", lot, (u64)pointer); + LOG_LUA("smlua_to_cobject received a pointer not in allow list. '%u', '%llu", lot, (u64)pointer); gSmLuaConvertSuccess = false; return NULL; } if (pointer == NULL) { - LOG_LUA("LUA: smlua_to_cobject received null pointer."); + LOG_LUA("smlua_to_cobject received null pointer."); smlua_logline(); gSmLuaConvertSuccess = false; return NULL; @@ -153,7 +153,7 @@ void smlua_push_number_field(int index, char* name, lua_Number val) { lua_Integer smlua_get_integer_field(int index, char* name) { if (lua_type(gLuaState, index) != LUA_TTABLE) { - LOG_LUA("LUA: smlua_get_integer_field received improper type '%d'", lua_type(gLuaState, index)); + LOG_LUA("smlua_get_integer_field received improper type '%d'", lua_type(gLuaState, index)); gSmLuaConvertSuccess = false; return 0; } @@ -165,7 +165,7 @@ lua_Integer smlua_get_integer_field(int index, char* name) { lua_Number smlua_get_number_field(int index, char* name) { if (lua_type(gLuaState, index) != LUA_TTABLE) { - LOG_LUA("LUA: smlua_get_number_field received improper type '%d'", lua_type(gLuaState, index)); + LOG_LUA("smlua_get_number_field received improper type '%d'", lua_type(gLuaState, index)); gSmLuaConvertSuccess = false; return 0; }