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