core/ConVarManager.h
author Scott Ehlert <ds@alliedmods.net>
Mon Jul 23 18:30:02 2007 +0000 (2007-07-23)
changeset 1145 f12e19245c7a
parent 696 d5cc8d0fa899
child 1231 17badaf66ce4
permissions -rw-r--r--
Fixed amb660 - crash on exit when handles were created for convars that belonged to Metamod:Source plugins (or SM extensions that were loaded as MM:S plugins)
ds@580
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@653
     3
 * ===============================================================
ds@653
     4
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
ds@653
     5
 * ===============================================================
ds@653
     6
 *
ds@653
     7
 * This file is not open source and may not be copied without explicit
ds@653
     8
 * written permission of AlliedModders LLC.  This file may not be redistributed 
ds@653
     9
 * in whole or significant part.
ds@653
    10
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
ds@653
    11
 *
ds@653
    12
 * Version: $Id$
ds@653
    13
 */
ds@580
    14
ds@580
    15
#ifndef _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
ds@580
    16
#define _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
ds@580
    17
ds@580
    18
#include "sm_globals.h"
ds@580
    19
#include "sourcemm_api.h"
ds@580
    20
#include "sm_trie.h"
ds@580
    21
#include <sh_list.h>
ds@580
    22
#include <IPluginSys.h>
ds@580
    23
#include <IForwardSys.h>
ds@580
    24
#include <IHandleSys.h>
ds@580
    25
#include <IRootConsoleMenu.h>
ds@580
    26
ds@580
    27
using namespace SourceHook;
ds@580
    28
ds@580
    29
/**
ds@580
    30
 * Holds SourceMod-specific information about a convar
ds@580
    31
 */
ds@580
    32
struct ConVarInfo
ds@580
    33
{
ds@580
    34
	Handle_t handle;					/**< Handle to convar */
ds@580
    35
	bool sourceMod;						/**< Determines whether or not convar was created by a SourceMod plugin */
ds@696
    36
	IChangeableForward *pChangeForward;	/**< Forward associated with convar */
ds@580
    37
	FnChangeCallback origCallback;		/**< The original callback function */
ds@1145
    38
#if PLAPI_VERSION < 12
ds@1145
    39
	const char *name;					/**< Name of convar */
ds@1145
    40
#endif
ds@580
    41
};
ds@580
    42
ds@696
    43
/**
ds@696
    44
 * Holds information about a client convar query
ds@696
    45
 */
ds@696
    46
struct ConVarQuery
ds@696
    47
{
ds@696
    48
	QueryCvarCookie_t cookie;			/**< Cookie that identifies query */
ds@696
    49
	IPluginFunction *pCallback;			/**< Function that will be called when query is finished */
ds@696
    50
	cell_t value;						/**< Optional value passed to query function */
ds@696
    51
};
ds@696
    52
ds@580
    53
class ConVarManager :
ds@580
    54
	public SMGlobalClass,
ds@580
    55
	public IHandleTypeDispatch,
ds@580
    56
	public IPluginsListener,
ds@580
    57
	public IRootConsoleCommand
