From 12f66994e66da459087dc54a8aab5e9705319b8e Mon Sep 17 00:00:00 2001 From: MysterD Date: Wed, 2 Feb 2022 19:33:36 -0800 Subject: [PATCH] Added on_player_connected/disconnected hooks --- autogen/lua_constants/constants.lua | 4 +++- developer/network.sh | 8 +++---- docs/lua/constants.md | 2 ++ src/pc/lua/smlua_constants_autogen.c | 4 +++- src/pc/lua/smlua_hooks.c | 22 +++++++++++++++++++ src/pc/lua/smlua_hooks.h | 5 +++++ src/pc/network/network_player.c | 6 +++++ .../network/packets/packet_lua_sync_table.c | 3 +++ 8 files changed, 48 insertions(+), 6 deletions(-) diff --git a/autogen/lua_constants/constants.lua b/autogen/lua_constants/constants.lua index 287e3cc0..6b820d99 100644 --- a/autogen/lua_constants/constants.lua +++ b/autogen/lua_constants/constants.lua @@ -8,7 +8,9 @@ HOOK_BEFORE_MARIO_UPDATE = 2 HOOK_ON_SET_MARIO_ACTION = 3 HOOK_BEFORE_PHYS_STEP = 4 HOOK_ON_PVP_ATTACK = 5 -HOOK_MAX = 6 +HOOK_ON_PLAYER_CONNECTED = 6 +HOOK_ON_PLAYER_DISCONNECTED = 7 +HOOK_MAX = 8 _CObject = { __index = function (t,k) diff --git a/developer/network.sh b/developer/network.sh index 8fd0a05e..75ac1b2f 100644 --- a/developer/network.sh +++ b/developer/network.sh @@ -19,14 +19,14 @@ fi # no debug, direct $FILE --server 27015 --configfile sm64config_server.txt & -sleep 3 +sleep 2 $FILE --client 127.0.0.1 27015 --configfile sm64config_client.txt & exit # debug on server -$FILE --client 127.0.0.1 27015 --configfile sm64config_client.txt & > /dev/null -winpty cgdb $FILE -ex 'break debug_breakpoint_here' -ex 'run --server 27015 --configfile sm64config_server.txt' -ex 'quit' -exit +#$FILE --client 127.0.0.1 27015 --configfile sm64config_client.txt & > /dev/null +#winpty cgdb $FILE -ex 'break debug_breakpoint_here' -ex 'run --server 27015 --configfile sm64config_server.txt' -ex 'quit' +#exit ################### # debug on client # diff --git a/docs/lua/constants.md b/docs/lua/constants.md index 342c1006..d1ba7103 100644 --- a/docs/lua/constants.md +++ b/docs/lua/constants.md @@ -631,6 +631,8 @@ - HOOK_ON_SET_MARIO_ACTION - HOOK_BEFORE_PHYS_STEP - HOOK_ON_PVP_ATTACK +- HOOK_ON_PLAYER_CONNECTED +- HOOK_ON_PLAYER_DISCONNECTED - HOOK_MAX [:arrow_up_small:](#) diff --git a/src/pc/lua/smlua_constants_autogen.c b/src/pc/lua/smlua_constants_autogen.c index 3d3731f3..1f6b0c98 100644 --- a/src/pc/lua/smlua_constants_autogen.c +++ b/src/pc/lua/smlua_constants_autogen.c @@ -4,7 +4,9 @@ char gSmluaConstants[] = "HOOK_UPDATE = 0\n" "HOOK_ON_SET_MARIO_ACTION = 3\n" "HOOK_BEFORE_PHYS_STEP = 4\n" "HOOK_ON_PVP_ATTACK = 5\n" -"HOOK_MAX = 6\n" +"HOOK_ON_PLAYER_CONNECTED = 6\n" +"HOOK_ON_PLAYER_DISCONNECTED = 7\n" +"HOOK_MAX = 8\n" "_CObject = {\n" " __index = function (t,k)\n" " return _get_field(t['_lot'], t['_pointer'], k)\n" diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index f505296c..002f633a 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -103,6 +103,28 @@ void smlua_call_event_hooks_mario_params(enum LuaHookedEventType hookType, struc } } +void smlua_call_event_hooks_network_player_param(enum LuaHookedEventType hookType, struct NetworkPlayer* np) { + lua_State* L = gLuaState; + if (L == NULL) { return; } + struct LuaHookedEvent* hook = &sHookedEvents[hookType]; + for (int i = 0; i < hook->count; i++) { + // push the callback onto the stack + lua_rawgeti(L, LUA_REGISTRYINDEX, hook->reference[i]); + + // push mario state + lua_getglobal(L, "gNetworkPlayers"); + lua_pushinteger(L, np->localIndex); + lua_gettable(L, -2); + lua_remove(L, -2); + + // call the callback + if (0 != lua_pcall(L, 1, 0, 0)) { + LOG_LUA("Failed to call the callback: %s", lua_tostring(L, -1)); + continue; + } + } +} + //////////////////// // hooked actions // //////////////////// diff --git a/src/pc/lua/smlua_hooks.h b/src/pc/lua/smlua_hooks.h index f487ace1..de55b514 100644 --- a/src/pc/lua/smlua_hooks.h +++ b/src/pc/lua/smlua_hooks.h @@ -10,6 +10,8 @@ enum LuaHookedEventType { HOOK_ON_SET_MARIO_ACTION, HOOK_BEFORE_PHYS_STEP, HOOK_ON_PVP_ATTACK, + HOOK_ON_PLAYER_CONNECTED, + HOOK_ON_PLAYER_DISCONNECTED, HOOK_MAX, }; @@ -20,12 +22,15 @@ static char* LuaHookedEventTypeName[] = { "HOOK_ON_SET_MARIO_ACTION", "HOOK_BEFORE_PHYS_STEP", "HOOK_ON_PVP_ATTACK", + "HOOK_ON_PLAYER_CONNECTED", + "HOOK_ON_PLAYER_DISCONNECTED", "HOOK_MAX" }; void smlua_call_event_hooks(enum LuaHookedEventType hookType); void smlua_call_event_hooks_mario_param(enum LuaHookedEventType hookType, struct MarioState* m); void smlua_call_event_hooks_mario_params(enum LuaHookedEventType hookType, struct MarioState* m1, struct MarioState* m2); + bool smlua_call_action_hook(struct MarioState* m, s32* returnValue); void smlua_bind_hooks(void); diff --git a/src/pc/network/network_player.c b/src/pc/network/network_player.c index 7293ecf5..b2ebd84e 100644 --- a/src/pc/network/network_player.c +++ b/src/pc/network/network_player.c @@ -7,6 +7,7 @@ #include "pc/utils/misc.h" #include "game/area.h" #include "game/level_info.h" +#include "pc/lua/smlua_hooks.h" struct NetworkPlayer gNetworkPlayers[MAX_PLAYERS] = { 0 }; struct NetworkPlayer* gNetworkPlayerLocal = NULL; @@ -245,6 +246,8 @@ u8 network_player_connected(enum NetworkPlayerType type, u8 globalIndex, u8 mode } LOG_INFO("player connected, local %d, global %d", localIndex, np->globalIndex); + smlua_call_event_hooks_mario_param(HOOK_ON_PLAYER_CONNECTED, &gMarioStates[localIndex]); + return localIndex; } @@ -288,6 +291,9 @@ u8 network_player_disconnected(u8 globalIndex) { packet_ordered_clear(globalIndex); reservation_area_change(np); + + smlua_call_event_hooks_mario_param(HOOK_ON_PLAYER_DISCONNECTED, &gMarioStates[i]); + return i; } return UNKNOWN_GLOBAL_INDEX; diff --git a/src/pc/network/packets/packet_lua_sync_table.c b/src/pc/network/packets/packet_lua_sync_table.c index cd591c1c..fe285c45 100644 --- a/src/pc/network/packets/packet_lua_sync_table.c +++ b/src/pc/network/packets/packet_lua_sync_table.c @@ -111,6 +111,7 @@ static bool packet_read_lnt(struct Packet* p, struct LSTNetworkType* lnt) { ///////////////////////////////////////////////////////////// void network_send_lua_sync_table(u8 toLocalIndex, u64 seq, u16 modRemoteIndex, u16 lntKeyCount, struct LSTNetworkType* lntKeys, struct LSTNetworkType* lntValue) { + if (gLuaState == NULL) { return; } struct Packet p = { 0 }; packet_init(&p, PACKET_LUA_SYNC_TABLE, true, PLMT_NONE); @@ -132,6 +133,8 @@ void network_send_lua_sync_table(u8 toLocalIndex, u64 seq, u16 modRemoteIndex, u } void network_receive_lua_sync_table(struct Packet* p) { + if (gLuaState == NULL) { return; } + u64 seq = 0; u16 modRemoteIndex = 0; u16 lntKeyCount = 0;