core/CConVarManager.cpp
author David Anderson <dvander@alliedmods.net>
Fri Feb 16 04:16:31 2007 +0000 (2007-02-16)
changeset 502 4530ea065a8b
parent 499 961a75a6d466
child 511 c29f8796bcf4
permissions -rw-r--r--
fixed a serious potential bug with swapping plugins to debug mode, leaving stale function pointers without plugin removal
ds@472
     1
/**
ds@472
     2
* ===============================================================
ds@472
     3
* SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
ds@472
     4
* ===============================================================
ds@472
     5
*
ds@472
     6
* This file is not open source and may not be copied without explicit
ds@472
     7
* written permission of AlliedModders LLC.  This file may not be redistributed 
ds@472
     8
* in whole or significant part.
ds@472
     9
* For information, see LICENSE.txt or http://www.sourcemod.net/license.php
ds@472
    10
*
ds@472
    11
* Version: $Id$
ds@472
    12
*/
ds@472
    13
ds@472
    14
#include "CConVarManager.h"
ds@472
    15
#include "PluginSys.h"
ds@487
    16
#include "ForwardSys.h"
ds@487
    17
#include "HandleSys.h"
ds@472
    18
#include "sm_srvcmds.h"
ds@472
    19
#include "sm_stringutil.h"
ds@487
    20
#include <sh_vector.h>
ds@472
    21
ds@472
    22
CConVarManager g_ConVarManager;
ds@472
    23
ds@472
    24
CConVarManager::CConVarManager() : m_ConVarType(0)
ds@472
    25
{
ds@472
    26
	// Create a convar lookup trie
ds@472
    27
	m_ConVarCache = sm_trie_create();
ds@472
    28
}
ds@472
    29
ds@472
    30
CConVarManager::~CConVarManager()
ds@472
    31
{
ds@472
    32
	List<ConVarInfo *>::iterator i;
ds@472
    33
ds@472
    34
	// Destroy our convar lookup trie
ds@472
    35
	sm_trie_destroy(m_ConVarCache);
ds@472
    36
ds@472
    37
	// Destroy all the ConVarInfo structures
ds@472
    38
	for (i = m_ConVars.begin(); i != m_ConVars.end(); i++)
ds@472
    39
	{
ds@472
    40
		delete (*i);
ds@472
    41
	}
ds@472
    42
ds@472
    43
	m_ConVars.clear();
ds@472
    44
}
ds@472
    45
ds@472
    46
void CConVarManager::OnSourceModAllInitialized()
ds@472
    47
{
ds@472
    48
	HandleAccess sec;
ds@472
    49
ds@472
    50
	// Set up access rights for the 'ConVar' handle type
ds@472
    51
	sec.access[HandleAccess_Read] = 0;
ds@472
    52
	sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@472
    53
	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@472
    54
ds@472
    55
	// Create the 'ConVar' handle type
ds@472
    56
	m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL);
ds@472
    57
ds@472
    58
	// Add the 'cvars' option to the 'sm' console command
ds@482
    59
	g_RootMenu.AddRootConsoleCommand("convars", "View convars created by a plugin", this);
ds@472
    60
}
ds@472
    61
ds@472
    62
void CConVarManager::OnSourceModShutdown()
ds@472
    63
{
ds@472
    64
	IChangeableForward *fwd;
ds@472
    65
	List<ConVarInfo *>::iterator i;
ds@472
    66
ds@472
    67
	// Iterate list of ConVarInfo structures
ds@472
    68
	for (i = m_ConVars.begin(); i != m_ConVars.end(); i++)
ds@472
    69
	{
ds@472
    70
		fwd = (*i)->changeForward;
ds@472
    71
ds@472
    72
		// Free any convar-change forwards that still exist
ds@472
    73
		if (fwd)
ds@472
    74
		{
ds@472
    75
			g_Forwards.ReleaseForward(fwd);
ds@472
    76
		}
ds@472
    77
	}
ds@472
    78
ds@472
    79
	// Remove the 'cvars' option from the 'sm' console command
ds@482
    80
	g_RootMenu.RemoveRootConsoleCommand("convars", this);
ds@472
    81
ds@472
    82
	// Remove the 'ConVar' handle type
ds@472
    83
	g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent);
