158 lines
4.5 KiB
C++
158 lines
4.5 KiB
C++
#include "CDetour/detours.h"
|
|
#include <iclient.h>
|
|
#include "extension.h"
|
|
|
|
/**
|
|
* @file extension.cpp
|
|
* @brief Implement extension code here.
|
|
*/
|
|
|
|
class CBaseEntity;
|
|
|
|
uint8_t g_ClientIndex[SM_MAXPLAYERS + 1];
|
|
char g_ClientSteamIDMap[SM_MAXPLAYERS + 1][32];
|
|
char g_ClientNameMap[SM_MAXPLAYERS + 1][32];
|
|
int g_Capacity = 8;
|
|
|
|
//https://sm.alliedmods.net/doxygen/index.html
|
|
|
|
GiveNamedItemTracker g_Interface; /**< Global singleton for extension's main interface */
|
|
SMEXT_LINK(&g_Interface);
|
|
|
|
ITimer *g_pGiveNamedItemTimer = NULL;
|
|
GiveNamedItemTimer g_GiveNamedItemTracker;
|
|
|
|
//taken from https://git.unloze.com/UNLOZE/sm-ext-connect/src/branch/master/extension.cpp
|
|
size_t strlcpy(char *dst, const char *src, size_t dsize)
|
|
{
|
|
const char *osrc = src;
|
|
size_t nleft = dsize;
|
|
|
|
/* Copy as many bytes as will fit. */
|
|
if (nleft != 0) {
|
|
while (--nleft != 0) {
|
|
if ((*dst++ = *src++) == '\0')
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* Not enough room in dst, add NUL and traverse rest of src. */
|
|
if (nleft == 0) {
|
|
if (dsize != 0)
|
|
*dst = '\0'; /* NUL-terminate dst */
|
|
while (*src++)
|
|
;
|
|
}
|
|
|
|
return(src - osrc - 1); /* count does not include NUL */
|
|
}
|
|
|
|
DETOUR_DECL_MEMBER2(GiveNamedItem, void, char*, pszName, int, iSubType)
|
|
{
|
|
CBaseEntity *pPlayer = (CBaseEntity *)this;
|
|
int idx = gamehelpers->EntityToBCompatRef(pPlayer);
|
|
if (idx > 0 && idx < playerhelpers->GetMaxClients()) //should be in the index range for clients.
|
|
{
|
|
IGamePlayer* gp = playerhelpers->GetGamePlayer(idx);
|
|
if (gp->IsConnected())
|
|
{
|
|
g_ClientIndex[idx]++;
|
|
if (g_ClientIndex[idx] <= g_Capacity)
|
|
{
|
|
smutils->LogMessage(myself, "got inside of DETOUR_DECL_MEMBER2. STEAMID: %s, playername: %s. pszName: %s. iSubType: %i",
|
|
g_ClientSteamIDMap[idx], g_ClientNameMap[idx], pszName, iSubType);
|
|
DETOUR_MEMBER_CALL(GiveNamedItem)(pszName, iSubType);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
GiveNamedItemTracker::GiveNamedItemTracker()
|
|
{
|
|
m_GiveNamedItem = NULL;
|
|
m_GiveNamedItemDetour = NULL;
|
|
}
|
|
|
|
void GiveNamedItemTracker::SDK_OnUnload()
|
|
{
|
|
if (g_pGiveNamedItemTimer)
|
|
{
|
|
timersys->KillTimer(g_pGiveNamedItemTimer);
|
|
}
|
|
}
|
|
|
|
bool GiveNamedItemTracker::SDK_OnLoad(char *error, size_t maxlength, bool late)
|
|
{
|
|
void *pBinary = dlopen("cstrike/bin/server_srv.so", RTLD_NOW);
|
|
|
|
if(!pBinary)
|
|
{
|
|
smutils->LogError(myself, "Could not dlopen cstrike/bin/server_srv.so");
|
|
return false;
|
|
}
|
|
|
|
void *adrGiveNamedItem = NULL;
|
|
//CBaseEntity *CCSPlayer::GiveNamedItem
|
|
adrGiveNamedItem = memutils->ResolveSymbol(pBinary, "_ZN9CCSPlayer13GiveNamedItemEPKci");
|
|
dlclose(pBinary);
|
|
|
|
// Setup GiveNamedItem detour.
|
|
CDetourManager::Init(g_pSM->GetScriptingEngine(), NULL);
|
|
|
|
m_GiveNamedItemDetour = DETOUR_CREATE_MEMBER(GiveNamedItem, adrGiveNamedItem);
|
|
|
|
if (!m_GiveNamedItemDetour)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
m_GiveNamedItemDetour->EnableDetour();
|
|
|
|
playerhelpers->AddClientListener(this); //needed so the OnClient forwards work.
|
|
|
|
//set up timer for reseting GiveNamedItemTracker counts.
|
|
g_pGiveNamedItemTimer = timersys->CreateTimer(&g_GiveNamedItemTracker, 2.0, NULL, TIMER_FLAG_REPEAT);
|
|
return true;
|
|
}
|
|
|
|
void GiveNamedItemTracker::OnClientPostAdminCheck(int client)
|
|
{
|
|
g_ClientIndex[client] = 0;
|
|
IGamePlayer* gp = playerhelpers->GetGamePlayer(client);
|
|
strlcpy(g_ClientSteamIDMap[client], gp->GetAuthString(), sizeof(*g_ClientSteamIDMap));
|
|
strlcpy(g_ClientNameMap[client], gp->GetName(), sizeof(*g_ClientNameMap));
|
|
}
|
|
|
|
void GiveNamedItemTracker::OnClientDisconnected(int client)
|
|
{
|
|
g_ClientIndex[client] = 0;
|
|
strlcpy(g_ClientSteamIDMap[client], "", sizeof(*g_ClientSteamIDMap));
|
|
strlcpy(g_ClientNameMap[client], "", sizeof(*g_ClientNameMap));
|
|
}
|
|
|
|
void GiveNamedItemTracker::OnTimer()
|
|
{
|
|
for(int idx = 1; idx < playerhelpers->GetMaxClients(); idx++)
|
|
{
|
|
IGamePlayer* gp = playerhelpers->GetGamePlayer(idx);
|
|
if (gp->IsConnected())
|
|
{
|
|
if (g_ClientIndex[idx] > g_Capacity)
|
|
{
|
|
smutils->LogMessage(myself, "Client with more weapons than capacity %i: STEAMID: %s. playername: %s. g_ClientIndex[idx]: %i",
|
|
g_Capacity, g_ClientSteamIDMap[idx], g_ClientNameMap[idx], g_ClientIndex[idx]);
|
|
}
|
|
g_ClientIndex[idx] = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
ResultType GiveNamedItemTimer::OnTimer(ITimer *pTimer, void *pData)
|
|
{
|
|
g_Interface.OnTimer();
|
|
return Pl_Continue;
|
|
}
|
|
void GiveNamedItemTimer::OnTimerEnd(ITimer *pTimer, void *pData) {}
|
|
|
|
|