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