extensions/geoip/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
ds@1952
    83
/** Exports the main interface */
ds@1952
    84
PLATFORM_EXTERN_C IExtensionInterface *GetSMExtAPI()
ds@1952
    85
{
ds@1952
    86
	return g_pExtensionIface;
ds@1952
    87
}
ds@1952
    88
ds@1952
    89
SDKExtension::SDKExtension()
ds@1952
    90
{
ds@1952
    91
#if defined SMEXT_CONF_METAMOD
ds@1952
    92
	m_SourceMMLoaded = false;
ds@1952
    93
	m_WeAreUnloaded = false;
ds@1952
    94
	m_WeGotPauseChange = false;
ds@1952
    95
#endif
ds@1952
    96
}
ds@1952
    97
ds@1952
    98
bool SDKExtension::OnExtensionLoad(IExtension *me, IShareSys *sys, char *error, size_t maxlength, bool late)
ds@1952
    99
{
ds@1952
   100
	g_pShareSys = sharesys = sys;
ds@1952
   101
	myself = me;
ds@1952
   102
ds@1952
   103
#if defined SMEXT_CONF_METAMOD
ds@1952
   104
	m_WeAreUnloaded = true;
ds@1952
   105
ds@1952
   106
	if (!m_SourceMMLoaded)
ds@1952
   107
	{
ds@1952
   108
		if (error)
ds@1952
   109
		{
ds@1952
   110
			snprintf(error, maxlength, "Metamod attach failed");
ds@1952
   111
		}
ds@1952
   112
		return false;
ds@1952
   113
	}
ds@1952
   114
#endif
ds@1952
   115
	SM_GET_IFACE(SOURCEMOD, g_pSM);
ds@1952
   116
	smutils = g_pSM;
ds@1952
   117
#if defined SMEXT_ENABLE_HANDLESYS
ds@1952
   118
	SM_GET_IFACE(HANDLESYSTEM, g_pHandleSys);
ds@1952
   119
	handlesys = g_pHandleSys;
ds@1952
   120
#endif
ds@1952
   121
#if defined SMEXT_ENABLE_FORWARDSYS
ds@1952
   122
	SM_GET_IFACE(FORWARDMANAGER, g_pForwards);
ds@1952
   123
	forwards = g_pForwards;
ds@1952
   124
#endif
ds@1952
   125
#if defined SMEXT_ENABLE_PLAYERHELPERS
ds@1952
   126
	SM_GET_IFACE(PLAYERMANAGER, playerhelpers);
ds@1952
   127
#endif
ds@1952
   128
#if defined SMEXT_ENABLE_DBMANAGER
ds@1952
   129
	SM_GET_IFACE(DBI, dbi);
ds@1952
   130
#endif
ds@1952
   131
#if defined SMEXT_ENABLE_GAMECONF
ds@1952
   132
	SM_GET_IFACE(GAMECONFIG, gameconfs);
ds@1952
   133
#endif
ds@1952
   134
#if defined SMEXT_ENABLE_MEMUTILS
ds@1952
   135
	SM_GET_IFACE(MEMORYUTILS, memutils);
ds@1952
   136
#endif
ds@1952
   137
#if defined SMEXT_ENABLE_GAMEHELPERS
ds@1952
   138
	SM_GET_IFACE(GAMEHELPERS, gamehelpers);
ds@1952
   139
#endif
ds@1952
   140
#if defined SMEXT_ENABLE_TIMERSYS
ds@1952
   141
	SM_GET_IFACE(TIMERSYS, timersys);
ds@1952
   142
#endif
ds@1952
   143
#if defined SMEXT_ENABLE_ADTFACTORY
ds@1952
   144
	SM_GET_IFACE(ADTFACTORY, adtfactory);
ds@1952
   145
#endif
ds@1952
   146
#if defined SMEXT_ENABLE_THREADER
ds@1952
   147
	SM_GET_IFACE(THREADER, threader);
ds@1952
   148
#endif
ds@1952
   149
#if defined SMEXT_ENABLE_LIBSYS
ds@1952
   150
	SM_GET_IFACE(LIBRARYSYS, libsys);
ds@1952
   151
#endif
ds@1952
   152
ds@1952
   153
	if (SDK_OnLoad(error, maxlength, late))
ds@1952
   154
	{
ds@1952
   155
#if defined SMEXT_CONF_METAMOD
ds@1952
   156
		m_WeAreUnloaded = true;
ds@1952
   157
#endif
ds@1952
   158
		return true;
ds@1952
   159
	}
ds@1952
   160
ds@1952
   161
	return false;
ds@1952
   162
}
ds@1952
   163
