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