extensions/geoip/sdk/smsdk_ext.h
author Nicholas Hastings <skamonkey@gmail.com>
Wed May 25 13:31:14 2011 -0400 (2011-05-25)
changeset 3285 065beec73902
parent 2289 44045ead61cd
permissions -rw-r--r--
Backed out changeset: 65eb3df32b1c
     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 
    77 #if defined SMEXT_CONF_METAMOD
    78 #include <ISmmPlugin.h>
    79 #include <eiface.h>
    80 #endif
    81 
    82 using namespace SourceMod;
    83 using namespace SourcePawn;
    84 
    85 class SDKExtension : 
    86 #if defined SMEXT_CONF_METAMOD
    87 	public ISmmPlugin,
    88 #endif
    89 	public IExtensionInterface
    90 {
    91 public:
    92 	/** Constructor */
    93 	SDKExtension();
    94 public:
    95 	/**
    96 	 * @brief This is called after the initial loading sequence has been processed.
    97 	 *
    98 	 * @param error		Error message buffer.
    99 	 * @param maxlength	Size of error message buffer.
   100 	 * @param late		Whether or not the module was loaded after map load.
   101 	 * @return			True to succeed loading, false to fail.
   102 	 */
   103 	virtual bool SDK_OnLoad(char *error, size_t maxlength, bool late);
   104 	
   105 	/**
   106 	 * @brief This is called right before the extension is unloaded.
   107 	 */
   108 	virtual void SDK_OnUnload();
   109 
   110 	/**
   111 	 * @brief This is called once all known extensions have been loaded.
   112 	 */
   113 	virtual void SDK_OnAllLoaded();
   114 
   115 	/**
   116 	 * @brief Called when the pause state is changed.
   117 	 */
   118 	virtual void SDK_OnPauseChange(bool paused);
   119 
   120 #if defined SMEXT_CONF_METAMOD
   121 	/**
   122 	 * @brief Called when Metamod is attached, before the extension version is called.
   123 	 *
   124 	 * @param error			Error buffer.
   125 	 * @param maxlength		Maximum size of error buffer.
   126 	 * @param late			Whether or not Metamod considers this a late load.
   127 	 * @return				True to succeed, false to fail.
   128 	 */
   129 	virtual bool SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlength, bool late);
   130 
   131 	/**
   132 	 * @brief Called when Metamod is detaching, after the extension version is called.
   133 	 * NOTE: By default this is blocked unless sent from SourceMod.
   134 	 *
   135 	 * @param error			Error buffer.
   136 	 * @param maxlength		Maximum size of error buffer.
   137 	 * @return				True to succeed, false to fail.
   138 	 */
   139 	virtual bool SDK_OnMetamodUnload(char *error, size_t maxlength);
   140 
   141 	/**
   142 	 * @brief Called when Metamod's pause state is changing.
   143 	 * NOTE: By default this is blocked unless sent from SourceMod.
   144 	 *
   145 	 * @param paused		Pause state being set.
   146 	 * @param error			Error buffer.
   147 	 * @param maxlength		Maximum size of error buffer.
   148 	 * @return				True to succeed, false to fail.
   149 	 */
   150 	virtual bool SDK_OnMetamodPauseChange(bool paused, char *error, size_t maxlength);
   151 #endif
   152 
   153 public: //IExtensionInterface
   154 	virtual bool OnExtensionLoad(IExtension *me, IShareSys *sys, char *error, size_t maxlength, bool late);
   155 	virtual void OnExtensionUnload();
   156 	virtual void OnExtensionsAllLoaded();
   157 
   158 	/** Returns whether or not this is a Metamod-based extension */
   159 	virtual bool IsMetamodExtension();
   160 
   161 	/**
   162 	 * @brief Called when the pause state changes.
   163 	 * 
   164 	 * @param state			True if being paused, false if being unpaused.
   165 	 */
   166 	virtual void OnExtensionPauseChange(bool state);
   167 
   168 	/** Returns name */
   169 	virtual const char *GetExtensionName();
   170 	/** Returns URL */
   171 	virtual const char *GetExtensionURL();
   172 	/** Returns log tag */
   173 	virtual const char *GetExtensionTag();
   174 	/** Returns author */
   175 	virtual const char *GetExtensionAuthor();
   176 	/** Returns version string */
   177 	virtual const char *GetExtensionVerString();
   178 	/** Returns description string */
   179 	virtual const char *GetExtensionDescription();
   180 	/** Returns date string */
   181 	virtual const char *GetExtensionDateString();
   182 #if defined SMEXT_CONF_METAMOD
   183 public: //ISmmPlugin
   184 	/** Called when the extension is attached to Metamod. */
   185 	virtual bool Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlength, bool late);
   186 	/** Returns the author to MM */
   187 	virtual const char *GetAuthor();
   188 	/** Returns the name to MM */
   189 	virtual const char *GetName();
   190 	/** Returns the description to MM */
   191 	virtual const char *GetDescription();
   192 	/** Returns the URL to MM */
   193 	virtual const char *GetURL();
   194 	/** Returns the license to MM */
   195 	virtual const char *GetLicense();
   196 	/** Returns the version string to MM */
   197 	virtual const char *GetVersion();
   198 	/** Returns the date string to MM */
   199 	virtual const char *GetDate();
   200 	/** Returns the logtag to MM */
   201 	virtual const char *GetLogTag();
   202 	/** Called on unload */
   203 	virtual bool Unload(char *error, size_t maxlength);
   204 	/** Called on pause */
   205 	virtual bool Pause(char *error, size_t maxlength);
   206 	/** Called on unpause */
   207 	virtual bool Unpause(char *error, size_t maxlength);
   208 private:
   209 	bool m_SourceMMLoaded;
   210 	bool m_WeAreUnloaded;
   211 	bool m_WeGotPauseChange;
   212 #endif
   213 };
   214 
   215 extern SDKExtension *g_pExtensionIface;
   216 extern IExtension *myself;
   217 
   218 extern IShareSys *g_pShareSys;
   219 extern IShareSys *sharesys;				/* Note: Newer name */
   220 extern ISourceMod *g_pSM;
   221 extern ISourceMod *smutils;				/* Note: Newer name */
   222 
   223 /* Optional interfaces are below */
   224 #if defined SMEXT_ENABLE_FORWARDSYS
   225 extern IForwardManager *g_pForwards;
   226 extern IForwardManager *forwards;		/* Note: Newer name */
   227 #endif //SMEXT_ENABLE_FORWARDSYS
   228 #if defined SMEXT_ENABLE_HANDLESYS
   229 extern IHandleSys *g_pHandleSys;
   230 extern IHandleSys *handlesys;			/* Note: Newer name */
   231 #endif //SMEXT_ENABLE_HANDLESYS
   232 #if defined SMEXT_ENABLE_PLAYERHELPERS
   233 extern IPlayerManager *playerhelpers;
   234 #endif //SMEXT_ENABLE_PLAYERHELPERS
   235 #if defined SMEXT_ENABLE_DBMANAGER
   236 extern IDBManager *dbi;
   237 #endif //SMEXT_ENABLE_DBMANAGER
   238 #if defined SMEXT_ENABLE_GAMECONF
   239 extern IGameConfigManager *gameconfs;
   240 #endif //SMEXT_ENABLE_DBMANAGER
   241 #if defined SMEXT_ENABLE_MEMUTILS
   242 extern IMemoryUtils *memutils;
   243 #endif
   244 #if defined SMEXT_ENABLE_GAMEHELPERS
   245 extern IGameHelpers *gamehelpers;
   246 #endif
   247 #if defined SMEXT_ENABLE_TIMERSYS
   248 extern ITimerSystem *timersys;
   249 #endif
   250 #if defined SMEXT_ENABLE_ADTFACTORY
   251 extern IADTFactory *adtfactory;
   252 #endif
   253 #if defined SMEXT_ENABLE_THREADER
   254 extern IThreader *threader;
   255 #endif
   256 #if defined SMEXT_ENABLE_LIBSYS
   257 extern ILibrarySys *libsys;
   258 #endif
   259 
   260 #if defined SMEXT_CONF_METAMOD
   261 PLUGIN_GLOBALVARS();
   262 extern IVEngineServer *engine;
   263 extern IServerGameDLL *gamedll;
   264 #endif
   265 
   266 /** Creates a SourceMod interface macro pair */
   267 #define SM_MKIFACE(name) SMINTERFACE_##name##_NAME, SMINTERFACE_##name##_VERSION
   268 /** Automates retrieving SourceMod interfaces */
   269 #define SM_GET_IFACE(prefix, addr) \
   270 	if (!g_pShareSys->RequestInterface(SM_MKIFACE(prefix), myself, (SMInterface **)&addr)) \
   271 	{ \
   272 		if (error != NULL && maxlength) \
   273 		{ \
   274 			size_t len = snprintf(error, maxlength, "Could not find interface: %s", SMINTERFACE_##prefix##_NAME); \
   275 			if (len >= maxlength) \
   276 			{ \
   277 				error[maxlength - 1] = '\0'; \
   278 			} \
   279 		} \
   280 		return false; \
   281 	}
   282 /** Automates retrieving SourceMod interfaces when needed outside of SDK_OnLoad() */
   283 #define SM_GET_LATE_IFACE(prefix, addr) \
   284 	g_pShareSys->RequestInterface(SM_MKIFACE(prefix), myself, (SMInterface **)&addr)
   285 /** Validates a SourceMod interface pointer */
   286 #define SM_CHECK_IFACE(prefix, addr) \
   287 	if (!addr) \
   288 	{ \
   289 		if (error != NULL && maxlength) \
   290 		{ \
   291 			size_t len = snprintf(error, maxlength, "Could not find interface: %s", SMINTERFACE_##prefix##_NAME); \
   292 			if (len >= maxlength) \
   293 			{ \
   294 				error[maxlength - 1] = '\0'; \
   295 			} \
   296 		} \
   297 		return false; \
   298 	}
   299 
   300 #endif // _INCLUDE_SOURCEMOD_EXTENSION_BASESDK_H_