From d806c2d8c2f7736a7e1cc53c2038ab4a793448e1 Mon Sep 17 00:00:00 2001 From: Isaac0-dev <62234577+Isaac0-dev@users.noreply.github.com> Date: Mon, 31 Oct 2022 10:34:50 +1000 Subject: [PATCH] add HOOK_ON_CHAT_MESSAGE (#220) includes the message string and the mario state of the player return false to prevent the message from showing (might be possible to make a chat flter with this?) --- autogen/lua_definitions/constants.lua | 5 +++- autogen/lua_definitions/structs.lua | 2 +- docs/lua/constants.md | 3 ++- docs/lua/structs.md | 2 +- src/pc/djui/djui_chat_message.c | 7 ++++++ src/pc/lua/smlua_cobject_autogen.c | 2 +- src/pc/lua/smlua_constants_autogen.c | 3 ++- src/pc/lua/smlua_hooks.c | 33 +++++++++++++++++++++++++++ src/pc/lua/smlua_hooks.h | 3 +++ 9 files changed, 54 insertions(+), 6 deletions(-) diff --git a/autogen/lua_definitions/constants.lua b/autogen/lua_definitions/constants.lua index 876be5ea7..319216798 100644 --- a/autogen/lua_definitions/constants.lua +++ b/autogen/lua_definitions/constants.lua @@ -8128,7 +8128,10 @@ HOOK_ON_SCREEN_TRANSITION = 25 HOOK_ALLOW_HAZARD_SURFACE = 26 --- @type LuaHookedEventType -HOOK_MAX = 27 +HOOK_ON_CHAT_MESSAGE = 27 + +--- @type LuaHookedEventType +HOOK_MAX = 28 --- @class HudDisplayFlags diff --git a/autogen/lua_definitions/structs.lua b/autogen/lua_definitions/structs.lua index e5029e920..20b1ac374 100644 --- a/autogen/lua_definitions/structs.lua +++ b/autogen/lua_definitions/structs.lua @@ -629,7 +629,7 @@ --- @field public exitCastleArea integer --- @field public exitCastleLevel LevelNum --- @field public exitCastleWarpNode integer ---- @field public fixCollisionBugs integer +--- @field public fixCollisionBugs boolean --- @field public floorLowerLimit integer --- @field public floorLowerLimitMisc integer --- @field public floorLowerLimitShadow integer diff --git a/docs/lua/constants.md b/docs/lua/constants.md index 6df37399e..2d1eb77ec 100644 --- a/docs/lua/constants.md +++ b/docs/lua/constants.md @@ -2878,7 +2878,8 @@ | HOOK_ON_CHANGE_CAMERA_ANGLE | 24 | | HOOK_ON_SCREEN_TRANSITION | 25 | | HOOK_ALLOW_HAZARD_SURFACE | 26 | -| HOOK_MAX | 27 | +| HOOK_ON_CHAT_MESSAGE | 27 | +| HOOK_MAX | 28 | [:arrow_up_small:](#) diff --git a/docs/lua/structs.md b/docs/lua/structs.md index 1dc20ea19..367f31e2f 100644 --- a/docs/lua/structs.md +++ b/docs/lua/structs.md @@ -924,7 +924,7 @@ | exitCastleArea | `integer` | | | exitCastleLevel | [enum LevelNum](constants.md#enum-LevelNum) | | | exitCastleWarpNode | `integer` | | -| fixCollisionBugs | `integer` | | +| fixCollisionBugs | `boolean` | | | floorLowerLimit | `integer` | | | floorLowerLimitMisc | `integer` | | | floorLowerLimitShadow | `integer` | | diff --git a/src/pc/djui/djui_chat_message.c b/src/pc/djui/djui_chat_message.c index eb4344cb3..90f411e75 100644 --- a/src/pc/djui/djui_chat_message.c +++ b/src/pc/djui/djui_chat_message.c @@ -6,6 +6,7 @@ #include "game/mario_misc.h" #include "djui.h" #include "pc/debuglog.h" +#include "pc/lua/smlua_hooks.h" #define DJUI_CHAT_LIFE_TIME 10.0f @@ -51,6 +52,12 @@ void djui_chat_message_create_from(u8 globalIndex, const char* message) { return; } + bool returnValue = true; + smlua_call_event_hooks_on_chat_message(HOOK_ON_CHAT_MESSAGE, &gMarioStates[np->localIndex], message, &returnValue); + if (!returnValue) { + return; + } + char* playerColorString = network_get_player_text_color_string(np->localIndex); char chatMsg[256] = { 0 }; snprintf(chatMsg, 256, "%s%s:\\#dcdcdc\\ %s", playerColorString, (np != NULL) ? np->name : "Player", message); diff --git a/src/pc/lua/smlua_cobject_autogen.c b/src/pc/lua/smlua_cobject_autogen.c index d48994f7c..8531f4daf 100644 --- a/src/pc/lua/smlua_cobject_autogen.c +++ b/src/pc/lua/smlua_cobject_autogen.c @@ -734,7 +734,7 @@ static struct LuaObjectField sLevelValuesFields[LUA_LEVEL_VALUES_FIELD_COUNT] = { "exitCastleArea", LVT_S16, offsetof(struct LevelValues, exitCastleArea), false, LOT_NONE }, { "exitCastleLevel", LVT_S32, offsetof(struct LevelValues, exitCastleLevel), false, LOT_NONE }, { "exitCastleWarpNode", LVT_U8, offsetof(struct LevelValues, exitCastleWarpNode), false, LOT_NONE }, - { "fixCollisionBugs", LVT_U8, offsetof(struct LevelValues, fixCollisionBugs), false, LOT_NONE }, + { "fixCollisionBugs", LVT_BOOL, offsetof(struct LevelValues, fixCollisionBugs), false, LOT_NONE }, { "floorLowerLimit", LVT_S16, offsetof(struct LevelValues, floorLowerLimit), false, LOT_NONE }, { "floorLowerLimitMisc", LVT_S16, offsetof(struct LevelValues, floorLowerLimitMisc), false, LOT_NONE }, { "floorLowerLimitShadow", LVT_S16, offsetof(struct LevelValues, floorLowerLimitShadow), false, LOT_NONE }, diff --git a/src/pc/lua/smlua_constants_autogen.c b/src/pc/lua/smlua_constants_autogen.c index b7a93dea1..67510b0bf 100644 --- a/src/pc/lua/smlua_constants_autogen.c +++ b/src/pc/lua/smlua_constants_autogen.c @@ -2885,7 +2885,8 @@ char gSmluaConstants[] = "" "HOOK_ON_CHANGE_CAMERA_ANGLE = 24\n" "HOOK_ON_SCREEN_TRANSITION = 25\n" "HOOK_ALLOW_HAZARD_SURFACE = 26\n" -"HOOK_MAX = 27\n" +"HOOK_ON_CHAT_MESSAGE = 27\n" +"HOOK_MAX = 28\n" "ACTION_HOOK_EVERY_FRAME = 0\n" "ACTION_HOOK_GRAVITY = 1\n" "ACTION_HOOK_MAX = 2\n" diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index 590a21700..3544d8434 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -581,6 +581,39 @@ void smlua_call_event_hooks_use_act_select(enum LuaHookedEventType hookType, int } } +void smlua_call_event_hooks_on_chat_message(enum LuaHookedEventType hookType, struct MarioState* m, const char* message, bool* returnValue) { + lua_State* L = gLuaState; + if (L == NULL) { return; } + struct LuaHookedEvent* hook = &sHookedEvents[hookType]; + for (int i = 0; i < hook->count; i++) { + s32 prevTop = lua_gettop(L); + + // push the callback onto the stack + lua_rawgeti(L, LUA_REGISTRYINDEX, hook->reference[i]); + + // push mario state + lua_getglobal(L, "gMarioStates"); + lua_pushinteger(L, m->playerIndex); + lua_gettable(L, -2); + lua_remove(L, -2); + + // push the string + lua_pushstring(L, message); + + // call the callback + if (0 != smlua_call_hook(L, 2, 1, 0, hook->mod[i])) { + LOG_LUA("Failed to call the callback: %u", hookType); + continue; + } + + // output the return value + if (lua_type(L, -1) == LUA_TBOOLEAN) { + *returnValue = smlua_to_boolean(L, -1); + } + lua_settop(L, prevTop); + } +} + //////////////////// // hooked actions // //////////////////// diff --git a/src/pc/lua/smlua_hooks.h b/src/pc/lua/smlua_hooks.h index 86873e7d7..b3d8a2e7e 100644 --- a/src/pc/lua/smlua_hooks.h +++ b/src/pc/lua/smlua_hooks.h @@ -38,6 +38,7 @@ enum LuaHookedEventType { HOOK_ON_CHANGE_CAMERA_ANGLE, HOOK_ON_SCREEN_TRANSITION, HOOK_ALLOW_HAZARD_SURFACE, + HOOK_ON_CHAT_MESSAGE, HOOK_MAX, }; @@ -69,6 +70,7 @@ static const char* LuaHookedEventTypeName[] = { "HOOK_ON_CHANGE_CAMERA_ANGLE", "HOOK_ON_SCREEN_TRANSITION", "HOOK_ALLOW_HAZARD_SURFACE", + "HOOK_ON_CHAT_MESSAGE", "HOOK_MAX" }; @@ -104,6 +106,7 @@ void smlua_call_event_hooks_int_params_ret_bool(enum LuaHookedEventType hookType void smlua_call_event_hooks_value_param(enum LuaHookedEventType hookType, int modIndex, int valueIndex); void smlua_call_event_hooks_use_act_select(enum LuaHookedEventType hookType, int value, bool* foundHook, bool* returnValue); void smlua_call_event_hooks_ret_bool(enum LuaHookedEventType hookType, bool* returnValue); +void smlua_call_event_hooks_on_chat_message(enum LuaHookedEventType hookType, struct MarioState* m, const char* message, bool* returnValue); enum BehaviorId smlua_get_original_behavior_id(const BehaviorScript* behavior); const BehaviorScript* smlua_override_behavior(const BehaviorScript* behavior);