core/ConCmdManager.h
author David Anderson <dvander@alliedmods.net>
Mon Oct 15 00:38:25 2007 +0000 (2007-10-15)
changeset 1568 ecbaee393b0b
parent 1541 e46ad208b9b4
child 1584 8158e9eafff5
permissions -rw-r--r--
extensions can now be built for multiple engines, and will autoload from auto.X.Y folders
     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 
    45 using namespace SourceHook;
    46 
    47 enum CmdType
    48 {
    49 	Cmd_Server,
    50 	Cmd_Console,
    51 	Cmd_Admin,
    52 };
    53 
    54 struct AdminCmdInfo
    55 {
    56 	AdminCmdInfo()
    57 	{
    58 		cmdGrpId = -1;
    59 		flags = 0;
    60 		eflags = 0;
    61 	}
    62 	int cmdGrpId;			/* index into cmdgroup string table */
    63 	FlagBits flags;			/* default flags */
    64 	FlagBits eflags;		/* effective flags */
    65 };
    66 
    67 struct CmdHook
    68 {
    69 	CmdHook()
    70 	{
    71 		pf = NULL;
    72 		pAdmin = NULL;
    73 	}
    74 	IPluginFunction *pf;	/* function hook */
    75 	String helptext;		/* help text */
    76 	AdminCmdInfo *pAdmin;	/* admin requirements, if any */
    77 };
    78 
    79 struct ConCmdInfo
    80 {
    81 	ConCmdInfo()
    82 	{
    83 		sourceMod = false;
    84 		pCmd = NULL;
    85 	}
    86 	bool sourceMod;					/**< Determines whether or not concmd was created by a SourceMod plugin */
    87 	ConCommand *pCmd;				/**< Pointer to the command itself */
    88 	List<CmdHook *> srvhooks;		/**< Hooks as a server command */
    89 	List<CmdHook *> conhooks;		/**< Hooks as a console command */
    90 	AdminCmdInfo admin;				/**< Admin info, if any */
    91 	bool is_admin_set;				/**< Whether or not admin info is set */
    92 };
    93 
    94 class ConCmdManager :
    95 	public SMGlobalClass,
    96 	public IRootConsoleCommand,
    97 	public IPluginsListener
    98 {
    99 #if defined ORANGEBOX_BUILD
   100 	friend void CommandCallback(const CCommand &command);
   101 #else
   102 	friend void CommandCallback();
   103 #endif
   104 public:
   105 	ConCmdManager();
   106 	~ConCmdManager();
   107 public: //SMGlobalClass
   108 	void OnSourceModAllInitialized();
   109 	void OnSourceModShutdown();
   110 public: //IPluginsListener
   111 	void OnPluginDestroyed(IPlugin *plugin);
   112 public: //IRootConsoleCommand
   113 	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
   114 public:
   115 	bool AddServerCommand(IPluginFunction *pFunction, const char *name, const char *description, int flags);
   116 	bool AddConsoleCommand(IPluginFunction *pFunction, const char *name, const char *description, int flags);
   117 	bool AddAdminCommand(IPluginFunction *pFunction, 
   118 						 const char *name, 
   119 						 const char *group,
   120 						 int adminflags,
   121 						 const char *description, 
   122 						 int flags);
   123 	ResultType DispatchClientCommand(int client, const char *cmd, int args, ResultType type);
   124 	void UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits, bool remove);
   125 	bool LookForSourceModCommand(const char *cmd);
   126 	bool LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags);
   127 	bool CheckCommandAccess(int client, const char *cmd, FlagBits flags);
   128 private:
   129 	void InternalDispatch(const CCommand &command);
   130 	ResultType RunAdminCommand(ConCmdInfo *pInfo, int client, int args);
   131 	ConCmdInfo *AddOrFindCommand(const char *name, const char *description, int flags);
   132 	void SetCommandClient(int client);
   133 	void AddToCmdList(ConCmdInfo *info);
   134 	void RemoveConCmd(ConCmdInfo *info);
   135 	void RemoveConCmds(List<CmdHook *> &cmdlist, IPluginContext *pContext);
   136 	bool CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin);
   137 public:
   138 	inline int GetCommandClient()
   139 	{
   140 		return m_CmdClient;
   141 	}
   142 	inline const List<ConCmdInfo *> & GetCommandList()
   143 	{
   144 		return m_CmdList;
   145 	}
   146 private:
   147 	Trie *m_pCmds;					/* command lookup */
   148 	Trie *m_pCmdGrps;				/* command group lookup */
   149 	List<ConCmdInfo *> m_CmdList;	/* command list */
   150 	int m_CmdClient;				/* current client */
   151 	BaseStringTable m_Strings;		/* string table */
   152 };
   153 
   154 extern ConCmdManager g_ConCmds;
   155 
   156 #endif // _INCLUDE_SOURCEMOD_CONCMDMANAGER_H_