core/PlayerManager.h
author Asher Baker <asherkin@gmail.com>
Sun Aug 14 21:42:32 2011 +0100 (2011-08-14)
changeset 3367 18eb63085ca4
parent 3350 4e7c8ee5bc0e
child 3418 b0e93f5101ef
permissions -rw-r--r--
Added a reset argument to 'sm cvars' (bug 5043, r=psychonic).
     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 		uint8_t  index;
    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();
    73 	edict_t *GetEdict();
    74 	bool IsInGame();
    75 	bool WasCountedAsInGame();
    76 	bool IsConnected();
    77 	bool IsAuthorized();
    78 	bool IsFakeClient();
    79 	void SetAdminId(AdminId id, bool temporary);
    80 	AdminId GetAdminId();
    81 	void Kick(const char *str);
    82 	bool IsInKickQueue();
    83 	IPlayerInfo *GetPlayerInfo();
    84 	unsigned int GetLanguageId();
    85 	void SetLanguageId(unsigned int id);
    86 	int GetUserId();
    87 	bool RunAdminCacheChecks();
    88 	void NotifyPostAdminChecks();
    89 	unsigned int GetSerial();
    90 public:
    91 	void DoBasicAdminChecks();
    92 	void MarkAsBeingKicked();
    93 	int GetLifeState();
    94 private:
    95 	void Initialize(const char *name, const char *ip, edict_t *pEntity);
    96 	void Connect();
    97 	void Disconnect();
    98 	void SetName(const char *name);
    99 	void DumpAdmin(bool deleting);
   100 	void Authorize(const char *auth);
   101 	void Authorize_Post();
   102 	void DoPostConnectAuthorization();
   103 private:
   104 	bool m_IsConnected;
   105 	bool m_IsInGame;
   106 	bool m_IsAuthorized;
   107 	bool m_bIsInKickQueue;
   108 	String m_Name;
   109 	String m_Ip;
   110 	String m_IpNoPort;
   111 	String m_AuthID;
   112 	AdminId m_Admin;
   113 	bool m_TempAdmin;
   114 	edict_t *m_pEdict;
   115 	IPlayerInfo *m_Info;
   116 	String m_LastPassword;
   117 	bool m_bAdminCheckSignalled;
   118 	int m_iIndex;
   119 	unsigned int m_LangId;
   120 	int m_UserId;
   121 	bool m_bFakeClient;
   122 	serial_t m_Serial;
   123 };
   124 
   125 class PlayerManager : 
   126 	public SMGlobalClass,
   127 	public IPlayerManager
   128 {
   129 	friend class CPlayer;
   130 public:
   131 	PlayerManager();
   132 	~PlayerManager();
   133 public: //SMGlobalClass
   134 	void OnSourceModAllInitialized();
   135 	void OnSourceModShutdown();
   136 	void OnSourceModLevelEnd();
   137 	ConfigResult OnSourceModConfigChanged(const char *key, const char *value, ConfigSource source, char *error, size_t maxlength);
   138 	void OnSourceModMaxPlayersChanged(int newvalue);
   139 public:
   140 	CPlayer *GetPlayerByIndex(int client) const;
   141 	void RunAuthChecks();
   142 	void ClearAdminId(AdminId id);
   143 	void ClearAllAdmins();
   144 public:
   145 	bool OnClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen);
   146 	bool OnClientConnect_Post(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen);
   147 	void OnClientPutInServer(edict_t *pEntity, char const *playername);
   148 	void OnClientDisconnect(edict_t *pEntity);
   149 	void OnClientDisconnect_Post(edict_t *pEntity);
   150 #if SOURCE_ENGINE >= SE_ORANGEBOX
   151 	void OnClientCommand(edict_t *pEntity, const CCommand &args);
   152 #else
   153 	void OnClientCommand(edict_t *pEntity);
   154 #endif
   155 	void OnClientSettingsChanged(edict_t *pEntity);
   156 	//void OnClientSettingsChanged_Pre(edict_t *pEntity);
   157 public: //IPlayerManager
   158 	void AddClientListener(IClientListener *listener);
   159 	void RemoveClientListener(IClientListener *listener);
   160 	IGamePlayer *GetGamePlayer(int client);
   161 	IGamePlayer *GetGamePlayer(edict_t *pEdict);
   162 	int GetMaxClients();
   163 	int GetNumPlayers();
   164 	int GetClientOfUserId(int userid);
   165 	bool IsServerActivated();
   166 	int FilterCommandTarget(IGamePlayer *pAdmin, IGamePlayer *pTarget, int flags);
   167 	int InternalFilterCommandTarget(CPlayer *pAdmin, CPlayer *pTarget, int flags);
   168 	void RegisterCommandTargetProcessor(ICommandTargetProcessor *pHandler);
   169 	void UnregisterCommandTargetProcessor(ICommandTargetProcessor *pHandler);
   170 	void ProcessCommandTarget(cmd_target_info_t *info);
   171 	int GetClientFromSerial(unsigned int serial);
   172 public:
   173 	inline int MaxClients()
   174 	{
   175 		return m_maxClients;
   176 	}
   177 	inline int NumPlayers()
   178 	{
   179 		return m_PlayerCount;
   180 	}
   181 	inline int ListenClient()
   182 	{
   183 		return m_ListenClient;
   184 	}
   185 	bool CheckSetAdmin(int index, CPlayer *pPlayer, AdminId id);
   186 	bool CheckSetAdminName(int index, CPlayer *pPlayer, AdminId id);
   187 	const char *GetPassInfoVar();
   188 	void RecheckAnyAdmins();
   189 	unsigned int GetReplyTo();
   190 	unsigned int SetReplyTo(unsigned int reply);
   191 	void MaxPlayersChanged(int newvalue = -1);
   192 private:
   193 	void OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax);
   194 	void InvalidatePlayer(CPlayer *pPlayer);
   195 private:
   196 	List<IClientListener *> m_hooks;
   197 	IForward *m_clconnect;
   198 	IForward *m_clconnect_post;
   199 	IForward *m_cldisconnect;
   200 	IForward *m_cldisconnect_post;
   201 	IForward *m_clputinserver;
   202 	IForward *m_clcommand;
   203 	IForward *m_clinfochanged;
   204 	IForward *m_clauth;
   205 	IForward *m_onActivate;
   206 	IForward *m_onActivate2;
   207 	CPlayer *m_Players;
   208 	int *m_UserIdLookUp;
   209 	int m_maxClients;
   210 	int m_PlayerCount;
   211 	bool m_FirstPass;
   212 	unsigned int *m_AuthQueue;
   213 	String m_PassInfoVar;
   214 	bool m_QueryLang;
   215 	bool m_bIsListenServer;
   216 	int m_ListenClient;
   217 };
   218 
   219 #if SOURCE_ENGINE >= SE_ORANGEBOX
   220 void CmdMaxplayersCallback(const CCommand &command);
   221 #else
   222 void CmdMaxplayersCallback();
   223 #endif
   224 
   225 extern void ClientConsolePrint(edict_t *e, const char *fmt, ...);
   226 
   227 extern PlayerManager g_Players;
   228 extern bool g_OnMapStarted;
   229 extern const unsigned int *g_NumPlayersToAuth;
   230 
   231 #endif //_INCLUDE_SOURCEMOD_CPLAYERMANAGER_H_