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