2d241316c7
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
199 lines
6.1 KiB
C++
199 lines
6.1 KiB
C++
/**
|
|
* vim: set ts=4 sw=4 tw=99 noet :
|
|
* =============================================================================
|
|
* 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_
|
|
|
|
#include <list>
|
|
#include <memory>
|
|
|
|
#include <am-inlinelist.h>
|
|
#include <am-refcounting.h>
|
|
#include <am-utility.h>
|
|
#include <sm_stringhashmap.h>
|
|
|
|
#include "sm_globals.h"
|
|
#include "sourcemm_api.h"
|
|
#include <IForwardSys.h>
|
|
#include <sh_list.h>
|
|
#include <sh_string.h>
|
|
#include <IRootConsoleMenu.h>
|
|
#include <IAdminSystem.h>
|
|
#include "concmd_cleaner.h"
|
|
#include "GameHooks.h"
|
|
#include <sm_namehashset.h>
|
|
|
|
using namespace SourceHook;
|
|
|
|
struct CmdHook;
|
|
struct ConCmdInfo;
|
|
|
|
struct CommandGroup : public ke::Refcounted<CommandGroup>
|
|
{
|
|
std::list<CmdHook *> hooks;
|
|
};
|
|
|
|
struct AdminCmdInfo
|
|
{
|
|
AdminCmdInfo(const ke::RefPtr<CommandGroup> &group, FlagBits flags)
|
|
: group(group),
|
|
flags(flags),
|
|
eflags(0)
|
|
{
|
|
}
|
|
ke::RefPtr<CommandGroup> group;
|
|
FlagBits flags; /* default flags */
|
|
FlagBits eflags; /* effective flags */
|
|
};
|
|
|
|
struct CmdHook : public ke::InlineListNode<CmdHook>
|
|
{
|
|
enum Type {
|
|
Server,
|
|
Client
|
|
};
|
|
|
|
CmdHook(Type type, ConCmdInfo *cmd, IPluginFunction *fun, const char *description, IPlugin *plugin)
|
|
: type(type),
|
|
info(cmd),
|
|
pf(fun),
|
|
plugin(plugin),
|
|
helptext(description)
|
|
{
|
|
}
|
|
|
|
Type type;
|
|
ConCmdInfo *info;
|
|
IPluginFunction *pf; /* function hook */
|
|
IPlugin *plugin; /* owning plugin */
|
|
std::string helptext; /* help text */
|
|
std::unique_ptr<AdminCmdInfo> admin; /* admin requirements, if any */
|
|
};
|
|
|
|
typedef ke::InlineList<CmdHook> CmdHookList;
|
|
|
|
struct ConCmdInfo
|
|
{
|
|
ConCmdInfo()
|
|
{
|
|
pPlugin = nullptr;
|
|
sourceMod = false;
|
|
pCmd = nullptr;
|
|
eflags = 0;
|
|
}
|
|
|
|
bool sourceMod; /**< Determines whether or not concmd was created by a SourceMod plugin */
|
|
ConCommand *pCmd; /**< Pointer to the command itself */
|
|
CmdHookList hooks; /**< Hook list */
|
|
FlagBits eflags; /**< Effective admin flags */
|
|
ke::RefPtr<CommandHook> sh_hook; /**< SourceHook hook, if any. */
|
|
IPlugin *pPlugin; /**< Owning plugin handle. */
|
|
|
|
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();
|
|
}
|
|
};
|
|
};
|
|
|
|
typedef List<ConCmdInfo *> ConCmdList;
|
|
|
|
class ConCmdManager :
|
|
public SMGlobalClass,
|
|
public IRootConsoleCommand,
|
|
public IPluginsListener,
|
|
public IConCommandTracker
|
|
{
|
|
friend void CommandCallback(DISPATCH_ARGS);
|
|
public:
|
|
ConCmdManager();
|
|
~ConCmdManager();
|
|
public: //SMGlobalClass
|
|
void OnSourceModAllInitialized();
|
|
void OnSourceModShutdown();
|
|
public: //IPluginsListener
|
|
void OnPluginDestroyed(IPlugin *plugin);
|
|
public: //IRootConsoleCommand
|
|
void OnRootConsoleCommand(const char *cmdname, const ICommandArgs *command) override;
|
|
public: //IConCommandTracker
|
|
void OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name) override;
|
|
public:
|
|
bool AddServerCommand(IPluginFunction *pFunction, const char *name, const char *description, int flags, IPlugin *pPlugin);
|
|
bool AddAdminCommand(IPluginFunction *pFunction,
|
|
const char *name,
|
|
const char *group,
|
|
int adminflags,
|
|
const char *description,
|
|
int flags,
|
|
IPlugin *pPlugin);
|
|
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:
|
|
bool InternalDispatch(int client, const ICommandArgs *args);
|
|
ResultType RunAdminCommand(ConCmdInfo *pInfo, int client, int args);
|
|
ConCmdInfo *AddOrFindCommand(const char *name, const char *description, int flags, IPlugin *pPlugin);
|
|
void AddToCmdList(ConCmdInfo *info);
|
|
void RemoveConCmd(ConCmdInfo *info, const char *cmd, bool untrack);
|
|
bool CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin);
|
|
ConCmdInfo *FindInTrie(const char *name);
|
|
public:
|
|
inline const List<ConCmdInfo *> & GetCommandList()
|
|
{
|
|
return m_CmdList;
|
|
}
|
|
private:
|
|
typedef StringHashMap<ke::RefPtr<CommandGroup> > GroupMap;
|
|
|
|
NameHashSet<ConCmdInfo *, ConCmdInfo::ConCmdPolicy> m_Cmds; /* command lookup */
|
|
GroupMap m_CmdGrps; /* command group map */
|
|
ConCmdList m_CmdList; /* command list */
|
|
};
|
|
|
|
extern ConCmdManager g_ConCmds;
|
|
|
|
#endif // _INCLUDE_SOURCEMOD_CONCMDMANAGER_H_
|
|
|