core/ConCmdManager.h
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 15 21:22:24 2014 -0400 (2014-05-15)
changeset 4362 8a15e967d983
parent 4139 de7d1cd00703
permissions -rw-r--r--
Update gamedata for CS:S update.
ds@579
     1
/**
dvander@4076
     2
 * vim: set ts=4 sw=4 tw=99 noet :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
ds@1947
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
ds@579
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
dvander@1231
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
dvander@1231
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1231
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
ds@579
    28
 *
ds@579
    29
 * Version: $Id$
ds@579
    30
 */
ds@579
    31
ds@579
    32
#ifndef _INCLUDE_SOURCEMOD_CONCMDMANAGER_H_
ds@579
    33
#define _INCLUDE_SOURCEMOD_CONCMDMANAGER_H_
ds@579
    34
ds@579
    35
#include "sm_globals.h"
ds@579
    36
#include "sourcemm_api.h"
skamonkey@4139
    37
#include <IForwardSys.h>
ds@579
    38
#include <sh_list.h>
ds@579
    39
#include <sh_string.h>
ds@579
    40
#include <IRootConsoleMenu.h>
ds@579
    41
#include <IAdminSystem.h>
dvander@1584
    42
#include "concmd_cleaner.h"
dvander@4076
    43
#include <sm_stringhashmap.h>
dvander@4076
    44
#include <am-utility.h>
dvander@4112
    45
#include <am-inlinelist.h>
dvander@4112
    46
#include <am-linkedlist.h>
dvander@4113
    47
#include <am-refcounting.h>
ds@579
    48
ds@579
    49
using namespace SourceHook;
ds@579
    50
dvander@4113
    51
struct CmdHook;
dvander@4113
    52
struct ConCmdInfo;
dvander@4113
    53
dvander@4113
    54
struct CommandGroup : public ke::Refcounted<CommandGroup>
dvander@4113
    55
{
dvander@4113
    56
	ke::LinkedList<CmdHook *> hooks;
dvander@4113
    57
};
dvander@4113
    58
ds@579
    59
struct AdminCmdInfo
ds@579
    60
{
dvander@4113
    61
	AdminCmdInfo(const ke::Ref<CommandGroup> &group, FlagBits flags)
dvander@4113
    62
		: group(group),
dvander@4113
    63
		  flags(flags),
dvander@4113
    64
		  eflags(0)
ds@579
    65
	{
ds@579
    66
	}
dvander@4113
    67
	ke::Ref<CommandGroup> group;
ds@579
    68
	FlagBits flags;			/* default flags */
ds@579
    69
	FlagBits eflags;		/* effective flags */
ds@579
    70
};
ds@579
    71
dvander@4112
    72
struct CmdHook : public ke::InlineListNode<CmdHook>
ds@579
    73
{
dvander@4112
    74
	enum Type {
dvander@4112
    75
		Server,
dvander@4112
    76
		Client
dvander@4112
    77
	};
dvander@4112
    78
dvander@4112
    79
	CmdHook(Type type, ConCmdInfo *cmd, IPluginFunction *fun, const char *description)
dvander@4112
    80
		: type(type),
dvander@4112
    81
		  info(cmd),
dvander@4112
    82
		  pf(fun),
dvander@4112
    83
		  helptext(description)
ds@579
    84
	{
ds@579
    85
	}
dvander@4112
    86
dvander@4112
    87
	Type type;
dvander@4112
    88
	ConCmdInfo *info;
dvander@4112
    89
	IPluginFunction *pf;				/* function hook */
dvander@4112
    90
	ke::AString helptext;				/* help text */
dvander@4112
    91
	ke::AutoPtr<AdminCmdInfo> admin;	/* admin requirements, if any */
ds@579
    92
};
ds@579
    93
dvander@4112
    94
typedef ke::InlineList<CmdHook> CmdHookList;
dvander@4112
    95
ds@579
    96
struct ConCmdInfo
ds@579
    97
{
ds@579
    98
	ConCmdInfo()
ds@579
    99
	{
ds@579
   100
		sourceMod = false;
ds@579
   101
		pCmd = NULL;
dvander@4112
   102
		eflags = 0;
ds@579
   103
	}
ds@579
   104
	bool sourceMod;					/**< Determines whether or not concmd was created by a SourceMod plugin */
ds@579
   105
	ConCommand *pCmd;				/**< Pointer to the command itself */
dvander@4112
   106
	CmdHookList hooks;				/**< Hook list */
dvander@4112
   107
	FlagBits eflags;				/**< Effective admin flags */
ds@579
   108
};
ds@579
   109
