core/ConVarManager.cpp
author David Anderson <dvander@alliedmods.net>
Wed Sep 26 14:38:40 2007 +0000 (2007-09-26)
changeset 1488 5562086c65c8
parent 1355 c15901d5c40b
child 1541 e46ad208b9b4
permissions -rw-r--r--
initial import of UNTESTED, UNFINISHED topmenus extension
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "ConVarManager.h"
    33 #include "HalfLife2.h"
    34 #include "PluginSys.h"
    35 #include "ForwardSys.h"
    36 #include "HandleSys.h"
    37 #include "sm_srvcmds.h"
    38 #include "sm_stringutil.h"
    39 #include <sh_vector.h>
    40 
    41 ConVarManager g_ConVarManager;
    42 
    43 SH_DECL_HOOK5_void(IServerGameDLL, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
    44 SH_DECL_HOOK5_void(IServerPluginCallbacks, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
    45 
    46 const ParamType CONVARCHANGE_PARAMS[] = {Param_Cell, Param_String, Param_String};
    47 typedef List<const ConVar *> ConVarList;
    48 
    49 ConVarManager::ConVarManager() : m_ConVarType(0), m_VSPIface(NULL), m_CanQueryConVars(false)
    50 {
    51 #if PLAPI_VERSION < 12
    52 	m_IgnoreHandle = false;
    53 #endif
    54 
    55 	/* Create a convar lookup trie */
    56 	m_ConVarCache = sm_trie_create();
    57 }
    58 
    59 ConVarManager::~ConVarManager()
    60 {
    61 	List<ConVarInfo *>::iterator iter;
    62 
    63 	/* Destroy our convar lookup trie */
    64 	sm_trie_destroy(m_ConVarCache);
    65 
    66 	/* Destroy all the ConVarInfo structures */
    67 	for (iter = m_ConVars.begin(); iter != m_ConVars.end(); iter++)
    68 	{
    69 		delete (*iter);
    70 	}
    71 
    72 	m_ConVars.clear();
    73 }
    74 
    75 void ConVarManager::OnSourceModAllInitialized()
    76 {
    77 	/* Only valid with ServerGameDLL006 or greater */
    78 	if (g_SMAPI->GetGameDLLVersion() >= 6)
    79 	{
    80 		SH_ADD_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
    81 		m_CanQueryConVars = true;
    82 	}
    83 
    84 	HandleAccess sec;
    85 
    86 	/* Set up access rights for the 'ConVar' handle type */
    87 	sec.access[HandleAccess_Read] = 0;
    88 	sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
    89 	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
    90 
    91 	/* Create the 'ConVar' handle type */
    92 	m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL);
    93 
    94 	/* Add the 'convars' option to the 'sm' console command */
    95 	g_RootMenu.AddRootConsoleCommand("cvars", "View convars created by a plugin", this);
    96 }
    97 
    98 void ConVarManager::OnSourceModShutdown()
    99 {
   100 	if (m_CanQueryConVars)
   101 	{
   102 		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
   103 		SH_REMOVE_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, m_VSPIface, this, &ConVarManager::OnQueryCvarValueFinished, false);
   104 	}
   105 
   106 	IChangeableForward *fwd;
   107 	List<ConVarInfo *>::iterator i;
   108 
   109 	/* Iterate list of ConVarInfo structures */
   110 	for (i = m_ConVars.begin(); i != m_ConVars.end(); i++)
   111 	{
   112 		fwd = (*i)->pChangeForward;
   113 
   114 		/* Free any convar-change forwards that still exist */
   115 		if (fwd)
   116 		{
   117 			g_Forwards.ReleaseForward(fwd);
   118 		}
   119 	}
   120 
   121 	/* Remove the 'convars' option from the 'sm' console command */
   122 	g_RootMenu.RemoveRootConsoleCommand("cvars", this);
   123 
   124 	/* Remove the 'ConVar' handle type */
   125 	g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent);
   126 }
   127 
   128 void ConVarManager::OnSourceModVSPReceived(IServerPluginCallbacks *iface)
   129 {
   130 	/* This will be called after OnSourceModAllInitialized(), so...
   131 	 *
   132 	 * If we haven't been able to hook the IServerGameDLL version at this point,
   133 	 * then use hook IServerPluginCallbacks version from the engine.
   134 	 */
   135 
   136 	/* The Ship currently only supports ServerPluginCallbacks001, but we need 002 */
   137 	if (g_IsOriginalEngine)
   138 	{
   139 		return;
   140 	}
   141 
   142 	if (!m_CanQueryConVars)
   143 	{
   144 		m_VSPIface = iface;
   145 
   146 		SH_ADD_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, iface, this, &ConVarManager::OnQueryCvarValueFinished, false);
   147 		m_CanQueryConVars = true;
   148 	}
   149 }
   150 
   151 #if PLAPI_VERSION >= 12
   152 void ConVarManager::OnUnlinkConCommandBase(PluginId id, ConCommandBase *pCommand)
   153 {
   154 	/* Only check convars that have not been created by SourceMod's core */
   155 	if (id != g_PLID && !pCommand->IsCommand())
   156 	{
   157 		ConVarInfo *pInfo;
   158 		const char *cvarName = pCommand->GetName();
   159 		HandleSecurity sec(NULL, g_pCoreIdent);
   160 		bool handleExists = sm_trie_retrieve(m_ConVarCache, cvarName, reinterpret_cast<void **>(&pInfo));
   161 
   162 		if (handleExists)
   163 		{
   164 			g_HandleSys.FreeHandle(pInfo->handle, &sec);
   165 			sm_trie_delete(m_ConVarCache, cvarName);
   166 			m_ConVars.remove(pInfo);
   167 		}
   168 	}
   169 }
   170 
   171 #else
   172 
   173 /* I truly detest this code */
   174 void ConVarManager::OnMetamodPluginUnloaded(PluginId id)
   175 {
   176 	ConVarInfo *pInfo;
   177 	const char *cvarName;
   178 	HandleSecurity sec(NULL, g_pCoreIdent);
   179 
   180 	List<ConVarInfo *>::iterator i;
   181 	for (i = m_ConVars.begin(); i != m_ConVars.end(); i++)
   182 	{
   183 		pInfo = (*i);
   184 		cvarName = pInfo->name;
   185 
   186 		if (cvarName && !icvar->FindVar(cvarName))
   187 		{
   188 			m_IgnoreHandle = true;
   189 			g_HandleSys.FreeHandle(pInfo->handle, &sec);
   190 
   191 			sm_trie_delete(m_ConVarCache, cvarName);
   192 			m_ConVars.erase(i);
   193 
   194 			delete [] cvarName;
   195 		}
   196 	}
   197 }
   198 #endif
   199 
   200 void ConVarManager::OnPluginUnloaded(IPlugin *plugin)
   201 {
   202 	ConVarList *pConVarList;
   203 
   204 	/* If plugin has a convar list, free its memory */
   205 	if (plugin->GetProperty("ConVarList", (void **)&pConVarList, true))
   206 	{
   207 		delete pConVarList;
   208 	}
   209 }
   210 
   211 void ConVarManager::OnHandleDestroy(HandleType_t type, void *object)
   212 {
   213 #if PLAPI_VERSION < 12
   214 	/* Lovely workaround for our workaround! */
   215 	if (m_IgnoreHandle)
   216 	{
   217 		m_IgnoreHandle = false;
   218 		return;
   219 	}
   220 #endif
   221 
   222 	ConVarInfo *info;
   223 	ConVar *pConVar = static_cast<ConVar *>(object);
   224 
   225 	/* Find convar in lookup trie */
   226 	sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), reinterpret_cast<void **>(&info));
   227 
   228 	/* If convar was created by SourceMod plugin... */
   229 	if (info->sourceMod)
   230 	{
   231 		/* Delete string allocations */
   232 		delete [] pConVar->GetName(); 
   233 		delete [] pConVar->GetDefault();
   234 		delete [] pConVar->GetHelpText();
   235 
   236 		/* Then unlink it from SourceMM */
   237 		g_SMAPI->UnregisterConCmdBase(g_PLAPI, pConVar);
   238 	}
   239 }
   240 
   241 void ConVarManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
   242 {
   243 	if (argcount >= 3)
   244 	{
   245 		/* Get plugin index that was passed */
   246 		int id = atoi(g_RootMenu.GetArgument(2));
   247 		
   248 		/* Get plugin object */
   249 		CPlugin *plugin = g_PluginSys.GetPluginByOrder(id);
   250 
   251 		if (!plugin)
   252 		{
   253 			g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
   254 			return;
   255 		}
   256 
   257 		/* Get plugin name */
   258 		const sm_plugininfo_t *plinfo = plugin->GetPublicInfo();
   259 		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : plugin->GetFilename();
   260 
   261 		ConVarList *pConVarList;
   262 		ConVarList::iterator iter;
   263 
   264 		/* If no convar list... */
   265 		if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
   266 		{
   267 			g_RootMenu.ConsolePrint("[SM] No convars found for: %s", plname);
   268 			return;
   269 		}
   270 
   271 		g_RootMenu.ConsolePrint("[SM] Listing %d convars for: %s", pConVarList->size(), plname);
   272 		g_RootMenu.ConsolePrint("  %-32.31s %s", "[Name]", "[Value]");
   273 
   274 		/* Iterate convar list and display each one */
   275 		for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
   276 		{
   277 			const ConVar *pConVar = (*iter);
   278 			g_RootMenu.ConsolePrint("  %-32.31s %s", pConVar->GetName(), pConVar->GetString()); 
   279 		}
   280 
   281 		return;
   282 	}
   283 
   284 	/* Display usage of subcommand */
   285 	g_RootMenu.ConsolePrint("[SM] Usage: sm convars <plugin #>");
   286 }
   287 
   288 Handle_t ConVarManager::CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal, const char *description, int flags, bool hasMin, float min, bool hasMax, float max)
   289 {
   290 	ConVar *pConVar = NULL;
   291 	ConVarInfo *pInfo = NULL;
   292 	Handle_t hndl = 0;
   293 
   294 	/* Find out if the convar exists already */
   295 	pConVar = icvar->FindVar(name);
   296 
   297 	/* If the convar already exists... */
   298 	if (pConVar)
   299 	{
   300 		/* Add convar to plugin's list */
   301 		AddConVarToPluginList(pContext, pConVar);
   302 
   303 		/* First find out if we already have a handle to it */
   304 		if (sm_trie_retrieve(m_ConVarCache, name, (void **)&pInfo))
   305 		{
   306 			return pInfo->handle;
   307 		} else {
   308 			/* If we don't, then create a new handle from the convar */
   309 			hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
   310 
   311 			/* Create and initialize ConVarInfo structure */
   312 			pInfo = new ConVarInfo();
   313 			pInfo->handle = hndl;
   314 			pInfo->sourceMod = false;
   315 			pInfo->pChangeForward = NULL;
   316 			pInfo->origCallback = pConVar->GetCallback();
   317 		#if PLAPI_VERSION < 12
   318 			pInfo->name = sm_strdup(pConVar->GetName());
   319 		#endif
   320 
   321 			/* Insert struct into caches */
   322 			m_ConVars.push_back(pInfo);
   323 			sm_trie_insert(m_ConVarCache, name, pInfo);
   324 
   325 			return hndl;
   326 		}
   327 	}
   328 
   329 	/* To prevent creating a convar that has the same name as a console command... ugh */
   330 	ConCommandBase *pBase = icvar->GetCommands();
   331 
   332 	while (pBase)
   333 	{
   334 		if (pBase->IsCommand() && strcmp(pBase->GetName(), name) == 0)
   335 		{
   336 			return BAD_HANDLE;
   337 		}
   338 
   339 		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
   340 	}
   341 
   342 	/* Since an existing convar (or concmd with the same name) was not found , now we can finally create it */
   343 	pConVar = new ConVar(sm_strdup(name), sm_strdup(defaultVal), flags, sm_strdup(description), hasMin, min, hasMax, max);
   344 
   345 	/* Add convar to plugin's list */
   346 	AddConVarToPluginList(pContext, pConVar);
   347 
   348 	/* Create a handle from the new convar */
   349 	hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
   350 
   351 	/* Create and initialize ConVarInfo structure */
   352 	pInfo = new ConVarInfo();
   353 	pInfo->handle = hndl;
   354 	pInfo->sourceMod = true;
   355 	pInfo->pChangeForward = NULL;
   356 	pInfo->origCallback = NULL;
   357 #if PLAPI_VERSION < 12
   358 	pInfo->name = NULL;
   359 #endif
   360 
   361 	/* Insert struct into caches */
   362 	m_ConVars.push_back(pInfo);
   363 	sm_trie_insert(m_ConVarCache, name, pInfo);
   364 
   365 	return hndl;
   366 }
   367 
   368 Handle_t ConVarManager::FindConVar(const char *name)
   369 {
   370 	ConVar *pConVar = NULL;
   371 	ConVarInfo *pInfo;
   372 	Handle_t hndl;
   373 
   374 	/* Search for convar */
   375 	pConVar = icvar->FindVar(name);
   376 
   377 	/* If it doesn't exist, then return an invalid handle */
   378 	if (!pConVar)
   379 	{
   380 		return BAD_HANDLE;
   381 	}
   382 
   383 	/* At this point, the convar exists. So, find out if we already have a handle */
   384 	if (sm_trie_retrieve(m_ConVarCache, name, (void **)&pInfo))
   385 	{
   386 		return pInfo->handle;
   387 	}
   388 
   389 	/* If we don't have a handle, then create a new one */
   390 	hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
   391 
   392 	/* Create and initialize ConVarInfo structure */
   393 	pInfo = new ConVarInfo();
   394 	pInfo->handle = hndl;
   395 	pInfo->sourceMod = false;
   396 	pInfo->pChangeForward = NULL;
   397 	pInfo->origCallback = pConVar->GetCallback();
   398 #if PLAPI_VERSION < 12
   399 	pInfo->name = sm_strdup(pConVar->GetName());
   400 #endif
   401 
   402 	/* Insert struct into our caches */
   403 	m_ConVars.push_back(pInfo);
   404 	sm_trie_insert(m_ConVarCache, name, pInfo);
   405 
   406 	return hndl;
   407 }
   408 
   409 void ConVarManager::HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
   410 {
   411 	ConVarInfo *pInfo;
   412 	IChangeableForward *pForward;
   413 
   414 	/* Find the convar in the lookup trie */
   415 	if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo))
   416 	{
   417 		/* Get the forward */
   418 		pForward = pInfo->pChangeForward;
   419 
   420 		/* If forward does not exist, create it */
   421 		if (!pForward)
   422 		{
   423 			pForward = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, CONVARCHANGE_PARAMS);
   424 			pInfo->pChangeForward = pForward;
   425 
   426 			/* Install our own callback */
   427 			pConVar->InstallChangeCallback(OnConVarChanged);
   428 		}
   429 
   430 		/* Add function to forward's list */
   431 		pForward->AddFunction(pFunction);
   432 	}
   433 }
   434 
   435 void ConVarManager::UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
   436 {
   437 	ConVarInfo *pInfo;
   438 	IChangeableForward *pForward;
   439 	IPluginContext *pContext = pFunction->GetParentContext();
   440 
   441 	/* Find the convar in the lookup trie */
   442 	if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo))
   443 	{
   444 		/* Get the forward */
   445 		pForward = pInfo->pChangeForward;
   446 
   447 		/* If the forward doesn't exist, we can't unhook anything */
   448 		if (!pForward)
   449 		{
   450 			pContext->ThrowNativeError("Convar \"%s\" has no active hook", pConVar->GetName());
   451 			return;
   452 		}
   453 
   454 		/* Remove the function from the forward's list */
   455 		if (!pForward->RemoveFunction(pFunction))
   456 		{
   457 			pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", pConVar->GetName());
   458 			return;
   459 		}
   460 
   461 		/* If the forward now has 0 functions in it... */
   462 		if (pForward->GetFunctionCount() == 0)
   463 		{
   464 			/* Free this forward */
   465 			g_Forwards.ReleaseForward(pForward);
   466 			pInfo->pChangeForward = NULL;
   467 
   468 			/* Put back the original convar callback */
   469 			pConVar->InstallChangeCallback(pInfo->origCallback);
   470 		}
   471 	}
   472 }
   473 
   474 QueryCvarCookie_t ConVarManager::QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback, Handle_t hndl)
   475 {
   476 	QueryCvarCookie_t cookie;
   477 
   478 	/* Call StartQueryCvarValue() in either the IVEngineServer or IServerPluginHelpers depending on situation */
   479 	if (!m_VSPIface)
   480 	{
   481 		cookie = engine->StartQueryCvarValue(pPlayer, name);	
   482 	} else {
   483 		cookie = serverpluginhelpers->StartQueryCvarValue(pPlayer, name);
   484 	}
   485 
   486 	ConVarQuery query = {cookie, pCallback, hndl};
   487 	m_ConVarQueries.push_back(query);
   488 
   489 	return cookie;
   490 }
   491 
   492 void ConVarManager::AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar)
   493 {
   494 	ConVarList *pConVarList;
   495 	ConVarList::iterator iter;
   496 	bool inserted = false;
   497 	const char *orig = pConVar->GetName();
   498 
   499 	IPlugin *plugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
   500 
   501 	/* Check plugin for an existing convar list */
   502 	if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
   503 	{
   504 		pConVarList = new ConVarList();
   505 		plugin->SetProperty("ConVarList", pConVarList);
   506 	} else if (pConVarList->find(pConVar) != pConVarList->end()) {
   507 		/* If convar is already in list, then don't add it */
   508 		return;
   509 	}
   510 
   511 	/* Insert convar into list which is sorted alphabetically */
   512 	for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
   513 	{
   514 		if (strcmp(orig, (*iter)->GetName()) < 0)
   515 		{
   516 			pConVarList->insert(iter, pConVar);
   517 			inserted = true;
   518 			break;
   519 		}
   520 	}
   521 
   522 	if (!inserted)
   523 	{
   524 		pConVarList->push_back(pConVar);
   525 	}
   526 }
   527 
   528 void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue)
   529 {
   530 	/* If the values are the same, exit early in order to not trigger callbacks */
   531 	if (strcmp(pConVar->GetString(), oldValue) == 0)
   532 	{
   533 		return;
   534 	}
   535 
   536 	Trie *pCache = g_ConVarManager.GetConVarCache();
   537 	ConVarInfo *pInfo;
   538 
   539 	/* Find the convar in the lookup trie */
   540 	sm_trie_retrieve(pCache, pConVar->GetName(), (void **)&pInfo);
   541 
   542 	FnChangeCallback origCallback = pInfo->origCallback;
   543 	IChangeableForward *pForward = pInfo->pChangeForward;
   544 
   545 	/* If there was a change callback installed previously, call it */
   546 	if (origCallback)
   547 	{
   548 		origCallback(pConVar, oldValue);
   549 	}
   550 
   551 	/* Now call forwards in plugins that have hooked this */
   552 	pForward->PushCell(pInfo->handle);
   553 	pForward->PushString(oldValue);
   554 	pForward->PushString(pConVar->GetString());
   555 	pForward->Execute(NULL);
   556 }
   557 
   558 void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
   559 {
   560 	IPluginFunction *pCallback = NULL;
   561 	cell_t value = 0;
   562 	List<ConVarQuery>::iterator iter;
   563 
   564 	for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end(); iter++)
   565 	{
   566 		ConVarQuery &query = (*iter);
   567 		if (query.cookie == cookie)
   568 		{
   569 			pCallback = query.pCallback;
   570 			value = query.value;
   571 			break;
   572 		}
   573 	}
   574 
   575 	if (pCallback)
   576 	{
   577 		cell_t ret;
   578 
   579 		pCallback->PushCell(cookie);
   580 		pCallback->PushCell(engine->IndexOfEdict(pPlayer));
   581 		pCallback->PushCell(result);
   582 		pCallback->PushString(cvarName);
   583 
   584 		if (result == eQueryCvarValueStatus_ValueIntact)
   585 		{
   586 			pCallback->PushString(cvarValue);
   587 		} else {
   588 			pCallback->PushString("\0");
   589 		}
   590 
   591 		pCallback->PushCell(value);
   592 		pCallback->Execute(&ret);
   593 
   594 		m_ConVarQueries.erase(iter);
   595 	}
   596 }
   597 
   598 
   599 static int s_YamagramState = 0;
   600 
   601 void _YamagramPrinterTwoPointOhOh(int yamagram)
   602 {
   603 	switch (yamagram)
   604 	{
   605 	case 0:
   606 		g_RootMenu.ConsolePrint("Answer the following questions correctly and Gaben may not eat you after all.");
   607 		g_RootMenu.ConsolePrint("You will be given one hint in the form of my patented yamagrams.");
   608 		g_RootMenu.ConsolePrint("Type sm_nana to see the last question.");
   609 		g_RootMenu.ConsolePrint("Type sm_nana <answer> to attempt an answer of the question.");
   610 		g_RootMenu.ConsolePrint("-------------------------------");
   611 		_YamagramPrinterTwoPointOhOh(1);
   612 		return;
   613 	case 1:
   614 		g_RootMenu.ConsolePrint("Question Ichi (1)");
   615 		g_RootMenu.ConsolePrint("One can turn into a cow by doing what action?");
   616 		g_RootMenu.ConsolePrint("Hint: AGE SANS GRIT");
   617 		break;
   618 	case 2:
   619 		g_RootMenu.ConsolePrint("Question Ni (2)");
   620 		g_RootMenu.ConsolePrint("What kind of hat should you wear when using the Internet?");
   621 		g_RootMenu.ConsolePrint("Hint: BRR MOOSE");
   622 		break;
   623 	case 3:
   624 		g_RootMenu.ConsolePrint("Question San (3)");
   625 		g_RootMenu.ConsolePrint("Who is the lead developer of SourceMod?");
   626 		g_RootMenu.ConsolePrint("Hint: VEAL BANDANA DID RIP SOON");
   627 		break;
   628 	case 4:
   629 		g_RootMenu.ConsolePrint("Question Yon (4)");
   630 		g_RootMenu.ConsolePrint("A terrible translation of 'SVN Revision' to Japanese romaji might be ...");
   631 		g_RootMenu.ConsolePrint("Hint: I TAKE IN AN AIR OK");
   632 		break;
   633 	case 5:
   634 		g_RootMenu.ConsolePrint("Question Go (5)");
   635 		g_RootMenu.ConsolePrint("What is a fundamental concept in the game of Go?");
   636 		g_RootMenu.ConsolePrint("Hint: AD LADEN THIEF");
   637 		break;
   638 	case 6:
   639 		g_RootMenu.ConsolePrint("Question Roku (6)");
   640 		g_RootMenu.ConsolePrint("Why am I asking all these strange questions?");
   641 		g_RootMenu.ConsolePrint("Hint: CHUBBY TITAN EATS EWE WAGE DATA");
   642 		break;
   643 	case 7:
   644 		g_RootMenu.ConsolePrint("Question Nana (7)");
   645 		g_RootMenu.ConsolePrint("What is my name?");
   646 		g_RootMenu.ConsolePrint("Hint: AD MODE LAG US");
   647 		break;
   648 	default:
   649 		break;
   650 	}
   651 
   652 	s_YamagramState = yamagram;
   653 }
   654 
   655 void _IntExt_CallYamagrams()
   656 {
   657 	bool correct = false;
   658 	const char *arg = engine->Cmd_Args();
   659 
   660 	if (!arg || arg[0] == '\0')
   661 	{
   662 		_YamagramPrinterTwoPointOhOh(s_YamagramState);
   663 		return;
   664 	}
   665 
   666 	switch (s_YamagramState)
   667 	{
   668 	case 1:
   669 		correct = !strcasecmp(arg, "eating grass");
   670 		break;
   671 	case 2:
   672 		correct = !strcasecmp(arg, "sombrero");
   673 		break;
   674 	case 3:
   675 		correct = !strcasecmp(arg, "david bailopan anderson");
   676 		break;
   677 	case 4:
   678 		correct = !strcasecmp(arg, "kaitei no kairan");
   679 		break;
   680 	case 5:
   681 		correct = !strcasecmp(arg, "life and death");
   682 		break;
   683 	case 6:
   684 		correct = !strcasecmp(arg, "because gabe wanted it that way");
   685 		if (correct)
   686 		{
   687 			g_RootMenu.ConsolePrint("Congratulations, you have answered 6 of my questions.");
   688 			g_RootMenu.ConsolePrint("However, I have one final question for you. It wouldn't be nana without it.");
   689 			g_RootMenu.ConsolePrint("-------------------------------");
   690 			_YamagramPrinterTwoPointOhOh(7);
   691 			return;
   692 		}
   693 		break;
   694 	case 7:
   695 		correct = !strcasecmp(arg, "damaged soul");
   696 		if (correct)
   697 		{
   698 			g_RootMenu.ConsolePrint("You don't know how lucky you are to still be alive!");
   699 			g_RootMenu.ConsolePrint("Congratulations. You have answered all 7 questions correctly.");
   700 			g_RootMenu.ConsolePrint("The SourceMod Dev Team will be at your door with anti-Gaben grenades");
   701 			g_RootMenu.ConsolePrint("within seconds. You will also be provided with a rocket launcher,");
   702 			g_RootMenu.ConsolePrint("just in case Alfred decides to strike you with a blitzkrieg in retaliation.");
   703 
   704 			s_YamagramState = 0;
   705 			return;
   706 		}
   707 		break;
   708 	default:
   709 		break;
   710 	}
   711 
   712 	if (s_YamagramState > 0)
   713 	{
   714 		if (correct)
   715 		{
   716 			g_RootMenu.ConsolePrint("Correct! You are one step closer to avoiding the deadly jaws of Gaben.");
   717 			g_RootMenu.ConsolePrint("-------------------------------");
   718 			s_YamagramState++;
   719 		} else {
   720 			g_RootMenu.ConsolePrint("Wrong! You better be more careful. Gaben may be at your door at any minute.");
   721 			return;
   722 		}
   723 	}
   724 
   725 	_YamagramPrinterTwoPointOhOh(s_YamagramState);
   726 }
   727 
   728 void _IntExt_EnableYamagrams()
   729 {
   730 	static ConCommand *pCmd = NULL;
   731 	if (!pCmd)
   732 	{
   733 		pCmd = new ConCommand("sm_nana", _IntExt_CallYamagrams, "Try these yamagrams!", FCVAR_GAMEDLL);
   734 		g_RootMenu.ConsolePrint("[SM] Warning: Gaben has been alerted of your actions. You may be eaten.");
   735 	} else {
   736 		g_RootMenu.ConsolePrint("[SM] Gaben has already been alerted of your actions...");
   737 	}
   738 }
   739 
   740 void _IntExt_OnHostnameChanged(ConVar *pConVar, char const *oldValue)
   741 {
   742 	if (strcmp(oldValue, "Good morning, DS-san.") == 0
   743 		&& strcmp(pConVar->GetString(), "Good night, talking desk lamp.") == 0)
   744 	{
   745 		_IntExt_EnableYamagrams();
   746 	}
   747 }