extensions/sdktools/sdk/smsdk_ext.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Mar 30 07:00:22 2008 +0000 (2008-03-30)
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Spring Cleaning, Part Ichi (1)
Various minor things done to project files
Updated sample extension project file and updated makefile to the new unified version (more changes likely on the way)
Updated regex project file and makefile
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod Base Extension Code
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include <stdio.h>
ds@1952
    33
#include <malloc.h>
ds@1952
    34
#include "smsdk_ext.h"
ds@1952
    35
ds@1952
    36
/**
ds@1952
    37
 * @file smsdk_ext.cpp
ds@1952
    38
 * @brief Contains wrappers for making Extensions easier to write.
ds@1952
    39
 */
ds@1952
    40
ds@1952
    41
IExtension *myself = NULL;				/**< Ourself */
ds@1952
    42
IShareSys *g_pShareSys = NULL;			/**< Share system */
ds@1952
    43
IShareSys *sharesys = NULL;				/**< Share system */
ds@1952
    44
ISourceMod *g_pSM = NULL;				/**< SourceMod helpers */
ds@1952
    45
ISourceMod *smutils = NULL;				/**< SourceMod helpers */
ds@1952
    46
ds@1952
    47
#if defined SMEXT_ENABLE_FORWARDSYS
ds@1952
    48
IForwardManager *g_pForwards = NULL;	/**< Forward system */
ds@1952
    49
IForwardManager *forwards = NULL;		/**< Forward system */
ds@1952
    50
#endif
ds@1952
    51
#if defined SMEXT_ENABLE_HANDLESYS
ds@1952
    52
IHandleSys *g_pHandleSys = NULL;		/**< Handle system */
ds@1952
    53
IHandleSys *handlesys = NULL;			/**< Handle system */
ds@1952
    54
#endif
ds@1952
    55
#if defined SMEXT_ENABLE_PLAYERHELPERS
ds@1952
    56
IPlayerManager *playerhelpers = NULL;	/**< Player helpers */
ds@1952
    57
#endif //SMEXT_ENABLE_PLAYERHELPERS
ds@1952
    58
#if defined SMEXT_ENABLE_DBMANAGER
ds@1952
    59
IDBManager *dbi = NULL;					/**< DB Manager */
ds@1952
    60
#endif //SMEXT_ENABLE_DBMANAGER
ds@1952
    61
#if defined SMEXT_ENABLE_GAMECONF
ds@1952
    62
IGameConfigManager *gameconfs = NULL;	/**< Game config manager */
ds@1952
    63
#endif //SMEXT_ENABLE_DBMANAGER
ds@1952
    64
#if defined SMEXT_ENABLE_MEMUTILS
ds@1952
    65
IMemoryUtils *memutils = NULL;
ds@1952
    66
#endif //SMEXT_ENABLE_DBMANAGER
ds@1952
    67
#if defined SMEXT_ENABLE_GAMEHELPERS
ds@1952
    68
IGameHelpers *gamehelpers = NULL;
ds@1952
    69
#endif
ds@1952
    70
#if defined SMEXT_ENABLE_TIMERSYS
ds@1952
    71
ITimerSystem *timersys = NULL;
ds@1952
    72
#endif
ds@1952
    73
#if defined SMEXT_ENABLE_ADTFACTORY
ds@1952
    74
IADTFactory *adtfactory = NULL;
ds@1952
    75
#endif
ds@1952
    76
#if defined SMEXT_ENABLE_THREADER
ds@1952
    77
IThreader *threader = NULL;
ds@1952
    78
#endif
ds@1952
    79
#if defined SMEXT_ENABLE_LIBSYS
ds@1952
    80
ILibrarySys *libsys = NULL;
ds@1952
    81
#endif
ds@1952
    82
#if defined SMEXT_ENABLE_PLUGINSYS
ds@1952
    83
SourceMod::IPluginManager *plsys;
ds@1952
    84
#endif
ds@1952
    85
ds@1952
    86
/** Exports the main interface */
ds@1952
    87
