core/PlayerManager.cpp
author Ryan Stecker <voidedweasel@gmail.com>
Sun May 27 01:51:08 2012 +0100 (2012-05-27)
changeset 3511 c9fd86b6bec3
parent 3502 2c1c30b50acc
child 3553 e966a2d03369
permissions -rw-r--r--
Exposed extensions list to clients (bug 5221, r=asherkin).
     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 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, Borja \"faluco\" Ferrer");
   784 			ClientConsolePrint(pEntity,
   785 				" Scott \"DS\" Ehlert, Matt \"pRED\" Woodrow");
   786 			ClientConsolePrint(pEntity,
   787 				" Michael \"ferret\" McKoy, Pavol \"PM OnoTo\" Marko");
   788 			ClientConsolePrint(pEntity,
   789 				"SourceMod is open source under the GNU General Public License.");
   790 			RETURN_META(MRES_SUPERCEDE);
   791 		}
   792 
   793 		ClientConsolePrint(pEntity,
   794 			"SourceMod %s, by AlliedModders LLC", SM_FULL_VERSION);
   795 		ClientConsolePrint(pEntity,
   796 			"To see running plugins, type \"sm plugins\"");
   797 		ClientConsolePrint(pEntity,
   798 			"To see credits, type \"sm credits\"");
   799 		ClientConsolePrint(pEntity,
   800 			"Visit http://www.sourcemod.net/");
   801 		RETURN_META(MRES_SUPERCEDE);
   802 	}
   803 
   804 	g_HL2.PushCommandStack(&args);
   805 
   806 	int argcount = args.ArgC() - 1;
   807 	const char *cmd = g_HL2.CurrentCommandName();
   808 
   809 	bool result = g_ValveMenuStyle.OnClientCommand(client, cmd, args);
   810 	if (result)
   811 	{
   812 		res = Pl_Handled;
   813 	} else {
   814 		result = g_RadioMenuStyle.OnClientCommand(client, cmd, args);
   815 		if (result)
   816 		{
   817 			res = Pl_Handled;
   818 		}
   819 	}
   820 
   821 	if (g_ConsoleDetours.IsEnabled())
   822 	{
   823 		cell_t res2 = g_ConsoleDetours.InternalDispatch(client, args);
   824 		if (res2 >= Pl_Stop)
   825 		{
   826 			g_HL2.PopCommandStack();
   827 			RETURN_META(MRES_SUPERCEDE);
   828 		}
   829 		else if (res2 > res)
   830 		{
   831 			res = res2;
   832 		}
   833 	}
   834 
   835 	cell_t res2 = Pl_Continue;
   836 	if (pPlayer->IsInGame())
   837 	{
   838 		m_clcommand->PushCell(client);
   839 		m_clcommand->PushCell(argcount);
   840 		m_clcommand->Execute(&res2, NULL);
   841 	}
   842 
   843 	if (res2 > res)
   844 	{
   845 		res = res2;
   846 	}
   847 
   848 	if (res >= Pl_Stop)
   849 	{
   850 		g_HL2.PopCommandStack();
   851 		RETURN_META(MRES_SUPERCEDE);
   852 	}
   853 
   854 	res = g_ConCmds.DispatchClientCommand(client, cmd, argcount, (ResultType)res);
   855 
   856 	g_HL2.PopCommandStack();
   857 
   858 	if (res >= Pl_Handled)
   859 	{
   860 		RETURN_META(MRES_SUPERCEDE);
   861 	}
   862 }
   863 
   864 void PlayerManager::OnClientSettingsChanged(edict_t *pEntity)
   865 {
   866 	cell_t res;
   867 	int client = IndexOfEdict(pEntity);
   868 	CPlayer *pPlayer = &m_Players[client];
   869 
   870 	if (!pPlayer->IsConnected())
   871 	{
   872 		return;
   873 	}
   874 
   875 	m_clinfochanged->PushCell(client);
   876 	m_clinfochanged->Execute(&res, NULL);
   877 
   878 	if (pPlayer->IsFakeClient())
   879 	{
   880 		return;
   881 	}
   882 
   883 	IPlayerInfo *info = pPlayer->GetPlayerInfo();
   884 	const char *new_name = info ? info->GetName() : engine->GetClientConVarValue(client, "name");
   885 	const char *old_name = pPlayer->m_Name.c_str();
   886 
   887 	if (strcmp(old_name, new_name) != 0)
   888 	{
   889 		AdminId id = g_Admins.FindAdminByIdentity("name", new_name);
   890 		if (id != INVALID_ADMIN_ID && pPlayer->GetAdminId() != id)
   891 		{
   892 			if (!CheckSetAdminName(client, pPlayer, id))
   893 			{
   894 				pPlayer->Kick("Your name is reserved by SourceMod; set your password to use it.");
   895 				RETURN_META(MRES_IGNORED);
   896 			}
   897 		} else if ((id = g_Admins.FindAdminByIdentity("name", old_name)) != INVALID_ADMIN_ID) {
   898 			if (id == pPlayer->GetAdminId())
   899 			{
   900 				/* This player is changing their name; force them to drop admin privileges! */
   901 				pPlayer->SetAdminId(INVALID_ADMIN_ID, false);
   902 			}
   903 		}
   904 		pPlayer->SetName(new_name);
   905 	}
   906 	
   907 	if (m_PassInfoVar.size() > 0)
   908 	{
   909 		/* Try for a password change */
   910 		const char *old_pass = pPlayer->m_LastPassword.c_str();
   911 		const char *new_pass = engine->GetClientConVarValue(client, m_PassInfoVar.c_str());
   912 		if (strcmp(old_pass, new_pass) != 0)
   913 		{
   914 			pPlayer->m_LastPassword.assign(new_pass);
   915 			if (pPlayer->IsInGame() && pPlayer->IsAuthorized())
   916 			{
   917 				/* If there is already an admin id assigned, this will just bail out. */
   918 				pPlayer->DoBasicAdminChecks();
   919 			}
   920 		}
   921 	}
   922 	/* Notify Extensions */
   923 	List<IClientListener *>::iterator iter;
   924 	IClientListener *pListener = NULL;
   925 	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
   926 	{
   927 		pListener = (*iter);
   928 		if (pListener->GetClientListenerVersion() >= 13)
   929 		{
   930 			pListener->OnClientSettingsChanged(client);
   931 		}
   932 	}
   933 }
   934 
   935 int PlayerManager::GetMaxClients()
   936 {
   937 	return m_maxClients;
   938 }
   939 
   940 CPlayer *PlayerManager::GetPlayerByIndex(int client) const
   941 {
   942 	if (client > m_maxClients || client < 1)
   943 	{
   944 		return NULL;
   945 	}
   946 	return &m_Players[client];
   947 }
   948 
   949 int PlayerManager::GetNumPlayers()
   950 {
   951 	return m_PlayerCount;
   952 }
   953 
   954 int PlayerManager::GetClientOfUserId(int userid)
   955 {
   956 	if (userid < 0 || userid > USHRT_MAX)
   957 	{
   958 		return 0;
   959 	}
   960 	
   961 	int client = m_UserIdLookUp[userid];
   962 
   963 	/* Verify the userid.  The cache can get messed up with older
   964 	 * Valve engines.  :TODO: If this gets fixed, do an old engine 
   965 	 * check before invoking this backwards compat code.
   966 	 */
   967 	if (client)
   968 	{
   969 		CPlayer *player = GetPlayerByIndex(client);
   970 		if (player && player->IsConnected())
   971 		{
   972 			int realUserId = engine->GetPlayerUserId(player->GetEdict());
   973 			if (realUserId == userid)
   974 			{
   975 				return client;
   976 			}
   977 		}
   978 	}
   979 
   980 	/* If we can't verify the userid, we have to do a manual loop */
   981 	CPlayer *player;
   982 	for (int i = 1; i <= m_maxClients; i++)
   983 	{
   984 		player = GetPlayerByIndex(i);
   985 		if (!player || !player->IsConnected())
   986 		{
   987 			continue;
   988 		}
   989 		if (engine->GetPlayerUserId(player->GetEdict()) == userid)
   990 		{
   991 			m_UserIdLookUp[userid] = i;
   992 			return i;
   993 		}
   994 	}
   995 
   996 	return 0;
   997 }
   998 
   999 void PlayerManager::AddClientListener(IClientListener *listener)
  1000 {
  1001 	m_hooks.push_back(listener);
  1002 }
  1003 
  1004 void PlayerManager::RemoveClientListener(IClientListener *listener)
  1005 {
  1006 	m_hooks.remove(listener);
  1007 }
  1008 
  1009 IGamePlayer *PlayerManager::GetGamePlayer(edict_t *pEdict)
  1010 {
  1011 	int index = IndexOfEdict(pEdict);
  1012 	return GetGamePlayer(index);
  1013 }
  1014 
  1015 IGamePlayer *PlayerManager::GetGamePlayer(int client)
  1016 {
  1017 	return GetPlayerByIndex(client);
  1018 }
  1019 
  1020 void PlayerManager::ClearAdminId(AdminId id)
  1021 {
  1022 	for (int i=1; i<=m_maxClients; i++)
  1023 	{
  1024 		if (m_Players[i].m_Admin == id)
  1025 		{
  1026 			m_Players[i].DumpAdmin(true);
  1027 		}
  1028 	}
  1029 }
  1030 
  1031 void PlayerManager::ClearAllAdmins()
  1032 {
  1033 	for (int i=1; i<=m_maxClients; i++)
  1034 	{
  1035 		m_Players[i].DumpAdmin(true);
  1036 	}
  1037 }
  1038 
  1039 const char *PlayerManager::GetPassInfoVar()
  1040 {
  1041 	return m_PassInfoVar.c_str();
  1042 }
  1043 
  1044 void PlayerManager::RecheckAnyAdmins()
  1045 {
  1046 	for (int i=1; i<=m_maxClients; i++)
  1047 	{
  1048 		if (m_Players[i].IsInGame() && m_Players[i].IsAuthorized())
  1049 		{
  1050 			m_Players[i].DoBasicAdminChecks();
  1051 		}
  1052 	}
  1053 }
  1054 
  1055 unsigned int PlayerManager::GetReplyTo()
  1056 {
  1057 	return g_ChatTriggers.GetReplyTo();
  1058 }
  1059 
  1060 unsigned int PlayerManager::SetReplyTo(unsigned int reply)
  1061 {
  1062 	return g_ChatTriggers.SetReplyTo(reply);
  1063 }
  1064 
  1065 int PlayerManager::FilterCommandTarget(IGamePlayer *pAdmin, IGamePlayer *pTarget, int flags)
  1066 {
  1067 	return InternalFilterCommandTarget((CPlayer *)pAdmin, (CPlayer *)pTarget, flags);
  1068 }
  1069 
  1070 void PlayerManager::RegisterCommandTargetProcessor(ICommandTargetProcessor *pHandler)
  1071 {
  1072 	target_processors.push_back(pHandler);
  1073 }
  1074 
  1075 void PlayerManager::UnregisterCommandTargetProcessor(ICommandTargetProcessor *pHandler)
  1076 {
  1077 	target_processors.remove(pHandler);
  1078 }
  1079 
  1080 void PlayerManager::InvalidatePlayer(CPlayer *pPlayer)
  1081 {
  1082 	/**
  1083 	* Remove client from auth queue if necessary
  1084 	*/
  1085 	if (!pPlayer->IsAuthorized())
  1086 	{
  1087 		for (unsigned int i=1; i<=m_AuthQueue[0]; i++)
  1088 		{
  1089 			if (m_AuthQueue[i] == (unsigned)pPlayer->m_iIndex)
  1090 			{
  1091 				/* Move everything ahead of us back by one */
  1092 				for (unsigned int j=i+1; j<=m_AuthQueue[0]; j++)
  1093 				{
  1094 					m_AuthQueue[j-1] = m_AuthQueue[j];
  1095 				}
  1096 				/* Remove us and break */
  1097 				m_AuthQueue[0]--;
  1098 				break;
  1099 			}
  1100 		}
  1101 	}
  1102 	
  1103 	m_UserIdLookUp[engine->GetPlayerUserId(pPlayer->m_pEdict)] = 0;
  1104 	pPlayer->Disconnect();
  1105 }
  1106 
  1107 int PlayerManager::InternalFilterCommandTarget(CPlayer *pAdmin, CPlayer *pTarget, int flags)
  1108 {
  1109 	if ((flags & COMMAND_FILTER_CONNECTED) == COMMAND_FILTER_CONNECTED
  1110 		&& !pTarget->IsConnected())
  1111 	{
  1112 		return COMMAND_TARGET_NONE;
  1113 	}
  1114 	else if ((flags & COMMAND_FILTER_CONNECTED) != COMMAND_FILTER_CONNECTED && 
  1115 		!pTarget->IsInGame())
  1116 	{
  1117 		return COMMAND_TARGET_NOT_IN_GAME;
  1118 	}
  1119 
  1120 	if ((flags & COMMAND_FILTER_NO_BOTS) == COMMAND_FILTER_NO_BOTS
  1121 		&& pTarget->IsFakeClient())
  1122 	{
  1123 		return COMMAND_TARGET_NOT_HUMAN;
  1124 	}
  1125 
  1126 	if (pAdmin != NULL)
  1127 	{
  1128 		if ((flags & COMMAND_FILTER_NO_IMMUNITY) != COMMAND_FILTER_NO_IMMUNITY
  1129 			&& !g_Admins.CanAdminTarget(pAdmin->GetAdminId(), pTarget->GetAdminId()))
  1130 		{
  1131 			return COMMAND_TARGET_IMMUNE;
  1132 		}
  1133 	}
  1134 
  1135 	if ((flags & COMMAND_FILTER_ALIVE) == COMMAND_FILTER_ALIVE 
  1136 		&& pTarget->GetLifeState() != PLAYER_LIFE_ALIVE)
  1137 	{
  1138 		return COMMAND_TARGET_NOT_ALIVE;
  1139 	}
  1140 
  1141 	if ((flags & COMMAND_FILTER_DEAD) == COMMAND_FILTER_DEAD
  1142 		&& pTarget->GetLifeState() != PLAYER_LIFE_DEAD)
  1143 	{
  1144 		return COMMAND_TARGET_NOT_DEAD;
  1145 	}
  1146 
  1147 	return COMMAND_TARGET_VALID;
  1148 }
  1149 
  1150 void PlayerManager::ProcessCommandTarget(cmd_target_info_t *info)
  1151 {
  1152 	CPlayer *pTarget, *pAdmin;
  1153 	int max_clients, total = 0;
  1154 
  1155 	max_clients = GetMaxClients();
  1156 
  1157 	if (info->max_targets < 1)
  1158 	{
  1159 		info->reason = COMMAND_TARGET_NONE;
  1160 		info->num_targets = 0;
  1161 	}
  1162 
  1163 	if (info->admin == 0)
  1164 	{
  1165 		pAdmin = NULL;
  1166 	}
  1167 	else
  1168 	{
  1169 		pAdmin = GetPlayerByIndex(info->admin);
  1170 	}
  1171 
  1172 	if (info->pattern[0] == '#')
  1173 	{
  1174 		int userid = atoi(&info->pattern[1]);
  1175 		int client = GetClientOfUserId(userid);
  1176 
  1177 		/* See if a valid userid matched */
  1178 		if (client > 0)
  1179 		{
  1180 			IGamePlayer *pTarget = GetPlayerByIndex(client);
  1181 			if (pTarget != NULL)
  1182 			{
  1183 				if ((info->reason = FilterCommandTarget(pAdmin, pTarget, info->flags)) == COMMAND_TARGET_VALID)
  1184 				{
  1185 					info->targets[0] = client;
  1186 					info->num_targets = 1;
  1187 					strncopy(info->target_name, pTarget->GetName(), info->target_name_maxlength);
  1188 					info->target_name_style = COMMAND_TARGETNAME_RAW;
  1189 				}
  1190 				else
  1191 				{
  1192 					info->num_targets = 0;
  1193 				}
  1194 				return;
  1195 			}
  1196 		}
  1197 
  1198 		/* Do we need to look for a steam id? */
  1199 		if (strncmp(&info->pattern[1], "STEAM_", 6) == 0)
  1200 		{
  1201 			size_t p, len;
  1202 			char new_pattern[256];
  1203 
  1204 			strcpy(new_pattern, "STEAM_");
  1205 			len = strlen(&info->pattern[7]);
  1206 			for (p = 0; p < len; p++)
  1207 			{
  1208 				new_pattern[6 + p] = info->pattern[7 + p];
  1209 				if (new_pattern[6 + p] == '_')
  1210 				{
  1211 					new_pattern[6 + p] = ':';
  1212 				}
  1213 			}
  1214 			new_pattern[6 + p] = '\0';
  1215 
  1216 			for (int i = 1; i <= max_clients; i++)
  1217 			{
  1218 				if ((pTarget = GetPlayerByIndex(i)) == NULL)
  1219 				{
  1220 					continue;
  1221 				}
  1222 				if (!pTarget->IsConnected() || !pTarget->IsAuthorized())
  1223 				{
  1224 					continue;
  1225 				}
  1226 				if (strcmp(pTarget->GetAuthString(), new_pattern) == 0)
  1227 				{
  1228 					if ((info->reason = FilterCommandTarget(pAdmin, pTarget, info->flags))
  1229 						== COMMAND_TARGET_VALID)
  1230 					{
  1231 						info->targets[0] = i;
  1232 						info->num_targets = 1;
  1233 						strncopy(info->target_name, pTarget->GetName(), info->target_name_maxlength);
  1234 						info->target_name_style = COMMAND_TARGETNAME_RAW;
  1235 					}
  1236 					else
  1237 					{
  1238 						info->num_targets = 0;
  1239 					}
  1240 					return;
  1241 				}
  1242 			}
  1243 		}
  1244 
  1245 		/* See if an exact name matches */
  1246 		for (int i = 1; i <= max_clients; i++)
  1247 		{
  1248 			if ((pTarget = GetPlayerByIndex(i)) == NULL)
  1249 			{
  1250 				continue;
  1251 			}
  1252 			if  (!pTarget->IsConnected())
  1253 			{
  1254 				continue;
  1255 			}
  1256 			if (strcmp(pTarget->GetName(), &info->pattern[1]) == 0)
  1257 			{
  1258 				if ((info->reason = FilterCommandTarget(pAdmin, pTarget, info->flags))
  1259 					== COMMAND_TARGET_VALID)
  1260 				{
  1261 					info->targets[0] = i;
  1262 					info->num_targets = 1;
  1263 					strncopy(info->target_name, pTarget->GetName(), info->target_name_maxlength);
  1264 					info->target_name_style = COMMAND_TARGETNAME_RAW;
  1265 				}
  1266 				else
  1267 				{
  1268 					info->num_targets = 0;
  1269 				}
  1270 				return;
  1271 			}
  1272 		}
  1273 	}
  1274 
  1275 	if (strcmp(info->pattern, "@me") == 0 && info->admin != 0)
  1276 	{
  1277 		info->reason = FilterCommandTarget(pAdmin, pAdmin, info->flags);
  1278 		if (info->reason == COMMAND_TARGET_VALID)
  1279 		{
  1280 			info->targets[0] = info->admin;
  1281 			info->num_targets = 1;
  1282 			strncopy(info->target_name, pAdmin->GetName(), info->target_name_maxlength);
  1283 			info->target_name_style = COMMAND_TARGETNAME_RAW;
  1284 		}
  1285 		else
  1286 		{
  1287 			info->num_targets = 0;
  1288 		}
  1289 		return;
  1290 	}
  1291 
  1292 	if ((info->flags & COMMAND_FILTER_NO_MULTI) != COMMAND_FILTER_NO_MULTI)
  1293 	{
  1294 		bool is_multi = false;
  1295 		bool bots_only = false;
  1296 		int skip_client = -1;
  1297 
  1298 		if (strcmp(info->pattern, "@all") == 0)
  1299 		{
  1300 			is_multi = true;
  1301 			strncopy(info->target_name, "all players", info->target_name_maxlength);
  1302 			info->target_name_style = COMMAND_TARGETNAME_ML;
  1303 		}
  1304 		else if (strcmp(info->pattern, "@dead") == 0)
  1305 		{
  1306 			is_multi = true;
  1307 			if ((info->flags & COMMAND_FILTER_ALIVE) == COMMAND_FILTER_ALIVE)
  1308 			{
  1309 				info->num_targets = 0;
  1310 				info->reason = COMMAND_TARGET_NOT_ALIVE;
  1311 				return;
  1312 			}
  1313 			info->flags |= COMMAND_FILTER_DEAD;
  1314 			strncopy(info->target_name, "all dead players", info->target_name_maxlength);
  1315 			info->target_name_style = COMMAND_TARGETNAME_ML;
  1316 		}
  1317 		else if (strcmp(info->pattern, "@alive") == 0)
  1318 		{
  1319 			is_multi = true;
  1320 			if ((info->flags & COMMAND_FILTER_DEAD) == COMMAND_FILTER_DEAD)
  1321 			{
  1322 				info->num_targets = 0;
  1323 				info->reason = COMMAND_TARGET_NOT_DEAD;
  1324 				return;
  1325 			}
  1326 			strncopy(info->target_name, "all alive players", info->target_name_maxlength);
  1327 			info->target_name_style = COMMAND_TARGETNAME_ML;
  1328 			info->flags |= COMMAND_FILTER_ALIVE;
  1329 		}
  1330 		else if (strcmp(info->pattern, "@bots") == 0)
  1331 		{
  1332 			is_multi = true;
  1333 			if ((info->flags & COMMAND_FILTER_NO_BOTS) == COMMAND_FILTER_NO_BOTS)
  1334 			{
  1335 				info->num_targets = 0;
  1336 				info->reason = COMMAND_FILTER_NO_BOTS;
  1337 				return;
  1338 			}
  1339 			strncopy(info->target_name, "all bots", info->target_name_maxlength);
  1340 			info->target_name_style = COMMAND_TARGETNAME_ML;
  1341 			bots_only = true;
  1342 		}
  1343 		else if (strcmp(info->pattern, "@humans") == 0)
  1344 		{
  1345 			is_multi = true;
  1346 			strncopy(info->target_name, "all humans", info->target_name_maxlength);
  1347 			info->target_name_style = COMMAND_TARGETNAME_ML;
  1348 			info->flags |= COMMAND_FILTER_NO_BOTS;
  1349 		}
  1350 		else if (strcmp(info->pattern, "@!me") == 0)
  1351 		{
  1352 			is_multi = true;
  1353 			strncopy(info->target_name, "all players", info->target_name_maxlength);
  1354 			info->target_name_style = COMMAND_TARGETNAME_ML;
  1355 			skip_client = info->admin;
  1356 		}
  1357 
  1358 		if (is_multi)
  1359 		{
  1360 			for (int i = 1; i <= max_clients && total < info->max_targets; i++)
  1361 			{
  1362 				if ((pTarget = GetPlayerByIndex(i)) == NULL)
  1363 				{
  1364 					continue;
  1365 				}
  1366 				if (FilterCommandTarget(pAdmin, pTarget, info->flags) > 0)
  1367 				{
  1368 					if ((!bots_only || pTarget->IsFakeClient())
  1369 						&& skip_client != i)
  1370 					{
  1371 						info->targets[total++] = i;
  1372 					}
  1373 				}
  1374 			}
  1375 
  1376 			info->num_targets = total;
  1377 			info->reason = (info->num_targets) ? COMMAND_TARGET_VALID : COMMAND_TARGET_EMPTY_FILTER;
  1378 			return;
  1379 		}
  1380 	}
  1381 
  1382 	List<ICommandTargetProcessor *>::iterator iter;
  1383 	for (iter = target_processors.begin(); iter != target_processors.end(); iter++)
  1384 	{
  1385 		ICommandTargetProcessor *pProcessor = (*iter);
  1386 		if (pProcessor->ProcessCommandTarget(info))
  1387 		{
  1388 			return;
  1389 		}
  1390 	}
  1391 
  1392 	/* Check partial names */
  1393 	int found_client = 0;
  1394 	CPlayer *pFoundClient = NULL;
  1395 	for (int i = 1; i <= max_clients; i++)
  1396 	{
  1397 		if ((pTarget = GetPlayerByIndex(i)) == NULL)
  1398 		{
  1399 			continue;
  1400 		}
  1401 
  1402 		if (logicore.stristr(pTarget->GetName(), info->pattern) != NULL)
  1403 		{
  1404 			if (found_client)
  1405 			{
  1406 				info->num_targets = 0;
  1407 				info->reason = COMMAND_TARGET_AMBIGUOUS;
  1408 				return;
  1409 			}
  1410 			else
  1411 			{
  1412 				found_client = i;
  1413 				pFoundClient = pTarget;
  1414 			}
  1415 		}
  1416 	}
  1417 
  1418 	if (found_client)
  1419 	{
  1420 		if ((info->reason = FilterCommandTarget(pAdmin, pFoundClient, info->flags)) 
  1421 			== COMMAND_TARGET_VALID)
  1422 		{
  1423 			info->targets[0] = found_client;
  1424 			info->num_targets = 1;
  1425 			strncopy(info->target_name, pFoundClient->GetName(), info->target_name_maxlength);
  1426 			info->target_name_style = COMMAND_TARGETNAME_RAW;
  1427 		}
  1428 		else
  1429 		{
  1430 			info->num_targets = 0;
  1431 		}
  1432 	}
  1433 	else
  1434 	{
  1435 		info->num_targets = 0;
  1436 		info->reason = COMMAND_TARGET_NONE;
  1437 	}
  1438 }
  1439 
  1440 void PlayerManager::OnSourceModMaxPlayersChanged( int newvalue )
  1441 {
  1442 	m_maxClients = newvalue;
  1443 }
  1444 
  1445 void PlayerManager::MaxPlayersChanged( int newvalue /*= -1*/ )
  1446 {
  1447 	if (!m_FirstPass)
  1448 	{
  1449 		return;
  1450 	}
  1451 
  1452 	if (newvalue == -1)
  1453 	{
  1454 		newvalue = gpGlobals->maxClients;
  1455 	}
  1456 
  1457 	if (newvalue == MaxClients())
  1458 	{
  1459 		return;
  1460 	}
  1461 
  1462 	/* Notify the rest of core */
  1463 	SMGlobalClass *pBase = SMGlobalClass::head;
  1464 	while (pBase)
  1465 	{
  1466 		pBase->OnSourceModMaxPlayersChanged(newvalue);
  1467 		pBase = pBase->m_pGlobalClassNext;
  1468 	}
  1469 
  1470 	/* Notify Extensions */
  1471 	List<IClientListener *>::iterator iter;
  1472 	IClientListener *pListener = NULL;
  1473 	for (iter=m_hooks.begin(); iter!=m_hooks.end(); iter++)
  1474 	{
  1475 		pListener = (*iter);
  1476 		if (pListener->GetClientListenerVersion() >= 8)
  1477 		{
  1478 			pListener->OnMaxPlayersChanged(newvalue);
  1479 		}
  1480 	}
  1481 }
  1482 
  1483 int PlayerManager::GetClientFromSerial(unsigned int serial)
  1484 {
  1485 	serial_t s;
  1486 	s.value = serial;
  1487 
  1488 	int client = s.bits.index;
  1489 
  1490 	IGamePlayer *pPlayer = GetGamePlayer(client);
  1491 
  1492 	if (!pPlayer)
  1493 	{
  1494 		return 0;
  1495 	}
  1496 
  1497 	if (serial == pPlayer->GetSerial())
  1498 	{
  1499 		return client;
  1500 	}
  1501 
  1502 	return 0;
  1503 }
  1504 
  1505 #if SOURCE_ENGINE >= SE_ORANGEBOX
  1506 void CmdMaxplayersCallback(const CCommand &command)
  1507 {
  1508 #else
  1509 void CmdMaxplayersCallback()
  1510 {
  1511 #endif
  1512 
  1513 	g_Players.MaxPlayersChanged();
  1514 }
  1515 
  1516 
  1517 /*******************
  1518  *** PLAYER CODE ***
  1519  *******************/
  1520 
  1521 CPlayer::CPlayer()
  1522 {
  1523 	m_IsConnected = false;
  1524 	m_IsInGame = false;
  1525 	m_IsAuthorized = false;
  1526 	m_pEdict = NULL;
  1527 	m_Admin = INVALID_ADMIN_ID;
  1528 	m_TempAdmin = false;
  1529 	m_Info = NULL;
  1530 	m_bAdminCheckSignalled = false;
  1531 	m_UserId = -1;
  1532 	m_bIsInKickQueue = false;
  1533 	m_LastPassword.clear();
  1534 	m_LangId = SOURCEMOD_LANGUAGE_ENGLISH;
  1535 	m_bFakeClient = false;
  1536 	m_bIsSourceTV = false;
  1537 	m_bIsReplay = false;
  1538 	m_Serial.value = -1;
  1539 }
  1540 
  1541 void CPlayer::Initialize(const char *name, const char *ip, edict_t *pEntity)
  1542 {
  1543 	m_IsConnected = true;
  1544 	m_Name.assign(name);
  1545 	m_Ip.assign(ip);
  1546 	m_pEdict = pEntity;
  1547 	m_iIndex = IndexOfEdict(pEntity);
  1548 	m_LangId = translator->GetServerLanguage();
  1549 
  1550 	m_Serial.bits.index = m_iIndex;
  1551 	m_Serial.bits.serial = g_PlayerSerialCount++;
  1552 
  1553 	char ip2[24], *ptr;
  1554 	strncopy(ip2, ip, sizeof(ip2));
  1555 	if ((ptr = strchr(ip2, ':')) != NULL)
  1556 	{
  1557 		*ptr = '\0';
  1558 	}
  1559 	m_IpNoPort.assign(ip2);
  1560 }
  1561 
  1562 void CPlayer::Connect()
  1563 {
  1564 	if (m_IsInGame)
  1565 	{
  1566 		return;
  1567 	}
  1568 
  1569 	m_IsInGame = true;
  1570 
  1571 	const char *var = g_Players.GetPassInfoVar();
  1572 	int client = IndexOfEdict(m_pEdict);
  1573 	if (var[0] != '\0')
  1574 	{
  1575 		const char *pass = engine->GetClientConVarValue(client, var);
  1576 		m_LastPassword.assign(pass ? pass : "");
  1577 	}
  1578 	else
  1579 	{
  1580 		m_LastPassword.assign("");
  1581 	}
  1582 }
  1583 
  1584 void CPlayer::Authorize(const char *steamid)
  1585 {
  1586 	if (m_IsAuthorized)
  1587 	{
  1588 		return;
  1589 	}
  1590 
  1591 	m_IsAuthorized = true;
  1592 	m_AuthID.assign(steamid);
  1593 }
  1594 
  1595 void CPlayer::Disconnect()
  1596 {
  1597 	DumpAdmin(false);
  1598 	m_IsConnected = false;
  1599 	m_IsInGame = false;
  1600 	m_IsAuthorized = false;
  1601 	m_Name.clear();
  1602 	m_Ip.clear();
  1603 	m_AuthID.clear();
  1604 	m_pEdict = NULL;
  1605 	m_Info = NULL;
  1606 	m_bAdminCheckSignalled = false;
  1607 	m_UserId = -1;
  1608 	m_bIsInKickQueue = false;
  1609 	m_bFakeClient = false;
  1610 	m_bIsSourceTV = false;
  1611 	m_bIsReplay = false;
  1612 	m_Serial.value = -1;
  1613 }
  1614 
  1615 void CPlayer::SetName(const char *name)
  1616 {
  1617 	m_Name.assign(name);
  1618 }
  1619 
  1620 const char *CPlayer::GetName()
  1621 {
  1622 	if (m_Info && m_pEdict->GetUnknown())
  1623 	{
  1624 		return m_Info->GetName();
  1625 	}
  1626 	
  1627 	return m_Name.c_str();
  1628 }
  1629 
  1630 const char *CPlayer::GetIPAddress()
  1631 {
  1632 	return m_Ip.c_str();
  1633 }
  1634 
  1635 const char *CPlayer::GetAuthString()
  1636 {
  1637 	return m_AuthID.c_str();
  1638 }
  1639 
  1640 edict_t *CPlayer::GetEdict()
  1641 {
  1642 	return m_pEdict;
  1643 }
  1644 
  1645 bool CPlayer::IsInGame()
  1646 {
  1647 	return m_IsInGame && (m_pEdict->GetUnknown() != NULL);
  1648 }
  1649 
  1650 bool CPlayer::WasCountedAsInGame()
  1651 {
  1652 	return m_IsInGame;
  1653 }
  1654 
  1655 bool CPlayer::IsConnected()
  1656 {
  1657 	return m_IsConnected;
  1658 }
  1659 
  1660 bool CPlayer::IsAuthorized()
  1661 {
  1662 	return m_IsAuthorized;
  1663 }
  1664 
  1665 #if SOURCE_ENGINE >= SE_ORANGEBOX
  1666 bool CPlayer::IsAuthedBySteam()
  1667 {
  1668 	return engine->IsClientFullyAuthenticated( m_pEdict );
  1669 }
  1670 #endif
  1671 
  1672 IPlayerInfo *CPlayer::GetPlayerInfo()
  1673 {
  1674 	if (m_pEdict->GetUnknown())
  1675 	{
  1676 		return m_Info;
  1677 	}
  1678 
  1679 	return NULL;
  1680 }
  1681 
  1682 bool CPlayer::IsFakeClient()
  1683 {
  1684 	return m_bFakeClient;
  1685 }
  1686 
  1687 bool CPlayer::IsSourceTV() const
  1688 {
  1689 	return m_bIsSourceTV;
  1690 }
  1691 
  1692 bool CPlayer::IsReplay() const
  1693 {
  1694 	return m_bIsReplay;
  1695 }
  1696 
  1697 void CPlayer::SetAdminId(AdminId id, bool temporary)
  1698 {
  1699 	if (!m_IsConnected)
  1700 	{
  1701 		return;
  1702 	}
  1703 
  1704 	DumpAdmin(false);
  1705 
  1706 	m_Admin = id;
  1707 	m_TempAdmin = temporary;
  1708 }
  1709 
  1710 AdminId CPlayer::GetAdminId()
  1711 {
  1712 	return m_Admin;
  1713 }
  1714 
  1715 void CPlayer::DumpAdmin(bool deleting)
  1716 {
  1717 	if (m_Admin != INVALID_ADMIN_ID)
  1718 	{
  1719 		if (m_TempAdmin && !deleting)
  1720 		{
  1721 			g_Admins.InvalidateAdmin(m_Admin);
  1722 		}
  1723 		m_Admin = INVALID_ADMIN_ID;
  1724 		m_TempAdmin = false;
  1725 	}
  1726 }
  1727 
  1728 void CPlayer::Kick(const char *str)
  1729 {
  1730 	MarkAsBeingKicked();
  1731 	INetChannel *pNetChan = static_cast<INetChannel *>(engine->GetPlayerNetInfo(m_iIndex));
  1732 	if (pNetChan == NULL)
  1733 	{
  1734 		/* What does this even mean? Hell if I know. */
  1735 		int userid = GetUserId();
  1736 		if (userid > 0)
  1737 		{
  1738 			char buffer[255];
  1739 			UTIL_Format(buffer, sizeof(buffer), "kickid %d %s\n", userid, str);
  1740 			engine->ServerCommand(buffer);
  1741 		}
  1742 	}
  1743 	else
  1744 	{
  1745 		IClient *pClient = static_cast<IClient *>(pNetChan->GetMsgHandler());
  1746 		pClient->Disconnect("%s", str);
  1747 	}
  1748 }
  1749 
  1750 void CPlayer::Authorize_Post()
  1751 {
  1752 	if (m_IsInGame)
  1753 	{
  1754 		DoPostConnectAuthorization();
  1755 	}
  1756 }
  1757 
  1758 void CPlayer::DoPostConnectAuthorization()
  1759 {
  1760 	bool delay = false;
  1761 
  1762 	List<IClientListener *>::iterator iter;
  1763 	for (iter = g_Players.m_hooks.begin();
  1764 		 iter != g_Players.m_hooks.end();
  1765 		 iter++)
  1766 	{
  1767 		IClientListener *pListener = (*iter);
  1768 #if defined MIN_API_FOR_ADMINCALLS
  1769 		if (pListener->GetClientListenerVersion() < MIN_API_FOR_ADMINCALLS)
  1770 		{
  1771 			continue;
  1772 		}
  1773 #endif
  1774 		if (!pListener->OnClientPreAdminCheck(m_iIndex))
  1775 		{
  1776 			delay = true;
  1777 		}
  1778 	}
  1779 	
  1780 	cell_t result = 0;
  1781 	PreAdminCheck->PushCell(m_iIndex);
  1782 	PreAdminCheck->Execute(&result);
  1783 
  1784 	/* Defer, for better or worse */
  1785 	if (delay || (ResultType)result >= Pl_Handled)
  1786 	{
  1787 		return;
  1788 	}
  1789 
  1790 	/* Sanity check */
  1791 	if (!IsConnected())
  1792 	{
  1793 		return;
  1794 	}
  1795 
  1796 	/* Otherwise, go ahead and do admin checks */
  1797 	DoBasicAdminChecks();
  1798 
  1799 	/* Send the notification out */
  1800 	NotifyPostAdminChecks();
  1801 }
  1802 
  1803 bool CPlayer::RunAdminCacheChecks()
  1804 {
  1805 	AdminId old_id = GetAdminId();
  1806 
  1807 	DoBasicAdminChecks();
  1808 
  1809 	return (GetAdminId() != old_id);
  1810 }
  1811 
  1812 void CPlayer::NotifyPostAdminChecks()
  1813 {
  1814 	if (m_bAdminCheckSignalled)
  1815 	{
  1816 		return;
  1817 	}
  1818 
  1819 	/* Block beforehand so they can't double-call */
  1820 	m_bAdminCheckSignalled = true;
  1821 
  1822 	List<IClientListener *>::iterator iter;
  1823 	for (iter = g_Players.m_hooks.begin();
  1824 		iter != g_Players.m_hooks.end();
  1825 		iter++)
  1826 	{
  1827 		IClientListener *pListener = (*iter);
  1828 #if defined MIN_API_FOR_ADMINCALLS
  1829 		if (pListener->GetClientListenerVersion() < MIN_API_FOR_ADMINCALLS)
  1830 		{
  1831 			continue;
  1832 		}
  1833 #endif
  1834 		pListener->OnClientPostAdminCheck(m_iIndex);
  1835 	}
  1836 
  1837 	PostAdminFilter->PushCell(m_iIndex);
  1838 	PostAdminFilter->Execute(NULL);
  1839 
  1840 	PostAdminCheck->PushCell(m_iIndex);
  1841 	PostAdminCheck->Execute(NULL);
  1842 }
  1843 
  1844 void CPlayer::DoBasicAdminChecks()
  1845 {
  1846 	if (GetAdminId() != INVALID_ADMIN_ID)
  1847 	{
  1848 		return;
  1849 	}
  1850 
  1851 	/* First check the name */
  1852 	AdminId id;
  1853 	int client = IndexOfEdict(m_pEdict);
  1854 
  1855 	if ((id = g_Admins.FindAdminByIdentity("name", GetName())) != INVALID_ADMIN_ID)
  1856 	{
  1857 		if (!g_Players.CheckSetAdminName(client, this, id))
  1858 		{
  1859 			int userid = engine->GetPlayerUserId(m_pEdict);
  1860 			g_Timers.CreateTimer(&s_KickPlayerTimer, 0.1f, (void *)userid, 0);
  1861 		}
  1862 		return;
  1863 	}
  1864 	
  1865 	/* Check IP */
  1866 	if ((id = g_Admins.FindAdminByIdentity("ip", m_IpNoPort.c_str())) != INVALID_ADMIN_ID)
  1867 	{
  1868 		if (g_Players.CheckSetAdmin(client, this, id))
  1869 		{
  1870 			return;
  1871 		}
  1872 	}
  1873 
  1874 	/* Check steam id */
  1875 	if ((id = g_Admins.FindAdminByIdentity("steam", m_AuthID.c_str())) != INVALID_ADMIN_ID)
  1876 	{
  1877 		if (g_Players.CheckSetAdmin(client, this, id))
  1878 		{
  1879 			return;
  1880 		}
  1881 	}
  1882 }
  1883 
  1884 unsigned int CPlayer::GetLanguageId()
  1885 {
  1886 	return m_LangId;
  1887 }
  1888 
  1889 void CPlayer::SetLanguageId(unsigned int id)
  1890 {
  1891 	m_LangId = id;
  1892 }
  1893 
  1894 int CPlayer::GetUserId()
  1895 {
  1896 	if (m_UserId == -1)
  1897 	{
  1898 		m_UserId = engine->GetPlayerUserId(GetEdict());
  1899 	}
  1900 
  1901 	return m_UserId;
  1902 }
  1903 
  1904 bool CPlayer::IsInKickQueue()
  1905 {
  1906 	return m_bIsInKickQueue;
  1907 }
  1908 
  1909 void CPlayer::MarkAsBeingKicked()
  1910 {
  1911 	m_bIsInKickQueue = true;
  1912 }
  1913 
  1914 int CPlayer::GetLifeState()
  1915 {
  1916 	if (lifestate_offset == -1)
  1917 	{
  1918 		if (!g_pGameConf->GetOffset("m_lifeState", &lifestate_offset))
  1919 		{
  1920 			lifestate_offset = -2;
  1921 		}
  1922 	}
  1923 
  1924 	if (lifestate_offset < 0)
  1925 	{
  1926 		IPlayerInfo *info = GetPlayerInfo();
  1927 		if (info == NULL)
  1928 		{
  1929 			return PLAYER_LIFE_UNKNOWN;
  1930 		}
  1931 		return info->IsDead() ? PLAYER_LIFE_DEAD : PLAYER_LIFE_ALIVE;
  1932 	}
  1933 
  1934 	if (m_pEdict == NULL)
  1935 	{
  1936 		return PLAYER_LIFE_UNKNOWN;
  1937 	}
  1938 
  1939 	CBaseEntity *pEntity;
  1940 	IServerUnknown *pUnknown = m_pEdict->GetUnknown();
  1941 	if (pUnknown == NULL || (pEntity = pUnknown->GetBaseEntity()) == NULL)
  1942 	{
  1943 		return PLAYER_LIFE_UNKNOWN;
  1944 	}
  1945 
  1946 	if (*((uint8_t *)pEntity + lifestate_offset) == LIFE_ALIVE)
  1947 	{
  1948 		return PLAYER_LIFE_ALIVE;
  1949 	}
  1950 	else
  1951 	{
  1952 		return PLAYER_LIFE_DEAD;
  1953 	}
  1954 }
  1955 
  1956 unsigned int CPlayer::GetSerial()
  1957 {
  1958 	return m_Serial.value;
  1959 }