core/ConVarManager.cpp
author Scott Ehlert <ds@alliedmods.net>
Mon Jul 23 19:38:02 2007 +0000 (2007-07-23)
changeset 1149 70d171906c7e
parent 1148 c523115e1124
child 1231 17badaf66ce4
permissions -rw-r--r--
No comment worth mentioning for this one :x
ds@580
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@653
     3
 * ===============================================================
ds@653
     4
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
ds@653
     5
 * ===============================================================
ds@653
     6
 *
ds@653
     7
 * This file is not open source and may not be copied without explicit
ds@653
     8
 * written permission of AlliedModders LLC.  This file may not be redistributed 
ds@653
     9
 * in whole or significant part.
ds@653
    10
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
ds@653
    11
 *
ds@653
    12
 * Version: $Id$
ds@653
    13
 */
ds@580
    14
ds@580
    15
#include "ConVarManager.h"
ds@696
    16
#include "HalfLife2.h"
ds@580
    17
#include "PluginSys.h"
ds@580
    18
#include "ForwardSys.h"
ds@580
    19
#include "HandleSys.h"
ds@580
    20
#include "sm_srvcmds.h"
ds@580
    21
#include "sm_stringutil.h"
ds@580
    22
#include <sh_vector.h>
ds@580
    23
ds@580
    24
ConVarManager g_ConVarManager;
ds@580
    25
ds@696
    26
SH_DECL_HOOK5_void(IServerGameDLL, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
ds@696
    27
SH_DECL_HOOK5_void(IServerPluginCallbacks, OnQueryCvarValueFinished, SH_NOATTRIB, 0, QueryCvarCookie_t, edict_t *, EQueryCvarValueStatus, const char *, const char *);
ds@696
    28
ds@582
    29
const ParamType CONVARCHANGE_PARAMS[] = {Param_Cell, Param_String, Param_String};
ds@582
    30
typedef List<const ConVar *> ConVarList;
ds@582
    31
ds@696
    32
ConVarManager::ConVarManager() : m_ConVarType(0), m_VSPIface(NULL), m_CanQueryConVars(false)
ds@580
    33
{
ds@1145
    34
#if PLAPI_VERSION < 12
ds@1145
    35
	m_IgnoreHandle = false;
ds@1145
    36
#endif
ds@1145
    37
ds@582
    38
	/* Create a convar lookup trie */
ds@580
    39
	m_ConVarCache = sm_trie_create();
ds@580
    40
}
ds@580
    41
ds@580
    42
ConVarManager::~ConVarManager()
ds@580
    43
{
ds@580
    44
	List<ConVarInfo *>::iterator iter;
ds@580
    45
ds@582
    46
	/* Destroy our convar lookup trie */
ds@580
    47
	sm_trie_destroy(m_ConVarCache);
ds@580
    48
ds@582
    49
	/* Destroy all the ConVarInfo structures */
ds@580
    50
	for (iter = m_ConVars.begin(); iter != m_ConVars.end(); iter++)
ds@580
    51
	{
ds@580
    52
		delete (*iter);
ds@580
    53
	}
ds@580
    54
ds@580
    55
	m_ConVars.clear();
ds@580
    56
}
ds@580
    57
ds@580
    58
void ConVarManager::OnSourceModAllInitialized()
ds@580
    59
{
ds@696
    60
	/* Only valid with ServerGameDLL006 or greater */
ds@696
    61
	if (g_SMAPI->GetGameDLLVersion() >= 6)
ds@696
    62
	{
ds@696
    63
		SH_ADD_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
ds@696
    64
		m_CanQueryConVars = true;
ds@696
    65
	}
ds@696
    66
ds@580
    67
	HandleAccess sec;
ds@580
    68
ds@582
    69
	/* Set up access rights for the 'ConVar' handle type */
ds@580
    70
	sec.access[HandleAccess_Read] = 0;
ds@580
    71
	sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@580
    72
	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@580
    73
ds@582
    74
	/* Create the 'ConVar' handle type */
ds@580
    75
	m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL);
ds@580
    76
ds@582
    77
	/* Add the 'convars' option to the 'sm' console command */
ds@990
    78
	g_RootMenu.AddRootConsoleCommand("cvars", "View convars created by a plugin", this);
ds@580
    79
}
ds@580
    80
ds@580
    81
void ConVarManager::OnSourceModShutdown()
ds@580
    82
{
ds@696
    83
	if (m_CanQueryConVars)
ds@696
    84
	{
ds@696
    85
		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
ds@696
    86
		SH_REMOVE_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, m_VSPIface, this, &ConVarManager::OnQueryCvarValueFinished, false);
ds@696
    87
	}
ds@696
    88
ds@580
    89
	IChangeableForward *fwd;