PLATFORM_EXTERN_C IExtensionInterface *GetSMExtAPI()
ds@1952
    88
{
ds@1952
    89
	return g_pExtensionIface;
ds@1952
    90
}
ds@1952
    91
ds@1952
    92
SDKExtension::SDKExtension()
ds@1952
    93
{
ds@1952
    94
#if defined SMEXT_CONF_METAMOD
ds@1952
    95
	m_SourceMMLoaded = false;
ds@1952
    96
	m_WeAreUnloaded = false;
ds@1952
    97
	m_WeGotPauseChange = false;
ds@1952
    98
#endif
ds@1952
    99
}
ds@1952
   100
ds@1952
   101
bool SDKExtension::OnExtensionLoad(IExtension *me, IShareSys *sys, char *error, size_t maxlength, bool late)
ds@1952
   102
{
ds@1952
   103
	g_pShareSys = sharesys = sys;
ds@1952
   104
	myself = me;
ds@1952
   105
ds@1952
   106
#if defined SMEXT_CONF_METAMOD
ds@1952
   107
	m_WeAreUnloaded = true;
ds@1952
   108
ds@1952
   109
	if (!m_SourceMMLoaded)
ds@1952
   110
	{
ds@1952
   111
		if (error)
ds@1952
   112
		{
ds@1952
   113
			snprintf(error, maxlength, "Metamod attach failed");
ds@1952
   114
		}
ds@1952
   115
		return false;
ds@1952
   116
	}
ds@1952
   117
#endif
ds@1952
   118
	SM_GET_IFACE(SOURCEMOD, g_pSM);
ds@1952
   119
	smutils = g_pSM;
ds@1952
   120
#if defined SMEXT_ENABLE_HANDLESYS
ds@1952
   121
	SM_GET_IFACE(HANDLESYSTEM, g_pHandleSys);
ds@1952
   122
	handlesys = g_pHandleSys;
ds@1952
   123
#endif
ds@1952
   124
#if defined SMEXT_ENABLE_FORWARDSYS
ds@1952
   125
	SM_GET_IFACE(FORWARDMANAGER, g_pForwards);
ds@1952
   126
	forwards = g_pForwards;
ds@1952
   127
#endif
ds@1952
   128
#if defined SMEXT_ENABLE_PLAYERHELPERS
ds@1952
   129
	SM_GET_IFACE(PLAYERMANAGER, playerhelpers);
ds@1952
   130
#endif
ds@1952
   131
#if defined SMEXT_ENABLE_DBMANAGER
ds@1952
   132
	SM_GET_IFACE(DBI, dbi);
ds@1952
   133
#endif
ds@1952
   134
#if defined SMEXT_ENABLE_GAMECONF
ds@1952
   135
	SM_GET_IFACE(GAMECONFIG, gameconfs);
ds@1952
   136
#endif
ds@1952
   137
#if defined SMEXT_ENABLE_MEMUTILS
ds@1952
   138
	SM_GET_IFACE(MEMORYUTILS, memutils);
ds@1952
   139
#endif
ds@1952
   140
#if defined SMEXT_ENABLE_GAMEHELPERS
ds@1952
   141
	SM_GET_IFACE(GAMEHELPERS, gamehelpers);
ds@1952
   142
#endif
ds@1952
   143
#if defined SMEXT_ENABLE_TIMERSYS
ds@1952
   144
	SM_GET_IFACE(TIMERSYS, timersys);
ds@1952
   145
#endif
ds@1952
   146
#if defined SMEXT_ENABLE_ADTFACTORY
ds@1952
   147
	SM_GET_IFACE(ADTFACTORY, adtfactory);
ds@1952
   148
#endif
ds@1952
   149
#if defined SMEXT_ENABLE_THREADER
ds@1952
   150
	SM_GET_IFACE(THREADER, threader);
ds@1952
   151
#endif
ds@1952
   152
#if defined SMEXT_ENABLE_LIBSYS
ds@1952
   153
	SM_GET_IFACE(LIBRARYSYS, libsys);
ds@1952
   154
#endif
ds@1952
   155
#if defined SMEXT_ENABLE_PLUGINSYS
ds@1952
   156
	SM_GET_IFACE(PLUGINSYSTEM, plsys);
ds@1952
   157
#endif
ds@1952
   158
ds@1952
   159
	if (SDK_OnLoad(error, maxlength, late))
ds@1952
   160
	{
ds@1952
   161
#if defined SMEXT_CONF_METAMOD
ds@1952
   162
		m_WeAreUnloaded = true;
ds@1952
   163
#endif
ds@1952
   164
		return true;
ds@1952
   165
	}
ds@1952
   166
ds@1952
   167
	return false;
ds@1952
   168
}
ds@1952
   169
