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