core/ConVarManager.h
author David Anderson <dvander@alliedmods.net>
Mon Oct 15 00:38:25 2007 +0000 (2007-10-15)
changeset 1568 ecbaee393b0b
parent 1547 78abc407c8a3
child 1581 2562512b4a44
permissions -rw-r--r--
extensions can now be built for multiple engines, and will autoload from auto.X.Y folders
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 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 "sm_trie.h"
    38 #include <sh_list.h>
    39 #include <IPluginSys.h>
    40 #include <IForwardSys.h>
    41 #include <IHandleSys.h>
    42 #include <IRootConsoleMenu.h>
    43 #include <compat_wrappers.h>
    44 
    45 using namespace SourceHook;
    46 
    47 /**
    48  * Holds SourceMod-specific information about a convar
    49  */
    50 struct ConVarInfo
    51 {
    52 	Handle_t handle;					/**< Handle to convar */
    53 	bool sourceMod;						/**< Determines whether or not convar was created by a SourceMod plugin */
    54 	IChangeableForward *pChangeForward;	/**< Forward associated with convar */
    55 	FnChangeCallback_t origCallback;	/**< The original callback function */
    56 #if PLAPI_VERSION < 12
    57 	const char *name;					/**< Name of convar */
    58 #endif
    59 };
    60 
    61 /**
    62  * Holds information about a client convar query
    63  */
    64 struct ConVarQuery
    65 {
    66 	QueryCvarCookie_t cookie;			/**< Cookie that identifies query */
    67 	IPluginFunction *pCallback;			/**< Function that will be called when query is finished */
    68 	cell_t value;						/**< Optional value passed to query function */
    69 };
    70 
    71 class ConVarManager :
    72 	public SMGlobalClass,
    73 	public IHandleTypeDispatch,
    74 	public IPluginsListener,
    75 	public IRootConsoleCommand
    76 {
    77 public:
    78 	ConVarManager();
    79 	~ConVarManager();
    80 public: // SMGlobalClass
    81 	void OnSourceModAllInitialized();
    82 	void OnSourceModShutdown();
    83 	void OnSourceModVSPReceived(IServerPluginCallbacks *iface);
    84 public: // IHandleTypeDispatch
    85 	void OnHandleDestroy(HandleType_t type, void *object);
    86 public: // IPluginsListener
    87 	void OnPluginUnloaded(IPlugin *plugin);
    88 public: //IRootConsoleCommand
    89 	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
    90 public:
    91 	/**
    92 	 * Get the 'ConVar' handle type ID.
    93 	 */
    94 	inline HandleType_t GetHandleType()
    95 	{
    96 		return m_ConVarType;
    97 	}
    98 
    99 	/**
   100 	 * Get the convar lookup trie.
   101 	 */
   102 	inline Trie *GetConVarCache()
   103 	{
   104 		return m_ConVarCache;
   105 	}
   106 public:
   107 	/**
   108 	 * Create a convar and return a handle to it.
   109 	 */
   110 	Handle_t CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal,
   111 	                      const char *description, int flags, bool hasMin, float min, bool hasMax, float max);
   112 
   113 	/**
   114 	 * Searches for a convar and returns a handle to it
   115 	 */
   116 	Handle_t FindConVar(const char* name);
   117 
   118 	/**
   119 	 * Add a function to call when the specified convar changes.
   120 	 */
   121 	void HookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
   122 
   123 	/**
   124 	 * Remove a function from the forward that will be called when the specified convar changes.
   125 	 */
   126 	void UnhookConVarChange(ConVar *pConVar, IPluginFunction *pFunction);
   127 
   128 	/**
   129 	 * Starts a query to find the value of a client convar.
   130 	 */
   131 	QueryCvarCookie_t QueryClientConVar(edict_t *pPlayer, const char *name, IPluginFunction *pCallback,
   132 	                                    Handle_t hndl);
   133 
   134 #if PLAPI_VERSION >= 12
   135 	/**
   136 	 * Called when Metamod:Source is about to remove convar
   137 	 */
   138 	void OnUnlinkConCommandBase(PluginId id, ConCommandBase *pCommand);
   139 #else
   140 	/**
   141 	 * Called when Metamod:Source has unloaded a plugin
   142 	 */
   143 	void OnMetamodPluginUnloaded(PluginId id);
   144 #endif
   145 private:
   146 	/**
   147 	 * Adds a convar to a plugin's list.
   148 	 */
   149 	static void AddConVarToPluginList(IPluginContext *pContext, const ConVar *pConVar);
   150 
   151 	/**
   152 	 * Static callback that Valve's ConVar object executes when the convar's value changes.
   153 	 */
   154 #if defined ORANGEBOX_BUILD
   155 	static void OnConVarChanged(IConVar *pConVar, const char *oldValue, float flOldValue);
   156 #else
   157 	static void OnConVarChanged(ConVar *pConVar, const char *oldValue);
   158 #endif
   159 
   160 	/**
   161 	 * Callback for when StartQueryCvarValue() has finished.
   162 	 */
   163 	void OnQueryCvarValueFinished(QueryCvarCookie_t cookie, edict_t *pPlayer, EQueryCvarValueStatus result,
   164 	                              const char *cvarName, const char *cvarValue);
   165 private:
   166 	HandleType_t m_ConVarType;
   167 	List<ConVarInfo *> m_ConVars;
   168 	List<ConVarQuery> m_ConVarQueries;
   169 	Trie *m_ConVarCache;
   170 	IServerPluginCallbacks *m_VSPIface;
   171 	bool m_CanQueryConVars;
   172 #if PLAPI_VERSION < 12
   173 	bool m_IgnoreHandle;
   174 #endif
   175 };
   176 
   177 extern ConVarManager g_ConVarManager;
   178 
   179 #endif // _INCLUDE_SOURCEMOD_CONVARMANAGER_H_
   180