2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
14 #include "CPlayerManager.h"
15 #include "sm_stringutil.h"
17 static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params)
21 return g_Players.NumPlayers();
24 int maxplayers = g_Players.MaxClients();
26 for (int i=1; i<=maxplayers; ++i)
28 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
29 if ((pPlayer->IsConnected()) && !(pPlayer->IsInGame()))
35 return (g_Players.NumPlayers() + count);
38 static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params)
40 return g_Players.MaxClients();
43 static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params)
45 int index = params[1];
46 if ((index < 1) || (index > g_Players.GetMaxClients()))
48 return pCtx->ThrowNativeError("Invalid client index %d.", index);
51 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
52 if (!pPlayer->IsConnected())
54 return pCtx->ThrowNativeError("Client %d is not connected.", index);
57 pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), pPlayer->GetName(), NULL);
61 static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params)
63 int index = params[1];
64 if ((index < 1) || (index > g_Players.GetMaxClients()))
66 return pCtx->ThrowNativeError("Invalid client index %d.", index);
69 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
70 if (!pPlayer->IsConnected())
72 return pCtx->ThrowNativeError("Client %d is not connected.", index);
76 strcpy(buf, pPlayer->GetIPAddress());
78 if (params[4] && (ptr = strchr(buf, ':')))
83 pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), buf);
87 static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params)
89 int index = params[1];
90 if ((index < 1) || (index > g_Players.GetMaxClients()))
92 return pCtx->ThrowNativeError("Invalid client index %d.", index);
95 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
96 if (!pPlayer->IsConnected())
98 return pCtx->ThrowNativeError("Client %d is not connected.", index);
101 if (!pPlayer->IsAuthorized())
106 pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), pPlayer->GetAuthString());
111 static cell_t sm_IsPlayerConnected(IPluginContext *pCtx, const cell_t *params)
113 int index = params[1];
114 if ((index < 1) || (index > g_Players.GetMaxClients()))
116 return pCtx->ThrowNativeError("Invalid client index %d.", index);
119 return (g_Players.GetPlayerByIndex(index)->IsConnected()) ? 1 : 0;
122 static cell_t sm_IsPlayerIngame(IPluginContext *pCtx, const cell_t *params)
124 int index = params[1];
125 if ((index < 1) || (index > g_Players.GetMaxClients()))
127 return pCtx->ThrowNativeError("Invalid client index %d.", index);
130 return (g_Players.GetPlayerByIndex(index)->IsInGame()) ? 1 : 0;
133 static cell_t sm_IsPlayerAuthorized(IPluginContext *pCtx, const cell_t *params)
135 int index = params[1];
136 if ((index < 1) || (index > g_Players.GetMaxClients()))
138 return pCtx->ThrowNativeError("Invalid client index %d.", index);
141 return (g_Players.GetPlayerByIndex(index)->IsAuthorized()) ? 1 : 0;
144 static cell_t sm_IsPlayerFakeClient(IPluginContext *pCtx, const cell_t *params)
146 int index = params[1];
147 if ((index < 1) || (index > g_Players.GetMaxClients()))
149 return pCtx->ThrowNativeError("Invalid client index %d.", index);
152 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
153 if (!pPlayer->IsConnected())
155 return pCtx->ThrowNativeError("Client %d is not connected.", index);
158 return (pPlayer->IsFakeClient()) ? 1 : 0;
161 static cell_t sm_PrintToServer(IPluginContext *pCtx, const cell_t *params)
167 pCtx->LocalToString(params[1], &fmt);
168 size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
170 buffer[res++] = '\n';
173 META_CONPRINT(buffer);
178 static cell_t sm_GetClientInfo(IPluginContext *pContext, const cell_t *params)
180 int client = params[1];
181 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
184 return pContext->ThrowNativeError("Invalid client index %d.", client);
186 if (!pPlayer->IsConnected())
188 return pContext->ThrowNativeError("Client %d is not connected.", client);
192 pContext->LocalToString(params[2], &key);
194 const char *val = engine->GetClientConVarValue(client, key);
200 pContext->StringToLocalUTF8(params[3], params[4], val, NULL);
204 static cell_t sm_PrintToConsole(IPluginContext *pCtx, const cell_t *params)
206 int index = params[1];
207 if ((index < 1) || (index > g_Players.GetMaxClients()))
209 return pCtx->ThrowNativeError("Invalid client index %d.", index);
212 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
213 if (!pPlayer->IsInGame())
215 return pCtx->ThrowNativeError("Client %d is not in game.", index);
222 pCtx->LocalToString(params[2], &fmt);
223 size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
225 buffer[res++] = '\n';
228 engine->ClientPrintf(pPlayer->GetEdict(), buffer);
233 REGISTER_NATIVES(playernatives)
235 {"GetMaxClients", sm_GetMaxClients},
236 {"GetClientCount", sm_GetClientCount},
237 {"GetClientName", sm_GetClientName},
238 {"GetClientIP", sm_GetClientIP},
239 {"GetClientAuthString", sm_GetClientAuthStr},
240 {"IsPlayerConnected", sm_IsPlayerConnected},
241 {"IsPlayerInGame", sm_IsPlayerIngame},
242 {"IsPlayerAuthorized", sm_IsPlayerAuthorized},
243 {"IsPlayerFakeClient", sm_IsPlayerFakeClient},
244 {"PrintToServer", sm_PrintToServer},
245 {"PrintToConsole", sm_PrintToConsole},
246 {"GetClientInfo", sm_GetClientInfo},