ds@580
    90
	List<ConVarInfo *>::iterator i;
ds@580
    91
ds@582
    92
	/* Iterate list of ConVarInfo structures */
ds@580
    93
	for (i = m_ConVars.begin(); i != m_ConVars.end(); i++)
ds@580
    94
	{
ds@696
    95
		fwd = (*i)->pChangeForward;
ds@580
    96
ds@582
    97
		/* Free any convar-change forwards that still exist */
ds@580
    98
		if (fwd)
ds@580
    99
		{
ds@580
   100
			g_Forwards.ReleaseForward(fwd);
ds@580
   101
		}
ds@580
   102
	}
ds@580
   103
ds@582
   104
	/* Remove the 'convars' option from the 'sm' console command */
ds@990
   105
	g_RootMenu.RemoveRootConsoleCommand("cvars", this);
ds@580
   106
ds@582
   107
	/* Remove the 'ConVar' handle type */
ds@580
   108
	g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent);
ds@580
   109
}
ds@580
   110
ds@696
   111
void ConVarManager::OnSourceModVSPReceived(IServerPluginCallbacks *iface)
ds@696
   112
{
ds@696
   113
	/* This will be called after OnSourceModAllInitialized(), so...
ds@696
   114
	 *
ds@696
   115
	 * If we haven't been able to hook the IServerGameDLL version at this point,
ds@696
   116
	 * then use hook IServerPluginCallbacks version from the engine.
ds@696
   117
	 */
ds@696
   118
ds@696
   119
	/* The Ship currently only supports ServerPluginCallbacks001, but we need 002 */
ds@696
   120
	if (g_IsOriginalEngine)
ds@696
   121
	{
ds@696
   122
		return;
ds@696
   123
	}
ds@696
   124
ds@696
   125
	if (!m_CanQueryConVars)
ds@696
   126
	{
ds@696
   127
		m_VSPIface = iface;
ds@696
   128
ds@696
   129
		SH_ADD_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, iface, this, &ConVarManager::OnQueryCvarValueFinished, false);
ds@696
   130
		m_CanQueryConVars = true;
ds@696
   131
	}
ds@696
   132
}
ds@696
   133
ds@1145
   134
#if PLAPI_VERSION >= 12
ds@1145
   135
void ConVarManager::OnUnlinkConCommandBase(PluginId id, ConCommandBase *pCommand)
ds@1145
   136
{
ds@1145
   137
	/* Only check convars that have not been created by SourceMod's core */
ds@1145
   138
	if (id != g_PLID && !pCommand->IsCommand())
ds@1145
   139
	{
ds@1145
   140
		ConVarInfo *pInfo;
ds@1146
   141
		const char *cvarName = pCommand->GetName();
ds@1145
   142
		HandleSecurity sec(NULL, g_pCoreIdent);
ds@1146
   143
		bool handleExists = sm_trie_retrieve(m_ConVarCache, cvarName, reinterpret_cast<void **>(&pInfo));
ds@1145
   144
ds@1145
   145
		if (handleExists)
ds@1145
   146
		{
ds@1145
   147
			g_HandleSys.FreeHandle(pInfo->handle, &sec);
ds@1146
   148
			sm_trie_delete(m_ConVarCache, cvarName);
ds@1146
   149
			m_ConVars.remove(pInfo);
ds@1145
   150
		}
ds@1145
   151
	}
ds@1145
   152
}
ds@1145
   153
ds@1145
   154
#else
ds@1145
   155
ds@1145
   156
/* I truly detest this code */
ds@1145
   157
void ConVarManager::OnMetamodPluginUnloaded(PluginId id)
ds@1145
   158
{
ds@1145
   159
	ConVarInfo *pInfo;
ds@1145
   160
	const char *cvarName;
ds@1145
   161
	HandleSecurity sec(NULL, g_pCoreIdent);
ds@1145
   162
ds@1145
   163
	List<ConVarInfo *>::iterator i;
ds@1145
   164
	for (i = m_ConVars.begin(); i != m_ConVars.end(); i++)
ds@1145
   165
	{
ds@1145
   166
		pInfo = (*i);
ds@1145
   167
		cvarName = pInfo->name;
ds@1145
   168
ds@1148
   169
		if (cvarName && !icvar->FindVar(cvarName))
ds@1145
   170
		{
ds@1145
   171
			m_IgnoreHandle = true;
ds@1145
   172
			g_HandleSys.FreeHandle(pInfo->handle, &sec);
ds@1145
   173
ds@1145
   174
			sm_trie_delete(m_ConVarCache, cvarName);
ds@1145
   175
			m_ConVars.erase(i);
ds@1145
   176
ds@1145
   177
			delete [] cvarName;
ds@1145
   178
		}
ds@1145
   179
	}
ds@1145
   180
}
ds@1145
   181
