core/ConVarManager.h
author David Anderson <dvander@alliedmods.net>
Wed Mar 12 03:33:52 2008 +0000 (2008-03-12)
changeset 1925 61d7a244b641
parent 1770 9c545310f947
child 1947 e73a1e51833b
permissions -rw-r--r--
reworked antiflood. it now has some logic in core to take care of loading order nastiness, and to fully prevent trigger spamming
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
#ifndef _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
ds@580
    33
#define _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
ds@580
    34
ds@580
    35
#include "sm_globals.h"
ds@580
    36
#include "sourcemm_api.h"
ds@580
    37
#include <sh_list.h>
ds@580
    38
#include <IPluginSys.h>
ds@580
    39
#include <IForwardSys.h>
ds@580
    40
#include <IHandleSys.h>
ds@580
    41
#include <IRootConsoleMenu.h>
dvander@1547
    42
#include <compat_wrappers.h>
dvander@1582
    43
#include "concmd_cleaner.h"
ds@580
    44
ds@580
    45
using namespace SourceHook;
ds@580
    46
dvander@1770
    47
class IConVarChangeListener
dvander@1770
    48
{
dvander@1770
    49
public:
dvander@1770
    50
	virtual void OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue) =0;
dvander@1770
    51
};
dvander@1770
    52
ds@580
    53
/**
ds@580
    54
 * Holds SourceMod-specific information about a convar
ds@580
    55
 */
ds@580
    56
struct ConVarInfo
ds@580
    57
{
dvander@1582
    58
	Handle_t handle;					/**< Handle to self */
ds@580
    59
	bool sourceMod;						/**< Determines whether or not convar was created by a SourceMod plugin */
ds@696
    60
	IChangeableForward *pChangeForward;	/**< Forward associated with convar */
dvander@1582
    61
	ConVar *pVar;						/**< The actual convar */
dvander@1770
    62
	List<IConVarChangeListener *> changeListeners;
ds@580
    63
};
ds@580
    64
ds@696
    65
/**
ds@696
    66
 * Holds information about a client convar query
ds@696
    67
 */
ds@696
    68
struct ConVarQuery
ds@696
    69
{
ds@696
    70
	QueryCvarCookie_t cookie;			/**< Cookie that identifies query */
ds@696
    71
	IPluginFunction *pCallback;			/**< Function that will be called when query is finished */
ds@696
    72
	cell_t value;						/**< Optional value passed to query function */
ds@696
    73
};
ds@696
    74
ds@580
    75
class ConVarManager :
ds@580
    76
	public SMGlobalClass,
ds@580
    77
	public IHandleTypeDispatch,
ds@580
    78
	public IPluginsListener,
dvander@1582
    79
	public IRootConsoleCommand,
dvander@1582
    80
	public IConCommandTracker
ds@580
    81
{
ds@580
    82
public:
ds@580
    83
	ConVarManager();
ds@580
    84
	~ConVarManager();
ds@580
    85
public: // SMGlobalClass
dvander@1770
    86
	void OnSourceModStartup(bool late);
ds@580
    87
	void OnSourceModAllInitialized();
ds@580
    88
	void OnSourceModShutdown();
dvander@1581
    89
	void OnSourceModVSPReceived();
ds@580
    90
public: // IHandleTypeDispatch
ds@580
    91
	void OnHandleDestroy(HandleType_t type, void *object);
dvander@1766
    92
	bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize);
ds@580
    93
public: // IPluginsListener
ds@582
    94
	void OnPluginUnloaded(IPlugin *plugin);
ds@580
    95
public: //IRootConsoleCommand
faluco@1541
    96
	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
dvander@1582
    97
public: //IConCommandTracker
dvander@1582
    98
	void OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe);
ds@580
    99
public:
ds@580
   100
	/**
ds@580
   101
	 * Create a convar and return a handle to it.
ds@580
   102
	 */
ds@582
   103
	Handle_t CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal,
ds@582
   104
	                      const char *description, int flags, bool hasMin, float min, bool hasMax, float max);
ds@580
   105
ds@580
   106
	/**
ds@580
   107
	 * Searches for a convar and returns a handle to it
ds@580
   108
	 */
ds@580
   109
	Handle_t FindConVar(const char* name);
ds@580
   110
ds@580
   111
	/**
ds@580
   112
	 * Add a function to call when the specified convar changes.
ds@580
   113
	 */
ds@582
   114
	void HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
ds@580
   115
ds@580
   116
	/**
ds@580
   117
	 * Remove a function from the forward that will be called when the specified convar changes.
ds@580
   118
	 */
ds@582
   119
	void UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
ds@696
   120
dvander@1770
   121
	void AddConVarChangeListener(const char *name, IConVarChangeListener *pListener);
dvander@1770
   122
	void RemoveConVarChangeListener(const char *name, IConVarChangeListener *pListener);
dvander@1770
   123
ds@696
   124
	/**
ds@696
   125
	 * Starts a query to find the value of a client convar.
ds@696
   126
	 */
ds@696
   127
	QueryCvarCookie_t QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback,
ds@696
   128
	                                    Handle_t hndl);
ds@1145
   129
dvander@1581
   130
	bool IsQueryingSupported();
dvander@1581
   131
dvander@1582
   132
	HandleError ReadConVarHandle(Handle_t hndl, ConVar **pVar);
dvander@1582
   133
ds@580
   134
private:
ds@580
   135
	/**
ds@582
   136
	 * Adds a convar to a plugin's list.
ds@582
   137
	 */
ds@582
   138
	static void AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar);
ds@582
   139
ds@582
   140
	/**
ds@696
   141
	 * Static callback that Valve's ConVar object executes when the convar's value changes.
ds@580
   142
	 */
faluco@1541
   143
#if defined ORANGEBOX_BUILD
dvander@1770
   144
	static void OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue);
faluco@1541
   145
#else
ds@580
   146
	static void OnConVarChanged(ConVar *pConVar, const char *oldValue);
faluco@1541
   147
#endif
ds@696
   148
ds@696
   149
	/**
ds@696
   150
	 * Callback for when StartQueryCvarValue() has finished.
ds@696
   151
	 */
ds@696
   152
	void OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result,
ds@696
   153
	                              const char *cvarName, const char *cvarValue);
ds@580
   154
private:
ds@580
   155
	HandleType_t m_ConVarType;
ds@580
   156
	List<ConVarInfo *> m_ConVars;
ds@696
   157
	List<ConVarQuery> m_ConVarQueries;
dvander@1581
   158
	bool m_bIsDLLQueryHooked;
dvander@1581
   159
	bool m_bIsVSPQueryHooked;
ds@580
   160
};
ds@580
   161
ds@580
   162
extern ConVarManager g_ConVarManager;
ds@580
   163
ds@580
   164
#endif // _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
ds@580
   165