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...
dvander@370
     1
/**
dvander@370
     2
 * ===============================================================
dvander@370
     3
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
dvander@370
     4
 * ===============================================================
dvander@370
     5
 *
dvander@370
     6
 * This file is not open source and may not be copied without explicit
dvander@370
     7
 * written permission of AlliedModders LLC.  This file may not be redistributed 
dvander@370
     8
 * in whole or significant part.
dvander@370
     9
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
dvander@370
    10
 *
dvander@370
    11
 * Version: $Id$
dvander@370
    12
 */
dvander@370
    13
faluco@242
    14
#include "CPlayerManager.h"
faluco@280
    15
#include "sm_stringutil.h"
faluco@242
    16
faluco@260
    17
static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params)
faluco@242
    18
{
faluco@242
    19
	if (params[1])
faluco@242
    20
	{
dvander@448
    21
		return g_Players.NumPlayers();
faluco@242
    22
	}
faluco@242
    23
dvander@448
    24
	int maxplayers = g_Players.MaxClients();
faluco@242
    25
	int count = 0;
faluco@242
    26
	for (int i=1; i<=maxplayers; ++i)
faluco@242
    27
	{
dvander@448
    28
		CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
dvander@435
    29
		if ((pPlayer->IsConnected()) && !(pPlayer->IsInGame()))
faluco@242
    30
		{
faluco@242
    31
			count++;
faluco@242
    32
		}
faluco@242
    33
	}
faluco@242
    34
dvander@448
    35
	return (g_Players.NumPlayers() + count);
faluco@242
    36
}
faluco@242
    37
faluco@260
    38
static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params)
faluco@242
    39
{
dvander@448
    40
	return g_Players.MaxClients();
faluco@242
    41
}
faluco@242
    42
faluco@260
    43
static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params)
faluco@242
    44
{
faluco@242
    45
	int index = params[1];
dvander@448
    46
	if ((index < 1) || (index > g_Players.GetMaxClients()))
faluco@242
    47
	{
faluco@260
    48
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@242
    49
	}
faluco@242
    50
dvander@448
    51
	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
dvander@435
    52
	if (!pPlayer->IsConnected())
faluco@260
    53
	{
faluco@260
    54
		return pCtx->ThrowNativeError("Client %d is not connected.", index);
faluco@260
    55
	}
faluco@260
    56
dvander@435
    57
	pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), pPlayer->GetName(), NULL);
faluco@242
    58
	return 1;
faluco@242
    59
}
faluco@242
    60
faluco@260
    61
static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params)
faluco@242
    62
{
faluco@242
    63
	int index = params[1];
dvander@448
    64
	if ((index < 1) || (index > g_Players.GetMaxClients()))
faluco@242
    65
	{
faluco@260
    66
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@242
    67
	}
faluco@242
    68
dvander@448
    69
	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
dvander@435
    70
	if (!pPlayer->IsConnected())
faluco@260
    71
	{
faluco@260
    72
		return pCtx->ThrowNativeError("Client %d is not connected.", index);
faluco@260
    73
	}
faluco@260
    74
faluco@280
    75
	char buf[64], *ptr;
dvander@435
    76
	strcpy(buf, pPlayer->GetIPAddress());
faluco@280
    77
faluco@280
    78
	if (params[4] && (ptr = strchr(buf, ':')))
faluco@280
    79
	{
faluco@280
    80
		*ptr = '\0';
faluco@280
    81
	}
faluco@280
    82
faluco@280
    83
	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), buf);
faluco@242
    84
	return 1;
faluco@242
    85
}
faluco@242
    86
faluco@260
    87
static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params)
faluco@242
    88
{
faluco@242
    89
	int index = params[1];
dvander@448
    90
	if ((index < 1) || (index > g_Players.GetMaxClients()))
faluco@242
    91
	{
faluco@260
    92
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@242
    93
	}
faluco@242
    94
dvander@448
    95
	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
dvander@435
    96
	if (!pPlayer->IsConnected())
faluco@260
    97
	{
faluco@260
    98
		return pCtx->ThrowNativeError("Client %d is not connected.", index);
faluco@260
    99
	}
faluco@260
   100
dvander@449
   101
	if (!pPlayer->IsAuthorized())
dvander@449
   102
	{
dvander@449
   103
		return 0;
dvander@449
   104
	}
dvander@449
   105
dvander@435
   106
	pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), pPlayer->GetAuthString());
dvander@449
   107
faluco@242
   108
	return 1;
faluco@242
   109
}
faluco@242
   110
faluco@260
   111
static cell_t sm_IsPlayerConnected(IPluginContext *pCtx, const cell_t *params)
faluco@242
   112
{
faluco@242
   113
	int index = params[1];
dvander@448
   114
	if ((index < 1) || (index > g_Players.GetMaxClients()))
faluco@242
   115
	{
faluco@260
   116
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@242
   117
	}
faluco@242
   118
dvander@448
   119
	return (g_Players.GetPlayerByIndex(index)->IsConnected()) ? 1 : 0;
faluco@242
   120
}
faluco@242
   121
faluco@260
   122
static cell_t sm_IsPlayerIngame(IPluginContext *pCtx, const cell_t *params)
faluco@242
   123
{
faluco@242
   124
	int index = params[1];
dvander@448
   125
	if ((index < 1) || (index > g_Players.GetMaxClients()))
faluco@242
   126
	{
faluco@260
   127
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@242
   128
	}
faluco@242
   129
dvander@448
   130
	return (g_Players.GetPlayerByIndex(index)->IsInGame()) ? 1 : 0;
faluco@242
   131
}
faluco@242
   132
faluco@260
   133