#endif
ds@1145
   182
ds@582
   183
void ConVarManager::OnPluginUnloaded(IPlugin *plugin)
ds@580
   184
{
ds@582
   185
	ConVarList *pConVarList;
ds@580
   186
ds@582
   187
	/* If plugin has a convar list, free its memory */
ds@582
   188
	if (plugin->GetProperty("ConVarList", (void **)&pConVarList, true))
ds@580
   189
	{
ds@582
   190
		delete pConVarList;
ds@580
   191
	}
ds@580
   192
}
ds@580
   193
ds@580
   194
void ConVarManager::OnHandleDestroy(HandleType_t type, void *object)
ds@580
   195
{
ds@1145
   196
#if PLAPI_VERSION < 12
ds@1145
   197
	/* Lovely workaround for our workaround! */
ds@1145
   198
	if (m_IgnoreHandle)
ds@1145
   199
	{
ds@1145
   200
		m_IgnoreHandle = false;
ds@1145
   201
		return;
ds@1145
   202
	}
ds@1145
   203
#endif
ds@1145
   204
ds@580
   205
	ConVarInfo *info;
ds@580
   206
	ConVar *pConVar = static_cast<ConVar *>(object);
ds@580
   207
ds@582
   208
	/* Find convar in lookup trie */
ds@580
   209
	sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), reinterpret_cast<void **>(&info));
ds@580
   210
ds@582
   211
	/* If convar was created by SourceMod plugin... */
ds@580
   212
	if (info->sourceMod)
ds@580
   213
	{
ds@582
   214
		/* Delete string allocations */
ds@580
   215
		delete [] pConVar->GetName(); 
ds@580
   216
		delete [] pConVar->GetDefault();
ds@580
   217
		delete [] pConVar->GetHelpText();
ds@580
   218
ds@582
   219
		/* Then unlink it from SourceMM */
ds@580
   220
		g_SMAPI->UnregisterConCmdBase(g_PLAPI, pConVar);
ds@580
   221
	}
ds@580
   222
}
ds@580
   223
ds@580
   224
void ConVarManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
ds@580
   225
{
ds@580
   226
	if (argcount >= 3)
ds@580
   227
	{
ds@582
   228
		/* Get plugin index that was passed */
ds@582
   229
		int id = atoi(g_RootMenu.GetArgument(2));
ds@580
   230
		
ds@582
   231
		/* Get plugin object */
ds@582
   232
		CPlugin *plugin = g_PluginSys.GetPluginByOrder(id);
ds@582
   233
ds@582
   234
		if (!plugin)
ds@580
   235
		{
ds@582
   236
			g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
ds@580
   237
			return;
ds@580
   238
		}
ds@580
   239
ds@582
   240
		/* Get plugin name */
ds@582
   241
		const sm_plugininfo_t *plinfo = plugin->GetPublicInfo();
ds@582
   242
		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : plugin->GetFilename();
ds@580
   243
ds@582
   244
		ConVarList *pConVarList;
ds@582
   245
		ConVarList::iterator iter;
ds@580
   246
ds@582
   247
		/* If no convar list... */
ds@582
   248
		if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
ds@580
   249
		{
ds@582
   250
			g_RootMenu.ConsolePrint("[SM] No convars found for: %s", plname);
ds@580
   251
			return;
ds@580
   252
		}
ds@580
   253
ds@582
   254
		g_RootMenu.ConsolePrint("[SM] Listing %d convars for: %s", pConVarList->size(), plname);
ds@582
   255
		g_RootMenu.ConsolePrint("  %-32.31s %s", "[Name]", "[Value]");
ds@580
   256
ds@582
   257
		/* Iterate convar list and display each one */
ds@582
   258
		for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
ds@580
   259
		{
ds@582
   260
			const ConVar *pConVar = (*iter);
ds@582
   261
			g_RootMenu.ConsolePrint("  %-32.31s %s", pConVar->GetName(), pConVar->GetString()); 
ds@580
   262
		}
ds@580
   263
ds@580
   264
		return;
ds@580
   265
	}
ds@580
   266
ds@582
   267
	/* Display usage of subcommand */
ds@580
   268
	g_RootMenu.ConsolePrint("[SM] Usage: sm convars <plugin #>");
ds@580
   269
}
ds@580
   270
ds@582
   271
