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