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