core/CConVarManager.cpp
author David Anderson <dvander@alliedmods.net>
Sat Feb 17 19:03:18 2007 +0000 (2007-02-17)
changeset 520 7676d61cde2d
parent 511 c29f8796bcf4
child 576 50696154c0b8
permissions -rw-r--r--
added helper a core translation helper to the translator
regadmincmd is now officially done and tested, as are overrides
printtoplayers now prints to the server on id==0 as a convenience
fixed a crash bug in the admin system
fixed up console.inc a bit
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@511
   100
	ConVar *cvar = static_cast<ConVar *>(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@511
   108
		// Delete string allocations
ds@511
   109
		delete [] cvar->GetName(); 
ds@511
   110
		delete [] cvar->GetDefault();
ds@511
   111
		delete [] cvar->GetHelpText();
ds@511
   112
ds@472
   113
		// Then unregister it
ds@472
   114
		g_SMAPI->UnregisterConCmdBase(g_PLAPI, cvar);
ds@472
   115
	}
ds@472
   116
}
ds@472
   117
ds@472
   118
void CConVarManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
ds@472
   119
{
ds@472
   120
	if (argcount >= 3)
ds@472
   121
	{
ds@472
   122
		int id = 1;
ds@472
   123
ds@472
   124
		// Get plugin index that was passed
ds@472
   125
		int num = atoi(g_RootMenu.GetArgument(2));
ds@472
   126
		
ds@472
   127
		// If invalid plugin index...
ds@472
   128
		if (num < 1 || num > (int)g_PluginSys.GetPluginCount())
ds@472
   129
		{
dvander@499
   130
			g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", num);
ds@472
   131
			return;
ds@472
   132
		}
ds@472
   133
ds@472
   134
		// Get plugin object
ds@472
   135
		CPlugin *pl = g_PluginSys.GetPluginByOrder(num);
ds@472
   136
ds@487
   137
		CVector<ConVar *> *cvarList = NULL;
ds@472
   138
ds@487
   139
		// Get convar list from 'ConVar' property
ds@487
   140
		pl->GetProperty("ConVar", reinterpret_cast<void **>(&cvarList));
ds@487
   141
ds@487
   142
		// If no cvar list...
ds@487
   143
		if (cvarList == NULL)
ds@472
   144
		{
ds@472
   145
			g_RootMenu.ConsolePrint("[SM] No convars for \"%s\"", pl->GetPublicInfo()->name);
ds@472
   146
			return;
ds@472
   147
		}
ds@472
   148
ds@482
   149
		g_RootMenu.ConsolePrint("[SM] Displaying convars for \"%s\"", pl->GetPublicInfo()->name);
ds@472
   150
ds@472
   151
		// Iterate convar list and display each one
ds@487
   152
		for (size_t i = 0; i < cvarList->size(); i++, id++)
ds@472
   153
		{
ds@487
   154
			ConVar *cvar = (*cvarList)[i];
ds@472
   155
			g_RootMenu.ConsolePrint("  %02d \"%s\" = \"%s\"", id, cvar->GetName(), cvar->GetString()); 
ds@472
   156
		}
ds@472
   157
ds@472
   158
		return;
ds@472
   159
	}
ds@472
   160
ds@472
   161
	// Display usage of subcommand
dvander@499
   162
	g_RootMenu.ConsolePrint("[SM] Usage: sm convars <plugin #>");
ds@472
   163
}
ds@472
   164
ds@472
   165
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
   166
{
ds@472
   167
	ConVar *cvar = NULL;
ds@472
   168
	ConVarInfo *info = NULL;
ds@487
   169
	CVector<ConVar *> *cvarList = NULL;
ds@472
   170
	Handle_t hndl = 0;
ds@472
   171
ds@472
   172
	// Find out if the convar exists already
ds@472
   173
	cvar = icvar->FindVar(name);
ds@472
   174
ds@472
   175
	// If the convar already exists...
ds@472
   176
	if (cvar != NULL)
ds@472
   177
	{
ds@472
   178
		// First check if we already have a handle to it
ds@472
   179
		if (sm_trie_retrieve(m_ConVarCache, name, reinterpret_cast<void **>(&info)))
ds@472
   180
		{
ds@472
   181
			// If we do, then return that handle
ds@472
   182
			return info->handle;
ds@472
   183
		} else {
ds@472
   184
			// If we don't, then create a new handle from the convar
ds@472
   185
			hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL);
ds@472
   186
ds@472
   187
			info = new ConVarInfo;
ds@472
   188
			info->handle = hndl;
ds@472
   189
			info->sourceMod = false;
ds@472
   190
			info->changeForward = NULL;
ds@472
   191
			info->origCallback = cvar->GetCallback();
ds@472
   192
ds@472
   193
			m_ConVars.push_back(info);
ds@472
   194
ds@472
   195
			return hndl;
ds@472
   196
		}
ds@472
   197
	}
ds@472
   198
ds@472
   199
	// To prevent creating a convar that has the same name as a console command... ugh
