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