Added on_player_connected/disconnected hooks

This commit is contained in:
MysterD 2022-02-02 19:33:36 -08:00
parent c59aeedf78
commit 12f66994e6
8 changed files with 48 additions and 6 deletions

View file

@ -8,7 +8,9 @@ HOOK_BEFORE_MARIO_UPDATE = 2
HOOK_ON_SET_MARIO_ACTION = 3 HOOK_ON_SET_MARIO_ACTION = 3
HOOK_BEFORE_PHYS_STEP = 4 HOOK_BEFORE_PHYS_STEP = 4
HOOK_ON_PVP_ATTACK = 5 HOOK_ON_PVP_ATTACK = 5
HOOK_MAX = 6 HOOK_ON_PLAYER_CONNECTED = 6
HOOK_ON_PLAYER_DISCONNECTED = 7
HOOK_MAX = 8
_CObject = { _CObject = {
__index = function (t,k) __index = function (t,k)

View file

@ -19,14 +19,14 @@ fi
# no debug, direct # no debug, direct
$FILE --server 27015 --configfile sm64config_server.txt & $FILE --server 27015 --configfile sm64config_server.txt &
sleep 3 sleep 2
$FILE --client 127.0.0.1 27015 --configfile sm64config_client.txt & $FILE --client 127.0.0.1 27015 --configfile sm64config_client.txt &
exit exit
# debug on server # debug on server
$FILE --client 127.0.0.1 27015 --configfile sm64config_client.txt & > /dev/null #$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' #winpty cgdb $FILE -ex 'break debug_breakpoint_here' -ex 'run --server 27015 --configfile sm64config_server.txt' -ex 'quit'
exit #exit
################### ###################
# debug on client # # debug on client #

View file

@ -631,6 +631,8 @@
- HOOK_ON_SET_MARIO_ACTION - HOOK_ON_SET_MARIO_ACTION
- HOOK_BEFORE_PHYS_STEP - HOOK_BEFORE_PHYS_STEP
- HOOK_ON_PVP_ATTACK - HOOK_ON_PVP_ATTACK
- HOOK_ON_PLAYER_CONNECTED
- HOOK_ON_PLAYER_DISCONNECTED
- HOOK_MAX - HOOK_MAX
[:arrow_up_small:](#) [:arrow_up_small:](#)

View file

@ -4,7 +4,9 @@ char gSmluaConstants[] = "HOOK_UPDATE = 0\n"
"HOOK_ON_SET_MARIO_ACTION = 3\n" "HOOK_ON_SET_MARIO_ACTION = 3\n"
"HOOK_BEFORE_PHYS_STEP = 4\n" "HOOK_BEFORE_PHYS_STEP = 4\n"
"HOOK_ON_PVP_ATTACK = 5\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" "_CObject = {\n"
" __index = function (t,k)\n" " __index = function (t,k)\n"
" return _get_field(t['_lot'], t['_pointer'], k)\n" " return _get_field(t['_lot'], t['_pointer'], k)\n"

View file

@ -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 // // hooked actions //
//////////////////// ////////////////////

View file

@ -10,6 +10,8 @@ enum LuaHookedEventType {
HOOK_ON_SET_MARIO_ACTION, HOOK_ON_SET_MARIO_ACTION,
HOOK_BEFORE_PHYS_STEP, HOOK_BEFORE_PHYS_STEP,
HOOK_ON_PVP_ATTACK, HOOK_ON_PVP_ATTACK,
HOOK_ON_PLAYER_CONNECTED,
HOOK_ON_PLAYER_DISCONNECTED,
HOOK_MAX, HOOK_MAX,
}; };
@ -20,12 +22,15 @@ static char* LuaHookedEventTypeName[] = {
"HOOK_ON_SET_MARIO_ACTION", "HOOK_ON_SET_MARIO_ACTION",
"HOOK_BEFORE_PHYS_STEP", "HOOK_BEFORE_PHYS_STEP",
"HOOK_ON_PVP_ATTACK", "HOOK_ON_PVP_ATTACK",
"HOOK_ON_PLAYER_CONNECTED",
"HOOK_ON_PLAYER_DISCONNECTED",
"HOOK_MAX" "HOOK_MAX"
}; };
void smlua_call_event_hooks(enum LuaHookedEventType hookType); 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_param(enum LuaHookedEventType hookType, struct MarioState* m);
void smlua_call_event_hooks_mario_params(enum LuaHookedEventType hookType, struct MarioState* m1, struct MarioState* m2); 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); bool smlua_call_action_hook(struct MarioState* m, s32* returnValue);
void smlua_bind_hooks(void); void smlua_bind_hooks(void);

View file

@ -7,6 +7,7 @@
#include "pc/utils/misc.h" #include "pc/utils/misc.h"
#include "game/area.h" #include "game/area.h"
#include "game/level_info.h" #include "game/level_info.h"
#include "pc/lua/smlua_hooks.h"
struct NetworkPlayer gNetworkPlayers[MAX_PLAYERS] = { 0 }; struct NetworkPlayer gNetworkPlayers[MAX_PLAYERS] = { 0 };
struct NetworkPlayer* gNetworkPlayerLocal = NULL; 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); 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; return localIndex;
} }
@ -288,6 +291,9 @@ u8 network_player_disconnected(u8 globalIndex) {
packet_ordered_clear(globalIndex); packet_ordered_clear(globalIndex);
reservation_area_change(np); reservation_area_change(np);
smlua_call_event_hooks_mario_param(HOOK_ON_PLAYER_DISCONNECTED, &gMarioStates[i]);
return i; return i;
} }
return UNKNOWN_GLOBAL_INDEX; return UNKNOWN_GLOBAL_INDEX;

View file

@ -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) { 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 }; struct Packet p = { 0 };
packet_init(&p, PACKET_LUA_SYNC_TABLE, true, PLMT_NONE); 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) { void network_receive_lua_sync_table(struct Packet* p) {
if (gLuaState == NULL) { return; }
u64 seq = 0; u64 seq = 0;
u16 modRemoteIndex = 0; u16 modRemoteIndex = 0;
u16 lntKeyCount = 0; u16 lntKeyCount = 0;