core/ConVarManager.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Fri Jul 12 02:31:41 2013 -0400 (2013-07-12)
changeset 3942 7aaa00382627
parent 3913 a73cf1f26984
child 3995 e94dfd569130
permissions -rw-r--r--
Updated for latest hl2sdk-dota changes.
     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 	/* Remove convar queries for this plugin that haven't returned results yet */
   308 	for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end(); iter++)
   309 	{
   310 		ConVarQuery &query = (*iter);
   311 		if (query.pCallback->GetParentRuntime() == plugin->GetRuntime())
   312 		{
   313 			m_ConVarQueries.erase(iter);
   314 		}
   315 	}
   316 }
   317 
   318 void ConVarManager::OnHandleDestroy(HandleType_t type, void *object)
   319 {
   320 }
   321 
   322 bool ConVarManager::GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
   323 {
   324 	*pSize = sizeof(ConVar) + sizeof(ConVarInfo);
   325 	return true;
   326 }
   327 
   328 void ConVarManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
   329 {
   330 	int argcount = command.ArgC();
   331 	if (argcount >= 3)
   332 	{
   333 		bool wantReset = false;
   334 		
   335 		/* Get plugin index that was passed */
   336 		const char *arg = command.Arg(2);
   337 		if (argcount >= 4 && strcmp(arg, "reset") == 0)
   338 		{
   339 			wantReset = true;
   340 			arg = command.Arg(3);
   341 		}
   342 		
   343 		/* Get plugin object */
   344 		IPlugin *plugin = scripts->FindPluginByConsoleArg(arg);
   345 
   346 		if (!plugin)
   347 		{
   348 			g_RootMenu.ConsolePrint("[SM] Plugin \"%s\" was not found.", arg);
   349 			return;
   350 		}
   351 
   352 		/* Get plugin name */
   353 		const sm_plugininfo_t *plinfo = plugin->GetPublicInfo();
   354 		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : plugin->GetFilename();
   355 
   356 		ConVarList *pConVarList;
   357 		ConVarList::iterator iter;
   358 
   359 		/* If no convar list... */
   360 		if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
   361 		{
   362 			g_RootMenu.ConsolePrint("[SM] No convars found for: %s", plname);
   363 			return;
   364 		}
   365 
   366 		if (!wantReset)
   367 		{
   368 			g_RootMenu.ConsolePrint("[SM] Listing %d convars for: %s", pConVarList->size(), plname);
   369 			g_RootMenu.ConsolePrint("  %-32.31s %s", "[Name]", "[Value]");
   370 		}
   371 		
   372 		/* Iterate convar list and display/reset each one */
   373 		for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
   374 		{
   375 			/*const */ConVar *pConVar = const_cast<ConVar *>(*iter);
   376 			if (!wantReset)
   377 			{
   378 				g_RootMenu.ConsolePrint("  %-32.31s %s", pConVar->GetName(), pConVar->GetString()); 
   379 			} else {
   380 				pConVar->Revert();
   381 			}
   382 		}
   383 		
   384 		if (wantReset)
   385 		{
   386 			g_RootMenu.ConsolePrint("[SM] Reset %d convars for: %s", pConVarList->size(), plname);
   387 		}
   388 
   389 		return;
   390 	}
   391 
   392 	/* Display usage of subcommand */
   393 	g_RootMenu.ConsolePrint("[SM] Usage: sm cvars [reset] <plugin #>");
   394 }
   395 
   396 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)
   397 {
   398 	ConVar *pConVar = NULL;
   399 	ConVarInfo *pInfo = NULL;
   400 	Handle_t hndl = 0;
   401 
   402 	/* Find out if the convar exists already */
   403 	pConVar = icvar->FindVar(name);
   404 
   405 	/* If the convar already exists... */
   406 	if (pConVar)
   407 	{
   408 		/* Add convar to plugin's list */
   409 		AddConVarToPluginList(pContext, pConVar);
   410 
   411 		/* First find out if we already have a handle to it */
   412 		if (convar_cache_lookup(name, &pInfo))
   413 		{
   414 			return pInfo->handle;
   415 		}
   416 		else
   417 		{
   418 			/* Create and initialize ConVarInfo structure */
   419 			pInfo = new ConVarInfo();
   420 			pInfo->sourceMod = false;
   421 			pInfo->pChangeForward = NULL;
   422 			pInfo->pVar = pConVar;
   423 
   424 			/* If we don't, then create a new handle from the convar */
   425 			hndl = handlesys->CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
   426 			if (hndl == BAD_HANDLE)
   427 			{
   428 				delete pInfo;
   429 				return BAD_HANDLE;
   430 			}
   431 
   432 			pInfo->handle = hndl;
   433 
   434 			/* Insert struct into caches */
   435 			m_ConVars.push_back(pInfo);
   436 			convar_cache.insert(name, pInfo);
   437 			TrackConCommandBase(pConVar, this);
   438 
   439 			return hndl;
   440 		}
   441 	}
   442 
   443 	/* Prevent creating a convar that has the same name as a console command */
   444 	if (FindCommand(name))
   445 	{
   446 		return BAD_HANDLE;
   447 	}
   448 
   449 	/* Create and initialize ConVarInfo structure */
   450 	pInfo = new ConVarInfo();
   451 	pInfo->handle = hndl;
   452 	pInfo->sourceMod = true;
   453 	pInfo->pChangeForward = NULL;
   454 
   455 	/* Create a handle from the new convar */
   456 	hndl = handlesys->CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
   457 	if (hndl == BAD_HANDLE)
   458 	{
   459 		delete pInfo;
   460 		return BAD_HANDLE;
   461 	}
   462 
   463 	pInfo->handle = hndl;
   464 
   465 	/* Since an existing convar (or concmd with the same name) was not found , now we can finally create it */
   466 	pConVar = new ConVar(sm_strdup(name), sm_strdup(defaultVal), flags, sm_strdup(description), hasMin, min, hasMax, max);
   467 	pInfo->pVar = pConVar;
   468 
   469 	/* Add convar to plugin's list */
   470 	AddConVarToPluginList(pContext, pConVar);
   471 
   472 	/* Insert struct into caches */
   473 	m_ConVars.push_back(pInfo);
   474 	convar_cache.insert(name, pInfo);
   475 
   476 	return hndl;
   477 }
   478 
   479 Handle_t ConVarManager::FindConVar(const char *name)
   480 {
   481 	ConVar *pConVar = NULL;
   482 	ConVarInfo *pInfo;
   483 	Handle_t hndl;
   484 
   485 	/* Search for convar */
   486 	pConVar = icvar->FindVar(name);
   487 
   488 	/* If it doesn't exist, then return an invalid handle */
   489 	if (!pConVar)
   490 	{
   491 		return BAD_HANDLE;
   492 	}
   493 
   494 	/* At this point, the convar exists. So, find out if we already have a handle */
   495 	if (convar_cache_lookup(name, &pInfo))
   496 	{
   497 		return pInfo->handle;
   498 	}
   499 
   500 	/* Create and initialize ConVarInfo structure */
   501 	pInfo = new ConVarInfo();
   502 	pInfo->sourceMod = false;
   503 	pInfo->pChangeForward = NULL;
   504 	pInfo->pVar = pConVar;
   505 
   506 	/* If we don't have a handle, then create a new one */
   507 	hndl = handlesys->CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
   508 	if (hndl == BAD_HANDLE)
   509 	{
   510 		delete pInfo;
   511 		return BAD_HANDLE;
   512 	}
   513 
   514 	pInfo->handle = hndl;
   515 
   516 	/* Insert struct into our caches */
   517 	m_ConVars.push_back(pInfo);
   518 	convar_cache.insert(name, pInfo);
   519 	TrackConCommandBase(pConVar, this);
   520 
   521 	return hndl;
   522 }
   523 
   524 void ConVarManager::AddConVarChangeListener(const char *name, IConVarChangeListener *pListener)
   525 {
   526 	ConVarInfo *pInfo;
   527 
   528 	if (FindConVar(name) == BAD_HANDLE)
   529 	{
   530 		return;
   531 	}
   532 
   533 	/* Find the convar in the lookup trie */
   534 	if (convar_cache_lookup(name, &pInfo))
   535 	{
   536 		pInfo->changeListeners.push_back(pListener);
   537 	}
   538 }
   539 
   540 void ConVarManager::RemoveConVarChangeListener(const char *name, IConVarChangeListener *pListener)
   541 {
   542 	ConVarInfo *pInfo;
   543 
   544 	/* Find the convar in the lookup trie */
   545 	if (convar_cache_lookup(name, &pInfo))
   546 	{
   547 		pInfo->changeListeners.remove(pListener);
   548 	}
   549 }
   550 
   551 void ConVarManager::HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
   552 {
   553 	ConVarInfo *pInfo;
   554 	IChangeableForward *pForward;
   555 
   556 	/* Find the convar in the lookup trie */
   557 	if (convar_cache_lookup(pConVar->GetName(), &pInfo))
   558 	{
   559 		/* Get the forward */
   560 		pForward = pInfo->pChangeForward;
   561 
   562 		/* If forward does not exist, create it */
   563 		if (!pForward)
   564 		{
   565 			pForward = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, CONVARCHANGE_PARAMS);
   566 			pInfo->pChangeForward = pForward;
   567 		}
   568 
   569 		/* Add function to forward's list */
   570 		pForward->AddFunction(pFunction);
   571 	}
   572 }
   573 
   574 void ConVarManager::UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
   575 {
   576 	ConVarInfo *pInfo;
   577 	IChangeableForward *pForward;
   578 	IPluginContext *pContext = pFunction->GetParentContext();
   579 
   580 	/* Find the convar in the lookup trie */
   581 	if (convar_cache_lookup(pConVar->GetName(), &pInfo))
   582 	{
   583 		/* Get the forward */
   584 		pForward = pInfo->pChangeForward;
   585 
   586 		/* If the forward doesn't exist, we can't unhook anything */
   587 		if (!pForward)
   588 		{
   589 			pContext->ThrowNativeError("Convar \"%s\" has no active hook", pConVar->GetName());
   590 			return;
   591 		}
   592 
   593 		/* Remove the function from the forward's list */
   594 		if (!pForward->RemoveFunction(pFunction))
   595 		{
   596 			pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", pConVar->GetName());
   597 			return;
   598 		}
   599 
   600 		/* If the forward now has 0 functions in it... */
   601 		if (pForward->GetFunctionCount() == 0 &&
   602 			!ConVarReentrancyGuard::IsCvarInChain(pConVar))
   603 		{
   604 			/* Free this forward */
   605 			g_Forwards.ReleaseForward(pForward);
   606 			pInfo->pChangeForward = NULL;
   607 		}
   608 	}
   609 }
   610 
   611 QueryCvarCookie_t ConVarManager::QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback, Handle_t hndl)
   612 {
   613 	QueryCvarCookie_t cookie = 0;
   614 
   615 #if SOURCE_ENGINE != SE_DARKMESSIAH
   616 	/* Call StartQueryCvarValue() in either the IVEngineServer or IServerPluginHelpers depending on situation */
   617 	if (m_bIsDLLQueryHooked)
   618 	{
   619 #if SOURCE_ENGINE == SE_DOTA
   620 		cookie = engine->StartQueryCvarValue(CEntityIndex(IndexOfEdict(pPlayer)), name);
   621 #else
   622 		cookie = engine->StartQueryCvarValue(pPlayer, name);	
   623 #endif
   624 	}
   625 #if SOURCE_ENGINE != SE_DOTA
   626 	else if (m_bIsVSPQueryHooked)
   627 	{
   628 		cookie = serverpluginhelpers->StartQueryCvarValue(pPlayer, name);
   629 	}
   630 #endif
   631 	else
   632 	{
   633 		return InvalidQueryCvarCookie;
   634 	}
   635 
   636 	ConVarQuery query = {cookie, pCallback, hndl};
   637 	m_ConVarQueries.push_back(query);
   638 #endif
   639 
   640 	return cookie;
   641 }
   642 
   643 void ConVarManager::AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar)
   644 {
   645 	ConVarList *pConVarList;
   646 	ConVarList::iterator iter;
   647 	bool inserted = false;
   648 	const char *orig = pConVar->GetName();
   649 
   650 	IPlugin *plugin = scripts->FindPluginByContext(pContext->GetContext());
   651 
   652 	/* Check plugin for an existing convar list */
   653 	if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
   654 	{
   655 		pConVarList = new ConVarList();
   656 		plugin->SetProperty("ConVarList", pConVarList);
   657 	}
   658 	else if (pConVarList->find(pConVar) != pConVarList->end())
   659 	{
   660 		/* If convar is already in list, then don't add it */
   661 		return;
   662 	}
   663 
   664 	/* Insert convar into list which is sorted alphabetically */
   665 	for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
   666 	{
   667 		if (strcmp(orig, (*iter)->GetName()) < 0)
   668 		{
   669 			pConVarList->insert(iter, pConVar);
   670 			inserted = true;
   671 			break;
   672 		}
   673 	}
   674 
   675 	if (!inserted)
   676 	{
   677 		pConVarList->push_back(pConVar);
   678 	}
   679 }
   680 
   681 #if SOURCE_ENGINE >= SE_ORANGEBOX
   682 void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue)
   683 #else
   684 void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue)
   685 #endif
   686 {
   687 	/* If the values are the same, exit early in order to not trigger callbacks */
   688 	if (strcmp(pConVar->GetString(), oldValue) == 0)
   689 	{
   690 		return;
   691 	}
   692 
   693 	ConVarInfo *pInfo;
   694 
   695 	/* Find the convar in the lookup trie */
   696 	if (!convar_cache_lookup(pConVar->GetName(), &pInfo))
   697 	{
   698 		return;
   699 	}
   700 
   701 	IChangeableForward *pForward = pInfo->pChangeForward;
   702 
   703 	if (pInfo->changeListeners.size() != 0)
   704 	{
   705 		for (List<IConVarChangeListener *>::iterator i = pInfo->changeListeners.begin();
   706 			 i != pInfo->changeListeners.end();
   707 			 i++)
   708 		{
   709 #if SOURCE_ENGINE >= SE_ORANGEBOX
   710 			(*i)->OnConVarChanged(pConVar, oldValue, flOldValue);
   711 #else
   712 			(*i)->OnConVarChanged(pConVar, oldValue, atof(oldValue));
   713 #endif
   714 		}
   715 	}
   716 
   717 	if (pForward != NULL)
   718 	{
   719 		ConVarReentrancyGuard guard(pConVar);
   720 
   721 		/* Now call forwards in plugins that have hooked this */
   722 		pForward->PushCell(pInfo->handle);
   723 		pForward->PushString(oldValue);
   724 		pForward->PushString(pConVar->GetString());
   725 		pForward->Execute(NULL);
   726 	}
   727 }
   728 
   729 bool ConVarManager::IsQueryingSupported()
   730 {
   731 	return (m_bIsDLLQueryHooked || m_bIsVSPQueryHooked);
   732 }
   733 
   734 #if SOURCE_ENGINE != SE_DARKMESSIAH
   735 #if SOURCE_ENGINE == SE_DOTA
   736 void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, CEntityIndex player, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
   737 #else
   738 void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
   739 #endif // SE_DOTA
   740 {
   741 	IPluginFunction *pCallback = NULL;
   742 	cell_t value = 0;
   743 	List<ConVarQuery>::iterator iter;
   744 
   745 	for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end(); iter++)
   746 	{
   747 		ConVarQuery &query = (*iter);
   748 		if (query.cookie == cookie)
   749 		{
   750 			pCallback = query.pCallback;
   751 			value = query.value;
   752 			break;
   753 		}
   754 	}
   755 
   756 	if (pCallback)
   757 	{
   758 		cell_t ret;
   759 
   760 		pCallback->PushCell(cookie);
   761 #if SOURCE_ENGINE == SE_DOTA
   762 		pCallback->PushCell(player.Get());
   763 #else
   764 		pCallback->PushCell(IndexOfEdict(pPlayer));
   765 #endif
   766 		pCallback->PushCell(result);
   767 		pCallback->PushString(cvarName);
   768 
   769 		if (result == eQueryCvarValueStatus_ValueIntact)
   770 		{
   771 			pCallback->PushString(cvarValue);
   772 		}
   773 		else
   774 		{
   775 			pCallback->PushString("\0");
   776 		}
   777 
   778 		pCallback->PushCell(value);
   779 		pCallback->Execute(&ret);
   780 
   781 		m_ConVarQueries.erase(iter);
   782 	}
   783 }
   784 #endif
   785 
   786 HandleError ConVarManager::ReadConVarHandle(Handle_t hndl, ConVar **pVar)
   787 {
   788 	ConVarInfo *pInfo;
   789 	HandleError error;
   790 	
   791 	if ((error = handlesys->ReadHandle(hndl, m_ConVarType, NULL, (void **)&pInfo)) != HandleError_None)
   792 	{
   793 		return error;
   794 	}
   795 
   796 	if (pVar)
   797 	{
   798 		*pVar = pInfo->pVar;
   799 	}
   800 
   801 	return error;
   802 }