core/ConVarManager.cpp
author David Anderson <dvander@alliedmods.net>
Wed Dec 05 18:07:18 2007 +0000 (2007-12-05)
changeset 1766 d30c252089be
parent 1759 7326ad131001
child 1770 9c545310f947
permissions -rw-r--r--
sm_dump_handles now shows memory usage
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>
dvander@1582
    40
#include <sm_trie_tpl.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;
dvander@1582
    49
KTrie<ConVarInfo *> convar_cache;
ds@582
    50
dvander@1581
    51
ConVarManager::ConVarManager() : m_ConVarType(0), m_bIsDLLQueryHooked(false), m_bIsVSPQueryHooked(false)
ds@580
    52
{
ds@580
    53
}
ds@580
    54
ds@580
    55
ConVarManager::~ConVarManager()
ds@580
    56
{
ds@580
    57
}
ds@580
    58
ds@580
    59
void ConVarManager::OnSourceModAllInitialized()
ds@580
    60
{
dvander@1581
    61
	/**
dvander@1581
    62
	 * Episode 2 has this function by default, but the older versions do not.
dvander@1581
    63
	 */
dvander@1581
    64
#if !defined ORANGEBOX_BUILD
ds@696
    65
	if (g_SMAPI->GetGameDLLVersion() >= 6)
ds@696
    66
	{
ds@696
    67
		SH_ADD_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
dvander@1581
    68
		m_bIsDLLQueryHooked = true;
ds@696
    69
	}
dvander@1759
    70
#endif
ds@696
    71
ds@580
    72
	HandleAccess sec;
ds@580
    73
ds@582
    74
	/* Set up access rights for the 'ConVar' handle type */
ds@580
    75
	sec.access[HandleAccess_Read] = 0;
ds@580
    76
	sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@580
    77
	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@580
    78
ds@582
    79
	/* Create the 'ConVar' handle type */
ds@580
    80
	m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL);
ds@580
    81
ds@582
    82
	/* Add the 'convars' option to the 'sm' console command */
ds@990
    83
	g_RootMenu.AddRootConsoleCommand("cvars", "View convars created by a plugin", this);
ds@580
    84
}
ds@580
    85
ds@580
    86
void ConVarManager::OnSourceModShutdown()
ds@580
    87
{
dvander@1582
    88
	List<ConVarInfo *>::iterator iter = m_ConVars.begin();
dvander@1582
    89
	HandleSecurity sec(NULL, g_pCoreIdent);
dvander@1582
    90
dvander@1582
    91
	/* Iterate list of ConVarInfo structures, remove every one of them */
dvander@1582
    92
	while (iter != m_ConVars.end())
dvander@1582
    93
	{
dvander@1582
    94
		ConVarInfo *pInfo = (*iter);
dvander@1582
    95
dvander@1582
    96
		iter = m_ConVars.erase(iter);
dvander@1582
    97
dvander@1582
    98
		g_HandleSys.FreeHandle(pInfo->handle, &sec);
dvander@1582
    99
		if (pInfo->pChangeForward != NULL)
dvander@1582
   100
		{
dvander@1582
   101
			g_Forwards.ReleaseForward(pInfo->pChangeForward);
dvander@1582
   102
		}
dvander@1582
   103
		if (pInfo->sourceMod)
dvander@1582
   104
		{
dvander@1582
   105
			/* If we created it, we won't be tracking it, therefore it is 
dvander@1582
   106
			 * safe to remove everything in one go.
dvander@1582
   107
			 */
dvander@1582
   108
			META_UNREGCVAR(pInfo->pVar);
dvander@1582
   109
			delete [] pInfo->pVar->GetName();
dvander@1582
   110
			delete [] pInfo->pVar->GetHelpText();
dvander@1582
   111
			delete [] pInfo->pVar->GetDefault();
dvander@1582
   112
			delete pInfo->pVar;
dvander@1582
   113
		}
dvander@1582
   114
		else
dvander@1582
   115
		{
dvander@1582
   116
			/* If we didn't create it, we might be tracking it.  Also, 
dvander@1582
   117
			 * it could be unreadable.
dvander@1582
   118
			 */
dvander@1582
   119
			UntrackConCommandBase(pInfo->pVar, this);
dvander@1582
   120
		}
dvander@1582
   121
dvander@1582
   122
		/* It's not safe to read the name here, so we simply delete the 
dvander@1582
   123
		 * the info struct and clear the lookup cache at the end.
dvander@1582
   124
		 */
dvander@1582
   125
		delete pInfo;
dvander@1582
   126
	}
dvander@1582
   127
	convar_cache.clear();
dvander@1582
   128
dvander@1582
   129
	/* Unhook things */
dvander@1581
   130
	if (m_bIsDLLQueryHooked)
ds@696
   131
	{
ds@696
   132
		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
dvander@1581
   133
		m_bIsDLLQueryHooked = false;
dvander@1581
   134
	}
dvander@1581
   135
	else if (m_bIsVSPQueryHooked)
dvander@1581
   136
	{
dvander@1581
   137
		SH_REMOVE_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, vsp_interface, this, &ConVarManager::OnQueryCvarValueFinished, false);
dvander@1581
   138
		m_bIsVSPQueryHooked = false;
ds@696
   139
	}