Handle_t ConVarManager::CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal, const char *description, int flags, bool hasMin, float min, bool hasMax, float max)
ds@580
   272
{
ds@580
   273
	ConVar *pConVar = NULL;
ds@582
   274
	ConVarInfo *pInfo = NULL;
ds@580
   275
	Handle_t hndl = 0;
ds@580
   276
ds@582
   277
	/* Find out if the convar exists already */
ds@580
   278
	pConVar = icvar->FindVar(name);
ds@580
   279
ds@582
   280
	/* If the convar already exists... */
ds@582
   281
	if (pConVar)
ds@580
   282
	{
ds@759
   283
		/* Add convar to plugin's list */
ds@759
   284
		AddConVarToPluginList(pContext, pConVar);
ds@759
   285
ds@582
   286
		/* First find out if we already have a handle to it */
ds@582
   287
		if (sm_trie_retrieve(m_ConVarCache, name, (void **)&pInfo))
ds@580
   288
		{
ds@582
   289
			return pInfo->handle;
ds@580
   290
		} else {
ds@582
   291
			/* If we don't, then create a new handle from the convar */
ds@580
   292
			hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
ds@580
   293
ds@582
   294
			/* Create and initialize ConVarInfo structure */
ds@582
   295
			pInfo = new ConVarInfo();
ds@582
   296
			pInfo->handle = hndl;
ds@582
   297
			pInfo->sourceMod = false;
ds@696
   298
			pInfo->pChangeForward = NULL;
ds@582
   299
			pInfo->origCallback = pConVar->GetCallback();
ds@1145
   300
		#if PLAPI_VERSION < 12
ds@1145
   301
			pInfo->name = sm_strdup(pConVar->GetName());
ds@1145
   302
		#endif
ds@580
   303
ds@582
   304
			/* Insert struct into caches */
ds@582
   305
			m_ConVars.push_back(pInfo);
ds@582
   306
			sm_trie_insert(m_ConVarCache, name, pInfo);
ds@580
   307
ds@580
   308
			return hndl;
ds@580
   309
		}
ds@580
   310
	}
ds@580
   311
ds@696
   312
	/* To prevent creating a convar that has the same name as a console command... ugh */
ds@580
   313
	ConCommandBase *pBase = icvar->GetCommands();
ds@580
   314
ds@580
   315
	while (pBase)
ds@580
   316
	{
ds@582
   317
		if (pBase->IsCommand() && strcmp(pBase->GetName(), name) == 0)
ds@580
   318
		{
ds@582
   319
			return BAD_HANDLE;
ds@580
   320
		}
ds@580
   321
ds@580
   322
		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
ds@580
   323
	}
ds@580
   324
ds@582
   325
	/* Since an existing convar (or concmd with the same name) was not found , now we can finally create it */
ds@582
   326
	pConVar = new ConVar(sm_strdup(name), sm_strdup(defaultVal), flags, sm_strdup(description), hasMin, min, hasMax, max);
ds@580
   327
ds@582
   328
	/* Add convar to plugin's list */
ds@582
   329
	AddConVarToPluginList(pContext, pConVar);
ds@580
   330
ds@582
   331
	/* Create a handle from the new convar */
ds@580
   332
	hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
ds@580
   333
ds@582
   334
	/* Create and initialize ConVarInfo structure */
ds@582
   335
	pInfo = new ConVarInfo();
ds@582
   336
	pInfo->handle = hndl;
ds@582
   337
	pInfo->sourceMod = true;
ds@696
   338
	pInfo->pChangeForward = NULL;
ds@582
   339
	pInfo->origCallback = NULL;
ds@1149
   340
#if PLAPI_VERSION < 12
ds@1149
   341
	pInfo->name = NULL;
ds@1149
   342
#endif
ds@580
   343
ds@582
   344
	/* Insert struct into caches */
ds@582
   345
	m_ConVars.push_back(pInfo);
ds@582
   346
	sm_trie_insert(m_ConVarCache, name, pInfo);
ds@580
   347
ds@580
   348
	return hndl;
ds@580
   349
}
ds@580
   350
ds@580
   351
