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