Merge branch 'Playerquit_hook' into 'next'

"PlayerQuit" Lua Hook

See merge request STJr/SRB2!127
This commit is contained in:
Monster Iestyn 2018-11-08 15:53:35 -05:00
commit d8f3a20e65
5 changed files with 64 additions and 2 deletions

View file

@ -2339,7 +2339,7 @@ void CL_ClearPlayer(INT32 playernum)
// //
// Removes a player from the current game // Removes a player from the current game
// //
static void CL_RemovePlayer(INT32 playernum) static void CL_RemovePlayer(INT32 playernum, INT32 reason)
{ {
// Sanity check: exceptional cases (i.e. c-fails) can cause multiple // Sanity check: exceptional cases (i.e. c-fails) can cause multiple
// kick commands to be issued for the same player. // kick commands to be issued for the same player.
@ -2394,6 +2394,10 @@ static void CL_RemovePlayer(INT32 playernum)
} }
} }
#ifdef HAVE_BLUA
LUAh_PlayerQuit(&players[playernum], reason); // Lua hook for player quitting
#endif
// Reset player data // Reset player data
CL_ClearPlayer(playernum); CL_ClearPlayer(playernum);
@ -2669,6 +2673,7 @@ static void Got_KickCmd(UINT8 **p, INT32 playernum)
INT32 pnum, msg; INT32 pnum, msg;
XBOXSTATIC char buf[3 + MAX_REASONLENGTH]; XBOXSTATIC char buf[3 + MAX_REASONLENGTH];
char *reason = buf; char *reason = buf;
kickreason_t kickreason = KR_KICK;
pnum = READUINT8(*p); pnum = READUINT8(*p);
msg = READUINT8(*p); msg = READUINT8(*p);
@ -2751,14 +2756,17 @@ static void Got_KickCmd(UINT8 **p, INT32 playernum)
{ {
case KICK_MSG_GO_AWAY: case KICK_MSG_GO_AWAY:
CONS_Printf(M_GetText("has been kicked (Go away)\n")); CONS_Printf(M_GetText("has been kicked (Go away)\n"));
kickreason = KR_KICK;
break; break;
#ifdef NEWPING #ifdef NEWPING
case KICK_MSG_PING_HIGH: case KICK_MSG_PING_HIGH:
CONS_Printf(M_GetText("left the game (Broke ping limit)\n")); CONS_Printf(M_GetText("left the game (Broke ping limit)\n"));
kickreason = KR_PINGLIMIT;
break; break;
#endif #endif
case KICK_MSG_CON_FAIL: case KICK_MSG_CON_FAIL:
CONS_Printf(M_GetText("left the game (Synch failure)\n")); CONS_Printf(M_GetText("left the game (Synch failure)\n"));
kickreason = KR_SYNCH;
if (M_CheckParm("-consisdump")) // Helps debugging some problems if (M_CheckParm("-consisdump")) // Helps debugging some problems
{ {
@ -2795,21 +2803,26 @@ static void Got_KickCmd(UINT8 **p, INT32 playernum)
break; break;
case KICK_MSG_TIMEOUT: case KICK_MSG_TIMEOUT:
CONS_Printf(M_GetText("left the game (Connection timeout)\n")); CONS_Printf(M_GetText("left the game (Connection timeout)\n"));
kickreason = KR_TIMEOUT;
break; break;
case KICK_MSG_PLAYER_QUIT: case KICK_MSG_PLAYER_QUIT:
if (netgame) // not splitscreen/bots if (netgame) // not splitscreen/bots
CONS_Printf(M_GetText("left the game\n")); CONS_Printf(M_GetText("left the game\n"));
kickreason = KR_LEAVE;
break; break;
case KICK_MSG_BANNED: case KICK_MSG_BANNED:
CONS_Printf(M_GetText("has been banned (Don't come back)\n")); CONS_Printf(M_GetText("has been banned (Don't come back)\n"));
kickreason = KR_BAN;
break; break;
case KICK_MSG_CUSTOM_KICK: case KICK_MSG_CUSTOM_KICK:
READSTRINGN(*p, reason, MAX_REASONLENGTH+1); READSTRINGN(*p, reason, MAX_REASONLENGTH+1);
CONS_Printf(M_GetText("has been kicked (%s)\n"), reason); CONS_Printf(M_GetText("has been kicked (%s)\n"), reason);
kickreason = KR_KICK;
break; break;
case KICK_MSG_CUSTOM_BAN: case KICK_MSG_CUSTOM_BAN:
READSTRINGN(*p, reason, MAX_REASONLENGTH+1); READSTRINGN(*p, reason, MAX_REASONLENGTH+1);
CONS_Printf(M_GetText("has been banned (%s)\n"), reason); CONS_Printf(M_GetText("has been banned (%s)\n"), reason);
kickreason = KR_BAN;
break; break;
} }
@ -2837,7 +2850,7 @@ static void Got_KickCmd(UINT8 **p, INT32 playernum)
M_StartMessage(M_GetText("You have been kicked by the server\n\nPress ESC\n"), NULL, MM_NOTHING); M_StartMessage(M_GetText("You have been kicked by the server\n\nPress ESC\n"), NULL, MM_NOTHING);
} }
else else
CL_RemovePlayer(pnum); CL_RemovePlayer(pnum, kickreason);
} }
consvar_t cv_allownewplayer = {"allowjoin", "On", CV_NETVAR, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL }; consvar_t cv_allownewplayer = {"allowjoin", "On", CV_NETVAR, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL };

