dvander@370: /** dvander@370: * =============================================================== dvander@370: * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. dvander@370: * =============================================================== dvander@370: * dvander@370: * This file is not open source and may not be copied without explicit dvander@370: * written permission of AlliedModders LLC. This file may not be redistributed dvander@370: * in whole or significant part. dvander@370: * For information, see LICENSE.txt or http://www.sourcemod.net/license.php dvander@370: * dvander@370: * Version: $Id$ dvander@370: */ dvander@370: faluco@242: #include "CPlayerManager.h" faluco@280: #include "sm_stringutil.h" faluco@242: faluco@260: static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params) faluco@242: { faluco@242: if (params[1]) faluco@242: { faluco@242: return g_PlayerManager.GetPlayerCount(); faluco@242: } faluco@242: faluco@242: int maxplayers = g_PlayerManager.GetMaxClients(); faluco@242: int count = 0; faluco@242: for (int i=1; i<=maxplayers; ++i) faluco@242: { faluco@242: CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(i); faluco@242: if ((pPlayer->IsPlayerConnected()) && !(pPlayer->IsPlayerInGame())) faluco@242: { faluco@242: count++; faluco@242: } faluco@242: } faluco@242: faluco@242: return (g_PlayerManager.GetPlayerCount() + count); faluco@242: } faluco@242: faluco@260: static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params) faluco@242: { faluco@242: return g_PlayerManager.GetMaxClients(); faluco@242: } faluco@242: faluco@260: static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params) faluco@242: { faluco@242: int index = params[1]; faluco@242: if ((index < 1) || (index > g_PlayerManager.GetMaxClients())) faluco@242: { faluco@260: return pCtx->ThrowNativeError("Invalid client index %d.", index); faluco@242: } faluco@242: faluco@260: CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index); faluco@260: if (!pPlayer->IsPlayerConnected()) faluco@260: { faluco@260: return pCtx->ThrowNativeError("Client %d is not connected.", index); faluco@260: } faluco@260: faluco@260: pCtx->StringToLocalUTF8(params[2], static_cast(params[3]), pPlayer->PlayerName(), NULL); faluco@242: return 1; faluco@242: } faluco@242: faluco@260: static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params) faluco@242: { faluco@242: int index = params[1]; faluco@242: if ((index < 1) || (index > g_PlayerManager.GetMaxClients())) faluco@242: { faluco@260: return pCtx->ThrowNativeError("Invalid client index %d.", index); faluco@242: } faluco@242: faluco@260: CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index); faluco@260: if (!pPlayer->IsPlayerConnected()) faluco@260: { faluco@260: return pCtx->ThrowNativeError("Client %d is not connected.", index); faluco@260: } faluco@260: faluco@280: char buf[64], *ptr; faluco@280: strcpy(buf, pPlayer->PlayerIP()); faluco@280: faluco@280: if (params[4] && (ptr = strchr(buf, ':'))) faluco@280: { faluco@280: *ptr = '\0'; faluco@280: } faluco@280: faluco@280: pCtx->StringToLocal(params[2], static_cast(params[3]), buf); faluco@242: return 1; faluco@242: } faluco@242: faluco@260: static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params) faluco@242: { faluco@242: int index = params[1]; faluco@242: if ((index < 1) || (index > g_PlayerManager.GetMaxClients())) faluco@242: { faluco@260: return pCtx->ThrowNativeError("Invalid client index %d.", index); faluco@242: } faluco@242: faluco@260: CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index); faluco@260: if (!pPlayer->IsPlayerConnected()) faluco@260: { faluco@260: return pCtx->ThrowNativeError("Client %d is not connected.", index); faluco@260: } faluco@260: faluco@260: pCtx->StringToLocal(params[2], static_cast(params[3]), pPlayer->PlayerAuthString()); faluco@242: return 1; faluco@242: } faluco@242: faluco@260: static cell_t sm_IsPlayerConnected(IPluginContext *pCtx, const cell_t *params) faluco@242: { faluco@242: int index = params[1]; faluco@242: if ((index < 1) || (index > g_PlayerManager.GetMaxClients())) faluco@242: { faluco@260: return pCtx->ThrowNativeError("Invalid client index %d.", index); faluco@242: } faluco@242: faluco@242: return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerConnected()) ? 1 : 0; faluco@242: } faluco@242: faluco@260: static cell_t sm_IsPlayerIngame(IPluginContext *pCtx, const cell_t *params) faluco@242: { faluco@242: int index = params[1]; faluco@242: if ((index < 1) || (index > g_PlayerManager.GetMaxClients())) faluco@242: { faluco@260: return pCtx->ThrowNativeError("Invalid client index %d.", index); faluco@242: } faluco@242: faluco@242: return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerInGame()) ? 1 : 0; faluco@242: } faluco@242: faluco@260: static cell_t sm_IsPlayerAuthorized(IPluginContext *pCtx, const cell_t *params) faluco@242: { faluco@242: int index = params[1]; faluco@242: if ((index < 1) || (index > g_PlayerManager.GetMaxClients())) faluco@242: { faluco@260: return pCtx->ThrowNativeError("Invalid client index %d.", index); faluco@242: } faluco@242: faluco@242: return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerAuthorized()) ? 1 : 0; faluco@242: } faluco@242: faluco@260: static cell_t sm_IsPlayerFakeClient(IPluginContext *pCtx, const cell_t *params) faluco@242: { faluco@242: int index = params[1]; faluco@242: if ((index < 1) || (index > g_PlayerManager.GetMaxClients())) faluco@242: { faluco@260: return pCtx->ThrowNativeError("Invalid client index %d.", index); faluco@260: } faluco@260: faluco@260: CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index); faluco@260: if (!pPlayer->IsPlayerConnected()) faluco@260: { faluco@260: return pCtx->ThrowNativeError("Client %d is not connected.", index); faluco@242: } faluco@242: faluco@260: return (pPlayer->IsPlayerFakeClient()) ? 1 : 0; faluco@260: } faluco@260: faluco@280: static cell_t sm_PrintToServer(IPluginContext *pCtx, const cell_t *params) faluco@280: { faluco@280: char buffer[1024]; faluco@280: char *fmt; faluco@280: int arg = 2; faluco@280: faluco@280: pCtx->LocalToString(params[1], &fmt); faluco@280: size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg); faluco@280: faluco@280: buffer[res++] = '\n'; faluco@280: buffer[res] = '\0'; faluco@280: faluco@280: META_CONPRINT(buffer); faluco@280: faluco@280: return 1; faluco@280: } faluco@280: faluco@280: static cell_t sm_PrintToConsole(IPluginContext *pCtx, const cell_t *params) faluco@280: { faluco@280: int index = params[1]; faluco@280: if ((index < 1) || (index > g_PlayerManager.GetMaxClients())) faluco@280: { faluco@280: return pCtx->ThrowNativeError("Invalid client index %d.", index); faluco@280: } faluco@280: faluco@280: CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index); faluco@280: if (!pPlayer->IsPlayerInGame()) faluco@280: { faluco@280: return pCtx->ThrowNativeError("Client %d is not in game.", index); faluco@280: } faluco@280: faluco@280: char buffer[1024]; faluco@280: char *fmt; faluco@280: int arg = 3; faluco@280: faluco@280: pCtx->LocalToString(params[2], &fmt); faluco@280: size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg); faluco@280: faluco@280: buffer[res++] = '\n'; faluco@280: buffer[res] = '\0'; faluco@280: faluco@280: engine->ClientPrintf(pPlayer->GetPlayerEdict(), buffer); faluco@280: faluco@280: return 1; faluco@280: } faluco@280: faluco@260: REGISTER_NATIVES(playernatives) faluco@260: { faluco@260: {"GetMaxClients", sm_GetMaxClients}, faluco@260: {"GetClientCount", sm_GetClientCount}, faluco@260: {"GetClientName", sm_GetClientName}, faluco@260: {"GetClientIP", sm_GetClientIP}, faluco@260: {"GetClientAuthString", sm_GetClientAuthStr}, faluco@260: {"IsPlayerConnected", sm_IsPlayerConnected}, faluco@260: {"IsPlayerInGame", sm_IsPlayerIngame}, faluco@260: {"IsPlayerAuthorized", sm_IsPlayerAuthorized}, faluco@260: {"IsPlayerFakeClient", sm_IsPlayerFakeClient}, faluco@280: {"PrintToServer", sm_PrintToServer}, faluco@280: {"PrintToConsole", sm_PrintToConsole}, faluco@260: {NULL, NULL} dvander@351: }; dvander@351: