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