extensions/bintools/sdk/smsdk_ext.h
author Matt Woodrow <pred@alliedmods.net>
Thu Jan 08 18:17:17 2009 +1300 (2009-01-08)
changeset 2504 74fea7db93e4
parent 2289 44045ead61cd
permissions -rw-r--r--
Added Dynamic Hooking to BinTools (bug 2616 r=dvander,DS)
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Base Extension Code
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_EXTENSION_BASESDK_H_
    33 #define _INCLUDE_SOURCEMOD_EXTENSION_BASESDK_H_
    34 
    35 /**
    36  * @file smsdk_ext.h
    37  * @brief Contains wrappers for making Extensions easier to write.
    38  */
    39 
    40 #include "smsdk_config.h"
    41 #include <IExtensionSys.h>
    42 #include <IHandleSys.h>
    43 #include <sp_vm_api.h>
    44 #include <sm_platform.h>
    45 #include <ISourceMod.h>
    46 #if defined SMEXT_ENABLE_FORWARDSYS
    47 #include <IForwardSys.h>
    48 #endif //SMEXT_ENABLE_FORWARDSYS
    49 #if defined SMEXT_ENABLE_PLAYERHELPERS
    50 #include <IPlayerHelpers.h>
    51 #endif //SMEXT_ENABLE_PlAYERHELPERS
    52 #if defined SMEXT_ENABLE_DBMANAGER
    53 #include <IDBDriver.h>
    54 #endif //SMEXT_ENABLE_DBMANAGER
    55 #if defined SMEXT_ENABLE_GAMECONF
    56 #include <IGameConfigs.h>
    57 #endif
    58 #if defined SMEXT_ENABLE_MEMUTILS
    59 #include <IMemoryUtils.h>
    60 #endif
    61 #if defined SMEXT_ENABLE_GAMEHELPERS
    62 #include <IGameHelpers.h>
    63 #endif
    64 #if defined SMEXT_ENABLE_TIMERSYS
    65 #include <ITimerSystem.h>
    66 #endif
    67 #if defined SMEXT_ENABLE_ADTFACTORY
    68 #include <IADTFactory.h>
    69 #endif
    70 #if defined SMEXT_ENABLE_THREADER
    71 #include <IThreader.h>
    72 #endif
    73 #if defined SMEXT_ENABLE_LIBSYS
    74 #include <ILibrarySys.h>
    75 #endif
    76 #if defined SMEXT_ENABLE_PLUGINSYS
    77 #include <IPluginSys.h>
    78 #endif
    79 #if defined SMEXT_ENABLE_MENUS
    80 #include <IMenuManager.h>
    81 #endif
    82 #if defined SMEXT_ENABLE_ADMINSYS
    83 #include <IAdminSystem.h>
    84 #endif
    85 #if defined SMEXT_ENABLE_TEXTPARSERS
    86 #include <ITextParsers.h>
    87 #endif
    88 #if defined SMEXT_ENABLE_USERMSGS
    89 #include <IUserMessages.h>
    90 #endif
    91 #if defined SMEXT_ENABLE_TRANSLATOR
    92 #include <ITranslator.h>
    93 #endif
    94 
    95 #if defined SMEXT_CONF_METAMOD
    96 #include <ISmmPlugin.h>
    97 #include <eiface.h>
    98 #endif
    99 
   100 using namespace SourceMod;
   101 using namespace SourcePawn;
   102 
   103 class SDKExtension : 
   104 #if defined SMEXT_CONF_METAMOD
   105 	public ISmmPlugin,
   106 #endif
   107 	public IExtensionInterface
   108 {
   109 public:
   110 	/** Constructor */
   111 	SDKExtension();
   112 public:
   113 	/**
   114 	 * @brief This is called after the initial loading sequence has been processed.
   115 	 *
   116 	 * @param error		Error message buffer.
   117 	 * @param maxlength	Size of error message buffer.
   118 	 * @param late		Whether or not the module was loaded after map load.
   119 	 * @return			True to succeed loading, false to fail.
   120 	 */
   121 	virtual bool SDK_OnLoad(char *error, size_t maxlength, bool late);
   122 	
   123 	/**
   124 	 * @brief This is called right before the extension is unloaded.
   125 	 */
   126 	virtual void SDK_OnUnload();
   127 
   128 	/**
   129 	 * @brief This is called once all known extensions have been loaded.
   130 	 */
   131 	virtual void SDK_OnAllLoaded();
   132 
   133 	/**
   134 	 * @brief Called when the pause state is changed.
   135 	 */
   136 	virtual void SDK_OnPauseChange(bool paused);
   137 
   138 #if defined SMEXT_CONF_METAMOD
   139 	/**
   140 	 * @brief Called when Metamod is attached, before the extension version is called.
   141 	 *
   142 	 * @param error			Error buffer.
   143 	 * @param maxlength		Maximum size of error buffer.
   144 	 * @param late			Whether or not Metamod considers this a late load.
   145 	 * @return				True to succeed, false to fail.
   146 	 */
   147 	virtual bool SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlength, bool late);
   148 
   149 	/**
   150 	 * @brief Called when Metamod is detaching, after the extension version is called.
   151 	 * NOTE: By default this is blocked unless sent from SourceMod.
   152 	 *
   153 	 * @param error			Error buffer.
   154 	 * @param maxlength		Maximum size of error buffer.
   155 	 * @return				True to succeed, false to fail.
   156 	 */
   157 	virtual bool SDK_OnMetamodUnload(char *error, size_t maxlength);
   158 
   159 	/**
   160 	 * @brief Called when Metamod's pause state is changing.
   161 	 * NOTE: By default this is blocked unless sent from SourceMod.
   162 	 *
   163 	 * @param paused		Pause state being set.
   164 	 * @param error			Error buffer.
   165 	 * @param maxlength		Maximum size of error buffer.
   166 	 * @return				True to succeed, false to fail.
   167 	 */
   168 	virtual bool SDK_OnMetamodPauseChange(bool paused, char *error, size_t maxlength);
   169 #endif
   170 
   171 public: //IExtensionInterface
   172 	virtual bool OnExtensionLoad(IExtension *me, IShareSys *sys, char *error, size_t maxlength, bool late);
   173 	virtual void OnExtensionUnload();
   174 	virtual void OnExtensionsAllLoaded();
   175 
   176 	/** Returns whether or not this is a Metamod-based extension */
   177 	virtual bool IsMetamodExtension();
   178 
   179 	/**
   180 	 * @brief Called when the pause state changes.
   181 	 * 
   182 	 * @param state			True if being paused, false if being unpaused.
   183 	 */
   184 	virtual void OnExtensionPauseChange(bool state);
   185 
   186 	/** Returns name */
   187 	virtual const char *GetExtensionName();
   188 	/** Returns URL */
   189 	virtual const char *GetExtensionURL();
   190 	/** Returns log tag */
   191 	virtual const char *GetExtensionTag();
   192 	/** Returns author */
   193 	virtual const char *GetExtensionAuthor();
   194 	/** Returns version string */
   195 	virtual const char *GetExtensionVerString();
   196 	/** Returns description string */
   197 	virtual const char *GetExtensionDescription();
   198 	/** Returns date string */
   199 	virtual const char *GetExtensionDateString();
   200 #if defined SMEXT_CONF_METAMOD
   201 public: //ISmmPlugin
   202 	/** Called when the extension is attached to Metamod. */
   203 	virtual bool Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlength, bool late);
   204 	/** Returns the author to MM */
   205 	virtual const char *GetAuthor();
   206 	/** Returns the name to MM */
   207 	virtual const char *GetName();
   208 	/** Returns the description to MM */
   209 	virtual const char *GetDescription();
   210 	/** Returns the URL to MM */
   211 	virtual const char *GetURL();
   212 	/** Returns the license to MM */
   213 	virtual const char *GetLicense();
   214 	/** Returns the version string to MM */
   215 	virtual const char *GetVersion();
   216 	/** Returns the date string to MM */
   217 	virtual const char *GetDate();
   218 	/** Returns the logtag to MM */
   219 	virtual const char *GetLogTag();
   220 	/** Called on unload */
   221 	virtual bool Unload(char *error, size_t maxlength);
   222 	/** Called on pause */
   223 	virtual bool Pause(char *error, size_t maxlength);
   224 	/** Called on unpause */
   225 	virtual bool Unpause(char *error, size_t maxlength);
   226 private:
   227 	bool m_SourceMMLoaded;
   228 	bool m_WeAreUnloaded;
   229 	bool m_WeGotPauseChange;
   230 #endif
   231 };
   232 
   233 extern SDKExtension *g_pExtensionIface;
   234 extern IExtension *myself;
   235 
   236 extern IShareSys *g_pShareSys;
   237 extern IShareSys *sharesys;				/* Note: Newer name */
   238 extern ISourceMod *g_pSM;
   239 extern ISourceMod *smutils;				/* Note: Newer name */
   240 
   241 /* Optional interfaces are below */
   242 #if defined SMEXT_ENABLE_FORWARDSYS
   243 extern IForwardManager *g_pForwards;
   244 extern IForwardManager *forwards;		/* Note: Newer name */
   245 #endif //SMEXT_ENABLE_FORWARDSYS
   246 #if defined SMEXT_ENABLE_HANDLESYS
   247 extern IHandleSys *g_pHandleSys;
   248 extern IHandleSys *handlesys;			/* Note: Newer name */
   249 #endif //SMEXT_ENABLE_HANDLESYS
   250 #if defined SMEXT_ENABLE_PLAYERHELPERS
   251 extern IPlayerManager *playerhelpers;
   252 #endif //SMEXT_ENABLE_PLAYERHELPERS
   253 #if defined SMEXT_ENABLE_DBMANAGER
   254 extern IDBManager *dbi;
   255 #endif //SMEXT_ENABLE_DBMANAGER
   256 #if defined SMEXT_ENABLE_GAMECONF
   257 extern IGameConfigManager *gameconfs;
   258 #endif //SMEXT_ENABLE_DBMANAGER
   259 #if defined SMEXT_ENABLE_MEMUTILS
   260 extern IMemoryUtils *memutils;
   261 #endif
   262 #if defined SMEXT_ENABLE_GAMEHELPERS
   263 extern IGameHelpers *gamehelpers;
   264 #endif
   265 #if defined SMEXT_ENABLE_TIMERSYS
   266 extern ITimerSystem *timersys;
   267 #endif
   268 #if defined SMEXT_ENABLE_ADTFACTORY
   269 extern IADTFactory *adtfactory;
   270 #endif
   271 #if defined SMEXT_ENABLE_THREADER
   272 extern IThreader *threader;
   273 #endif
   274 #if defined SMEXT_ENABLE_LIBSYS
   275 extern ILibrarySys *libsys;
   276 #endif
   277 #if defined SMEXT_ENABLE_PLUGINSYS
   278 extern SourceMod::IPluginManager *plsys;
   279 #endif
   280 #if defined SMEXT_ENABLE_MENUS
   281 extern IMenuManager *menus;
   282 #endif
   283 #if defined SMEXT_ENABLE_ADMINSYS
   284 extern IAdminSystem *adminsys;
   285 #endif
   286 #if defined SMEXT_ENABLE_USERMSGS
   287 extern IUserMessages *usermsgs;
   288 #endif
   289 #if defined SMEXT_ENABLE_TRANSLATOR
   290 extern ITranslator *translator;
   291 #endif
   292 
   293 #if defined SMEXT_CONF_METAMOD
   294 PLUGIN_GLOBALVARS();
   295 extern IVEngineServer *engine;
   296 extern IServerGameDLL *gamedll;
   297 #endif
   298 
   299 /** Creates a SourceMod interface macro pair */
   300 #define SM_MKIFACE(name) SMINTERFACE_##name##_NAME, SMINTERFACE_##name##_VERSION
   301 /** Automates retrieving SourceMod interfaces */
   302 #define SM_GET_IFACE(prefix, addr) \
   303 	if (!g_pShareSys->RequestInterface(SM_MKIFACE(prefix), myself, (SMInterface **)&addr)) \
   304 	{ \
   305 		if (error != NULL && maxlength) \
   306 		{ \
   307 			size_t len = snprintf(error, maxlength, "Could not find interface: %s", SMINTERFACE_##prefix##_NAME); \
   308 			if (len >= maxlength) \
   309 			{ \
   310 				error[maxlength - 1] = '\0'; \
   311 			} \
   312 		} \
   313 		return false; \
   314 	}
   315 /** Automates retrieving SourceMod interfaces when needed outside of SDK_OnLoad() */
   316 #define SM_GET_LATE_IFACE(prefix, addr) \
   317 	g_pShareSys->RequestInterface(SM_MKIFACE(prefix), myself, (SMInterface **)&addr)
   318 /** Validates a SourceMod interface pointer */
   319 #define SM_CHECK_IFACE(prefix, addr) \
   320 	if (!addr) \
   321 	{ \
   322 		if (error != NULL && maxlength) \
   323 		{ \
   324 			size_t len = snprintf(error, maxlength, "Could not find interface: %s", SMINTERFACE_##prefix##_NAME); \
   325 			if (len >= maxlength) \
   326 			{ \
   327 				error[maxlength - 1] = '\0'; \
   328 			} \
   329 		} \
   330 		return false; \
   331 	}
   332 
   333 #endif // _INCLUDE_SOURCEMOD_EXTENSION_BASESDK_H_