core/ConVarManager.cpp
author David Anderson <dvander@alliedmods.net>
Sun Oct 21 17:58:35 2007 +0000 (2007-10-21)
changeset 1620 fd5c786ca8e5
parent 1582 6f18465dd9f9
child 1759 7326ad131001
permissions -rw-r--r--
atcprintf() is now fully re-entrant, meaning translations can be used from inside translations
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)
dvander@1581
    66
#endif
ds@696
    67
	{
ds@696
    68
		SH_ADD_HOOK_MEMFUNC(IServerGameDLL, OnQueryCvarValueFinished, gamedll, this, &ConVarManager::OnQueryCvarValueFinished, false);
dvander@1581
    69
		m_bIsDLLQueryHooked = true;
ds@696
    70
	}
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
faluco@1541
   244
void ConVarManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
ds@580
   245
{
faluco@1541
   246
	int argcount = command.ArgC();
ds@580
   247
	if (argcount >= 3)
ds@580
   248
	{
ds@582
   249
		/* Get plugin index that was passed */
faluco@1541
   250
		int id = atoi(command.Arg(2));
ds@580
   251
		
ds@582
   252
		/* Get plugin object */
ds@582
   253
		CPlugin *plugin = g_PluginSys.GetPluginByOrder(id);
ds@582
   254
ds@582
   255
		if (!plugin)
ds@580
   256
		{
ds@582
   257
			g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
ds@580
   258
			return;
ds@580
   259
		}
ds@580
   260
ds@582
   261
		/* Get plugin name */
ds@582
   262
		const sm_plugininfo_t *plinfo = plugin->GetPublicInfo();
ds@582
   263
		const char *plname = IS_STR_FILLED(plinfo->name) ? plinfo->name : plugin->GetFilename();
ds@580
   264
ds@582
   265
		ConVarList *pConVarList;
ds@582
   266
		ConVarList::iterator iter;
ds@580
   267
ds@582
   268
		/* If no convar list... */
ds@582
   269
		if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
ds@580
   270
		{
ds@582
   271
			g_RootMenu.ConsolePrint("[SM] No convars found for: %s", plname);
ds@580
   272
			return;
ds@580
   273
		}
ds@580
   274
ds@582
   275
		g_RootMenu.ConsolePrint("[SM] Listing %d convars for: %s", pConVarList->size(), plname);
ds@582
   276
		g_RootMenu.ConsolePrint("  %-32.31s %s", "[Name]", "[Value]");
ds@580
   277
ds@582
   278
		/* Iterate convar list and display each one */
ds@582
   279
		for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
ds@580
   280
		{
ds@582
   281
			const ConVar *pConVar = (*iter);
ds@582
   282
			g_RootMenu.ConsolePrint("  %-32.31s %s", pConVar->GetName(), pConVar->GetString()); 
ds@580
   283
		}
ds@580
   284
ds@580
   285
		return;
ds@580
   286
	}
ds@580
   287
ds@582
   288
	/* Display usage of subcommand */
ds@580
   289
	g_RootMenu.ConsolePrint("[SM] Usage: sm convars <plugin #>");
ds@580
   290
}
ds@580
   291
