core/ConVarManager.h
author Scott Ehlert <ds@alliedmods.net>
Tue Mar 06 23:59:25 2007 +0000 (2007-03-06)
changeset 582 00c843a1bb18
parent 580 416aecc7de51
child 653 ef60870067ff
permissions -rw-r--r--
Many things:
1) General code clean-up in convar manager
2) Fixed bug where convar handles were not being cached for pre-existing convars
3) Fixed bug where a convar's old value and new value were switched in the ConVarChanged hooks
4) Made convar and concmd listings more consistent, I think
5) Alphabetized convar listing
6) Minor clean-up in event manager
     1 /**
     2 * ===============================================================
     3 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4 * ===============================================================
     5 *
     6 * This file is not open source and may not be copied without explicit
     7 * written permission of AlliedModders LLC.  This file may not be redistributed 
     8 * in whole or significant part.
     9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    10 *
    11 * Version: $Id$
    12 */
    13 
    14 #ifndef _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
    15 #define _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
    16 
    17 #include "sm_globals.h"
    18 #include "sourcemm_api.h"
    19 #include "sm_trie.h"
    20 #include <sh_list.h>
    21 #include <IPluginSys.h>
    22 #include <IForwardSys.h>
    23 #include <IHandleSys.h>
    24 #include <IRootConsoleMenu.h>
    25 
    26 using namespace SourceHook;
    27 
    28 /**
    29  * Holds SourceMod-specific information about a convar
    30  */
    31 struct ConVarInfo
    32 {
    33 	Handle_t handle;					/**< Handle to convar */
    34 	bool sourceMod;						/**< Determines whether or not convar was created by a SourceMod plugin */
    35 	IChangeableForward *changeForward;	/**< Forward associated with convar */
    36 	FnChangeCallback origCallback;		/**< The original callback function */
    37 };
    38 
    39 class ConVarManager :
    40 	public SMGlobalClass,
    41 	public IHandleTypeDispatch,
    42 	public IPluginsListener,
    43 	public IRootConsoleCommand
    44 {
    45 public:
    46 	ConVarManager();
    47 	~ConVarManager();
    48 public: // SMGlobalClass
    49 	void OnSourceModAllInitialized();
    50 	void OnSourceModShutdown();
    51 public: // IHandleTypeDispatch
    52 	void OnHandleDestroy(HandleType_t type, void *object);
    53 public: // IPluginsListener
    54 	void OnPluginUnloaded(IPlugin *plugin);
    55 public: //IRootConsoleCommand
    56 	void OnRootConsoleCommand(const char *command, unsigned int argcount);
    57 public:
    58 	/**
    59 	 * Get the 'ConVar' handle type ID.
    60 	 */
    61 	inline HandleType_t GetHandleType()
    62 	{
    63 		return m_ConVarType;
    64 	}
    65 
    66 	/**
    67 	 * Get the convar lookup trie.
    68 	 */
    69 	inline Trie *GetConVarCache()
    70 	{
    71 		return m_ConVarCache;
    72 	}
    73 public:
    74 	/**
    75 	 * Create a convar and return a handle to it.
    76 	 */
    77 	Handle_t CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal,
    78 	                      const char *description, int flags, bool hasMin, float min, bool hasMax, float max);
    79 
    80 	/**
    81 	 * Searches for a convar and returns a handle to it
    82 	 */
    83 	Handle_t FindConVar(const char* name);
    84 
    85 	/**
    86 	 * Add a function to call when the specified convar changes.
    87 	 */
    88 	void HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
    89 
    90 	/**
    91 	 * Remove a function from the forward that will be called when the specified convar changes.
    92 	 */
    93 	void UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
    94 private:
    95 	/**
    96 	 * Adds a convar to a plugin's list.
    97 	 */
    98 	static void AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar);
    99 
   100 	/**
   101 	 * Static callback that Valve's ConVar class executes when the convar's value changes.
   102 	 */
   103 	static void OnConVarChanged(ConVar *pConVar, const char *oldValue);
   104 private:
   105 	HandleType_t m_ConVarType;
   106 	List<ConVarInfo *> m_ConVars;
   107 	Trie *m_ConVarCache;
   108 };
   109 
   110 extern ConVarManager g_ConVarManager;
   111 
   112 #endif // _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
   113