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