ds@582
   292
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
   293
{
ds@580
   294
	ConVar *pConVar = NULL;
ds@582
   295
	ConVarInfo *pInfo = NULL;
ds@580
   296
	Handle_t hndl = 0;
ds@580
   297
ds@582
   298
	/* Find out if the convar exists already */
ds@580
   299
	pConVar = icvar->FindVar(name);
ds@580
   300
ds@582
   301
	/* If the convar already exists... */
ds@582
   302
	if (pConVar)
ds@580
   303
	{
ds@759
   304
		/* Add convar to plugin's list */
ds@759
   305
		AddConVarToPluginList(pContext, pConVar);
ds@759
   306
ds@582
   307
		/* First find out if we already have a handle to it */
dvander@1582
   308
		if (convar_cache_lookup(name, &pInfo))
ds@580
   309
		{
ds@582
   310
			return pInfo->handle;
dvander@1581
   311
		}
dvander@1581
   312
		else
dvander@1581
   313
		{
ds@582
   314
			/* Create and initialize ConVarInfo structure */
ds@582
   315
			pInfo = new ConVarInfo();
ds@582
   316
			pInfo->sourceMod = false;
ds@696
   317
			pInfo->pChangeForward = NULL;
ds@582
   318
			pInfo->origCallback = pConVar->GetCallback();
dvander@1582
   319
			pInfo->pVar = pConVar;
dvander@1582
   320
dvander@1582
   321
			/* If we don't, then create a new handle from the convar */
dvander@1582
   322
			hndl = g_HandleSys.CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
dvander@1582
   323
			if (hndl == BAD_HANDLE)
dvander@1582
   324
			{
dvander@1582
   325
				delete pInfo;
dvander@1582
   326
				return BAD_HANDLE;
dvander@1582
   327
			}
dvander@1582
   328
dvander@1582
   329
			pInfo->handle = hndl;
ds@580
   330
ds@582
   331
			/* Insert struct into caches */
ds@582
   332
			m_ConVars.push_back(pInfo);
dvander@1582
   333
			convar_cache.insert(name, pInfo);
dvander@1582
   334
			TrackConCommandBase(pConVar, this);
ds@580
   335
ds@580
   336
			return hndl;
ds@580
   337
		}
ds@580
   338
	}
ds@580
   339
ds@696
   340
	/* To prevent creating a convar that has the same name as a console command... ugh */
ds@580
   341
	ConCommandBase *pBase = icvar->GetCommands();
ds@580
   342
ds@580
   343
	while (pBase)
ds@580
   344
	{
ds@582
   345
		if (pBase->IsCommand() && strcmp(pBase->GetName(), name) == 0)
ds@580
   346
		{
ds@582
   347
			return BAD_HANDLE;
ds@580
   348
		}
ds@580
   349
ds@580
   350
		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
ds@580
   351
	}
ds@580
   352
ds@582
   353
	/* Create and initialize ConVarInfo structure */
ds@582
   354
	pInfo = new ConVarInfo();
ds@582
   355
	pInfo->handle = hndl;
ds@582
   356
	pInfo->sourceMod = true;
ds@696
   357
	pInfo->pChangeForward = NULL;
ds@582
   358
	pInfo->origCallback = NULL;
dvander@1582
   359
dvander@1582
   360
	/* Create a handle from the new convar */
dvander@1582
   361
	hndl = g_HandleSys.CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
dvander@1582
   362
	if (hndl == BAD_HANDLE)
dvander@1582
   363
	{
dvander@1582
   364
		delete pInfo;
dvander@1582
   365
		return BAD_HANDLE;
dvander@1582
   366
	}
dvander@1582
   367
dvander@1582
   368
	pInfo->handle = hndl;
dvander@1582
   369
dvander@1582
   370
	/* Since an existing convar (or concmd with the same name) was not found , now we can finally create it */
dvander@1582
   371
	pConVar = new ConVar(sm_strdup(name), sm_strdup(defaultVal), flags, sm_strdup(description), hasMin, min, hasMax, max);
dvander@1582
   372
	pInfo->pVar = pConVar;
dvander@1582
   373
dvander@1582
   374
	/* Add convar to plugin's list */
dvander@1582
   375
	AddConVarToPluginList(pContext, pConVar);
ds@580
   376
ds@582
   377
	/* Insert struct into caches */
ds@582
   378
	m_ConVars.push_back(pInfo);
dvander@1582
   379
	convar_cache.insert(name, pInfo);
ds@580
   380
ds@580
   381
	return hndl;
ds@580
   382
}
ds@580
   383
ds@580
   384
