core/ConCmdManager.h
author Borja Ferrer <faluco@alliedmods.net>
Sat Feb 23 20:18:49 2008 +0000 (2008-02-23)
changeset 1894 e6efc8ffa47a
parent 1584 8158e9eafff5
child 1947 e73a1e51833b
permissions -rw-r--r--
Executable memory is now provided by MM:S's allocator, this should reduce virtual memory usage.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 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_CONCMDMANAGER_H_
    33 #define _INCLUDE_SOURCEMOD_CONCMDMANAGER_H_
    34 
    35 #include "sm_globals.h"
    36 #include "sourcemm_api.h"
    37 #include "ForwardSys.h"
    38 #include "sm_trie.h"
    39 #include "sm_memtable.h"
    40 #include <sh_list.h>
    41 #include <sh_string.h>
    42 #include <IRootConsoleMenu.h>
    43 #include <IAdminSystem.h>
    44 #include "concmd_cleaner.h"
    45 
    46 using namespace SourceHook;
    47 
    48 enum CmdType
    49 {
    50 	Cmd_Server,
    51 	Cmd_Console,
    52 	Cmd_Admin,
    53 };
    54 
    55 struct AdminCmdInfo
    56 {
    57 	AdminCmdInfo()
    58 	{
    59 		cmdGrpId = -1;
    60 		flags = 0;
    61 		eflags = 0;
    62 	}
    63 	int cmdGrpId;			/* index into cmdgroup string table */
    64 	FlagBits flags;			/* default flags */
    65 	FlagBits eflags;		/* effective flags */
    66 };
    67 
    68 struct CmdHook
    69 {
    70 	CmdHook()
    71 	{
    72 		pf = NULL;
    73 		pAdmin = NULL;
    74 	}
    75 	IPluginFunction *pf;	/* function hook */
    76 	String helptext;		/* help text */
    77 	AdminCmdInfo *pAdmin;	/* admin requirements, if any */
    78 };
    79 
    80 struct ConCmdInfo
    81 {
    82 	ConCmdInfo()
    83 	{
    84 		sourceMod = false;
    85 		pCmd = NULL;
    86 	}
    87 	bool sourceMod;					/**< Determines whether or not concmd was created by a SourceMod plugin */
    88 	ConCommand *pCmd;				/**< Pointer to the command itself */
    89 	List<CmdHook *> srvhooks;		/**< Hooks as a server command */
    90 	List<CmdHook *> conhooks;		/**< Hooks as a console command */
    91 	AdminCmdInfo admin;				/**< Admin info, if any */
    92 	bool is_admin_set;				/**< Whether or not admin info is set */
    93 };
    94 
    95 class ConCmdManager :
    96 	public SMGlobalClass,
    97 	public IRootConsoleCommand,
    98 	public IPluginsListener,
    99 	public IConCommandTracker
   100 {
   101 #if defined ORANGEBOX_BUILD
   102 	friend void CommandCallback(const CCommand &command);
   103 #else
   104 	friend void CommandCallback();
   105 #endif
   106 public:
   107 	ConCmdManager();
   108 	~ConCmdManager();
   109 public: //SMGlobalClass
   110 	void OnSourceModAllInitialized();
   111 	void OnSourceModShutdown();
   112 public: //IPluginsListener
   113 	void OnPluginDestroyed(IPlugin *plugin);
   114 public: //IRootConsoleCommand
   115 	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
   116 public: //IConCommandTracker
   117 	void OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe);
   118 public:
   119 	bool AddServerCommand(IPluginFunction *pFunction, const char *name, const char *description, int flags);
   120 	bool AddConsoleCommand(IPluginFunction *pFunction, const char *name, const char *description, int flags);
   121 	bool AddAdminCommand(IPluginFunction *pFunction, 
   122 						 const char *name, 
   123 						 const char *group,
   124 						 int adminflags,
   125 						 const char *description, 
   126 						 int flags);
   127 	ResultType DispatchClientCommand(int client, const char *cmd, int args, ResultType type);
   128 	void UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits, bool remove);
   129 	bool LookForSourceModCommand(const char *cmd);
   130 	bool LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags);
   131 	bool CheckCommandAccess(int client, const char *cmd, FlagBits flags);
   132 private:
   133 	void InternalDispatch(const CCommand &command);
   134 	ResultType RunAdminCommand(ConCmdInfo *pInfo, int client, int args);
   135 	ConCmdInfo *AddOrFindCommand(const char *name, const char *description, int flags);
   136 	void SetCommandClient(int client);
   137 	void AddToCmdList(ConCmdInfo *info);
   138 	void RemoveConCmd(ConCmdInfo *info, const char *cmd, bool is_read_safe, bool untrack);
   139 	void RemoveConCmds(List<CmdHook *> &cmdlist);
   140 	void RemoveConCmds(List<CmdHook *> &cmdlist, IPluginContext *pContext);
   141 	bool CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin);
   142 public:
   143 	inline int GetCommandClient()
   144 	{
   145 		return m_CmdClient;
   146 	}
   147 	inline const List<ConCmdInfo *> & GetCommandList()
   148 	{
   149 		return m_CmdList;
   150 	}
   151 private:
   152 	Trie *m_pCmds;					/* command lookup */
   153 	Trie *m_pCmdGrps;				/* command group lookup */
   154 	List<ConCmdInfo *> m_CmdList;	/* command list */
   155 	int m_CmdClient;				/* current client */
   156 	BaseStringTable m_Strings;		/* string table */
   157 };
   158 
   159 extern ConCmdManager g_ConCmds;
   160 
   161 #endif // _INCLUDE_SOURCEMOD_CONCMDMANAGER_H_