ds@696
   140
ds@582
   141
	/* Remove the 'convars' option from the 'sm' console command */
ds@990
   142
	g_RootMenu.RemoveRootConsoleCommand("cvars", this);
ds@580
   143
ds@582
   144
	/* Remove the 'ConVar' handle type */
ds@580
   145
	g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent);
ds@580
   146
}
ds@580
   147
dvander@1581
   148
/**
dvander@1581
   149
 * Orange Box will never use this.
dvander@1581
   150
 */
dvander@1581
   151
void ConVarManager::OnSourceModVSPReceived()
ds@696
   152
{
dvander@1581
   153
	/**
dvander@1581
   154
	 * Don't bother if the DLL is already hooked.
ds@696
   155
	 */
dvander@1581
   156
	if (m_bIsDLLQueryHooked)
ds@696
   157
	{
ds@696
   158
		return;
ds@696
   159
	}
ds@696
   160
dvander@1581
   161
	/* For later MM:S versions, use the updated API, since it's cleaner. */
dvander@1581
   162
#if defined METAMOD_PLAPI_VERSION
dvander@1581
   163
	int engine = g_SMAPI->GetSourceEngineBuild();
dvander@1581
   164
	if (engine == SOURCE_ENGINE_ORIGINAL || vsp_version < 2)
ds@696
   165
	{
dvander@1581
   166
		return;
dvander@1581
   167
	}
dvander@1581
   168
#else
dvander@1581
   169
	if (g_HL2.IsOriginalEngine() || vsp_version < 2)
dvander@1581
   170
	{
dvander@1581
   171
		return;
dvander@1581
   172
	}
dvander@1581
   173
#endif
ds@696
   174
dvander@1581
   175
	SH_ADD_HOOK_MEMFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished, vsp_interface, this, &ConVarManager::OnQueryCvarValueFinished, false);
dvander@1581
   176
	m_bIsVSPQueryHooked = true;
ds@696
   177
}
ds@696
   178
dvander@1582
   179
bool convar_cache_lookup(const char *name, ConVarInfo **pVar)
ds@1145
   180
{
dvander@1582
   181
	ConVarInfo **pLookup = convar_cache.retrieve(name);
dvander@1582
   182
	if (pLookup != NULL)
ds@1145
   183
	{
dvander@1582
   184
		*pVar = *pLookup;
dvander@1582
   185
		return true;
dvander@1582
   186
	}
dvander@1582
   187
	else
dvander@1582
   188
	{
dvander@1582
   189
		return false;
ds@1145
   190
	}
ds@1145
   191
}
ds@1145
   192
dvander@1582
   193
void ConVarManager::OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe)
dvander@1582
   194
{
dvander@1582
   195
	/* Only check convars that have not been created by SourceMod's core */
dvander@1582
   196
	ConVarInfo *pInfo;
dvander@1582
   197
	if (!convar_cache_lookup(name, &pInfo))
dvander@1582
   198
	{
dvander@1582
   199
		return;
dvander@1582
   200
	}
ds@1145
   201
ds@1145
   202
	HandleSecurity sec(NULL, g_pCoreIdent);
ds@1145
   203
dvander@1582
   204
	/* Remove it from our cache */
dvander@1582
   205
	m_ConVars.remove(pInfo);
dvander@1582
   206
	convar_cache.remove(name);
dvander@1582
   207
dvander@1582
   208
	/* Now make sure no plugins are referring to this pointer */
dvander@1582
   209
	IPluginIterator *pl_iter = g_PluginSys.GetPluginIterator();
dvander@1582
   210
	while (pl_iter->MorePlugins())
ds@1145
   211
	{
dvander@1582
   212
		IPlugin *pl = pl_iter->GetPlugin();
ds@1145
   213
dvander@1582
   214
		ConVarList *pConVarList;
dvander@1582
   215
		if (pl->GetProperty("ConVarList", (void **)&pConVarList, true) 
dvander@1582
   216
			&& pConVarList != NULL)
ds@1145
   217
		{
dvander@1582
   218
			pConVarList->remove(pInfo->pVar);
dvander@1582
   219
		}
ds@1145
   220
dvander@1582
   221
		pl_iter->NextPlugin();
ds@1145
   222
	}
dvander@1582
   223
dvander@1582
   224
	/* Free resources */
dvander@1582
   225
	g_HandleSys.FreeHandle(pInfo->handle, &sec);
dvander@1582
   226
	delete pInfo;
ds@1145
   227
}
ds@1145
   228
ds@582
   229
void ConVarManager::OnPluginUnloaded(IPlugin *plugin)
ds@580
   230
{
ds@582
   231
	ConVarList *pConVarList;
ds@580
   232
ds@582
   233
	/* If plugin has a convar list, free its memory */
ds@582
   234
	if (plugin->GetProperty("ConVarList", (void **)&pConVarList, true))
ds@580
   235
	{
ds@582
   236
		delete pConVarList;
ds@580
   237
	}
ds@580
   238
}
ds@580
   239