Handle_t ConVarManager::FindConVar(const char *name)
ds@580
   385
{
ds@580
   386
	ConVar *pConVar = NULL;
ds@582
   387
	ConVarInfo *pInfo;
ds@582
   388
	Handle_t hndl;
ds@580
   389
ds@582
   390
	/* Search for convar */
ds@580
   391
	pConVar = icvar->FindVar(name);
ds@580
   392
ds@582
   393
	/* If it doesn't exist, then return an invalid handle */
ds@582
   394
	if (!pConVar)
ds@580
   395
	{
ds@580
   396
		return BAD_HANDLE;
ds@580
   397
	}
ds@580
   398
ds@582
   399
	/* At this point, the convar exists. So, find out if we already have a handle */
dvander@1582
   400
	if (convar_cache_lookup(name, &pInfo))
ds@580
   401
	{
ds@582
   402
		return pInfo->handle;
ds@580
   403
	}
ds@580
   404
ds@729
   405
	/* Create and initialize ConVarInfo structure */
ds@582
   406
	pInfo = new ConVarInfo();
ds@582
   407
	pInfo->sourceMod = false;
ds@696
   408
	pInfo->pChangeForward = NULL;
ds@582
   409
	pInfo->origCallback = pConVar->GetCallback();
dvander@1582
   410
	pInfo->pVar = pConVar;
dvander@1582
   411
dvander@1582
   412
	/* If we don't have a handle, then create a new one */
dvander@1582
   413
	hndl = g_HandleSys.CreateHandle(m_ConVarType, pInfo, NULL, g_pCoreIdent, NULL);
dvander@1582
   414
	if (hndl == BAD_HANDLE)
dvander@1582
   415
	{
dvander@1582
   416
		delete pInfo;
dvander@1582
   417
		return BAD_HANDLE;
dvander@1582
   418
	}
dvander@1582
   419
dvander@1582
   420
	pInfo->handle = hndl;
ds@580
   421
ds@582
   422
	/* Insert struct into our caches */
ds@582
   423
	m_ConVars.push_back(pInfo);
dvander@1582
   424
	convar_cache.insert(name, pInfo);
dvander@1582
   425
	TrackConCommandBase(pConVar, this);
ds@580
   426
ds@580
   427
	return hndl;
ds@580
   428
}
ds@580
   429
ds@582
   430
void ConVarManager::HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
ds@580
   431
{
ds@582
   432
	ConVarInfo *pInfo;
ds@582
   433
	IChangeableForward *pForward;
ds@580
   434
ds@582
   435
	/* Find the convar in the lookup trie */
dvander@1582
   436
	if (convar_cache_lookup(pConVar->GetName(), &pInfo))
ds@580
   437
	{
ds@582
   438
		/* Get the forward */
ds@696
   439
		pForward = pInfo->pChangeForward;
ds@580
   440
ds@582
   441
		/* If forward does not exist, create it */
ds@582
   442
		if (!pForward)
ds@582
   443
		{
ds@582
   444
			pForward = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, CONVARCHANGE_PARAMS);
ds@696
   445
			pInfo->pChangeForward = pForward;
ds@580
   446
ds@582
   447
			/* Install our own callback */
ds@580
   448
			pConVar->InstallChangeCallback(OnConVarChanged);
ds@580
   449
		}
ds@582
   450
ds@582
   451
		/* Add function to forward's list */
ds@582
   452
		pForward->AddFunction(pFunction);
ds@580
   453
	}
ds@580
   454
}
ds@580
   455
ds@582
   456
