core/ConVarManager.h
author David Anderson <dvander@alliedmods.net>
Sun May 06 06:15:48 2007 +0000 (2007-05-06)
changeset 743 a2804c2faba2
parent 696 d5cc8d0fa899
child 1145 f12e19245c7a
permissions -rw-r--r--
added request amb251
     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 };
    39 
    40 /**
    41  * Holds information about a client convar query
    42  */
    43 struct ConVarQuery
    44 {
    45 	QueryCvarCookie_t cookie;			/**< Cookie that identifies query */
    46 	IPluginFunction *pCallback;			/**< Function that will be called when query is finished */
    47 	cell_t value;						/**< Optional value passed to query function */
    48 };
    49 
    50 class ConVarManager :
    51 	public SMGlobalClass,
    52 	public IHandleTypeDispatch,
    53 	public IPluginsListener,
    54 	public IRootConsoleCommand
    55 {
    56 public:
    57 	ConVarManager();
    58 	~ConVarManager();
    59 public: // SMGlobalClass
    60 	void OnSourceModAllInitialized();
    61 	void OnSourceModShutdown();
    62 	void OnSourceModVSPReceived(IServerPluginCallbacks *iface);
    63 public: // IHandleTypeDispatch
    64 	void OnHandleDestroy(HandleType_t type, void *object);
    65 public: // IPluginsListener
    66 	void OnPluginUnloaded(IPlugin *plugin);
    67 public: //IRootConsoleCommand
    68 	void OnRootConsoleCommand(const char *command, unsigned int argcount);
    69 public:
    70 	/**
    71 	 * Get the 'ConVar' handle type ID.
    72 	 */
    73 	inline HandleType_t GetHandleType()
    74 	{
    75 		return m_ConVarType;
    76 	}
    77 
    78 	/**
    79 	 * Get the convar lookup trie.
    80 	 */
    81 	inline Trie *GetConVarCache()
    82 	{
    83 		return m_ConVarCache;
    84 	}
    85 public:
    86 	/**
    87 	 * Create a convar and return a handle to it.
    88 	 */
    89 	Handle_t CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal,
    90 	                      const char *description, int flags, bool hasMin, float min, bool hasMax, float max);
    91 
    92 	/**
    93 	 * Searches for a convar and returns a handle to it
    94 	 */
    95 	Handle_t FindConVar(const char* name);
    96 
    97 	/**
    98 	 * Add a function to call when the specified convar changes.
    99 	 */
   100 	void HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
   101 
   102 	/**
   103 	 * Remove a function from the forward that will be called when the specified convar changes.
   104 	 */
   105 	void UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
   106 
   107 	/**
   108 	 * Starts a query to find the value of a client convar.
   109 	 */
   110 	QueryCvarCookie_t QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback,
   111 	                                    Handle_t hndl);
   112 private:
   113 	/**
   114 	 * Adds a convar to a plugin's list.
   115 	 */
   116 	static void AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar);
   117 
   118 	/**
   119 	 * Static callback that Valve's ConVar object executes when the convar's value changes.
   120 	 */
   121 	static void OnConVarChanged(ConVar *pConVar, const char *oldValue);
   122 
   123 	/**
   124 	 * Callback for when StartQueryCvarValue() has finished.
   125 	 */
   126 	void OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result,
   127 	                              const char *cvarName, const char *cvarValue);
   128 private:
   129 	HandleType_t m_ConVarType;
   130 	List<ConVarInfo *> m_ConVars;
   131 	List<ConVarQuery> m_ConVarQueries;
   132 	Trie *m_ConVarCache;
   133 	IServerPluginCallbacks *m_VSPIface;
   134 	bool m_CanQueryConVars;
   135 };
   136 
   137 extern ConVarManager g_ConVarManager;
   138 
   139 #endif // _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
   140