ds@1952
   170
bool SDKExtension::IsMetamodExtension()
ds@1952
   171
{
ds@1952
   172
#if defined SMEXT_CONF_METAMOD
ds@1952
   173
	return true;
ds@1952
   174
#else
ds@1952
   175
	return false;
ds@1952
   176
#endif
ds@1952
   177
}
ds@1952
   178
ds@1952
   179
void SDKExtension::OnExtensionPauseChange(bool state)
ds@1952
   180
{
ds@1952
   181
#if defined SMEXT_CONF_METAMOD
ds@1952
   182
	m_WeGotPauseChange = true;
ds@1952
   183
#endif
ds@1952
   184
	SDK_OnPauseChange(state);
ds@1952
   185
}
ds@1952
   186
ds@1952
   187
void SDKExtension::OnExtensionsAllLoaded()
ds@1952
   188
{
ds@1952
   189
	SDK_OnAllLoaded();
ds@1952
   190
}
ds@1952
   191
ds@1952
   192
void SDKExtension::OnExtensionUnload()
ds@1952
   193
{
ds@1952
   194
#if defined SMEXT_CONF_METAMOD
ds@1952
   195
	m_WeAreUnloaded = true;
ds@1952
   196
#endif
ds@1952
   197
	SDK_OnUnload();
ds@1952
   198
}
ds@1952
   199
ds@1952
   200
const char *SDKExtension::GetExtensionAuthor()
ds@1952
   201
{
ds@1952
   202
	return SMEXT_CONF_AUTHOR;
ds@1952
   203
}
ds@1952
   204
ds@1952
   205
const char *SDKExtension::GetExtensionDateString()
ds@1952
   206
{
ds@1952
   207
	return SMEXT_CONF_DATESTRING;
ds@1952
   208
}
ds@1952
   209
ds@1952
   210
const char *SDKExtension::GetExtensionDescription()
ds@1952
   211
{
ds@1952
   212
	return SMEXT_CONF_DESCRIPTION;
ds@1952
   213
}
ds@1952
   214
ds@1952
   215
const char *SDKExtension::GetExtensionVerString()
ds@1952
   216
{
ds@1952
   217
	return SMEXT_CONF_VERSION;
ds@1952
   218
}
ds@1952
   219
ds@1952
   220
const char *SDKExtension::GetExtensionName()
ds@1952
   221
{
ds@1952
   222
	return SMEXT_CONF_NAME;
ds@1952
   223
}
ds@1952
   224
ds@1952
   225
const char *SDKExtension::GetExtensionTag()
ds@1952
   226
{
ds@1952
   227
	return SMEXT_CONF_LOGTAG;
ds@1952
   228
}
ds@1952
   229
ds@1952
   230
const char *SDKExtension::GetExtensionURL()
ds@1952
   231
{
ds@1952
   232
	return SMEXT_CONF_URL;
ds@1952
   233
}
ds@1952
   234
ds@1952
   235
bool SDKExtension::SDK_OnLoad(char *error, size_t maxlength, bool late)
ds@1952
   236
{
ds@1952
   237
	return true;
ds@1952
   238
}
ds@1952
   239
ds@1952
   240
void SDKExtension::SDK_OnUnload()
ds@1952
   241
{
ds@1952
   242
}
ds@1952
   243