void ConVarManager::UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction)
ds@580
   457
{
ds@582
   458
	ConVarInfo *pInfo;
ds@582
   459
	IChangeableForward *pForward;
ds@582
   460
	IPluginContext *pContext = pFunction->GetParentContext();
ds@580
   461
ds@582
   462
	/* Find the convar in the lookup trie */
dvander@1582
   463
	if (convar_cache_lookup(pConVar->GetName(), &pInfo))
ds@580
   464
	{
ds@582
   465
		/* Get the forward */
ds@696
   466
		pForward = pInfo->pChangeForward;
ds@580
   467
ds@582
   468
		/* If the forward doesn't exist, we can't unhook anything */
ds@582
   469
		if (!pForward)
ds@580
   470
		{
ds@580
   471
			pContext->ThrowNativeError("Convar \"%s\" has no active hook", pConVar->GetName());
ds@580
   472
			return;
ds@580
   473
		}
ds@580
   474
ds@582
   475
		/* Remove the function from the forward's list */
ds@582
   476
		if (!pForward->RemoveFunction(pFunction))
ds@580
   477
		{
ds@580
   478
			pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", pConVar->GetName());
ds@580
   479
			return;
ds@580
   480
		}
ds@580
   481
ds@582
   482
		/* If the forward now has 0 functions in it... */
ds@582
   483
		if (pForward->GetFunctionCount() == 0)
ds@580
   484
		{
ds@582
   485
			/* Free this forward */
ds@582
   486
			g_Forwards.ReleaseForward(pForward);
ds@696
   487
			pInfo->pChangeForward = NULL;
ds@582
   488
ds@582
   489
			/* Put back the original convar callback */
ds@582
   490
			pConVar->InstallChangeCallback(pInfo->origCallback);
ds@582
   491
		}
ds@582
   492
	}
ds@582
   493
}
ds@582
   494
ds@696
   495
QueryCvarCookie_t ConVarManager::QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback, Handle_t hndl)
ds@696
   496
{
ds@696
   497
	QueryCvarCookie_t cookie;
ds@696
   498
ds@696
   499
	/* Call StartQueryCvarValue() in either the IVEngineServer or IServerPluginHelpers depending on situation */
dvander@1581
   500
	if (m_bIsDLLQueryHooked)
ds@696
   501
	{
ds@696
   502
		cookie = engine->StartQueryCvarValue(pPlayer, name);	
dvander@1581
   503
	}
dvander@1581
   504
	else if (m_bIsVSPQueryHooked)
dvander@1581
   505
	{
ds@696
   506
		cookie = serverpluginhelpers->StartQueryCvarValue(pPlayer, name);
ds@696
   507
	}
dvander@1581
   508
	else
dvander@1581
   509
	{
dvander@1581
   510
		return InvalidQueryCvarCookie;
dvander@1581
   511
	}
ds@696
   512
ds@696
   513
	ConVarQuery query = {cookie, pCallback, hndl};
ds@696
   514
	m_ConVarQueries.push_back(query);
ds@696
   515
ds@696
   516
	return cookie;
ds@696
   517
}
ds@696
   518
ds@582
   519
void ConVarManager::AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar)
ds@582
   520
{
ds@582
   521
	ConVarList *pConVarList;
ds@582
   522
	ConVarList::iterator iter;
ds@582
   523
	bool inserted = false;
ds@582
   524
	const char *orig = pConVar->GetName();
ds@582
   525
ds@582
   526
	IPlugin *plugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@580
   527
ds@582
   528
	/* Check plugin for an existing convar list */
ds@582
   529
	if (!plugin->GetProperty("ConVarList", (void **)&pConVarList))
ds@582
   530
	{
ds@582
   531
		pConVarList = new ConVarList();
ds@582
   532
		plugin->SetProperty("ConVarList", pConVarList);
dvander@1581
   533
	}
dvander@1581
   534
	else if (pConVarList->find(pConVar) != pConVarList->end())
dvander@1581
   535
	{
ds@867
   536
		/* If convar is already in list, then don't add it */
ds@867
   537
		return;
ds@582
   538
	}
ds@582
   539
ds@582
   540
	/* Insert convar into list which is sorted alphabetically */
ds@582
   541
	for (iter = pConVarList->begin(); iter != pConVarList->end(); iter++)
ds@582
   542
	{
ds@582
   543
		if (strcmp(orig, (*iter)->GetName()) < 0)
ds@582
   544
		{
ds@582
   545
			pConVarList->insert(iter, pConVar);
ds@582
   546
			inserted = true;
ds@582
   547
			break;
ds@580
   548
		}
ds@580
   549
	}
ds@582
   550
ds@582
   551
	if (!inserted)
ds@582
   552
	{
ds@582
   553
		pConVarList->push_back(pConVar);
ds@582
   554
	}
ds@580
   555
}
ds@580
   556
