core/PlayerManager.h
author David Anderson <dvander@alliedmods.net>
Wed Feb 25 20:07:12 2009 -0500 (2009-02-25)
changeset 2607 f3b8bbe17f85
parent 2323 efa6bfdd7571
child 2400 76b7b4575e21
permissions -rw-r--r--
Backed out changeset fa286305bd3f
     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 class CPlayer : public IGamePlayer
    55 {
    56 	friend class PlayerManager;
    57 public:
    58 	CPlayer();
    59 public:
    60 	const char *GetName();
    61 	const char *GetIPAddress();
    62 	const char *GetAuthString();
    63 	edict_t *GetEdict();
    64 	bool IsInGame();
    65 	bool WasCountedAsInGame();
    66 	bool IsConnected();
    67 	bool IsAuthorized();
    68 	bool IsFakeClient();
    69 	void SetAdminId(AdminId id, bool temporary);
    70 	AdminId GetAdminId();
    71 	void Kick(const char *str);
    72 	IPlayerInfo *GetPlayerInfo();
    73 	unsigned int GetLanguageId();
    74 	int GetUserId();
    75 	bool RunAdminCacheChecks();
    76 	void NotifyPostAdminChecks();
    77 public:
    78 	void DoBasicAdminChecks();
    79 	bool IsInKickQueue();
    80 	void MarkAsBeingKicked();
    81 	int GetLifeState();
    82 private:
    83 	void Initialize(const char *name, const char *ip, edict_t *pEntity);
    84 	void Connect();
    85 	void Disconnect();
    86 	void SetName(const char *name);
    87 	void DumpAdmin(bool deleting);
    88 	void Authorize(const char *auth);
    89 	void Authorize_Post();
    90 	void DoPostConnectAuthorization();
    91 private:
    92 	bool m_IsConnected;
    93 	bool m_IsInGame;
    94 	bool m_IsAuthorized;
    95 	bool m_bIsInKickQueue;
    96 	String m_Name;
    97 	String m_Ip;
    98 	String m_IpNoPort;
    99 	String m_AuthID;
   100 	AdminId m_Admin;
   101 	bool m_TempAdmin;
   102 	edict_t *m_pEdict;
   103 	IPlayerInfo *m_Info;
   104 	String m_LastPassword;
   105 	bool m_bAdminCheckSignalled;
   106 	int m_iIndex;
   107 	unsigned int m_LangId;
   108 	int m_UserId;
   109 	bool m_bFakeClient;
   110 };
   111 
   112 class PlayerManager : 
   113 	public SMGlobalClass,
   114 	public IPlayerManager,
   115 	public IConVarChangeListener
   116 {
   117 	friend class CPlayer;
   118 public:
   119 	PlayerManager();
   120 	~PlayerManager();
   121 public: //SMGlobalClass
   122 	void OnSourceModAllInitialized();
   123 	void OnSourceModShutdown();
   124 	void OnSourceModLevelEnd();
   125 	ConfigResult OnSourceModConfigChanged(const char *key, const char *value, ConfigSource source, char *error, size_t maxlength);
   126 	void OnSourceModMaxPlayersChanged(int newvalue);
   127 public:
   128 	CPlayer *GetPlayerByIndex(int client) const;
   129 	void RunAuthChecks();
   130 	void ClearAdminId(AdminId id);
   131 	void ClearAllAdmins();
   132 public:
   133 	bool OnClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen);
   134 	bool OnClientConnect_Post(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen);
   135 	void OnClientPutInServer(edict_t *pEntity, char const *playername);
   136 	void OnClientDisconnect(edict_t *pEntity);
   137 	void OnClientDisconnect_Post(edict_t *pEntity);
   138 #if defined ORANGEBOX_BUILD
   139 	void OnClientCommand(edict_t *pEntity, const CCommand &args);
   140 #else
   141 	void OnClientCommand(edict_t *pEntity);
   142 #endif
   143 	void OnClientSettingsChanged(edict_t *pEntity);
   144 	//void OnClientSettingsChanged_Pre(edict_t *pEntity);
   145 public: //IPlayerManager
   146 	void AddClientListener(IClientListener *listener);
   147 	void RemoveClientListener(IClientListener *listener);
   148 	IGamePlayer *GetGamePlayer(int client);
   149 	IGamePlayer *GetGamePlayer(edict_t *pEdict);
   150 	int GetMaxClients();
   151 	int GetNumPlayers();
   152 	int GetClientOfUserId(int userid);
   153 	bool IsServerActivated();
   154 	int FilterCommandTarget(IGamePlayer *pAdmin, IGamePlayer *pTarget, int flags);
   155 	int InternalFilterCommandTarget(CPlayer *pAdmin, CPlayer *pTarget, int flags);
   156 	void RegisterCommandTargetProcessor(ICommandTargetProcessor *pHandler);
   157 	void UnregisterCommandTargetProcessor(ICommandTargetProcessor *pHandler);
   158 	void ProcessCommandTarget(cmd_target_info_t *info);
   159 public: // IConVarChangeListener
   160 	void OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue);
   161 public:
   162 	inline int MaxClients()
   163 	{
   164 		return m_maxClients;
   165 	}
   166 	inline int NumPlayers()
   167 	{
   168 		return m_PlayerCount;
   169 	}
   170 	inline int ListenClient()
   171 	{
   172 		return m_ListenClient;
   173 	}
   174 	bool CheckSetAdmin(int index, CPlayer *pPlayer, AdminId id);
   175 	bool CheckSetAdminName(int index, CPlayer *pPlayer, AdminId id);
   176 	const char *GetPassInfoVar();
   177 	void RecheckAnyAdmins();
   178 	unsigned int GetReplyTo();
   179 	unsigned int SetReplyTo(unsigned int reply);
   180 	void MaxPlayersChanged(int newvalue = -1);
   181 private:
   182 	void OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax);
   183 	void InvalidatePlayer(CPlayer *pPlayer);
   184 private:
   185 	List<IClientListener *> m_hooks;
   186 	IForward *m_clconnect;
   187 	IForward *m_cldisconnect;
   188 	IForward *m_cldisconnect_post;
   189 	IForward *m_clputinserver;
   190 	IForward *m_clcommand;
   191 	IForward *m_clinfochanged;
   192 	IForward *m_clauth;
   193 	IForward *m_onActivate;
   194 	IForward *m_onActivate2;
   195 	CPlayer *m_Players;
   196 	int *m_UserIdLookUp;
   197 	int m_maxClients;
   198 	int m_PlayerCount;
   199 	bool m_FirstPass;
   200 	unsigned int *m_AuthQueue;
   201 	String m_PassInfoVar;
   202 	bool m_QueryLang;
   203 	bool m_bIsListenServer;
   204 	int m_ListenClient;
   205 };
   206 
   207 #if defined ORANGEBOX_BUILD
   208 void CmdMaxplayersCallback(const CCommand &command);
   209 #else
   210 void CmdMaxplayersCallback();
   211 #endif
   212 
   213 extern PlayerManager g_Players;
   214 extern bool g_OnMapStarted;
   215 extern const unsigned int *g_NumPlayersToAuth;
   216 
   217 #endif //_INCLUDE_SOURCEMOD_CPLAYERMANAGER_H_