core/PlayerManager.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Sun Aug 26 23:35:18 2012 -0400 (2012-08-26)
changeset 3635 14e7af400bcc
parent 3632 2fe3c4c0eb76
parent 3630 b72450a8be42
child 3682 7b63d4a881c8
permissions -rw-r--r--
Merge.
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2009 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "PlayerManager.h"
    33 #include "ForwardSys.h"
    34 #include "ShareSys.h"
    35 #include "AdminCache.h"
    36 #include "ConCmdManager.h"
    37 #include "MenuStyle_Valve.h"
    38 #include "MenuStyle_Radio.h"
    39 #include "sm_stringutil.h"
    40 #include "CoreConfig.h"
    41 #include "TimerSys.h"
    42 #include "Logger.h"
    43 #include "ChatTriggers.h"
    44 #include "HalfLife2.h"
    45 #include <inetchannel.h>
    46 #include <iclient.h>
    47 #include <IGameConfigs.h>
    48 #include "ExtensionSys.h"
    49 #include <sourcemod_version.h>
    50 #include "ConsoleDetours.h"
    51 #include "logic_bridge.h"
    52 
    53 PlayerManager g_Players;
    54 bool g_OnMapStarted = false;
    55 IForward *PreAdminCheck = NULL;
    56 IForward *PostAdminCheck = NULL;
    57 IForward *PostAdminFilter = NULL;
    58 
    59 const unsigned int *g_NumPlayersToAuth = NULL;
    60 int lifestate_offset = -1;
    61 List<ICommandTargetProcessor *> target_processors;
    62 
    63 SH_DECL_HOOK5(IServerGameClients, ClientConnect, SH_NOATTRIB, 0, bool, edict_t *, const char *, const char *, char *, int);
    64 SH_DECL_HOOK2_void(IServerGameClients, ClientPutInServer, SH_NOATTRIB, 0, edict_t *, const char *);
    65 SH_DECL_HOOK1_void(IServerGameClients, ClientDisconnect, SH_NOATTRIB, 0, edict_t *);
    66 #if SOURCE_ENGINE >= SE_ORANGEBOX
    67 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
    68 #else
    69 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
    70 #endif
    71 SH_DECL_HOOK1_void(IServerGameClients, ClientSettingsChanged, SH_NOATTRIB, 0, edict_t *);
    72 SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int, int);
    73 
    74 #if SOURCE_ENGINE >= SE_ORANGEBOX
    75 SH_DECL_EXTERN1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
    76 #elif SOURCE_ENGINE == SE_DARKMESSIAH
    77 SH_DECL_EXTERN0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
    78 #else
    79 # if SH_IMPL_VERSION >= 4
    80  extern int __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    81 # else
    82  extern bool __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    83 # endif
    84 extern bool __SourceHook_FHRemoveConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    85 #endif
    86 
    87 ConCommand *maxplayersCmd = NULL;
    88 
    89 unsigned int g_PlayerSerialCount = 0;
    90 
    91 class KickPlayerTimer : public ITimedEvent
    92 {
    93 public:
    94 	ResultType OnTimer(ITimer *pTimer, void *pData)
    95 	{
    96 		int userid = (int)pData;
    97 		int client = g_Players.GetClientOfUserId(userid);
    98 		if (client)
    99 		{
   100 			CPlayer *player = g_Players.GetPlayerByIndex(client);
   101 			player->Kick("Your name is reserved by SourceMod; set your password to use it.");
   102 		}
   103 		return Pl_Stop;
   104 	}
   105 	void OnTimerEnd(ITimer *pTimer, void *pData)
   106 	{
   107 	}
   108 } s_KickPlayerTimer;
   109 
   110 PlayerManager::PlayerManager()
   111 {
   112 	m_AuthQueue = NULL;
   113 	m_FirstPass = false;
   114 	m_maxClients = 0;
   115 
   116 	m_SourceTVUserId = -1;
   117 	m_ReplayUserId = -1;
   118 
   119 	m_bUseSteamAdminAuth = true; // use steam auth by default
   120 
   121 	m_UserIdLookUp = new int[USHRT_MAX+1];
   122 	memset(m_UserIdLookUp, 0, sizeof(int) * (USHRT_MAX+1));
   123 }
   124 
   125 PlayerManager::~PlayerManager()
   126 {
   127 	g_NumPlayersToAuth = NULL;
   128 
   129 	delete [] m_AuthQueue;
   130 	delete [] m_UserIdLookUp;
   131 }
   132 
   133 void PlayerManager::OnSourceModAllInitialized()
   134 {
   135 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, serverClients, this, &PlayerManager::OnClientConnect, false);
   136 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, serverClients, this, &PlayerManager::OnClientConnect_Post, true);
   137 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, serverClients, this, &PlayerManager::OnClientPutInServer, true);
   138 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, serverClients, this, &PlayerManager::OnClientDisconnect, false);
   139 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, serverClients, this, &PlayerManager::OnClientDisconnect_Post, true);
   140 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, serverClients, this, &PlayerManager::OnClientCommand, false);
   141 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, serverClients, this, &PlayerManager::OnClientSettingsChanged, true);
   142 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, gamedll, this, &PlayerManager::OnServerActivate, true);
   143 
   144 	g_ShareSys.AddInterface(NULL, this);
   145 
   146 	ParamType p1[] = {Param_Cell, Param_String, Param_Cell};
   147 	ParamType p2[] = {Param_Cell};
   148 
   149 	m_clconnect = g_Forwards.CreateForward("OnClientConnect", ET_LowEvent, 3, p1);
   150 	m_clconnect_post = g_Forwards.CreateForward("OnClientConnected", ET_Ignore, 1, p2);
   151 	m_clputinserver = g_Forwards.CreateForward("OnClientPutInServer", ET_Ignore, 1, p2);
   152 	m_cldisconnect = g_Forwards.CreateForward("OnClientDisconnect", ET_Ignore, 1, p2);
   153 	m_cldisconnect_post = g_Forwards.CreateForward("OnClientDisconnect_Post", ET_Ignore, 1, p2);
   154 	m_clcommand = g_Forwards.CreateForward("OnClientCommand", ET_Hook, 2, NULL, Param_Cell, Param_Cell);
   155 	m_clinfochanged = g_Forwards.CreateForward("OnClientSettingsChanged", ET_Ignore, 1, p2);
   156 	m_clauth = g_Forwards.CreateForward("OnClientAuthorized", ET_Ignore, 2, NULL, Param_Cell, Param_String);
   157 	m_onActivate = g_Forwards.CreateForward("OnServerLoad", ET_Ignore, 0, NULL);
   158 	m_onActivate2 = g_Forwards.CreateForward("OnMapStart", ET_Ignore, 0, NULL);
   159 
   160 	PreAdminCheck = g_Forwards.CreateForward("OnClientPreAdminCheck", ET_Event, 1, p1);
   161 	PostAdminCheck = g_Forwards.CreateForward("OnClientPostAdminCheck", ET_Ignore, 1, p1);
   162 	PostAdminFilter = g_Forwards.CreateForward("OnClientPostAdminFilter", ET_Ignore, 1, p1);
   163 
   164 	m_bIsListenServer = !engine->IsDedicatedServer();
   165 	m_ListenClient = 0;
   166 
   167 	ConCommand *pCmd = FindCommand("maxplayers");
   168 	if (pCmd != NULL)
   169 	{
   170 		SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CmdMaxplayersCallback, true);
   171 		maxplayersCmd = pCmd;
   172 	}
   173 }
   174 
   175 void PlayerManager::OnSourceModShutdown()
   176 {
   177 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, serverClients, this, &PlayerManager::OnClientConnect, false);
   178 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, serverClients, this, &PlayerManager::OnClientConnect_Post, true);
   179 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, serverClients, this, &PlayerManager::OnClientPutInServer, true);
   180 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, serverClients, this, &PlayerManager::OnClientDisconnect, false);
   181 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, serverClients, this, &PlayerManager::OnClientDisconnect_Post, true);
   182 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, serverClients, this, &PlayerManager::OnClientCommand, false);
   183 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, serverClients, this, &PlayerManager::OnClientSettingsChanged, true);
   184 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, gamedll, this, &PlayerManager::OnServerActivate, true);
   185 
   186 	/* Release forwards */
   187 	g_Forwards.ReleaseForward(m_clconnect);
   188 	g_Forwards.ReleaseForward(m_clconnect_post);
   189 	g_Forwards.ReleaseForward(m_clputinserver);
   190 	g_Forwards.ReleaseForward(m_cldisconnect);
   191 	g_Forwards.ReleaseForward(m_cldisconnect_post);
   192 	g_Forwards.ReleaseForward(m_clcommand);
   193 	g_Forwards.ReleaseForward(m_clinfochanged);
   194 	g_Forwards.ReleaseForward(m_clauth);
   195 	g_Forwards.ReleaseForward(m_onActivate);
   196 	g_Forwards.ReleaseForward(m_onActivate2);
   197 
   198 	g_Forwards.ReleaseForward(PreAdminCheck);
   199 	g_Forwards.ReleaseForward(PostAdminCheck);
   200 	g_Forwards.ReleaseForward(PostAdminFilter);
   201 
   202 	delete [] m_Players;
   203 
   204 	if (maxplayersCmd != NULL)
   205 	{
   206 		SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, maxplayersCmd, CmdMaxplayersCallback, true);
   207 	}
   208 }
   209 
   210 ConfigResult PlayerManager::OnSourceModConfigChanged(const char *key, 
   211 												 const char *value, 
   212 												 ConfigSource source, 
   213 												 char *error, 
   214 												 size_t maxlength)
   215 {
   216 	if (strcmp(key, "PassInfoVar") == 0)
   217 	{
   218 		if (strcmp(value, "_password") != 0)
   219 		{
   220 			m_PassInfoVar.assign(value);
   221 		}
   222 		return ConfigResult_Accept;
   223 	} else if (strcmp(key, "AllowClLanguageVar") == 0) {
   224 		if (strcasecmp(value, "on") == 0)
   225 		{
   226 			m_QueryLang = true;
   227 		} else if (strcasecmp(value, "off") == 0) {
   228 			m_QueryLang = false;
   229 		} else {
   230 			UTIL_Format(error, maxlength, "Invalid value: must be \"on\" or \"off\"");
   231 			return ConfigResult_Reject;
   232 		}
   233 		return ConfigResult_Accept;
   234 	} else if (strcmp( key, "SteamAuthstringValidation" ) == 0) {
   235 		if (strcasecmp(value, "yes") == 0)
   236 		{
   237 			m_bUseSteamAdminAuth = true;
   238 		} else if ( strcasecmp(value, "no") == 0) {
   239 			m_bUseSteamAdminAuth = false;
   240 		} else {
   241 			UTIL_Format(error, maxlength, "Invalid value: must be \"yes\" or \"no\"");
   242 			return ConfigResult_Reject;
   243 		}
   244 		return ConfigResult_Accept;
   245 	}
   246 	return ConfigResult_Ignore;
   247 }
   248 
   249 void PlayerManager::OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
   250 {
   251 	static ConVar *tv_enable = icvar->FindVar("tv_enable");
   252 #if SOURCE_ENGINE == SE_ORANGEBOXVALVE
   253 	static ConVar *replay_enable = icvar->FindVar("replay_enable");
   254 #endif
   255 
   256 	// clientMax will not necessarily be correct here (such as on late SourceTV enable)
   257 	m_maxClients = gpGlobals->maxClients;
   258 
   259 	ICommandLine *commandLine = g_HL2.GetValveCommandLine();
   260 	m_bIsSourceTVActive = (tv_enable && tv_enable->GetBool() && (!commandLine || commandLine->FindParm("-nohltv") == 0));
   261 	m_bIsReplayActive = false;
   262 #if SOURCE_ENGINE == SE_ORANGEBOXVALVE
   263 	m_bIsReplayActive = (replay_enable && replay_enable->GetBool());
   264 #endif
   265 	m_PlayersSinceActive = 0;
   266 	
   267 	if (!m_FirstPass)
   268 	{
   269 		/* Initialize all players */
   270 
   271 		m_PlayerCount = 0;
   272 		m_Players = new CPlayer[ABSOLUTE_PLAYER_LIMIT + 1];
   273 		m_AuthQueue = new unsigned int[ABSOLUTE_PLAYER_LIMIT + 1];
   274 		m_FirstPass = true;
   275 
   276 		memset(m_AuthQueue, 0, sizeof(unsigned int) * (ABSOLUTE_PLAYER_LIMIT + 1));
   277 
   278 		g_NumPlayersToAuth = &m_AuthQueue[0];
   279 	}
   280 
   281 	g_PluginSys.SyncMaxClients(m_maxClients);
   282 
   283 	g_OnMapStarted = true;
   284 
   285 	g_Extensions.CallOnCoreMapStart(pEdictList, edictCount, m_maxClients);
   286 	m_onActivate->Execute(NULL);
   287 	m_onActivate2->Execute(NULL);
   288 
   289 	List<IClientListener *>::iterator iter;
   290 	for (iter = m_hooks.begin(); iter != m_hooks.end(); iter++)
   291 	{
   292 		if ((*iter)->GetClientListenerVersion() >= 5)
   293 		{
   294 			(*iter)->OnServerActivated(m_maxClients);
   295 		}
   296 	}
   297 
   298 	SMGlobalClass *cls = SMGlobalClass::head;
   299 	while (cls)
   300 	{
   301 		cls->OnSourceModLevelActivated();
   302 		cls = cls->m_pGlobalClassNext;
   303 	}
   304 
   305 	SM_ExecuteAllConfigs();
   306 }
   307 
   308 bool PlayerManager::IsServerActivated()
   309 {
   310 	return m_FirstPass;
   311 }
   312 
   313 bool PlayerManager::CheckSetAdmin(int index, CPlayer *pPlayer, AdminId id)
   314 {
   315 	const char *password = g_Admins.GetAdminPassword(id);
   316 	if (password != NULL)
   317 	{
   318 		if (m_PassInfoVar.size() < 1)
   319 		{
   320 			return false;
   321 		}
   322 
   323 		/* Whoa... the user needs a password! */
   324 		const char *given = engine->GetClientConVarValue(index, m_PassInfoVar.c_str());
   325 		if (!given || strcmp(given, password) != 0)
   326 		{
   327 			return false;
   328 		}
   329 	}
   330 
   331 	pPlayer->SetAdminId(id, false);
   332 
   333 	return true;
   334 }
   335 
   336 bool PlayerManager::CheckSetAdminName(int index, CPlayer *pPlayer, AdminId id)
   337 {
   338 	const char *password = g_Admins.GetAdminPassword(id);
   339 	if (password == NULL)
   340 	{
   341 		return false;
   342 	}
   343 
   344 	if (m_PassInfoVar.size() < 1)
   345 	{
   346 		return false;
   347 	}
   348 
   349 	/* Whoa... the user needs a password! */
   350 	const char *given = engine->GetClientConVarValue(index, m_PassInfoVar.c_str());
   351 	if (!given || strcmp(given, password) != 0)
   352 	{
   353 		return false;
   354 	}
   355 
   356 	pPlayer->SetAdminId(id, false);
   357 
   358 	return true;
   359 }
   360 
   361 void PlayerManager::RunAuthChecks()
   362 {
   363 	CPlayer *pPlayer;
   364 	const char *authstr;
   365 	unsigned int removed = 0;
   366 	for (unsigned int i=1; i<=m_AuthQueue[0]; i++)
   367 	{
   368 		pPlayer = &m_Players[m_AuthQueue[i]];
   369 		authstr = engine->GetPlayerNetworkIDString(pPlayer->m_pEdict);
   370 
   371 #if SOURCE_ENGINE >= SE_ORANGEBOX
   372 		// we can only easily check if the client is fully authed if we're on a recent engine
   373 		if (m_bUseSteamAdminAuth && !g_HL2.IsLANServer())
   374 		{
   375 			if (!pPlayer->IsAuthedBySteam())
   376 			{
   377 				continue; // we're using steam auth, and steam doesn't know about this player yet so we can't do anything about them for now
   378 			}
   379 		}
   380 #endif
   381 
   382 		if (authstr && authstr[0] != '\0'
   383 			&& (strcmp(authstr, "STEAM_ID_PENDING") != 0))
   384 		{
   385 			/* Set authorization */
   386 			pPlayer->Authorize(authstr);
   387 
   388 			/* Mark as removed from queue */
   389 			unsigned int client = m_AuthQueue[i];
   390 			m_AuthQueue[i] = 0;
   391 			removed++;
   392 
   393 			/* Send to extensions */
   394 			List<IClientListener *>::iterator iter;
   395 			IClientListener *pListener;
   396 			for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   397 			{
   398 				pListener = (*iter);
   399 				pListener->OnClientAuthorized(client, authstr);
   400 				if (!pPlayer->IsConnected())
   401 				{
   402 					break;
   403 				}
   404 			}
   405 
   406 			/* Send to plugins if player is still connected */
   407 			if (pPlayer->IsConnected() && m_clauth->GetFunctionCount())
   408 			{
   409 				/* :TODO: handle the case of a player disconnecting in the middle */
   410 				m_clauth->PushCell(client);
   411 				m_clauth->PushString(authstr);
   412 				m_clauth->Execute(NULL);
   413 			}
   414 
   415 			if (pPlayer->IsConnected())
   416 			{
   417 				pPlayer->Authorize_Post();
   418 			}
   419 		}
   420 	}
   421 
   422 	/* Clean up the queue */
   423 	if (removed)
   424 	{
   425 		/* We don't have to compact the list if it's empty */
   426 		if (removed != m_AuthQueue[0])
   427 		{
   428 			unsigned int diff = 0;
   429 			for (unsigned int i=1; i<=m_AuthQueue[0]; i++)
   430 			{
   431 				/* If this member is removed... */
   432 				if (m_AuthQueue[i] == 0)
   433 				{
   434 					/* Increase the differential */
   435 					diff++;
   436 				} else {
   437 					/* diff cannot increase faster than i+1 */
   438 					assert(i > diff);
   439 					assert(i - diff >= 1);
   440 					/* move this index down */
   441 					m_AuthQueue[i - diff] = m_AuthQueue[i];
   442 				}
   443 			}
   444 			m_AuthQueue[0] -= removed;
   445 		} else {
   446 			m_AuthQueue[0] = 0;
   447 		}
   448 	}
   449 }
   450 
   451 bool PlayerManager::OnClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen)
   452 {
   453 	int client = IndexOfEdict(pEntity);
   454 	CPlayer *pPlayer = &m_Players[client];
   455 	++m_PlayersSinceActive;
   456 
   457 	pPlayer->Initialize(pszName, pszAddress, pEntity);
   458 	
   459 	/* Get the client's language */
   460 	if (m_QueryLang)
   461 	{
   462 		const char *name;
   463 		if (!pPlayer->IsFakeClient() && (name=engine->GetClientConVarValue(client, "cl_language")))
   464 		{
   465 			unsigned int langid;
   466 			pPlayer->m_LangId = (translator->GetLanguageByName(name, &langid)) ? langid : translator->GetServerLanguage();
   467 		} else {
   468 			pPlayer->m_LangId = translator->GetServerLanguage();
   469 		}
   470 	}
   471 	
   472 	List<IClientListener *>::iterator iter;
   473 	IClientListener *pListener = NULL;
   474 	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   475 	{
   476 		pListener = (*iter);
   477 		if (!pListener->InterceptClientConnect(client, reject, maxrejectlen))
   478 		{
   479 			RETURN_META_VALUE(MRES_SUPERCEDE, false);
   480 		}
   481 	}
   482 
   483 	cell_t res = 1;
   484 
   485 	m_clconnect->PushCell(client);
   486 	m_clconnect->PushStringEx(reject, maxrejectlen, SM_PARAM_STRING_UTF8 | SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
   487 	m_clconnect->PushCell(maxrejectlen);
   488 	m_clconnect->Execute(&res);
   489 
   490 	if (res)
   491 	{
   492 		if (!pPlayer->IsAuthorized())
   493 		{
   494 			m_AuthQueue[++m_AuthQueue[0]] = client;
   495 		}
   496 
   497 		m_UserIdLookUp[engine->GetPlayerUserId(pEntity)] = client;
   498 	}
   499 	else
   500 	{
   501 		if (!pPlayer->IsFakeClient())
   502 		{
   503 			RETURN_META_VALUE(MRES_SUPERCEDE, false);
   504 		}
   505 	}
   506 
   507 	return true;
   508 }
   509 
   510 bool PlayerManager::OnClientConnect_Post(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen)
   511 {
   512 	int client = IndexOfEdict(pEntity);
   513 	bool orig_value = META_RESULT_ORIG_RET(bool);
   514 	CPlayer *pPlayer = &m_Players[client];
   515 
   516 	if (orig_value)
   517 	{
   518 		List<IClientListener *>::iterator iter;
   519 		IClientListener *pListener = NULL;
   520 		for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   521 		{
   522 			pListener = (*iter);
   523 			pListener->OnClientConnected(client);
   524 			if (!pPlayer->IsConnected())
   525 			{
   526 				return true;
   527 			}
   528 		}
   529 
   530 		if (!pPlayer->IsFakeClient() 
   531 			&& m_bIsListenServer
   532 			&& strncmp(pszAddress, "127.0.0.1", 9) == 0)
   533 		{
   534 			m_ListenClient = client;
   535 		}
   536 
   537 		cell_t res;
   538 		m_clconnect_post->PushCell(client);
   539 		m_clconnect_post->Execute(&res, NULL);
   540 	}
   541 	else
   542 	{
   543 		InvalidatePlayer(pPlayer);
   544 	}
   545 
   546 	return true;
   547 }
   548 
   549 void PlayerManager::OnClientPutInServer(edict_t *pEntity, const char *playername)
   550 {
   551 	cell_t res;
   552 	int client = IndexOfEdict(pEntity);
   553 	CPlayer *pPlayer = &m_Players[client];
   554 
   555 	/* If they're not connected, they're a bot */
   556 	if (!pPlayer->IsConnected())
   557 	{
   558 		/* Run manual connection routines */
   559 		char error[255];
   560 		const char *authid = engine->GetPlayerNetworkIDString(pEntity);
   561 		pPlayer->Authorize(authid);
   562 		pPlayer->m_bFakeClient = true;
   563 
   564 		/*
   565 		 * While we're already filtered to just bots, we'll do other checks to
   566 		 * make sure that the requisite services are enabled and that the bots
   567 		 * have joined at the expected time.
   568 		 *
   569 		 * Checking playerinfo's IsHLTV and IsReplay would be better and less
   570 		 * error-prone but will always show false until later in the frame,
   571 		 * after PutInServer and Activate, and we want it now!
   572 		 */
   573 		
   574 		// This doesn't actually get incremented until OnClientConnect. Fake it to check.
   575 		int newCount = m_PlayersSinceActive + 1;
   576 		int userId = engine->GetPlayerUserId(pEntity);
   577 
   578 #if SOURCE_ENGINE == SE_ORANGEBOXVALVE
   579 		if (m_bIsReplayActive && newCount == 1
   580 			&& (m_ReplayUserId == userId || strcmp(playername, "Replay") == 0))
   581 		{
   582 			pPlayer->m_bIsReplay = true;
   583 			m_ReplayUserId = userId;
   584 		}
   585 #endif
   586 
   587 		if (m_bIsSourceTVActive
   588 			&& ((!m_bIsReplayActive && newCount == 1)
   589 				|| (m_bIsReplayActive && newCount == 2))
   590 			&& (m_SourceTVUserId == userId || strcmp(playername, "SourceTV") == 0)
   591 			)
   592 		{
   593 			pPlayer->m_bIsSourceTV = true;
   594 			m_SourceTVUserId = userId;
   595 		}
   596 
   597 		if (!OnClientConnect(pEntity, playername, "127.0.0.1", error, sizeof(error)))
   598 		{
   599 			/* :TODO: kick the bot if it's rejected */
   600 			return;
   601 		}
   602 		List<IClientListener *>::iterator iter;
   603 		IClientListener *pListener = NULL;
   604 		for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   605 		{
   606 			pListener = (*iter);
   607 			pListener->OnClientConnected(client);
   608 			/* See if bot was kicked */
   609 			if (!pPlayer->IsConnected())
   610 			{
   611 				return;
   612 			}
   613 		}
   614 
   615 		cell_t res;
   616 		m_clconnect_post->PushCell(client);
   617 		m_clconnect_post->Execute(&res, NULL);
   618 
   619 		/* Now do authorization */
   620 		for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   621 		{
   622 			pListener = (*iter);
   623 			pListener->OnClientAuthorized(client, authid);
   624 		}
   625 		/* Finally, tell plugins */
   626 		if (m_clauth->GetFunctionCount())
   627 		{
   628 			m_clauth->PushCell(client);
   629 			m_clauth->PushString(authid);
   630 			m_clauth->Execute(NULL);
   631 		}
   632 		pPlayer->Authorize_Post();
   633 	}
   634 
   635 	if (playerinfo)
   636 	{
   637 		pPlayer->m_Info = playerinfo->GetPlayerInfo(pEntity);
   638 	}
   639 
   640 	pPlayer->Connect();
   641 	m_PlayerCount++;
   642 
   643 	List<IClientListener *>::iterator iter;
   644 	IClientListener *pListener = NULL;
   645 	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   646 	{
   647 		pListener = (*iter);
   648 		pListener->OnClientPutInServer(client);
   649 	}
   650 
   651 	m_clputinserver->PushCell(client);
   652 	m_clputinserver->Execute(&res, NULL);
   653 
   654 	if (pPlayer->IsAuthorized())
   655 	{
   656 		pPlayer->DoPostConnectAuthorization();
   657 	}
   658 }
   659 
   660 void PlayerManager::OnSourceModLevelEnd()
   661 {
   662 	/* Disconnect all bots still in game */
   663 	for (int i=1; i<=m_maxClients; i++)
   664 	{
   665 		if (m_Players[i].IsConnected())
   666 		{
   667 			OnClientDisconnect(m_Players[i].GetEdict());
   668 		}
   669 	}
   670 	m_PlayerCount = 0;
   671 }
   672 
   673 void PlayerManager::OnClientDisconnect(edict_t *pEntity)
   674 {
   675 	cell_t res;
   676 	int client = IndexOfEdict(pEntity);
   677 	CPlayer *pPlayer = &m_Players[client];
   678 
   679 	if (pPlayer->IsConnected())
   680 	{
   681 		m_cldisconnect->PushCell(client);
   682 		m_cldisconnect->Execute(&res, NULL);
   683 	}
   684 	else
   685 	{
   686 		/* We don't care, prevent a double call */
   687 		return;
   688 	}
   689 
   690 	if (pPlayer->WasCountedAsInGame())
   691 	{
   692 		m_PlayerCount--;
   693 	}
   694 
   695 	List<IClientListener *>::iterator iter;
   696 	IClientListener *pListener = NULL;
   697 	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   698 	{
   699 		pListener = (*iter);
   700 		pListener->OnClientDisconnecting(client);
   701 	}
   702 
   703 	InvalidatePlayer(pPlayer);
   704 
   705 	if (m_ListenClient == client)
   706 	{
   707 		m_ListenClient = 0;
   708 	}
   709 }
   710 
   711 void PlayerManager::OnClientDisconnect_Post(edict_t *pEntity)
   712 {
   713 	cell_t res;
   714 	int client = IndexOfEdict(pEntity);
   715 
   716 	m_cldisconnect_post->PushCell(client);
   717 	m_cldisconnect_post->Execute(&res, NULL);
   718 
   719 	List<IClientListener *>::iterator iter;
   720 	IClientListener *pListener = NULL;
   721 	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   722 	{
   723 		pListener = (*iter);
   724 		pListener->OnClientDisconnected(client);
   725 	}
   726 }
   727 
   728 void ClientConsolePrint(edict_t *e, const char *fmt, ...)
   729 {
   730 	char buffer[512];
   731 
   732 	va_list ap;
   733 	va_start(ap, fmt);
   734 	size_t len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
   735 	va_end(ap);
   736 
   737 	if (len >= sizeof(buffer) - 1)
   738 	{
   739 		buffer[sizeof(buffer) - 2] = '\n';
   740 		buffer[sizeof(buffer) - 1] = '\0';
   741 	} else {
   742 		buffer[len++] = '\n';
   743 		buffer[len] = '\0';
   744 	}
   745 
   746 	engine->ClientPrintf(e, buffer);
   747 }
   748 
   749 #if SOURCE_ENGINE >= SE_ORANGEBOX
   750 void PlayerManager::OnClientCommand(edict_t *pEntity, const CCommand &args)
   751 {
   752 #else
   753 void PlayerManager::OnClientCommand(edict_t *pEntity)
   754 {
   755 	CCommand args;
   756 #endif
   757 	int client = IndexOfEdict(pEntity);
   758 	cell_t res = Pl_Continue;
   759 	CPlayer *pPlayer = &m_Players[client];
   760 
   761 	if (!pPlayer->IsConnected())
   762 	{
   763 		return;
   764 	}
   765 
   766 	if (strcmp(args.Arg(0), "sm") == 0)
   767 	{
   768 		if (args.ArgC() > 1 && strcmp(args.Arg(1), "plugins") == 0)
   769 		{
   770 			g_PluginSys.ListPluginsToClient(pPlayer, args);
   771 			RETURN_META(MRES_SUPERCEDE);
   772 		}
   773 		else if (args.ArgC() > 1 && strcmp(args.Arg(1), "exts") == 0)
   774 		{
   775 			g_Extensions.ListExtensionsToClient(pPlayer, args);
   776 			RETURN_META(MRES_SUPERCEDE);
   777 		}
   778 		else if (args.ArgC() > 1 && strcmp(args.Arg(1), "credits") == 0)
   779 		{
   780  			ClientConsolePrint(pEntity,
   781  				"SourceMod would not be possible without:");
   782  			ClientConsolePrint(pEntity,
   783 				" David \"BAILOPAN\" Anderson, Matt \"pRED\" Woodrow");
   784  			ClientConsolePrint(pEntity,
   785 				" Scott \"DS\" Ehlert, Fyren");
   786  			ClientConsolePrint(pEntity,
   787 				" Nicholas \"psychonic\" Hastings, Asher \"asherkin\" Baker");
   788 			ClientConsolePrint(pEntity,
   789 				" Borja \"faluco\" Ferrer, Pavol \"PM OnoTo\" Marko");
   790 			ClientConsolePrint(pEntity,
   791 				"SourceMod is open source under the GNU General Public License.");
   792 			RETURN_META(MRES_SUPERCEDE);
   793 		}
   794 
   795 		ClientConsolePrint(pEntity,
   796 			"SourceMod %s, by AlliedModders LLC", SM_VERSION_STRING);
   797 		ClientConsolePrint(pEntity,
   798 			"To see running plugins, type \"sm plugins\"");
   799 		ClientConsolePrint(pEntity,
   800 			"To see credits, type \"sm credits\"");
   801 		ClientConsolePrint(pEntity,
   802 			"Visit http://www.sourcemod.net/");
   803 		RETURN_META(MRES_SUPERCEDE);
   804 	}
   805 
   806 	g_HL2.PushCommandStack(&args);
   807 
   808 	int argcount = args.ArgC() - 1;
   809 	const char *cmd = g_HL2.CurrentCommandName();
   810 
   811 	bool result = g_ValveMenuStyle.OnClientCommand(client, cmd, args);
   812 	if (result)
   813 	{
   814 		res = Pl_Handled;
   815 	} else {
   816 		result = g_RadioMenuStyle.OnClientCommand(client, cmd, args);
   817 		if (result)
   818 		{
   819 			res = Pl_Handled;
   820 		}
   821 	}
   822 
   823 	if (g_ConsoleDetours.IsEnabled())
   824 	{
   825 		cell_t res2 = g_ConsoleDetours.InternalDispatch(client, args);
   826 		if (res2 >= Pl_Stop)
   827 		{
   828 			g_HL2.PopCommandStack();
   829 			RETURN_META(MRES_SUPERCEDE);
   830 		}
   831 		else if (res2 > res)
   832 		{
   833 			res = res2;
   834 		}
   835 	}
   836 
   837 	cell_t res2 = Pl_Continue;
   838 	if (pPlayer->IsInGame())
   839 	{
   840 		m_clcommand->PushCell(client);
   841 		m_clcommand->PushCell(argcount);
   842 		m_clcommand->Execute(&res2, NULL);
   843 	}
   844 
   845 	if (res2 > res)
   846 	{
   847 		res = res2;
   848 	}
   849 
   850 	if (res >= Pl_Stop)
   851 	{
   852 		g_HL2.PopCommandStack();
   853 		RETURN_META(MRES_SUPERCEDE);
   854 	}
   855 
   856 	res = g_ConCmds.DispatchClientCommand(client, cmd, argcount, (ResultType)res);
   857 
   858 	g_HL2.PopCommandStack();
   859 
   860 	if (res >= Pl_Handled)
   861 	{
   862 		RETURN_META(MRES_SUPERCEDE);
   863 	}
   864 }
   865 
   866 void PlayerManager::OnClientSettingsChanged(edict_t *pEntity)
   867 {
   868 	cell_t res;
   869 	int client = IndexOfEdict(pEntity);
   870 	CPlayer *pPlayer = &m_Players[client];
   871 
   872 	if (!pPlayer->IsConnected())
   873 	{
   874 		return;
   875 	}
   876 
   877 	m_clinfochanged->PushCell(client);
   878 	m_clinfochanged->Execute(&res, NULL);
   879 
   880 	if (pPlayer->IsFakeClient())
   881 	{
   882 		return;
   883 	}
   884 
   885 	IPlayerInfo *info = pPlayer->GetPlayerInfo();
   886 	const char *new_name = info ? info->GetName() : engine->GetClientConVarValue(client, "name");
   887 	const char *old_name = pPlayer->m_Name.c_str();
   888 
   889 #if SOURCE_ENGINE >= SE_LEFT4DEAD
   890 	const char *networkid_force;
   891 	if ((networkid_force = engine->GetClientConVarValue(client, "networkid_force")) && networkid_force[0] != '\0')
   892 	{
   893 		unsigned long long *steamId = (unsigned long long *)engine->GetClientSteamID(pEntity);
   894 		unsigned int accountId = steamId ? (*steamId & 0xFFFFFFFF) : 0;
   895 		g_Logger.LogMessage("\"%s<%d><STEAM_1:%d:%d><>\" has bad networkid (id \"%s\") (ip \"%s\")",
   896 			new_name, pPlayer->GetUserId(), accountId & 1, accountId >> 1, networkid_force, pPlayer->GetIPAddress());
   897 
   898 		pPlayer->Kick("NetworkID spoofing detected.");
   899 		RETURN_META(MRES_IGNORED);
   900 	}
   901 #endif
   902 
   903 	if (strcmp(old_name, new_name) != 0)
   904 	{
   905 		AdminId id = g_Admins.FindAdminByIdentity("name", new_name);
   906 		if (id != INVALID_ADMIN_ID && pPlayer->GetAdminId() != id)
   907 		{
   908 			if (!CheckSetAdminName(client, pPlayer, id))
   909 			{
   910 				pPlayer->Kick("Your name is reserved by SourceMod; set your password to use it.");
   911 				RETURN_META(MRES_IGNORED);
   912 			}
   913 		} else if ((id = g_Admins.FindAdminByIdentity("name", old_name)) != INVALID_ADMIN_ID) {
   914 			if (id == pPlayer->GetAdminId())
   915 			{
   916 				/* This player is changing their name; force them to drop admin privileges! */
   917 				pPlayer->SetAdminId(INVALID_ADMIN_ID, false);
   918 			}
   919 		}
   920 		pPlayer->SetName(new_name);
   921 	}
   922 	
   923 	if (m_PassInfoVar.size() > 0)
   924 	{
   925 		/* Try for a password change */
   926 		const char *old_pass = pPlayer->m_LastPassword.c_str();
   927 		const char *new_pass = engine->GetClientConVarValue(client, m_PassInfoVar.c_str());
   928 		if (strcmp(old_pass, new_pass) != 0)
   929 		{
   930 			pPlayer->m_LastPassword.assign(new_pass);
   931 			if (pPlayer->IsInGame() && pPlayer->IsAuthorized())
   932 			{
   933 				/* If there is already an admin id assigned, this will just bail out. */
   934 				pPlayer->DoBasicAdminChecks();
   935 			}
   936 		}
   937 	}
   938 	/* Notify Extensions */
   939 	List<IClientListener *>::iterator iter;
   940 	IClientListener *pListener = NULL;
   941 	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   942 	{
   943 		pListener = (*iter);
   944 		if (pListener->GetClientListenerVersion() >= 13)
   945 		{
   946 			pListener->OnClientSettingsChanged(client);
   947 		}
   948 	}
   949 }
   950 
   951 int PlayerManager::GetMaxClients()
   952 {
   953 	return m_maxClients;
   954 }
   955 
   956 CPlayer *PlayerManager::GetPlayerByIndex(int client) const
   957 {
   958 	if (client > m_maxClients || client < 1)
   959 	{
   960 		return NULL;
   961 	}
   962 	return &m_Players[client];
   963 }
   964 
   965 int PlayerManager::GetNumPlayers()
   966 {
   967 	return m_PlayerCount;
   968 }
   969 
   970 int PlayerManager::GetClientOfUserId(int userid)
   971 {
   972 	if (userid < 0 || userid > USHRT_MAX)
   973 	{
   974 		return 0;
   975 	}
   976 	
   977 	int client = m_UserIdLookUp[userid];
   978 
   979 	/* Verify the userid.  The cache can get messed up with older
   980 	 * Valve engines.  :TODO: If this gets fixed, do an old engine 
   981 	 * check before invoking this backwards compat code.
   982 	 */
   983 	if (client)
   984 	{
   985 		CPlayer *player = GetPlayerByIndex(client);
   986 		if (player && player->IsConnected())
   987 		{
   988 			int realUserId = engine->GetPlayerUserId(player->GetEdict());
   989 			if (realUserId == userid)
   990 			{
   991 				return client;
   992 			}
   993 		}
   994 	}
   995 
   996 	/* If we can't verify the userid, we have to do a manual loop */
   997 	CPlayer *player;
   998 	for (int i = 1; i <= m_maxClients; i++)
   999 	{
  1000 		player = GetPlayerByIndex(i);
  1001 		if (!player || !player->IsConnected())
  1002 		{
  1003 			continue;
  1004 		}
  1005 		if (engine->GetPlayerUserId(player->GetEdict()) == userid)
  1006 		{
  1007 			m_UserIdLookUp[userid] = i;
  1008 			return i;
  1009 		}
  1010 	}
  1011 
  1012 	return 0;
  1013 }
  1014 
  1015 void PlayerManager::AddClientListener(IClientListener *listener)
  1016 {
  1017 	m_hooks.push_back(listener);
  1018 }
  1019 
  1020 void PlayerManager::RemoveClientListener(IClientListener *listener)
  1021 {
  1022 	m_hooks.remove(listener);
  1023 }
  1024 
  1025 IGamePlayer *PlayerManager::GetGamePlayer(edict_t *pEdict)
  1026 {
  1027 	int index = IndexOfEdict(pEdict);
  1028 	return GetGamePlayer(index);
  1029 }
  1030 
  1031 IGamePlayer *PlayerManager::GetGamePlayer(int client)
  1032 {
  1033 	return GetPlayerByIndex(client);
  1034 }
  1035 
  1036 void PlayerManager::ClearAdminId(AdminId id)
  1037 {
  1038 	for (int i=1; i<=m_maxClients; i++)
  1039 	{
  1040 		if (m_Players[i].m_Admin == id)
  1041 		{
  1042 			m_Players[i].DumpAdmin(true);
  1043 		}
  1044 	}
  1045 }
  1046 
  1047 void PlayerManager::ClearAllAdmins()
  1048 {
  1049 	for (int i=1; i<=m_maxClients; i++)
  1050 	{
  1051 		m_Players[i].DumpAdmin(true);
  1052 	}
  1053 }
  1054 
  1055 const char *PlayerManager::GetPassInfoVar()
  1056 {
  1057 	return m_PassInfoVar.c_str();
  1058 }
  1059 
  1060 void PlayerManager::RecheckAnyAdmins()
  1061 {
  1062 	for (int i=1; i<=m_maxClients; i++)
  1063 	{
  1064 		if (m_Players[i].IsInGame() && m_Players[i].IsAuthorized())
  1065 		{
  1066 			m_Players[i].DoBasicAdminChecks();
  1067 		}
  1068 	}
  1069 }
  1070 
  1071 unsigned int PlayerManager::GetReplyTo()
  1072 {
  1073 	return g_ChatTriggers.GetReplyTo();
  1074 }
  1075 
  1076 unsigned int PlayerManager::SetReplyTo(unsigned int reply)
  1077 {
  1078 	return g_ChatTriggers.SetReplyTo(reply);
  1079 }
  1080 
  1081 int PlayerManager::FilterCommandTarget(IGamePlayer *pAdmin, IGamePlayer *pTarget, int flags)
  1082 {
  1083 	return InternalFilterCommandTarget((CPlayer *)pAdmin, (CPlayer *)pTarget, flags);
  1084 }
  1085 
  1086 void PlayerManager::RegisterCommandTargetProcessor(ICommandTargetProcessor *pHandler)
  1087 {
  1088 	target_processors.push_back(pHandler);
  1089 }
  1090 
  1091 void PlayerManager::UnregisterCommandTargetProcessor(ICommandTargetProcessor *pHandler)
  1092 {
  1093 	target_processors.remove(pHandler);
  1094 }
  1095 
  1096 void PlayerManager::InvalidatePlayer(CPlayer *pPlayer)
  1097 {
  1098 	/**
  1099 	* Remove client from auth queue if necessary
  1100 	*/
  1101 	if (!pPlayer->IsAuthorized())
  1102 	{
  1103 		for (unsigned int i=1; i<=m_AuthQueue[0]; i++)
  1104 		{
  1105 			if (m_AuthQueue[i] == (unsigned)pPlayer->m_iIndex)
  1106 			{
  1107 				/* Move everything ahead of us back by one */
  1108 				for (unsigned int j=i+1; j<=m_AuthQueue[0]; j++)
  1109 				{
  1110 					m_AuthQueue[j-1] = m_AuthQueue[j];
  1111 				}
  1112 				/* Remove us and break */
  1113 				m_AuthQueue[0]--;
  1114 				break;
  1115 			}
  1116 		}
  1117 	}
  1118 	
  1119 	m_UserIdLookUp[engine->GetPlayerUserId(pPlayer->m_pEdict)] = 0;
  1120 	pPlayer->Disconnect();
  1121 }
  1122 
  1123 int PlayerManager::InternalFilterCommandTarget(CPlayer *pAdmin, CPlayer *pTarget, int flags)
  1124 {
  1125 	if ((flags & COMMAND_FILTER_CONNECTED) == COMMAND_FILTER_CONNECTED
  1126 		&& !pTarget->IsConnected())
  1127 	{
  1128 		return COMMAND_TARGET_NONE;
  1129 	}
  1130 	else if ((flags & COMMAND_FILTER_CONNECTED) != COMMAND_FILTER_CONNECTED && 
  1131 		!pTarget->IsInGame())
  1132 	{
  1133 		return COMMAND_TARGET_NOT_IN_GAME;
  1134 	}
  1135 
  1136 	if ((flags & COMMAND_FILTER_NO_BOTS) == COMMAND_FILTER_NO_BOTS
  1137 		&& pTarget->IsFakeClient())
  1138 	{
  1139 		return COMMAND_TARGET_NOT_HUMAN;
  1140 	}
  1141 
  1142 	if (pAdmin != NULL)
  1143 	{
  1144 		if ((flags & COMMAND_FILTER_NO_IMMUNITY) != COMMAND_FILTER_NO_IMMUNITY
  1145 			&& !g_Admins.CanAdminTarget(pAdmin->GetAdminId(), pTarget->GetAdminId()))
  1146 		{
  1147 			return COMMAND_TARGET_IMMUNE;
  1148 		}
  1149 	}
  1150 
  1151 	if ((flags & COMMAND_FILTER_ALIVE) == COMMAND_FILTER_ALIVE 
  1152 		&& pTarget->GetLifeState() != PLAYER_LIFE_ALIVE)
  1153 	{
  1154 		return COMMAND_TARGET_NOT_ALIVE;
  1155 	}
  1156 
  1157 	if ((flags & COMMAND_FILTER_DEAD) == COMMAND_FILTER_DEAD
  1158 		&& pTarget->GetLifeState() != PLAYER_LIFE_DEAD)
  1159 	{
  1160 		return COMMAND_TARGET_NOT_DEAD;
  1161 	}
  1162 
  1163 	return COMMAND_TARGET_VALID;
  1164 }
  1165 
  1166 void PlayerManager::ProcessCommandTarget(cmd_target_info_t *info)
  1167 {
  1168 	CPlayer *pTarget, *pAdmin;
  1169 	int max_clients, total = 0;
  1170 
  1171 	max_clients = GetMaxClients();
  1172 
  1173 	if (info->max_targets < 1)
  1174 	{
  1175 		info->reason = COMMAND_TARGET_NONE;
  1176 		info->num_targets = 0;
  1177 	}
  1178 
  1179 	if (info->admin == 0)
  1180 	{
  1181 		pAdmin = NULL;
  1182 	}
  1183 	else
  1184 	{
  1185 		pAdmin = GetPlayerByIndex(info->admin);
  1186 	}
  1187 
  1188 	if (info->pattern[0] == '#')
  1189 	{
  1190 		int userid = atoi(&info->pattern[1]);
  1191 		int client = GetClientOfUserId(userid);
  1192 
  1193 		/* See if a valid userid matched */
  1194 		if (client > 0)
  1195 		{
  1196 			IGamePlayer *pTarget = GetPlayerByIndex(client);
  1197 			if (pTarget != NULL)
  1198 			{
  1199 				if ((info->reason = FilterCommandTarget(pAdmin, pTarget, info->flags)) == COMMAND_TARGET_VALID)
  1200 				{
  1201 					info->targets[0] = client;
  1202 					info->num_targets = 1;
  1203 					strncopy(info->target_name, pTarget->GetName(), info->target_name_maxlength);
  1204 					info->target_name_style = COMMAND_TARGETNAME_RAW;
  1205 				}
  1206 				else
  1207 				{
  1208 					info->num_targets = 0;
  1209 				}
  1210 				return;
  1211 			}
  1212 		}
  1213 
  1214 		/* Do we need to look for a steam id? */
  1215 		if (strncmp(&info->pattern[1], "STEAM_", 6) == 0)
  1216 		{
  1217 			size_t p, len;
  1218 			char new_pattern[256];
  1219 
  1220 			strcpy(new_pattern, "STEAM_");
  1221 			len = strlen(&info->pattern[7]);
  1222 			for (p = 0; p < len; p++)
  1223 			{
  1224 				new_pattern[6 + p] = info->pattern[7 + p];
  1225 				if (new_pattern[6 + p] == '_')
  1226 				{
  1227 					new_pattern[6 + p] = ':';
  1228 				}
  1229 			}
  1230 			new_pattern[6 + p] = '\0';
  1231 
  1232 			for (int i = 1; i <= max_clients; i++)
  1233 			{
  1234 				if ((pTarget = GetPlayerByIndex(i)) == NULL)
  1235 				{
  1236 					continue;
  1237 				}
  1238 				if (!pTarget->IsConnected() || !pTarget->IsAuthorized())
  1239 				{
  1240 					continue;
  1241 				}
  1242 				if (strcmp(pTarget->GetAuthString(), new_pattern) == 0)
  1243 				{
  1244 					if ((info->reason = FilterCommandTarget(pAdmin, pTarget, info->flags))
  1245 						== COMMAND_TARGET_VALID)
  1246 					{
  1247 						info->targets[0] = i;
  1248 						info->num_targets = 1;
  1249 						strncopy(info->target_name, pTarget->GetName(), info->target_name_maxlength);
  1250 						info->target_name_style = COMMAND_TARGETNAME_RAW;
  1251 					}
  1252 					else
  1253 					{
  1254 						info->num_targets = 0;
  1255 					}
  1256 					return;
  1257 				}
  1258 			}
  1259 		}
  1260 
  1261 		/* See if an exact name matches */
  1262 		for (int i = 1; i <= max_clients; i++)
  1263 		{
  1264 			if ((pTarget = GetPlayerByIndex(i)) == NULL)
  1265 			{
  1266 				continue;
  1267 			}
  1268 			if  (!pTarget->IsConnected())
  1269 			{
  1270 				continue;
  1271 			}
  1272 			if (strcmp(pTarget->GetName(), &info->pattern[1]) == 0)
  1273 			{
  1274 				if ((info->reason = FilterCommandTarget(pAdmin, pTarget, info->flags))
  1275 					== COMMAND_TARGET_VALID)
  1276 				{
  1277 					info->targets[0] = i;
  1278 					info->num_targets = 1;
  1279 					strncopy(info->target_name, pTarget->GetName(), info->target_name_maxlength);
  1280 					info->target_name_style = COMMAND_TARGETNAME_RAW;
  1281 				}
  1282 				else
  1283 				{
  1284 					info->num_targets = 0;
  1285 				}
  1286 				return;
  1287 			}
  1288 		}
  1289 	}
  1290 
  1291 	if (strcmp(info->pattern, "@me") == 0 && info->admin != 0)
  1292 	{
  1293 		info->reason = FilterCommandTarget(pAdmin, pAdmin, info->flags);
  1294 		if (info->reason == COMMAND_TARGET_VALID)
  1295 		{
  1296 			info->targets[0] = info->admin;
  1297 			info->num_targets = 1;
  1298 			strncopy(info->target_name, pAdmin->GetName(), info->target_name_maxlength);
  1299 			info->target_name_style = COMMAND_TARGETNAME_RAW;
  1300 		}
  1301 		else
  1302 		{
  1303 			info->num_targets = 0;
  1304 		}
  1305 		return;
  1306 	}
  1307 
  1308 	if ((info->flags & COMMAND_FILTER_NO_MULTI) != COMMAND_FILTER_NO_MULTI)
  1309 	{
  1310 		bool is_multi = false;
  1311 		bool bots_only = false;
  1312 		int skip_client = -1;
  1313 
  1314 		if (strcmp(info->pattern, "@all") == 0)
  1315 		{
  1316 			is_multi = true;
  1317 			strncopy(info->target_name, "all players", info->target_name_maxlength);
  1318 			info->target_name_style = COMMAND_TARGETNAME_ML;
  1319 		}
  1320 		else if (strcmp(info->pattern, "@dead") == 0)
  1321 		{
  1322 			is_multi = true;
  1323 			if ((info->flags & COMMAND_FILTER_ALIVE) == COMMAND_FILTER_ALIVE)
  1324 			{
  1325 				info->num_targets = 0;
  1326 				info->reason = COMMAND_TARGET_NOT_ALIVE;
  1327 				return;
  1328 			}
  1329 			info->flags |= COMMAND_FILTER_DEAD;
  1330 			strncopy(info->target_name, "all dead players", info->target_name_maxlength);
  1331 			info->target_name_style = COMMAND_TARGETNAME_ML;
  1332 		}
  1333 		else if (strcmp(info->pattern, "@alive") == 0)
  1334 		{
  1335 			is_multi = true;
  1336 			if ((info->flags & COMMAND_FILTER_DEAD) == COMMAND_FILTER_DEAD)
  1337 			{
  1338 				info->num_targets = 0;
  1339 				info->reason = COMMAND_TARGET_NOT_DEAD;
  1340 				return;
  1341 			}
  1342 			strncopy(info->target_name, "all alive players", info->target_name_maxlength);
  1343 			info->target_name_style = COMMAND_TARGETNAME_ML;
  1344 			info->flags |= COMMAND_FILTER_ALIVE;
  1345 		}
  1346 		else if (strcmp(info->pattern, "@bots") == 0)
  1347 		{
  1348 			is_multi = true;
  1349 			if ((info->flags & COMMAND_FILTER_NO_BOTS) == COMMAND_FILTER_NO_BOTS)
  1350 			{
  1351 				info->num_targets = 0;
  1352 				info->reason = COMMAND_FILTER_NO_BOTS;
  1353 				return;
  1354 			}
  1355 			strncopy(info->target_name, "all bots", info->target_name_maxlength);
  1356 			info->target_name_style = COMMAND_TARGETNAME_ML;
  1357 			bots_only = true;
  1358 		}
  1359 		else if (strcmp(info->pattern, "@humans") == 0)
  1360 		{
  1361 			is_multi = true;
  1362 			strncopy(info->target_name, "all humans", info->target_name_maxlength);
  1363 			info->target_name_style = COMMAND_TARGETNAME_ML;
  1364 			info->flags |= COMMAND_FILTER_NO_BOTS;
  1365 		}
  1366 		else if (strcmp(info->pattern, "@!me") == 0)
  1367 		{
  1368 			is_multi = true;
  1369 			strncopy(info->target_name, "all players", info->target_name_maxlength);
  1370 			info->target_name_style = COMMAND_TARGETNAME_ML;
  1371 			skip_client = info->admin;
  1372 		}
  1373 
  1374 		if (is_multi)
  1375 		{
  1376 			for (int i = 1; i <= max_clients && total < info->max_targets; i++)
  1377 			{
  1378 				if ((pTarget = GetPlayerByIndex(i)) == NULL)
  1379 				{
  1380 					continue;
  1381 				}
  1382 				if (FilterCommandTarget(pAdmin, pTarget, info->flags) > 0)
  1383 				{
  1384 					if ((!bots_only || pTarget->IsFakeClient())
  1385 						&& skip_client != i)
  1386 					{
  1387 						info->targets[total++] = i;
  1388 					}
  1389 				}
  1390 			}
  1391 
  1392 			info->num_targets = total;
  1393 			info->reason = (info->num_targets) ? COMMAND_TARGET_VALID : COMMAND_TARGET_EMPTY_FILTER;
  1394 			return;
  1395 		}
  1396 	}
  1397 
  1398 	List<ICommandTargetProcessor *>::iterator iter;
  1399 	for (iter = target_processors.begin(); iter != target_processors.end(); iter++)
  1400 	{
  1401 		ICommandTargetProcessor *pProcessor = (*iter);
  1402 		if (pProcessor->ProcessCommandTarget(info))
  1403 		{
  1404 			return;
  1405 		}
  1406 	}
  1407 
  1408 	/* Check partial names */
  1409 	int found_client = 0;
  1410 	CPlayer *pFoundClient = NULL;
  1411 	for (int i = 1; i <= max_clients; i++)
  1412 	{
  1413 		if ((pTarget = GetPlayerByIndex(i)) == NULL)
  1414 		{
  1415 			continue;
  1416 		}
  1417 
  1418 		if (logicore.stristr(pTarget->GetName(), info->pattern) != NULL)
  1419 		{
  1420 			if (found_client)
  1421 			{
  1422 				info->num_targets = 0;
  1423 				info->reason = COMMAND_TARGET_AMBIGUOUS;
  1424 				return;
  1425 			}
  1426 			else
  1427 			{
  1428 				found_client = i;
  1429 				pFoundClient = pTarget;
  1430 			}
  1431 		}
  1432 	}
  1433 
  1434 	if (found_client)
  1435 	{
  1436 		if ((info->reason = FilterCommandTarget(pAdmin, pFoundClient, info->flags)) 
  1437 			== COMMAND_TARGET_VALID)
  1438 		{
  1439 			info->targets[0] = found_client;
  1440 			info->num_targets = 1;
  1441 			strncopy(info->target_name, pFoundClient->GetName(), info->target_name_maxlength);
  1442 			info->target_name_style = COMMAND_TARGETNAME_RAW;
  1443 		}
  1444 		else
  1445 		{
  1446 			info->num_targets = 0;
  1447 		}
  1448 	}
  1449 	else
  1450 	{
  1451 		info->num_targets = 0;
  1452 		info->reason = COMMAND_TARGET_NONE;
  1453 	}
  1454 }
  1455 
  1456 void PlayerManager::OnSourceModMaxPlayersChanged( int newvalue )
  1457 {
  1458 	m_maxClients = newvalue;
  1459 }
  1460 
  1461 void PlayerManager::MaxPlayersChanged( int newvalue /*= -1*/ )
  1462 {
  1463 	if (!m_FirstPass)
  1464 	{
  1465 		return;
  1466 	}
  1467 
  1468 	if (newvalue == -1)
  1469 	{
  1470 		newvalue = gpGlobals->maxClients;
  1471 	}
  1472 
  1473 	if (newvalue == MaxClients())
  1474 	{
  1475 		return;
  1476 	}
  1477 
  1478 	/* Notify the rest of core */
  1479 	SMGlobalClass *pBase = SMGlobalClass::head;
  1480 	while (pBase)
  1481 	{
  1482 		pBase->OnSourceModMaxPlayersChanged(newvalue);
  1483 		pBase = pBase->m_pGlobalClassNext;
  1484 	}
  1485 
  1486 	/* Notify Extensions */
  1487 	List<IClientListener *>::iterator iter;
  1488 	IClientListener *pListener = NULL;
  1489 	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
  1490 	{
  1491 		pListener = (*iter);
  1492 		if (pListener->GetClientListenerVersion() >= 8)
  1493 		{
  1494 			pListener->OnMaxPlayersChanged(newvalue);
  1495 		}
  1496 	}
  1497 }
  1498 
  1499 int PlayerManager::GetClientFromSerial(unsigned int serial)
  1500 {
  1501 	serial_t s;
  1502 	s.value = serial;
  1503 
  1504 	int client = s.bits.index;
  1505 
  1506 	IGamePlayer *pPlayer = GetGamePlayer(client);
  1507 
  1508 	if (!pPlayer)
  1509 	{
  1510 		return 0;
  1511 	}
  1512 
  1513 	if (serial == pPlayer->GetSerial())
  1514 	{
  1515 		return client;
  1516 	}
  1517 
  1518 	return 0;
  1519 }
  1520 
  1521 #if SOURCE_ENGINE >= SE_ORANGEBOX
  1522 void CmdMaxplayersCallback(const CCommand &command)
  1523 {
  1524 #else
  1525 void CmdMaxplayersCallback()
  1526 {
  1527 #endif
  1528 
  1529 	g_Players.MaxPlayersChanged();
  1530 }
  1531 
  1532 
  1533 /*******************
  1534  *** PLAYER CODE ***
  1535  *******************/
  1536 
  1537 CPlayer::CPlayer()
  1538 {
  1539 	m_IsConnected = false;
  1540 	m_IsInGame = false;
  1541 	m_IsAuthorized = false;
  1542 	m_pEdict = NULL;
  1543 	m_Admin = INVALID_ADMIN_ID;
  1544 	m_TempAdmin = false;
  1545 	m_Info = NULL;
  1546 	m_bAdminCheckSignalled = false;
  1547 	m_UserId = -1;
  1548 	m_bIsInKickQueue = false;
  1549 	m_LastPassword.clear();
  1550 	m_LangId = SOURCEMOD_LANGUAGE_ENGLISH;
  1551 	m_bFakeClient = false;
  1552 	m_bIsSourceTV = false;
  1553 	m_bIsReplay = false;
  1554 	m_Serial.value = -1;
  1555 }
  1556 
  1557 void CPlayer::Initialize(const char *name, const char *ip, edict_t *pEntity)
  1558 {
  1559 	m_IsConnected = true;
  1560 	m_Name.assign(name);
  1561 	m_Ip.assign(ip);
  1562 	m_pEdict = pEntity;
  1563 	m_iIndex = IndexOfEdict(pEntity);
  1564 	m_LangId = translator->GetServerLanguage();
  1565 
  1566 	m_Serial.bits.index = m_iIndex;
  1567 	m_Serial.bits.serial = g_PlayerSerialCount++;
  1568 
  1569 	char ip2[24], *ptr;
  1570 	strncopy(ip2, ip, sizeof(ip2));
  1571 	if ((ptr = strchr(ip2, ':')) != NULL)
  1572 	{
  1573 		*ptr = '\0';
  1574 	}
  1575 	m_IpNoPort.assign(ip2);
  1576 }
  1577 
  1578 void CPlayer::Connect()
  1579 {
  1580 	if (m_IsInGame)
  1581 	{
  1582 		return;
  1583 	}
  1584 
  1585 	m_IsInGame = true;
  1586 
  1587 	const char *var = g_Players.GetPassInfoVar();
  1588 	int client = IndexOfEdict(m_pEdict);
  1589 	if (var[0] != '\0')
  1590 	{
  1591 		const char *pass = engine->GetClientConVarValue(client, var);
  1592 		m_LastPassword.assign(pass ? pass : "");
  1593 	}
  1594 	else
  1595 	{
  1596 		m_LastPassword.assign("");
  1597 	}
  1598 }
  1599 
  1600 void CPlayer::Authorize(const char *steamid)
  1601 {
  1602 	if (m_IsAuthorized)
  1603 	{
  1604 		return;
  1605 	}
  1606 
  1607 	m_IsAuthorized = true;
  1608 	m_AuthID.assign(steamid);
  1609 }
  1610 
  1611 void CPlayer::Disconnect()
  1612 {
  1613 	DumpAdmin(false);
  1614 	m_IsConnected = false;
  1615 	m_IsInGame = false;
  1616 	m_IsAuthorized = false;
  1617 	m_Name.clear();
  1618 	m_Ip.clear();
  1619 	m_AuthID.clear();
  1620 	m_pEdict = NULL;
  1621 	m_Info = NULL;
  1622 	m_bAdminCheckSignalled = false;
  1623 	m_UserId = -1;
  1624 	m_bIsInKickQueue = false;
  1625 	m_bFakeClient = false;
  1626 	m_bIsSourceTV = false;
  1627 	m_bIsReplay = false;
  1628 	m_Serial.value = -1;
  1629 }
  1630 
  1631 void CPlayer::SetName(const char *name)
  1632 {
  1633 	m_Name.assign(name);
  1634 }
  1635 
  1636 const char *CPlayer::GetName()
  1637 {
  1638 	if (m_Info && m_pEdict->GetUnknown())
  1639 	{
  1640 		return m_Info->GetName();
  1641 	}
  1642 	
  1643 	return m_Name.c_str();
  1644 }
  1645 
  1646 const char *CPlayer::GetIPAddress()
  1647 {
  1648 	return m_Ip.c_str();
  1649 }
  1650 
  1651 const char *CPlayer::GetAuthString()
  1652 {
  1653 	return m_AuthID.c_str();
  1654 }
  1655 
  1656 edict_t *CPlayer::GetEdict()
  1657 {
  1658 	return m_pEdict;
  1659 }
  1660 
  1661 bool CPlayer::IsInGame()
  1662 {
  1663 	return m_IsInGame && (m_pEdict->GetUnknown() != NULL);
  1664 }
  1665 
  1666 bool CPlayer::WasCountedAsInGame()
  1667 {
  1668 	return m_IsInGame;
  1669 }
  1670 
  1671 bool CPlayer::IsConnected()
  1672 {
  1673 	return m_IsConnected;
  1674 }
  1675 
  1676 bool CPlayer::IsAuthorized()
  1677 {
  1678 	return m_IsAuthorized;
  1679 }
  1680 
  1681 #if SOURCE_ENGINE >= SE_ORANGEBOX
  1682 bool CPlayer::IsAuthedBySteam()
  1683 {
  1684 	return engine->IsClientFullyAuthenticated( m_pEdict );
  1685 }
  1686 #endif
  1687 
  1688 IPlayerInfo *CPlayer::GetPlayerInfo()
  1689 {
  1690 	if (m_pEdict->GetUnknown())
  1691 	{
  1692 		return m_Info;
  1693 	}
  1694 
  1695 	return NULL;
  1696 }
  1697 
  1698 bool CPlayer::IsFakeClient()
  1699 {
  1700 	return m_bFakeClient;
  1701 }
  1702 
  1703 bool CPlayer::IsSourceTV() const
  1704 {
  1705 	return m_bIsSourceTV;
  1706 }
  1707 
  1708 bool CPlayer::IsReplay() const
  1709 {
  1710 	return m_bIsReplay;
  1711 }
  1712 
  1713 void CPlayer::SetAdminId(AdminId id, bool temporary)
  1714 {
  1715 	if (!m_IsConnected)
  1716 	{
  1717 		return;
  1718 	}
  1719 
  1720 	DumpAdmin(false);
  1721 
  1722 	m_Admin = id;
  1723 	m_TempAdmin = temporary;
  1724 }
  1725 
  1726 AdminId CPlayer::GetAdminId()
  1727 {
  1728 	return m_Admin;
  1729 }
  1730 
  1731 void CPlayer::DumpAdmin(bool deleting)
  1732 {
  1733 	if (m_Admin != INVALID_ADMIN_ID)
  1734 	{
  1735 		if (m_TempAdmin && !deleting)
  1736 		{
  1737 			g_Admins.InvalidateAdmin(m_Admin);
  1738 		}
  1739 		m_Admin = INVALID_ADMIN_ID;
  1740 		m_TempAdmin = false;
  1741 	}
  1742 }
  1743 
  1744 void CPlayer::Kick(const char *str)
  1745 {
  1746 	MarkAsBeingKicked();
  1747 	INetChannel *pNetChan = static_cast<INetChannel *>(engine->GetPlayerNetInfo(m_iIndex));
  1748 	if (pNetChan == NULL)
  1749 	{
  1750 		/* What does this even mean? Hell if I know. */
  1751 		int userid = GetUserId();
  1752 		if (userid > 0)
  1753 		{
  1754 			char buffer[255];
  1755 			UTIL_Format(buffer, sizeof(buffer), "kickid %d %s\n", userid, str);
  1756 			engine->ServerCommand(buffer);
  1757 		}
  1758 	}
  1759 	else
  1760 	{
  1761 		IClient *pClient = static_cast<IClient *>(pNetChan->GetMsgHandler());
  1762 		pClient->Disconnect("%s", str);
  1763 	}
  1764 }
  1765 
  1766 void CPlayer::Authorize_Post()
  1767 {
  1768 	if (m_IsInGame)
  1769 	{
  1770 		DoPostConnectAuthorization();
  1771 	}
  1772 }
  1773 
  1774 void CPlayer::DoPostConnectAuthorization()
  1775 {
  1776 	bool delay = false;
  1777 
  1778 	List<IClientListener *>::iterator iter;
  1779 	for (iter = g_Players.m_hooks.begin();
  1780 		 iter != g_Players.m_hooks.end();
  1781 		 iter++)
  1782 	{
  1783 		IClientListener *pListener = (*iter);
  1784 #if defined MIN_API_FOR_ADMINCALLS
  1785 		if (pListener->GetClientListenerVersion() < MIN_API_FOR_ADMINCALLS)
  1786 		{
  1787 			continue;
  1788 		}
  1789 #endif
  1790 		if (!pListener->OnClientPreAdminCheck(m_iIndex))
  1791 		{
  1792 			delay = true;
  1793 		}
  1794 	}
  1795 	
  1796 	cell_t result = 0;
  1797 	PreAdminCheck->PushCell(m_iIndex);
  1798 	PreAdminCheck->Execute(&result);
  1799 
  1800 	/* Defer, for better or worse */
  1801 	if (delay || (ResultType)result >= Pl_Handled)
  1802 	{
  1803 		return;
  1804 	}
  1805 
  1806 	/* Sanity check */
  1807 	if (!IsConnected())
  1808 	{
  1809 		return;
  1810 	}
  1811 
  1812 	/* Otherwise, go ahead and do admin checks */
  1813 	DoBasicAdminChecks();
  1814 
  1815 	/* Send the notification out */
  1816 	NotifyPostAdminChecks();
  1817 }
  1818 
  1819 bool CPlayer::RunAdminCacheChecks()
  1820 {
  1821 	AdminId old_id = GetAdminId();
  1822 
  1823 	DoBasicAdminChecks();
  1824 
  1825 	return (GetAdminId() != old_id);
  1826 }
  1827 
  1828 void CPlayer::NotifyPostAdminChecks()
  1829 {
  1830 	if (m_bAdminCheckSignalled)
  1831 	{
  1832 		return;
  1833 	}
  1834 
  1835 	/* Block beforehand so they can't double-call */
  1836 	m_bAdminCheckSignalled = true;
  1837 
  1838 	List<IClientListener *>::iterator iter;
  1839 	for (iter = g_Players.m_hooks.begin();
  1840 		iter != g_Players.m_hooks.end();
  1841 		iter++)
  1842 	{
  1843 		IClientListener *pListener = (*iter);
  1844 #if defined MIN_API_FOR_ADMINCALLS
  1845 		if (pListener->GetClientListenerVersion() < MIN_API_FOR_ADMINCALLS)
  1846 		{
  1847 			continue;
  1848 		}
  1849 #endif
  1850 		pListener->OnClientPostAdminCheck(m_iIndex);
  1851 	}
  1852 
  1853 	PostAdminFilter->PushCell(m_iIndex);
  1854 	PostAdminFilter->Execute(NULL);
  1855 
  1856 	PostAdminCheck->PushCell(m_iIndex);
  1857 	PostAdminCheck->Execute(NULL);
  1858 }
  1859 
  1860 void CPlayer::DoBasicAdminChecks()
  1861 {
  1862 	if (GetAdminId() != INVALID_ADMIN_ID)
  1863 	{
  1864 		return;
  1865 	}
  1866 
  1867 	/* First check the name */
  1868 	AdminId id;
  1869 	int client = IndexOfEdict(m_pEdict);
  1870 
  1871 	if ((id = g_Admins.FindAdminByIdentity("name", GetName())) != INVALID_ADMIN_ID)
  1872 	{
  1873 		if (!g_Players.CheckSetAdminName(client, this, id))
  1874 		{
  1875 			int userid = engine->GetPlayerUserId(m_pEdict);
  1876 			g_Timers.CreateTimer(&s_KickPlayerTimer, 0.1f, (void *)userid, 0);
  1877 		}
  1878 		return;
  1879 	}
  1880 	
  1881 	/* Check IP */
  1882 	if ((id = g_Admins.FindAdminByIdentity("ip", m_IpNoPort.c_str())) != INVALID_ADMIN_ID)
  1883 	{
  1884 		if (g_Players.CheckSetAdmin(client, this, id))
  1885 		{
  1886 			return;
  1887 		}
  1888 	}
  1889 
  1890 	/* Check steam id */
  1891 	if ((id = g_Admins.FindAdminByIdentity("steam", m_AuthID.c_str())) != INVALID_ADMIN_ID)
  1892 	{
  1893 		if (g_Players.CheckSetAdmin(client, this, id))
  1894 		{
  1895 			return;
  1896 		}
  1897 	}
  1898 }
  1899 
  1900 unsigned int CPlayer::GetLanguageId()
  1901 {
  1902 	return m_LangId;
  1903 }
  1904 
  1905 void CPlayer::SetLanguageId(unsigned int id)
  1906 {
  1907 	m_LangId = id;
  1908 }
  1909 
  1910 int CPlayer::GetUserId()
  1911 {
  1912 	if (m_UserId == -1)
  1913 	{
  1914 		m_UserId = engine->GetPlayerUserId(GetEdict());
  1915 	}
  1916 
  1917 	return m_UserId;
  1918 }
  1919 
  1920 bool CPlayer::IsInKickQueue()
  1921 {
  1922 	return m_bIsInKickQueue;
  1923 }
  1924 
  1925 void CPlayer::MarkAsBeingKicked()
  1926 {
  1927 	m_bIsInKickQueue = true;
  1928 }
  1929 
  1930 int CPlayer::GetLifeState()
  1931 {
  1932 	if (lifestate_offset == -1)
  1933 	{
  1934 		if (!g_pGameConf->GetOffset("m_lifeState", &lifestate_offset))
  1935 		{
  1936 			lifestate_offset = -2;
  1937 		}
  1938 	}
  1939 
  1940 	if (lifestate_offset < 0)
  1941 	{
  1942 		IPlayerInfo *info = GetPlayerInfo();
  1943 		if (info == NULL)
  1944 		{
  1945 			return PLAYER_LIFE_UNKNOWN;
  1946 		}
  1947 		return info->IsDead() ? PLAYER_LIFE_DEAD : PLAYER_LIFE_ALIVE;
  1948 	}
  1949 
  1950 	if (m_pEdict == NULL)
  1951 	{
  1952 		return PLAYER_LIFE_UNKNOWN;
  1953 	}
  1954 
  1955 	CBaseEntity *pEntity;
  1956 	IServerUnknown *pUnknown = m_pEdict->GetUnknown();
  1957 	if (pUnknown == NULL || (pEntity = pUnknown->GetBaseEntity()) == NULL)
  1958 	{
  1959 		return PLAYER_LIFE_UNKNOWN;
  1960 	}
  1961 
  1962 	if (*((uint8_t *)pEntity + lifestate_offset) == LIFE_ALIVE)
  1963 	{
  1964 		return PLAYER_LIFE_ALIVE;
  1965 	}
  1966 	else
  1967 	{
  1968 		return PLAYER_LIFE_DEAD;
  1969 	}
  1970 }
  1971 
  1972 unsigned int CPlayer::GetSerial()
  1973 {
  1974 	return m_Serial.value;
  1975 }