dvander@3302
   110
typedef List<ConCmdInfo *> ConCmdList;
dvander@3302
   111
ds@579
   112
class ConCmdManager :
ds@579
   113
	public SMGlobalClass,
ds@579
   114
	public IRootConsoleCommand,
dvander@1584
   115
	public IPluginsListener,
dvander@1584
   116
	public IConCommandTracker
ds@579
   117
{
skamonkey@3899
   118
#if SOURCE_ENGINE == SE_DOTA
skamonkey@3942
   119
	friend void CommandCallback(const CCommandContext &context, const CCommand &command);
skamonkey@3899
   120
#elif SOURCE_ENGINE >= SE_ORANGEBOX
faluco@1541
   121
	friend void CommandCallback(const CCommand &command);
faluco@1541
   122
#else
ds@579
   123
	friend void CommandCallback();
faluco@1541
   124
#endif
ds@579
   125
public:
ds@579
   126
	ConCmdManager();
ds@579
   127
	~ConCmdManager();
ds@579
   128
public: //SMGlobalClass
ds@579
   129
	void OnSourceModAllInitialized();
ds@579
   130
	void OnSourceModShutdown();
ds@579
   131
public: //IPluginsListener
ds@579
   132
	void OnPluginDestroyed(IPlugin *plugin);
ds@579
   133
public: //IRootConsoleCommand
faluco@1541
   134
	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
dvander@1584
   135
public: //IConCommandTracker
dvander@1584
   136
	void OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe);
ds@579
   137
public:
ds@579
   138
	bool AddServerCommand(IPluginFunction *pFunction, const char *name, const char *description, int flags);
ds@579
   139
	bool AddAdminCommand(IPluginFunction *pFunction, 
ds@579
   140
						 const char *name, 
ds@579
   141
						 const char *group,
ds@579
   142
						 int adminflags,
ds@579
   143
						 const char *description, 
ds@579
   144
						 int flags);
dvander@1335
   145
	ResultType DispatchClientCommand(int client, const char *cmd, int args, ResultType type);
dvander@1435
   146
	void UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits, bool remove);
dvander@893
   147
	bool LookForSourceModCommand(const char *cmd);
dvander@1349
   148
	bool LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags);
skamonkey@3389
   149
	bool CheckClientCommandAccess(int client, const char *cmd, FlagBits flags);
skamonkey@3389
   150
	bool CheckAdminCommandAccess(AdminId adm, const char *cmd, FlagBits flags);
ds@579
   151
private:
faluco@1541
   152
	void InternalDispatch(const CCommand &command);
ds@579
   153
	ResultType RunAdminCommand(ConCmdInfo *pInfo, int client, int args);
ds@579
   154
	ConCmdInfo *AddOrFindCommand(const char *name, const char *description, int flags);
ds@579
   155
	void SetCommandClient(int client);
ds@579
   156
	void AddToCmdList(ConCmdInfo *info);
dvander@1584
   157
	void RemoveConCmd(ConCmdInfo *info, const char *cmd, bool is_read_safe, bool untrack);
ds@579
   158
	bool CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin);
dvander@3302
   159
dvander@3302
   160
	// Case insensitive
dvander@3302
   161
	ConCmdList::iterator FindInList(const char *name);
dvander@3302
   162
dvander@3302
   163
	// Case sensitive
dvander@3302
   164
	ConCmdInfo *FindInTrie(const char *name);
dvander@769
   165
public:
dvander@769
   166
	inline int GetCommandClient()
dvander@769
   167
	{
dvander@769
   168
		return m_CmdClient;
dvander@769
   169
	}
dvander@1119
   170
	inline const List<ConCmdInfo *> & GetCommandList()
dvander@1119
   171
	{
dvander@1119
   172
		return m_CmdList;
dvander@1119
   173
	}
ds@579
   174
private:
dvander@4113
   175
	typedef StringHashMap<ke::Ref<CommandGroup> > GroupMap;
dvander@4113
   176
dvander@4076
   177
	StringHashMap<ConCmdInfo *> m_Cmds; /* command lookup */
dvander@4113
   178
	GroupMap m_CmdGrps;				/* command group map */
dvander@3302
   179
	ConCmdList m_CmdList;			/* command list */
ds@579
   180
	int m_CmdClient;				/* current client */
ds@579
   181
};
ds@579
   182
ds@579
   183
extern ConCmdManager g_ConCmds;
ds@579
   184
ds@579
   185
#endif // _INCLUDE_SOURCEMOD_CONCMDMANAGER_H_
dvander@2806
   186