ds@1952
   164
bool SDKExtension::IsMetamodExtension()
ds@1952
   165
{
ds@1952
   166
#if defined SMEXT_CONF_METAMOD
ds@1952
   167
	return true;
ds@1952
   168
#else
ds@1952
   169
	return false;
ds@1952
   170
#endif
ds@1952
   171
}
ds@1952
   172
ds@1952
   173
void SDKExtension::OnExtensionPauseChange(bool state)
ds@1952
   174
{
ds@1952
   175
#if defined SMEXT_CONF_METAMOD
ds@1952
   176
	m_WeGotPauseChange = true;
ds@1952
   177
#endif
ds@1952
   178
	SDK_OnPauseChange(state);
ds@1952
   179
}
ds@1952
   180
ds@1952
   181
void SDKExtension::OnExtensionsAllLoaded()
ds@1952
   182
{
ds@1952
   183
	SDK_OnAllLoaded();
ds@1952
   184
}
ds@1952
   185
ds@1952
   186
void SDKExtension::OnExtensionUnload()
ds@1952
   187
{
ds@1952
   188
#if defined SMEXT_CONF_METAMOD
ds@1952
   189
	m_WeAreUnloaded = true;
ds@1952
   190
#endif
ds@1952
   191
	SDK_OnUnload();
ds@1952
   192
}
ds@1952
   193
ds@1952
   194
const char *SDKExtension::GetExtensionAuthor()
ds@1952
   195
{
ds@1952
   196
	return SMEXT_CONF_AUTHOR;
ds@1952
   197
}
ds@1952
   198
ds@1952
   199
const char *SDKExtension::GetExtensionDateString()
ds@1952
   200
{
ds@1952
   201
	return SMEXT_CONF_DATESTRING;
ds@1952
   202
}
ds@1952
   203
ds@1952
   204
const char *SDKExtension::GetExtensionDescription()
ds@1952
   205
{
ds@1952
   206
	return SMEXT_CONF_DESCRIPTION;
ds@1952
   207
}
ds@1952
   208
ds@1952
   209
const char *SDKExtension::GetExtensionVerString()
ds@1952
   210
{
ds@1952
   211
	return SMEXT_CONF_VERSION;
ds@1952
   212
}
ds@1952
   213
ds@1952
   214
const char *SDKExtension::GetExtensionName()
ds@1952
   215
{
ds@1952
   216
	return SMEXT_CONF_NAME;
ds@1952
   217
}
ds@1952
   218
ds@1952
   219
const char *SDKExtension::GetExtensionTag()
ds@1952
   220
{
ds@1952
   221
	return SMEXT_CONF_LOGTAG;
ds@1952
   222
}
ds@1952
   223
ds@1952
   224
const char *SDKExtension::GetExtensionURL()
ds@1952
   225
{
ds@1952
   226
	return SMEXT_CONF_URL;
ds@1952
   227
}
ds@1952
   228
ds@1952
   229
bool SDKExtension::SDK_OnLoad(char *error, size_t maxlength, bool late)
ds@1952
   230
{
ds@1952
   231
	return true;
ds@1952
   232
}
ds@1952
   233
ds@1952
   234
void SDKExtension::SDK_OnUnload()
ds@1952
   235
{
ds@1952
   236
}
ds@1952
   237
ds@1952
   238