ds@472
    84
}
ds@472
    85
ds@487
    86
void CConVarManager::OnPluginDestroyed(IPlugin *plugin)
ds@487
    87
{
ds@487
    88
	CVector<ConVar *> *cvarList;
ds@487
    89
ds@487
    90
	// If plugin has a convar list, free its memory
ds@487
    91
	if (plugin->GetProperty("ConVar", reinterpret_cast<void **>(&cvarList), true))
ds@487
    92
	{
ds@487
    93
		delete cvarList;
ds@487
    94
	}
ds@487
    95
}
ds@487
    96
ds@472
    97
void CConVarManager::OnHandleDestroy(HandleType_t type, void *object)
ds@472
    98
{
ds@472
    99
	ConVarInfo *info;
ds@472
   100
	ConCommandBase *cvar = static_cast<ConCommandBase *>(object);
ds@472
   101
ds@472
   102
	// Find convar in lookup trie
ds@472
   103
	sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast<void **>(&info));
ds@472
   104
ds@472
   105
	// If convar was created by SourceMod plugin...
ds@472
   106
	if (info->sourceMod)
ds@472
   107
	{
ds@472
   108
		// Then unregister it
ds@472
   109
		g_SMAPI->UnregisterConCmdBase(g_PLAPI, cvar);
ds@472
   110
	}
ds@472
   111
}
ds@472
   112
ds@472
   113
void CConVarManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
ds@472
   114
{
ds@472
   115
	if (argcount >= 3)
ds@472
   116
	{
ds@472
   117
		int id = 1;
ds@472
   118
ds@472
   119
		// Get plugin index that was passed
ds@472
   120
		int num = atoi(g_RootMenu.GetArgument(2));
ds@472
   121
		
ds@472
   122
		// If invalid plugin index...
ds@472
   123
		if (num < 1 || num > (int)g_PluginSys.GetPluginCount())
ds@472
   124
		{
dvander@499
   125
			g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", num);
ds@472
   126
			return;
ds@472
   127
		}
ds@472
   128
ds@472
   129
		// Get plugin object
ds@472
   130
		CPlugin *pl = g_PluginSys.GetPluginByOrder(num);
ds@472
   131
ds@487
   132
		CVector<ConVar *> *cvarList = NULL;
ds@472
   133
ds@487
   134
		// Get convar list from 'ConVar' property
ds@487
   135
		pl->GetProperty("ConVar", reinterpret_cast<void **>(&cvarList));
ds@487
   136
ds@487
   137
		// If no cvar list...
ds@487
   138
		if (cvarList == NULL)
ds@472
   139
		{
ds@472
   140
			g_RootMenu.ConsolePrint("[SM] No convars for \"%s\"", pl->GetPublicInfo()->name);
ds@472
   141
			return;
ds@472
   142
		}
ds@472
   143
ds@482
   144
		g_RootMenu.ConsolePrint("[SM] Displaying convars for \"%s\"", pl->GetPublicInfo()->name);
ds@472
   145
ds@472
   146
		// Iterate convar list and display each one
ds@487
   147
		for (size_t i = 0; i < cvarList->size(); i++, id++)
ds@472
   148
		{
ds@487
   149
			ConVar *cvar = (*cvarList)[i];
ds@472
   150
			g_RootMenu.ConsolePrint("  %02d \"%s\" = \"%s\"", id, cvar->GetName(), cvar->GetString()); 
ds@472
   151
		}
ds@472
   152
ds@472
   153
		return;
ds@472
   154
	}
ds@472
   155
ds@472
   156
	// Display usage of subcommand
dvander@499
   157
	g_RootMenu.ConsolePrint("[SM] Usage: sm convars <plugin #>");
ds@472
   158
}
ds@472
   159
