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