ds@1952
   244
void SDKExtension::SDK_OnPauseChange(bool paused)
ds@1952
   245
{
ds@1952
   246
}
ds@1952
   247
ds@1952
   248
void SDKExtension::SDK_OnAllLoaded()
ds@1952
   249
{
ds@1952
   250
}
ds@1952
   251
ds@1952
   252
#if defined SMEXT_CONF_METAMOD
ds@1952
   253
ds@1952
   254
PluginId g_PLID = 0;						/**< Metamod plugin ID */
ds@1952
   255
ISmmPlugin *g_PLAPI = NULL;					/**< Metamod plugin API */
ds@1952
   256
SourceHook::ISourceHook *g_SHPtr = NULL;	/**< SourceHook pointer */
ds@1952
   257
ISmmAPI *g_SMAPI = NULL;					/**< SourceMM API pointer */
ds@1952
   258
ds@1952
   259
IVEngineServer *engine = NULL;				/**< IVEngineServer pointer */
ds@1952
   260
IServerGameDLL *gamedll = NULL;				/**< IServerGameDLL pointer */
ds@1952
   261
ds@1952
   262
/** Exposes the extension to Metamod */
ds@1952
   263
SMM_API void *PL_EXPOSURE(const char *name, int *code)
ds@1952
   264
{
ds@1952
   265
	if (name && !strcmp(name, METAMOD_PLAPI_NAME))
ds@1952
   266
	{
ds@1952
   267
		if (code)
ds@1952
   268
		{
ds@1952
   269
			*code = IFACE_OK;
ds@1952
   270
		}
ds@1952
   271
		return static_cast<void *>(g_pExtensionIface);
ds@1952
   272
	}
ds@1952
   273
ds@1952
   274
	if (code)
ds@1952
   275
	{
ds@1952
   276
		*code = IFACE_FAILED;
ds@1952
   277
	}
ds@1952
   278
ds@1952
   279
	return NULL;
ds@1952
   280
}
ds@1952
   281
ds@1952
   282