Handle_t ConVarManager::FindConVar(const char *name)
ds@580
   352
{
ds@580
   353
	ConVar *pConVar = NULL;
ds@582
   354
	ConVarInfo *pInfo;
ds@582
   355
	Handle_t hndl;
ds@580
   356
ds@582
   357
	/* Search for convar */
ds@580
   358
	pConVar = icvar->FindVar(name);
ds@580
   359
ds@582
   360
	/* If it doesn't exist, then return an invalid handle */
ds@582
   361
	if (!pConVar)
ds@580
   362
	{
ds@580
   363
		return BAD_HANDLE;
ds@580
   364
	}
ds@580
   365
ds@582
   366
	/* At this point, the convar exists. So, find out if we already have a handle */
ds@582
   367
	if (sm_trie_retrieve(m_ConVarCache, name, (void **)&pInfo))
ds@580
   368
	{
ds@582
   369
		return pInfo->handle;
ds@580
   370
	}
ds@580
   371
ds@582
   372
	/* If we don't have a handle, then create a new one */
ds@580
   373
	hndl = g_HandleSys.CreateHandle(m_ConVarType, pConVar, NULL, g_pCoreIdent, NULL);
ds@580
   374
ds@729
   375
	/* Create and initialize ConVarInfo structure */
ds@582
   376
	pInfo = new ConVarInfo();
ds@582
   377
	pInfo->handle = hndl;
ds@582
   378
	pInfo->sourceMod = false;
ds@696
   379
	pInfo->pChangeForward = NULL;
ds@582
   380
	pInfo->origCallback = pConVar->GetCallback();
ds@1145
   381
#if PLAPI_VERSION < 12
ds@1145
   382
	pInfo->name = sm_strdup(pConVar->GetName());
ds@1145
   383
#endif
ds@580
   384
ds@582
   385
	/* Insert struct into our caches */
ds@582
   386
	m_ConVars.push_back(pInfo);
ds@582
   387
	sm_trie_insert(m_ConVarCache, name, pInfo);
ds@580
   388
ds@580
   389
	return hndl;
ds@580
   390
}
ds@580
   391
ds@582
   392
void ConVarManager::HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
ds@580
   393
{
ds@582
   394
	ConVarInfo *pInfo;
ds@582
   395
	IChangeableForward *pForward;
ds@580
   396
ds@582
   397
	/* Find the convar in the lookup trie */
ds@582
   398
	if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo))
ds@580
   399
	{
ds@582
   400
		/* Get the forward */
ds@696
   401
		pForward = pInfo->pChangeForward;
ds@580
   402
ds@582
   403
		/* If forward does not exist, create it */
ds@582
   404
		if (!pForward)
ds@582
   405
		{
ds@582
   406
			pForward = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, CONVARCHANGE_PARAMS);
ds@696
   407
			pInfo->pChangeForward = pForward;
ds@580
   408
ds@582
   409
			/* Install our own callback */
ds@580
   410
			pConVar->InstallChangeCallback(OnConVarChanged);
ds@580
   411
		}
ds@582
   412
ds@582
   413
		/* Add function to forward's list */
ds@582
   414
		pForward->AddFunction(pFunction);
ds@580
   415
	}
ds@580
   416
}
ds@580
   417
ds@582
   418
void ConVarManager::UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
ds@580
   419
{
ds@582
   420
	ConVarInfo *pInfo;
ds@582
   421
	IChangeableForward *pForward;
ds@582
   422
	IPluginContext *pContext = pFunction->GetParentContext();
ds@580
   423
ds@582
   424
	/* Find the convar in the lookup trie */
ds@582
   425
	if (sm_trie_retrieve(m_ConVarCache, pConVar->GetName(), (void **)&pInfo))
ds@580
   426
	{
ds@582
   427
		/* Get the forward */
ds@696
   428
		pForward = pInfo->pChangeForward;
ds@580
   429
ds@582
   430
		/* If the forward doesn't exist, we can't unhook anything */
ds@582
   431
		if (!pForward)
ds@580
   432
		{
ds@580
   433
			pContext->ThrowNativeError("Convar \"%s\" has no active hook", pConVar->GetName());
ds@580
   434
			return;
ds@580
   435
		}
ds@580
   436
ds@582
   437
		/* Remove the function from the forward's list */
ds@582
   438
		if (!pForward->RemoveFunction(pFunction))
ds@580
   439
		{
ds@580
   440
			pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", pConVar->GetName());
ds@580
   441
			return;
ds@580
   442
		}
ds@580
   443
ds@582
   444
		/* If the forward now has 0 functions in it... */
ds@582
   445
		if (pForward->GetFunctionCount() == 0)
ds@580
   446
		{
ds@582
   447
			/* Free this forward */
ds@582
   448
			g_Forwards.ReleaseForward(pForward);
ds@696
   449
			pInfo->pChangeForward = NULL;
ds@582
   450
ds@582
   451
			/* Put back the original convar callback */
ds@582
   452
			pConVar->InstallChangeCallback(pInfo->origCallback);
ds@582
   453
		}
ds@582
   454
	}
ds@582
   455
}
ds@582
   456
ds@696
   457
