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