core/ConVarManager.h
author Asher Baker <asherkin@gmail.com>
Sun Aug 14 21:42:32 2011 +0100 (2011-08-14)
changeset 3367 18eb63085ca4
parent 2583 d3b2571c378b
child 3942 7aaa00382627
permissions -rw-r--r--
Added a reset argument to 'sm cvars' (bug 5043, r=psychonic).
     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 #if SOURCE_ENGINE == SE_DARKMESSIAH
    46 class EQueryCvarValueStatus;
    47 typedef int QueryCvarCookie_t;
    48 #endif
    49 
    50 using namespace SourceHook;
    51 
    52 class IConVarChangeListener
    53 {
    54 public:
    55 	virtual void OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue) =0;
    56 };
    57 
    58 /**
    59  * Holds SourceMod-specific information about a convar
    60  */
    61 struct ConVarInfo
    62 {
    63 	Handle_t handle;					/**< Handle to self */
    64 	bool sourceMod;						/**< Determines whether or not convar was created by a SourceMod plugin */
    65 	IChangeableForward *pChangeForward;	/**< Forward associated with convar */
    66 	ConVar *pVar;						/**< The actual convar */
    67 	List<IConVarChangeListener *> changeListeners;
    68 };
    69 
    70 /**
    71  * Holds information about a client convar query
    72  */
    73 struct ConVarQuery
    74 {
    75 	QueryCvarCookie_t cookie;			/**< Cookie that identifies query */
    76 	IPluginFunction *pCallback;			/**< Function that will be called when query is finished */
    77 	cell_t value;						/**< Optional value passed to query function */
    78 };
    79 
    80 class ConVarManager :
    81 	public SMGlobalClass,
    82 	public IHandleTypeDispatch,
    83 	public IPluginsListener,
    84 	public IRootConsoleCommand,
    85 	public IConCommandTracker
    86 {
    87 public:
    88 	ConVarManager();
    89 	~ConVarManager();
    90 public: // SMGlobalClass
    91 	void OnSourceModStartup(bool late);
    92 	void OnSourceModAllInitialized();
    93 	void OnSourceModShutdown();
    94 	void OnSourceModVSPReceived();
    95 public: // IHandleTypeDispatch
    96 	void OnHandleDestroy(HandleType_t type, void *object);
    97 	bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize);
    98 public: // IPluginsListener
    99 	void OnPluginUnloaded(IPlugin *plugin);
   100 public: //IRootConsoleCommand
   101 	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
   102 public: //IConCommandTracker
   103 	void OnUnlinkConCommandBase(ConCommandBase *pBase, const char *name, bool is_read_safe);
   104 public:
   105 	/**
   106 	 * Create a convar and return a handle to it.
   107 	 */
   108 	Handle_t CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal,
   109 	                      const char *description, int flags, bool hasMin, float min, bool hasMax, float max);
   110 
   111 	/**
   112 	 * Searches for a convar and returns a handle to it
   113 	 */
   114 	Handle_t FindConVar(const char* name);
   115 
   116 	/**
   117 	 * Add a function to call when the specified convar changes.
   118 	 */
   119 	void HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
   120 
   121 	/**
   122 	 * Remove a function from the forward that will be called when the specified convar changes.
   123 	 */
   124 	void UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
   125 
   126 	void AddConVarChangeListener(const char *name, IConVarChangeListener *pListener);
   127 	void RemoveConVarChangeListener(const char *name, IConVarChangeListener *pListener);
   128 
   129 	/**
   130 	 * Starts a query to find the value of a client convar.
   131 	 */
   132 	QueryCvarCookie_t QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback,
   133 	                                    Handle_t hndl);
   134 
   135 	bool IsQueryingSupported();
   136 
   137 	HandleError ReadConVarHandle(Handle_t hndl, ConVar **pVar);
   138 
   139 private:
   140 	/**
   141 	 * Adds a convar to a plugin's list.
   142 	 */
   143 	static void AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar);
   144 
   145 	/**
   146 	 * Static callback that Valve's ConVar object executes when the convar's value changes.
   147 	 */
   148 #if SOURCE_ENGINE >= SE_ORANGEBOX
   149 	static void OnConVarChanged(ConVar *pConVar, const char *oldValue, float flOldValue);
   150 #else
   151 	static void OnConVarChanged(ConVar *pConVar, const char *oldValue);
   152 #endif
   153 
   154 	/**
   155 	 * Callback for when StartQueryCvarValue() has finished.
   156 	 */
   157 #if SOURCE_ENGINE != SE_DARKMESSIAH
   158 	void OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result,
   159 	                              const char *cvarName, const char *cvarValue);
   160 #endif
   161 private:
   162 	HandleType_t m_ConVarType;
   163 	List<ConVarInfo *> m_ConVars;
   164 	List<ConVarQuery> m_ConVarQueries;
   165 	bool m_bIsDLLQueryHooked;
   166 	bool m_bIsVSPQueryHooked;
   167 };
   168 
   169 extern ConVarManager g_ConVarManager;
   170 
   171 #endif // _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
   172