core/CConVarManager.h
author David Anderson <dvander@alliedmods.net>
Fri Feb 16 04:16:31 2007 +0000 (2007-02-16)
changeset 502 4530ea065a8b
parent 487 3410298035cb
child 576 50696154c0b8
permissions -rw-r--r--
fixed a serious potential bug with swapping plugins to debug mode, leaving stale function pointers without plugin removal
     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_CCONVARMANAGER_H_
    15 #define _INCLUDE_SOURCEMOD_CCONVARMANAGER_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 CConVarManager :
    40 	public SMGlobalClass,
    41 	public IHandleTypeDispatch,
    42 	public IPluginsListener,
    43 	public IRootConsoleCommand
    44 {
    45 public:
    46 	CConVarManager();
    47 	~CConVarManager();
    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 OnPluginDestroyed(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, const char *helpText,
    78 	                      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(IPluginContext *pContext, ConVar *cvar, funcid_t funcid);
    89 
    90 	/**
    91 	 * Remove a function from the forward that will be called when the specified convar changes.
    92 	 */
    93 	void UnhookConVarChange(IPluginContext *pContext, ConVar *cvar, funcid_t funcid);
    94 private:
    95 	/**
    96 	* Static callback that Valve's ConVar class executes when the convar's value changes.
    97 	*/
    98 	static void OnConVarChanged(ConVar *cvar, const char *oldValue);
    99 private:
   100 	HandleType_t m_ConVarType;
   101 	List<ConVarInfo *> m_ConVars;
   102 	Trie *m_ConVarCache;
   103 };
   104 
   105 extern CConVarManager g_ConVarManager;
   106 
   107 #endif // _INCLUDE_SOURCEMOD_CCONVARMANAGER_H_
   108