QueryCvarCookie_t ConVarManager::QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback, Handle_t hndl)
ds@696
   458
{
ds@696
   459
	QueryCvarCookie_t cookie;
ds@696
   460
ds@696
   461
	/* Call StartQueryCvarValue() in either the IVEngineServer or IServerPluginHelpers depending on situation */
ds@696
   462
	if (!m_VSPIface)
ds@696
   463
	{
ds@696
   464
		cookie = engine->StartQueryCvarValue(pPlayer, name);	
ds@696
   465
	} else {
ds@696
   466
		cookie = serverpluginhelpers->StartQueryCvarValue(pPlayer, name);
ds@696
   467
	}
ds@696
   468
ds@696
   469
	ConVarQuery query = {cookie, pCallback, hndl};
ds@696
   470
	m_ConVarQueries.push_back(query);
ds@696
   471
ds@696
   472
	return cookie;
ds@696
   473
}
ds@696
   474
ds@582
   475
void ConVarManager::AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar)
ds@582
   476
{
ds@582
   477
	ConVarList *pConVarList;
ds@582
   478
	ConVarList::iterator iter;
ds@582
   479
	bool inserted = false;
ds@582
   480
	const char *orig = pConVar->GetName();
ds@582
   481
ds@582
   482
	IPlugin *plugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@580
   483
ds@582
   484
	/* Check plugin for an existing convar list */
ds@582
   485
	if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
ds@582
   486
	{
ds@582
   487
		pConVarList = new ConVarList();
ds@582
   488
		plugin->SetProperty("ConVarList", pConVarList);
ds@867
   489
	} else if (pConVarList->find(pConVar) != pConVarList->end()) {
ds@867
   490
		/* If convar is already in list, then don't add it */
ds@867
   491
		return;
ds@582
   492
	}
ds@582
   493
ds@582
   494
	/* Insert convar into list which is sorted alphabetically */
ds@582
   495
	for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
ds@582
   496
	{
ds@582
   497
		if (strcmp(orig, (*iter)->GetName()) < 0)
ds@582
   498
		{
ds@582
   499
			pConVarList->insert(iter, pConVar);
ds@582
   500
			inserted = true;
ds@582
   501
			break;
ds@580
   502
		}
ds@580
   503
	}
ds@582
   504
ds@582
   505
	if (!inserted)
ds@582
   506
	{
ds@582
   507
		pConVarList->push_back(pConVar);
ds@582
   508
	}
ds@580
   509
}
ds@580
   510
ds@580
   511
void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue)
ds@580
   512
{
ds@582
   513
	/* If the values are the same, exit early in order to not trigger callbacks */
ds@580
   514
	if (strcmp(pConVar->GetString(), oldValue) == 0)
ds@580
   515
	{
ds@580
   516
		return;
ds@580
   517
	}
ds@580
   518
ds@582
   519
	Trie *pCache = g_ConVarManager.GetConVarCache();
ds@582
   520
	ConVarInfo *pInfo;
ds@580
   521
ds@582
   522
	/* Find the convar in the lookup trie */
ds@582
   523
	sm_trie_retrieve(pCache, pConVar->GetName(), (void **)&pInfo);
ds@580
   524
ds@582
   525
	FnChangeCallback origCallback = pInfo->origCallback;
ds@696
   526
	IChangeableForward *pForward = pInfo->pChangeForward;
ds@580
   527
ds@582
   528
	/* If there was a change callback installed previously, call it */
ds@580
   529
	if (origCallback)
ds@580
   530
	{
ds@580
   531
		origCallback(pConVar, oldValue);
ds@580
   532
	}
ds@580
   533
ds@582
   534
	/* Now call forwards in plugins that have hooked this */
ds@582
   535
	pForward->PushCell(pInfo->handle);
ds@582
   536
	pForward->PushString(oldValue);
ds@582
   537
	pForward->PushString(pConVar->GetString());
ds@582
   538
	pForward->Execute(NULL);
ds@580
   539
}
ds@627
   540
ds@696
   541
void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
ds@696
   542
{
ds@696
   543
	IPluginFunction *pCallback = NULL;
ds@696
   544
	cell_t value = 0;
ds@696
   545
	List<ConVarQuery>::iterator iter;
ds@696
   546
ds@696
   547
	for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end(); iter++)
ds@696
   548
	{
ds@696
   549
		ConVarQuery &query = (*iter);
ds@696
   550
		
ds@696
   551
		if (query.cookie == cookie)
ds@696
   552
		{
ds@696
   553
			pCallback = query.pCallback;
ds@696
   554
			value = query.value;
ds@696
   555
ds@696
   556
			break;
ds@696
   557
		}
ds@696
   558
	}
ds@696
   559
ds@696
   560
	if (pCallback)