ds@580
   240
void ConVarManager::OnHandleDestroy(HandleType_t type, void *object)
ds@580
   241
{
ds@580
   242
}
ds@580
   243
dvander@1766
   244
bool ConVarManager::GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
dvander@1766
   245
{
dvander@1766
   246
	*pSize = sizeof(ConVar);
dvander@1766
   247
	return true;
dvander@1766
   248
}
dvander@1766
   249
faluco@1541
   250
void ConVarManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
ds@580
   251
{
faluco@1541
   252
	int argcount = command.ArgC();
ds@580
   253
	if (argcount >= 3)
ds@580
   254
	{
ds@582
   255
		/* Get plugin index that was passed */
faluco@1541
   256
		int id = atoi(command.Arg(2));
ds@580
   257
		
ds@582
   258
		/* Get plugin object */
ds@582
   259
		CPlugin *plugin = g_PluginSys.GetPluginByOrder(id);
ds@582
   260
ds@582
   261
		if (!plugin)
ds@580
   262
		{
ds@582
   263
			g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
ds@580
   264
			return;
ds@580
   265
		}
ds@580
   266
ds@582
   267
		/* Get plugin name */
ds@582
   268
		const sm_plugininfo_t *plinfo = plugin->GetPublicInfo();
ds@582
   269
		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : plugin->GetFilename();
ds@580
   270
ds@582
   271
		ConVarList *pConVarList;
ds@582
   272
		ConVarList::iterator iter;
ds@580
   273
ds@582
   274
		/* If no convar list... */
ds@582
   275
		if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
ds@580
   276
		{
ds@582
   277
			g_RootMenu.ConsolePrint("[SM] No convars found for: %s", plname);
ds@580
   278
			return;
ds@580
   279
		}
ds@580
   280
ds@582
   281
		g_RootMenu.ConsolePrint("[SM] Listing %d convars for: %s", pConVarList->size(), plname);
ds@582
   282
		g_RootMenu.ConsolePrint("  %-32.31s %s", "[Name]", "[Value]");
ds@580
   283
ds@582
   284
		/* Iterate convar list and display each one */
ds@582
   285
		for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
ds@580
   286
		{
ds@582
   287
			const ConVar *pConVar = (*iter);
ds@582
   288
			g_RootMenu.ConsolePrint("  %-32.31s %s", pConVar->GetName(), pConVar->GetString()); 
ds@580
   289
		}
ds@580
   290
ds@580
   291
		return;
ds@580
   292
	}
ds@580
   293
ds@582
   294
	/* Display usage of subcommand */
ds@580
   295
	g_RootMenu.ConsolePrint("[SM] Usage: sm convars <plugin #>");
ds@580
   296
}
ds@580
   297
