sourcemm/metamod_plugins.h
author David Anderson <dvander@alliedmods.net>
Thu Oct 04 19:18:19 2007 +0000 (2007-10-04)
branchsourcemm-1.6.0
changeset 369 2b8c2ea08b6d
parent 367 fc76472b980f
permissions -rw-r--r--
fixed usermessages not printing
removed old ConMsg extraction code
     1 /**
     2  * vim: set ts=4 :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2007 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 <sh_list.h>
    39 #include <sh_string.h>
    40 #include "IPluginManager.h"
    41 #include "metamod_oslink.h"
    42 
    43 /**
    44  * History of plugin versions: (M=min, C=current)
    45  *     1: Initial interface
    46  *     2: Added GetLogTag()
    47  *     3: Moved GetApiVersion() to top  (2005-04-16)
    48  *     4: Added AllPluginsLoaded() callback (2005-04-18)
    49  *     5: Bumped version for SourceHook V4 (2005-05-01)
    50  *     6: Added functions for console printing (2005-05-26)
    51  * M   7: Changed template libraries (2005-08-11)
    52  * 	      New loading structure mechanism
    53  *        New SourceHook version
    54  *     8: New SourceHook version (2005-12-23)
    55  *        New ISmmAPI additions
    56  *     9: New ISmmPluginManager additions
    57  *  C 10: Added VSP listen functions to ISmmAPI and IMetamodListener (2007-02-09)
    58  *  C 11: New SourceHook version V5 (May, 2007)
    59  */
    60 
    61 #define PLAPI_MIN_VERSION	7
    62 
    63 namespace SourceMM
    64 {
    65 	struct CNameAlias
    66 	{
    67 		SourceHook::String alias;
    68 		SourceHook::String value;
    69 	};
    70 	/**
    71 	 * @brief Implements Plugin Manager API
    72 	 */
    73 	class CPluginManager : public ISmmPluginManager
    74 	{	
    75 	public:
    76 		/**
    77 		 * @brief Internal structure for holding plugin data
    78 		 */
    79 		class CPlugin
    80 		{
    81 		public:
    82 			CPlugin();
    83 		public:
    84 			PluginId m_Id;
    85 			SourceHook::String m_File;
    86 			Pl_Status m_Status;
    87 			PluginId m_Source;
    88 			ISmmPlugin *m_API;
    89 			HINSTANCE m_Lib;
    90 			SourceHook::List<ConCommandBase *> m_Cvars;
    91 			SourceHook::List<ConCommandBase *> m_Cmds;
    92 			SourceHook::List<IMetamodListener *> m_Events;
    93 		};
    94 	public:
    95 		CPluginManager();
    96 		~CPluginManager();
    97 		void SetAllLoaded();
    98 	public:
    99 		PluginId Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen);
   100 		bool Unload(PluginId id, bool force, char *error, size_t maxlen);
   101 		bool Pause(PluginId id, char *error, size_t maxlen);
   102 		bool Unpause(PluginId id, char *error, size_t maxlen);
   103 		bool UnloadAll();
   104 		void SetAlias(const char *alias, const char *value);
   105 	public:
   106 		bool Query(PluginId id, const char *&file, Pl_Status &status, PluginId &source);
   107 		bool QueryRunning(PluginId id, char *error, size_t maxlength);
   108 		bool QueryHandle(PluginId id, void *&handle);
   109 
   110 		void AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
   111 		void AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
   112 		void RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
   113 		void RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
   114 
   115 		/**
   116 		 * @brief Finds a plugin by Id
   117 		 *
   118 		 * @param id Id of plugin
   119 		 * @return CPlugin on success, NULL otherwise
   120 		 */
   121 		CPlugin *FindById(PluginId id);
   122 
   123 		CPlugin *FindByAPI(ISmmPlugin *api);
   124 
   125 		/**
   126 		 * @brief Attempts to reload a failed plugin
   127 		 *
   128 		 * @param id Id of plugin
   129 		 * @param error Error message buffer
   130 		 * @param len Maximum length of buffer
   131 		 * @return True on success, false otherwise
   132 		 */
   133 		bool Retry(PluginId id, char *error, size_t len);
   134 
   135 		int GetPluginCount();
   136 		const char *GetStatusText(CPlugin *pl);
   137 
   138 		//get alias info
   139 		const char *LookupAlias(const char *alias);
   140 		SourceHook::List<SourceMM::CNameAlias *>::iterator _alias_begin();
   141 		SourceHook::List<SourceMM::CNameAlias *>::iterator _alias_end();
   142 
   143 		//Internal iterators
   144 		SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator _begin();
   145 		SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator _end();
   146 	private:
   147 		//These are identical internal functions for the wrappers above.
   148 		CPlugin *_Load(const char *file, PluginId source, char *error, size_t maxlen);
   149 		bool _Unload(CPlugin *pl, bool force, char *error, size_t maxlen);
   150 		bool _Pause(CPlugin *pl, char *error, size_t maxlen);
   151 		bool _Unpause(CPlugin *pl, char *error, size_t maxlen);
   152 		void UnregAllConCmds(CPlugin *pl);
   153 	private:
   154 		PluginId m_LastId;
   155 		SourceHook::List<CPlugin *> m_Plugins;
   156 		SourceHook::List<CNameAlias *> m_Aliases;
   157 		bool m_AllLoaded;
   158 	};
   159 };
   160 
   161 typedef SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator PluginIter;
   162 
   163 /** @brief Singleton for plugin manager */
   164 extern SourceMM::CPluginManager g_PluginMngr;
   165 
   166 #endif //_INCLUDE_CPLUGIN_H
   167