ds@580
    58
{
ds@580
    59
public:
ds@580
    60
	ConVarManager();
ds@580
    61
	~ConVarManager();
ds@580
    62
public: // SMGlobalClass
ds@580
    63
	void OnSourceModAllInitialized();
ds@580
    64
	void OnSourceModShutdown();
ds@696
    65
	void OnSourceModVSPReceived(IServerPluginCallbacks *iface);
ds@580
    66
public: // IHandleTypeDispatch
ds@580
    67
	void OnHandleDestroy(HandleType_t type, void *object);
ds@580
    68
public: // IPluginsListener
ds@582
    69
	void OnPluginUnloaded(IPlugin *plugin);
ds@580
    70
public: //IRootConsoleCommand
ds@580
    71
	void OnRootConsoleCommand(const char *command, unsigned int argcount);
ds@580
    72
public:
ds@580
    73
	/**
ds@580
    74
	 * Get the 'ConVar' handle type ID.
ds@580
    75
	 */
ds@580
    76
	inline HandleType_t GetHandleType()
ds@580
    77
	{
ds@580
    78
		return m_ConVarType;
ds@580
    79
	}
ds@580
    80
ds@580
    81
	/**
ds@580
    82
	 * Get the convar lookup trie.
ds@580
    83
	 */
ds@580
    84
	inline Trie *GetConVarCache()
ds@580
    85
	{
ds@580
    86
		return m_ConVarCache;
ds@580
    87
	}
ds@580
    88
public:
ds@580
    89
	/**
ds@580
    90
	 * Create a convar and return a handle to it.
ds@580
    91
	 */
ds@582
    92
	Handle_t CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal,
ds@582
    93
	                      const char *description, int flags, bool hasMin, float min, bool hasMax, float max);
ds@580
    94
ds@580
    95
	/**
ds@580
    96
	 * Searches for a convar and returns a handle to it
ds@580
    97
	 */
ds@580
    98
	Handle_t FindConVar(const char* name);
ds@580
    99
ds@580
   100
	/**
ds@580
   101
	 * Add a function to call when the specified convar changes.
ds@580
   102
	 */
ds@582
   103
	void HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
ds@580
   104
ds@580
   105
	/**
ds@580
   106
	 * Remove a function from the forward that will be called when the specified convar changes.
ds@580
   107
	 */
ds@582
   108
	void UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
ds@696
   109
ds@696
   110
	/**
ds@696
   111
	 * Starts a query to find the value of a client convar.
ds@696
   112
	 */
ds@696
   113
	QueryCvarCookie_t QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback,
ds@696
   114
	                                    Handle_t hndl);
ds@1145
   115
ds@1145
   116
#if PLAPI_VERSION >= 12
ds@1145
   117
	/**
ds@1145
   118
	 * Called when Metamod:Source is about to remove convar
ds@1145
   119
	 */
ds@1145
   120
	void OnUnlinkConCommandBase(PluginId id, ConCommandBase *pCommand);
ds@1145
   121
#else
ds@1145
   122
	/**
ds@1145
   123
	 * Called when Metamod:Source has unloaded a plugin
ds@1145
   124
	 */
ds@1145
   125
	void OnMetamodPluginUnloaded(PluginId id);
ds@1145
   126
#endif
ds@580
   127
private:
ds@580
   128
	/**
ds@582
   129
	 * Adds a convar to a plugin's list.
ds@582
   130
	 */
ds@582
   131
	static void AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar);
ds@582
   132
ds@582
   133
	/**
ds@696
   134
	 * Static callback that Valve's ConVar object executes when the convar's value changes.
ds@580
   135
	 */
ds@580
   136
	static void OnConVarChanged(ConVar *pConVar, const char *oldValue);
ds@696
   137
ds@696
   138
	/**
ds@696
   139
	 * Callback for when StartQueryCvarValue() has finished.
ds@696
   140
	 */
ds@696
   141
	void OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result,
ds@696
   142
	                              const char *cvarName, const char *cvarValue);
ds@580
   143
private:
ds@580
   144
	HandleType_t m_ConVarType;
ds@580
   145
	List<ConVarInfo *> m_ConVars;
ds@696
   146
	List<ConVarQuery> m_ConVarQueries;
ds@580
   147
	Trie *m_ConVarCache;
ds@696
   148
	IServerPluginCallbacks *m_VSPIface;
ds@696
   149
	bool m_CanQueryConVars;
ds@1145
   150
#if PLAPI_VERSION < 12
ds@1145
   151
	bool m_IgnoreHandle;
ds@1145
   152
#endif
ds@580
   153
};
ds@580
   154
ds@580
   155
extern ConVarManager g_ConVarManager;
ds@580
   156
ds@580
   157
#endif // _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
ds@580
   158