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