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