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