sourcemm/metamod_plugins.h
author David Anderson <dvander@alliedmods.net>
Mon Oct 08 17:32:28 2007 +0000 (2007-10-08)
changeset 395 9fef23a837ac
parent 375 17ca1d5645ac
child 396 ed81bca8944a
permissions -rw-r--r--
added linux support, though we need a tier1_i486.a for ob
     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  *     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  *    10: Added VSP listen functions to ISmmAPI and IMetamodListener (2007-02-09)
    58  *    11: New SourceHook version v4.5 (May, 2007)
    59  *    12: Orange Box API
    60  * MC 13: Breaking of API for next SH version and other API changes
    61  */
    62 
    63 #define PLAPI_MIN_VERSION	13
    64 
    65 namespace SourceMM
    66 {
    67 	struct CNameAlias
    68 	{
    69 		SourceHook::String alias;
    70 		SourceHook::String value;
    71 	};
    72 	/**
    73 	 * @brief Implements Plugin Manager API
    74 	 */
    75 	class CPluginManager : public ISmmPluginManager
    76 	{	
    77 	public:
    78 		/**
    79 		 * @brief Internal structure for holding plugin data
    80 		 */
    81 		class CPlugin
    82 		{
    83 		public:
    84 			CPlugin();
    85 		public:
    86 			PluginId m_Id;
    87 			SourceHook::String m_File;
    88 			Pl_Status m_Status;
    89 			PluginId m_Source;
    90 			ISmmPlugin *m_API;
    91 			HINSTANCE m_Lib;
    92 			SourceHook::List<ConCommandBase *> m_Cvars;
    93 			SourceHook::List<ConCommandBase *> m_Cmds;
    94 			SourceHook::List<IMetamodListener *> m_Events;
    95 		};
    96 	public:
    97 		CPluginManager();
    98 		~CPluginManager();
    99 		void SetAllLoaded();
   100 	public:
   101 		PluginId Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen);
   102 		bool Unload(PluginId id, bool force, char *error, size_t maxlen);
   103 		bool Pause(PluginId id, char *error, size_t maxlen);
   104 		bool Unpause(PluginId id, char *error, size_t maxlen);
   105 		bool UnloadAll();
   106 		void SetAlias(const char *alias, const char *value);
   107 	public:
   108 		bool Query(PluginId id, const char *&file, Pl_Status &status, PluginId &source);
   109 		bool QueryRunning(PluginId id, char *error, size_t maxlength);
   110 		bool QueryHandle(PluginId id, void *&handle);
   111 
   112 		void AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
   113 		void AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
   114 		void RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
   115 		void RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
   116 
   117 		/**
   118 		 * @brief Finds a plugin by Id
   119 		 *
   120 		 * @param id Id of plugin
   121 		 * @return CPlugin on success, NULL otherwise
   122 		 */
   123 		CPlugin *FindById(PluginId id);
   124 
   125 		CPlugin *FindByAPI(ISmmPlugin *api);
   126 
   127 		/**
   128 		 * @brief Attempts to reload a failed plugin
   129 		 *
   130 		 * @param id Id of plugin
   131 		 * @param error Error message buffer
   132 		 * @param len Maximum length of buffer
   133 		 * @return True on success, false otherwise
   134 		 */
   135 		bool Retry(PluginId id, char *error, size_t len);
   136 
   137 		int GetPluginCount();
   138 		const char *GetStatusText(CPlugin *pl);
   139 
   140 		//get alias info
   141 		const char *LookupAlias(const char *alias);
   142 		SourceHook::List<SourceMM::CNameAlias *>::iterator _alias_begin();
   143 		SourceHook::List<SourceMM::CNameAlias *>::iterator _alias_end();
   144 
   145 		//Internal iterators
   146 		SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator _begin();
   147 		SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator _end();
   148 	private:
   149 		//These are identical internal functions for the wrappers above.
   150 		CPlugin *_Load(const char *file, PluginId source, char *error, size_t maxlen);
   151 		bool _Unload(CPlugin *pl, bool force, char *error, size_t maxlen);
   152 		bool _Pause(CPlugin *pl, char *error, size_t maxlen);
   153 		bool _Unpause(CPlugin *pl, char *error, size_t maxlen);
   154 		void UnregAllConCmds(CPlugin *pl);
   155 	private:
   156 		PluginId m_LastId;
   157 		SourceHook::List<CPlugin *> m_Plugins;
   158 		SourceHook::List<CNameAlias *> m_Aliases;
   159 		bool m_AllLoaded;
   160 	};
   161 };
   162 
   163 typedef SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator PluginIter;
   164 
   165 /** @brief Singleton for plugin manager */
   166 extern SourceMM::CPluginManager g_PluginMngr;
   167 
   168 #endif //_INCLUDE_CPLUGIN_H
   169