core/metamod_plugins.h
changeset 539 db914129ae01
parent 523 6f1630519b81
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/metamod_plugins.h	Fri Nov 14 04:04:02 2008 -0600
     1.3 @@ -0,0 +1,170 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ======================================================
     1.7 + * Metamod:Source
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC and authors.
     1.9 + * All rights reserved.
    1.10 + * ======================================================
    1.11 + *
    1.12 + * This software is provided 'as-is', without any express or implied warranty.
    1.13 + * In no event will the authors be held liable for any damages arising from 
    1.14 + * the use of this software.
    1.15 + * 
    1.16 + * Permission is granted to anyone to use this software for any purpose, 
    1.17 + * including commercial applications, and to alter it and redistribute it 
    1.18 + * freely, subject to the following restrictions:
    1.19 + *
    1.20 + * 1. The origin of this software must not be misrepresented; you must not 
    1.21 + * claim that you wrote the original software. If you use this software in a 
    1.22 + * product, an acknowledgment in the product documentation would be 
    1.23 + * appreciated but is not required.
    1.24 + * 2. Altered source versions must be plainly marked as such, and must not be
    1.25 + * misrepresented as being the original software.
    1.26 + * 3. This notice may not be removed or altered from any source distribution.
    1.27 + *
    1.28 + * Version: $Id$
    1.29 + */
    1.30 +
    1.31 +#ifndef _INCLUDE_CPLUGIN_H
    1.32 +#define _INCLUDE_CPLUGIN_H
    1.33 +
    1.34 +/**
    1.35 + * @brief Implementation of Plugin API and Management routines
    1.36 + * @file CPlugin.cpp
    1.37 + */
    1.38 +
    1.39 +#include <interface.h>
    1.40 +#include <eiface.h>
    1.41 +#include <convar.h>
    1.42 +#include <sh_list.h>
    1.43 +#include <sh_string.h>
    1.44 +#include <IPluginManager.h>
    1.45 +#include <ISmmPluginExt.h>
    1.46 +#include "metamod_oslink.h"
    1.47 +
    1.48 +/**
    1.49 + * History of plugin versions: (M=min, C=current)
    1.50 + *     1: Initial interface
    1.51 + *     2: Added GetLogTag()
    1.52 + *     3: Moved GetApiVersion() to top  (2005-04-16)
    1.53 + *     4: Added AllPluginsLoaded() callback (2005-04-18)
    1.54 + *     5: Bumped version for SourceHook V4 (2005-05-01)
    1.55 + *     6: Added functions for console printing (2005-05-26)
    1.56 + *     7: Changed template libraries (2005-08-11)
    1.57 + * 	      New loading structure mechanism
    1.58 + *        New SourceHook version
    1.59 + *     8: New SourceHook version (2005-12-23)
    1.60 + *        New ISmmAPI additions
    1.61 + *     9: New ISmmPluginManager additions
    1.62 + *    10: Added VSP listen functions to ISmmAPI and IMetamodListener (2007-02-09)
    1.63 + *    11: New SourceHook version v4.5 (May, 2007)
    1.64 + *    12: Orange Box API
    1.65 + *    13: Breaking of API for next SH version and other API changes
    1.66 + * MC 14: ABI stability reached for 1.6.0 changes
    1.67 + */
    1.68 +
    1.69 +#define PLAPI_MIN_VERSION	14
    1.70 +
    1.71 +struct CNameAlias
    1.72 +{
    1.73 +	SourceHook::String alias;
    1.74 +	SourceHook::String value;
    1.75 +};
    1.76 +/**
    1.77 + * @brief Implements Plugin Manager API
    1.78 + */
    1.79 +class CPluginManager : public ISmmPluginManager
    1.80 +{	
    1.81 +public:
    1.82 +	/**
    1.83 +	 * @brief Internal structure for holding plugin data
    1.84 +	 */
    1.85 +	class CPlugin
    1.86 +	{
    1.87 +	public:
    1.88 +		CPlugin();
    1.89 +	public:
    1.90 +		PluginId m_Id;
    1.91 +		SourceHook::String m_File;
    1.92 +		Pl_Status m_Status;
    1.93 +		PluginId m_Source;
    1.94 +		ISmmPlugin *m_API;
    1.95 +		HINSTANCE m_Lib;
    1.96 +		SourceHook::List<ConCommandBase *> m_Cvars;
    1.97 +		SourceHook::List<ConCommandBase *> m_Cmds;
    1.98 +		SourceHook::List<IMetamodListener *> m_Events;
    1.99 +		METAMOD_FN_UNLOAD m_UnloadFn;
   1.100 +	};
   1.101 +public:
   1.102 +	CPluginManager();
   1.103 +	~CPluginManager();
   1.104 +	void SetAllLoaded();
   1.105 +public:
   1.106 +	PluginId Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen);
   1.107 +	bool Unload(PluginId id, bool force, char *error, size_t maxlen);
   1.108 +	bool Pause(PluginId id, char *error, size_t maxlen);
   1.109 +	bool Unpause(PluginId id, char *error, size_t maxlen);
   1.110 +	bool UnloadAll();
   1.111 +	void SetAlias(const char *alias, const char *value);
   1.112 +public:
   1.113 +	bool Query(PluginId id, const char **file, Pl_Status *status, PluginId *source);
   1.114 +	bool QueryRunning(PluginId id, char *error, size_t maxlength);
   1.115 +	bool QueryHandle(PluginId id, void **handle);
   1.116 +
   1.117 +	void AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
   1.118 +	void AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
   1.119 +	void RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
   1.120 +	void RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
   1.121 +
   1.122 +	/**
   1.123 +	 * @brief Finds a plugin by Id
   1.124 +	 *
   1.125 +	 * @param id Id of plugin
   1.126 +	 * @return CPlugin on success, NULL otherwise
   1.127 +	 */
   1.128 +	CPlugin *FindById(PluginId id);
   1.129 +
   1.130 +	CPlugin *FindByAPI(ISmmPlugin *api);
   1.131 +
   1.132 +	/**
   1.133 +	 * @brief Attempts to reload a failed plugin
   1.134 +	 *
   1.135 +	 * @param id Id of plugin
   1.136 +	 * @param error Error message buffer
   1.137 +	 * @param len Maximum length of buffer
   1.138 +	 * @return True on success, false otherwise
   1.139 +	 */
   1.140 +	bool Retry(PluginId id, char *error, size_t len);
   1.141 +
   1.142 +	int GetPluginCount();
   1.143 +	const char *GetStatusText(CPlugin *pl);
   1.144 +
   1.145 +	//get alias info
   1.146 +	const char *LookupAlias(const char *alias);
   1.147 +	SourceHook::List<CNameAlias *>::iterator _alias_begin();
   1.148 +	SourceHook::List<CNameAlias *>::iterator _alias_end();
   1.149 +
   1.150 +	//Internal iterators
   1.151 +	SourceHook::List<CPluginManager::CPlugin *>::iterator _begin();
   1.152 +	SourceHook::List<CPluginManager::CPlugin *>::iterator _end();
   1.153 +private:
   1.154 +	//These are identical internal functions for the wrappers above.
   1.155 +	CPlugin *_Load(const char *file, PluginId source, char *error, size_t maxlen);
   1.156 +	bool _Unload(CPlugin *pl, bool force, char *error, size_t maxlen);
   1.157 +	bool _Pause(CPlugin *pl, char *error, size_t maxlen);
   1.158 +	bool _Unpause(CPlugin *pl, char *error, size_t maxlen);
   1.159 +	void UnregAllConCmds(CPlugin *pl);
   1.160 +private:
   1.161 +	PluginId m_LastId;
   1.162 +	SourceHook::List<CPlugin *> m_Plugins;
   1.163 +	SourceHook::List<CNameAlias *> m_Aliases;
   1.164 +	bool m_AllLoaded;
   1.165 +};
   1.166 +
   1.167 +typedef SourceHook::List<CPluginManager::CPlugin *>::iterator PluginIter;
   1.168 +
   1.169 +/** @brief Singleton for plugin manager */
   1.170 +extern CPluginManager g_PluginMngr;
   1.171 +
   1.172 +#endif //_INCLUDE_CPLUGIN_H
   1.173 +