void SDKExtension::SDK_OnPauseChange(bool paused)
ds@1952
   239
{
ds@1952
   240
}
ds@1952
   241
ds@1952
   242
void SDKExtension::SDK_OnAllLoaded()
ds@1952
   243
{
ds@1952
   244
}
ds@1952
   245
ds@1952
   246
#if defined SMEXT_CONF_METAMOD
ds@1952
   247
ds@1952
   248
PluginId g_PLID = 0;						/**< Metamod plugin ID */
ds@1952
   249
ISmmPlugin *g_PLAPI = NULL;					/**< Metamod plugin API */
ds@1952
   250
SourceHook::ISourceHook *g_SHPtr = NULL;	/**< SourceHook pointer */
ds@1952
   251
ISmmAPI *g_SMAPI = NULL;					/**< SourceMM API pointer */
ds@1952
   252
ds@1952
   253
IVEngineServer *engine = NULL;				/**< IVEngineServer pointer */
ds@1952
   254
IServerGameDLL *gamedll = NULL;				/**< IServerGameDLL pointer */
ds@1952
   255
ds@1952
   256
/** Exposes the extension to Metamod */
ds@1952
   257
SMM_API void *PL_EXPOSURE(const char *name, int *code)
ds@1952
   258
{
ds@1952
   259
	if (name && !strcmp(name, PLAPI_NAME))
ds@1952
   260
	{
ds@1952
   261
		if (code)
ds@1952
   262
		{
ds@1952
   263
			*code = IFACE_OK;
ds@1952
   264
		}
ds@1952
   265
		return static_cast<void *>(g_pExtensionIface);
ds@1952
   266
	}
ds@1952
   267
ds@1952
   268
	if (code)
ds@1952
   269
	{
ds@1952
   270
		*code = IFACE_FAILED;
ds@1952
   271
	}
ds@1952
   272
ds@1952
   273
	return NULL;
ds@1952
   274
}
ds@1952
   275
ds@1952
   276