View file

@ -444,6 +444,17 @@ extern consvar_t cv_playbackspeed;
#define KICK_MSG_CUSTOM_KICK 7 #define KICK_MSG_CUSTOM_KICK 7
#define KICK_MSG_CUSTOM_BAN 8 #define KICK_MSG_CUSTOM_BAN 8
typedef enum
{
KR_KICK = 1, //Kicked by server
KR_PINGLIMIT = 2, //Broke Ping Limit
KR_SYNCH = 3, //Synch Failure
KR_TIMEOUT = 4, //Connection Timeout
KR_BAN = 5, //Banned by server
KR_LEAVE = 6, //Quit the game
} kickreason_t;
extern boolean server; extern boolean server;
#define client (!server) #define client (!server)
extern boolean dedicated; // For dedicated server extern boolean dedicated; // For dedicated server

View file

@ -32,6 +32,7 @@
#include "fastcmp.h" #include "fastcmp.h"
#include "lua_script.h" #include "lua_script.h"
#include "lua_hook.h" #include "lua_hook.h"
#include "d_clisrv.h"
#include "m_cond.h" #include "m_cond.h"
@ -7416,6 +7417,14 @@ struct {
{"V_CHARCOLORSHIFT",V_CHARCOLORSHIFT}, {"V_CHARCOLORSHIFT",V_CHARCOLORSHIFT},
{"V_ALPHASHIFT",V_ALPHASHIFT}, {"V_ALPHASHIFT",V_ALPHASHIFT},
//Kick Reasons
{"KR_KICK",KR_KICK},
{"KR_PINGLIMIT",KR_PINGLIMIT},
{"KR_SYNCH",KR_SYNCH},
{"KR_TIMEOUT",KR_TIMEOUT},
{"KR_BAN",KR_BAN},
{"KR_LEAVE",KR_LEAVE},
#endif #endif
{NULL,0} {NULL,0}

View file

@ -43,6 +43,7 @@ enum hook {
hook_PlayerMsg, hook_PlayerMsg,
hook_HurtMsg, hook_HurtMsg,
hook_PlayerSpawn, hook_PlayerSpawn,
hook_PlayerQuit,
hook_MAX // last hook hook_MAX // last hook
}; };
@ -77,5 +78,6 @@ boolean LUAh_LinedefExecute(line_t *line, mobj_t *mo, sector_t *sector); // Hook
boolean LUAh_PlayerMsg(int source, int target, int flags, char *msg); // Hook for chat messages boolean LUAh_PlayerMsg(int source, int target, int flags, char *msg); // Hook for chat messages
boolean LUAh_HurtMsg(player_t *player, mobj_t *inflictor, mobj_t *source); // Hook for hurt messages boolean LUAh_HurtMsg(player_t *player, mobj_t *inflictor, mobj_t *source); // Hook for hurt messages
#define LUAh_PlayerSpawn(player) LUAh_PlayerHook(player, hook_PlayerSpawn) // Hook for G_SpawnPlayer #define LUAh_PlayerSpawn(player) LUAh_PlayerHook(player, hook_PlayerSpawn) // Hook for G_SpawnPlayer
void LUAh_PlayerQuit(player_t *plr, int reason); // Hook for player quitting
#endif #endif

View file

@ -54,6 +54,7 @@ const char *const hookNames[hook_MAX+1] = {
"PlayerMsg", "PlayerMsg",
"HurtMsg", "HurtMsg",
"PlayerSpawn", "PlayerSpawn",
"PlayerQuit",
NULL NULL
}; };
@ -1074,4 +1075,30 @@ void LUAh_NetArchiveHook(lua_CFunction archFunc)
// stack: tables // stack: tables
} }
void LUAh_PlayerQuit(player_t *plr, int reason)
{
hook_p hookp;
if (!gL || !(hooksAvailable[hook_PlayerQuit/8] & (1<<(hook_PlayerQuit%8))))
return;
lua_settop(gL, 0);
for (hookp = roothook; hookp; hookp = hookp->next)
if (hookp->type == hook_PlayerQuit)
{
if (lua_gettop(gL) == 0)
{
LUA_PushUserdata(gL, plr, META_PLAYER); // Player that quit
lua_pushinteger(gL, reason); // Reason for quitting
}
lua_pushfstring(gL, FMT_HOOKID, hookp->id);
lua_gettable(gL, LUA_REGISTRYINDEX);
lua_pushvalue(gL, -3);
lua_pushvalue(gL, -3);
LUA_Call(gL, 2);
}
lua_settop(gL, 0);
}
#endif #endif