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