core/ConVarManager.cpp
author Scott Ehlert <ds@alliedmods.net>
Wed Nov 19 02:16:45 2008 -0600 (2008-11-19)
changeset 2415 9ac720a36ce4
parent 2405 ce30eb732ba2
child 2583 d3b2571c378b
permissions -rw-r--r--
Merge.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 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 #if SOURCE_ENGINE == SE_EPISODEONE
    45 #define CallGlobalChangeCallbacks	CallGlobalChangeCallback
    46 #endif
    47 
    48 #if SOURCE_ENGINE >= SE_ORANGEBOX
    49 SH_DECL_HOOK3_void(ICvar, CallGlobalChangeCallbacks, SH_NOATTRIB, false, ConVar *, const char *, float);
    50 #else
    51 SH_DECL_HOOK2_void(ICvar, CallGlobalChangeCallbacks, SH_NOATTRIB, false, ConVar *, const char *);
    52 #endif
    53 
    54 SH_DECL_HOOK5_void(IServerGameDLL, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
    55 SH_DECL_HOOK5_void(IServerPluginCallbacks, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
    56 
    57 const ParamType CONVARCHANGE_PARAMS[] = {Param_Cell, Param_String, Param_String};
    58 typedef List<const ConVar *> ConVarList;
    59 KTrie<ConVarInfo *> convar_cache;
    60 
    61 ConVarManager::ConVarManager() : m_ConVarType(0), m_bIsDLLQueryHooked(false), m_bIsVSPQueryHooked(false)
    62 {
    63 }
    64 
    65 ConVarManager::~ConVarManager()
    66 {
    67 }
    68 
    69 void ConVarManager::OnSourceModStartup(bool late)
    70 {
    71 	HandleAccess sec;
    72 
    73 	/* Set up access rights for the 'ConVar' handle type */
    74 	sec.access[HandleAccess_Read] = 0;
    75 	sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
    76 	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
    77 
    78 	/* Create the 'ConVar' handle type */
    79 	m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL);
    80 }
    81 
    82 void ConVarManager::OnSourceModAllInitialized()
    83 {
    84 	/**
    85 	 * Episode 2 has this function by default, but the older versions do not.
    86 	 */
    87 #if SOURCE_ENGINE == SE_EPISODEONE
    88 	if (g_SMAPI->GetGameDLLVersion() >= 6)
    89 	{
    90 		SH_ADD_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
    91 		m_bIsDLLQueryHooked = true;
    92 	}
    93 #endif
    94 
    95 	SH_ADD_HOOK_STATICFUNC(ICvar, CallGlobalChangeCallbacks, icvar, OnConVarChanged, false);
    96 
    97 	/* Add the 'convars' option to the 'sm' console command */
    98 	g_RootMenu.AddRootConsoleCommand("cvars", "View convars created by a plugin", this);
    99 }
   100 
   101 void ConVarManager::OnSourceModShutdown()
   102 {
   103 	List<ConVarInfo *>::iterator iter = m_ConVars.begin();
   104 	HandleSecurity sec(NULL, g_pCoreIdent);
   105 
   106 	/* Iterate list of ConVarInfo structures, remove every one of them */
   107 	while (iter != m_ConVars.end())
   108 	{
   109 		ConVarInfo *pInfo = (*iter);
   110 
   111 		iter = m_ConVars.erase(iter);
   112 
   113 		g_HandleSys.FreeHandle(pInfo->handle, &sec);
   114 		if (pInfo->pChangeForward != NULL)
   115 		{
   116 			g_Forwards.ReleaseForward(pInfo->pChangeForward);
   117 		}
   118 		if (pInfo->sourceMod)
   119 		{
   120 			/* If we created it, we won't be tracking it, therefore it is 
   121 			 * safe to remove everything in one go.
   122 			 */
   123 			META_UNREGCVAR(pInfo->pVar);
   124 			delete [] pInfo->pVar->GetName();
   125 			delete [] pInfo->pVar->GetHelpText();
   126 			delete [] pInfo->pVar->GetDefault();
   127 			delete pInfo->pVar;
   128 		}
   129 		else
   130 		{
   131 			/* If we didn't create it, we might be tracking it.  Also, 
   132 			 * it could be unreadable.
   133 			 */
   134 			UntrackConCommandBase(pInfo->pVar, this);
   135 		}
   136 
   137 		/* It's not safe to read the name here, so we simply delete the 
   138 		 * the info struct and clear the lookup cache at the end.
   139 		 */
   140 		delete pInfo;
   141 	}
   142 	convar_cache.clear();
   143 
   144 	/* Unhook things */
   145 	if (m_bIsDLLQueryHooked)
   146 	{
   147 		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
   148 		m_bIsDLLQueryHooked = false;
   149 	}
   150 	else if (m_bIsVSPQueryHooked)
   151 	{
   152 		SH_REMOVE_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, vsp_interface, this, &ConVarManager::OnQueryCvarValueFinished, false);
   153 		m_bIsVSPQueryHooked = false;
   154 	}
   155 
   156 	SH_REMOVE_HOOK_STATICFUNC(ICvar, CallGlobalChangeCallbacks, icvar, OnConVarChanged, false);
   157 
   158 	/* Remove the 'convars' option from the 'sm' console command */
   159 	g_RootMenu.RemoveRootConsoleCommand("cvars", this);
   160 
   161 	/* Remove the 'ConVar' handle type */
   162 	g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent);
   163 }
   164 
   165 /**
   166  * Orange Box will never use this.
   167  */
   168 void ConVarManager::OnSourceModVSPReceived()
   169 {
   170 	/**
   171 	 * Don't bother if the DLL is already hooked.
   172 	 */
   173 	if (m_bIsDLLQueryHooked)
   174 	{
   175 		return;
   176 	}
   177 
   178 	/* For later MM:S versions, use the updated API, since it's cleaner. */
   179 #if defined METAMOD_PLAPI_VERSION
   180 	int engine = g_SMAPI->GetSourceEngineBuild();
   181 	if (engine == SOURCE_ENGINE_ORIGINAL || vsp_version < 2)
   182 	{
   183 		return;
   184 	}
   185 #else
   186 	if (g_HL2.IsOriginalEngine() || vsp_version < 2)
   187 	{
   188 		return;
   189 	}
   190 #endif
   191 
   192 	SH_ADD_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, vsp_interface, this, &ConVarManager::OnQueryCvarValueFinished, false);
   193 	m_bIsVSPQueryHooked = true;
   194 }
   195 
   196 bool convar_cache_lookup(const char *name, ConVarInfo **pVar)
   197 {
   198 	ConVarInfo **pLookup = convar_cache.retrieve(name);
   199 	if (pLookup != NULL)
   200 	{
   201 		*pVar = *pLookup;
   202 		return true;
   203 	}
   204 	else
   205 	{
   206 		return false;
   207 	}
   208 }
   209 
   210 void ConVarManager::OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe)
   211 {
   212 	/* Only check convars that have not been created by SourceMod's core */
   213 	ConVarInfo *pInfo;
   214 	if (!convar_cache_lookup(name, &pInfo))
   215 	{
   216 		return;
   217 	}
   218 
   219 	HandleSecurity sec(NULL, g_pCoreIdent);
   220 
   221 	/* Remove it from our cache */
   222 	m_ConVars.remove(pInfo);
   223 	convar_cache.remove(name);
   224 
   225 	/* Now make sure no plugins are referring to this pointer */
   226 	IPluginIterator *pl_iter = g_PluginSys.GetPluginIterator();
   227 	while (pl_iter->MorePlugins())
   228 	{
   229 		IPlugin *pl = pl_iter->GetPlugin();
   230 
   231 		ConVarList *pConVarList;
   232 		if (pl->GetProperty("ConVarList", (void **)&pConVarList, true) 
   233 			&& pConVarList != NULL)
   234 		{
   235 			pConVarList->remove(pInfo->pVar);
   236 		}
   237 
   238 		pl_iter->NextPlugin();
   239 	}
   240 
   241 	/* Free resources */
   242 	g_HandleSys.FreeHandle(pInfo->handle, &sec);
   243 	delete pInfo;
   244 }
   245 
   246 void ConVarManager::OnPluginUnloaded(IPlugin *plugin)
   247 {
   248 	ConVarList *pConVarList;
   249 
   250 	/* If plugin has a convar list, free its memory */
   251 	if (plugin->GetProperty("ConVarList", (void **)&pConVarList, true))
   252 	{
   253 		delete pConVarList;
   254 	}
   255 }
   256 
   257 void ConVarManager::OnHandleDestroy(HandleType_t type, void *object)
   258 {
   259 }
   260 
   261 bool ConVarManager::GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
   262 {
   263 	*pSize = sizeof(ConVar) + sizeof(ConVarInfo);
   264 	return true;
   265 }
   266 
   267 void ConVarManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   268 {
   269 	int argcount = command.ArgC();
   270 	if (argcount >= 3)
   271 	{
   272 		/* Get plugin index that was passed */
   273 		const char *arg = command.Arg(2);
   274 		
   275 		/* Get plugin object */
   276 		CPlugin *plugin = g_PluginSys.FindPluginByConsoleArg(arg);
   277 
   278 		if (!plugin)
   279 		{
   280 			g_RootMenu.ConsolePrint("[SM] Plugin \"%s\" was not found.", arg);
   281 			return;
   282 		}
   283 
   284 		/* Get plugin name */
   285 		const sm_plugininfo_t *plinfo = plugin->GetPublicInfo();
   286 		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : plugin->GetFilename();
   287 
   288 		ConVarList *pConVarList;
   289 		ConVarList::iterator iter;
   290 
   291 		/* If no convar list... */
   292 		if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
   293 		{
   294 			g_RootMenu.ConsolePrint("[SM] No convars found for: %s", plname);
   295 			return;
   296 		}
   297 
   298 		g_RootMenu.ConsolePrint("[SM] Listing %d convars for: %s", pConVarList->size(), plname);
   299 		g_RootMenu.ConsolePrint("  %-32.31s %s", "[Name]", "[Value]");
   300 
   301 		/* Iterate convar list and display each one */
   302 		for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
   303 		{
   304 			const ConVar *pConVar = (*iter);
   305 			g_RootMenu.ConsolePrint("  %-32.31s %s", pConVar->GetName(), pConVar->GetString()); 
   306 		}
   307 
   308 		return;
   309 	}
   310 
   311 	/* Display usage of subcommand */
   312 	g_RootMenu.ConsolePrint("[SM] Usage: sm convars <plugin #>");
   313 }
   314 
   315 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)
   316 {
   317 	ConVar *pConVar = NULL;
   318 	ConVarInfo *pInfo = NULL;
   319 	Handle_t hndl = 0;
   320 
   321 	/* Find out if the convar exists already */
   322 	pConVar = icvar->FindVar(name);
   323 
   324 	/* If the convar already exists... */
   325 	if (pConVar)
   326 	{
   327 		/* Add convar to plugin's list */
   328 		AddConVarToPluginList(pContext, pConVar);
   329 
   330 		/* First find out if we already have a handle to it */
   331 		if (convar_cache_lookup(name, &pInfo))
   332 		{
   333 			return pInfo->handle;
   334 		}
   335 		else
   336 		{
   337 			/* Create and initialize ConVarInfo structure */
   338 			pInfo = new ConVarInfo();
   339 			pInfo->sourceMod = false;
   340 			pInfo->pChangeForward = NULL;
   341 			pInfo->pVar = pConVar;
   342 
   343 			/* If we don't, then create a new handle from the convar */
   344 			hndl = g_HandleSys.CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
   345 			if (hndl == BAD_HANDLE)
   346 			{
   347 				delete pInfo;
   348 				return BAD_HANDLE;
   349 			}
   350 
   351 			pInfo->handle = hndl;
   352 
   353 			/* Insert struct into caches */
   354 			m_ConVars.push_back(pInfo);
   355 			convar_cache.insert(name, pInfo);
   356 			TrackConCommandBase(pConVar, this);
   357 
   358 			return hndl;
   359 		}
   360 	}
   361 
   362 	/* Prevent creating a convar that has the same name as a console command */
   363 	if (FindCommand(name))
   364 	{
   365 		return BAD_HANDLE;
   366 	}
   367 
   368 	/* Create and initialize ConVarInfo structure */
   369 	pInfo = new ConVarInfo();
   370 	pInfo->handle = hndl;
   371 	pInfo->sourceMod = true;
   372 	pInfo->pChangeForward = NULL;
   373 
   374 	/* Create a handle from the new convar */
   375 	hndl = g_HandleSys.CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
   376 	if (hndl == BAD_HANDLE)
   377 	{
   378 		delete pInfo;
   379 		return BAD_HANDLE;
   380 	}
   381 
   382 	pInfo->handle = hndl;
   383 
   384 	/* Since an existing convar (or concmd with the same name) was not found , now we can finally create it */
   385 	pConVar = new ConVar(sm_strdup(name), sm_strdup(defaultVal), flags, sm_strdup(description), hasMin, min, hasMax, max);
   386 	pInfo->pVar = pConVar;
   387 
   388 	/* Add convar to plugin's list */
   389 	AddConVarToPluginList(pContext, pConVar);
   390 
   391 	/* Insert struct into caches */
   392 	m_ConVars.push_back(pInfo);
   393 	convar_cache.insert(name, pInfo);
   394 
   395 	return hndl;
   396 }
   397 
   398 Handle_t ConVarManager::FindConVar(const char *name)
   399 {
   400 	ConVar *pConVar = NULL;
   401 	ConVarInfo *pInfo;
   402 	Handle_t hndl;
   403 
   404 	/* Search for convar */
   405 	pConVar = icvar->FindVar(name);
   406 
   407 	/* If it doesn't exist, then return an invalid handle */
   408 	if (!pConVar)
   409 	{
   410 		return BAD_HANDLE;
   411 	}
   412 
   413 	/* At this point, the convar exists. So, find out if we already have a handle */
   414 	if (convar_cache_lookup(name, &pInfo))
   415 	{
   416 		return pInfo->handle;
   417 	}
   418 
   419 	/* Create and initialize ConVarInfo structure */
   420 	pInfo = new ConVarInfo();
   421 	pInfo->sourceMod = false;
   422 	pInfo->pChangeForward = NULL;
   423 	pInfo->pVar = pConVar;
   424 
   425 	/* If we don't have a handle, then create a new one */
   426 	hndl = g_HandleSys.CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
   427 	if (hndl == BAD_HANDLE)
   428 	{
   429 		delete pInfo;
   430 		return BAD_HANDLE;
   431 	}
   432 
   433 	pInfo->handle = hndl;
   434 
   435 	/* Insert struct into our caches */
   436 	m_ConVars.push_back(pInfo);
   437 	convar_cache.insert(name, pInfo);
   438 	TrackConCommandBase(pConVar, this);
   439 
   440 	return hndl;
   441 }
   442 
   443 void ConVarManager::AddConVarChangeListener(const char *name, IConVarChangeListener *pListener)
   444 {
   445 	ConVarInfo *pInfo;
   446 
   447 	if (FindConVar(name) == BAD_HANDLE)
   448 	{
   449 		return;
   450 	}
   451 
   452 	/* Find the convar in the lookup trie */
   453 	if (convar_cache_lookup(name, &pInfo))
   454 	{
   455 		pInfo->changeListeners.push_back(pListener);
   456 	}
   457 }
   458 
   459 void ConVarManager::RemoveConVarChangeListener(const char *name, IConVarChangeListener *pListener)
   460 {
   461 	ConVarInfo *pInfo;
   462 
   463 	/* Find the convar in the lookup trie */
   464 	if (convar_cache_lookup(name, &pInfo))
   465 	{
   466 		pInfo->changeListeners.remove(pListener);
   467 	}
   468 }
   469 
   470 void ConVarManager::HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
   471 {
   472 	ConVarInfo *pInfo;
   473 	IChangeableForward *pForward;
   474 
   475 	/* Find the convar in the lookup trie */
   476 	if (convar_cache_lookup(pConVar->GetName(), &pInfo))
   477 	{
   478 		/* Get the forward */
   479 		pForward = pInfo->pChangeForward;
   480 
   481 		/* If forward does not exist, create it */
   482 		if (!pForward)
   483 		{
   484 			pForward = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, CONVARCHANGE_PARAMS);
   485 			pInfo->pChangeForward = pForward;
   486 		}
   487 
   488 		/* Add function to forward's list */
   489 		pForward->AddFunction(pFunction);
   490 	}
   491 }
   492 
   493 void ConVarManager::UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
   494 {
   495 	ConVarInfo *pInfo;
   496 	IChangeableForward *pForward;
   497 	IPluginContext *pContext = pFunction->GetParentContext();
   498 
   499 	/* Find the convar in the lookup trie */
   500 	if (convar_cache_lookup(pConVar->GetName(), &pInfo))
   501 	{
   502 		/* Get the forward */
   503 		pForward = pInfo->pChangeForward;
   504 
   505 		/* If the forward doesn't exist, we can't unhook anything */
   506 		if (!pForward)
   507 		{
   508 			pContext->ThrowNativeError("Convar \"%s\" has no active hook", pConVar->GetName());
   509 			return;
   510 		}
   511 
   512 		/* Remove the function from the forward's list */
   513 		if (!pForward->RemoveFunction(pFunction))
   514 		{
   515 			pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", pConVar->GetName());
   516 			return;
   517 		}
   518 
   519 		/* If the forward now has 0 functions in it... */
   520 		if (pForward->GetFunctionCount() == 0)
   521 		{
   522 			/* Free this forward */
   523 			g_Forwards.ReleaseForward(pForward);
   524 			pInfo->pChangeForward = NULL;
   525 		}
   526 	}
   527 }
   528 
   529 QueryCvarCookie_t ConVarManager::QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback, Handle_t hndl)
   530 {
   531 	QueryCvarCookie_t cookie;
   532 
   533 	/* Call StartQueryCvarValue() in either the IVEngineServer or IServerPluginHelpers depending on situation */
   534 	if (m_bIsDLLQueryHooked)
   535 	{
   536 		cookie = engine->StartQueryCvarValue(pPlayer, name);	
   537 	}
   538 	else if (m_bIsVSPQueryHooked)
   539 	{
   540 		cookie = serverpluginhelpers->StartQueryCvarValue(pPlayer, name);
   541 	}
   542 	else
   543 	{
   544 		return InvalidQueryCvarCookie;
   545 	}
   546 
   547 	ConVarQuery query = {cookie, pCallback, hndl};
   548 	m_ConVarQueries.push_back(query);
   549 
   550 	return cookie;
   551 }
   552 
   553 void ConVarManager::AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar)
   554 {
   555 	ConVarList *pConVarList;
   556 	ConVarList::iterator iter;
   557 	bool inserted = false;
   558 	const char *orig = pConVar->GetName();
   559 
   560 	IPlugin *plugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
   561 
   562 	/* Check plugin for an existing convar list */
   563 	if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
   564 	{
   565 		pConVarList = new ConVarList();
   566 		plugin->SetProperty("ConVarList", pConVarList);
   567 	}
   568 	else if (pConVarList->find(pConVar) != pConVarList->end())
   569 	{
   570 		/* If convar is already in list, then don't add it */
   571 		return;
   572 	}
   573 
   574 	/* Insert convar into list which is sorted alphabetically */
   575 	for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
   576 	{
   577 		if (strcmp(orig, (*iter)->GetName()) < 0)
   578 		{
   579 			pConVarList->insert(iter, pConVar);
   580 			inserted = true;
   581 			break;
   582 		}
   583 	}
   584 
   585 	if (!inserted)
   586 	{
   587 		pConVarList->push_back(pConVar);
   588 	}
   589 }
   590 
   591 #if SOURCE_ENGINE >= SE_ORANGEBOX
   592 void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue)
   593 #else
   594 void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue)
   595 #endif
   596 {
   597 	/* If the values are the same, exit early in order to not trigger callbacks */
   598 	if (strcmp(pConVar->GetString(), oldValue) == 0)
   599 	{
   600 		return;
   601 	}
   602 
   603 	ConVarInfo *pInfo;
   604 
   605 	/* Find the convar in the lookup trie */
   606 	if (!convar_cache_lookup(pConVar->GetName(), &pInfo))
   607 	{
   608 		return;
   609 	}
   610 
   611 	IChangeableForward *pForward = pInfo->pChangeForward;
   612 
   613 	if (pInfo->changeListeners.size() != 0)
   614 	{
   615 		for (List<IConVarChangeListener *>::iterator i = pInfo->changeListeners.begin();
   616 			 i != pInfo->changeListeners.end();
   617 			 i++)
   618 		{
   619 #if SOURCE_ENGINE >= SE_ORANGEBOX
   620 			(*i)->OnConVarChanged(pConVar, oldValue, flOldValue);
   621 #else
   622 			(*i)->OnConVarChanged(pConVar, oldValue, atof(oldValue));
   623 #endif
   624 		}
   625 	}
   626 
   627 	if (pForward != NULL)
   628 	{
   629 		/* Now call forwards in plugins that have hooked this */
   630 		pForward->PushCell(pInfo->handle);
   631 		pForward->PushString(oldValue);
   632 		pForward->PushString(pConVar->GetString());
   633 		pForward->Execute(NULL);
   634 	}
   635 }
   636 
   637 bool ConVarManager::IsQueryingSupported()
   638 {
   639 	return (m_bIsDLLQueryHooked || m_bIsVSPQueryHooked);
   640 }
   641 
   642 void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
   643 {
   644 	IPluginFunction *pCallback = NULL;
   645 	cell_t value = 0;
   646 	List<ConVarQuery>::iterator iter;
   647 
   648 	for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end(); iter++)
   649 	{
   650 		ConVarQuery &query = (*iter);
   651 		if (query.cookie == cookie)
   652 		{
   653 			pCallback = query.pCallback;
   654 			value = query.value;
   655 			break;
   656 		}
   657 	}
   658 
   659 	if (pCallback)
   660 	{
   661 		cell_t ret;
   662 
   663 		pCallback->PushCell(cookie);
   664 		pCallback->PushCell(IndexOfEdict(pPlayer));
   665 		pCallback->PushCell(result);
   666 		pCallback->PushString(cvarName);
   667 
   668 		if (result == eQueryCvarValueStatus_ValueIntact)
   669 		{
   670 			pCallback->PushString(cvarValue);
   671 		}
   672 		else
   673 		{
   674 			pCallback->PushString("\0");
   675 		}
   676 
   677 		pCallback->PushCell(value);
   678 		pCallback->Execute(&ret);
   679 
   680 		m_ConVarQueries.erase(iter);
   681 	}
   682 }
   683 
   684 HandleError ConVarManager::ReadConVarHandle(Handle_t hndl, ConVar **pVar)
   685 {
   686 	ConVarInfo *pInfo;
   687 	HandleError error;
   688 	
   689 	if ((error = g_HandleSys.ReadHandle(hndl, m_ConVarType, NULL, (void **)&pInfo)) != HandleError_None)
   690 	{
   691 		return error;
   692 	}
   693 
   694 	if (pVar)
   695 	{
   696 		*pVar = pInfo->pVar;
   697 	}
   698 
   699 	return error;
   700 }