ds@582
   298
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
   299
{
ds@580
   300
	ConVar *pConVar = NULL;
ds@582
   301
	ConVarInfo *pInfo = NULL;
ds@580
   302
	Handle_t hndl = 0;
ds@580
   303
ds@582
   304
	/* Find out if the convar exists already */
ds@580
   305
	pConVar = icvar->FindVar(name);
ds@580
   306
ds@582
   307
	/* If the convar already exists... */
ds@582
   308
	if (pConVar)
ds@580
   309
	{
ds@759
   310
		/* Add convar to plugin's list */
ds@759
   311
		AddConVarToPluginList(pContext, pConVar);
ds@759
   312
ds@582
   313
		/* First find out if we already have a handle to it */
dvander@1582
   314
		if (convar_cache_lookup(name, &pInfo))
ds@580
   315
		{
ds@582
   316
			return pInfo->handle;
dvander@1581
   317
		}
dvander@1581
   318
		else
dvander@1581
   319
		{
ds@582
   320
			/* Create and initialize ConVarInfo structure */
ds@582
   321
			pInfo = new ConVarInfo();
ds@582
   322
			pInfo->sourceMod = false;
ds@696
   323
			pInfo->pChangeForward = NULL;
ds@582
   324
			pInfo->origCallback = pConVar->GetCallback();
dvander@1582
   325
			pInfo->pVar = pConVar;
dvander@1582
   326
dvander@1582
   327
			/* If we don't, then create a new handle from the convar */
dvander@1582
   328
			hndl = g_HandleSys.CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
dvander@1582
   329
			if (hndl == BAD_HANDLE)
dvander@1582
   330
			{
dvander@1582
   331
				delete pInfo;
dvander@1582
   332
				return BAD_HANDLE;
dvander@1582
   333
			}
dvander@1582
   334
dvander@1582
   335
			pInfo->handle = hndl;
ds@580
   336
ds@582
   337
			/* Insert struct into caches */
ds@582
   338
			m_ConVars.push_back(pInfo);
dvander@1582
   339
			convar_cache.insert(name, pInfo);
dvander@1582
   340
			TrackConCommandBase(pConVar, this);
ds@580
   341
ds@580
   342
			return hndl;
ds@580
   343
		}
ds@580
   344
	}
ds@580
   345
ds@696
   346
	/* To prevent creating a convar that has the same name as a console command... ugh */
ds@580
   347
	ConCommandBase *pBase = icvar->GetCommands();
ds@580
   348
ds@580
   349
	while (pBase)
ds@580
   350
	{
ds@582
   351
		if (pBase->IsCommand() && strcmp(pBase->GetName(), name) == 0)
ds@580
   352
		{
ds@582
   353
			return BAD_HANDLE;
ds@580
   354
		}
ds@580
   355
ds@580
   356
		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
ds@580
   357
	}
ds@580
   358
ds@582
   359
	/* Create and initialize ConVarInfo structure */
ds@582
   360
	pInfo = new ConVarInfo();
ds@582
   361
	pInfo->handle = hndl;
ds@582
   362
	pInfo->sourceMod = true;
ds@696
   363
	pInfo->pChangeForward = NULL;
ds@582
   364
	pInfo->origCallback = NULL;
dvander@1582
   365
dvander@1582
   366
	/* Create a handle from the new convar */
dvander@1582
   367
	hndl = g_HandleSys.CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
dvander@1582
   368
	if (hndl == BAD_HANDLE)
dvander@1582
   369
	{
dvander@1582
   370
		delete pInfo;
dvander@1582
   371
		return BAD_HANDLE;
dvander@1582
   372
	}
dvander@1582
   373
dvander@1582
   374
	pInfo->handle = hndl;
dvander@1582
   375
dvander@1582
   376
	/* Since an existing convar (or concmd with the same name) was not found , now we can finally create it */
dvander@1582
   377
	pConVar = new ConVar(sm_strdup(name), sm_strdup(defaultVal), flags, sm_strdup(description), hasMin, min, hasMax, max);
dvander@1582
   378
	pInfo->pVar = pConVar;
dvander@1582
   379
dvander@1582
   380
	/* Add convar to plugin's list */
dvander@1582
   381
	AddConVarToPluginList(pContext, pConVar);
ds@580
   382
ds@582
   383
	/* Insert struct into caches */
ds@582
   384
	m_ConVars.push_back(pInfo);
dvander@1582
   385
	convar_cache.insert(name, pInfo);
ds@580
   386
ds@580
   387
	return hndl;
ds@580
   388
}
ds@580
   389
ds@580
   390
Handle_t ConVarManager::FindConVar(const char *name)
ds@580
   391
{
ds@580
   392
	ConVar *pConVar = NULL;
ds@582
   393
	ConVarInfo *pInfo;
ds@582
   394
	Handle_t hndl;
ds@580
   395
ds@582
   396
	/* Search for convar */
ds@580
   397
	pConVar = icvar->FindVar(name);
ds@580
   398
ds@582
   399
	/* If it doesn't exist, then return an invalid handle */
ds@582
   400
	if (!pConVar)
ds@580
   401
	{
ds@580
   402
		return BAD_HANDLE;
ds@580
   403
	}
ds@580
   404
ds@582
   405
	/* At this point, the convar exists. So, find out if we already have a handle */
dvander@1582
   406
	if (convar_cache_lookup(name, &pInfo))
ds@580
   407
	{
ds@582
   408
		return pInfo->handle;
ds@580
   409
	}
ds@580
   410
ds@729
   411
	/* Create and initialize ConVarInfo structure */
ds@582
   412
	pInfo = new ConVarInfo();
ds@582
   413
	pInfo->sourceMod = false;
ds@696
   414
	pInfo->pChangeForward = NULL;
ds@582
   415
	pInfo->origCallback = pConVar->GetCallback();
dvander@1582
   416
	pInfo->pVar = pConVar;
dvander@1582
   417
dvander@1582
   418
	/* If we don't have a handle, then create a new one */
dvander@1582
   419
	hndl = g_HandleSys.CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
dvander@1582
   420
	if (hndl == BAD_HANDLE)
dvander@1582
   421
	{
dvander@1582
   422
		delete pInfo;
dvander@1582
   423
		return BAD_HANDLE;
dvander@1582
   424
	}
dvander@1582
   425
dvander@1582
   426
	pInfo->handle = hndl;
ds@580
   427
ds@582
   428
	/* Insert struct into our caches */
ds@582
   429
	m_ConVars.push_back(pInfo);
dvander@1582
   430
	convar_cache.insert(name, pInfo);
dvander@1582
   431
	TrackConCommandBase(pConVar, this);
ds@580
   432
ds@580
   433
	return hndl;
ds@580
   434
}
ds@580
   435
ds@582
   436
