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