Update extension.cpp

This commit is contained in:
zaCade 2019-05-21 18:01:26 +02:00
parent f91e41db5d
commit efc4342b7a

View File

@ -55,14 +55,13 @@ A2SFixes g_Interface;
SMEXT_LINK(&g_Interface); SMEXT_LINK(&g_Interface);
CDetour *g_pDetour_SendTo = NULL; CDetour *g_pDetour_SendTo = NULL;
CDetour *g_pDetour_RecvFrom = NULL;
char g_PlayerReply[1024];
bf_write g_PlayerReplyPacket(g_PlayerReply, 1024);
char *NewBuf; char *NewBuf;
char *OldBuf; char *OldBuf;
char g_PlayerReply[1024];
bf_write g_PlayerReplyPacket(g_PlayerReply, 1024);
/** /**
* @brief * @brief
*/ */
@ -70,11 +69,13 @@ DETOUR_DECL_STATIC6(Detour_SendTo, int, int, s, char *, buf, int, len, int, flag
{ {
if (memcmp(buf, A2S_INFO_REPLY, 5) == 0) if (memcmp(buf, A2S_INFO_REPLY, 5) == 0)
{ {
// g_pSM->LogMessage(myself, "A2S_INFO_REPLY: %s", buf);
delete[] NewBuf; delete[] NewBuf;
NewBuf = new char[len]; NewBuf = new char[len];
// Copy memory
memcpy(NewBuf, buf, len); memcpy(NewBuf, buf, len);
// Store Pointer
OldBuf = NewBuf; OldBuf = NewBuf;
// Skip header and protocol // Skip header and protocol
@ -131,14 +132,12 @@ DETOUR_DECL_STATIC6(Detour_SendTo, int, int, s, char *, buf, int, len, int, flag
if (memcmp(buf, A2S_PLAYER_REPLY, 5) == 0) if (memcmp(buf, A2S_PLAYER_REPLY, 5) == 0)
{ {
//g_pSM->LogMessage(myself, "A2S_PLAYER_REPLY: %s", buf); g_PlayerReplyPacket.Reset(); // Build up the packet as a bitbuffer so we can use the nice helper functions to do the work for us
g_PlayerReplyPacket.Reset(); //build up the packet as a bitbuffer so we can use the nice helper functions to do the work for us g_PlayerReplyPacket.WriteLong(-1); // FF FF FF FF
g_PlayerReplyPacket.WriteByte(68); // 44
g_PlayerReplyPacket.WriteLong(-1);//FF FF FF FF g_PlayerReplyPacket.WriteByte(playerhelpers->GetNumPlayers()); // Number of players
g_PlayerReplyPacket.WriteByte(68);//44
g_PlayerReplyPacket.WriteByte(playerhelpers->GetNumPlayers()); //number of players
int iPlayers = 0; int iPlayers = 0;
@ -146,20 +145,21 @@ DETOUR_DECL_STATIC6(Detour_SendTo, int, int, s, char *, buf, int, len, int, flag
{ {
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(index); IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(index);
if (pPlayer != NULL && pPlayer->IsConnected() && pPlayer->IsInGame() && !pPlayer->IsSourceTV()) if (pPlayer != NULL && pPlayer->IsInGame() && !pPlayer->IsSourceTV())
{ {
IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
INetChannelInfo *pNetInfo = engine->GetPlayerNetInfo(index);
g_PlayerReplyPacket.WriteByte(iPlayers); g_PlayerReplyPacket.WriteByte(iPlayers);
g_PlayerReplyPacket.WriteString(pPlayer->GetName()); g_PlayerReplyPacket.WriteString(pPlayer->GetName());
if(pInfo != NULL) IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
if (pInfo != NULL)
g_PlayerReplyPacket.WriteLong(pInfo->GetFragCount()); g_PlayerReplyPacket.WriteLong(pInfo->GetFragCount());
else else
g_PlayerReplyPacket.WriteLong(0); g_PlayerReplyPacket.WriteLong(0);
if(pNetInfo != NULL) INetChannelInfo *pNetInfo = engine->GetPlayerNetInfo(index);
if (pNetInfo != NULL)
g_PlayerReplyPacket.WriteFloat(pNetInfo->GetTimeConnected()); g_PlayerReplyPacket.WriteFloat(pNetInfo->GetTimeConnected());
else else
g_PlayerReplyPacket.WriteFloat(0.0); g_PlayerReplyPacket.WriteFloat(0.0);
@ -174,31 +174,6 @@ DETOUR_DECL_STATIC6(Detour_SendTo, int, int, s, char *, buf, int, len, int, flag
return DETOUR_STATIC_CALL(Detour_SendTo)(s, buf, len, flags, to, tolen); return DETOUR_STATIC_CALL(Detour_SendTo)(s, buf, len, flags, to, tolen);
} }
/**
* @brief
*/
DETOUR_DECL_STATIC6(Detour_RecvFrom, int, int, s, char *, buf, int, len, int, flags, sockaddr *, from, socklen_t*, fromlen)
{
// if (memcmp(buf, A2S_INFO_REQUEST, 5) == 0)
// {
// g_pSM->LogMessage(myself, "A2S_INFO_REQUEST: %s", buf);
// }
// if (memcmp(buf, A2S_PLAYER_REQUEST, 5) == 0)
// {
// g_pSM->LogMessage(myself, "A2S_PLAYER_REQUEST: %s", buf);
// }
return DETOUR_STATIC_CALL(Detour_RecvFrom)(s, buf, len, flags, from, fromlen);
}
bool A2SFixes::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
{
GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
return true;
}
/** /**
* @brief This is called after the initial loading sequence has been processed. * @brief This is called after the initial loading sequence has been processed.
* *
@ -220,17 +195,7 @@ bool A2SFixes::SDK_OnLoad(char *error, size_t maxlength, bool late)
return false; return false;
} }
g_pDetour_RecvFrom = DETOUR_CREATE_STATIC_ADDRESS(Detour_RecvFrom, (void*)recvfrom);
if (g_pDetour_RecvFrom == NULL)
{
snprintf(error, maxlength, "Could not create detour for 'recvfrom'");
SDK_OnUnload();
return false;
}
g_pDetour_SendTo->EnableDetour(); g_pDetour_SendTo->EnableDetour();
g_pDetour_RecvFrom->EnableDetour();
return true; return true;
} }
@ -239,15 +204,24 @@ bool A2SFixes::SDK_OnLoad(char *error, size_t maxlength, bool late)
*/ */
void A2SFixes::SDK_OnUnload() void A2SFixes::SDK_OnUnload()
{ {
if(g_pDetour_SendTo != NULL) if (g_pDetour_SendTo != NULL)
{ {
g_pDetour_SendTo->Destroy(); g_pDetour_SendTo->Destroy();
g_pDetour_SendTo = NULL; g_pDetour_SendTo = NULL;
} }
}
if(g_pDetour_RecvFrom != NULL)
{ /**
g_pDetour_RecvFrom->Destroy(); * @brief Called when Metamod is attached, before the extension version is called.
g_pDetour_RecvFrom = NULL; *
} * @param error Error buffer.
* @param maxlength Maximum size of error buffer.
* @param late Whether or not Metamod considers this a late load.
* @return True to succeed, false to fail.
*/
bool A2SFixes::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
{
GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
return true;
} }