bool SDKExtension::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
ds@1952
   277
{
ds@1952
   278
	PLUGIN_SAVEVARS();
ds@1952
   279
ds@1952
   280
	GET_V_IFACE_ANY(serverFactory, gamedll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
ds@1952
   281
	GET_V_IFACE_CURRENT(engineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
ds@1952
   282
ds@1952
   283
	m_SourceMMLoaded = true;
ds@1952
   284
ds@1952
   285
	return SDK_OnMetamodLoad(ismm, error, maxlen, late);
ds@1952
   286
}
ds@1952
   287
ds@1952
   288
bool SDKExtension::Unload(char *error, size_t maxlen)
ds@1952
   289
{
ds@1952
   290
	if (!m_WeAreUnloaded)
ds@1952
   291
	{
ds@1952
   292
		if (error)
ds@1952
   293
		{
ds@1952
   294
			snprintf(error, maxlen, "This extension must be unloaded by SourceMod.");
ds@1952
   295
		}
ds@1952
   296
		return false;
ds@1952
   297
	}
ds@1952
   298
ds@1952
   299
	return SDK_OnMetamodUnload(error, maxlen);
ds@1952
   300
}
ds@1952
   301
ds@1952
   302
bool SDKExtension::Pause(char *error, size_t maxlen)
ds@1952
   303
{
ds@1952
   304
	if (!m_WeGotPauseChange)
ds@1952
   305
	{
ds@1952
   306
		if (error)
ds@1952
   307
		{
ds@1952
   308
			snprintf(error, maxlen, "This extension must be paused by SourceMod.");
ds@1952
   309
		}
ds@1952
   310
		return false;
ds@1952
   311
	}
ds@1952
   312
ds@1952
   313
	m_WeGotPauseChange = false;
ds@1952
   314
ds@1952
   315
	return SDK_OnMetamodPauseChange(true, error, maxlen);
ds@1952
   316
}
ds@1952
   317
ds@1952
   318
bool SDKExtension::Unpause(char *error, size_t maxlen)
ds@1952
   319
{
ds@1952
   320
	if (!m_WeGotPauseChange)
ds@1952
   321
	{
ds@1952
   322
		if (error)
ds@1952
   323
		{
ds@1952
   324
			snprintf(error, maxlen, "This extension must be unpaused by SourceMod.");
ds@1952
   325
		}
ds@1952
   326
		return false;
ds@1952
   327
	}
ds@1952
   328
ds@1952
   329
	m_WeGotPauseChange = false;
ds@1952
   330
ds@1952
   331
	return SDK_OnMetamodPauseChange(false, error, maxlen);
ds@1952
   332
}
ds@1952
   333
ds@1952
   334
const char *SDKExtension::GetAuthor()
ds@1952
   335
{
ds@1952
   336
	return GetExtensionAuthor();
ds@1952
   337
}
ds@1952
   338
ds@1952
   339
const char *SDKExtension::GetDate()
ds@1952
   340
{
ds@1952
   341
	return GetExtensionDateString();
ds@1952
   342
}
ds@1952
   343
ds@1952
   344
const char *SDKExtension::GetDescription()
ds@1952
   345
{
ds@1952
   346
	return GetExtensionDescription();
ds@1952
   347
}
ds@1952
   348
ds@1952
   349
const char *SDKExtension::GetLicense()
ds@1952
   350
{
ds@1952
   351
	return SMEXT_CONF_LICENSE;
ds@1952
   352
}
ds@1952
   353
ds@1952
   354
const char *SDKExtension::GetLogTag()
ds@1952
   355
{
ds@1952
   356
	return GetExtensionTag();
ds@1952
   357
}
ds@1952
   358
ds@1952
   359
const char *SDKExtension::GetName()
ds@1952
   360
{
ds@1952
   361
	return GetExtensionName();
ds@1952
   362
}
ds@1952
   363
ds@1952
   364
const char *SDKExtension::GetURL()
ds@1952
   365
{
ds@1952
   366
	return GetExtensionURL();
ds@1952
   367
}
ds@1952
   368
ds@1952
   369
const char *SDKExtension::GetVersion()
ds@1952
   370
{
ds@1952
   371
	return GetExtensionVerString();
ds@1952
   372
}
ds@1952
   373
ds@1952
   374
bool SDKExtension::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlength, bool late)
ds@1952
   375
{
ds@1952
   376
	return true;
ds@1952
   377
}
ds@1952
   378
ds@1952
   379
bool SDKExtension::SDK_OnMetamodUnload(char *error, size_t maxlength)
ds@1952
   380
{
ds@1952
   381
	return true;
ds@1952
   382
}
ds@1952
   383
ds@1952
   384
bool SDKExtension::SDK_OnMetamodPauseChange(bool paused, char *error, size_t maxlength)
ds@1952
   385
{
ds@1952
   386
	return true;
ds@1952
   387
}
ds@1952
   388
ds@1952
   389
#endif
ds@1952
   390
ds@1952
   391
/* Overload a few things to prevent libstdc++ linking */
ds@1952
   392
#if defined __linux__
ds@1952
   393
extern "C" void __cxa_pure_virtual(void)
ds@1952
   394
{
ds@1952
   395
}
ds@1952
   396
ds@1952
   397
void *operator new(size_t size)
ds@1952
   398
{
ds@1952
   399
	return malloc(size);
ds@1952
   400
}
ds@1952
   401
ds@1952
   402
void *operator new[](size_t size) 
ds@1952
   403
{
ds@1952
   404
	return malloc(size);
ds@1952
   405
}
ds@1952
   406
ds@1952
   407
void operator delete(void *ptr) 
ds@1952
   408
{
ds@1952
   409
	free(ptr);
ds@1952
   410
}
ds@1952
   411
ds@1952
   412
void operator delete[](void * ptr)
ds@1952
   413
{
ds@1952
   414
	free(ptr);
ds@1952
   415
}
ds@1952
   416
#endif