core/metamod_plugins.h
author Nicholas Hastings <skamonkey@gmail.com>
Sun Mar 23 23:37:46 2014 -0400 (2014-03-23)
changeset 909 57797db1d513
parent 539 db914129ae01
permissions -rw-r--r--
Add support for Contagion (bug 6006).
     1 /**
     2  * vim: set ts=4 :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2008 AlliedModders LLC and authors.
     6  * All rights reserved.
     7  * ======================================================
     8  *
     9  * This software is provided 'as-is', without any express or implied warranty.
    10  * In no event will the authors be held liable for any damages arising from 
    11  * the use of this software.
    12  * 
    13  * Permission is granted to anyone to use this software for any purpose, 
    14  * including commercial applications, and to alter it and redistribute it 
    15  * freely, subject to the following restrictions:
    16  *
    17  * 1. The origin of this software must not be misrepresented; you must not 
    18  * claim that you wrote the original software. If you use this software in a 
    19  * product, an acknowledgment in the product documentation would be 
    20  * appreciated but is not required.
    21  * 2. Altered source versions must be plainly marked as such, and must not be
    22  * misrepresented as being the original software.
    23  * 3. This notice may not be removed or altered from any source distribution.
    24  *
    25  * Version: $Id$
    26  */
    27 
    28 #ifndef _INCLUDE_CPLUGIN_H
    29 #define _INCLUDE_CPLUGIN_H
    30 
    31 /**
    32  * @brief Implementation of Plugin API and Management routines
    33  * @file CPlugin.cpp
    34  */
    35 
    36 #include <interface.h>
    37 #include <eiface.h>
    38 #include <convar.h>
    39 #include <sh_list.h>
    40 #include <sh_string.h>
    41 #include <IPluginManager.h>
    42 #include <ISmmPluginExt.h>
    43 #include "metamod_oslink.h"
    44 
    45 /**
    46  * History of plugin versions: (M=min, C=current)
    47  *     1: Initial interface
    48  *     2: Added GetLogTag()
    49  *     3: Moved GetApiVersion() to top  (2005-04-16)
    50  *     4: Added AllPluginsLoaded() callback (2005-04-18)
    51  *     5: Bumped version for SourceHook V4 (2005-05-01)
    52  *     6: Added functions for console printing (2005-05-26)
    53  *     7: Changed template libraries (2005-08-11)
    54  * 	      New loading structure mechanism
    55  *        New SourceHook version
    56  *     8: New SourceHook version (2005-12-23)
    57  *        New ISmmAPI additions
    58  *     9: New ISmmPluginManager additions
    59  *    10: Added VSP listen functions to ISmmAPI and IMetamodListener (2007-02-09)
    60  *    11: New SourceHook version v4.5 (May, 2007)
    61  *    12: Orange Box API
    62  *    13: Breaking of API for next SH version and other API changes
    63  * MC 14: ABI stability reached for 1.6.0 changes
    64  */
    65 
    66 #define PLAPI_MIN_VERSION	14
    67 
    68 struct CNameAlias
    69 {
    70 	SourceHook::String alias;
    71 	SourceHook::String value;
    72 };
    73 /**
    74  * @brief Implements Plugin Manager API
    75  */
    76 class CPluginManager : public ISmmPluginManager
    77 {	
    78 public:
    79 	/**
    80 	 * @brief Internal structure for holding plugin data
    81 	 */
    82 	class CPlugin
    83 	{
    84 	public:
    85 		CPlugin();
    86 	public:
    87 		PluginId m_Id;
    88 		SourceHook::String m_File;
    89 		Pl_Status m_Status;
    90 		PluginId m_Source;
    91 		ISmmPlugin *m_API;
    92 		HINSTANCE m_Lib;
    93 		SourceHook::List<ConCommandBase *> m_Cvars;
    94 		SourceHook::List<ConCommandBase *> m_Cmds;
    95 		SourceHook::List<IMetamodListener *> m_Events;
    96 		METAMOD_FN_UNLOAD m_UnloadFn;
    97 	};
    98 public:
    99 	CPluginManager();
   100 	~CPluginManager();
   101 	void SetAllLoaded();
   102 public:
   103 	PluginId Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen);
   104 	bool Unload(PluginId id, bool force, char *error, size_t maxlen);
   105 	bool Pause(PluginId id, char *error, size_t maxlen);
   106 	bool Unpause(PluginId id, char *error, size_t maxlen);
   107 	bool UnloadAll();
   108 	void SetAlias(const char *alias, const char *value);
   109 public:
   110 	bool Query(PluginId id, const char **file, Pl_Status *status, PluginId *source);
   111 	bool QueryRunning(PluginId id, char *error, size_t maxlength);
   112 	bool QueryHandle(PluginId id, void **handle);
   113 
   114 	void AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
   115 	void AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
   116 	void RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
   117 	void RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
   118 
   119 	/**
   120 	 * @brief Finds a plugin by Id
   121 	 *
   122 	 * @param id Id of plugin
   123 	 * @return CPlugin on success, NULL otherwise
   124 	 */
   125 	CPlugin *FindById(PluginId id);
   126 
   127 	CPlugin *FindByAPI(ISmmPlugin *api);
   128 
   129 	/**
   130 	 * @brief Attempts to reload a failed plugin
   131 	 *
   132 	 * @param id Id of plugin
   133 	 * @param error Error message buffer
   134 	 * @param len Maximum length of buffer
   135 	 * @return True on success, false otherwise
   136 	 */
   137 	bool Retry(PluginId id, char *error, size_t len);
   138 
   139 	int GetPluginCount();
   140 	const char *GetStatusText(CPlugin *pl);
   141 
   142 	//get alias info
   143 	const char *LookupAlias(const char *alias);
   144 	SourceHook::List<CNameAlias *>::iterator _alias_begin();
   145 	SourceHook::List<CNameAlias *>::iterator _alias_end();
   146 
   147 	//Internal iterators
   148 	SourceHook::List<CPluginManager::CPlugin *>::iterator _begin();
   149 	SourceHook::List<CPluginManager::CPlugin *>::iterator _end();
   150 private:
   151 	//These are identical internal functions for the wrappers above.
   152 	CPlugin *_Load(const char *file, PluginId source, char *error, size_t maxlen);
   153 	bool _Unload(CPlugin *pl, bool force, char *error, size_t maxlen);
   154 	bool _Pause(CPlugin *pl, char *error, size_t maxlen);
   155 	bool _Unpause(CPlugin *pl, char *error, size_t maxlen);
   156 	void UnregAllConCmds(CPlugin *pl);
   157 private:
   158 	PluginId m_LastId;
   159 	SourceHook::List<CPlugin *> m_Plugins;
   160 	SourceHook::List<CNameAlias *> m_Aliases;
   161 	bool m_AllLoaded;
   162 };
   163 
   164 typedef SourceHook::List<CPluginManager::CPlugin *>::iterator PluginIter;
   165 
   166 /** @brief Singleton for plugin manager */
   167 extern CPluginManager g_PluginMngr;
   168 
   169 #endif //_INCLUDE_CPLUGIN_H
   170