sm-ext-A2SFixes/extension.cpp

253 lines
6.8 KiB
C++
Raw Normal View History

2019-05-20 13:48:35 +02:00
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod A2SFixes Extension
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include "extension.h"
#include "string.h"
#include "sys/socket.h"
#include "iplayerinfo.h"
#include <inetchannel.h>
2019-05-20 13:48:35 +02:00
#include "CDetour/detours.h"
#include <bitbuf.h>
2019-05-20 13:48:35 +02:00
#define DETOUR_CREATE_MEMBER_ADDRESS(name, address) CDetourManager::CreateDetour(GET_MEMBER_CALLBACK(name), GET_MEMBER_TRAMPOLINE(name), address);
#define DETOUR_CREATE_STATIC_ADDRESS(name, address) CDetourManager::CreateDetour(GET_STATIC_CALLBACK(name), GET_STATIC_TRAMPOLINE(name), address);
#define A2S_INFO_REQUEST "\xFF\xFF\xFF\xFF\x54"
#define A2S_INFO_REPLY "\xFF\xFF\xFF\xFF\x49"
#define A2S_PLAYER_REQUEST "\xFF\xFF\xFF\xFF\x55"
#define A2S_PLAYER_REPLY "\xFF\xFF\xFF\xFF\x44"
/**
* @file extension.cpp
* @brief Implement extension code here.
*/
A2SFixes g_Interface;
SMEXT_LINK(&g_Interface);
CDetour *g_pDetour_SendTo = NULL;
CDetour *g_pDetour_RecvFrom = NULL;
char g_PlayerReply[1024];
bf_write g_PlayerReplyPacket(g_PlayerReply, 1024);
char *NewBuf;
char *OldBuf;
2019-05-20 13:48:35 +02:00
/**
* @brief
*/
DETOUR_DECL_STATIC6(Detour_SendTo, int, int, s, char *, buf, int, len, int, flags, sockaddr *, to, socklen_t*, tolen)
{
if (memcmp(buf, A2S_INFO_REPLY, 5) == 0)
{
2019-05-20 21:56:49 +02:00
// g_pSM->LogMessage(myself, "A2S_INFO_REPLY: %s", buf);
2019-05-20 17:54:40 +02:00
delete[] NewBuf;
2019-05-20 17:54:40 +02:00
NewBuf = new char[len];
memcpy(NewBuf, buf, len);
OldBuf = NewBuf;
// Skip header and protocol
NewBuf += 5;
// Skip server name
while(*NewBuf)
NewBuf++;
NewBuf++;
// Skip map name
while(*NewBuf)
NewBuf++;
NewBuf++;
// Skip folder
while(*NewBuf)
NewBuf++;
NewBuf++;
// Skip game name
while(*NewBuf)
NewBuf++;
NewBuf++;
// Skip game id
NewBuf += 2;
2019-05-20 21:25:06 +02:00
// Calculate playercount
int iPlayers = 0;
for (int index = 0; index <= SM_MAXPLAYERS; index++)
{
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(index);
2019-05-20 17:54:40 +02:00
2019-05-20 21:25:06 +02:00
if (pPlayer != NULL && pPlayer->IsConnected() && !pPlayer->IsSourceTV())
{
iPlayers++;
2019-05-20 21:25:06 +02:00
}
}
2019-05-20 17:54:40 +02:00
2019-05-20 21:25:06 +02:00
// Correct playercount
memset(NewBuf, iPlayers, 1);
2019-05-20 17:54:40 +02:00
// Reset Pointer
NewBuf = OldBuf;
return DETOUR_STATIC_CALL(Detour_SendTo)(s, NewBuf, len, flags, to, tolen);
2019-05-20 13:48:35 +02:00
}
if (memcmp(buf, A2S_PLAYER_REPLY, 5) == 0)
{
2019-05-21 03:33:46 +02:00
//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.WriteLong(-1);//FF FF FF FF
g_PlayerReplyPacket.WriteByte(68);//44
g_PlayerReplyPacket.WriteByte(playerhelpers->GetNumPlayers()); //number of players
int iPlayers = 0;
for (int index = 0; index <= SM_MAXPLAYERS; index++)
{
IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(index);
if (pPlayer != NULL && pPlayer->IsConnected() && pPlayer->IsInGame() && !pPlayer->IsSourceTV())
{
IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
INetChannelInfo *pNetInfo = engine->GetPlayerNetInfo(index);
g_PlayerReplyPacket.WriteByte(iPlayers);
g_PlayerReplyPacket.WriteString(pPlayer->GetName());
if(pInfo != NULL)
g_PlayerReplyPacket.WriteLong(pInfo->GetFragCount());
else
g_PlayerReplyPacket.WriteLong(0);
if(pNetInfo != NULL)
g_PlayerReplyPacket.WriteFloat(pNetInfo->GetTimeConnected());
else
g_PlayerReplyPacket.WriteFloat(0.0);
iPlayers++;
}
}
return DETOUR_STATIC_CALL(Detour_SendTo)(s, (char *)g_PlayerReplyPacket.GetData(), g_PlayerReplyPacket.GetNumBytesWritten(), flags, to, tolen);
}
2019-05-20 13:48:35 +02:00
2019-05-20 17:52:15 +02:00
return DETOUR_STATIC_CALL(Detour_SendTo)(s, buf, len, flags, to, tolen);
2019-05-20 13:48:35 +02:00
}
/**
* @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);
// }
2019-05-20 17:52:15 +02:00
return DETOUR_STATIC_CALL(Detour_RecvFrom)(s, buf, len, flags, from, fromlen);
2019-05-20 13:48:35 +02:00
}
bool A2SFixes::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
{
GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
return true;
}
2019-05-20 13:48:35 +02:00
/**
* @brief This is called after the initial loading sequence has been processed.
*
* @param error Error message buffer.
* @param maxlength Size of error message buffer.
* @param late Whether or not the module was loaded after map load.
* @return True to succeed loading, false to fail.
*/
bool A2SFixes::SDK_OnLoad(char *error, size_t maxlength, bool late)
{
CDetourManager::Init(g_pSM->GetScriptingEngine(), NULL);
g_pDetour_SendTo = DETOUR_CREATE_STATIC_ADDRESS(Detour_SendTo, (void*)sendto);
if (g_pDetour_SendTo == NULL)
{
snprintf(error, maxlength, "Could not create detour for 'sendto'");
SDK_OnUnload();
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_RecvFrom->EnableDetour();
return true;
}
/**
* @brief This is called right before the extension is unloaded.
*/
void A2SFixes::SDK_OnUnload()
{
if(g_pDetour_SendTo != NULL)
{
g_pDetour_SendTo->Destroy();
g_pDetour_SendTo = NULL;
}
if(g_pDetour_RecvFrom != NULL)
{
g_pDetour_RecvFrom->Destroy();
g_pDetour_RecvFrom = NULL;
}
}