core/PlayerManager.h
author David Anderson <danderson@mozilla.com>
Thu Aug 08 09:40:23 2013 -0700 (2013-08-08)
changeset 3976 bc77cfde7c5c
parent 3942 7aaa00382627
child 4217 4c87212c5817
permissions -rw-r--r--
Add -Wno-narrowing to support building on GCC-4.7 (no bug, r=me).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 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 #ifndef _INCLUDE_SOURCEMOD_CPLAYERMANAGER_H_
    33 #define _INCLUDE_SOURCEMOD_CPLAYERMANAGER_H_
    34 
    35 #include "sm_globals.h"
    36 #include <eiface.h>
    37 #include "sourcemm_api.h"
    38 #include <IForwardSys.h>
    39 #include <IPlayerHelpers.h>
    40 #include <IAdminSystem.h>
    41 #include <sh_string.h>
    42 #include <sh_list.h>
    43 #include <sh_vector.h>
    44 #include "ConVarManager.h"
    45 
    46 using namespace SourceHook;
    47 
    48 #define PLAYER_LIFE_UNKNOWN	0
    49 #define PLAYER_LIFE_ALIVE	1
    50 #define PLAYER_LIFE_DEAD	2
    51 
    52 #define MIN_API_FOR_ADMINCALLS		7
    53 
    54 union serial_t
    55 {
    56 	uint32_t   value;
    57 	struct
    58 	{
    59 		uint32_t index  :  8;
    60 		uint32_t serial : 24;
    61 	} bits;
    62 };
    63 
    64 class CPlayer : public IGamePlayer
    65 {
    66 	friend class PlayerManager;
    67 public:
    68 	CPlayer();
    69 public:
    70 	const char *GetName();
    71 	const char *GetIPAddress();
    72 	const char *GetAuthString(bool validated = true);
    73 	unsigned int GetSteamAccountID(bool validated = true);
    74 	edict_t *GetEdict();
    75 	bool IsInGame();
    76 	bool WasCountedAsInGame();
    77 	bool IsConnected();
    78 	bool IsAuthorized();
    79 	bool IsFakeClient();
    80 	bool IsSourceTV() const;
    81 	bool IsReplay() const;
    82 	void SetAdminId(AdminId id, bool temporary);
    83 	AdminId GetAdminId();
    84 	void Kick(const char *str);
    85 	bool IsInKickQueue();
    86 	IPlayerInfo *GetPlayerInfo();
    87 	unsigned int GetLanguageId();
    88 	void SetLanguageId(unsigned int id);
    89 	int GetUserId();
    90 	bool RunAdminCacheChecks();
    91 	void NotifyPostAdminChecks();
    92 	unsigned int GetSerial();
    93 	int GetIndex() const;
    94 public:
    95 	void DoBasicAdminChecks();
    96 	void MarkAsBeingKicked();
    97 	int GetLifeState();
    98 private:
    99 	void Initialize(const char *name, const char *ip, edict_t *pEntity);
   100 	void Connect();
   101 	void Disconnect();
   102 	void SetName(const char *name);
   103 	void DumpAdmin(bool deleting);
   104 	void SetAuthString(const char *auth);
   105 	void Authorize();
   106 	void Authorize_Post();
   107 	void DoPostConnectAuthorization();
   108 	bool IsAuthStringValidated();
   109 private:
   110 	bool m_IsConnected;
   111 	bool m_IsInGame;
   112 	bool m_IsAuthorized;
   113 	bool m_bIsInKickQueue;
   114 	String m_Name;
   115 	String m_Ip;
   116 	String m_IpNoPort;
   117 	String m_AuthID;
   118 	AdminId m_Admin;
   119 	bool m_TempAdmin;
   120 	edict_t *m_pEdict;
   121 	IPlayerInfo *m_Info;
   122 	String m_LastPassword;
   123 	bool m_bAdminCheckSignalled;
   124 	int m_iIndex;
   125 	unsigned int m_LangId;
   126 	int m_UserId;
   127 	bool m_bFakeClient;
   128 	bool m_bIsSourceTV;
   129 	bool m_bIsReplay;
   130 	serial_t m_Serial;
   131 	unsigned int m_SteamAccountID;
   132 };
   133 
   134 class PlayerManager : 
   135 	public SMGlobalClass,
   136 	public IPlayerManager
   137 {
   138 	friend class CPlayer;
   139 public:
   140 	PlayerManager();
   141 	~PlayerManager();
   142 public: //SMGlobalClass
   143 	void OnSourceModAllInitialized();
   144 	void OnSourceModShutdown();
   145 	void OnSourceModLevelEnd();
   146 	ConfigResult OnSourceModConfigChanged(const char *key, const char *value, ConfigSource source, char *error, size_t maxlength);
   147 	void OnSourceModMaxPlayersChanged(int newvalue);
   148 public:
   149 	CPlayer *GetPlayerByIndex(int client) const;
   150 	void RunAuthChecks();
   151 	void ClearAdminId(AdminId id);
   152 	void ClearAllAdmins();
   153 public:
   154 #if SOURCE_ENGINE == SE_DOTA
   155 	bool OnClientConnect(CEntityIndex index, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen);
   156 	bool OnClientConnect_Post(CEntityIndex index, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen);
   157 	void OnClientPutInServer(CEntityIndex index, char const *playername);
   158 	void OnClientDisconnect(CEntityIndex index);
   159 	void OnClientDisconnect_Post(CEntityIndex index);
   160 	void OnClientCommand(CEntityIndex index, const CCommand &args);
   161 	void OnClientSettingsChanged(CEntityIndex index);
   162 	//void OnClientSettingsChanged_Pre(CEntityIndex client);
   163 #else
   164 	bool OnClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen);
   165 	bool OnClientConnect_Post(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen);
   166 	void OnClientPutInServer(edict_t *pEntity, char const *playername);
   167 	void OnClientDisconnect(edict_t *pEntity);
   168 	void OnClientDisconnect_Post(edict_t *pEntity);
   169 #if SOURCE_ENGINE >= SE_ORANGEBOX
   170 	void OnClientCommand(edict_t *pEntity, const CCommand &args);
   171 #else
   172 	void OnClientCommand(edict_t *pEntity);
   173 #endif
   174 	void OnClientSettingsChanged(edict_t *pEntity);
   175 	//void OnClientSettingsChanged_Pre(edict_t *pEntity);
   176 #endif
   177 public: //IPlayerManager
   178 	void AddClientListener(IClientListener *listener);
   179 	void RemoveClientListener(IClientListener *listener);
   180 	IGamePlayer *GetGamePlayer(int client);
   181 	IGamePlayer *GetGamePlayer(edict_t *pEdict);
   182 	int GetMaxClients();
   183 	int GetNumPlayers();
   184 	int GetClientOfUserId(int userid);
   185 	bool IsServerActivated();
   186 	int FilterCommandTarget(IGamePlayer *pAdmin, IGamePlayer *pTarget, int flags);
   187 	int InternalFilterCommandTarget(CPlayer *pAdmin, CPlayer *pTarget, int flags);
   188 	void RegisterCommandTargetProcessor(ICommandTargetProcessor *pHandler);
   189 	void UnregisterCommandTargetProcessor(ICommandTargetProcessor *pHandler);
   190 	void ProcessCommandTarget(cmd_target_info_t *info);
   191 	int GetClientFromSerial(unsigned int serial);
   192 public:
   193 	inline int MaxClients()
   194 	{
   195 		return m_maxClients;
   196 	}
   197 	inline int NumPlayers()
   198 	{
   199 		return m_PlayerCount;
   200 	}
   201 	inline int ListenClient()
   202 	{
   203 		return m_ListenClient;
   204 	}
   205 	bool CheckSetAdmin(int index, CPlayer *pPlayer, AdminId id);
   206 	bool CheckSetAdminName(int index, CPlayer *pPlayer, AdminId id);
   207 	const char *GetPassInfoVar();
   208 	void RecheckAnyAdmins();
   209 	unsigned int GetReplyTo();
   210 	unsigned int SetReplyTo(unsigned int reply);
   211 	void MaxPlayersChanged(int newvalue = -1);
   212 private:
   213 #if SOURCE_ENGINE == SE_DOTA
   214 	void OnServerActivate();
   215 #else
   216 	void OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax);
   217 #endif
   218 	void InvalidatePlayer(CPlayer *pPlayer);
   219 private:
   220 	List<IClientListener *> m_hooks;
   221 	IForward *m_clconnect;
   222 	IForward *m_clconnect_post;
   223 	IForward *m_cldisconnect;
   224 	IForward *m_cldisconnect_post;
   225 	IForward *m_clputinserver;
   226 	IForward *m_clcommand;
   227 	IForward *m_clinfochanged;
   228 	IForward *m_clauth;
   229 	IForward *m_onActivate;
   230 	IForward *m_onActivate2;
   231 	CPlayer *m_Players;
   232 	int *m_UserIdLookUp;
   233 	int m_maxClients;
   234 	int m_PlayerCount;
   235 	int m_PlayersSinceActive;
   236 	bool m_FirstPass;
   237 	unsigned int *m_AuthQueue;
   238 	String m_PassInfoVar;
   239 	bool m_QueryLang;
   240 	bool m_bAuthstringValidation; // are we validating admins with steam before authorizing?
   241 	bool m_bIsListenServer;
   242 	int m_ListenClient;
   243 	bool m_bIsSourceTVActive;
   244 	bool m_bIsReplayActive;
   245 	int m_SourceTVUserId;
   246 	int m_ReplayUserId;
   247 };
   248 
   249 #if SOURCE_ENGINE == SE_DOTA
   250 void CmdMaxplayersCallback(const CCommandContext &context, const CCommand &command);
   251 #elif SOURCE_ENGINE >= SE_ORANGEBOX
   252 void CmdMaxplayersCallback(const CCommand &command);
   253 #else
   254 void CmdMaxplayersCallback();
   255 #endif
   256 
   257 extern void ClientConsolePrint(edict_t *e, const char *fmt, ...);
   258 
   259 extern PlayerManager g_Players;
   260 extern bool g_OnMapStarted;
   261 extern const unsigned int *g_NumPlayersToAuth;
   262 
   263 #endif //_INCLUDE_SOURCEMOD_CPLAYERMANAGER_H_