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