faluco@1541
   557
#if defined ORANGEBOX_BUILD
dvander@1544
   558
void ConVarManager::OnConVarChanged(IConVar *pIConVar, const char *oldValue, float flOldValue)
faluco@1541
   559
#else
ds@580
   560
void ConVarManager::OnConVarChanged(ConVar *pConVar, const char *oldValue)
faluco@1541
   561
#endif
ds@580
   562
{
ds@582
   563
	/* If the values are the same, exit early in order to not trigger callbacks */
dvander@1544
   564
#if defined ORANGEBOX_BUILD
dvander@1544
   565
	ConVar *pConVar = (ConVar *)pIConVar;
dvander@1544
   566
#endif
dvander@1544
   567
	if (strcmp(pConVar->GetString(), oldValue) == 0)
ds@580
   568
	{
ds@580
   569
		return;
ds@580
   570
	}
ds@580
   571
ds@582
   572
	ConVarInfo *pInfo;
ds@580
   573
ds@582
   574
	/* Find the convar in the lookup trie */
dvander@1582
   575
	if (!convar_cache_lookup(pConVar->GetName(), &pInfo))
dvander@1582
   576
	{
dvander@1582
   577
		return;
dvander@1582
   578
	}
ds@580
   579
faluco@1541
   580
	FnChangeCallback_t origCallback = pInfo->origCallback;
ds@696
   581
	IChangeableForward *pForward = pInfo->pChangeForward;
ds@580
   582
ds@582
   583
	/* If there was a change callback installed previously, call it */
ds@580
   584
	if (origCallback)
ds@580
   585
	{
faluco@1541
   586
#if defined ORANGEBOX_BUILD
faluco@1541
   587
		origCallback(pConVar, oldValue, flOldValue);
faluco@1541
   588
#else
ds@580
   589
		origCallback(pConVar, oldValue);
faluco@1541
   590
#endif
ds@580
   591
	}
ds@580
   592
ds@582
   593
	/* Now call forwards in plugins that have hooked this */
ds@582
   594
	pForward->PushCell(pInfo->handle);
ds@582
   595
	pForward->PushString(oldValue);
faluco@1541
   596
	pForward->PushString(reinterpret_cast<ConVar *>(pConVar)->GetString());
ds@582
   597
	pForward->Execute(NULL);
ds@580
   598
}
ds@627
   599
dvander@1581
   600
bool ConVarManager::IsQueryingSupported()
dvander@1581
   601
{
dvander@1581
   602
	return (m_bIsDLLQueryHooked || m_bIsVSPQueryHooked);
dvander@1581
   603
}
dvander@1581
   604
ds@696
   605
