2007-01-25 23:36:38 +01:00
|
|
|
/**
|
|
|
|
* ===============================================================
|
|
|
|
* SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
|
|
|
|
* ===============================================================
|
|
|
|
*
|
|
|
|
* This file is not open source and may not be copied without explicit
|
|
|
|
* written permission of AlliedModders LLC. This file may not be redistributed
|
|
|
|
* in whole or significant part.
|
|
|
|
* For information, see LICENSE.txt or http://www.sourcemod.net/license.php
|
|
|
|
*
|
|
|
|
* Version: $Id$
|
|
|
|
*/
|
|
|
|
|
2007-01-01 04:33:14 +01:00
|
|
|
#include "CPlayerManager.h"
|
2007-01-11 00:49:22 +01:00
|
|
|
#include "sm_stringutil.h"
|
2007-01-01 04:33:14 +01:00
|
|
|
|
2007-01-02 03:40:32 +01:00
|
|
|
static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params)
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
|
|
|
if (params[1])
|
|
|
|
{
|
2007-02-07 09:44:48 +01:00
|
|
|
return g_Players.NumPlayers();
|
2007-01-01 04:33:14 +01:00
|
|
|
}
|
|
|
|
|
2007-02-07 09:44:48 +01:00
|
|
|
int maxplayers = g_Players.MaxClients();
|
2007-01-01 04:33:14 +01:00
|
|
|
int count = 0;
|
|
|
|
for (int i=1; i<=maxplayers; ++i)
|
|
|
|
{
|
2007-02-07 09:44:48 +01:00
|
|
|
CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
|
2007-02-05 21:32:34 +01:00
|
|
|
if ((pPlayer->IsConnected()) && !(pPlayer->IsInGame()))
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-07 09:44:48 +01:00
|
|
|
return (g_Players.NumPlayers() + count);
|
2007-01-01 04:33:14 +01:00
|
|
|
}
|
|
|
|
|
2007-01-02 03:40:32 +01:00
|
|
|
static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params)
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
2007-02-07 09:44:48 +01:00
|
|
|
return g_Players.MaxClients();
|
2007-01-01 04:33:14 +01:00
|
|
|
}
|
|
|
|
|
2007-01-02 03:40:32 +01:00
|
|
|
static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params)
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
|
|
|
int index = params[1];
|
2007-02-07 09:44:48 +01:00
|
|
|
if ((index < 1) || (index > g_Players.GetMaxClients()))
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
2007-01-02 03:40:32 +01:00
|
|
|
return pCtx->ThrowNativeError("Invalid client index %d.", index);
|
|
|
|
}
|
|
|
|
|
2007-02-07 09:44:48 +01:00
|
|
|
CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
|
2007-02-05 21:32:34 +01:00
|
|
|
if (!pPlayer->IsConnected())
|
2007-01-02 03:40:32 +01:00
|
|
|
{
|
|
|
|
return pCtx->ThrowNativeError("Client %d is not connected.", index);
|
2007-01-01 04:33:14 +01:00
|
|
|
}
|
|
|
|
|
2007-02-05 21:32:34 +01:00
|
|
|
pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), pPlayer->GetName(), NULL);
|
2007-01-01 04:33:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-01-02 03:40:32 +01:00
|
|
|
static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params)
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
|
|
|
int index = params[1];
|
2007-02-07 09:44:48 +01:00
|
|
|
if ((index < 1) || (index > g_Players.GetMaxClients()))
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
2007-01-02 03:40:32 +01:00
|
|
|
return pCtx->ThrowNativeError("Invalid client index %d.", index);
|
2007-01-01 04:33:14 +01:00
|
|
|
}
|
|
|
|
|
2007-02-07 09:44:48 +01:00
|
|
|
CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
|
2007-02-05 21:32:34 +01:00
|
|
|
if (!pPlayer->IsConnected())
|
2007-01-02 03:40:32 +01:00
|
|
|
{
|
|
|
|
return pCtx->ThrowNativeError("Client %d is not connected.", index);
|
|
|
|
}
|
|
|
|
|
2007-01-11 00:49:22 +01:00
|
|
|
char buf[64], *ptr;
|
2007-02-05 21:32:34 +01:00
|
|
|
strcpy(buf, pPlayer->GetIPAddress());
|
2007-01-11 00:49:22 +01:00
|
|
|
|
|
|
|
if (params[4] && (ptr = strchr(buf, ':')))
|
|
|
|
{
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), buf);
|
2007-01-01 04:33:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-01-02 03:40:32 +01:00
|
|
|
static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params)
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
|
|
|
int index = params[1];
|
2007-02-07 09:44:48 +01:00
|
|
|
if ((index < 1) || (index > g_Players.GetMaxClients()))
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
2007-01-02 03:40:32 +01:00
|
|
|
return pCtx->ThrowNativeError("Invalid client index %d.", index);
|
|
|
|
}
|
|
|
|
|
2007-02-07 09:44:48 +01:00
|
|
|
CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
|
2007-02-05 21:32:34 +01:00
|
|
|
if (!pPlayer->IsConnected())
|
2007-01-02 03:40:32 +01:00
|
|
|
{
|
|
|
|
return pCtx->ThrowNativeError("Client %d is not connected.", index);
|
2007-01-01 04:33:14 +01:00
|
|
|
}
|
|
|
|
|
2007-02-07 20:30:53 +01:00
|
|
|
if (!pPlayer->IsAuthorized())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-05 21:32:34 +01:00
|
|
|
pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), pPlayer->GetAuthString());
|
2007-02-07 20:30:53 +01:00
|
|
|
|
2007-01-01 04:33:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-01-02 03:40:32 +01:00
|
|
|
static cell_t sm_IsPlayerConnected(IPluginContext *pCtx, const cell_t *params)
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
|
|
|
int index = params[1];
|
2007-02-07 09:44:48 +01:00
|
|
|
if ((index < 1) || (index > g_Players.GetMaxClients()))
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
2007-01-02 03:40:32 +01:00
|
|
|
return pCtx->ThrowNativeError("Invalid client index %d.", index);
|
2007-01-01 04:33:14 +01:00
|
|
|
}
|
|
|
|
|
2007-02-07 09:44:48 +01:00
|
|
|
return (g_Players.GetPlayerByIndex(index)->IsConnected()) ? 1 : 0;
|
2007-01-01 04:33:14 +01:00
|
|
|
}
|
|
|
|
|
2007-01-02 03:40:32 +01:00
|
|
|
static cell_t sm_IsPlayerIngame(IPluginContext *pCtx, const cell_t *params)
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
|
|
|
int index = params[1];
|
2007-02-07 09:44:48 +01:00
|
|
|
if ((index < 1) || (index > g_Players.GetMaxClients()))
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
2007-01-02 03:40:32 +01:00
|
|
|
return pCtx->ThrowNativeError("Invalid client index %d.", index);
|
2007-01-01 04:33:14 +01:00
|
|
|
}
|
|
|
|
|
2007-02-07 09:44:48 +01:00
|
|
|
return (g_Players.GetPlayerByIndex(index)->IsInGame()) ? 1 : 0;
|
2007-01-01 04:33:14 +01:00
|
|
|
}
|
|
|
|
|
2007-01-02 03:40:32 +01:00
|
|
|
static cell_t sm_IsPlayerAuthorized(IPluginContext *pCtx, const cell_t *params)
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
|
|
|
int index = params[1];
|
2007-02-07 09:44:48 +01:00
|
|
|
if ((index < 1) || (index > g_Players.GetMaxClients()))
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
2007-01-02 03:40:32 +01:00
|
|
|
return pCtx->ThrowNativeError("Invalid client index %d.", index);
|
2007-01-01 04:33:14 +01:00
|
|
|
}
|
|
|
|
|
2007-02-07 09:44:48 +01:00
|
|
|
return (g_Players.GetPlayerByIndex(index)->IsAuthorized()) ? 1 : 0;
|
2007-01-01 04:33:14 +01:00
|
|
|
}
|
|
|
|
|
2007-01-02 03:40:32 +01:00
|
|
|
static cell_t sm_IsPlayerFakeClient(IPluginContext *pCtx, const cell_t *params)
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
|
|
|
int index = params[1];
|
2007-02-07 09:44:48 +01:00
|
|
|
if ((index < 1) || (index > g_Players.GetMaxClients()))
|
2007-01-01 04:33:14 +01:00
|
|
|
{
|
2007-01-02 03:40:32 +01:00
|
|
|
return pCtx->ThrowNativeError("Invalid client index %d.", index);
|
|
|
|
}
|
|
|
|
|
2007-02-07 09:44:48 +01:00
|
|
|
CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
|
2007-02-05 21:32:34 +01:00
|
|
|
if (!pPlayer->IsConnected())
|
2007-01-02 03:40:32 +01:00
|
|
|
{
|
|
|
|
return pCtx->ThrowNativeError("Client %d is not connected.", index);
|
2007-01-01 04:33:14 +01:00
|
|
|
}
|
|
|
|
|
2007-02-05 21:32:34 +01:00
|
|
|
return (pPlayer->IsFakeClient()) ? 1 : 0;
|
2007-01-02 03:40:32 +01:00
|
|
|
}
|
|
|
|
|
2007-01-11 00:49:22 +01:00
|
|
|
static cell_t sm_PrintToServer(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
|
|
|
char buffer[1024];
|
|
|
|
char *fmt;
|
|
|
|
int arg = 2;
|
|
|
|
|
|
|
|
pCtx->LocalToString(params[1], &fmt);
|
|
|
|
size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
|
|
|
|
|
|
|
|
buffer[res++] = '\n';
|
|
|
|
buffer[res] = '\0';
|
|
|
|
|
|
|
|
META_CONPRINT(buffer);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_PrintToConsole(IPluginContext *pCtx, const cell_t *params)
|
|
|
|
{
|
|
|
|
int index = params[1];
|
2007-02-07 09:44:48 +01:00
|
|
|
if ((index < 1) || (index > g_Players.GetMaxClients()))
|
2007-01-11 00:49:22 +01:00
|
|
|
{
|
|
|
|
return pCtx->ThrowNativeError("Invalid client index %d.", index);
|
|
|
|
}
|
|
|
|
|
2007-02-07 09:44:48 +01:00
|
|
|
CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
|
2007-02-05 21:32:34 +01:00
|
|
|
if (!pPlayer->IsInGame())
|
2007-01-11 00:49:22 +01:00
|
|
|
{
|
|
|
|
return pCtx->ThrowNativeError("Client %d is not in game.", index);
|
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[1024];
|
|
|
|
char *fmt;
|
|
|
|
int arg = 3;
|
|
|
|
|
|
|
|
pCtx->LocalToString(params[2], &fmt);
|
|
|
|
size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
|
|
|
|
|
|
|
|
buffer[res++] = '\n';
|
|
|
|
buffer[res] = '\0';
|
|
|
|
|
2007-02-05 21:32:34 +01:00
|
|
|
engine->ClientPrintf(pPlayer->GetEdict(), buffer);
|
2007-01-11 00:49:22 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-01-02 03:40:32 +01:00
|
|
|
REGISTER_NATIVES(playernatives)
|
|
|
|
{
|
|
|
|
{"GetMaxClients", sm_GetMaxClients},
|
|
|
|
{"GetClientCount", sm_GetClientCount},
|
|
|
|
{"GetClientName", sm_GetClientName},
|
|
|
|
{"GetClientIP", sm_GetClientIP},
|
|
|
|
{"GetClientAuthString", sm_GetClientAuthStr},
|
|
|
|
{"IsPlayerConnected", sm_IsPlayerConnected},
|
|
|
|
{"IsPlayerInGame", sm_IsPlayerIngame},
|
|
|
|
{"IsPlayerAuthorized", sm_IsPlayerAuthorized},
|
|
|
|
{"IsPlayerFakeClient", sm_IsPlayerFakeClient},
|
2007-01-11 00:49:22 +01:00
|
|
|
{"PrintToServer", sm_PrintToServer},
|
|
|
|
{"PrintToConsole", sm_PrintToConsole},
|
2007-01-02 03:40:32 +01:00
|
|
|
{NULL, NULL}
|
2007-01-25 10:19:38 +01:00
|
|
|
};
|
|
|
|
|