sourcemm/CPlugin.h
author David Anderson <dvander@alliedmods.net>
Sun Aug 07 03:26:58 2005 +0000 (2005-08-07)
changeset 95 beffdec7d04c
parent 71 8c6c2684f216
child 97 7d3a2a8d5bf5
permissions -rw-r--r--
Bumped version number
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2005 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 <list>
    25 #include <string>
    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  * MC 5: Bumped version for SourceHook V4 (2005-05-01)
    34  */
    35 
    36 #define PLAPI_MIN_VERSION	5
    37 
    38 namespace SourceMM
    39 {
    40 	/**
    41 	 * @brief Implements Plugin Manager API
    42 	 */
    43 	class CPluginManager : public ISmmPluginManager
    44 	{	
    45 	public:
    46 		/**
    47 		 * @brief Internal structure for holding plugin data
    48 		 */
    49 		class CPlugin
    50 		{
    51 		public:
    52 			CPlugin();
    53 		public:
    54 			PluginId m_Id;
    55 			std::string m_File;
    56 			Pl_Status m_Status;
    57 			PluginId m_Source;
    58 			ISmmPlugin *m_API;
    59 			HINSTANCE m_Lib;
    60 			factories fac_list;
    61 			std::list<ConCommandBase *> m_Cvars;
    62 			std::list<ConCommandBase *> m_Cmds;
    63 		};
    64 	public:
    65 		CPluginManager();
    66 		~CPluginManager();
    67 		void SetAllLoaded();
    68 	public:
    69 		PluginId Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen);
    70 		bool Unload(PluginId id, bool force, char *error, size_t maxlen);
    71 		bool Pause(PluginId id, char *error, size_t maxlen);
    72 		bool Unpause(PluginId id, char *error, size_t maxlen);
    73 		bool UnloadAll();
    74 	public:
    75 		bool Query(PluginId id, const char *&file, factories *&list, Pl_Status &status, PluginId &source);
    76 
    77 		void AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
    78 		void AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
    79 		void RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
    80 		void RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
    81 
    82 		/**
    83 		 * @brief Finds a plugin by Id
    84 		 *
    85 		 * @param id Id of plugin
    86 		 * @return CPlugin on success, NULL otherwise
    87 		 */
    88 		CPlugin *FindById(PluginId id);
    89 
    90 		CPlugin *FindByAPI(ISmmPlugin *api);
    91 
    92 		/**
    93 		 * @brief Attempts to reload a failed plugin
    94 		 *
    95 		 * @param id Id of plugin
    96 		 * @param error Error message buffer
    97 		 * @param len Maximum length of buffer
    98 		 * @return True on success, false otherwise
    99 		 */
   100 		bool Retry(PluginId id, char *error, size_t len);
   101 
   102 		//Internal iterators
   103 		std::list<SourceMM::CPluginManager::CPlugin *>::iterator _begin();
   104 		std::list<SourceMM::CPluginManager::CPlugin *>::iterator _end();
   105 	private:
   106 		//These are identical internal functions for the wrappers above.
   107 		CPlugin *_Load(const char *file, PluginId source, char *error, size_t maxlen);
   108 		bool _Unload(CPlugin *pl, bool force, char *error, size_t maxlen);
   109 		bool _Pause(CPlugin *pl, char *error, size_t maxlen);
   110 		bool _Unpause(CPlugin *pl, char *error, size_t maxlen);
   111 		void UnregAllConCmds(CPlugin *pl);
   112 	private:
   113 		PluginId m_LastId;
   114 		std::list<CPlugin *> m_Plugins;
   115 		bool m_AllLoaded;
   116 	};
   117 };
   118 
   119 typedef std::list<SourceMM::CPluginManager::CPlugin *>::iterator PluginIter;
   120 
   121 /** @brief Singleton for plugin manager */
   122 extern SourceMM::CPluginManager g_PluginMngr;
   123 
   124 #endif //_INCLUDE_CPLUGIN_H