void ConVarManager::OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result, const char *cvarName, const char *cvarValue)
ds@696
   606
{
ds@696
   607
	IPluginFunction *pCallback = NULL;
ds@696
   608
	cell_t value = 0;
ds@696
   609
	List<ConVarQuery>::iterator iter;
ds@696
   610
ds@696
   611
	for (iter = m_ConVarQueries.begin(); iter != m_ConVarQueries.end(); iter++)
ds@696
   612
	{
ds@696
   613
		ConVarQuery &query = (*iter);
ds@696
   614
		if (query.cookie == cookie)
ds@696
   615
		{
ds@696
   616
			pCallback = query.pCallback;
ds@696
   617
			value = query.value;
ds@696
   618
			break;
ds@696
   619
		}
ds@696
   620
	}
ds@696
   621
ds@696
   622
	if (pCallback)
ds@696
   623
	{
ds@696
   624
		cell_t ret;
ds@696
   625
ds@696
   626
		pCallback->PushCell(cookie);
ds@696
   627
		pCallback->PushCell(engine->IndexOfEdict(pPlayer));
ds@696
   628
		pCallback->PushCell(result);
ds@696
   629
		pCallback->PushString(cvarName);
ds@696
   630
ds@696
   631
		if (result == eQueryCvarValueStatus_ValueIntact)
ds@696
   632
		{
ds@696
   633
			pCallback->PushString(cvarValue);
dvander@1581
   634
		}
dvander@1581
   635
		else
dvander@1581
   636
		{
ds@696
   637
			pCallback->PushString("\0");
ds@696
   638
		}
ds@696
   639
ds@696
   640
		pCallback->PushCell(value);
ds@696
   641
		pCallback->Execute(&ret);
ds@696
   642
ds@696
   643
		m_ConVarQueries.erase(iter);
ds@696
   644
	}
ds@696
   645
}
ds@696
   646
dvander@1582
   647
HandleError ConVarManager::ReadConVarHandle(Handle_t hndl, ConVar **pVar)
dvander@1582
   648
{
dvander@1582
   649
	ConVarInfo *pInfo;
dvander@1582
   650
	HandleError error;
dvander@1582
   651
	
dvander@1582
   652
	if ((error = g_HandleSys.ReadHandle(hndl, m_ConVarType, NULL, (void **)&pInfo)) != HandleError_None)
dvander@1582
   653
	{
dvander@1582
   654
		return error;
dvander@1582
   655
	}
dvander@1582
   656
dvander@1582
   657
	if (pVar)
dvander@1582
   658
	{
dvander@1582
   659
		*pVar = pInfo->pVar;
dvander@1582
   660
	}
dvander@1582
   661
dvander@1582
   662
	return error;
dvander@1582
   663
}
ds@696
   664
ds@627
   665
static int s_YamagramState = 0;
ds@627
   666
ds@627
   667
