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