core/ConVarManager.cpp
author David Anderson <dvander@alliedmods.net>
Mon Oct 15 00:38:25 2007 +0000 (2007-10-15)
changeset 1568 ecbaee393b0b
parent 1544 feff70182207
child 1578 378b2e4b1f19
permissions -rw-r--r--
extensions can now be built for multiple engines, and will autoload from auto.X.Y folders
     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->UnregisterConCommandBase(g_PLAPI, pConVar);
   238 	}
   239 }
   240 
   241 void ConVarManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   242 {
   243 	int argcount = command.ArgC();
   244 	if (argcount >= 3)
   245 	{
   246 		/* Get plugin index that was passed */
   247 		int id = atoi(command.Arg(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 #if defined ORANGEBOX_BUILD
   530 void ConVarManager::OnConVarChanged(IConVar *pIConVar, const char *oldValue, float flOldValue)
   531 #else
   532 void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue)
   533 #endif
   534 {
   535 	/* If the values are the same, exit early in order to not trigger callbacks */
   536 #if defined ORANGEBOX_BUILD
   537 	ConVar *pConVar = (ConVar *)pIConVar;
   538 #endif
   539 	if (strcmp(pConVar->GetString(), oldValue) == 0)
   540 	{
   541 		return;
   542 	}
   543 
   544 	Trie *pCache = g_ConVarManager.GetConVarCache();
   545 	ConVarInfo *pInfo;
   546 
   547 	/* Find the convar in the lookup trie */
   548 	sm_trie_retrieve(pCache, pConVar->GetName(), (void **)&pInfo);
   549 
   550 	FnChangeCallback_t origCallback = pInfo->origCallback;
   551 	IChangeableForward *pForward = pInfo->pChangeForward;
   552 
   553 	/* If there was a change callback installed previously, call it */
   554 	if (origCallback)
   555 	{
   556 #if defined ORANGEBOX_BUILD
   557 		origCallback(pConVar, oldValue, flOldValue);
   558 #else
   559 		origCallback(pConVar, oldValue);
   560 #endif
   561 	}
   562 
   563 	/* Now call forwards in plugins that have hooked this */
   564 	pForward->PushCell(pInfo->handle);
   565 	pForward->PushString(oldValue);
   566 	pForward->PushString(reinterpret_cast<ConVar *>(pConVar)->GetString());
   567 	pForward->Execute(NULL);
   568 }
   569 
   570 void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
   571 {
   572 	IPluginFunction *pCallback = NULL;
   573 	cell_t value = 0;
   574 	List<ConVarQuery>::iterator iter;
   575 
   576 	for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end(); iter++)
   577 	{
   578 		ConVarQuery &query = (*iter);
   579 		if (query.cookie == cookie)
   580 		{
   581 			pCallback = query.pCallback;
   582 			value = query.value;
   583 			break;
   584 		}
   585 	}
   586 
   587 	if (pCallback)
   588 	{
   589 		cell_t ret;
   590 
   591 		pCallback->PushCell(cookie);
   592 		pCallback->PushCell(engine->IndexOfEdict(pPlayer));
   593 		pCallback->PushCell(result);
   594 		pCallback->PushString(cvarName);
   595 
   596 		if (result == eQueryCvarValueStatus_ValueIntact)
   597 		{
   598 			pCallback->PushString(cvarValue);
   599 		} else {
   600 			pCallback->PushString("\0");
   601 		}
   602 
   603 		pCallback->PushCell(value);
   604 		pCallback->Execute(&ret);
   605 
   606 		m_ConVarQueries.erase(iter);
   607 	}
   608 }
   609 
   610 
   611 static int s_YamagramState = 0;
   612 
   613 void _YamagramPrinterTwoPointOhOh(int yamagram)
   614 {
   615 	switch (yamagram)
   616 	{
   617 	case 0:
   618 		g_RootMenu.ConsolePrint("Answer the following questions correctly and Gaben may not eat you after all.");
   619 		g_RootMenu.ConsolePrint("You will be given one hint in the form of my patented yamagrams.");
   620 		g_RootMenu.ConsolePrint("Type sm_nana to see the last question.");
   621 		g_RootMenu.ConsolePrint("Type sm_nana <answer> to attempt an answer of the question.");
   622 		g_RootMenu.ConsolePrint("-------------------------------");
   623 		_YamagramPrinterTwoPointOhOh(1);
   624 		return;
   625 	case 1:
   626 		g_RootMenu.ConsolePrint("Question Ichi (1)");
   627 		g_RootMenu.ConsolePrint("One can turn into a cow by doing what action?");
   628 		g_RootMenu.ConsolePrint("Hint: AGE SANS GRIT");
   629 		break;
   630 	case 2:
   631 		g_RootMenu.ConsolePrint("Question Ni (2)");
   632 		g_RootMenu.ConsolePrint("What kind of hat should you wear when using the Internet?");
   633 		g_RootMenu.ConsolePrint("Hint: BRR MOOSE");
   634 		break;
   635 	case 3:
   636 		g_RootMenu.ConsolePrint("Question San (3)");
   637 		g_RootMenu.ConsolePrint("Who is the lead developer of SourceMod?");
   638 		g_RootMenu.ConsolePrint("Hint: VEAL BANDANA DID RIP SOON");
   639 		break;
   640 	case 4:
   641 		g_RootMenu.ConsolePrint("Question Yon (4)");
   642 		g_RootMenu.ConsolePrint("A terrible translation of 'SVN Revision' to Japanese romaji might be ...");
   643 		g_RootMenu.ConsolePrint("Hint: I TAKE IN AN AIR OK");
   644 		break;
   645 	case 5:
   646 		g_RootMenu.ConsolePrint("Question Go (5)");
   647 		g_RootMenu.ConsolePrint("What is a fundamental concept in the game of Go?");
   648 		g_RootMenu.ConsolePrint("Hint: AD LADEN THIEF");
   649 		break;
   650 	case 6:
   651 		g_RootMenu.ConsolePrint("Question Roku (6)");
   652 		g_RootMenu.ConsolePrint("Why am I asking all these strange questions?");
   653 		g_RootMenu.ConsolePrint("Hint: CHUBBY TITAN EATS EWE WAGE DATA");
   654 		break;
   655 	case 7:
   656 		g_RootMenu.ConsolePrint("Question Nana (7)");
   657 		g_RootMenu.ConsolePrint("What is my name?");
   658 		g_RootMenu.ConsolePrint("Hint: AD MODE LAG US");
   659 		break;
   660 	default:
   661 		break;
   662 	}
   663 
   664 	s_YamagramState = yamagram;
   665 }
   666 
   667 #if defined ORANGEBOX_BUILD
   668 void _IntExt_CallYamagrams(const CCommand &cmd)
   669 {
   670 #else
   671 void _IntExt_CallYamagrams()
   672 {
   673 	CCommand cmd;
   674 #endif
   675 	bool correct = false;
   676 	const char *arg = cmd.ArgS();
   677 
   678 	if (!arg || arg[0] == '\0')
   679 	{
   680 		_YamagramPrinterTwoPointOhOh(s_YamagramState);
   681 		return;
   682 	}
   683 
   684 	switch (s_YamagramState)
   685 	{
   686 	case 1:
   687 		correct = !strcasecmp(arg, "eating grass");
   688 		break;
   689 	case 2:
   690 		correct = !strcasecmp(arg, "sombrero");
   691 		break;
   692 	case 3:
   693 		correct = !strcasecmp(arg, "david bailopan anderson");
   694 		break;
   695 	case 4:
   696 		correct = !strcasecmp(arg, "kaitei no kairan");
   697 		break;
   698 	case 5:
   699 		correct = !strcasecmp(arg, "life and death");
   700 		break;
   701 	case 6:
   702 		correct = !strcasecmp(arg, "because gabe wanted it that way");
   703 		if (correct)
   704 		{
   705 			g_RootMenu.ConsolePrint("Congratulations, you have answered 6 of my questions.");
   706 			g_RootMenu.ConsolePrint("However, I have one final question for you. It wouldn't be nana without it.");
   707 			g_RootMenu.ConsolePrint("-------------------------------");
   708 			_YamagramPrinterTwoPointOhOh(7);
   709 			return;
   710 		}
   711 		break;
   712 	case 7:
   713 		correct = !strcasecmp(arg, "damaged soul");
   714 		if (correct)
   715 		{
   716 			g_RootMenu.ConsolePrint("You don't know how lucky you are to still be alive!");
   717 			g_RootMenu.ConsolePrint("Congratulations. You have answered all 7 questions correctly.");
   718 			g_RootMenu.ConsolePrint("The SourceMod Dev Team will be at your door with anti-Gaben grenades");
   719 			g_RootMenu.ConsolePrint("within seconds. You will also be provided with a rocket launcher,");
   720 			g_RootMenu.ConsolePrint("just in case Alfred decides to strike you with a blitzkrieg in retaliation.");
   721 
   722 			s_YamagramState = 0;
   723 			return;
   724 		}
   725 		break;
   726 	default:
   727 		break;
   728 	}
   729 
   730 	if (s_YamagramState > 0)
   731 	{
   732 		if (correct)
   733 		{
   734 			g_RootMenu.ConsolePrint("Correct! You are one step closer to avoiding the deadly jaws of Gaben.");
   735 			g_RootMenu.ConsolePrint("-------------------------------");
   736 			s_YamagramState++;
   737 		} else {
   738 			g_RootMenu.ConsolePrint("Wrong! You better be more careful. Gaben may be at your door at any minute.");
   739 			return;
   740 		}
   741 	}
   742 
   743 	_YamagramPrinterTwoPointOhOh(s_YamagramState);
   744 }
   745 
   746 void _IntExt_EnableYamagrams()
   747 {
   748 	static ConCommand *pCmd = NULL;
   749 	if (!pCmd)
   750 	{
   751 		pCmd = new ConCommand("sm_nana", _IntExt_CallYamagrams, "Try these yamagrams!", FCVAR_GAMEDLL);
   752 		g_RootMenu.ConsolePrint("[SM] Warning: Gaben has been alerted of your actions. You may be eaten.");
   753 	} else {
   754 		g_RootMenu.ConsolePrint("[SM] Gaben has already been alerted of your actions...");
   755 	}
   756 }
   757 
   758 #if defined ORANGEBOX_BUILD
   759 void _IntExt_OnHostnameChanged(IConVar *pConVar, const char *oldValue, float flOldValue)
   760 #else
   761 void _IntExt_OnHostnameChanged(ConVar *pConVar, char const *oldValue)
   762 #endif
   763 {
   764 	if (strcmp(oldValue, "Good morning, DS-san.") == 0
   765 		&& strcmp(reinterpret_cast<ConVar *>(pConVar)->GetString(), "Good night, talking desk lamp.") == 0)
   766 	{
   767 		_IntExt_EnableYamagrams();
   768 	}
   769 }