core/ConVarManager.h
author David Anderson <dvander@alliedmods.net>
Thu Apr 05 07:09:17 2007 +0000 (2007-04-05)
changeset 667 0347227ef615
parent 653 ef60870067ff
child 696 d5cc8d0fa899
permissions -rw-r--r--
added file headers
     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 *changeForward;	/**< Forward associated with convar */
    37 	FnChangeCallback origCallback;		/**< The original callback function */
    38 };
    39 
    40 class ConVarManager :
    41 	public SMGlobalClass,
    42 	public IHandleTypeDispatch,
    43 	public IPluginsListener,
    44 	public IRootConsoleCommand
    45 {
    46 public:
    47 	ConVarManager();
    48 	~ConVarManager();
    49 public: // SMGlobalClass
    50 	void OnSourceModAllInitialized();
    51 	void OnSourceModShutdown();
    52 public: // IHandleTypeDispatch
    53 	void OnHandleDestroy(HandleType_t type, void *object);
    54 public: // IPluginsListener
    55 	void OnPluginUnloaded(IPlugin *plugin);
    56 public: //IRootConsoleCommand
    57 	void OnRootConsoleCommand(const char *command, unsigned int argcount);
    58 public:
    59 	/**
    60 	 * Get the 'ConVar' handle type ID.
    61 	 */
    62 	inline HandleType_t GetHandleType()
    63 	{
    64 		return m_ConVarType;
    65 	}
    66 
    67 	/**
    68 	 * Get the convar lookup trie.
    69 	 */
    70 	inline Trie *GetConVarCache()
    71 	{
    72 		return m_ConVarCache;
    73 	}
    74 public:
    75 	/**
    76 	 * Create a convar and return a handle to it.
    77 	 */
    78 	Handle_t CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal,
    79 	                      const char *description, int flags, bool hasMin, float min, bool hasMax, float max);
    80 
    81 	/**
    82 	 * Searches for a convar and returns a handle to it
    83 	 */
    84 	Handle_t FindConVar(const char* name);
    85 
    86 	/**
    87 	 * Add a function to call when the specified convar changes.
    88 	 */
    89 	void HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
    90 
    91 	/**
    92 	 * Remove a function from the forward that will be called when the specified convar changes.
    93 	 */
    94 	void UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
    95 private:
    96 	/**
    97 	 * Adds a convar to a plugin's list.
    98 	 */
    99 	static void AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar);
   100 
   101 	/**
   102 	 * Static callback that Valve's ConVar class executes when the convar's value changes.
   103 	 */
   104 	static void OnConVarChanged(ConVar *pConVar, const char *oldValue);
   105 private:
   106 	HandleType_t m_ConVarType;
   107 	List<ConVarInfo *> m_ConVars;
   108 	Trie *m_ConVarCache;
   109 };
   110 
   111 extern ConVarManager g_ConVarManager;
   112 
   113 #endif // _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
   114