Update CS:GO gamedata.
2 * vim: set ts=4 sw=4 tw=99 noet :
3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "ConCmdManager.h"
33 #include "sm_srvcmds.h"
34 #include "AdminCache.h"
35 #include "sm_stringutil.h"
36 #include "PlayerManager.h"
37 #include "HalfLife2.h"
38 #include "ChatTriggers.h"
39 #include "logic_bridge.h"
43 ConCmdManager g_ConCmds;
45 #if SOURCE_ENGINE == SE_DOTA
46 SH_DECL_HOOK2_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommandContext &, const CCommand &);
47 #elif SOURCE_ENGINE >= SE_ORANGEBOX
48 SH_DECL_HOOK1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
50 SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
53 SH_DECL_HOOK1_void(IServerGameClients, SetCommandClient, SH_NOATTRIB, false, int);
55 typedef ke::LinkedList<CmdHook *> PluginHookList;
56 void RegisterInPlugin(CmdHook *hook);
58 ConCmdManager::ConCmdManager()
63 ConCmdManager::~ConCmdManager()
67 void ConCmdManager::OnSourceModAllInitialized()
69 scripts->AddPluginsListener(this);
70 g_RootMenu.AddRootConsoleCommand("cmds", "List console commands", this);
71 SH_ADD_HOOK(IServerGameClients, SetCommandClient, serverClients, SH_MEMBER(this, &ConCmdManager::SetCommandClient), false);
74 void ConCmdManager::OnSourceModShutdown()
76 scripts->RemovePluginsListener(this);
77 /* All commands should already be removed by the time we're done */
78 SH_REMOVE_HOOK(IServerGameClients, SetCommandClient, serverClients, SH_MEMBER(this, &ConCmdManager::SetCommandClient), false);
79 g_RootMenu.RemoveRootConsoleCommand("cmds", this);
82 void ConCmdManager::OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe)
84 /* Whoa, first get its information struct */
87 if (!m_Cmds.retrieve(name, &pInfo))
90 CmdHookList::iterator iter = pInfo->hooks.begin();
91 while (iter != pInfo->hooks.end())
93 CmdHook *hook = *iter;
95 IPluginContext *pContext = hook->pf->GetParentContext();
96 IPlugin *pPlugin = scripts->FindPluginByContext(pContext->GetContext());
98 // The list is guaranteed to exist.
100 pPlugin->GetProperty("CommandList", (void **)&list, false);
101 for (PluginHookList::iterator hiter = list->begin(); hiter != list->end(); hiter++)
111 hook->admin->group->hooks.remove(hook);
113 iter = pInfo->hooks.erase(iter);
117 RemoveConCmd(pInfo, name, is_read_safe, false);
120 void ConCmdManager::OnPluginDestroyed(IPlugin *plugin)
122 PluginHookList *pList;
123 if (!plugin->GetProperty("CommandList", (void **)&pList, true))
126 PluginHookList::iterator iter = pList->begin();
127 while (iter != pList->end())
129 CmdHook *hook = *iter;
131 hook->info->hooks.remove(hook);
133 hook->admin->group->hooks.remove(hook);
135 if (hook->info->hooks.empty())
136 RemoveConCmd(hook->info, hook->info->pCmd->GetName(), true, true);
138 iter = pList->erase(iter);
145 #if SOURCE_ENGINE == SE_DOTA
146 void CommandCallback(const CCommandContext &context, const CCommand &command)
148 #elif SOURCE_ENGINE >= SE_ORANGEBOX
149 void CommandCallback(const CCommand &command)
152 void CommandCallback()
157 g_HL2.PushCommandStack(&command);
159 g_ConCmds.InternalDispatch(command);
161 g_HL2.PopCommandStack();
164 void ConCmdManager::SetCommandClient(int client)
166 m_CmdClient = client + 1;
169 ConCmdInfo *ConCmdManager::FindInTrie(const char *name)
172 if (!m_Cmds.retrieve(name, &pInfo))
177 ConCmdList::iterator ConCmdManager::FindInList(const char *cmd)
179 List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
181 while (iter != m_CmdList.end())
183 if (strcasecmp((*iter)->pCmd->GetName(), cmd) == 0)
191 ResultType ConCmdManager::DispatchClientCommand(int client, const char *cmd, int args, ResultType type)
195 if ((pInfo = FindInTrie(cmd)) == NULL)
197 ConCmdList::iterator item = FindInList(cmd);
198 if (item == m_CmdList.end())
203 cell_t result = type;
204 for (CmdHookList::iterator iter = pInfo->hooks.begin(); iter != pInfo->hooks.end(); iter++)
206 CmdHook *hook = *iter;
208 if (hook->type == CmdHook::Server || !hook->pf->IsRunnable())
211 if (hook->admin && !CheckAccess(client, cmd, hook->admin))
213 if (result < Pl_Handled)
218 hook->pf->PushCell(client);
219 hook->pf->PushCell(args);
221 cell_t tempres = result;
222 if (hook->pf->Execute(&tempres) == SP_ERROR_NONE)
224 if (tempres > result)
226 if (result == Pl_Stop)
231 return (ResultType)result;
234 void ConCmdManager::InternalDispatch(const CCommand &command)
236 int client = m_CmdClient;
240 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
241 if (!pPlayer || !pPlayer->IsConnected())
246 * Note: Console commands will EITHER go through IServerGameDLL::ClientCommand,
247 * OR this dispatch. They will NEVER go through both.
249 * Whether or not it goes through the callback is determined by FCVAR_GAMEDLL
251 const char *cmd = g_HL2.CurrentCommandName();
253 ConCmdInfo *pInfo = FindInTrie(cmd);
256 /* Unfortunately, we now have to do a slow lookup because Valve made client commands
257 * case-insensitive. We can't even use our sortedness.
259 if (client == 0 && !engine->IsDedicatedServer())
262 ConCmdList::iterator item = FindInList(cmd);
263 if (item == m_CmdList.end())
269 /* This is a hack to prevent say triggers from firing on messages that were
270 * blocked because of flooding. We won't remove this, but the hack will get
271 * "nicer" when we expose explicit say hooks.
273 if (g_ChatTriggers.WasFloodedMessage())
276 cell_t result = Pl_Continue;
277 int args = command.ArgC() - 1;
279 // On a listen server, sometimes the server host's client index can be set
280 // as 0. So index 1 is passed to the command callback to correct this
281 // potential problem.
282 int realClient = (!engine->IsDedicatedServer() && client == 0)
283 ? g_Players.ListenClient()
285 int dedicatedClient = engine->IsDedicatedServer() ? 0 : g_Players.ListenClient();
287 for (CmdHookList::iterator iter = pInfo->hooks.begin(); iter != pInfo->hooks.end(); iter++)
289 CmdHook *hook = *iter;
291 if (!hook->pf->IsRunnable())
294 if (hook->type == CmdHook::Server)
296 // Don't execute unless we're in the server console.
297 if (realClient != dedicatedClient)
300 // Check admin rights if needed. realClient isn't needed since we
301 // should bypass admin checks if client == 0 anyway.
302 if (client && hook->admin && !CheckAccess(client, cmd, hook->admin))
304 if (result < Pl_Handled)
309 // Client hooks get a client argument.
310 hook->pf->PushCell(realClient);
313 hook->pf->PushCell(args);
315 cell_t tempres = result;
316 if (hook->pf->Execute(&tempres) == SP_ERROR_NONE)
318 if (tempres > result)
320 if (result == Pl_Stop)
325 if (result >= Pl_Handled)
327 if (!pInfo->sourceMod)
328 RETURN_META(MRES_SUPERCEDE);
333 bool ConCmdManager::CheckClientCommandAccess(int client, const char *cmd, FlagBits cmdflags)
335 if (cmdflags == 0 || client == 0)
340 /* If running listen server, then client 1 is the server host and should have 'root' access */
341 if (client == 1 && !engine->IsDedicatedServer())
346 CPlayer *player = g_Players.GetPlayerByIndex(client);
348 || player->GetEdict() == NULL
349 || player->IsFakeClient())
354 return CheckAdminCommandAccess(player->GetAdminId(), cmd, cmdflags);
357 bool ConCmdManager::CheckAdminCommandAccess(AdminId adm, const char *cmd, FlagBits cmdflags)
359 if (adm != INVALID_ADMIN_ID)
361 FlagBits bits = g_Admins.GetAdminFlags(adm, Access_Effective);
363 /* root knows all, WHOA */
364 if ((bits & ADMFLAG_ROOT) == ADMFLAG_ROOT)
369 /* Check for overrides
370 * :TODO: is it worth optimizing this?
372 unsigned int groups = g_Admins.GetAdminGroupCount(adm);
375 bool override = false;
376 for (unsigned int i=0; i<groups; i++)
378 gid = g_Admins.GetAdminGroup(adm, i, NULL);
379 /* First get group-level override */
380 override = g_Admins.GetGroupCommandOverride(gid, cmd, Override_CommandGroup, &rule);
381 /* Now get the specific command override */
382 if (g_Admins.GetGroupCommandOverride(gid, cmd, Override_Command, &rule))
388 if (rule == Command_Allow)
392 else if (rule == Command_Deny)
399 /* See if our other flags match */
400 if ((bits & cmdflags) == cmdflags)
409 bool ConCmdManager::CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin)
411 if (CheckClientCommandAccess(client, cmd, pAdmin->eflags))
416 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
422 /* If we got here, the command failed... */
424 if (!logicore.CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "No Access", &client))
426 UTIL_Format(buffer, sizeof(buffer), "You do not have access to this command");
429 unsigned int replyto = g_ChatTriggers.GetReplyTo();
430 if (replyto == SM_REPLY_CONSOLE)
432 char fullbuffer[192];
433 UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
434 pPlayer->PrintToConsole(fullbuffer);
436 else if (replyto == SM_REPLY_CHAT)
438 char fullbuffer[192];
439 UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s.", buffer);
440 g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
446 bool ConCmdManager::AddAdminCommand(IPluginFunction *pFunction,
450 const char *description,
453 ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
458 GroupMap::Insert i = m_CmdGrps.findForAdd(group);
461 if (!m_CmdGrps.add(i, group))
463 i->value = NoAddRef(new CommandGroup());
465 Ref<CommandGroup> cmdgroup = i->value;
467 CmdHook *pHook = new CmdHook(CmdHook::Client, pInfo, pFunction, description);
468 pHook->admin = new AdminCmdInfo(cmdgroup, adminflags);
470 /* First get the command group override, if any */
471 bool override = g_Admins.GetCommandOverride(group,
472 Override_CommandGroup,
473 &(pHook->admin->eflags));
475 /* Next get the command override, if any */
476 if (g_Admins.GetCommandOverride(name,
478 &(pHook->admin->eflags)))
483 /* Assign normal flags if there were no overrides */
485 pHook->admin->eflags = pHook->admin->flags;
486 pInfo->eflags = pHook->admin->eflags;
488 cmdgroup->hooks.append(pHook);
489 pInfo->hooks.append(pHook);
490 RegisterInPlugin(pHook);
494 bool ConCmdManager::AddServerCommand(IPluginFunction *pFunction,
496 const char *description,
500 ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
505 CmdHook *pHook = new CmdHook(CmdHook::Server, pInfo, pFunction, description);
507 pInfo->hooks.append(pHook);
508 RegisterInPlugin(pHook);
512 void RegisterInPlugin(CmdHook *hook)
514 PluginHookList *pList;
516 IPlugin *pPlugin = scripts->FindPluginByContext(hook->pf->GetParentContext());
517 if (!pPlugin->GetProperty("CommandList", (void **)&pList))
519 pList = new PluginHookList();
520 pPlugin->SetProperty("CommandList", pList);
523 const char *orig = hook->info->pCmd->GetName();
525 /* Insert this into the help list, SORTED alphabetically. */
526 PluginHookList::iterator iter = pList->begin();
527 while (iter != pList->end())
529 const char *cmd = (*iter)->info->pCmd->GetName();
530 if (strcmp(orig, cmd) < 0)
532 pList->insertBefore(iter, hook);
541 void ConCmdManager::AddToCmdList(ConCmdInfo *info)
543 List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
545 bool inserted = false;
546 const char *orig = NULL;
548 orig = info->pCmd->GetName();
550 /* Insert this into the help list, SORTED alphabetically. */
551 while (iter != m_CmdList.end())
553 const char *cmd = NULL;
555 cmd = pInfo->pCmd->GetName();
556 if (strcmp(orig, cmd) < 0)
558 m_CmdList.insert(iter, info);
567 m_CmdList.push_back(info);
571 void ConCmdManager::UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits, bool remove)
573 if (type == Override_Command)
576 if (!m_Cmds.retrieve(cmd, &pInfo))
579 for (CmdHookList::iterator iter = pInfo->hooks.begin(); iter != pInfo->hooks.end(); iter++)
585 iter->admin->eflags = bits;
587 iter->admin->eflags = iter->admin->flags;
588 pInfo->eflags = iter->admin->eflags;
591 else if (type == Override_CommandGroup)
593 GroupMap::Result r = m_CmdGrps.find(cmd);
597 Ref<CommandGroup> group(r->value);
599 for (PluginHookList::iterator iter = group->hooks.begin(); iter != group->hooks.end(); iter++)
601 CmdHook *hook = *iter;
603 hook->admin->eflags = bits;
605 hook->admin->eflags = hook->admin->flags;
606 hook->info->eflags = hook->admin->eflags;
611 void ConCmdManager::RemoveConCmd(ConCmdInfo *info, const char *name, bool is_read_safe, bool untrack)
613 /* Remove from the trie */
616 /* Remove console-specific information
617 * This should always be true as of right now
623 /* Unlink from SourceMM */
624 g_SMAPI->UnregisterConCommandBase(g_PLAPI, info->pCmd);
625 /* Delete the command's memory */
626 char *new_help = const_cast<char *>(info->pCmd->GetHelpText());
627 char *new_name = const_cast<char *>(info->pCmd->GetName());
636 /* Remove the external hook */
637 SH_REMOVE_HOOK(ConCommand, Dispatch, info->pCmd, SH_STATIC(CommandCallback), false);
641 UntrackConCommandBase(info->pCmd, this);
646 /* Remove from list */
647 m_CmdList.remove(info);
652 bool ConCmdManager::LookForSourceModCommand(const char *cmd)
655 if (!m_Cmds.retrieve(cmd, &pInfo))
658 return pInfo->sourceMod && !pInfo->hooks.empty();
661 bool ConCmdManager::LookForCommandAdminFlags(const char *cmd, FlagBits *pFlags)
664 if (!m_Cmds.retrieve(cmd, &pInfo))
667 *pFlags = pInfo->eflags;
671 ConCmdInfo *ConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags)
674 if (!m_Cmds.retrieve(name, &pInfo))
676 ConCmdList::iterator item = FindInList(name);
677 if (item != m_CmdList.end())
680 pInfo = new ConCmdInfo();
681 /* Find the commandopan */
682 ConCommand *pCmd = FindCommand(name);
686 /* Note that we have to duplicate because the source might not be
687 * a static string, and these expect static memory.
693 char *new_name = sm_strdup(name);
694 char *new_help = sm_strdup(description);
695 pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
696 pInfo->sourceMod = true;
700 TrackConCommandBase(pCmd, this);
701 SH_ADD_HOOK(ConCommand, Dispatch, pCmd, SH_STATIC(CommandCallback), false);
706 m_Cmds.insert(name, pInfo);
713 void ConCmdManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
715 if (command.ArgC() >= 3)
717 const char *text = command.Arg(2);
719 IPlugin *pPlugin = scripts->FindPluginByConsoleArg(text);
723 g_RootMenu.ConsolePrint("[SM] Plugin \"%s\" was not found.", text);
727 const sm_plugininfo_t *plinfo = pPlugin->GetPublicInfo();
728 const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : pPlugin->GetFilename();
730 PluginHookList *pList;
731 if (!pPlugin->GetProperty("CommandList", (void **)&pList))
733 g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
738 g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
742 const char *type = NULL;
745 g_RootMenu.ConsolePrint("[SM] Listing commands for: %s", plname);
746 g_RootMenu.ConsolePrint(" %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
747 for (PluginHookList::iterator iter = pList->begin(); iter != pList->end(); iter++)
749 CmdHook *hook = *iter;
750 if (hook->type == CmdHook::Server)
753 type = hook->info->eflags == 0 ? "console" : "admin";
755 name = hook->info->pCmd->GetName();
756 if (hook->helptext.length())
757 help = hook->helptext.chars();
759 help = hook->info->pCmd->GetHelpText();
760 g_RootMenu.ConsolePrint(" %-17.16s %-12.11s %s", name, type, help);
766 g_RootMenu.ConsolePrint("[SM] Usage: sm cmds <plugin #>");