core/ConVarManager.h
author Nicholas Hastings <skamonkey@gmail.com>
Mon Jun 27 09:30:52 2011 -0400 (2011-06-27)
changeset 3320 f1ccb4a17a06
parent 2583 d3b2571c378b
child 3942 7aaa00382627
permissions -rw-r--r--
Added support for gamedata lookups from matchmaking_ds lib (bug 4158, r=Fyren).
ds@580
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
ds@1947
     5
 * Copyright (C) 2004-2008 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@2583
    45
#if SOURCE_ENGINE == SE_DARKMESSIAH
ds@2583
    46
class EQueryCvarValueStatus;
ds@2583
    47
typedef int QueryCvarCookie_t;
ds@2583
    48
#endif
ds@2583
    49
ds@580
    50
using namespace SourceHook;
ds@580
    51
dvander@1770
    52
class IConVarChangeListener
dvander@1770
    53
{
dvander@1770
    54
public:
dvander@1770
    55
	virtual void OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue) =0;
dvander@1770
    56
};
dvander@1770
    57
ds@580
    58
/**
ds@580
    59
 * Holds SourceMod-specific information about a convar
ds@580
    60
 */
ds@580
    61
struct ConVarInfo
ds@580
    62
{
dvander@1582
    63
	Handle_t handle;					/**< Handle to self */
ds@580
    64
	bool sourceMod;						/**< Determines whether or not convar was created by a SourceMod plugin */
ds@696
    65
	IChangeableForward *pChangeForward;	/**< Forward associated with convar */
dvander@1582
    66
	ConVar *pVar;						/**< The actual convar */
dvander@1770
    67
	List<IConVarChangeListener *> changeListeners;
ds@580
    68
};
ds@580
    69
ds@696
    70
/**
ds@696
    71
 * Holds information about a client convar query
ds@696
    72
 */
ds@696
    73
struct ConVarQuery
ds@696
    74
{
ds@696
    75
	QueryCvarCookie_t cookie;			/**< Cookie that identifies query */
ds@696
    76
	IPluginFunction *pCallback;			/**< Function that will be called when query is finished */
ds@696
    77
	cell_t value;						/**< Optional value passed to query function */
ds@696
    78
};
ds@696
    79
ds@580
    80
class ConVarManager :
ds@580
    81
	public SMGlobalClass,
ds@580
    82
	public IHandleTypeDispatch,
ds@580
    83
	public IPluginsListener,
dvander@1582
    84
	public IRootConsoleCommand,
dvander@1582
    85
	public IConCommandTracker
ds@580
    86
{
ds@580
    87
public:
ds@580
    88
	ConVarManager();
ds@580
    89
	~ConVarManager();
ds@580
    90
public: // SMGlobalClass
dvander@1770
    91
	void OnSourceModStartup(bool late);
ds@580
    92
	void OnSourceModAllInitialized();
ds@580
    93
	void OnSourceModShutdown();
dvander@1581
    94
	void OnSourceModVSPReceived();
ds@580
    95
public: // IHandleTypeDispatch
ds@580
    96
	void OnHandleDestroy(HandleType_t type, void *object);
dvander@1766
    97
	bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize);
ds@580
    98
public: // IPluginsListener
ds@582
    99
	void OnPluginUnloaded(IPlugin *plugin);
ds@580
   100
public: //IRootConsoleCommand
faluco@1541
   101
	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
dvander@1582
   102
public: //IConCommandTracker
dvander@1582
   103
	void OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe);
ds@580
   104
public:
ds@580
   105
	/**
ds@580
   106
	 * Create a convar and return a handle to it.
ds@580
   107
	 */
ds@582
   108
	Handle_t CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal,
ds@582
   109
	                      const char *description, int flags, bool hasMin, float min, bool hasMax, float max);
ds@580
   110
ds@580
   111
	/**
ds@580
   112
	 * Searches for a convar and returns a handle to it
ds@580
   113
	 */
ds@580
   114
	Handle_t FindConVar(const char* name);
ds@580
   115
ds@580
   116
	/**
ds@580
   117
	 * Add a function to call when the specified convar changes.
ds@580
   118
	 */
ds@582
   119
	void HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
ds@580
   120
ds@580
   121
	/**
ds@580
   122
	 * Remove a function from the forward that will be called when the specified convar changes.
ds@580
   123
	 */
ds@582
   124
	void UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
ds@696
   125
dvander@1770
   126
	void AddConVarChangeListener(const char *name, IConVarChangeListener *pListener);
dvander@1770
   127
	void RemoveConVarChangeListener(const char *name, IConVarChangeListener *pListener);
dvander@1770
   128
ds@696
   129
	/**
ds@696
   130
	 * Starts a query to find the value of a client convar.
ds@696
   131
	 */
ds@696
   132
	QueryCvarCookie_t QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback,
ds@696
   133
	                                    Handle_t hndl);
ds@1145
   134
dvander@1581
   135
	bool IsQueryingSupported();
dvander@1581
   136
dvander@1582
   137
	HandleError ReadConVarHandle(Handle_t hndl, ConVar **pVar);
dvander@1582
   138
ds@580
   139
private:
ds@580
   140
	/**
ds@582
   141
	 * Adds a convar to a plugin's list.
ds@582
   142
	 */
ds@582
   143
	static void AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar);
ds@582
   144
ds@582
   145
	/**
ds@696
   146
	 * Static callback that Valve's ConVar object executes when the convar's value changes.
ds@580
   147
	 */
ds@2400
   148
#if SOURCE_ENGINE >= SE_ORANGEBOX
dvander@1770
   149
	static void OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue);
faluco@1541
   150
#else
ds@580
   151
	static void OnConVarChanged(ConVar *pConVar, const char *oldValue);
faluco@1541
   152
#endif
ds@696
   153
ds@696
   154
	/**
ds@696
   155
	 * Callback for when StartQueryCvarValue() has finished.
ds@696
   156
	 */
ds@2583
   157
#if SOURCE_ENGINE != SE_DARKMESSIAH
ds@696
   158
	void OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result,
ds@696
   159
	                              const char *cvarName, const char *cvarValue);
ds@2583
   160
#endif
ds@580
   161
private:
ds@580
   162
	HandleType_t m_ConVarType;
ds@580
   163
	List<ConVarInfo *> m_ConVars;
ds@696
   164
	List<ConVarQuery> m_ConVarQueries;
dvander@1581
   165
	bool m_bIsDLLQueryHooked;
dvander@1581
   166
	bool m_bIsVSPQueryHooked;
ds@580
   167
};
ds@580
   168
ds@580
   169
extern ConVarManager g_ConVarManager;
ds@580
   170
ds@580
   171
#endif // _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
ds@580
   172