void _YamagramPrinterTwoPointOhOh(int yamagram)
ds@627
   668
{
ds@627
   669
	switch (yamagram)
ds@627
   670
	{
ds@627
   671
	case 0:
ds@627
   672
		g_RootMenu.ConsolePrint("Answer the following questions correctly and Gaben may not eat you after all.");
ds@627
   673
		g_RootMenu.ConsolePrint("You will be given one hint in the form of my patented yamagrams.");
ds@627
   674
		g_RootMenu.ConsolePrint("Type sm_nana to see the last question.");
ds@627
   675
		g_RootMenu.ConsolePrint("Type sm_nana <answer> to attempt an answer of the question.");
ds@627
   676
		g_RootMenu.ConsolePrint("-------------------------------");
ds@627
   677
		_YamagramPrinterTwoPointOhOh(1);
ds@627
   678
		return;
ds@627
   679
	case 1:
ds@627
   680
		g_RootMenu.ConsolePrint("Question Ichi (1)");
ds@627
   681
		g_RootMenu.ConsolePrint("One can turn into a cow by doing what action?");
ds@627
   682
		g_RootMenu.ConsolePrint("Hint: AGE SANS GRIT");
ds@627
   683
		break;
ds@627
   684
	case 2:
ds@627
   685
		g_RootMenu.ConsolePrint("Question Ni (2)");
ds@627
   686
		g_RootMenu.ConsolePrint("What kind of hat should you wear when using the Internet?");
ds@627
   687
		g_RootMenu.ConsolePrint("Hint: BRR MOOSE");
ds@627
   688
		break;
ds@627
   689
	case 3:
ds@627
   690
		g_RootMenu.ConsolePrint("Question San (3)");
ds@627
   691
		g_RootMenu.ConsolePrint("Who is the lead developer of SourceMod?");
ds@627
   692
		g_RootMenu.ConsolePrint("Hint: VEAL BANDANA DID RIP SOON");
ds@627
   693
		break;
ds@627
   694
	case 4:
ds@627
   695
		g_RootMenu.ConsolePrint("Question Yon (4)");
ds@627
   696
		g_RootMenu.ConsolePrint("A terrible translation of 'SVN Revision' to Japanese romaji might be ...");
ds@627
   697
		g_RootMenu.ConsolePrint("Hint: I TAKE IN AN AIR OK");
ds@627
   698
		break;
ds@627
   699
	case 5:
ds@627
   700
		g_RootMenu.ConsolePrint("Question Go (5)");
ds@627
   701
		g_RootMenu.ConsolePrint("What is a fundamental concept in the game of Go?");
ds@628
   702
		g_RootMenu.ConsolePrint("Hint: AD LADEN THIEF");
ds@627
   703
		break;
ds@627
   704
	case 6:
ds@627
   705
		g_RootMenu.ConsolePrint("Question Roku (6)");
ds@627
   706
		g_RootMenu.ConsolePrint("Why am I asking all these strange questions?");
ds@627
   707
		g_RootMenu.ConsolePrint("Hint: CHUBBY TITAN EATS EWE WAGE DATA");
ds@627
   708
		break;
ds@627
   709
	case 7:
ds@627
   710
		g_RootMenu.ConsolePrint("Question Nana (7)");
ds@627
   711
		g_RootMenu.ConsolePrint("What is my name?");
ds@627
   712
		g_RootMenu.ConsolePrint("Hint: AD MODE LAG US");
ds@627
   713
		break;
ds@627
   714
	default:
ds@627
   715
		break;
ds@627
   716
	}
ds@627
   717
ds@627
   718
	s_YamagramState = yamagram;
ds@627
   719
}
ds@627
   720
faluco@1541
   721
#if defined ORANGEBOX_BUILD
faluco@1541
   722