ds@696
   561
	{
ds@696
   562
		cell_t ret;
ds@696
   563
ds@696
   564
		pCallback->PushCell(cookie);
ds@696
   565
		pCallback->PushCell(engine->IndexOfEdict(pPlayer));
ds@696
   566
		pCallback->PushCell(result);
ds@696
   567
		pCallback->PushString(cvarName);
ds@696
   568
ds@696
   569
		if (result == eQueryCvarValueStatus_ValueIntact)
ds@696
   570
		{
ds@696
   571
			pCallback->PushString(cvarValue);
ds@696
   572
		} else {
ds@696
   573
			pCallback->PushString("\0");
ds@696
   574
		}
ds@696
   575
ds@696
   576
		pCallback->PushCell(value);
ds@696
   577
		pCallback->Execute(&ret);
ds@696
   578
ds@696
   579
		m_ConVarQueries.erase(iter);
ds@696
   580
	}
ds@696
   581
}
ds@696
   582
ds@696
   583
ds@627
   584
static int s_YamagramState = 0;
ds@627
   585
ds@627
   586
void _YamagramPrinterTwoPointOhOh(int yamagram)
ds@627
   587
{
ds@627
   588
	switch (yamagram)
ds@627
   589
	{
ds@627
   590
	case 0:
ds@627
   591
		g_RootMenu.ConsolePrint("Answer the following questions correctly and Gaben may not eat you after all.");
ds@627
   592
		g_RootMenu.ConsolePrint("You will be given one hint in the form of my patented yamagrams.");
ds@627
   593
		g_RootMenu.ConsolePrint("Type sm_nana to see the last question.");
ds@627
   594
		g_RootMenu.ConsolePrint("Type sm_nana <answer> to attempt an answer of the question.");
ds@627
   595
		g_RootMenu.ConsolePrint("-------------------------------");
ds@627
   596
		_YamagramPrinterTwoPointOhOh(1);
ds@627
   597
		return;
ds@627
   598
	case 1:
ds@627
   599
		g_RootMenu.ConsolePrint("Question Ichi (1)");
ds@627
   600
		g_RootMenu.ConsolePrint("One can turn into a cow by doing what action?");
ds@627
   601
		g_RootMenu.ConsolePrint("Hint: AGE SANS GRIT");
ds@627
   602
		break;
ds@627
   603
	case 2:
ds@627
   604
		g_RootMenu.ConsolePrint("Question Ni (2)");
ds@627
   605
		g_RootMenu.ConsolePrint("What kind of hat should you wear when using the Internet?");
ds@627
   606
		g_RootMenu.ConsolePrint("Hint: BRR MOOSE");
ds@627
   607
		break;
ds@627
   608
	case 3:
ds@627
   609
		g_RootMenu.ConsolePrint("Question San (3)");
ds@627
   610
		g_RootMenu.ConsolePrint("Who is the lead developer of SourceMod?");
ds@627
   611
		g_RootMenu.ConsolePrint("Hint: VEAL BANDANA DID RIP SOON");
ds@627
   612
		break;
ds@627
   613
	case 4:
ds@627
   614
		g_RootMenu.ConsolePrint("Question Yon (4)");
ds@627
   615
		g_RootMenu.ConsolePrint("A terrible translation of 'SVN Revision' to Japanese romaji might be ...");
ds@627
   616
		g_RootMenu.ConsolePrint("Hint: I TAKE IN AN AIR OK");
ds@627
   617
		break;
ds@627
   618
	case 5:
ds@627
   619
		g_RootMenu.ConsolePrint("Question Go (5)");
ds@627
   620
		g_RootMenu.ConsolePrint("What is a fundamental concept in the game of Go?");
ds@628
   621
		g_RootMenu.ConsolePrint("Hint: AD LADEN THIEF");
ds@627
   622
		break;
ds@627
   623
	case 6:
ds@627
   624
		g_RootMenu.ConsolePrint("Question Roku (6)");
ds@627
   625
		g_RootMenu.ConsolePrint("Why am I asking all these strange questions?");
ds@627
   626
		g_RootMenu.ConsolePrint("Hint: CHUBBY TITAN EATS EWE WAGE DATA");
ds@627
   627
		break;
ds@627
   628
	case 7:
ds@627
   629
		g_RootMenu.ConsolePrint("Question Nana (7)");
ds@627
   630
		g_RootMenu.ConsolePrint("What is my name?");
ds@627
   631
		g_RootMenu.ConsolePrint("Hint: AD MODE LAG US");
ds@627
   632
		break;
ds@627
   633
	default:
ds@627
   634
		break;
ds@627
   635
	}
ds@627
   636
ds@627
   637
	s_YamagramState = yamagram;
ds@627
   638
}
ds@627
   639
ds@627
   640