bool SDKExtension::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
ds@1952
   283
{
ds@1952
   284
	PLUGIN_SAVEVARS();
ds@1952
   285
ds@1952
   286
	GET_V_IFACE_ANY(GetServerFactory, gamedll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
ds@1952
   287
	GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
ds@1952
   288
ds@1952
   289
	m_SourceMMLoaded = true;
ds@1952
   290
ds@1952
   291
	return SDK_OnMetamodLoad(ismm, error, maxlen, late);
ds@1952
   292
}
ds@1952
   293
ds@1952
   294
bool SDKExtension::Unload(char *error, size_t maxlen)
ds@1952
   295
{
ds@1952
   296
	if (!m_WeAreUnloaded)
ds@1952
   297
	{
ds@1952
   298
		if (error)
ds@1952
   299
		{
ds@1952
   300
			snprintf(error, maxlen, "This extension must be unloaded by SourceMod.");
ds@1952
   301
		}
ds@1952
   302
		return false;
ds@1952
   303
	}
ds@1952
   304
ds@1952
   305
	return SDK_OnMetamodUnload(error, maxlen);
ds@1952
   306
}
ds@1952
   307
ds@1952
   308
bool SDKExtension::Pause(char *error, size_t maxlen)
ds@1952
   309
{
ds@1952
   310
	if (!m_WeGotPauseChange)
ds@1952
   311
	{
ds@1952
   312
		if (error)
ds@1952
   313
		{
ds@1952
   314
			snprintf(error, maxlen, "This extension must be paused by SourceMod.");
ds@1952
   315
		}
ds@1952
   316
		return false;
ds@1952
   317
	}
ds@1952
   318
ds@1952
   319
	m_WeGotPauseChange = false;
ds@1952
   320
ds@1952
   321
	return SDK_OnMetamodPauseChange(true, error, maxlen);
ds@1952
   322
}
ds@1952
   323
ds@1952
   324
bool SDKExtension::Unpause(char *error, size_t maxlen)
ds@1952
   325
{
ds@1952
   326
	if (!m_WeGotPauseChange)
ds@1952
   327
	{
ds@1952
   328
		if (error)
ds@1952
   329
		{
ds@1952
   330
			snprintf(error, maxlen, "This extension must be unpaused by SourceMod.");
ds@1952
   331
		}
ds@1952
   332
		return false;
ds@1952
   333
	}
ds@1952
   334
ds@1952
   335
	m_WeGotPauseChange = false;
ds@1952
   336
ds@1952
   337
	return SDK_OnMetamodPauseChange(false, error, maxlen);
ds@1952
   338
}
ds@1952
   339
ds@1952
   340
const char *SDKExtension::GetAuthor()
ds@1952
   341
{
ds@1952
   342
	return GetExtensionAuthor();
ds@1952
   343
}
ds@1952
   344
ds@1952
   345
const char *SDKExtension::GetDate()
ds@1952
   346
{
ds@1952
   347
	return GetExtensionDateString();
ds@1952
   348
}
ds@1952
   349
ds@1952
   350
const char *SDKExtension::GetDescription()
ds@1952
   351
{
ds@1952
   352
	return GetExtensionDescription();
ds@1952
   353
}
ds@1952
   354
ds@1952
   355
const char *SDKExtension::GetLicense()
ds@1952
   356
{
ds@1952
   357
	return SMEXT_CONF_LICENSE;
ds@1952
   358
}
ds@1952
   359
ds@1952
   360
const char *SDKExtension::GetLogTag()
ds@1952
   361
{
ds@1952
   362
	return GetExtensionTag();
ds@1952
   363
}
ds@1952
   364
ds@1952
   365
const char *SDKExtension::GetName()
ds@1952
   366
{
ds@1952
   367
	return GetExtensionName();
ds@1952
   368
}
ds@1952
   369
ds@1952
   370
const char *SDKExtension::GetURL()
ds@1952
   371
{
ds@1952
   372
	return GetExtensionURL();
ds@1952
   373
}
ds@1952
   374
ds@1952
   375
const char *SDKExtension::GetVersion()
ds@1952
   376
{
ds@1952
   377
	return GetExtensionVerString();
ds@1952
   378
}
ds@1952
   379
ds@1952
   380
bool SDKExtension::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlength, bool late)
ds@1952
   381
{
ds@1952
   382
	return true;
ds@1952
   383
}
ds@1952
   384
ds@1952
   385
bool SDKExtension::SDK_OnMetamodUnload(char *error, size_t maxlength)
ds@1952
   386
{
ds@1952
   387
	return true;
ds@1952
   388
}
ds@1952
   389
ds@1952
   390
bool SDKExtension::SDK_OnMetamodPauseChange(bool paused, char *error, size_t maxlength)
ds@1952
   391
{
ds@1952
   392
	return true;
ds@1952
   393
}
ds@1952
   394
ds@1952
   395
#endif
ds@1952
   396
ds@1952
   397
/* Overload a few things to prevent libstdc++ linking */
ds@1952
   398
#if defined __linux__
ds@1952
   399
extern "C" void __cxa_pure_virtual(void)
ds@1952
   400
{
ds@1952
   401
}
ds@1952
   402
ds@1952
   403
void *operator new(size_t size)
ds@1952
   404
{
ds@1952
   405
	return malloc(size);
ds@1952
   406
}
ds@1952
   407
ds@1952
   408
void *operator new[](size_t size) 
ds@1952
   409
{
ds@1952
   410
	return malloc(size);
ds@1952
   411
}
ds@1952
   412
ds@1952
   413
void operator delete(void *ptr) 
ds@1952
   414
{
ds@1952
   415
	free(ptr);
ds@1952
   416
}
ds@1952
   417
ds@1952
   418
void operator delete[](void * ptr)
ds@1952
   419
{
ds@1952
   420
	free(ptr);
ds@1952
   421
}
ds@1952
   422
#endif