void _IntExt_CallYamagrams(const CCommand &cmd)
faluco@1541
   723
{
faluco@1541
   724
#else
ds@627
   725
void _IntExt_CallYamagrams()
ds@627
   726
{
faluco@1541
   727
	CCommand cmd;
faluco@1541
   728
#endif
ds@627
   729
	bool correct = false;
faluco@1541
   730
	const char *arg = cmd.ArgS();
ds@627
   731
ds@627
   732
	if (!arg || arg[0] == '\0')
ds@627
   733
	{
ds@627
   734
		_YamagramPrinterTwoPointOhOh(s_YamagramState);
ds@627
   735
		return;
ds@627
   736
	}
ds@627
   737
ds@627
   738
	switch (s_YamagramState)
ds@627
   739
	{
ds@627
   740
	case 1:
ds@627
   741
		correct = !strcasecmp(arg, "eating grass");
ds@627
   742
		break;
ds@627
   743
	case 2:
ds@627
   744
		correct = !strcasecmp(arg, "sombrero");
ds@627
   745
		break;
ds@627
   746
	case 3:
ds@627
   747
		correct = !strcasecmp(arg, "david bailopan anderson");
ds@627
   748
		break;
ds@627
   749
	case 4:
ds@627
   750
		correct = !strcasecmp(arg, "kaitei no kairan");
ds@627
   751
		break;
ds@627
   752
	case 5:
ds@628
   753
		correct = !strcasecmp(arg, "life and death");
ds@627
   754
		break;
ds@627
   755
	case 6:
ds@627
   756
		correct = !strcasecmp(arg, "because gabe wanted it that way");
ds@627
   757
		if (correct)
ds@627
   758
		{
ds@627
   759
			g_RootMenu.ConsolePrint("Congratulations, you have answered 6 of my questions.");
ds@627
   760
			g_RootMenu.ConsolePrint("However, I have one final question for you. It wouldn't be nana without it.");
ds@627
   761
			g_RootMenu.ConsolePrint("-------------------------------");
ds@627
   762
			_YamagramPrinterTwoPointOhOh(7);
ds@627
   763
			return;
ds@627
   764
		}
ds@627
   765
		break;
ds@627
   766
	case 7:
ds@627
   767
		correct = !strcasecmp(arg, "damaged soul");
ds@627
   768
		if (correct)
ds@627
   769
		{
ds@627
   770
			g_RootMenu.ConsolePrint("You don't know how lucky you are to still be alive!");
ds@627
   771
			g_RootMenu.ConsolePrint("Congratulations. You have answered all 7 questions correctly.");
ds@627
   772
			g_RootMenu.ConsolePrint("The SourceMod Dev Team will be at your door with anti-Gaben grenades");
ds@627
   773
			g_RootMenu.ConsolePrint("within seconds. You will also be provided with a rocket launcher,");
ds@627
   774
			g_RootMenu.ConsolePrint("just in case Alfred decides to strike you with a blitzkrieg in retaliation.");
ds@627
   775
ds@627
   776
			s_YamagramState = 0;
ds@627
   777
			return;
ds@627
   778
		}
ds@627
   779
		break;
ds@627
   780
	default:
ds@627
   781
		break;
ds@627
   782
	}
ds@627
   783
ds@627
   784
	if (s_YamagramState > 0)
ds@627
   785
	{
ds@627
   786
		if (correct)
ds@627
   787
		{
ds@627
   788
			g_RootMenu.ConsolePrint("Correct! You are one step closer to avoiding the deadly jaws of Gaben.");
ds@627
   789
			g_RootMenu.ConsolePrint("-------------------------------");
ds@627
   790
			s_YamagramState++;
ds@627
   791
		} else {
ds@627
   792
			g_RootMenu.ConsolePrint("Wrong! You better be more careful. Gaben may be at your door at any minute.");
ds@627
   793
			return;
ds@627
   794
		}
ds@627
   795
	}
ds@627
   796
ds@627
   797
	_YamagramPrinterTwoPointOhOh(s_YamagramState);
ds@627
   798
}
ds@627
   799
ds@627
   800
void _IntExt_EnableYamagrams()
ds@627
   801
{
ds@627
   802
	static ConCommand *pCmd = NULL;
ds@627
   803
	if (!pCmd)
ds@627
   804
	{
ds@627
   805
		pCmd = new ConCommand("sm_nana", _IntExt_CallYamagrams, "Try these yamagrams!", FCVAR_GAMEDLL);
ds@627
   806
		g_RootMenu.ConsolePrint("[SM] Warning: Gaben has been alerted of your actions. You may be eaten.");
ds@627
   807
	} else {
ds@627
   808
		g_RootMenu.ConsolePrint("[SM] Gaben has already been alerted of your actions...");
ds@627
   809
	}
ds@627
   810
}
ds@627
   811
faluco@1541
   812
#if defined ORANGEBOX_BUILD
faluco@1541
   813
void _IntExt_OnHostnameChanged(IConVar *pConVar, const char *oldValue, float flOldValue)
faluco@1541
   814
#else
ds@627
   815
void _IntExt_OnHostnameChanged(ConVar *pConVar, char const *oldValue)
faluco@1541
   816
#endif
ds@627
   817
{
ds@627
   818
	if (strcmp(oldValue, "Good morning, DS-san.") == 0
faluco@1541
   819
		&& strcmp(reinterpret_cast<ConVar *>(pConVar)->GetString(), "Good night, talking desk lamp.") == 0)
ds@627
   820
	{
ds@627
   821
		_IntExt_EnableYamagrams();
ds@627
   822
	}
ds@627
   823
}