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