void ConVarManager::HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
ds@580
   437
{
ds@582
   438
	ConVarInfo *pInfo;
ds@582
   439
	IChangeableForward *pForward;
ds@580
   440
ds@582
   441
	/* Find the convar in the lookup trie */
dvander@1582
   442
	if (convar_cache_lookup(pConVar->GetName(), &pInfo))
ds@580
   443
	{
ds@582
   444
		/* Get the forward */
ds@696
   445
		pForward = pInfo->pChangeForward;
ds@580
   446
ds@582
   447
		/* If forward does not exist, create it */
ds@582
   448
		if (!pForward)
ds@582
   449
		{
ds@582
   450
			pForward = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, CONVARCHANGE_PARAMS);
ds@696
   451
			pInfo->pChangeForward = pForward;
ds@580
   452
ds@582
   453
			/* Install our own callback */
ds@580
   454
			pConVar->InstallChangeCallback(OnConVarChanged);
ds@580
   455
		}
ds@582
   456
ds@582
   457
		/* Add function to forward's list */
ds@582
   458
		pForward->AddFunction(pFunction);
ds@580
   459
	}
ds@580
   460
}
ds@580
   461
ds@582
   462
void ConVarManager::UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
ds@580
   463
{
ds@582
   464
	ConVarInfo *pInfo;
ds@582
   465
	IChangeableForward *pForward;
ds@582
   466
	IPluginContext *pContext = pFunction->GetParentContext();
ds@580
   467
ds@582
   468
	/* Find the convar in the lookup trie */
dvander@1582
   469
	if (convar_cache_lookup(pConVar->GetName(), &pInfo))
ds@580
   470
	{
ds@582
   471
		/* Get the forward */
ds@696
   472
		pForward = pInfo->pChangeForward;
ds@580
   473
ds@582
   474
		/* If the forward doesn't exist, we can't unhook anything */
ds@582
   475
		if (!pForward)
ds@580
   476
		{
ds@580
   477
			pContext->ThrowNativeError("Convar \"%s\" has no active hook", pConVar->GetName());
ds@580
   478
			return;
ds@580
   479
		}
ds@580
   480
ds@582
   481
		/* Remove the function from the forward's list */
ds@582
   482
		if (!pForward->RemoveFunction(pFunction))
ds@580
   483
		{
ds@580
   484
			pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", pConVar->GetName());
ds@580
   485
			return;
ds@580
   486
		}
ds@580
   487
ds@582
   488
		/* If the forward now has 0 functions in it... */
ds@582
   489
		if (pForward->GetFunctionCount() == 0)
ds@580
   490
		{
ds@582
   491
			/* Free this forward */
ds@582
   492
			g_Forwards.ReleaseForward(pForward);
ds@696
   493
			pInfo->pChangeForward = NULL;
ds@582
   494
ds@582
   495
			/* Put back the original convar callback */
ds@582
   496
			pConVar->InstallChangeCallback(pInfo->origCallback);
ds@582
   497
		}
ds@582
   498
	}
ds@582
   499
}
ds@582
   500
ds@696
   501
QueryCvarCookie_t ConVarManager::QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback, Handle_t hndl)
ds@696
   502
{
ds@696
   503
	QueryCvarCookie_t cookie;
ds@696
   504
ds@696
   505
	/* Call StartQueryCvarValue() in either the IVEngineServer or IServerPluginHelpers depending on situation */
dvander@1581
   506
	if (m_bIsDLLQueryHooked)
ds@696
   507
	{
ds@696
   508
		cookie = engine->StartQueryCvarValue(pPlayer, name);	
dvander@1581
   509
	}
dvander@1581
   510
	else if (m_bIsVSPQueryHooked)
dvander@1581
   511
	{
ds@696
   512
		cookie = serverpluginhelpers->StartQueryCvarValue(pPlayer, name);
ds@696
   513
	}
dvander@1581
   514
	else
dvander@1581
   515
	{
dvander@1581
   516
		return InvalidQueryCvarCookie;
dvander@1581
   517
	}
ds@696
   518
ds@696
   519
	ConVarQuery query = {cookie, pCallback, hndl};
ds@696
   520
	m_ConVarQueries.push_back(query);
ds@696
   521
ds@696
   522
	return cookie;
ds@696
   523
}
ds@696
   524
ds@582
   525
void ConVarManager::AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar)
ds@582
   526
{
ds@582
   527
	ConVarList *pConVarList;
ds@582
   528
	ConVarList::iterator iter;
ds@582
   529
	bool inserted = false;
ds@582
   530
	const char *orig = pConVar->GetName();
ds@582
   531
ds@582
   532
	IPlugin *plugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@580
   533
ds@582
   534
	/* Check plugin for an existing convar list */
ds@582
   535
	if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
ds@582
   536
	{
ds@582
   537
		pConVarList = new ConVarList();
ds@582
   538
		plugin->SetProperty("ConVarList", pConVarList);
dvander@1581
   539
	}
dvander@1581
   540
	else if (pConVarList->find(pConVar) != pConVarList->end())
dvander@1581
   541
	{
ds@867
   542
		/* If convar is already in list, then don't add it */
ds@867
   543
		return;
ds@582
   544
	}
ds@582
   545
ds@582
   546
	/* Insert convar into list which is sorted alphabetically */
ds@582
   547
	for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
ds@582
   548
	{
ds@582
   549
		if (strcmp(orig, (*iter)->GetName()) < 0)
ds@582
   550
		{
ds@582
   551
			pConVarList->insert(iter, pConVar);
ds@582
   552
			inserted = true;
ds@582
   553
			break;
ds@580
   554
		}
ds@580
   555
	}
ds@582
   556
ds@582
   557
	if (!inserted)
ds@582
   558
	{
ds@582
   559
		pConVarList->push_back(pConVar);
ds@582
   560
	}
ds@580
   561
}
ds@580
   562