ds@472
   200
	ConCommandBase *pBase = icvar->GetCommands();
ds@472
   201
ds@472
   202
	while (pBase)
ds@472
   203
	{
ds@472
   204
		if (strcmp(pBase->GetName(), name) == 0)
ds@472
   205
		{
ds@472
   206
			return pContext->ThrowNativeError("Convar \"%s\" was not created. A console command with the same name already exists.", name);
ds@472
   207
		}
ds@472
   208
ds@472
   209
		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
ds@472
   210
	}
ds@472
   211
ds@472
   212
	// Since we didn't find an existing convar (or concmd with the same name), now we can finally create it!
ds@511
   213
	cvar = new ConVar(sm_strdup(name), sm_strdup(defaultVal), flags, sm_strdup(helpText), hasMin, min, hasMax, max);
ds@472
   214
ds@487
   215
	// Find plugin creating convar
ds@487
   216
	IPlugin *pl = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@487
   217
ds@487
   218
	// Get convar list from 'ConVar' property of plugin
ds@487
   219
	pl->GetProperty("ConVar", reinterpret_cast<void **>(&cvarList));
ds@487
   220
ds@487
   221
	// If 'ConVar' property doesn't exist...
ds@487
   222
	if (cvarList == NULL)
ds@487
   223
	{
ds@487
   224
		// Then create it
ds@487
   225
		cvarList = new CVector<ConVar *>;
ds@487
   226
		pl->SetProperty("ConVar", cvarList);
ds@487
   227
	}
ds@487
   228
ds@472
   229
	// Add new convar to plugin's list
ds@487
   230
	cvarList->push_back(cvar);
ds@472
   231
ds@472
   232
	// Create a handle from the new convar
ds@472
   233
	hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL);
ds@472
   234
ds@472
   235
	info = new ConVarInfo;
ds@472
   236
	info->handle = hndl;
ds@472
   237
	info->sourceMod = true;
ds@472
   238
	info->changeForward = NULL;
ds@472
   239
	info->origCallback = NULL;
ds@472
   240
ds@472
   241
	m_ConVars.push_back(info);
ds@472
   242
ds@472
   243
	// Insert the handle into our lookup trie
ds@472
   244
	sm_trie_insert(m_ConVarCache, name, info);
ds@472
   245
ds@472
   246
	return hndl;
ds@472
   247
}
ds@472
   248
ds@472
   249
Handle_t CConVarManager::FindConVar(const char *name)
ds@472
   250
{
ds@472
   251
	ConVar *cvar = NULL;
ds@472
   252
	ConVarInfo *info = NULL;
ds@472
   253
	Handle_t hndl = 0;
ds@472
   254
ds@472
   255
	// Search for convar
ds@472
   256
	cvar = icvar->FindVar(name);
ds@472
   257
ds@472
   258
	// If it doesn't exist, then return an invalid handle
ds@472
   259
	if (cvar == NULL)
ds@472
   260
	{
ds@472
   261
		return BAD_HANDLE;
ds@472
   262
	}
ds@472
   263
ds@472
   264
	// At this point, convar exists, so find out if we already have a handle for it
ds@472
   265
	if (sm_trie_retrieve(m_ConVarCache, name, reinterpret_cast<void **>(&info)))
ds@472
   266
	{
ds@472
   267
		// If we do, then return that handle
ds@472
   268
		return info->handle;
ds@472
   269
	}
ds@472
   270
ds@472
   271
	// If we don't, then create a new handle from the convar
ds@472
   272
	hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL);
ds@472
   273
ds@472
   274
	info = new ConVarInfo;
ds@472
   275
	info->handle = hndl;
ds@472
   276
	info->sourceMod = false;
ds@472
   277
	info->changeForward = NULL;
ds@472
   278
	info->origCallback = cvar->GetCallback();
ds@472
   279
ds@472
   280
	m_ConVars.push_back(info);
ds@472
   281
ds@472
   282
	// Insert the handle into our cache
ds@472
   283
	sm_trie_insert(m_ConVarCache, name, info);
ds@472
   284
ds@472
   285
	return hndl;
ds@472
   286
}
ds@472
   287
ds@472
   288
