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$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ConCmdManager.h"
|
|
|
|
#include "sm_stringutil.h"
|
|
|
|
#include "PlayerManager.h"
|
|
|
|
#include "HalfLife2.h"
|
|
|
|
#include "ChatTriggers.h"
|
2010-05-15 04:43:53 +02:00
|
|
|
#include "logic_bridge.h"
|
2015-08-28 20:08:25 +02:00
|
|
|
#include "sourcemod.h"
|
2015-09-06 21:35:04 +02:00
|
|
|
#include "provider.h"
|
|
|
|
#include "command_args.h"
|
2015-08-31 06:27:32 +02:00
|
|
|
#include <bridge/include/IScriptManager.h>
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2013-09-01 04:50:15 +02:00
|
|
|
using namespace ke;
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
ConCmdManager g_ConCmds;
|
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
typedef ke::LinkedList<CmdHook *> PluginHookList;
|
|
|
|
void RegisterInPlugin(CmdHook *hook);
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2013-09-01 04:50:15 +02:00
|
|
|
ConCmdManager::ConCmdManager()
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ConCmdManager::~ConCmdManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConCmdManager::OnSourceModAllInitialized()
|
|
|
|
{
|
2013-03-29 19:37:29 +01:00
|
|
|
scripts->AddPluginsListener(this);
|
2015-08-28 05:46:36 +02:00
|
|
|
rootmenu->AddRootConsoleCommand3("cmds", "List console commands", this);
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConCmdManager::OnSourceModShutdown()
|
|
|
|
{
|
2013-03-29 19:37:29 +01:00
|
|
|
scripts->RemovePluginsListener(this);
|
2015-08-28 05:46:36 +02:00
|
|
|
rootmenu->RemoveRootConsoleCommand("cmds", this);
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
2015-09-09 04:47:22 +02:00
|
|
|
void ConCmdManager::OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
|
|
|
/* Whoa, first get its information struct */
|
|
|
|
ConCmdInfo *pInfo;
|
|
|
|
|
2013-08-25 21:18:25 +02:00
|
|
|
if (!m_Cmds.retrieve(name, &pInfo))
|
2008-03-30 09:00:22 +02:00
|
|
|
return;
|
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
CmdHookList::iterator iter = pInfo->hooks.begin();
|
|
|
|
while (iter != pInfo->hooks.end())
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2013-08-31 20:51:23 +02:00
|
|
|
CmdHook *hook = *iter;
|
|
|
|
|
|
|
|
IPluginContext *pContext = hook->pf->GetParentContext();
|
2013-03-29 19:37:29 +01:00
|
|
|
IPlugin *pPlugin = scripts->FindPluginByContext(pContext->GetContext());
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
// The list is guaranteed to exist.
|
|
|
|
PluginHookList *list;
|
|
|
|
pPlugin->GetProperty("CommandList", (void **)&list, false);
|
|
|
|
for (PluginHookList::iterator hiter = list->begin(); hiter != list->end(); hiter++)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2013-08-31 20:51:23 +02:00
|
|
|
if (*hiter == hook)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2013-08-31 20:51:23 +02:00
|
|
|
list->erase(hiter);
|
|
|
|
break;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 04:50:15 +02:00
|
|
|
if (hook->admin)
|
|
|
|
hook->admin->group->hooks.remove(hook);
|
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
iter = pInfo->hooks.erase(iter);
|
|
|
|
delete hook;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
2015-09-09 04:47:22 +02:00
|
|
|
RemoveConCmd(pInfo, name, false);
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConCmdManager::OnPluginDestroyed(IPlugin *plugin)
|
|
|
|
{
|
2013-08-31 20:51:23 +02:00
|
|
|
PluginHookList *pList;
|
|
|
|
if (!plugin->GetProperty("CommandList", (void **)&pList, true))
|
|
|
|
return;
|
|
|
|
|
|
|
|
PluginHookList::iterator iter = pList->begin();
|
|
|
|
while (iter != pList->end())
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2013-08-31 20:51:23 +02:00
|
|
|
CmdHook *hook = *iter;
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
hook->info->hooks.remove(hook);
|
2013-09-01 04:50:15 +02:00
|
|
|
if (hook->admin)
|
|
|
|
hook->admin->group->hooks.remove(hook);
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
if (hook->info->hooks.empty())
|
2015-09-09 04:47:22 +02:00
|
|
|
RemoveConCmd(hook->info, hook->info->pCmd->GetName(), true);
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
iter = pList->erase(iter);
|
|
|
|
delete hook;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
2013-08-31 20:51:23 +02:00
|
|
|
|
|
|
|
delete pList;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
2013-08-31 20:51:23 +02:00
|
|
|
|
2015-09-06 21:35:04 +02:00
|
|
|
void CommandCallback(DISPATCH_ARGS)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2015-09-06 21:35:04 +02:00
|
|
|
DISPATCH_PROLOGUE;
|
|
|
|
EngineArgs args(command);
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2015-09-06 21:35:04 +02:00
|
|
|
AutoEnterCommand autoEnterCommand(&args);
|
2015-09-12 09:26:47 +02:00
|
|
|
g_ConCmds.InternalDispatch(sCoreProviderImpl.CommandClient(), &args);
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
2011-06-20 19:30:14 +02:00
|
|
|
ConCmdInfo *ConCmdManager::FindInTrie(const char *name)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
|
|
|
ConCmdInfo *pInfo;
|
2013-08-25 21:18:25 +02:00
|
|
|
if (!m_Cmds.retrieve(name, &pInfo))
|
2011-06-20 19:30:14 +02:00
|
|
|
return NULL;
|
|
|
|
return pInfo;
|
|
|
|
}
|
2009-02-01 08:13:17 +01:00
|
|
|
|
2011-06-20 19:30:14 +02:00
|
|
|
ConCmdList::iterator ConCmdManager::FindInList(const char *cmd)
|
|
|
|
{
|
|
|
|
List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
|
|
|
|
|
|
|
|
while (iter != m_CmdList.end())
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2011-06-20 19:30:14 +02:00
|
|
|
if (strcasecmp((*iter)->pCmd->GetName(), cmd) == 0)
|
|
|
|
break;
|
|
|
|
iter++;
|
|
|
|
}
|
2009-02-01 08:13:17 +01:00
|
|
|
|
2011-06-20 19:30:14 +02:00
|
|
|
return iter;
|
|
|
|
}
|
2009-02-01 08:13:17 +01:00
|
|
|
|
2011-06-20 19:30:14 +02:00
|
|
|
ResultType ConCmdManager::DispatchClientCommand(int client, const char *cmd, int args, ResultType type)
|
|
|
|
{
|
|
|
|
ConCmdInfo *pInfo;
|
|
|
|
|
|
|
|
if ((pInfo = FindInTrie(cmd)) == NULL)
|
|
|
|
{
|
|
|
|
ConCmdList::iterator item = FindInList(cmd);
|
|
|
|
if (item == m_CmdList.end())
|
2009-02-01 08:13:17 +01:00
|
|
|
return type;
|
2011-06-20 19:30:14 +02:00
|
|
|
pInfo = *item;
|
2009-02-01 08:13:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cell_t result = type;
|
2013-08-31 20:51:23 +02:00
|
|
|
for (CmdHookList::iterator iter = pInfo->hooks.begin(); iter != pInfo->hooks.end(); iter++)
|
2009-02-01 08:13:17 +01:00
|
|
|
{
|
2013-08-31 20:51:23 +02:00
|
|
|
CmdHook *hook = *iter;
|
|
|
|
|
|
|
|
if (hook->type == CmdHook::Server || !hook->pf->IsRunnable())
|
2009-02-01 08:13:17 +01:00
|
|
|
continue;
|
2013-08-31 20:51:23 +02:00
|
|
|
|
|
|
|
if (hook->admin && !CheckAccess(client, cmd, hook->admin))
|
2009-02-01 08:13:17 +01:00
|
|
|
{
|
|
|
|
if (result < Pl_Handled)
|
|
|
|
result = Pl_Handled;
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-31 20:51:23 +02:00
|
|
|
|
|
|
|
hook->pf->PushCell(client);
|
|
|
|
hook->pf->PushCell(args);
|
|
|
|
|
|
|
|
cell_t tempres = result;
|
Implement a new stack and error handling model for the SourcePawn VM.
This has three major changes to SourcePawn. First, the API now supports the concept of "exceptions". The exception state is a global property of an instance of the SourcePawn VM. Exceptions can be caught or suppressed. Many places in SourceMod have been updated to check exceptions instead of errors.
The new API obsoletes major parts of the embedder API - all but one method of invoking functions is obsoleted, and the debug interface has been scrapped. Extensions using the native API will not be affected, however, ThrowNativeError has been deprecated in favor of ReportError.
Second, the SourcePawn concept of a "stack" has been unified at the API level. A stack frame iterator now iterates over all SourcePawn invocations, rather than the topmost plugin. This makes error handling more consistent and removes another dependency on context-per-plugin.
Finally, the implementation of stack frames has been changed dramatically. Rather than maintain a complicated and expensive return pointer stack, we now rely on the implicit one provided by the CPU. The stack frame iterator now walks the JIT stack directly. This removes many unnecessary bookkeeping instructions from the generated code, in particular making the CALL instruction 40% faster.
These changes required some fair surgery to the JIT. Its error paths are now slightly more complicated, as they have to throw an exception rather than return an error code. In addition, any path that can throw an exception is now responsible for creating an "exit frame", which exists to tell the stack frame iterator about transitions from the JIT to the VM.
2015-02-27 09:32:44 +01:00
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
if (hook->pf->Execute(&tempres) == SP_ERROR_NONE)
|
2009-02-01 08:13:17 +01:00
|
|
|
{
|
|
|
|
if (tempres > result)
|
|
|
|
result = tempres;
|
|
|
|
if (result == Pl_Stop)
|
|
|
|
break;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
return (ResultType)result;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
2015-09-06 23:55:51 +02:00
|
|
|
bool ConCmdManager::InternalDispatch(int client, const ICommandArgs *args)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
|
|
|
if (client)
|
|
|
|
{
|
|
|
|
CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
|
|
|
|
if (!pPlayer || !pPlayer->IsConnected())
|
2015-09-06 21:59:19 +02:00
|
|
|
return false;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Note: Console commands will EITHER go through IServerGameDLL::ClientCommand,
|
|
|
|
* OR this dispatch. They will NEVER go through both.
|
|
|
|
* --
|
|
|
|
* Whether or not it goes through the callback is determined by FCVAR_GAMEDLL
|
|
|
|
*/
|
|
|
|
const char *cmd = g_HL2.CurrentCommandName();
|
|
|
|
|
2011-06-20 19:30:14 +02:00
|
|
|
ConCmdInfo *pInfo = FindInTrie(cmd);
|
|
|
|
if (pInfo == NULL)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2008-09-02 05:58:34 +02:00
|
|
|
/* Unfortunately, we now have to do a slow lookup because Valve made client commands
|
|
|
|
* case-insensitive. We can't even use our sortedness.
|
|
|
|
*/
|
|
|
|
if (client == 0 && !engine->IsDedicatedServer())
|
2015-09-06 21:59:19 +02:00
|
|
|
return false;
|
2011-06-20 19:30:14 +02:00
|
|
|
|
|
|
|
ConCmdList::iterator item = FindInList(cmd);
|
|
|
|
if (item == m_CmdList.end())
|
2015-09-06 21:59:19 +02:00
|
|
|
return false;
|
2011-06-20 19:30:14 +02:00
|
|
|
|
|
|
|
pInfo = *item;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a hack to prevent say triggers from firing on messages that were
|
|
|
|
* blocked because of flooding. We won't remove this, but the hack will get
|
|
|
|
* "nicer" when we expose explicit say hooks.
|
|
|
|
*/
|
|
|
|
if (g_ChatTriggers.WasFloodedMessage())
|
2015-09-06 21:59:19 +02:00
|
|
|
return false;
|
2008-03-30 09:00:22 +02:00
|
|
|
|
|
|
|
cell_t result = Pl_Continue;
|
2015-09-06 21:35:04 +02:00
|
|
|
int argc = args->ArgC() - 1;
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
// On a listen server, sometimes the server host's client index can be set
|
|
|
|
// as 0. So index 1 is passed to the command callback to correct this
|
|
|
|
// potential problem.
|
|
|
|
int realClient = (!engine->IsDedicatedServer() && client == 0)
|
|
|
|
? g_Players.ListenClient()
|
|
|
|
: client;
|
|
|
|
int dedicatedClient = engine->IsDedicatedServer() ? 0 : g_Players.ListenClient();
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
for (CmdHookList::iterator iter = pInfo->hooks.begin(); iter != pInfo->hooks.end(); iter++)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2013-08-31 20:51:23 +02:00
|
|
|
CmdHook *hook = *iter;
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
if (!hook->pf->IsRunnable())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (hook->type == CmdHook::Server)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2013-08-31 20:51:23 +02:00
|
|
|
// Don't execute unless we're in the server console.
|
|
|
|
if (realClient != dedicatedClient)
|
2008-03-30 09:00:22 +02:00
|
|
|
continue;
|
2013-08-31 20:51:23 +02:00
|
|
|
} else {
|
|
|
|
// Check admin rights if needed. realClient isn't needed since we
|
|
|
|
// should bypass admin checks if client == 0 anyway.
|
|
|
|
if (client && hook->admin && !CheckAccess(client, cmd, hook->admin))
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
|
|
|
if (result < Pl_Handled)
|
|
|
|
result = Pl_Handled;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
// Client hooks get a client argument.
|
|
|
|
hook->pf->PushCell(realClient);
|
|
|
|
}
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2015-09-06 21:35:04 +02:00
|
|
|
hook->pf->PushCell(argc);
|
2008-04-12 04:18:03 +02:00
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
cell_t tempres = result;
|
|
|
|
if (hook->pf->Execute(&tempres) == SP_ERROR_NONE)
|
|
|
|
{
|
|
|
|
if (tempres > result)
|
|
|
|
result = tempres;
|
|
|
|
if (result == Pl_Stop)
|
|
|
|
break;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result >= Pl_Handled)
|
2015-09-06 21:59:19 +02:00
|
|
|
return !pInfo->sourceMod;
|
|
|
|
|
|
|
|
return false;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ConCmdManager::CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin)
|
|
|
|
{
|
2014-05-29 19:31:12 +02:00
|
|
|
if (adminsys->CheckClientCommandAccess(client, cmd, pAdmin->eflags))
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-30 05:41:41 +01:00
|
|
|
CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
|
|
|
|
if (!pPlayer)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
/* If we got here, the command failed... */
|
|
|
|
char buffer[128];
|
2010-05-15 04:43:53 +02:00
|
|
|
if (!logicore.CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "No Access", &client))
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2018-03-19 21:12:51 +01:00
|
|
|
ke::SafeStrcpy(buffer, sizeof(buffer), "You do not have access to this command");
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int replyto = g_ChatTriggers.GetReplyTo();
|
|
|
|
if (replyto == SM_REPLY_CONSOLE)
|
|
|
|
{
|
|
|
|
char fullbuffer[192];
|
2015-09-06 06:47:33 +02:00
|
|
|
ke::SafeSprintf(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
|
2014-01-30 05:41:41 +01:00
|
|
|
pPlayer->PrintToConsole(fullbuffer);
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
else if (replyto == SM_REPLY_CHAT)
|
|
|
|
{
|
|
|
|
char fullbuffer[192];
|
2015-09-06 06:47:33 +02:00
|
|
|
ke::SafeSprintf(fullbuffer, sizeof(fullbuffer), "[SM] %s.", buffer);
|
2008-03-30 09:00:22 +02:00
|
|
|
g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConCmdManager::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
|
|
|
{
|
2018-07-10 23:39:31 +02:00
|
|
|
ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags, pPlugin);
|
2008-03-30 09:00:22 +02:00
|
|
|
|
|
|
|
if (!pInfo)
|
|
|
|
return false;
|
|
|
|
|
2013-09-01 04:50:15 +02:00
|
|
|
GroupMap::Insert i = m_CmdGrps.findForAdd(group);
|
|
|
|
if (!i.found())
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2013-09-01 04:50:15 +02:00
|
|
|
if (!m_CmdGrps.add(i, group))
|
|
|
|
return false;
|
2014-08-23 07:50:25 +02:00
|
|
|
i->value = new CommandGroup();
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
2015-11-08 22:14:57 +01:00
|
|
|
RefPtr<CommandGroup> cmdgroup = i->value;
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2013-09-01 04:50:15 +02:00
|
|
|
CmdHook *pHook = new CmdHook(CmdHook::Client, pInfo, pFunction, description);
|
|
|
|
pHook->admin = new AdminCmdInfo(cmdgroup, adminflags);
|
2008-03-30 09:00:22 +02:00
|
|
|
|
|
|
|
/* First get the command group override, if any */
|
2014-05-29 19:31:12 +02:00
|
|
|
bool override = adminsys->GetCommandOverride(group,
|
2008-03-30 09:00:22 +02:00
|
|
|
Override_CommandGroup,
|
2013-08-31 20:51:23 +02:00
|
|
|
&(pHook->admin->eflags));
|
2008-03-30 09:00:22 +02:00
|
|
|
|
|
|
|
/* Next get the command override, if any */
|
2014-05-29 19:31:12 +02:00
|
|
|
if (adminsys->GetCommandOverride(name,
|
2008-03-30 09:00:22 +02:00
|
|
|
Override_Command,
|
2013-08-31 20:51:23 +02:00
|
|
|
&(pHook->admin->eflags)))
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
|
|
|
override = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assign normal flags if there were no overrides */
|
|
|
|
if (!override)
|
2013-08-31 20:51:23 +02:00
|
|
|
pHook->admin->eflags = pHook->admin->flags;
|
|
|
|
pInfo->eflags = pHook->admin->eflags;
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2013-09-01 04:50:15 +02:00
|
|
|
cmdgroup->hooks.append(pHook);
|
2013-08-31 20:51:23 +02:00
|
|
|
pInfo->hooks.append(pHook);
|
|
|
|
RegisterInPlugin(pHook);
|
2008-03-30 09:00:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConCmdManager::AddServerCommand(IPluginFunction *pFunction,
|
|
|
|
const char *name,
|
|
|
|
const char *description,
|
2018-07-10 23:39:31 +02:00
|
|
|
int flags,
|
|
|
|
IPlugin *pPlugin)
|
2008-03-30 09:00:22 +02:00
|
|
|
|
|
|
|
{
|
2018-07-10 23:39:31 +02:00
|
|
|
ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags, pPlugin);
|
2008-03-30 09:00:22 +02:00
|
|
|
|
|
|
|
if (!pInfo)
|
|
|
|
return false;
|
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
CmdHook *pHook = new CmdHook(CmdHook::Server, pInfo, pFunction, description);
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
pInfo->hooks.append(pHook);
|
|
|
|
RegisterInPlugin(pHook);
|
|
|
|
return true;
|
|
|
|
}
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
void RegisterInPlugin(CmdHook *hook)
|
|
|
|
{
|
|
|
|
PluginHookList *pList;
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
IPlugin *pPlugin = scripts->FindPluginByContext(hook->pf->GetParentContext());
|
2008-03-30 09:00:22 +02:00
|
|
|
if (!pPlugin->GetProperty("CommandList", (void **)&pList))
|
|
|
|
{
|
2013-08-31 20:51:23 +02:00
|
|
|
pList = new PluginHookList();
|
2008-03-30 09:00:22 +02:00
|
|
|
pPlugin->SetProperty("CommandList", pList);
|
|
|
|
}
|
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
const char *orig = hook->info->pCmd->GetName();
|
2008-03-30 09:00:22 +02:00
|
|
|
|
|
|
|
/* Insert this into the help list, SORTED alphabetically. */
|
2013-08-31 20:51:23 +02:00
|
|
|
PluginHookList::iterator iter = pList->begin();
|
2008-03-30 09:00:22 +02:00
|
|
|
while (iter != pList->end())
|
|
|
|
{
|
2013-08-31 20:51:23 +02:00
|
|
|
const char *cmd = (*iter)->info->pCmd->GetName();
|
2008-03-30 09:00:22 +02:00
|
|
|
if (strcmp(orig, cmd) < 0)
|
|
|
|
{
|
2013-08-31 20:51:23 +02:00
|
|
|
pList->insertBefore(iter, hook);
|
|
|
|
return;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
pList->append(hook);
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConCmdManager::AddToCmdList(ConCmdInfo *info)
|
|
|
|
{
|
|
|
|
List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
|
|
|
|
ConCmdInfo *pInfo;
|
|
|
|
bool inserted = false;
|
|
|
|
const char *orig = NULL;
|
|
|
|
|
|
|
|
orig = info->pCmd->GetName();
|
|
|
|
|
|
|
|
/* Insert this into the help list, SORTED alphabetically. */
|
|
|
|
while (iter != m_CmdList.end())
|
|
|
|
{
|
|
|
|
const char *cmd = NULL;
|
|
|
|
pInfo = (*iter);
|
|
|
|
cmd = pInfo->pCmd->GetName();
|
|
|
|
if (strcmp(orig, cmd) < 0)
|
|
|
|
{
|
|
|
|
m_CmdList.insert(iter, info);
|
|
|
|
inserted = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inserted)
|
|
|
|
{
|
|
|
|
m_CmdList.push_back(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConCmdManager::UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits, bool remove)
|
|
|
|
{
|
|
|
|
if (type == Override_Command)
|
|
|
|
{
|
2013-08-31 20:51:23 +02:00
|
|
|
ConCmdInfo *pInfo;
|
2013-08-25 21:18:25 +02:00
|
|
|
if (!m_Cmds.retrieve(cmd, &pInfo))
|
2008-03-30 09:00:22 +02:00
|
|
|
return;
|
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
for (CmdHookList::iterator iter = pInfo->hooks.begin(); iter != pInfo->hooks.end(); iter++)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2013-08-31 20:51:23 +02:00
|
|
|
if (!iter->admin)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!remove)
|
|
|
|
iter->admin->eflags = bits;
|
|
|
|
else
|
|
|
|
iter->admin->eflags = iter->admin->flags;
|
|
|
|
pInfo->eflags = iter->admin->eflags;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type == Override_CommandGroup)
|
|
|
|
{
|
2013-09-01 04:50:15 +02:00
|
|
|
GroupMap::Result r = m_CmdGrps.find(cmd);
|
|
|
|
if (!r.found())
|
2008-03-30 09:00:22 +02:00
|
|
|
return;
|
|
|
|
|
2015-11-08 22:14:57 +01:00
|
|
|
RefPtr<CommandGroup> group(r->value);
|
2013-09-01 04:50:15 +02:00
|
|
|
|
|
|
|
for (PluginHookList::iterator iter = group->hooks.begin(); iter != group->hooks.end(); iter++)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2013-09-01 04:50:15 +02:00
|
|
|
CmdHook *hook = *iter;
|
|
|
|
if (remove)
|
|
|
|
hook->admin->eflags = bits;
|
|
|
|
else
|
|
|
|
hook->admin->eflags = hook->admin->flags;
|
|
|
|
hook->info->eflags = hook->admin->eflags;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 04:47:22 +02:00
|
|
|
void ConCmdManager::RemoveConCmd(ConCmdInfo *info, const char *name, bool untrack)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
|
|
|
/* Remove from the trie */
|
2013-08-25 21:18:25 +02:00
|
|
|
m_Cmds.remove(name);
|
2008-03-30 09:00:22 +02:00
|
|
|
|
|
|
|
/* Remove console-specific information
|
|
|
|
* This should always be true as of right now
|
|
|
|
*/
|
|
|
|
if (info->pCmd)
|
|
|
|
{
|
|
|
|
if (info->sourceMod)
|
|
|
|
{
|
|
|
|
/* Unlink from SourceMM */
|
|
|
|
g_SMAPI->UnregisterConCommandBase(g_PLAPI, info->pCmd);
|
|
|
|
/* Delete the command's memory */
|
|
|
|
char *new_help = const_cast<char *>(info->pCmd->GetHelpText());
|
|
|
|
char *new_name = const_cast<char *>(info->pCmd->GetName());
|
|
|
|
delete [] new_help;
|
|
|
|
delete [] new_name;
|
|
|
|
delete info->pCmd;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (untrack)
|
|
|
|
UntrackConCommandBase(info->pCmd, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove from list */
|
|
|
|
m_CmdList.remove(info);
|
|
|
|
|
|
|
|
delete info;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConCmdManager::LookForSourceModCommand(const char *cmd)
|
|
|
|
{
|
|
|
|
ConCmdInfo *pInfo;
|
2013-08-25 21:18:25 +02:00
|
|
|
if (!m_Cmds.retrieve(cmd, &pInfo))
|
2008-03-30 09:00:22 +02:00
|
|
|
return false;
|
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
return pInfo->sourceMod && !pInfo->hooks.empty();
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ConCmdManager::LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags)
|
|
|
|
{
|
|
|
|
ConCmdInfo *pInfo;
|
2013-08-25 21:18:25 +02:00
|
|
|
if (!m_Cmds.retrieve(cmd, &pInfo))
|
2008-03-30 09:00:22 +02:00
|
|
|
return false;
|
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
*pFlags = pInfo->eflags;
|
2012-11-15 18:53:11 +01:00
|
|
|
return true;
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 23:39:31 +02:00
|
|
|
ConCmdInfo *ConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags, IPlugin *pPlugin)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
|
|
|
ConCmdInfo *pInfo;
|
2013-08-25 21:18:25 +02:00
|
|
|
if (!m_Cmds.retrieve(name, &pInfo))
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2011-06-20 19:30:14 +02:00
|
|
|
ConCmdList::iterator item = FindInList(name);
|
|
|
|
if (item != m_CmdList.end())
|
|
|
|
return *item;
|
|
|
|
|
2008-03-30 09:00:22 +02:00
|
|
|
pInfo = new ConCmdInfo();
|
|
|
|
/* Find the commandopan */
|
2008-11-14 16:18:30 +01:00
|
|
|
ConCommand *pCmd = FindCommand(name);
|
2008-03-30 09:00:22 +02:00
|
|
|
|
|
|
|
if (!pCmd)
|
|
|
|
{
|
|
|
|
/* Note that we have to duplicate because the source might not be
|
|
|
|
* a static string, and these expect static memory.
|
|
|
|
*/
|
|
|
|
if (!description)
|
|
|
|
{
|
|
|
|
description = "";
|
|
|
|
}
|
|
|
|
char *new_name = sm_strdup(name);
|
|
|
|
char *new_help = sm_strdup(description);
|
|
|
|
pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
|
2018-07-10 23:39:31 +02:00
|
|
|
pInfo->pPlugin = pPlugin;
|
2008-03-30 09:00:22 +02:00
|
|
|
pInfo->sourceMod = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TrackConCommandBase(pCmd, this);
|
2015-09-06 23:55:51 +02:00
|
|
|
CommandHook::Callback callback = [this] (int client, const ICommandArgs *args) -> bool {
|
2015-09-06 21:35:04 +02:00
|
|
|
AutoEnterCommand autoEnterCommand(args);
|
2015-09-06 23:55:51 +02:00
|
|
|
return this->InternalDispatch(client, args);
|
2015-09-06 21:35:04 +02:00
|
|
|
};
|
|
|
|
pInfo->sh_hook = sCoreProviderImpl.AddCommandHook(pCmd, callback);
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pInfo->pCmd = pCmd;
|
|
|
|
|
2013-08-25 21:18:25 +02:00
|
|
|
m_Cmds.insert(name, pInfo);
|
2008-03-30 09:00:22 +02:00
|
|
|
AddToCmdList(pInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pInfo;
|
|
|
|
}
|
|
|
|
|
2015-08-28 04:59:04 +02:00
|
|
|
void ConCmdManager::OnRootConsoleCommand(const char *cmdname, const ICommandArgs *command)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2015-08-28 04:59:04 +02:00
|
|
|
if (command->ArgC() >= 3)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2015-08-28 04:59:04 +02:00
|
|
|
const char *text = command->Arg(2);
|
2008-03-30 09:00:22 +02:00
|
|
|
|
2013-03-29 19:37:29 +01:00
|
|
|
IPlugin *pPlugin = scripts->FindPluginByConsoleArg(text);
|
2008-03-30 09:00:22 +02:00
|
|
|
|
|
|
|
if (!pPlugin)
|
|
|
|
{
|
2015-08-28 20:08:25 +02:00
|
|
|
UTIL_ConsolePrint("[SM] Plugin \"%s\" was not found.", text);
|
2008-03-30 09:00:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sm_plugininfo_t *plinfo = pPlugin->GetPublicInfo();
|
|
|
|
const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : pPlugin->GetFilename();
|
|
|
|
|
2013-08-31 20:51:23 +02:00
|
|
|
PluginHookList *pList;
|
2008-03-30 09:00:22 +02:00
|
|
|
if (!pPlugin->GetProperty("CommandList", (void **)&pList))
|
|
|
|
{
|
2015-08-28 20:08:25 +02:00
|
|
|
UTIL_ConsolePrint("[SM] No commands found for: %s", plname);
|
2008-03-30 09:00:22 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-08-31 20:51:23 +02:00
|
|
|
if (pList->empty())
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2015-08-28 20:08:25 +02:00
|
|
|
UTIL_ConsolePrint("[SM] No commands found for: %s", plname);
|
2008-03-30 09:00:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *type = NULL;
|
|
|
|
const char *name;
|
|
|
|
const char *help;
|
2015-08-28 20:08:25 +02:00
|
|
|
UTIL_ConsolePrint("[SM] Listing commands for: %s", plname);
|
|
|
|
UTIL_ConsolePrint(" %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
|
2013-08-31 20:51:23 +02:00
|
|
|
for (PluginHookList::iterator iter = pList->begin(); iter != pList->end(); iter++)
|
2008-03-30 09:00:22 +02:00
|
|
|
{
|
2013-08-31 20:51:23 +02:00
|
|
|
CmdHook *hook = *iter;
|
|
|
|
if (hook->type == CmdHook::Server)
|
2008-03-30 09:00:22 +02:00
|
|
|
type = "server";
|
2013-08-31 20:51:23 +02:00
|
|
|
else
|
|
|
|
type = hook->info->eflags == 0 ? "console" : "admin";
|
|
|
|
|
|
|
|
name = hook->info->pCmd->GetName();
|
|
|
|
if (hook->helptext.length())
|
|
|
|
help = hook->helptext.chars();
|
2008-03-30 09:00:22 +02:00
|
|
|
else
|
2013-08-31 20:51:23 +02:00
|
|
|
help = hook->info->pCmd->GetHelpText();
|
2015-08-28 20:08:25 +02:00
|
|
|
UTIL_ConsolePrint(" %-17.16s %-12.11s %s", name, type, help);
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-28 20:08:25 +02:00
|
|
|
UTIL_ConsolePrint("[SM] Usage: sm cmds <plugin #>");
|
2008-03-30 09:00:22 +02:00
|
|
|
}
|