|
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 |
|
|
faluco@280
|
178 |
static cell_t sm_PrintToConsole(IPluginContext *pCtx, const cell_t *params) |
|
faluco@280
|
179 |
{ |
|
faluco@280
|
180 |
int index = params[1]; |
|
dvander@448
|
181 |
if ((index < 1) || (index > g_Players.GetMaxClients())) |
|
faluco@280
|
182 |
{ |
|
faluco@280
|
183 |
return pCtx->ThrowNativeError("Invalid client index %d.", index); |
|
faluco@280
|
184 |
} |
|
faluco@280
|
185 |
|
|
dvander@448
|
186 |
CPlayer *pPlayer = g_Players.GetPlayerByIndex(index); |
|
dvander@435
|
187 |
if (!pPlayer->IsInGame()) |
|
faluco@280
|
188 |
{ |
|
faluco@280
|
189 |
return pCtx->ThrowNativeError("Client %d is not in game.", index); |
|
faluco@280
|
190 |
} |
|
faluco@280
|
191 |
|
|
faluco@280
|
192 |
char buffer[1024]; |
|
faluco@280
|
193 |
char *fmt; |
|
faluco@280
|
194 |
int arg = 3; |
|
faluco@280
|
195 |
|
|
faluco@280
|
196 |
pCtx->LocalToString(params[2], &fmt); |
|
faluco@280
|
197 |
size_t res = atcprintf(buffer, sizeof(buffer)-2, fmt, pCtx, params, &arg); |
|
faluco@280
|
198 |
|
|
faluco@280
|
199 |
buffer[res++] = '\n'; |
|
faluco@280
|
200 |
buffer[res] = '\0'; |
|
faluco@280
|
201 |
|
|
dvander@435
|
202 |
engine->ClientPrintf(pPlayer->GetEdict(), buffer); |
|
faluco@280
|
203 |
|
|
faluco@280
|
204 |
return 1; |
|
faluco@280
|
205 |
} |
|
faluco@280
|
206 |
|
|
faluco@260
|
207 |
REGISTER_NATIVES(playernatives) |
|
faluco@260
|
208 |
{ |
|
faluco@260
|
209 |
{"GetMaxClients", sm_GetMaxClients}, |
|
faluco@260
|
210 |
{"GetClientCount", sm_GetClientCount}, |
|
faluco@260
|
211 |
{"GetClientName", sm_GetClientName}, |
|
faluco@260
|
212 |
{"GetClientIP", sm_GetClientIP}, |
|
faluco@260
|
213 |
{"GetClientAuthString", sm_GetClientAuthStr}, |
|
faluco@260
|
214 |
{"IsPlayerConnected", sm_IsPlayerConnected}, |
|
faluco@260
|
215 |
{"IsPlayerInGame", sm_IsPlayerIngame}, |
|
faluco@260
|
216 |
{"IsPlayerAuthorized", sm_IsPlayerAuthorized}, |
|
faluco@260
|
217 |
{"IsPlayerFakeClient", sm_IsPlayerFakeClient}, |
|
faluco@280
|
218 |
{"PrintToServer", sm_PrintToServer}, |
|
faluco@280
|
219 |
{"PrintToConsole", sm_PrintToConsole}, |
|
faluco@260
|
220 |
{NULL, NULL} |
|
dvander@351
|
221 |
}; |
|
dvander@351
|
222 |
|