void CConVarManager::HookConVarChange(IPluginContext *pContext, ConVar *cvar, funcid_t funcid)
ds@472
   289
{
ds@472
   290
	IPluginFunction *func = pContext->GetFunctionById(funcid);
ds@472
   291
	IChangeableForward *fwd = NULL;
ds@472
   292
	char fwdName[64];
ds@472
   293
	ConVarInfo *info = NULL;
ds@472
   294
ds@472
   295
	// This shouldn't happen...
ds@472
   296
	if (func == NULL)
ds@472
   297
	{
ds@472
   298
		pContext->ThrowNativeError("Invalid function specified");
ds@472
   299
		return;
ds@472
   300
	}
ds@472
   301
ds@472
   302
	// Create a forward name
ds@472
   303
	UTIL_Format(fwdName, sizeof(fwdName), "ConVar.%s", cvar->GetName());
ds@472
   304
ds@472
   305
	// First find out if the forward already exists
ds@472
   306
	g_Forwards.FindForward(fwdName, &fwd);
ds@472
   307
ds@472
   308
	// If the forward doesn't exist...
ds@472
   309
	if (fwd == NULL)
ds@472
   310
	{
ds@472
   311
		// This is the forward's parameter type list
ds@472
   312
		ParamType p[] = {Param_Cell, Param_String, Param_String};
ds@472
   313
ds@472
   314
		// Create the forward
ds@472
   315
		fwd = g_Forwards.CreateForwardEx(fwdName, ET_Ignore, 3, p);
ds@472
   316
ds@472
   317
		// Find the convar in the lookup trie
ds@472
   318
		if (sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast<void **>(&info)))
ds@472
   319
		{
ds@472
   320
			// Set the convar's forward to the newly created one
ds@472
   321
			info->changeForward = fwd;
ds@472
   322
ds@472
   323
			// Set the convar's callback to our static one
ds@472
   324
			cvar->InstallChangeCallback(OnConVarChanged);
ds@472
   325
		}
ds@472
   326
	}
ds@472
   327
ds@472
   328
	// Add the function to the forward's list
ds@472
   329
	fwd->AddFunction(func);
ds@472
   330
}
ds@472
   331
ds@472
   332
void CConVarManager::UnhookConVarChange(IPluginContext *pContext, ConVar *cvar, funcid_t funcid)
ds@472
   333
{
ds@472
   334
	IPluginFunction *func = pContext->GetFunctionById(funcid);
ds@472
   335
	IChangeableForward *fwd = NULL;
ds@472
   336
	ConVarInfo *info = NULL;
ds@472
   337
ds@472
   338
	// This shouldn't happen...
ds@472
   339
	if (func == NULL)
ds@472
   340
	{
ds@472
   341
		pContext->ThrowNativeError("Invalid function specified");
ds@472
   342
		return;
ds@472
   343
	}
ds@472
   344
ds@472
   345
	// Find the convar in the lookup trie
ds@472
   346
	if (sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast<void **>(&info)))
ds@472
   347
	{
ds@472
   348
		// Get the forward
ds@472
   349
		fwd = info->changeForward;
ds@472
   350
ds@472
   351
		// If the forward doesn't exist, we can't unhook anything
ds@472
   352
		if (fwd == NULL)
ds@472
   353
		{
ds@472
   354
			pContext->ThrowNativeError("Convar \"%s\" has no active hook", cvar->GetName());
ds@472
   355
			return;
ds@472
   356
		}
ds@472
   357
ds@472
   358
		// Remove the function from the forward's list
ds@472
   359
		if (!fwd->RemoveFunction(func))
ds@472
   360
		{
ds@472
   361
			pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", cvar->GetName());
ds@472
   362
			return;
ds@472
   363
		}
ds@472
   364
ds@472
   365
		// If the forward now has 0 functions in it...
ds@472
   366
		if (fwd->GetFunctionCount() == 0)
ds@472
   367
		{
ds@472
   368
			// Free this forward
ds@472
   369
			g_Forwards.ReleaseForward(fwd);
ds@472
   370
			info->changeForward = NULL;
ds@472
   371
ds@472
   372
			// Put the back the original convar callback
ds@472
   373
			cvar->InstallChangeCallback(info->origCallback);
ds@472
   374
		}
ds@472
   375
	}
ds@472
   376
}
ds@472
   377
ds@472
   378
void CConVarManager::OnConVarChanged(ConVar *cvar, const char *oldValue)
ds@472
   379
{
ds@494
   380
	// If the values are the same...
ds@494
   381
	if (strcmp(cvar->GetString(), oldValue) == 0)
ds@494
   382
	{
ds@494
   383
		// Exit early in order to not trigger callbacks
ds@494
   384
		return;
ds@494
   385
	}
ds@494
   386
ds@472
   387
	Trie *cache = g_ConVarManager.GetConVarCache();
ds@472
   388
	ConVarInfo *info;
ds@472
   389
ds@472
   390
	// Find the convar in the lookup trie
ds@472
   391
	sm_trie_retrieve(cache, cvar->GetName(), reinterpret_cast<void **>(&info));
ds@472
   392
ds@472
   393
	FnChangeCallback origCallback = info->origCallback;
ds@472
   394
	IChangeableForward *fwd = info->changeForward;
ds@472
   395
ds@472
   396
	// If there was a change callback installed previously, call it
ds@472
   397
	if (origCallback)
ds@472
   398
	{
ds@472
   399
		origCallback(cvar, oldValue);
ds@472
   400
	}
ds@472
   401
ds@472
   402
	// Now call forwards in plugins that have hooked this
ds@472
   403
	fwd->PushCell(info->handle);
ds@472
   404
	fwd->PushString(cvar->GetString());
ds@472
   405
	fwd->PushString(oldValue);
ds@472
   406
	fwd->Execute(NULL);
ds@472
   407
}