core/ConVarManager.h
author David Anderson <dvander@alliedmods.net>
Sun Oct 14 18:53:29 2007 +0000 (2007-10-14)
changeset 1547 78abc407c8a3
parent 1541 e46ad208b9b4
child 1581 2562512b4a44
permissions -rw-r--r--
various build fixes and vcproj changes
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 "sm_trie.h"
ds@580
    38
#include <sh_list.h>
ds@580
    39
#include <IPluginSys.h>
ds@580
    40
#include <IForwardSys.h>
ds@580
    41
#include <IHandleSys.h>
ds@580
    42
#include <IRootConsoleMenu.h>
dvander@1547
    43
#include <compat_wrappers.h>
ds@580
    44
ds@580
    45
using namespace SourceHook;
ds@580
    46
ds@580
    47
/**
ds@580
    48
 * Holds SourceMod-specific information about a convar
ds@580
    49
 */
ds@580
    50
struct ConVarInfo
ds@580
    51
{
ds@580
    52
	Handle_t handle;					/**< Handle to convar */
ds@580
    53
	bool sourceMod;						/**< Determines whether or not convar was created by a SourceMod plugin */
ds@696
    54
	IChangeableForward *pChangeForward;	/**< Forward associated with convar */
dvander@1547
    55
	FnChangeCallback_t origCallback;	/**< The original callback function */
ds@1145
    56
#if PLAPI_VERSION < 12
ds@1145
    57
	const char *name;					/**< Name of convar */
ds@1145
    58
#endif
ds@580
    59
};
ds@580
    60
ds@696
    61
/**
ds@696
    62
 * Holds information about a client convar query
ds@696
    63
 */
ds@696
    64
struct ConVarQuery
ds@696
    65
{
ds@696
    66
	QueryCvarCookie_t cookie;			/**< Cookie that identifies query */
ds@696
    67
	IPluginFunction *pCallback;			/**< Function that will be called when query is finished */
ds@696
    68
	cell_t value;						/**< Optional value passed to query function */
ds@696
    69
};
ds@696
    70
ds@580
    71
class ConVarManager :
ds@580
    72
	public SMGlobalClass,
ds@580
    73
	public IHandleTypeDispatch,
ds@580
    74
	public IPluginsListener,
ds@580
    75
	public IRootConsoleCommand
ds@580
    76
{
ds@580
    77
public:
ds@580
    78
	ConVarManager();
ds@580
    79
	~ConVarManager();
ds@580
    80
public: // SMGlobalClass
ds@580
    81
	void OnSourceModAllInitialized();
ds@580
    82
	void OnSourceModShutdown();
ds@696
    83
	void OnSourceModVSPReceived(IServerPluginCallbacks *iface);
ds@580
    84
public: // IHandleTypeDispatch
ds@580
    85
	void OnHandleDestroy(HandleType_t type, void *object);
ds@580
    86
public: // IPluginsListener
ds@582
    87
	void OnPluginUnloaded(IPlugin *plugin);
ds@580
    88
public: //IRootConsoleCommand
faluco@1541
    89
	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
ds@580
    90
public:
ds@580
    91
	/**
ds@580
    92
	 * Get the 'ConVar' handle type ID.
ds@580
    93
	 */
ds@580
    94
	inline HandleType_t GetHandleType()
ds@580
    95
	{
ds@580
    96
		return m_ConVarType;
ds@580
    97
	}
ds@580
    98
ds@580
    99
	/**
ds@580
   100
	 * Get the convar lookup trie.
ds@580
   101
	 */
ds@580
   102
	inline Trie *GetConVarCache()
ds@580
   103
	{
ds@580
   104
		return m_ConVarCache;
ds@580
   105
	}
ds@580
   106
public:
ds@580
   107
	/**
ds@580
   108
	 * Create a convar and return a handle to it.
ds@580
   109
	 */
ds@582
   110
	Handle_t CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal,
ds@582
   111
	                      const char *description, int flags, bool hasMin, float min, bool hasMax, float max);
ds@580
   112
ds@580
   113
	/**
ds@580
   114
	 * Searches for a convar and returns a handle to it
ds@580
   115
	 */
ds@580
   116
	Handle_t FindConVar(const char* name);
ds@580
   117
ds@580
   118
	/**
ds@580
   119
	 * Add a function to call when the specified convar changes.
ds@580
   120
	 */
ds@582
   121
	void HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
ds@580
   122
ds@580
   123
	/**
ds@580
   124
	 * Remove a function from the forward that will be called when the specified convar changes.
ds@580
   125
	 */
ds@582
   126
	void UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
ds@696
   127
ds@696
   128
	/**
ds@696
   129
	 * Starts a query to find the value of a client convar.
ds@696
   130
	 */
ds@696
   131
	QueryCvarCookie_t QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback,
ds@696
   132
	                                    Handle_t hndl);
ds@1145
   133
ds@1145
   134
#if PLAPI_VERSION >= 12
ds@1145
   135
	/**
ds@1145
   136
	 * Called when Metamod:Source is about to remove convar
ds@1145
   137
	 */
ds@1145
   138
	void OnUnlinkConCommandBase(PluginId id, ConCommandBase *pCommand);
ds@1145
   139
#else
ds@1145
   140
	/**
ds@1145
   141
	 * Called when Metamod:Source has unloaded a plugin
ds@1145
   142
	 */
ds@1145
   143
	void OnMetamodPluginUnloaded(PluginId id);
ds@1145
   144
#endif
ds@580
   145
private:
ds@580
   146
	/**
ds@582
   147
	 * Adds a convar to a plugin's list.
ds@582
   148
	 */
ds@582
   149
	static void AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar);
ds@582
   150
ds@582
   151
	/**
ds@696
   152
	 * Static callback that Valve's ConVar object executes when the convar's value changes.
ds@580
   153
	 */
faluco@1541
   154
#if defined ORANGEBOX_BUILD
faluco@1541
   155
	static void OnConVarChanged(IConVar *pConVar, const char *oldValue, float flOldValue);
faluco@1541
   156
#else
ds@580
   157
	static void OnConVarChanged(ConVar *pConVar, const char *oldValue);
faluco@1541
   158
#endif
ds@696
   159
ds@696
   160
	/**
ds@696
   161
	 * Callback for when StartQueryCvarValue() has finished.
ds@696
   162
	 */
ds@696
   163
	void OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result,
ds@696
   164
	                              const char *cvarName, const char *cvarValue);
ds@580
   165
private:
ds@580
   166
	HandleType_t m_ConVarType;
ds@580
   167
	List<ConVarInfo *> m_ConVars;
ds@696
   168
	List<ConVarQuery> m_ConVarQueries;
ds@580
   169
	Trie *m_ConVarCache;
ds@696
   170
	IServerPluginCallbacks *m_VSPIface;
ds@696
   171
	bool m_CanQueryConVars;
ds@1145
   172
#if PLAPI_VERSION < 12
ds@1145
   173
	bool m_IgnoreHandle;
ds@1145
   174
#endif
ds@580
   175
};
ds@580
   176
ds@580
   177
extern ConVarManager g_ConVarManager;
ds@580
   178
ds@580
   179
#endif // _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
ds@580
   180