1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcemm/metamod_plugins.h Thu Oct 04 20:15:25 2007 +0000
1.3 @@ -0,0 +1,167 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * ======================================================
1.7 + * Metamod:Source
1.8 + * Copyright (C) 2004-2007 AlliedModders LLC and authors.
1.9 + * All rights reserved.
1.10 + * ======================================================
1.11 + *
1.12 + * This software is provided 'as-is', without any express or implied warranty.
1.13 + * In no event will the authors be held liable for any damages arising from
1.14 + * the use of this software.
1.15 + *
1.16 + * Permission is granted to anyone to use this software for any purpose,
1.17 + * including commercial applications, and to alter it and redistribute it
1.18 + * freely, subject to the following restrictions:
1.19 + *
1.20 + * 1. The origin of this software must not be misrepresented; you must not
1.21 + * claim that you wrote the original software. If you use this software in a
1.22 + * product, an acknowledgment in the product documentation would be
1.23 + * appreciated but is not required.
1.24 + * 2. Altered source versions must be plainly marked as such, and must not be
1.25 + * misrepresented as being the original software.
1.26 + * 3. This notice may not be removed or altered from any source distribution.
1.27 + *
1.28 + * Version: $Id$
1.29 + */
1.30 +
1.31 +#ifndef _INCLUDE_CPLUGIN_H
1.32 +#define _INCLUDE_CPLUGIN_H
1.33 +
1.34 +/**
1.35 + * @brief Implementation of Plugin API and Management routines
1.36 + * @file CPlugin.cpp
1.37 + */
1.38 +
1.39 +#include <interface.h>
1.40 +#include <eiface.h>
1.41 +#include <sh_list.h>
1.42 +#include <sh_string.h>
1.43 +#include "IPluginManager.h"
1.44 +#include "metamod_oslink.h"
1.45 +
1.46 +/**
1.47 + * History of plugin versions: (M=min, C=current)
1.48 + * 1: Initial interface
1.49 + * 2: Added GetLogTag()
1.50 + * 3: Moved GetApiVersion() to top (2005-04-16)
1.51 + * 4: Added AllPluginsLoaded() callback (2005-04-18)
1.52 + * 5: Bumped version for SourceHook V4 (2005-05-01)
1.53 + * 6: Added functions for console printing (2005-05-26)
1.54 + * M 7: Changed template libraries (2005-08-11)
1.55 + * New loading structure mechanism
1.56 + * New SourceHook version
1.57 + * 8: New SourceHook version (2005-12-23)
1.58 + * New ISmmAPI additions
1.59 + * 9: New ISmmPluginManager additions
1.60 + * C 10: Added VSP listen functions to ISmmAPI and IMetamodListener (2007-02-09)
1.61 + * C 11: New SourceHook version V5 (May, 2007)
1.62 + */
1.63 +
1.64 +#define PLAPI_MIN_VERSION 7
1.65 +
1.66 +namespace SourceMM
1.67 +{
1.68 + struct CNameAlias
1.69 + {
1.70 + SourceHook::String alias;
1.71 + SourceHook::String value;
1.72 + };
1.73 + /**
1.74 + * @brief Implements Plugin Manager API
1.75 + */
1.76 + class CPluginManager : public ISmmPluginManager
1.77 + {
1.78 + public:
1.79 + /**
1.80 + * @brief Internal structure for holding plugin data
1.81 + */
1.82 + class CPlugin
1.83 + {
1.84 + public:
1.85 + CPlugin();
1.86 + public:
1.87 + PluginId m_Id;
1.88 + SourceHook::String m_File;
1.89 + Pl_Status m_Status;
1.90 + PluginId m_Source;
1.91 + ISmmPlugin *m_API;
1.92 + HINSTANCE m_Lib;
1.93 + SourceHook::List<ConCommandBase *> m_Cvars;
1.94 + SourceHook::List<ConCommandBase *> m_Cmds;
1.95 + SourceHook::List<IMetamodListener *> m_Events;
1.96 + };
1.97 + public:
1.98 + CPluginManager();
1.99 + ~CPluginManager();
1.100 + void SetAllLoaded();
1.101 + public:
1.102 + PluginId Load(const char *file, PluginId source, bool &already, char *error, size_t maxlen);
1.103 + bool Unload(PluginId id, bool force, char *error, size_t maxlen);
1.104 + bool Pause(PluginId id, char *error, size_t maxlen);
1.105 + bool Unpause(PluginId id, char *error, size_t maxlen);
1.106 + bool UnloadAll();
1.107 + void SetAlias(const char *alias, const char *value);
1.108 + public:
1.109 + bool Query(PluginId id, const char *&file, Pl_Status &status, PluginId &source);
1.110 + bool QueryRunning(PluginId id, char *error, size_t maxlength);
1.111 + bool QueryHandle(PluginId id, void *&handle);
1.112 +
1.113 + void AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
1.114 + void AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
1.115 + void RemovePluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
1.116 + void RemovePluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
1.117 +
1.118 + /**
1.119 + * @brief Finds a plugin by Id
1.120 + *
1.121 + * @param id Id of plugin
1.122 + * @return CPlugin on success, NULL otherwise
1.123 + */
1.124 + CPlugin *FindById(PluginId id);
1.125 +
1.126 + CPlugin *FindByAPI(ISmmPlugin *api);
1.127 +
1.128 + /**
1.129 + * @brief Attempts to reload a failed plugin
1.130 + *
1.131 + * @param id Id of plugin
1.132 + * @param error Error message buffer
1.133 + * @param len Maximum length of buffer
1.134 + * @return True on success, false otherwise
1.135 + */
1.136 + bool Retry(PluginId id, char *error, size_t len);
1.137 +
1.138 + int GetPluginCount();
1.139 + const char *GetStatusText(CPlugin *pl);
1.140 +
1.141 + //get alias info
1.142 + const char *LookupAlias(const char *alias);
1.143 + SourceHook::List<SourceMM::CNameAlias *>::iterator _alias_begin();
1.144 + SourceHook::List<SourceMM::CNameAlias *>::iterator _alias_end();
1.145 +
1.146 + //Internal iterators
1.147 + SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator _begin();
1.148 + SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator _end();
1.149 + private:
1.150 + //These are identical internal functions for the wrappers above.
1.151 + CPlugin *_Load(const char *file, PluginId source, char *error, size_t maxlen);
1.152 + bool _Unload(CPlugin *pl, bool force, char *error, size_t maxlen);
1.153 + bool _Pause(CPlugin *pl, char *error, size_t maxlen);
1.154 + bool _Unpause(CPlugin *pl, char *error, size_t maxlen);
1.155 + void UnregAllConCmds(CPlugin *pl);
1.156 + private:
1.157 + PluginId m_LastId;
1.158 + SourceHook::List<CPlugin *> m_Plugins;
1.159 + SourceHook::List<CNameAlias *> m_Aliases;
1.160 + bool m_AllLoaded;
1.161 + };
1.162 +};
1.163 +
1.164 +typedef SourceHook::List<SourceMM::CPluginManager::CPlugin *>::iterator PluginIter;
1.165 +
1.166 +/** @brief Singleton for plugin manager */
1.167 +extern SourceMM::CPluginManager g_PluginMngr;
1.168 +
1.169 +#endif //_INCLUDE_CPLUGIN_H
1.170 +