core/ConCmdManager.cpp
author David Anderson <dvander@alliedmods.net>
Wed May 02 20:44:35 2007 +0000 (2007-05-02)
changeset 723 60395a16ddea
parent 653 ef60870067ff
child 724 d659204548ca
permissions -rw-r--r--
added amb217 (server.cfg forward)
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include "ConCmdManager.h"
    16 #include "sm_srvcmds.h"
    17 #include "AdminCache.h"
    18 #include "sm_stringutil.h"
    19 #include "PlayerManager.h"
    20 #include "Translator.h"
    21 
    22 ConCmdManager g_ConCmds;
    23 
    24 SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
    25 SH_DECL_HOOK1_void(IServerGameClients, SetCommandClient, SH_NOATTRIB, false, int);
    26 
    27 struct PlCmdInfo
    28 {
    29 	ConCmdInfo *pInfo;
    30 	CmdHook *pHook;
    31 	CmdType type;
    32 };
    33 typedef List<PlCmdInfo> CmdList;
    34 
    35 void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info);
    36 
    37 ConCmdManager::ConCmdManager() : m_Strings(1024)
    38 {
    39 	m_pCmds = sm_trie_create();
    40 	m_pCmdGrps = sm_trie_create();
    41 	m_bServerCfgDone = true;
    42 	m_pExecCmd = NULL;
    43 	m_pServerCfgFile = NULL;
    44 	m_pServerCfgFwd = NULL;
    45 }
    46 
    47 ConCmdManager::~ConCmdManager()
    48 {
    49 	sm_trie_destroy(m_pCmds);
    50 	sm_trie_destroy(m_pCmdGrps);
    51 }
    52 
    53 void ConCmdManager::OnSourceModAllInitialized()
    54 {
    55 	g_PluginSys.AddPluginsListener(this);
    56 	g_RootMenu.AddRootConsoleCommand("cmds", "List console commands", this);
    57 	SH_ADD_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, serverClients, this, &ConCmdManager::SetCommandClient, false);
    58 
    59 	ConCommandBase *pCmd = icvar->GetCommands();
    60 	while (pCmd)
    61 	{
    62 		if (pCmd->IsCommand() 
    63 			&& strcmp(pCmd->GetName(), "exec") == 0)
    64 		{
    65 			m_pExecCmd = (ConCommand *)pCmd;
    66 			break;
    67 		}
    68 		pCmd = const_cast<ConCommandBase *>(pCmd->GetNext());
    69 	}
    70 
    71 	if (m_pExecCmd)
    72 	{
    73 		m_pServerCfgFile = (ConVar *)icvar->FindVar("servercfgfile");
    74 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pExecCmd, this, &ConCmdManager::OnExecCmd, true);
    75 		m_pServerCfgFwd = g_Forwards.CreateForward("OnServerCfg", ET_Ignore, 0, NULL);
    76 	}
    77 }
    78 
    79 void ConCmdManager::OnSourceModShutdown()
    80 {
    81 	if (m_pExecCmd)
    82 	{
    83 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pExecCmd, this, &ConCmdManager::OnExecCmd, true);
    84 		g_Forwards.ReleaseForward(m_pServerCfgFwd);
    85 		m_pServerCfgFwd = NULL;
    86 		m_pExecCmd = NULL;
    87 	}
    88 
    89 	/* All commands should already be removed by the time we're done */
    90 	SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, SetCommandClient, serverClients, this, &ConCmdManager::SetCommandClient, false);
    91 	g_RootMenu.RemoveRootConsoleCommand("cmds", this);
    92 }
    93 
    94 void ConCmdManager::OnSourceModPluginsLoaded()
    95 {
    96 	m_bServerCfgDone = false;
    97 }
    98 
    99 void ConCmdManager::OnExecCmd()
   100 {
   101 	const char *arg = engine->Cmd_Argv(1);
   102 	const char *cfgfile = "server.cfg";
   103 
   104 	if (m_pServerCfgFile)
   105 	{
   106 		cfgfile = m_pServerCfgFile->GetString();
   107 	}
   108 
   109 	if (strcmp(arg, cfgfile) == 0)
   110 	{
   111 		engine->ServerCommand("sm cmds internal 1\n");
   112 	}
   113 }
   114 
   115 void ConCmdManager::NotifyExecDone(const char *file)
   116 {
   117 	if (file == NULL && !m_bServerCfgDone)
   118 	{
   119 		/* Server-cfg file */
   120 		m_bServerCfgDone = true;
   121 		m_pServerCfgFwd->Execute(NULL);
   122 	}
   123 }
   124 
   125 void ConCmdManager::RemoveConCmds(List<CmdHook *> &cmdlist, IPluginContext *pContext)
   126 {
   127 	List<CmdHook *>::iterator iter = cmdlist.begin();
   128 	CmdHook *pHook;
   129 
   130 	while (iter != cmdlist.end())
   131 	{
   132 		pHook = (*iter);
   133 		if (pHook->pf->GetParentContext() == pContext)
   134 		{
   135 			delete pHook->pAdmin;
   136 			delete pHook;
   137 			iter = cmdlist.erase(iter);
   138 		} else {
   139 			iter++;
   140 		}
   141 	}
   142 }
   143 
   144 void ConCmdManager::OnPluginDestroyed(IPlugin *plugin)
   145 {
   146 	CmdList *pList;
   147 	List<ConCmdInfo *> removed;
   148 	if (plugin->GetProperty("CommandList", (void **)&pList, true))
   149 	{
   150 		IPluginContext *pContext = plugin->GetBaseContext();
   151 		CmdList::iterator iter;
   152 		/* First pass!
   153 	 	 * Only bother if there's an actual command list on this plugin... 
   154 	 	 */
   155 		for (iter=pList->begin();
   156 			iter!=pList->end();
   157 			iter++)
   158 		{
   159 			PlCmdInfo &cmd = (*iter);
   160 			ConCmdInfo *pInfo = cmd.pInfo;
   161 
   162 			/* Has this chain already been fully cleaned/removed? */
   163 			if (removed.find(pInfo) != removed.end())
   164 			{
   165 				continue;
   166 			}
   167 
   168 			/* Remove any hooks from us on this command */
   169 			RemoveConCmds(pInfo->conhooks, pContext);
   170 			RemoveConCmds(pInfo->srvhooks, pContext);
   171 
   172 			/* See if there are still hooks */
   173 			if (pInfo->srvhooks.size())
   174 			{
   175 				continue;
   176 			}
   177 			if (pInfo->conhooks.size())
   178 			{
   179 				continue;
   180 			}
   181 
   182 			/* Remove the command, it should be safe now */
   183 			RemoveConCmd(pInfo);
   184 			removed.push_back(pInfo);
   185 		}
   186 		delete pList;
   187 	}
   188 }
   189 
   190 void CommandCallback()
   191 {
   192 	g_ConCmds.InternalDispatch();
   193 }
   194 
   195 void ConCmdManager::SetCommandClient(int client)
   196 {
   197 	m_CmdClient = client + 1;
   198 }
   199 
   200 ResultType ConCmdManager::DispatchClientCommand(int client, ResultType type)
   201 {
   202 	const char *cmd = engine->Cmd_Argv(0);
   203 	int args = engine->Cmd_Argc() - 1;
   204 
   205 	ConCmdInfo *pInfo;
   206 	if (sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   207 	{
   208 		cell_t result = type;
   209 		cell_t tempres = result;
   210 		List<CmdHook *>::iterator iter;
   211 		CmdHook *pHook;
   212 		for (iter=pInfo->conhooks.begin();
   213 			 iter!=pInfo->conhooks.end();
   214 			 iter++)
   215 		{
   216 			pHook = (*iter);
   217 			if (!pHook->pf->IsRunnable())
   218 			{
   219 				continue;
   220 			}
   221 			if (pHook->pAdmin && !CheckAccess(client, cmd, pHook->pAdmin))
   222 			{
   223 				if (result < Pl_Handled)
   224 				{
   225 					result = Pl_Handled;
   226 				}
   227 				continue;
   228 			}
   229 			pHook->pf->PushCell(client);
   230 			pHook->pf->PushCell(args);
   231 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   232 			{
   233 				if (tempres > result)
   234 				{
   235 					result = tempres;
   236 				}
   237 				if (result == Pl_Stop)
   238 				{
   239 					break;
   240 				}
   241 			}
   242 		}
   243 		type = (ResultType)result;
   244 	}
   245 
   246 	return type;
   247 }
   248 
   249 void ConCmdManager::InternalDispatch()
   250 {
   251 	/**
   252 	 * Note: Console commands will EITHER go through IServerGameDLL::ClientCommand,
   253 	 * OR this dispatch.  They will NEVER go through both.
   254 	 * -- 
   255 	 * Whether or not it goes through the callback is determined by FCVAR_GAMEDLL
   256 	 */
   257 
   258 	const char *cmd = engine->Cmd_Argv(0);
   259 	ConCmdInfo *pInfo;
   260 	if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   261 	{
   262 		return;
   263 	}
   264 
   265 	cell_t result = Pl_Continue;
   266 	int args = engine->Cmd_Argc() - 1;
   267 
   268 	List<CmdHook *>::iterator iter;
   269 	CmdHook *pHook;
   270 
   271 	/* Execute server-only commands if viable */
   272 	if (m_CmdClient == 0 && pInfo->srvhooks.size())
   273 	{
   274 		cell_t tempres = result;
   275 		for (iter=pInfo->srvhooks.begin();
   276 			 iter!=pInfo->srvhooks.end();
   277 			 iter++)
   278 		{
   279 			pHook = (*iter);
   280 			if (!pHook->pf->IsRunnable())
   281 			{
   282 				continue;
   283 			}
   284 			pHook->pf->PushCell(args);
   285 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   286 			{
   287 				if (tempres > result)
   288 				{
   289 					result = tempres;
   290 				}
   291 				if (result == Pl_Stop)
   292 				{
   293 					break;
   294 				}
   295 			}
   296 		}
   297 	
   298 		/* Check if there's an early stop */
   299 		if (result >= Pl_Stop)
   300 		{
   301 			if (!pInfo->sourceMod)
   302 			{
   303 				RETURN_META(MRES_SUPERCEDE);
   304 			}
   305 			return;
   306 		}
   307 	}
   308 
   309 	/* Execute console commands */
   310 	if (pInfo->conhooks.size())
   311 	{
   312 		cell_t tempres = result;
   313 		for (iter=pInfo->conhooks.begin();
   314 			iter!=pInfo->conhooks.end();
   315 			iter++)
   316 		{
   317 			pHook = (*iter);
   318 			if (!pHook->pf->IsRunnable())
   319 			{
   320 				continue;
   321 			}
   322 			if (m_CmdClient 
   323 				&& pHook->pAdmin
   324 				&& !CheckAccess(m_CmdClient, cmd, pHook->pAdmin))
   325 			{
   326 				if (result < Pl_Handled)
   327 				{
   328 					result = Pl_Handled;
   329 				}
   330 				continue;
   331 			}
   332 			pHook->pf->PushCell(m_CmdClient);
   333 			pHook->pf->PushCell(args);
   334 			if (pHook->pf->Execute(&tempres) == SP_ERROR_NONE)
   335 			{
   336 				if (tempres > result)
   337 				{
   338 					result = tempres;
   339 				}
   340 				if (result == Pl_Stop)
   341 				{
   342 					break;
   343 				}
   344 			}
   345 		}
   346 	}
   347 
   348 	if (result >= Pl_Handled)
   349 	{
   350 		if (!pInfo->sourceMod)
   351 		{
   352 			RETURN_META(MRES_SUPERCEDE);
   353 		}
   354 		return;
   355 	}
   356 }
   357 
   358 bool ConCmdManager::CheckAccess(int client, const char *cmd, AdminCmdInfo *pAdmin)
   359 {
   360 	FlagBits cmdflags = pAdmin->eflags;
   361 	if (cmdflags == 0)
   362 	{
   363 		return true;
   364 	}
   365 
   366 	CPlayer *player = g_Players.GetPlayerByIndex(client);
   367 	if (!player)
   368 	{
   369 		return false;
   370 	}
   371 
   372 	AdminId adm = player->GetAdminId();
   373 	if (adm != INVALID_ADMIN_ID)
   374 	{
   375 		FlagBits bits = g_Admins.GetAdminFlags(adm, Access_Effective);
   376 
   377 		/* root knows all, WHOA */
   378 		if ((bits & ADMFLAG_ROOT) == ADMFLAG_ROOT)
   379 		{
   380 			return true;
   381 		}
   382 
   383 		/* See if our other flags match */
   384 		if ((bits & cmdflags) == cmdflags)
   385 		{
   386 			return true;
   387 		}
   388 
   389 		/* Check for overrides */
   390 		unsigned int groups = g_Admins.GetAdminGroupCount(adm);
   391 		GroupId gid;
   392 		OverrideRule rule;
   393 		bool override = false;
   394 		for (unsigned int i=0; i<groups; i++)
   395 		{
   396 			gid = g_Admins.GetAdminGroup(adm, i, NULL);
   397 			/* First get group-level override */
   398 			override = g_Admins.GetGroupCommandOverride(gid, cmd, Override_CommandGroup, &rule);
   399 			/* Now get the specific command override */
   400 			if (g_Admins.GetGroupCommandOverride(gid, cmd, Override_Command, &rule))
   401 			{
   402 				override = true;
   403 			}
   404 			if (override)
   405 			{
   406 				if (rule == Command_Allow)
   407 				{
   408 					return true;
   409 				} else if (rule == Command_Deny) {
   410 					break;
   411 				}
   412 			}
   413 		}
   414 	}
   415 
   416 	if (player->IsFakeClient()
   417 		|| player->GetEdict() == NULL)
   418 	{
   419 		return false;
   420 	}
   421 	
   422 	/* If we got here, the command failed... */
   423 	char buffer[128];
   424 	if (g_Translator.CoreTrans(client, buffer, sizeof(buffer), "No Access", NULL, NULL)
   425 		!= Trans_Okay)
   426 	{
   427 		snprintf(buffer, sizeof(buffer), "You do not have access to this command");
   428 	}
   429 
   430 	char fullbuffer[192];
   431 	snprintf(fullbuffer, sizeof(fullbuffer), "[SM] %s.\n", buffer);
   432 
   433 	engine->ClientPrintf(player->GetEdict(), fullbuffer);
   434 	
   435 	return false;
   436 }
   437 
   438 bool ConCmdManager::AddConsoleCommand(IPluginFunction *pFunction, 
   439 									   const char *name, 
   440 									   const char *description, 
   441 									   int flags)
   442 {
   443 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   444 
   445 	if (!pInfo)
   446 	{
   447 		return false;
   448 	}
   449 
   450 	CmdHook *pHook = new CmdHook();
   451 
   452 	pHook->pf = pFunction;
   453 	if (description && description[0])
   454 	{
   455 		pHook->helptext.assign(description);
   456 	}
   457 	pInfo->conhooks.push_back(pHook);
   458 
   459 	/* Add to the plugin */
   460 	CmdList *pList;
   461 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   462 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   463 	{
   464 		pList = new CmdList();
   465 		pPlugin->SetProperty("CommandList", pList);
   466 	}
   467 	PlCmdInfo info;
   468 	info.pInfo = pInfo;
   469 	info.type = Cmd_Console;
   470 	info.pHook = pHook;
   471 	AddToPlCmdList(pList, info);
   472 
   473 	return true;
   474 }
   475 
   476 bool ConCmdManager::AddAdminCommand(IPluginFunction *pFunction, 
   477 									 const char *name, 
   478 									 const char *group,
   479 									 int adminflags,
   480 									 const char *description, 
   481 									 int flags)
   482 {
   483 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   484 
   485 	if (!pInfo)
   486 	{
   487 		return false;
   488 	}
   489 
   490 	CmdHook *pHook = new CmdHook();
   491 	AdminCmdInfo *pAdmin = new AdminCmdInfo();
   492 
   493 	pHook->pf = pFunction;
   494 	if (description && description[0])
   495 	{
   496 		pHook->helptext.assign(description);
   497 	}
   498 	pHook->pAdmin = pAdmin;
   499 
   500 	void *object;
   501 	int grpid;
   502 	if (!sm_trie_retrieve(m_pCmdGrps, group, (void **)&object))
   503 	{
   504 		grpid = m_Strings.AddString(group);
   505 		sm_trie_insert(m_pCmdGrps, group, (void *)grpid);
   506 	} else {
   507 		grpid = (int)object;
   508 	}
   509 
   510 	pAdmin->cmdGrpId = grpid;
   511 	pAdmin->flags = adminflags;
   512 
   513 	/* First get the command group override, if any */
   514 	bool override = g_Admins.GetCommandOverride(group, 
   515 		Override_CommandGroup,
   516 		&(pAdmin->eflags));
   517 
   518 	/* Next get the command override, if any */
   519 	if (g_Admins.GetCommandOverride(name,
   520 		Override_Command,
   521 		&(pAdmin->eflags)))
   522 	{
   523 		override = true;
   524 	}
   525 
   526 	/* Assign normal flags if there were no overrides */
   527 	if (!override)
   528 	{
   529 		pAdmin->eflags = pAdmin->flags;
   530 	}
   531 
   532 	/* Finally, add the hook */
   533 	pInfo->conhooks.push_back(pHook);
   534 
   535 	/* Now add to the plugin */
   536 	CmdList *pList;
   537 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   538 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   539 	{
   540 		pList = new CmdList();
   541 		pPlugin->SetProperty("CommandList", pList);
   542 	}
   543 	PlCmdInfo info;
   544 	info.pInfo = pInfo;
   545 	info.type = Cmd_Admin;
   546 	info.pHook = pHook;
   547 	AddToPlCmdList(pList, info);
   548 
   549 	return true;
   550 }
   551 
   552 bool ConCmdManager::AddServerCommand(IPluginFunction *pFunction, 
   553 									  const char *name, 
   554 									  const char *description, 
   555 									  int flags)
   556 
   557 {
   558 	ConCmdInfo *pInfo = AddOrFindCommand(name, description, flags);
   559 
   560 	if (!pInfo)
   561 	{
   562 		return false;
   563 	}
   564 
   565 	CmdHook *pHook = new CmdHook();
   566 
   567 	pHook->pf = pFunction;
   568 	if (description && description[0])
   569 	{
   570 		pHook->helptext.assign(description);
   571 	}
   572 
   573 	pInfo->srvhooks.push_back(pHook);
   574 
   575 	/* Add to the plugin */
   576 	CmdList *pList;
   577 	IPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
   578 	if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   579 	{
   580 		pList = new CmdList();
   581 		pPlugin->SetProperty("CommandList", pList);
   582 	}
   583 	PlCmdInfo info;
   584 	info.pInfo = pInfo;
   585 	info.type = Cmd_Server;
   586 	info.pHook = pHook;
   587 	AddToPlCmdList(pList, info);
   588 
   589 	return true;
   590 }
   591 
   592 void AddToPlCmdList(CmdList *pList, const PlCmdInfo &info)
   593 {
   594 	CmdList::iterator iter = pList->begin();
   595 	bool inserted = false;
   596 	const char *orig = NULL;
   597 
   598 	orig = info.pInfo->pCmd->GetName();
   599 
   600 	/* Insert this into the help list, SORTED alphabetically. */
   601 	while (iter != pList->end())
   602 	{
   603 		PlCmdInfo &obj = (*iter);
   604 		const char *cmd = obj.pInfo->pCmd->GetName();
   605 		if (strcmp(orig, cmd) < 0)
   606 		{
   607 			pList->insert(iter, info);
   608 			inserted = true;
   609 			break;
   610 		}
   611 		iter++;
   612 	}
   613 
   614 	if (!inserted)
   615 	{
   616 		pList->push_back(info);
   617 	}
   618 }
   619 
   620 void ConCmdManager::AddToCmdList(ConCmdInfo *info)
   621 {
   622 	List<ConCmdInfo *>::iterator iter = m_CmdList.begin();
   623 	ConCmdInfo *pInfo;
   624 	bool inserted = false;
   625 	const char *orig = NULL;
   626 
   627 	orig = info->pCmd->GetName();
   628 
   629 	/* Insert this into the help list, SORTED alphabetically. */
   630 	while (iter != m_CmdList.end())
   631 	{
   632 		const char *cmd = NULL;
   633 		pInfo = (*iter);
   634 		cmd = pInfo->pCmd->GetName();
   635 		if (strcmp(orig, cmd) < 0)
   636 		{
   637 			m_CmdList.insert(iter, info);
   638 			inserted = true;
   639 			break;
   640 		}
   641 		iter++;
   642 	}
   643 
   644 	if (!inserted)
   645 	{
   646 		m_CmdList.push_back(info);
   647 	}
   648 }
   649 
   650 void ConCmdManager::UpdateAdminCmdFlags(const char *cmd, OverrideType type, FlagBits bits)
   651 {
   652 	ConCmdInfo *pInfo;
   653 
   654 	if (type == Override_Command)
   655 	{
   656 		if (!sm_trie_retrieve(m_pCmds, cmd, (void **)&pInfo))
   657 		{
   658 			return;
   659 		}
   660 
   661 		List<CmdHook *>::iterator iter;
   662 		CmdHook *pHook;
   663 
   664 		for (iter=pInfo->conhooks.begin(); iter!=pInfo->conhooks.end(); iter++)
   665 		{
   666 			pHook = (*iter);
   667 			if (pHook->pAdmin)
   668 			{
   669 				if (bits)
   670 				{
   671 					pHook->pAdmin->eflags = bits;
   672 				} else {
   673 					pHook->pAdmin->eflags = pHook->pAdmin->flags;
   674 				}
   675 			}
   676 		}
   677 	} else if (type == Override_CommandGroup) {
   678 		void *object;
   679 		if (!sm_trie_retrieve(m_pCmdGrps, cmd, &object))
   680 		{
   681 			return;
   682 		}
   683 		int grpid = (int)object;
   684 
   685 		/* This is bad :( loop through all commands */
   686 		List<ConCmdInfo *>::iterator iter;
   687 		CmdHook *pHook;
   688 		for (iter=m_CmdList.begin(); iter!=m_CmdList.end(); iter++)
   689 		{
   690 			pInfo = (*iter);
   691 			for (List<CmdHook *>::iterator citer=pInfo->conhooks.begin();
   692 				 citer!=pInfo->conhooks.end();
   693 				 citer++)
   694 			{
   695 				pHook = (*citer);
   696 				if (pHook->pAdmin && pHook->pAdmin->cmdGrpId == grpid)
   697 				{
   698 					if (bits)
   699 					{
   700 						pHook->pAdmin->eflags = bits;
   701 					} else {
   702 						pHook->pAdmin->eflags = pHook->pAdmin->flags;
   703 					}
   704 				}
   705 			}
   706 		}
   707 	}
   708 }
   709 
   710 void ConCmdManager::RemoveConCmd(ConCmdInfo *info)
   711 {
   712 	/* Remove console-specific information
   713 	 * This should always be true as of right now
   714 	 */
   715 	if (info->pCmd)
   716 	{
   717 		/* Remove from the trie */
   718 		sm_trie_delete(m_pCmds, info->pCmd->GetName());
   719 
   720 		if (info->sourceMod)
   721 		{
   722 			/* Unlink from SourceMM */
   723 			g_SMAPI->UnregisterConCmdBase(g_PLAPI, info->pCmd);
   724 			/* Delete the command's memory */
   725 			char *new_help = const_cast<char *>(info->pCmd->GetHelpText());
   726 			char *new_name = const_cast<char *>(info->pCmd->GetName());
   727 			delete [] new_help;
   728 			delete [] new_name;
   729 			delete info->pCmd;
   730 		} else {
   731 			/* Remove the external hook */
   732 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, info->pCmd, CommandCallback, false);
   733 		}
   734 	}
   735 	
   736 	/* Remove from list */
   737 	m_CmdList.remove(info);
   738 
   739 	delete info;
   740 }
   741 
   742 ConCmdInfo *ConCmdManager::AddOrFindCommand(const char *name, const char *description, int flags)
   743 {
   744 	ConCmdInfo *pInfo;
   745 	if (!sm_trie_retrieve(m_pCmds, name, (void **)&pInfo))
   746 	{
   747 		pInfo = new ConCmdInfo();
   748 		/* Find the commandopan */
   749 		ConCommandBase *pBase = icvar->GetCommands();
   750 		ConCommand *pCmd = NULL;
   751 		while (pBase)
   752 		{
   753 			if (strcmp(pBase->GetName(), name) == 0)
   754 			{
   755 				/* Don't want to return convar with same name */
   756 				if (!pBase->IsCommand())
   757 				{
   758 					return NULL;
   759 				}
   760 
   761 				pCmd = (ConCommand *)pBase;
   762 				break;
   763 			}
   764 			pBase = const_cast<ConCommandBase *>(pBase->GetNext());
   765 		}
   766 
   767 		if (!pCmd)
   768 		{
   769 			/* Note that we have to duplicate because the source might not be 
   770 			 * a static string, and these expect static memory.
   771 			 */
   772 			if (!description)
   773 			{
   774 				description = "";
   775 			}
   776 			char *new_name = sm_strdup(name);
   777 			char *new_help = sm_strdup(description);
   778 			pCmd = new ConCommand(new_name, CommandCallback, new_help, flags);
   779 			pInfo->sourceMod = true;
   780 		} else {
   781 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CommandCallback, false);
   782 		}
   783 
   784 		pInfo->pCmd = pCmd;
   785 
   786 		sm_trie_insert(m_pCmds, name, pInfo);
   787 		AddToCmdList(pInfo);
   788 	}
   789 
   790 	return pInfo;
   791 }
   792 
   793 void ConCmdManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
   794 {
   795 	if (argcount >= 3)
   796 	{
   797 		const char *text = engine->Cmd_Argv(2);
   798 
   799 		if (strcmp(text, "internal") == 0)
   800 		{
   801 			const char *num = engine->Cmd_Argv(3);
   802 			if (atoi(num) == 1)
   803 			{
   804 				g_ConCmds.NotifyExecDone(NULL);
   805 			}
   806 		}
   807 
   808 		int id = atoi(text);
   809 		CPlugin *pPlugin = g_PluginSys.GetPluginByOrder(id);
   810 
   811 		if (!pPlugin)
   812 		{
   813 			g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
   814 			return;
   815 		}
   816 
   817 		const sm_plugininfo_t *plinfo = pPlugin->GetPublicInfo();
   818 		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : pPlugin->GetFilename();
   819 
   820 		CmdList *pList;
   821 		if (!pPlugin->GetProperty("CommandList", (void **)&pList))
   822 		{
   823 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   824 			return;
   825 		}
   826 		if (!pList->size())
   827 		{
   828 			g_RootMenu.ConsolePrint("[SM] No commands found for: %s", plname);
   829 			return;
   830 		}
   831 
   832 		CmdList::iterator iter;
   833 		const char *type = NULL;
   834 		const char *name;
   835 		const char *help;
   836 		g_RootMenu.ConsolePrint("[SM] Listing %d commands for: %s", pList->size(), plname);
   837 		g_RootMenu.ConsolePrint("  %-17.16s %-8.7s %s", "[Name]", "[Type]", "[Help]");
   838 		for (iter=pList->begin();
   839 			 iter!=pList->end();
   840 			 iter++, id++)
   841 		{
   842 			PlCmdInfo &cmd = (*iter);
   843 			if (cmd.type == Cmd_Server)
   844 			{
   845 				type = "server";
   846 			} else if (cmd.type == Cmd_Console) {
   847 				type = "console";
   848 			} else if (cmd.type == Cmd_Admin) {
   849 				type = "admin";
   850 			}
   851 			name = cmd.pInfo->pCmd->GetName();
   852 			if (cmd.pHook->helptext.size())
   853 			{
   854 				help = cmd.pHook->helptext.c_str();
   855 			} else {
   856 				help = cmd.pInfo->pCmd->GetHelpText();
   857 			}
   858 			g_RootMenu.ConsolePrint("  %-17.16s %-12.11s %s", name, type, help);		
   859 		}
   860 
   861 		return;
   862 	}
   863 
   864 	g_RootMenu.ConsolePrint("[SM] Usage: sm cmds <plugin #>");
   865 }
   866 
   867 static int g_yam_state = 0;
   868 void _YamState(int state)
   869 {
   870 	if (state == 0)
   871 	{
   872 		g_RootMenu.ConsolePrint("Welcome to the SourceMod Text Adventure.");
   873 		g_RootMenu.ConsolePrint("Type sm_text to see the last message.");
   874 		g_RootMenu.ConsolePrint("Type sm_text <word> to follow a capital word.");
   875 		g_RootMenu.ConsolePrint("-------------------------------");
   876 		g_RootMenu.ConsolePrint("You are at VALVE HEADQUARTERS.");
   877 		g_RootMenu.ConsolePrint("To your LEFT is BAILOPAN, rearranging the letters to spell");
   878 		g_RootMenu.ConsolePrint("\"A HARD VEAL QUEST\".  FORWARD is the entrance to the building.");
   879 		g_RootMenu.ConsolePrint("To your RIGHT is your last chance to flee in terror.");
   880 	} else if (state == 1) {
   881 		g_RootMenu.ConsolePrint("BAILOPAN tells you that his name his pronounced");
   882 		g_RootMenu.ConsolePrint("\"bye low pahn,\" not \"bay low pan.\"  Do you ");
   883 		g_RootMenu.ConsolePrint("MOCK him, or NOD quietly?");
   884 	} else if (state == 2) {
   885 		g_RootMenu.ConsolePrint("You enter the Valve building.  You hear screams coming from within.");
   886 		g_RootMenu.ConsolePrint("A grotesque figure lumbers up to greet you; it is Gabe Newell.");
   887 		g_RootMenu.ConsolePrint("\"Welcome,\" he belches, \"to my lair.\"");
   888 		g_RootMenu.ConsolePrint("Do you SHAKE Gaben's hand, WALK past him, or OFFER a donut?");
   889 	} else if (state == 3) {
   890 		g_RootMenu.ConsolePrint("You walk into the break room.  Alfred \"Adolf\" Reynolds and");
   891 		g_RootMenu.ConsolePrint("Yahn \"Yeti\" Bernier are discussing something (you overhear ");
   892 		g_RootMenu.ConsolePrint("the phrase \"and next Steam update, here's what we should break\").");
   893 		g_RootMenu.ConsolePrint("Should you DIE in a fire, REPORT a bug, REQUEST a feature, or ");
   894 		g_RootMenu.ConsolePrint("SPRAY them with butter?");
   895 	}
   896 	g_yam_state = state;
   897 }
   898 
   899 void _IntExt_CallYams()
   900 {
   901 	const char *arg = engine->Cmd_Argv(1);
   902 
   903 	/* should be impossible */
   904 	if (!arg || arg[0] == '\0')
   905 	{
   906 		_YamState(g_yam_state);
   907 	}
   908 
   909 	if (g_yam_state == 1)
   910 	{
   911 		if (strcasecmp(arg, "mock") == 0)
   912 		{
   913 			g_RootMenu.ConsolePrint("You mock BAILOPAN's pronounciation.  In a fit of rage, ");
   914 			g_RootMenu.ConsolePrint("he sticks an INT 3 call into your chest, rendering you broken.");
   915 			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
   916 			g_yam_state = 0;
   917 		} else if (strcasecmp(arg, "nod") == 0) {
   918 			g_RootMenu.ConsolePrint("You nod quietly, and then slowly back away into the Valve headquarters.");
   919 			_YamState(2);
   920 		} else {
   921 			g_RootMenu.ConsolePrint("Commands are MOCK and NOD.");
   922 		}
   923 	} else if (g_yam_state == 3) {
   924 		if (strcasecmp(arg, "report") == 0)
   925 		{
   926 			g_RootMenu.ConsolePrint("You report a bug to Alfred and Yeti.  Immediately, both fall asleep.");
   927 			g_RootMenu.ConsolePrint("You decay in the break room for two years while they sleep.");
   928 			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
   929 			g_yam_state = 0;
   930 		} else if (strcasecmp(arg, "request") == 0) {
   931 			g_RootMenu.ConsolePrint("You request a feature to Alfred and Yeti.  They both mutter something");
   932 			g_RootMenu.ConsolePrint("about it being implemented \"soon.\"  Then, by accident, someone sends");
   933 			g_RootMenu.ConsolePrint("a message over \"Friends.\"  The entire building catches fire.");
   934 			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
   935 			g_yam_state = 0;
   936 		} else if (strcasecmp(arg, "die") == 0) {
   937 			g_RootMenu.ConsolePrint("For no reason, you suddenly catch fire.  Alfred and Yeti find this");
   938 			g_RootMenu.ConsolePrint("deeply disturbing, and cover your flaming corpse with Episode 2");
   939 			g_RootMenu.ConsolePrint("advertisements.  Coming soon, with Team Fortress 2, and Portal!");
   940 			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
   941 			g_yam_state = 0;
   942 		} else if (strcasecmp(arg, "spray") == 0) {
   943 			g_RootMenu.ConsolePrint("You spray Alfred and Yeti with butter.  Like Jack Thompson to an");
   944 			g_RootMenu.ConsolePrint("ambulance, Gabe Newell instantly appears and devours them both.");
   945 			g_RootMenu.ConsolePrint("You run away, just in time, as Gabe Newell explodes, registering ");
   946 			g_RootMenu.ConsolePrint("a 5.6 earthquake.  Outside, world peace is achieved.");
   947 			g_RootMenu.ConsolePrint("YOU HAVE WON.");
   948 			g_yam_state = 0;
   949 		}
   950 	} else if (g_yam_state == 2) {
   951 		if (strcasecmp(arg, "shake") == 0)
   952 		{
   953 			g_RootMenu.ConsolePrint("You shake Gaben's hand.  It is a terrifying and disgusting experience.");
   954 			g_RootMenu.ConsolePrint("However, you survive, and continue on.");
   955 			_YamState(3);
   956 		} else if (strcasecmp(arg, "offer") == 0) {
   957 			g_RootMenu.ConsolePrint("You offer Gabe Newell one (1) donut.  With a gleam in his eyes, ");
   958 			g_RootMenu.ConsolePrint("he picks you up and devours you whole.");
   959 			g_RootMenu.ConsolePrint("YOU HAVE DIED.  GAME OVER.");
   960 			g_yam_state = 0;
   961 		} else if (strcasecmp(arg, "walk") == 0) {
   962 			g_RootMenu.ConsolePrint("You walk past Gabe Newell.  He can't keep up with your pace!");
   963 			_YamState(3);
   964 		} else {
   965 			g_RootMenu.ConsolePrint("Commands are SHAKE, OFFER, and WALK.");
   966 		}
   967 	} else if (g_yam_state == 0) {
   968 		if (strcasecmp(arg, "left") == 0)
   969 		{
   970 			_YamState(1);
   971 		} else if (strcasecmp(arg, "right") == 0) {
   972 			g_RootMenu.ConsolePrint("You run away from the Valve headquarters in sheer terror.");
   973 			g_RootMenu.ConsolePrint("While running, you smash into an unknown person, who turns out to be your soul mate.");
   974 			g_RootMenu.ConsolePrint("You marry and raise a family of 3 kids.");
   975 			g_RootMenu.ConsolePrint("Many years later, you look back, and realize this was your best choice.");
   976 			g_RootMenu.ConsolePrint("YOU HAVE WON.");
   977 			g_yam_state = 0;
   978 		} else if (strcasecmp(arg, "forward") == 0) {
   979 			_YamState(2);
   980 		} else if (arg[0] != '\0') {
   981 			g_RootMenu.ConsolePrint("Commands are FORWARD, LEFT, and RIGHT.");
   982 		}
   983 	}
   984 }
   985 
   986 void _IntExt_EnableYams()
   987 {
   988 	static ConCommand *pCmd = NULL;
   989 	if (!pCmd)
   990 	{
   991 		pCmd = new ConCommand("sm_text", _IntExt_CallYams, "Fountain of Yams Adventure Game", FCVAR_GAMEDLL);
   992 		g_RootMenu.ConsolePrint("Something is now enabled...");
   993 	} else {
   994 		g_RootMenu.ConsolePrint("Something is already enabled...");
   995 	}
   996 }