ds@472
   160
Handle_t CConVarManager::CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal, const char *helpText, int flags, bool hasMin, float min, bool hasMax, float max)
ds@472
   161
{
ds@472
   162
	ConVar *cvar = NULL;
ds@472
   163
	ConVarInfo *info = NULL;
ds@487
   164
	CVector<ConVar *> *cvarList = NULL;
ds@472
   165
	Handle_t hndl = 0;
ds@472
   166
ds@472
   167
	// Find out if the convar exists already
ds@472
   168
	cvar = icvar->FindVar(name);
ds@472
   169
ds@472
   170
	// If the convar already exists...
ds@472
   171
	if (cvar != NULL)
ds@472
   172
	{
ds@472
   173
		// First check if we already have a handle to it
ds@472
   174
		if (sm_trie_retrieve(m_ConVarCache, name, reinterpret_cast<void **>(&info)))
ds@472
   175
		{
ds@472
   176
			// If we do, then return that handle
ds@472
   177
			return info->handle;
ds@472
   178
		} else {
ds@472
   179
			// If we don't, then create a new handle from the convar
ds@472
   180
			hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL);
ds@472
   181
ds@472
   182
			info = new ConVarInfo;
ds@472
   183
			info->handle = hndl;
ds@472
   184
			info->sourceMod = false;
ds@472
   185
			info->changeForward = NULL;
ds@472
   186
			info->origCallback = cvar->GetCallback();
ds@472
   187
ds@472
   188
			m_ConVars.push_back(info);
ds@472
   189
ds@472
   190
			return hndl;
ds@472
   191
		}
ds@472
   192
	}
ds@472
   193
ds@472
   194
	// To prevent creating a convar that has the same name as a console command... ugh
ds@472
   195
	ConCommandBase *pBase = icvar->GetCommands();
ds@472
   196
ds@472
   197
	while (pBase)
ds@472
   198
	{
ds@472
   199
		if (strcmp(pBase->GetName(), name) == 0)
ds@472
   200
		{
ds@472
   201
			return pContext->ThrowNativeError("Convar \"%s\" was not created. A console command with the same name already exists.", name);
ds@472
   202
		}
ds@472
   203
ds@472
   204
		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
ds@472
   205
	}
ds@472
   206
ds@472
   207
	// Since we didn't find an existing convar (or concmd with the same name), now we can finally create it!
ds@472
   208
	cvar = new ConVar(name, defaultVal, flags, helpText, hasMin, min, hasMax, max);
ds@472
   209
ds@487
   210
	// Find plugin creating convar
ds@487
   211
	IPlugin *pl = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@487
   212
ds@487
   213
	// Get convar list from 'ConVar' property of plugin
ds@487
   214
	pl->GetProperty("ConVar", reinterpret_cast<void **>(&cvarList));
ds@487
   215
ds@487
   216
	// If 'ConVar' property doesn't exist...
ds@487
   217
	if (cvarList == NULL)
ds@487
   218
	{
ds@487
   219
		// Then create it
ds@487
   220
		cvarList = new CVector<ConVar *>;
ds@487
   221
		pl->SetProperty("ConVar", cvarList);
ds@487
   222
	}
ds@487
   223
ds@472
   224
	// Add new convar to plugin's list
ds@487
   225
	cvarList->push_back(cvar);
ds@472
   226
ds@472
   227
	// Create a handle from the new convar
ds@472
   228
	hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL);
ds@472
   229
ds@472
   230
	info = new ConVarInfo;
ds@472
   231
	info->handle = hndl;
ds@472
   232
	info->sourceMod = true;
ds@472
   233
	info->changeForward = NULL;
ds@472
   234
	info->origCallback = NULL;
ds@472
   235
ds@472
   236
	m_ConVars.push_back(info);
ds@472
   237
ds@472
   238
	// Insert the handle into our lookup trie
