sourcemm/CPlugin.h
author Scott Ehlert <ds@alliedmods.net>
Mon Apr 17 19:42:55 2006 +0000 (2006-04-17)
changeset 187 d81c2fc9a708
parent 175 b497b7777f6b
child 192 47649a1d49e3
permissions -rw-r--r--
Fixed a warning in stub and sample :|
     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 	/**
    47 	 * @brief Implements Plugin Manager API
    48 	 */
    49 	class CPluginManager : public ISmmPluginManager
    50 	{	
    51 	public:
    52 		/**
    53 		 * @brief Internal structure for holding plugin data
    54 		 */
    55 		class CPlugin
    56 		{
    57 		public:
    58 			CPlugin();
    59 		public:
    60 			PluginId m_Id;
    61 			SourceHook::String m_File;
    62 			Pl_Status m_Status;
    63 			PluginId m_Source;
    64 			ISmmPlugin *m_API;
    65 			HINSTANCE m_Lib;
    66 			SourceHook::List<ConCommandBase *> m_Cvars;
    67 			SourceHook::List<ConCommandBase *> m_Cmds;
    68 			SourceHook::List<IMetamodListener *> m_Events;
    69 		};
    70 	public:
    71 		CPluginManager();
    72 		~CPluginManager();
    73 		void SetAllLoaded();
    74 	public:
    75 		PluginId Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen);
    76 		bool Unload(PluginId id, bool force, char *error, size_t maxlen);
    77 		bool Pause(PluginId id, char *error, size_t maxlen);
    78 		bool Unpause(PluginId id, char *error, size_t maxlen);
    79 		bool UnloadAll();
    80 	public:
    81 		bool Query(PluginId id, const char *&file, Pl_Status &status, PluginId &source);
    82 		bool QueryRunning(PluginId id, char *error, size_t maxlength);
    83 
    84 		void AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
    85 		void AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
    86 		void RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
    87 		void RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
    88 
    89 		/**
    90 		 * @brief Finds a plugin by Id
    91 		 *
    92 		 * @param id Id of plugin
    93 		 * @return CPlugin on success, NULL otherwise
    94 		 */
    95 		CPlugin *FindById(PluginId id);
    96 
    97 		CPlugin *FindByAPI(ISmmPlugin *api);
    98 
    99 		/**
   100 		 * @brief Attempts to reload a failed plugin
   101 		 *
   102 		 * @param id Id of plugin
   103 		 * @param error Error message buffer
   104 		 * @param len Maximum length of buffer
   105 		 * @return True on success, false otherwise
   106 		 */
   107 		bool Retry(PluginId id, char *error, size_t len);
   108 
   109 		//Internal iterators
   110 		SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator _begin();
   111 		SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator _end();
   112 	private:
   113 		//These are identical internal functions for the wrappers above.
   114 		CPlugin *_Load(const char *file, PluginId source, char *error, size_t maxlen);
   115 		bool _Unload(CPlugin *pl, bool force, char *error, size_t maxlen);
   116 		bool _Pause(CPlugin *pl, char *error, size_t maxlen);
   117 		bool _Unpause(CPlugin *pl, char *error, size_t maxlen);
   118 		void UnregAllConCmds(CPlugin *pl);
   119 	private:
   120 		PluginId m_LastId;
   121 		SourceHook::List<CPlugin *> m_Plugins;
   122 		bool m_AllLoaded;
   123 	};
   124 };
   125 
   126 typedef SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator PluginIter;
   127 
   128 /** @brief Singleton for plugin manager */
   129 extern SourceMM::CPluginManager g_PluginMngr;
   130 
   131 #endif //_INCLUDE_CPLUGIN_H