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