faluco@1541
   563
#if defined ORANGEBOX_BUILD
dvander@1544
   564
void ConVarManager::OnConVarChanged(IConVar *pIConVar, const char *oldValue, float flOldValue)
faluco@1541
   565
#else
ds@580
   566
void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue)
faluco@1541
   567
#endif
ds@580
   568
{
ds@582
   569
	/* If the values are the same, exit early in order to not trigger callbacks */
dvander@1544
   570
#if defined ORANGEBOX_BUILD
dvander@1544
   571
	ConVar *pConVar = (ConVar *)pIConVar;
dvander@1544
   572
#endif
dvander@1544
   573
	if (strcmp(pConVar->GetString(), oldValue) == 0)
ds@580
   574
	{
ds@580
   575
		return;
ds@580
   576
	}
ds@580
   577
ds@582
   578
	ConVarInfo *pInfo;
ds@580
   579
ds@582
   580
	/* Find the convar in the lookup trie */
dvander@1582
   581
	if (!convar_cache_lookup(pConVar->GetName(), &pInfo))
dvander@1582
   582
	{
dvander@1582
   583
		return;
dvander@1582
   584
	}
ds@580
   585
faluco@1541
   586
	FnChangeCallback_t origCallback = pInfo->origCallback;
ds@696
   587
	IChangeableForward *pForward = pInfo->pChangeForward;
ds@580
   588
ds@582
   589
	/* If there was a change callback installed previously, call it */
ds@580
   590
	if (origCallback)
ds@580
   591
	{
faluco@1541
   592
#if defined ORANGEBOX_BUILD
faluco@1541
   593
		origCallback(pConVar, oldValue, flOldValue);
faluco@1541
   594
#else
ds@580
   595
		origCallback(pConVar, oldValue);
faluco@1541
   596
#endif
ds@580
   597
	}
ds@580
   598
ds@582
   599
	/* Now call forwards in plugins that have hooked this */
ds@582
   600
	pForward->PushCell(pInfo->handle);
ds@582
   601
	pForward->PushString(oldValue);
faluco@1541
   602
	pForward->PushString(reinterpret_cast<ConVar *>(pConVar)->GetString());
ds@582
   603
	pForward->Execute(NULL);
ds@580
   604
}
ds@627
   605
dvander@1581
   606
bool ConVarManager::IsQueryingSupported()
dvander@1581
   607
{
dvander@1581
   608
	return (m_bIsDLLQueryHooked || m_bIsVSPQueryHooked);
dvander@1581
   609
}
dvander@1581
   610
ds@696
   611
void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
ds@696
   612
{
ds@696
   613
	IPluginFunction *pCallback = NULL;
ds@696
   614
	cell_t value = 0;
ds@696
   615
	List<ConVarQuery>::iterator iter;
ds@696
   616
ds@696
   617
	for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end(); iter++)
ds@696
   618
	{
ds@696
   619
		ConVarQuery &query = (*iter);
ds@696
   620
		if (query.cookie == cookie)
ds@696
   621
		{
ds@696
   622
			pCallback = query.pCallback;
ds@696
   623
			value = query.value;
ds@696
   624
			break;
ds@696
   625
		}
ds@696
   626
	}
ds@696
   627
ds@696
   628
	if (pCallback)
ds@696
   629
	{
ds@696
   630
		cell_t ret;
ds@696
   631
ds@696
   632
		pCallback->PushCell(cookie);
ds@696
   633
		pCallback->PushCell(engine->IndexOfEdict(pPlayer));
ds@696
   634
		pCallback->PushCell(result);
ds@696
   635
		pCallback->PushString(cvarName);
ds@696
   636
ds@696
   637
		if (result == eQueryCvarValueStatus_ValueIntact)
ds@696
   638
		{
ds@696
   639
			pCallback->PushString(cvarValue);
dvander@1581
   640
		}
dvander@1581
   641
		else
dvander@1581
   642
		{
ds@696
   643
			pCallback->PushString("\0");
ds@696
   644
		}
ds@696
   645
ds@696
   646
		pCallback->PushCell(value);
ds@696
   647
		pCallback->Execute(&ret);
ds@696
   648
ds@696
   649
		m_ConVarQueries.erase(iter);
ds@696
   650
	}
ds@696
   651
}
ds@696
   652
dvander@1582
   653