ds@472
   239
	sm_trie_insert(m_ConVarCache, name, info);
ds@472
   240
ds@472
   241
	return hndl;
ds@472
   242
}
ds@472
   243
ds@472
   244
Handle_t CConVarManager::FindConVar(const char *name)
ds@472
   245
{
ds@472
   246
	ConVar *cvar = NULL;
ds@472
   247
	ConVarInfo *info = NULL;
ds@472
   248
	Handle_t hndl = 0;
ds@472
   249
ds@472
   250
	// Search for convar
ds@472
   251
	cvar = icvar->FindVar(name);
ds@472
   252
ds@472
   253
	// If it doesn't exist, then return an invalid handle
ds@472
   254
	if (cvar == NULL)
ds@472
   255
	{
ds@472
   256
		return BAD_HANDLE;
ds@472
   257
	}
ds@472
   258
ds@472
   259
	// At this point, convar exists, so find out if we already have a handle for it
ds@472
   260
	if (sm_trie_retrieve(m_ConVarCache, name, reinterpret_cast<void **>(&info)))
ds@472
   261
	{
ds@472
   262
		// If we do, then return that handle
ds@472
   263
		return info->handle;
ds@472
   264
	}
ds@472
   265
ds@472
   266
	// If we don't, then create a new handle from the convar
ds@472
   267
	hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL);
ds@472
   268
ds@472
   269
	info = new ConVarInfo;
ds@472
   270
	info->handle = hndl;
ds@472
   271
	info->sourceMod = false;
ds@472
   272
	info->changeForward = NULL;
ds@472
   273
	info->origCallback = cvar->GetCallback();
ds@472
   274
ds@472
   275
	m_ConVars.push_back(info);
ds@472
   276
ds@472
   277
	// Insert the handle into our cache
ds@472
   278
	sm_trie_insert(m_ConVarCache, name, info);
ds@472
   279
ds@472
   280
	return hndl;
ds@472
   281
}
ds@472
   282
ds@472
   283
void CConVarManager::HookConVarChange(IPluginContext *pContext, ConVar *cvar, funcid_t funcid)
ds@472
   284
{
ds@472
   285
	IPluginFunction *func = pContext->GetFunctionById(funcid);
ds@472
   286
	IChangeableForward *fwd = NULL;
ds@472
   287
	char fwdName[64];
ds@472
   288
	ConVarInfo *info = NULL;
ds@472
   289
ds@472
   290
	// This shouldn't happen...
ds@472
   291
	if (func == NULL)
ds@472
   292
	{
ds@472
   293
		pContext->ThrowNativeError("Invalid function specified");
ds@472
   294
		return;
ds@472
   295
	}
ds@472
   296
ds@472
   297
	// Create a forward name
ds@472
   298
	UTIL_Format(fwdName, sizeof(fwdName), "ConVar.%s", cvar->GetName());
ds@472
   299
ds@472
   300
	// First find out if the forward already exists
ds@472
   301
	g_Forwards.FindForward(fwdName, &fwd);
ds@472
   302
ds@472
   303
	// If the forward doesn't exist...
ds@472
   304
	if (fwd == NULL)
ds@472
   305
	{
ds@472
   306
		// This is the forward's parameter type list
ds@472
   307
		ParamType p[] = {Param_Cell, Param_String, Param_String};
ds@472
   308
ds@472
   309
		// Create the forward
ds@472
   310
		fwd = g_Forwards.CreateForwardEx(fwdName, ET_Ignore, 3, p);
ds@472
   311
ds@472
   312
		// Find the convar in the lookup trie
ds@472
   313
		if (sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast<void **>(&info)))
ds@472
   314
		{
ds@472
   315
			// Set the convar's forward to the newly created one
ds@472
   316
			info->changeForward = fwd;
ds@472
   317
ds@472
   318
			// Set the convar's callback to our static one
ds@472
   319
			cvar->InstallChangeCallback(OnConVarChanged);
ds@472
   320
		}
ds@472
   321
	}
