sourcemm/CPlugin.h
author Scott Ehlert <ds@alliedmods.net>
Thu Jul 20 11:31:41 2006 +0000 (2006-07-20)
changeset 223 f44cad5df4e8
parent 205 08964c08d894
child 227 b93cfe2e3a0b
permissions -rw-r--r--
I shouldn't have touched this again in the first place
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2006 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): David "BAILOPAN" Anderson
     8 * ============================
     9 */
    10 
    11 #ifndef _INCLUDE_CPLUGIN_H
    12 #define _INCLUDE_CPLUGIN_H
    13 
    14 /**
    15  * @brief Implementation of Plugin API and Management routines
    16  * @file CPlugin.cpp
    17  */
    18 
    19 #include <interface.h>
    20 #include <eiface.h>
    21 #include <convar.h>
    22 #include "IPluginManager.h"
    23 #include "oslink.h"
    24 #include "sh_list.h"
    25 #include "sh_string.h"
    26 
    27 /**
    28  * History of plugin versions: (M=min, C=current)
    29  *    1: Initial interface
    30  *    2: Added GetLogTag()
    31  *    3: Moved GetApiVersion() to top  (2005-04-16)
    32  *    4: Added AllPluginsLoaded() callback (2005-04-18)
    33  *    5: Bumped version for SourceHook V4 (2005-05-01)
    34  *    6: Added functions for console printing (2005-05-26)
    35  *  M 7: Changed template libraries (2005-08-11)
    36  *		 New loading structure mechanism
    37  *	     New SourceHook version
    38  *  C 8: New SourceHook version (2005-12-23)
    39  *       New ISmmAPI additions
    40  */
    41 
    42 #define PLAPI_MIN_VERSION	7
    43 
    44 namespace SourceMM
    45 {
    46 	struct CNameAlias
    47 	{
    48 		SourceHook::String alias;
    49 		SourceHook::String value;
    50 	};
    51 	/**
    52 	 * @brief Implements Plugin Manager API
    53 	 */
    54 	class CPluginManager : public ISmmPluginManager
    55 	{	
    56 	public:
    57 		/**
    58 		 * @brief Internal structure for holding plugin data
    59 		 */
    60 		class CPlugin
    61 		{
    62 		public:
    63 			CPlugin();
    64 		public:
    65 			PluginId m_Id;
    66 			SourceHook::String m_File;
    67 			Pl_Status m_Status;
    68 			PluginId m_Source;
    69 			ISmmPlugin *m_API;
    70 			HINSTANCE m_Lib;
    71 			SourceHook::List<ConCommandBase *> m_Cvars;
    72 			SourceHook::List<ConCommandBase *> m_Cmds;
    73 			SourceHook::List<IMetamodListener *> m_Events;
    74 		};
    75 	public:
    76 		CPluginManager();
    77 		~CPluginManager();
    78 		void SetAllLoaded();
    79 	public:
    80 		PluginId Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen);
    81 		bool Unload(PluginId id, bool force, char *error, size_t maxlen);
    82 		bool Pause(PluginId id, char *error, size_t maxlen);
    83 		bool Unpause(PluginId id, char *error, size_t maxlen);
    84 		bool UnloadAll();
    85 		void SetAlias(const char *alias, const char *value);
    86 	public:
    87 		bool Query(PluginId id, const char *&file, Pl_Status &status, PluginId &source);
    88 		bool QueryRunning(PluginId id, char *error, size_t maxlength);
    89 
    90 		void AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
    91 		void AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
    92 		void RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
    93 		void RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
    94 
    95 		/**
    96 		 * @brief Finds a plugin by Id
    97 		 *
    98 		 * @param id Id of plugin
    99 		 * @return CPlugin on success, NULL otherwise
   100 		 */
   101 		CPlugin *FindById(PluginId id);
   102 
   103 		CPlugin *FindByAPI(ISmmPlugin *api);
   104 
   105 		/**
   106 		 * @brief Attempts to reload a failed plugin
   107 		 *
   108 		 * @param id Id of plugin
   109 		 * @param error Error message buffer
   110 		 * @param len Maximum length of buffer
   111 		 * @return True on success, false otherwise
   112 		 */
   113 		bool Retry(PluginId id, char *error, size_t len);
   114 
   115 		//get alias info
   116 		const char *LookupAlias(const char *alias);
   117 		SourceHook::List<SourceMM::CNameAlias *>::iterator _alias_begin();
   118 		SourceHook::List<SourceMM::CNameAlias *>::iterator _alias_end();
   119 
   120 		//Internal iterators
   121 		SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator _begin();
   122 		SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator _end();
   123 	private:
   124 		//These are identical internal functions for the wrappers above.
   125 		CPlugin *_Load(const char *file, PluginId source, char *error, size_t maxlen);
   126 		bool _Unload(CPlugin *pl, bool force, char *error, size_t maxlen);
   127 		bool _Pause(CPlugin *pl, char *error, size_t maxlen);
   128 		bool _Unpause(CPlugin *pl, char *error, size_t maxlen);
   129 		void UnregAllConCmds(CPlugin *pl);
   130 	private:
   131 		PluginId m_LastId;
   132 		SourceHook::List<CPlugin *> m_Plugins;
   133 		SourceHook::List<CNameAlias *> m_Aliases;
   134 		bool m_AllLoaded;
   135 	};
   136 };
   137 
   138 typedef SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator PluginIter;
   139 
   140 /** @brief Singleton for plugin manager */
   141 extern SourceMM::CPluginManager g_PluginMngr;
   142 
   143 #endif //_INCLUDE_CPLUGIN_H