HandleError ConVarManager::ReadConVarHandle(Handle_t hndl, ConVar **pVar)
dvander@1582
   654
{
dvander@1582
   655
	ConVarInfo *pInfo;
dvander@1582
   656
	HandleError error;
dvander@1582
   657
	
dvander@1582
   658
	if ((error = g_HandleSys.ReadHandle(hndl, m_ConVarType, NULL, (void **)&pInfo)) != HandleError_None)
dvander@1582
   659
	{
dvander@1582
   660
		return error;
dvander@1582
   661
	}
dvander@1582
   662
dvander@1582
   663
	if (pVar)
dvander@1582
   664
	{
dvander@1582
   665
		*pVar = pInfo->pVar;
dvander@1582
   666
	}
dvander@1582
   667
dvander@1582
   668
	return error;
dvander@1582
   669
}
ds@696
   670
ds@627
   671
static int s_YamagramState = 0;
ds@627
   672
ds@627
   673
void _YamagramPrinterTwoPointOhOh(int yamagram)
ds@627
   674
{
ds@627
   675
	switch (yamagram)
ds@627
   676
	{
ds@627
   677
	case 0:
ds@627
   678
		g_RootMenu.ConsolePrint("Answer the following questions correctly and Gaben may not eat you after all.");
ds@627
   679
		g_RootMenu.ConsolePrint("You will be given one hint in the form of my patented yamagrams.");
ds@627
   680
		g_RootMenu.ConsolePrint("Type sm_nana to see the last question.");
ds@627
   681
		g_RootMenu.ConsolePrint("Type sm_nana <answer> to attempt an answer of the question.");
ds@627
   682
		g_RootMenu.ConsolePrint("-------------------------------");
ds@627
   683
		_YamagramPrinterTwoPointOhOh(1);
ds@627
   684
		return;
ds@627
   685
	case 1:
ds@627
   686
		g_RootMenu.ConsolePrint("Question Ichi (1)");
ds@627
   687
		g_RootMenu.ConsolePrint("One can turn into a cow by doing what action?");
ds@627
   688
		g_RootMenu.ConsolePrint("Hint: AGE SANS GRIT");
ds@627
   689
		break;
ds@627
   690
	case 2:
ds@627
   691
		g_RootMenu.ConsolePrint("Question Ni (2)");
ds@627
   692
		g_RootMenu.ConsolePrint("What kind of hat should you wear when using the Internet?");
ds@627
   693
		g_RootMenu.ConsolePrint("Hint: BRR MOOSE");
ds@627
   694
		break;
ds@627
   695
	case 3:
ds@627
   696
		g_RootMenu.ConsolePrint("Question San (3)");
ds@627
   697
		g_RootMenu.ConsolePrint("Who is the lead developer of SourceMod?");
ds@627
   698
		g_RootMenu.ConsolePrint("Hint: VEAL BANDANA DID RIP SOON");
ds@627
   699
		break;
ds@627
   700
	case 4:
ds@627
   701
		g_RootMenu.ConsolePrint("Question Yon (4)");
ds@627
   702
		g_RootMenu.ConsolePrint("A terrible translation of 'SVN Revision' to Japanese romaji might be ...");
ds@627
   703
		g_RootMenu.ConsolePrint("Hint: I TAKE IN AN AIR OK");
ds@627
   704
		break;
ds@627
   705
	case 5:
ds@627
   706
		g_RootMenu.ConsolePrint("Question Go (5)");
ds@627
   707
		g_RootMenu.ConsolePrint("What is a fundamental concept in the game of Go?");
ds@628
   708
		g_RootMenu.ConsolePrint("Hint: AD LADEN THIEF");
ds@627
   709
		break;
ds@627
   710
	case 6:
ds@627
   711
		g_RootMenu.ConsolePrint("Question Roku (6)");
ds@627
   712
		g_RootMenu.ConsolePrint("Why am I asking all these strange questions?");
ds@627
   713
		g_RootMenu.ConsolePrint("Hint: CHUBBY TITAN EATS EWE WAGE DATA");
ds@627
   714
		break;
ds@627
   715
	case 7:
ds@627
   716
		g_RootMenu.ConsolePrint("Question Nana (7)");
ds@627
   717
		g_RootMenu.ConsolePrint("What is my name?");
ds@627
   718
		g_RootMenu.ConsolePrint("Hint: AD MODE LAG US");
ds@627
   719
		break;
ds@627
   720
	default:
ds@627
   721
		break;
ds@627
   722
	}
ds@627
   723
ds@627
   724
	s_YamagramState = yamagram;
ds@627
   725
}
ds@627
   726
faluco@1541
   727
#if defined ORANGEBOX_BUILD
faluco@1541
   728
