core/ConVarManager.h
author Scott Ehlert <ds@alliedmods.net>
Sun Sep 14 03:01:05 2008 -0500 (2008-09-14)
changeset 2289 44045ead61cd
parent 1952 b7aa2b626807
parent 1947 e73a1e51833b
child 2400 76b7b4575e21
permissions -rw-r--r--
Eliminated head: da55a80522cf, a remnant of sourcemod-1.1.0 branch
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
    33 #define _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
    34 
    35 #include "sm_globals.h"
    36 #include "sourcemm_api.h"
    37 #include <sh_list.h>
    38 #include <IPluginSys.h>
    39 #include <IForwardSys.h>
    40 #include <IHandleSys.h>
    41 #include <IRootConsoleMenu.h>
    42 #include <compat_wrappers.h>
    43 #include "concmd_cleaner.h"
    44 
    45 using namespace SourceHook;
    46 
    47 class IConVarChangeListener
    48 {
    49 public:
    50 	virtual void OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue) =0;
    51 };
    52 
    53 /**
    54  * Holds SourceMod-specific information about a convar
    55  */
    56 struct ConVarInfo
    57 {
    58 	Handle_t handle;					/**< Handle to self */
    59 	bool sourceMod;						/**< Determines whether or not convar was created by a SourceMod plugin */
    60 	IChangeableForward *pChangeForward;	/**< Forward associated with convar */
    61 	ConVar *pVar;						/**< The actual convar */
    62 	List<IConVarChangeListener *> changeListeners;
    63 };
    64 
    65 /**
    66  * Holds information about a client convar query
    67  */
    68 struct ConVarQuery
    69 {
    70 	QueryCvarCookie_t cookie;			/**< Cookie that identifies query */
    71 	IPluginFunction *pCallback;			/**< Function that will be called when query is finished */
    72 	cell_t value;						/**< Optional value passed to query function */
    73 };
    74 
    75 class ConVarManager :
    76 	public SMGlobalClass,
    77 	public IHandleTypeDispatch,
    78 	public IPluginsListener,
    79 	public IRootConsoleCommand,
    80 	public IConCommandTracker
    81 {
    82 public:
    83 	ConVarManager();
    84 	~ConVarManager();
    85 public: // SMGlobalClass
    86 	void OnSourceModStartup(bool late);
    87 	void OnSourceModAllInitialized();
    88 	void OnSourceModShutdown();
    89 	void OnSourceModVSPReceived();
    90 public: // IHandleTypeDispatch
    91 	void OnHandleDestroy(HandleType_t type, void *object);
    92 	bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize);
    93 public: // IPluginsListener
    94 	void OnPluginUnloaded(IPlugin *plugin);
    95 public: //IRootConsoleCommand
    96 	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
    97 public: //IConCommandTracker
    98 	void OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe);
    99 public:
   100 	/**
   101 	 * Create a convar and return a handle to it.
   102 	 */
   103 	Handle_t CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal,
   104 	                      const char *description, int flags, bool hasMin, float min, bool hasMax, float max);
   105 
   106 	/**
   107 	 * Searches for a convar and returns a handle to it
   108 	 */
   109 	Handle_t FindConVar(const char* name);
   110 
   111 	/**
   112 	 * Add a function to call when the specified convar changes.
   113 	 */
   114 	void HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
   115 
   116 	/**
   117 	 * Remove a function from the forward that will be called when the specified convar changes.
   118 	 */
   119 	void UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
   120 
   121 	void AddConVarChangeListener(const char *name, IConVarChangeListener *pListener);
   122 	void RemoveConVarChangeListener(const char *name, IConVarChangeListener *pListener);
   123 
   124 	/**
   125 	 * Starts a query to find the value of a client convar.
   126 	 */
   127 	QueryCvarCookie_t QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback,
   128 	                                    Handle_t hndl);
   129 
   130 	bool IsQueryingSupported();
   131 
   132 	HandleError ReadConVarHandle(Handle_t hndl, ConVar **pVar);
   133 
   134 private:
   135 	/**
   136 	 * Adds a convar to a plugin's list.
   137 	 */
   138 	static void AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar);
   139 
   140 	/**
   141 	 * Static callback that Valve's ConVar object executes when the convar's value changes.
   142 	 */
   143 #if defined ORANGEBOX_BUILD
   144 	static void OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue);
   145 #else
   146 	static void OnConVarChanged(ConVar *pConVar, const char *oldValue);
   147 #endif
   148 
   149 	/**
   150 	 * Callback for when StartQueryCvarValue() has finished.
   151 	 */
   152 	void OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result,
   153 	                              const char *cvarName, const char *cvarValue);
   154 private:
   155 	HandleType_t m_ConVarType;
   156 	List<ConVarInfo *> m_ConVars;
   157 	List<ConVarQuery> m_ConVarQueries;
   158 	bool m_bIsDLLQueryHooked;
   159 	bool m_bIsVSPQueryHooked;
   160 };
   161 
   162 extern ConVarManager g_ConVarManager;
   163 
   164 #endif // _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
   165