ds@472
   322
ds@472
   323
	// Add the function to the forward's list
ds@472
   324
	fwd->AddFunction(func);
ds@472
   325
}
ds@472
   326
ds@472
   327
void CConVarManager::UnhookConVarChange(IPluginContext *pContext, ConVar *cvar, funcid_t funcid)
ds@472
   328
{
ds@472
   329
	IPluginFunction *func = pContext->GetFunctionById(funcid);
ds@472
   330
	IChangeableForward *fwd = NULL;
ds@472
   331
	ConVarInfo *info = NULL;
ds@472
   332
ds@472
   333
	// This shouldn't happen...
ds@472
   334
	if (func == NULL)
ds@472
   335
	{
ds@472
   336
		pContext->ThrowNativeError("Invalid function specified");
ds@472
   337
		return;
ds@472
   338
	}
ds@472
   339
ds@472
   340
	// Find the convar in the lookup trie
ds@472
   341
	if (sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast<void **>(&info)))
ds@472
   342
	{
ds@472
   343
		// Get the forward
ds@472
   344
		fwd = info->changeForward;
ds@472
   345
ds@472
   346
		// If the forward doesn't exist, we can't unhook anything
ds@472
   347
		if (fwd == NULL)
ds@472
   348
		{
ds@472
   349
			pContext->ThrowNativeError("Convar \"%s\" has no active hook", cvar->GetName());
ds@472
   350
			return;
ds@472
   351
		}
ds@472
   352
ds@472
   353
		// Remove the function from the forward's list
ds@472
   354
		if (!fwd->RemoveFunction(func))
ds@472
   355
		{
ds@472
   356
			pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", cvar->GetName());
ds@472
   357
			return;
ds@472
   358
		}
ds@472
   359
ds@472
   360
		// If the forward now has 0 functions in it...
ds@472
   361
		if (fwd->GetFunctionCount() == 0)
ds@472
   362
		{
ds@472
   363
			// Free this forward
ds@472
   364
			g_Forwards.ReleaseForward(fwd);
ds@472
   365
			info->changeForward = NULL;
ds@472
   366
ds@472
   367
			// Put the back the original convar callback
ds@472
   368
			cvar->InstallChangeCallback(info->origCallback);
ds@472
   369
		}
ds@472
   370
	}
ds@472
   371
}
ds@472
   372
ds@472
   373
void CConVarManager::OnConVarChanged(ConVar *cvar, const char *oldValue)
ds@472
   374
{
ds@494
   375
	// If the values are the same...
ds@494
   376
	if (strcmp(cvar->GetString(), oldValue) == 0)
ds@494
   377
	{
ds@494
   378
		// Exit early in order to not trigger callbacks
ds@494
   379
		return;
ds@494
   380
	}
ds@494
   381
ds@472
   382
	Trie *cache = g_ConVarManager.GetConVarCache();
ds@472
   383
	ConVarInfo *info;
ds@472
   384
ds@472
   385
	// Find the convar in the lookup trie
ds@472
   386
	sm_trie_retrieve(cache, cvar->GetName(), reinterpret_cast<void **>(&info));
ds@472
   387
ds@472
   388
	FnChangeCallback origCallback = info->origCallback;
ds@472
   389
	IChangeableForward *fwd = info->changeForward;
ds@472
   390
ds@472
   391
	// If there was a change callback installed previously, call it
ds@472
   392
	if (origCallback)
ds@472
   393
	{
ds@472
   394
		origCallback(cvar, oldValue);
ds@472
   395
	}
ds@472
   396
ds@472
   397
	// Now call forwards in plugins that have hooked this
ds@472
   398
	fwd->PushCell(info->handle);
ds@472
   399
	fwd->PushString(cvar->GetString());
ds@472
   400
	fwd->PushString(oldValue);
ds@472
   401
	fwd->Execute(NULL);
ds@472
   402
}