void _IntExt_CallYamagrams(const CCommand &cmd)
faluco@1541
   729
{
faluco@1541
   730
#else
ds@627
   731
void _IntExt_CallYamagrams()
ds@627
   732
{
faluco@1541
   733
	CCommand cmd;
faluco@1541
   734
#endif
ds@627
   735
	bool correct = false;
faluco@1541
   736
	const char *arg = cmd.ArgS();
ds@627
   737
ds@627
   738
	if (!arg || arg[0] == '\0')
ds@627
   739
	{
ds@627
   740
		_YamagramPrinterTwoPointOhOh(s_YamagramState);
ds@627
   741
		return;
ds@627
   742
	}
ds@627
   743
ds@627
   744
	switch (s_YamagramState)
ds@627
   745
	{
ds@627
   746
	case 1:
ds@627
   747
		correct = !strcasecmp(arg, "eating grass");
ds@627
   748
		break;
ds@627
   749
	case 2:
ds@627
   750
		correct = !strcasecmp(arg, "sombrero");
ds@627
   751
		break;
ds@627
   752
	case 3:
ds@627
   753
		correct = !strcasecmp(arg, "david bailopan anderson");
ds@627
   754
		break;
ds@627
   755
	case 4:
ds@627
   756
		correct = !strcasecmp(arg, "kaitei no kairan");
ds@627
   757
		break;
ds@627
   758
	case 5:
ds@628
   759
		correct = !strcasecmp(arg, "life and death");
ds@627
   760
		break;
ds@627
   761
	case 6:
ds@627
   762
		correct = !strcasecmp(arg, "because gabe wanted it that way");
ds@627
   763
		if (correct)
ds@627
   764
		{
ds@627
   765
			g_RootMenu.ConsolePrint("Congratulations, you have answered 6 of my questions.");
ds@627
   766
			g_RootMenu.ConsolePrint("However, I have one final question for you. It wouldn't be nana without it.");
ds@627
   767
			g_RootMenu.ConsolePrint("-------------------------------");
ds@627
   768
			_YamagramPrinterTwoPointOhOh(7);
ds@627
   769
			return;
ds@627
   770
		}
ds@627
   771
		break;
ds@627
   772
	case 7:
ds@627
   773
		correct = !strcasecmp(arg, "damaged soul");
ds@627
   774
		if (correct)
ds@627
   775
		{
ds@627
   776
			g_RootMenu.ConsolePrint("You don't know how lucky you are to still be alive!");
ds@627
   777
			g_RootMenu.ConsolePrint("Congratulations. You have answered all 7 questions correctly.");
ds@627
   778
			g_RootMenu.ConsolePrint("The SourceMod Dev Team will be at your door with anti-Gaben grenades");
ds@627
   779
			g_RootMenu.ConsolePrint("within seconds. You will also be provided with a rocket launcher,");
ds@627
   780
			g_RootMenu.ConsolePrint("just in case Alfred decides to strike you with a blitzkrieg in retaliation.");
ds@627
   781
ds@627
   782
			s_YamagramState = 0;
ds@627
   783
			return;
ds@627
   784
		}
ds@627
   785
		break;
ds@627
   786
	default:
ds@627
   787
		break;
ds@627
   788
	}
ds@627
   789
ds@627
   790
	if (s_YamagramState > 0)
ds@627
   791
	{
ds@627
   792
		if (correct)
ds@627
   793
		{
ds@627
   794
			g_RootMenu.ConsolePrint("Correct! You are one step closer to avoiding the deadly jaws of Gaben.");
ds@627
   795
			g_RootMenu.ConsolePrint("-------------------------------");
ds@627
   796
			s_YamagramState++;
ds@627
   797
		} else {
ds@627
   798
			g_RootMenu.ConsolePrint("Wrong! You better be more careful. Gaben may be at your door at any minute.");
ds@627
   799
			return;
ds@627
   800
		}
ds@627
   801
	}
ds@627
   802
ds@627
   803
	_YamagramPrinterTwoPointOhOh(s_YamagramState);
ds@627
   804
}
ds@627
   805
ds@627
   806
void _IntExt_EnableYamagrams()
ds@627
   807
{
ds@627
   808
	static ConCommand *pCmd = NULL;
ds@627
   809
	if (!pCmd)
ds@627
   810
	{
ds@627
   811
		pCmd = new ConCommand("sm_nana", _IntExt_CallYamagrams, "Try these yamagrams!", FCVAR_GAMEDLL);
ds@627
   812
		g_RootMenu.ConsolePrint("[SM] Warning: Gaben has been alerted of your actions. You may be eaten.");
ds@627
   813
	} else {
ds@627
   814
		g_RootMenu.ConsolePrint("[SM] Gaben has already been alerted of your actions...");
ds@627
   815
	}
ds@627
   816
}
ds@627
   817
faluco@1541
   818
#if defined ORANGEBOX_BUILD
faluco@1541
   819
void _IntExt_OnHostnameChanged(IConVar *pConVar, const char *oldValue, float flOldValue)
faluco@1541
   820
#else
ds@627
   821
void _IntExt_OnHostnameChanged(ConVar *pConVar, char const *oldValue)
faluco@1541
   822
#endif
ds@627
   823
{
ds@627
   824
	if (strcmp(oldValue, "Good morning, DS-san.") == 0
faluco@1541
   825
		&& strcmp(reinterpret_cast<ConVar *>(pConVar)->GetString(), "Good night, talking desk lamp.") == 0)
ds@627
   826
	{
ds@627
   827
		_IntExt_EnableYamagrams();
ds@627
   828
	}
ds@627
   829
}