void _IntExt_CallYamagrams()
ds@627
   641
{
ds@627
   642
	bool correct = false;
ds@627
   643
	const char *arg = engine->Cmd_Args();
ds@627
   644
ds@627
   645
	if (!arg || arg[0] == '\0')
ds@627
   646
	{
ds@627
   647
		_YamagramPrinterTwoPointOhOh(s_YamagramState);
ds@627
   648
		return;
ds@627
   649
	}
ds@627
   650
ds@627
   651
	switch (s_YamagramState)
ds@627
   652
	{
ds@627
   653
	case 1:
ds@627
   654
		correct = !strcasecmp(arg, "eating grass");
ds@627
   655
		break;
ds@627
   656
	case 2:
ds@627
   657
		correct = !strcasecmp(arg, "sombrero");
ds@627
   658
		break;
ds@627
   659
	case 3:
ds@627
   660
		correct = !strcasecmp(arg, "david bailopan anderson");
ds@627
   661
		break;
ds@627
   662
	case 4:
ds@627
   663
		correct = !strcasecmp(arg, "kaitei no kairan");
ds@627
   664
		break;
ds@627
   665
	case 5:
ds@628
   666
		correct = !strcasecmp(arg, "life and death");
ds@627
   667
		break;
ds@627
   668
	case 6:
ds@627
   669
		correct = !strcasecmp(arg, "because gabe wanted it that way");
ds@627
   670
		if (correct)
ds@627
   671
		{
ds@627
   672
			g_RootMenu.ConsolePrint("Congratulations, you have answered 6 of my questions.");
ds@627
   673
			g_RootMenu.ConsolePrint("However, I have one final question for you. It wouldn't be nana without it.");
ds@627
   674
			g_RootMenu.ConsolePrint("-------------------------------");
ds@627
   675
			_YamagramPrinterTwoPointOhOh(7);
ds@627
   676
			return;
ds@627
   677
		}
ds@627
   678
		break;
ds@627
   679
	case 7:
ds@627
   680
		correct = !strcasecmp(arg, "damaged soul");
ds@627
   681
		if (correct)
ds@627
   682
		{
ds@627
   683
			g_RootMenu.ConsolePrint("You don't know how lucky you are to still be alive!");
ds@627
   684
			g_RootMenu.ConsolePrint("Congratulations. You have answered all 7 questions correctly.");
ds@627
   685
			g_RootMenu.ConsolePrint("The SourceMod Dev Team will be at your door with anti-Gaben grenades");
ds@627
   686
			g_RootMenu.ConsolePrint("within seconds. You will also be provided with a rocket launcher,");
ds@627
   687
			g_RootMenu.ConsolePrint("just in case Alfred decides to strike you with a blitzkrieg in retaliation.");
ds@627
   688
ds@627
   689
			s_YamagramState = 0;
ds@627
   690
			return;
ds@627
   691
		}
ds@627
   692
		break;
ds@627
   693
	default:
ds@627
   694
		break;
ds@627
   695
	}
ds@627
   696
ds@627
   697
	if (s_YamagramState > 0)
ds@627
   698
	{
ds@627
   699
		if (correct)
ds@627
   700
		{
ds@627
   701
			g_RootMenu.ConsolePrint("Correct! You are one step closer to avoiding the deadly jaws of Gaben.");
ds@627
   702
			g_RootMenu.ConsolePrint("-------------------------------");
ds@627
   703
			s_YamagramState++;
ds@627
   704
		} else {
ds@627
   705
			g_RootMenu.ConsolePrint("Wrong! You better be more careful. Gaben may be at your door at any minute.");
ds@627
   706
			return;
ds@627
   707
		}
ds@627
   708
	}
ds@627
   709
ds@627
   710
	_YamagramPrinterTwoPointOhOh(s_YamagramState);
ds@627
   711
}
ds@627
   712
ds@627
   713
void _IntExt_EnableYamagrams()
ds@627
   714
{
ds@627
   715
	static ConCommand *pCmd = NULL;
ds@627
   716
	if (!pCmd)
ds@627
   717
	{
ds@627
   718
		pCmd = new ConCommand("sm_nana", _IntExt_CallYamagrams, "Try these yamagrams!", FCVAR_GAMEDLL);
ds@627
   719
		g_RootMenu.ConsolePrint("[SM] Warning: Gaben has been alerted of your actions. You may be eaten.");
ds@627
   720
	} else {
ds@627
   721
		g_RootMenu.ConsolePrint("[SM] Gaben has already been alerted of your actions...");
ds@627
   722
	}
ds@627
   723
}
ds@627
   724
ds@627
   725
void _IntExt_OnHostnameChanged(ConVar *pConVar, char const *oldValue)
ds@627
   726
{
ds@627
   727
	if (strcmp(oldValue, "Good morning, DS-san.") == 0
ds@627
   728
		&& strcmp(pConVar->GetString(), "Good night, talking desk lamp.") == 0)
ds@627
   729
	{
ds@627
   730
		_IntExt_EnableYamagrams();
ds@627
   731
	}
ds@627
   732
}