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