2008-03-30 09:00:22 +02:00
|
|
|
/**
|
2013-08-25 21:18:25 +02:00
|
|
|
* vim: set ts=4 sw=4 tw=99 noet :
|
2008-03-30 09:00:22 +02:00
|
|
|
* =============================================================================
|
|
|
|
* SourceMod
|
|
|
|
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
|
|
|
* =============================================================================
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License, version 3.0, as published by the
|
|
|
|
* Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* As a special exception, AlliedModders LLC gives you permission to link the
|
|
|
|
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
|
|
|
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
|
|
|
* by the Valve Corporation. You must obey the GNU General Public License in
|
|
|
|
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
|
|
|
* this exception to all derivative works. AlliedModders LLC defines further
|
|
|
|
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
|
|
|
* or <http://www.sourcemod.net/license.php>.
|
|
|
|
*
|
|
|
|
* Version: $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE_SOURCEMOD_CONCMDMANAGER_H_
|
|
|
|
#define _INCLUDE_SOURCEMOD_CONCMDMANAGER_H_
|
|
|
|
|
2020-05-31 20:59:54 +02:00
|
|
|
#include <list>
|
2020-05-17 08:15:32 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2020-05-31 20:59:54 +02:00
|
|
|
#include <am-inlinelist.h>
|
|
|
|
#include <am-refcounting.h>
|
|
|
|
#include <am-utility.h>
|
|
|
|
#include <sm_stringhashmap.h>
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
#include "sm_globals.h"
|
|
|
|
#include "sourcemm_api.h"
|
2013-10-09 14:43:08 +02:00
|
|
|
#include <IForwardSys.h>
|
2008-03-30 09:00:22 +02:00
|
|
|
#include <sh_list.h>
|
|
|
|
#include <sh_string.h>
|
|
|
|
#include <IRootConsoleMenu.h>
|
|
|
|
#include <IAdminSystem.h>
|
|
|
|
#include "concmd_cleaner.h"
|
2015-09-06 21:35:04 +02:00
|
|
|
#include "GameHooks.h"
|
Make all command lookups case-insensitive (#1542)
SM internally maintained both a case-sensitive and a case-insensitive
lookup method for commands, where the case-sensitive hashmap was used as
a fast path, and case-insensitive iteration over a list used as the slow
path if a command was not found in the hashmap. But only command
dispatch handling used this dual path approach, chat triggers for
example only did a loopup in the hashmap.
Over the years Valve has made more and more of the command dispatch
logic case-insensitive to the point where all console commands are now
case-insensitive, so maintaining case sensitivity when using chat
triggers does not make a lot of sense. There are somewhat popular
plugins that attempt to "correct" this behaviour - but at least one is
having issues after the previous case-sensitivity fixes for commands -
see #1480.
We still have to keep the list around for the sorted help use case and
command iteration, but this PR changes the hashmap to use a
case-insensitive hashing policy (as previously done for convars, and
more recently for game command lookup) and changes all by-name lookup to
exclusively use the hashmap (as there is no need to fall back to the
list any more).
Tested a bunch in TF2, I don't know of any games that still have a
case-sensitive command dispatch pipeline to test. I think the worst case
would be that we'd accept a chat command in the "wrong" case then fail
to execute the underlying command. If that turns out to be an issue in
practice, we should be able to fix it easily enough by replacing the
command name in the buffer with the correct casing of the command we
looked up.
Also fixed a couple of very minor Lookup vs. Key issues in NameHashSet
(noted in #1529) that were being masked due to CharsAndLength's
converting constructor. I tried to make the constructor explicit to
avoid this happening in the future but HashTable's add function relies
on being able to do an implicit conversion so that wasn't possible. We
might want to just rely on the implicit conversion up here as well, but
it doesn't really matter either way.
Fixes #1480, #1529
2021-07-18 20:05:06 +02:00
|
|
|
#include <sm_namehashset.h>
|
2008-03-30 09:00:22 +02:00
|
|
|
|
|
|
|
using namespace SourceHook;
|
|
|
|
|
2013-09-01 04:50:15 +02:00
|
|
|
struct CmdHook;
|
|
|
|
struct ConCmdInfo;
|
|
|
|
|
|
|
|
struct CommandGroup : public ke::Refcounted<CommandGroup>
|
|
|
|
{
|
2020-05-31 20:59:54 +02:00
|
|
|
std::list<CmdHook *> hooks;
|
2013-09-01 04:50:15 +02:00
|
|
|
};
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
struct AdminCmdInfo
|
|
|
|
{
|
2015-11-08 22:14:57 +01:00
|
|
|
AdminCmdInfo(const ke::RefPtr<CommandGroup> &group, FlagBits flags)
|
2013-09-01 04:50:15 +02:00
|
|
|
: group(group),
|
|
|
|
flags(flags),
|
|
|
|
eflags(0)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
|
|
|
}
|
2015-11-08 22:14:57 +01:00
|
|
|
ke::RefPtr<CommandGroup> group;
|
2008-03-30 09:00:22 +02:00
|
|
|
FlagBits flags; /* default flags */
|
|
|
|
FlagBits eflags; /* effective flags */
|
|
|
|
};
|
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
struct CmdHook : public ke::InlineListNode<CmdHook>
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2013-08-31 20:51:23 +02:00
|
|
|
enum Type {
|
|
|
|
Server,
|
|
|
|
Client
|
|
|
|
};
|
|
|
|
|
2021-03-11 00:21:57 +01:00
|
|
|
CmdHook(Type type, ConCmdInfo *cmd, IPluginFunction *fun, const char *description, IPlugin *plugin)
|
2013-08-31 20:51:23 +02:00
|
|
|
: type(type),
|
|
|
|
info(cmd),
|
|
|
|
pf(fun),
|
2021-03-11 00:21:57 +01:00
|
|
|
plugin(plugin),
|
2013-08-31 20:51:23 +02:00
|
|
|
helptext(description)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
|
|
|
}
|
2013-08-31 20:51:23 +02:00
|
|
|
|
|
|
|
Type type;
|
|
|
|
ConCmdInfo *info;
|
|
|
|
IPluginFunction *pf; /* function hook */
|
2021-03-11 00:21:57 +01:00
|
|
|
IPlugin *plugin; /* owning plugin */
|
2020-05-20 21:35:26 +02:00
|
|
|
std::string helptext; /* help text */
|
2020-05-17 08:15:32 +02:00
|
|
|
std::unique_ptr<AdminCmdInfo> admin; /* admin requirements, if any */
|
2008-03-30 09:00:22 +02:00
|
|
|
};
|
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
typedef ke::InlineList<CmdHook> CmdHookList;
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
struct ConCmdInfo
|
|
|
|
{
|
|
|
|
ConCmdInfo()
|
|
|
|
{
|
2018-07-10 23:39:31 +02:00
|
|
|
pPlugin = nullptr;
|
2008-03-30 09:00:22 +02:00
|
|
|
sourceMod = false;
|
2018-07-10 23:39:31 +02:00
|
|
|
pCmd = nullptr;
|
2013-08-31 20:51:23 +02:00
|
|
|
eflags = 0;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
Make all command lookups case-insensitive (#1542)
SM internally maintained both a case-sensitive and a case-insensitive
lookup method for commands, where the case-sensitive hashmap was used as
a fast path, and case-insensitive iteration over a list used as the slow
path if a command was not found in the hashmap. But only command
dispatch handling used this dual path approach, chat triggers for
example only did a loopup in the hashmap.
Over the years Valve has made more and more of the command dispatch
logic case-insensitive to the point where all console commands are now
case-insensitive, so maintaining case sensitivity when using chat
triggers does not make a lot of sense. There are somewhat popular
plugins that attempt to "correct" this behaviour - but at least one is
having issues after the previous case-sensitivity fixes for commands -
see #1480.
We still have to keep the list around for the sorted help use case and
command iteration, but this PR changes the hashmap to use a
case-insensitive hashing policy (as previously done for convars, and
more recently for game command lookup) and changes all by-name lookup to
exclusively use the hashmap (as there is no need to fall back to the
list any more).
Tested a bunch in TF2, I don't know of any games that still have a
case-sensitive command dispatch pipeline to test. I think the worst case
would be that we'd accept a chat command in the "wrong" case then fail
to execute the underlying command. If that turns out to be an issue in
practice, we should be able to fix it easily enough by replacing the
command name in the buffer with the correct casing of the command we
looked up.
Also fixed a couple of very minor Lookup vs. Key issues in NameHashSet
(noted in #1529) that were being masked due to CharsAndLength's
converting constructor. I tried to make the constructor explicit to
avoid this happening in the future but HashTable's add function relies
on being able to do an implicit conversion so that wasn't possible. We
might want to just rely on the implicit conversion up here as well, but
it doesn't really matter either way.
Fixes #1480, #1529
2021-07-18 20:05:06 +02:00
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
bool sourceMod; /**< Determines whether or not concmd was created by a SourceMod plugin */
|
|
|
|
ConCommand *pCmd; /**< Pointer to the command itself */
|
2013-08-31 20:51:23 +02:00
|
|
|
CmdHookList hooks; /**< Hook list */
|
|
|
|
FlagBits eflags; /**< Effective admin flags */
|
2015-11-08 22:14:57 +01:00
|
|
|
ke::RefPtr<CommandHook> sh_hook; /**< SourceHook hook, if any. */
|
2018-07-10 23:39:31 +02:00
|
|
|
IPlugin *pPlugin; /**< Owning plugin handle. */
|
Make all command lookups case-insensitive (#1542)
SM internally maintained both a case-sensitive and a case-insensitive
lookup method for commands, where the case-sensitive hashmap was used as
a fast path, and case-insensitive iteration over a list used as the slow
path if a command was not found in the hashmap. But only command
dispatch handling used this dual path approach, chat triggers for
example only did a loopup in the hashmap.
Over the years Valve has made more and more of the command dispatch
logic case-insensitive to the point where all console commands are now
case-insensitive, so maintaining case sensitivity when using chat
triggers does not make a lot of sense. There are somewhat popular
plugins that attempt to "correct" this behaviour - but at least one is
having issues after the previous case-sensitivity fixes for commands -
see #1480.
We still have to keep the list around for the sorted help use case and
command iteration, but this PR changes the hashmap to use a
case-insensitive hashing policy (as previously done for convars, and
more recently for game command lookup) and changes all by-name lookup to
exclusively use the hashmap (as there is no need to fall back to the
list any more).
Tested a bunch in TF2, I don't know of any games that still have a
case-sensitive command dispatch pipeline to test. I think the worst case
would be that we'd accept a chat command in the "wrong" case then fail
to execute the underlying command. If that turns out to be an issue in
practice, we should be able to fix it easily enough by replacing the
command name in the buffer with the correct casing of the command we
looked up.
Also fixed a couple of very minor Lookup vs. Key issues in NameHashSet
(noted in #1529) that were being masked due to CharsAndLength's
converting constructor. I tried to make the constructor explicit to
avoid this happening in the future but HashTable's add function relies
on being able to do an implicit conversion so that wasn't possible. We
might want to just rely on the implicit conversion up here as well, but
it doesn't really matter either way.
Fixes #1480, #1529
2021-07-18 20:05:06 +02:00
|
|
|
|
|
|
|
struct ConCmdPolicy
|
|
|
|
{
|
|
|
|
static inline bool matches(const char *name, ConCmdInfo *info)
|
|
|
|
{
|
|
|
|
const char *conCmdChars = info->pCmd->GetName();
|
|
|
|
|
|
|
|
std::string concmdName = ke::Lowercase(conCmdChars);
|
|
|
|
std::string input = ke::Lowercase(name);
|
|
|
|
|
|
|
|
return concmdName == input;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t hash(const detail::CharsAndLength &key)
|
|
|
|
{
|
|
|
|
std::string lower = ke::Lowercase(key.c_str());
|
|
|
|
return detail::CharsAndLength(lower.c_str()).hash();
|
|
|
|
}
|
|
|
|
};
|
2008-03-30 09:00:22 +02:00
|
|
|
};
|
|
|
|
|
2011-06-20 19:30:14 +02:00
|
|
|
typedef List<ConCmdInfo *> ConCmdList;
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
class ConCmdManager :
|
|
|
|
public SMGlobalClass,
|
|
|
|
public IRootConsoleCommand,
|
|
|
|
public IPluginsListener,
|
|
|
|
public IConCommandTracker
|
|
|
|
{
|
2015-09-06 21:35:04 +02:00
|
|
|
friend void CommandCallback(DISPATCH_ARGS);
|
2008-03-30 09:00:22 +02:00
|
|
|
public:
|
|
|
|
ConCmdManager();
|
|
|
|
~ConCmdManager();
|
|
|
|
public: //SMGlobalClass
|
|
|
|
void OnSourceModAllInitialized();
|
|
|
|
void OnSourceModShutdown();
|
|
|
|
public: //IPluginsListener
|
|
|
|
void OnPluginDestroyed(IPlugin *plugin);
|
|
|
|
public: //IRootConsoleCommand
|
2015-08-28 04:59:04 +02:00
|
|
|
void OnRootConsoleCommand(const char *cmdname, const ICommandArgs *command) override;
|
2008-03-30 09:00:22 +02:00
|
|
|
public: //IConCommandTracker
|
2015-09-09 04:47:22 +02:00
|
|
|
void OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name) override;
|
2008-03-30 09:00:22 +02:00
|
|
|
public:
|
2018-07-10 23:39:31 +02:00
|
|
|
bool AddServerCommand(IPluginFunction *pFunction, const char *name, const char *description, int flags, IPlugin *pPlugin);
|
2008-03-30 09:00:22 +02:00
|
|
|
bool AddAdminCommand(IPluginFunction *pFunction,
|
|
|
|
const char *name,
|
|
|
|
const char *group,
|
|
|
|
int adminflags,
|
|
|
|
const char *description,
|
2018-07-10 23:39:31 +02:00
|
|
|
int flags,
|
|
|
|
IPlugin *pPlugin);
|
2008-03-30 09:00:22 +02:00
|
|
|
ResultType DispatchClientCommand(int client, const char *cmd, int args, ResultType type);
|
|
|
|
void UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits, bool remove);
|
|
|
|
bool LookForSourceModCommand(const char *cmd);
|
|
|
|
bool LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags);
|
|
|
|
private:
|
2015-09-06 23:55:51 +02:00
|
|
|
bool InternalDispatch(int client, const ICommandArgs *args);
|
2008-03-30 09:00:22 +02:00
|
|
|
ResultType RunAdminCommand(ConCmdInfo *pInfo, int client, int args);
|
2018-07-10 23:39:31 +02:00
|
|
|
ConCmdInfo *AddOrFindCommand(const char *name, const char *description, int flags, IPlugin *pPlugin);
|
2008-03-30 09:00:22 +02:00
|
|
|
void AddToCmdList(ConCmdInfo *info);
|
2015-09-09 04:47:22 +02:00
|
|
|
void RemoveConCmd(ConCmdInfo *info, const char *cmd, bool untrack);
|
2008-03-30 09:00:22 +02:00
|
|
|
bool CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin);
|
2011-06-20 19:30:14 +02:00
|
|
|
ConCmdInfo *FindInTrie(const char *name);
|
2008-03-30 09:00:22 +02:00
|
|
|
public:
|
|
|
|
inline const List<ConCmdInfo *> & GetCommandList()
|
|
|
|
{
|
|
|
|
return m_CmdList;
|
|
|
|
}
|
|
|
|
private:
|
2015-11-08 22:14:57 +01:00
|
|
|
typedef StringHashMap<ke::RefPtr<CommandGroup> > GroupMap;
|
2013-09-01 04:50:15 +02:00
|
|
|
|
Make all command lookups case-insensitive (#1542)
SM internally maintained both a case-sensitive and a case-insensitive
lookup method for commands, where the case-sensitive hashmap was used as
a fast path, and case-insensitive iteration over a list used as the slow
path if a command was not found in the hashmap. But only command
dispatch handling used this dual path approach, chat triggers for
example only did a loopup in the hashmap.
Over the years Valve has made more and more of the command dispatch
logic case-insensitive to the point where all console commands are now
case-insensitive, so maintaining case sensitivity when using chat
triggers does not make a lot of sense. There are somewhat popular
plugins that attempt to "correct" this behaviour - but at least one is
having issues after the previous case-sensitivity fixes for commands -
see #1480.
We still have to keep the list around for the sorted help use case and
command iteration, but this PR changes the hashmap to use a
case-insensitive hashing policy (as previously done for convars, and
more recently for game command lookup) and changes all by-name lookup to
exclusively use the hashmap (as there is no need to fall back to the
list any more).
Tested a bunch in TF2, I don't know of any games that still have a
case-sensitive command dispatch pipeline to test. I think the worst case
would be that we'd accept a chat command in the "wrong" case then fail
to execute the underlying command. If that turns out to be an issue in
practice, we should be able to fix it easily enough by replacing the
command name in the buffer with the correct casing of the command we
looked up.
Also fixed a couple of very minor Lookup vs. Key issues in NameHashSet
(noted in #1529) that were being masked due to CharsAndLength's
converting constructor. I tried to make the constructor explicit to
avoid this happening in the future but HashTable's add function relies
on being able to do an implicit conversion so that wasn't possible. We
might want to just rely on the implicit conversion up here as well, but
it doesn't really matter either way.
Fixes #1480, #1529
2021-07-18 20:05:06 +02:00
|
|
|
NameHashSet<ConCmdInfo *, ConCmdInfo::ConCmdPolicy> m_Cmds; /* command lookup */
|
2013-09-01 04:50:15 +02:00
|
|
|
GroupMap m_CmdGrps; /* command group map */
|
2011-06-20 19:30:14 +02:00
|
|
|
ConCmdList m_CmdList; /* command list */
|
2008-03-30 09:00:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern ConCmdManager g_ConCmds;
|
|
|
|
|
|
|
|
#endif // _INCLUDE_SOURCEMOD_CONCMDMANAGER_H_
|
2009-09-26 23:12:23 +02:00
|
|
|
|