static cell_t sm_IsPlayerAuthorized(IPluginContext *pCtx, const cell_t *params)
faluco@242
   134
{
faluco@242
   135
	int index = params[1];
dvander@448
   136
	if ((index < 1) || (index > g_Players.GetMaxClients()))
faluco@242
   137
	{
faluco@260
   138
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@242
   139
	}
faluco@242
   140
dvander@448
   141
	return (g_Players.GetPlayerByIndex(index)->IsAuthorized()) ? 1 : 0;
faluco@242
   142
}
faluco@242
   143
faluco@260
   144
static cell_t sm_IsPlayerFakeClient(IPluginContext *pCtx, const cell_t *params)
faluco@242
   145
{
faluco@242
   146
	int index = params[1];
dvander@448
   147
	if ((index < 1) || (index > g_Players.GetMaxClients()))
faluco@242
   148
	{
faluco@260
   149
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@260
   150
	}
faluco@260
   151
dvander@448
   152
	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
dvander@435
   153
	if (!pPlayer->IsConnected())
faluco@260
   154
	{
faluco@260
   155
		return pCtx->ThrowNativeError("Client %d is not connected.", index);
faluco@242
   156
	}
faluco@242
   157
dvander@435
   158
	return (pPlayer->IsFakeClient()) ? 1 : 0;
faluco@260
   159
}
faluco@260
   160
faluco@280
   161
static cell_t sm_PrintToServer(IPluginContext *pCtx, const cell_t *params)
faluco@280
   162
{
faluco@280
   163
	char buffer[1024];
faluco@280
   164
	char *fmt;
faluco@280
   165
	int arg = 2;
faluco@280
   166
faluco@280
   167
	pCtx->LocalToString(params[1], &fmt);
faluco@280
   168
	size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
faluco@280
   169
faluco@280
   170
	buffer[res++] = '\n';
faluco@280
   171
	buffer[res] = '\0';
faluco@280
   172
faluco@280
   173
	META_CONPRINT(buffer);
faluco@280
   174
faluco@280
   175
	return 1;
faluco@280
   176
}
faluco@280
   177
dvander@456
   178
static cell_t sm_GetClientInfo(IPluginContext *pContext, const cell_t *params)
dvander@456
   179
{
dvander@456
   180
	int client = params[1];
dvander@456
   181
	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
dvander@456
   182
	if (!pPlayer)
dvander@456
   183
	{
dvander@456
   184
		return pContext->ThrowNativeError("Invalid client index %d.", client);
dvander@456
   185
	}
dvander@456
   186
	if (!pPlayer->IsConnected())
dvander@456
   187
	{
dvander@456
   188
		return pContext->ThrowNativeError("Client %d is not connected.", client);
dvander@456
   189
	}
dvander@456
   190
dvander@456
   191
	char *key;
dvander@456
   192
	pContext->LocalToString(params[2], &key);
dvander@456
   193
dvander@456
   194
	const char *val = engine->GetClientConVarValue(client, key);
dvander@456
   195
	if (!val)
dvander@456
   196
	{
dvander@456
   197
		return false;
dvander@456
   198
	}
dvander@456
   199
dvander@456
   200
	pContext->StringToLocalUTF8(params[3], params[4], val, NULL);
dvander@456
   201
	return 1;
dvander@456
   202
}
dvander@456
   203
faluco@280
   204
static cell_t sm_PrintToConsole(IPluginContext *pCtx, const cell_t *params)
faluco@280
   205
{
faluco@280
   206
	int index = params[1];
dvander@448
   207
	if ((index < 1) || (index > g_Players.GetMaxClients()))
faluco@280
   208
	{
faluco@280
   209
		return pCtx->ThrowNativeError("Invalid client index %d.", index);
faluco@280
   210
	}
faluco@280
   211
dvander@448
   212
	CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
dvander@435
   213
	if (!pPlayer->IsInGame())
faluco@280
   214
	{
faluco@280
   215
		return pCtx->ThrowNativeError("Client %d is not in game.", index);
faluco@280
   216
	}
faluco@280
   217
faluco@280
   218
	char buffer[1024];
faluco@280
   219
	char *fmt;
faluco@280
   220
	int arg = 3;
faluco@280
   221
faluco@280
   222
	pCtx->LocalToString(params[2], &fmt);
faluco@280
   223
	size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg);
faluco@280
   224
faluco@280
   225
	buffer[res++] = '\n';
faluco@280
   226
	buffer[res] = '\0';
faluco@280
   227
dvander@435
   228
	engine->ClientPrintf(pPlayer->GetEdict(), buffer);
faluco@280
   229
faluco@280
   230
	return 1;
faluco@280
   231
}
faluco@280
   232
faluco@260
   233
REGISTER_NATIVES(playernatives)
faluco@260
   234
{
faluco@260
   235
	{"GetMaxClients",			sm_GetMaxClients},
faluco@260
   236
	{"GetClientCount",			sm_GetClientCount},
faluco@260
   237
	{"GetClientName",			sm_GetClientName},
faluco@260
   238
	{"GetClientIP",				sm_GetClientIP},
faluco@260
   239
	{"GetClientAuthString",		sm_GetClientAuthStr},
faluco@260
   240
	{"IsPlayerConnected",		sm_IsPlayerConnected},
faluco@260
   241
	{"IsPlayerInGame",			sm_IsPlayerIngame},
faluco@260
   242
	{"IsPlayerAuthorized",		sm_IsPlayerAuthorized},
faluco@260
   243
	{"IsPlayerFakeClient",		sm_IsPlayerFakeClient},
faluco@280
   244
	{"PrintToServer",			sm_PrintToServer},
faluco@280
   245
	{"PrintToConsole",			sm_PrintToConsole},
dvander@456
   246
	{"GetClientInfo",			sm_GetClientInfo},
faluco@260
   247
	{NULL,						NULL}
dvander@351
   248
};
dvander@351
   249