core/smn_player.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Feb 08 21:41:28 2007 +0000 (2007-02-08)
changeset 457 9b8a85bf4175
parent 456 b181fc6f59c7
child 458 77520125ee67
permissions -rw-r--r--
Initial import of convar stuff
1) Natives to create, find, set, and get convars
2) "cvars" option added to sm menu
3) Some internal additions to CPlugin to store a convar list
Still some things left to do...
     1 /**
     2  * ===============================================================
     3  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ===============================================================
     5  *
     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
    10  *
    11  * Version: $Id$
    12  */
    13 
    14 #include "CPlayerManager.h"
    15 #include "sm_stringutil.h"
    16 
    17 static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params)
    18 {
    19 	if (params[1])
    20 	{
    21 		return g_Players.NumPlayers();
    22 	}
    23 
    24 	int maxplayers = g_Players.MaxClients();
    25 	int count = 0;
    26 	for (int i=1; i<=maxplayers; ++i)
    27 	{
    28 		CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
    29 		if ((pPlayer->IsConnected()) && !(pPlayer->IsInGame()))
    30 		{
    31 			count++;
    32 		}
    33 	}
    34 
    35 	return (g_Players.NumPlayers() + count);
    36 }
    37 
    38 static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params)
    39 {
    40 	return g_Players.MaxClients();
    41 }
    42 
    43 static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params)
    44 {
    45 	int index = params[1];
    46 	if ((index < 1) || (index > g_Players.GetMaxClients()))
    47 	{
    48 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
    49 	}
    50 
    51 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
    52 	if (!pPlayer->IsConnected())
    53 	{
    54 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
    55 	}
    56 
    57 	pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), pPlayer->GetName(), NULL);
    58 	return 1;
    59 }
    60 
    61 static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params)
    62 {
    63 	int index = params[1];
    64 	if ((index < 1) || (index > g_Players.GetMaxClients()))
    65 	{
    66 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
    67 	}
    68 
    69 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
    70 	if (!pPlayer->IsConnected())
    71 	{
    72 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
    73 	}
    74 
    75 	char buf[64], *ptr;
    76 	strcpy(buf, pPlayer->GetIPAddress());
    77 
    78 	if (params[4] && (ptr = strchr(buf, ':')))
    79 	{
    80 		*ptr = '\0';
    81 	}
    82 
    83 	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), buf);
    84 	return 1;
    85 }
    86 
    87 static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params)
    88 {
    89 	int index = params[1];
    90 	if ((index < 1) || (index > g_Players.GetMaxClients()))
    91 	{
    92 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
    93 	}
    94 
    95 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
    96 	if (!pPlayer->IsConnected())
    97 	{
    98 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
    99 	}
   100 
   101 	if (!pPlayer->IsAuthorized())
   102 	{
   103 		return 0;
   104 	}
   105 
   106 	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), pPlayer->GetAuthString());
   107 
   108 	return 1;
   109 }
   110 
   111 static cell_t sm_IsPlayerConnected(IPluginContext *pCtx, const cell_t *params)
   112 {
   113 	int index = params[1];
   114 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   115 	{
   116 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   117 	}
   118 
   119 	return (g_Players.GetPlayerByIndex(index)->IsConnected()) ? 1 : 0;
   120 }
   121 
   122 static cell_t sm_IsPlayerIngame(IPluginContext *pCtx, const cell_t *params)
   123 {
   124 	int index = params[1];
   125 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   126 	{
   127 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   128 	}
   129 
   130 	return (g_Players.GetPlayerByIndex(index)->IsInGame()) ? 1 : 0;
   131 }
   132 
   133 static cell_t sm_IsPlayerAuthorized(IPluginContext *pCtx, const cell_t *params)
   134 {
   135 	int index = params[1];
   136 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   137 	{
   138 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   139 	}
   140 
   141 	return (g_Players.GetPlayerByIndex(index)->IsAuthorized()) ? 1 : 0;
   142 }
   143 
   144 static cell_t sm_IsPlayerFakeClient(IPluginContext *pCtx, const cell_t *params)
   145 {
   146 	int index = params[1];
   147 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   148 	{
   149 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   150 	}
   151 
   152 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
   153 	if (!pPlayer->IsConnected())
   154 	{
   155 		return pCtx->ThrowNativeError("Client %d is not connected.", index);
   156 	}
   157 
   158 	return (pPlayer->IsFakeClient()) ? 1 : 0;
   159 }
   160 
   161 static cell_t sm_PrintToServer(IPluginContext *pCtx, const cell_t *params)
   162 {
   163 	char buffer[1024];
   164 	char *fmt;
   165 	int arg = 2;
   166 
   167 	pCtx->LocalToString(params[1], &fmt);
   168 	size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
   169 
   170 	buffer[res++] = '\n';
   171 	buffer[res] = '\0';
   172 
   173 	META_CONPRINT(buffer);
   174 
   175 	return 1;
   176 }
   177 
   178 static cell_t sm_GetClientInfo(IPluginContext *pContext, const cell_t *params)
   179 {
   180 	int client = params[1];
   181 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   182 	if (!pPlayer)
   183 	{
   184 		return pContext->ThrowNativeError("Invalid client index %d.", client);
   185 	}
   186 	if (!pPlayer->IsConnected())
   187 	{
   188 		return pContext->ThrowNativeError("Client %d is not connected.", client);
   189 	}
   190 
   191 	char *key;
   192 	pContext->LocalToString(params[2], &key);
   193 
   194 	const char *val = engine->GetClientConVarValue(client, key);
   195 	if (!val)
   196 	{
   197 		return false;
   198 	}
   199 
   200 	pContext->StringToLocalUTF8(params[3], params[4], val, NULL);
   201 	return 1;
   202 }
   203 
   204 static cell_t sm_PrintToConsole(IPluginContext *pCtx, const cell_t *params)
   205 {
   206 	int index = params[1];
   207 	if ((index < 1) || (index > g_Players.GetMaxClients()))
   208 	{
   209 		return pCtx->ThrowNativeError("Invalid client index %d.", index);
   210 	}
   211 
   212 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
   213 	if (!pPlayer->IsInGame())
   214 	{
   215 		return pCtx->ThrowNativeError("Client %d is not in game.", index);
   216 	}
   217 
   218 	char buffer[1024];
   219 	char *fmt;
   220 	int arg = 3;
   221 
   222 	pCtx->LocalToString(params[2], &fmt);
   223 	size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
   224 
   225 	buffer[res++] = '\n';
   226 	buffer[res] = '\0';
   227 
   228 	engine->ClientPrintf(pPlayer->GetEdict(), buffer);
   229 
   230 	return 1;
   231 }
   232 
   233 REGISTER_NATIVES(playernatives)
   234 {
   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},
   247 	{NULL,						NULL}
   248 };
   249