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