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