sdk/smsdk_ext.cpp
changeset 0 4c53fd0b014e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sdk/smsdk_ext.cpp	Fri Feb 20 15:24:04 2009 +1300
     1.3 @@ -0,0 +1,461 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod Base Extension Code
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id: smsdk_ext.cpp 248 2008-08-27 00:56:22Z pred $
    1.33 + */
    1.34 +
    1.35 +#include <stdio.h>
    1.36 +#include <malloc.h>
    1.37 +#include "smsdk_ext.h"
    1.38 +
    1.39 +/**
    1.40 + * @file smsdk_ext.cpp
    1.41 + * @brief Contains wrappers for making Extensions easier to write.
    1.42 + */
    1.43 +
    1.44 +IExtension *myself = NULL;				/**< Ourself */
    1.45 +IShareSys *g_pShareSys = NULL;			/**< Share system */
    1.46 +IShareSys *sharesys = NULL;				/**< Share system */
    1.47 +ISourceMod *g_pSM = NULL;				/**< SourceMod helpers */
    1.48 +ISourceMod *smutils = NULL;				/**< SourceMod helpers */
    1.49 +
    1.50 +#if defined SMEXT_ENABLE_FORWARDSYS
    1.51 +IForwardManager *g_pForwards = NULL;	/**< Forward system */
    1.52 +IForwardManager *forwards = NULL;		/**< Forward system */
    1.53 +#endif
    1.54 +#if defined SMEXT_ENABLE_HANDLESYS
    1.55 +IHandleSys *g_pHandleSys = NULL;		/**< Handle system */
    1.56 +IHandleSys *handlesys = NULL;			/**< Handle system */
    1.57 +#endif
    1.58 +#if defined SMEXT_ENABLE_PLAYERHELPERS
    1.59 +IPlayerManager *playerhelpers = NULL;	/**< Player helpers */
    1.60 +#endif //SMEXT_ENABLE_PLAYERHELPERS
    1.61 +#if defined SMEXT_ENABLE_DBMANAGER
    1.62 +IDBManager *dbi = NULL;					/**< DB Manager */
    1.63 +#endif //SMEXT_ENABLE_DBMANAGER
    1.64 +#if defined SMEXT_ENABLE_GAMECONF
    1.65 +IGameConfigManager *gameconfs = NULL;	/**< Game config manager */
    1.66 +#endif //SMEXT_ENABLE_DBMANAGER
    1.67 +#if defined SMEXT_ENABLE_MEMUTILS
    1.68 +IMemoryUtils *memutils = NULL;
    1.69 +#endif //SMEXT_ENABLE_DBMANAGER
    1.70 +#if defined SMEXT_ENABLE_GAMEHELPERS
    1.71 +IGameHelpers *gamehelpers = NULL;
    1.72 +#endif
    1.73 +#if defined SMEXT_ENABLE_TIMERSYS
    1.74 +ITimerSystem *timersys = NULL;
    1.75 +#endif
    1.76 +#if defined SMEXT_ENABLE_ADTFACTORY
    1.77 +IADTFactory *adtfactory = NULL;
    1.78 +#endif
    1.79 +#if defined SMEXT_ENABLE_THREADER
    1.80 +IThreader *threader = NULL;
    1.81 +#endif
    1.82 +#if defined SMEXT_ENABLE_LIBSYS
    1.83 +ILibrarySys *libsys = NULL;
    1.84 +#endif
    1.85 +#if defined SMEXT_ENABLE_PLUGINSYS
    1.86 +SourceMod::IPluginManager *plsys;
    1.87 +#endif
    1.88 +#if defined SMEXT_ENABLE_MENUS
    1.89 +IMenuManager *menus = NULL;
    1.90 +#endif
    1.91 +#if defined SMEXT_ENABLE_ADMINSYS
    1.92 +IAdminSystem *adminsys = NULL;
    1.93 +#endif
    1.94 +#if defined SMEXT_ENABLE_TEXTPARSERS
    1.95 +ITextParsers *textparsers = NULL;
    1.96 +#endif
    1.97 +#if defined SMEXT_ENABLE_USERMSGS
    1.98 +IUserMessages *usermsgs = NULL;
    1.99 +#endif
   1.100 +#if defined SMEXT_ENABLE_TRANSLATOR
   1.101 +ITranslator *translator = NULL;
   1.102 +#endif
   1.103 +
   1.104 +/** Exports the main interface */
   1.105 +PLATFORM_EXTERN_C IExtensionInterface *GetSMExtAPI()
   1.106 +{
   1.107 +	return g_pExtensionIface;
   1.108 +}
   1.109 +
   1.110 +SDKExtension::SDKExtension()
   1.111 +{
   1.112 +#if defined SMEXT_CONF_METAMOD
   1.113 +	m_SourceMMLoaded = false;
   1.114 +	m_WeAreUnloaded = false;
   1.115 +	m_WeGotPauseChange = false;
   1.116 +#endif
   1.117 +}
   1.118 +
   1.119 +bool SDKExtension::OnExtensionLoad(IExtension *me, IShareSys *sys, char *error, size_t maxlength, bool late)
   1.120 +{
   1.121 +	g_pShareSys = sharesys = sys;
   1.122 +	myself = me;
   1.123 +
   1.124 +#if defined SMEXT_CONF_METAMOD
   1.125 +	m_WeAreUnloaded = true;
   1.126 +
   1.127 +	if (!m_SourceMMLoaded)
   1.128 +	{
   1.129 +		if (error)
   1.130 +		{
   1.131 +			snprintf(error, maxlength, "Metamod attach failed");
   1.132 +		}
   1.133 +		return false;
   1.134 +	}
   1.135 +#endif
   1.136 +	SM_GET_IFACE(SOURCEMOD, g_pSM);
   1.137 +	smutils = g_pSM;
   1.138 +#if defined SMEXT_ENABLE_HANDLESYS
   1.139 +	SM_GET_IFACE(HANDLESYSTEM, g_pHandleSys);
   1.140 +	handlesys = g_pHandleSys;
   1.141 +#endif
   1.142 +#if defined SMEXT_ENABLE_FORWARDSYS
   1.143 +	SM_GET_IFACE(FORWARDMANAGER, g_pForwards);
   1.144 +	forwards = g_pForwards;
   1.145 +#endif
   1.146 +#if defined SMEXT_ENABLE_PLAYERHELPERS
   1.147 +	SM_GET_IFACE(PLAYERMANAGER, playerhelpers);
   1.148 +#endif
   1.149 +#if defined SMEXT_ENABLE_DBMANAGER
   1.150 +	SM_GET_IFACE(DBI, dbi);
   1.151 +#endif
   1.152 +#if defined SMEXT_ENABLE_GAMECONF
   1.153 +	SM_GET_IFACE(GAMECONFIG, gameconfs);
   1.154 +#endif
   1.155 +#if defined SMEXT_ENABLE_MEMUTILS
   1.156 +	SM_GET_IFACE(MEMORYUTILS, memutils);
   1.157 +#endif
   1.158 +#if defined SMEXT_ENABLE_GAMEHELPERS
   1.159 +	SM_GET_IFACE(GAMEHELPERS, gamehelpers);
   1.160 +#endif
   1.161 +#if defined SMEXT_ENABLE_TIMERSYS
   1.162 +	SM_GET_IFACE(TIMERSYS, timersys);
   1.163 +#endif
   1.164 +#if defined SMEXT_ENABLE_ADTFACTORY
   1.165 +	SM_GET_IFACE(ADTFACTORY, adtfactory);
   1.166 +#endif
   1.167 +#if defined SMEXT_ENABLE_THREADER
   1.168 +	SM_GET_IFACE(THREADER, threader);
   1.169 +#endif
   1.170 +#if defined SMEXT_ENABLE_LIBSYS
   1.171 +	SM_GET_IFACE(LIBRARYSYS, libsys);
   1.172 +#endif
   1.173 +#if defined SMEXT_ENABLE_PLUGINSYS
   1.174 +	SM_GET_IFACE(PLUGINSYSTEM, plsys);
   1.175 +#endif
   1.176 +#if defined SMEXT_ENABLE_MENUS
   1.177 +	SM_GET_IFACE(MENUMANAGER, menus);
   1.178 +#endif
   1.179 +#if defined SMEXT_ENABLE_ADMINSYS
   1.180 +	SM_GET_IFACE(ADMINSYS, adminsys);
   1.181 +#endif
   1.182 +#if defined SMEXT_ENABLE_TEXTPARSERS
   1.183 +	SM_GET_IFACE(TEXTPARSERS, textparsers);
   1.184 +#endif
   1.185 +#if defined SMEXT_ENABLE_USERMSGS
   1.186 +	SM_GET_IFACE(USERMSGS, usermsgs);
   1.187 +#endif
   1.188 +#if defined SMEXT_ENABLE_TRANSLATOR
   1.189 +	SM_GET_IFACE(TRANSLATOR, translator);
   1.190 +#endif
   1.191 +
   1.192 +	if (SDK_OnLoad(error, maxlength, late))
   1.193 +	{
   1.194 +#if defined SMEXT_CONF_METAMOD
   1.195 +		m_WeAreUnloaded = true;
   1.196 +#endif
   1.197 +		return true;
   1.198 +	}
   1.199 +
   1.200 +	return false;
   1.201 +}
   1.202 +
   1.203 +bool SDKExtension::IsMetamodExtension()
   1.204 +{
   1.205 +#if defined SMEXT_CONF_METAMOD
   1.206 +	return true;
   1.207 +#else
   1.208 +	return false;
   1.209 +#endif
   1.210 +}
   1.211 +
   1.212 +void SDKExtension::OnExtensionPauseChange(bool state)
   1.213 +{
   1.214 +#if defined SMEXT_CONF_METAMOD
   1.215 +	m_WeGotPauseChange = true;
   1.216 +#endif
   1.217 +	SDK_OnPauseChange(state);
   1.218 +}
   1.219 +
   1.220 +void SDKExtension::OnExtensionsAllLoaded()
   1.221 +{
   1.222 +	SDK_OnAllLoaded();
   1.223 +}
   1.224 +
   1.225 +void SDKExtension::OnExtensionUnload()
   1.226 +{
   1.227 +#if defined SMEXT_CONF_METAMOD
   1.228 +	m_WeAreUnloaded = true;
   1.229 +#endif
   1.230 +	SDK_OnUnload();
   1.231 +}
   1.232 +
   1.233 +const char *SDKExtension::GetExtensionAuthor()
   1.234 +{
   1.235 +	return SMEXT_CONF_AUTHOR;
   1.236 +}
   1.237 +
   1.238 +const char *SDKExtension::GetExtensionDateString()
   1.239 +{
   1.240 +	return SMEXT_CONF_DATESTRING;
   1.241 +}
   1.242 +
   1.243 +const char *SDKExtension::GetExtensionDescription()
   1.244 +{
   1.245 +	return SMEXT_CONF_DESCRIPTION;
   1.246 +}
   1.247 +
   1.248 +const char *SDKExtension::GetExtensionVerString()
   1.249 +{
   1.250 +	return SMEXT_CONF_VERSION;
   1.251 +}
   1.252 +
   1.253 +const char *SDKExtension::GetExtensionName()
   1.254 +{
   1.255 +	return SMEXT_CONF_NAME;
   1.256 +}
   1.257 +
   1.258 +const char *SDKExtension::GetExtensionTag()
   1.259 +{
   1.260 +	return SMEXT_CONF_LOGTAG;
   1.261 +}
   1.262 +
   1.263 +const char *SDKExtension::GetExtensionURL()
   1.264 +{
   1.265 +	return SMEXT_CONF_URL;
   1.266 +}
   1.267 +
   1.268 +bool SDKExtension::SDK_OnLoad(char *error, size_t maxlength, bool late)
   1.269 +{
   1.270 +	return true;
   1.271 +}
   1.272 +
   1.273 +void SDKExtension::SDK_OnUnload()
   1.274 +{
   1.275 +}
   1.276 +
   1.277 +void SDKExtension::SDK_OnPauseChange(bool paused)
   1.278 +{
   1.279 +}
   1.280 +
   1.281 +void SDKExtension::SDK_OnAllLoaded()
   1.282 +{
   1.283 +}
   1.284 +
   1.285 +#if defined SMEXT_CONF_METAMOD
   1.286 +
   1.287 +PluginId g_PLID = 0;						/**< Metamod plugin ID */
   1.288 +ISmmPlugin *g_PLAPI = NULL;					/**< Metamod plugin API */
   1.289 +SourceHook::ISourceHook *g_SHPtr = NULL;	/**< SourceHook pointer */
   1.290 +ISmmAPI *g_SMAPI = NULL;					/**< SourceMM API pointer */
   1.291 +
   1.292 +IVEngineServer *engine = NULL;				/**< IVEngineServer pointer */
   1.293 +IServerGameDLL *gamedll = NULL;				/**< IServerGameDLL pointer */
   1.294 +
   1.295 +/** Exposes the extension to Metamod */
   1.296 +SMM_API void *PL_EXPOSURE(const char *name, int *code)
   1.297 +{
   1.298 +#if defined METAMOD_PLAPI_VERSION
   1.299 +	if (name && !strcmp(name, METAMOD_PLAPI_NAME))
   1.300 +#else
   1.301 +	if (name && !strcmp(name, PLAPI_NAME))
   1.302 +#endif
   1.303 +	{
   1.304 +		if (code)
   1.305 +		{
   1.306 +			*code = IFACE_OK;
   1.307 +		}
   1.308 +		return static_cast<void *>(g_pExtensionIface);
   1.309 +	}
   1.310 +
   1.311 +	if (code)
   1.312 +	{
   1.313 +		*code = IFACE_FAILED;
   1.314 +	}
   1.315 +
   1.316 +	return NULL;
   1.317 +}
   1.318 +
   1.319 +bool SDKExtension::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
   1.320 +{
   1.321 +	PLUGIN_SAVEVARS();
   1.322 +
   1.323 +#if !defined METAMOD_PLAPI_VERSION
   1.324 +	GET_V_IFACE_ANY(serverFactory, gamedll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
   1.325 +	GET_V_IFACE_CURRENT(engineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
   1.326 +#else
   1.327 +	GET_V_IFACE_ANY(GetServerFactory, gamedll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
   1.328 +	GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
   1.329 +#endif
   1.330 +
   1.331 +	m_SourceMMLoaded = true;
   1.332 +
   1.333 +	return SDK_OnMetamodLoad(ismm, error, maxlen, late);
   1.334 +}
   1.335 +
   1.336 +bool SDKExtension::Unload(char *error, size_t maxlen)
   1.337 +{
   1.338 +	if (!m_WeAreUnloaded)
   1.339 +	{
   1.340 +		if (error)
   1.341 +		{
   1.342 +			snprintf(error, maxlen, "This extension must be unloaded by SourceMod.");
   1.343 +		}
   1.344 +		return false;
   1.345 +	}
   1.346 +
   1.347 +	return SDK_OnMetamodUnload(error, maxlen);
   1.348 +}
   1.349 +
   1.350 +bool SDKExtension::Pause(char *error, size_t maxlen)
   1.351 +{
   1.352 +	if (!m_WeGotPauseChange)
   1.353 +	{
   1.354 +		if (error)
   1.355 +		{
   1.356 +			snprintf(error, maxlen, "This extension must be paused by SourceMod.");
   1.357 +		}
   1.358 +		return false;
   1.359 +	}
   1.360 +
   1.361 +	m_WeGotPauseChange = false;
   1.362 +
   1.363 +	return SDK_OnMetamodPauseChange(true, error, maxlen);
   1.364 +}
   1.365 +
   1.366 +bool SDKExtension::Unpause(char *error, size_t maxlen)
   1.367 +{
   1.368 +	if (!m_WeGotPauseChange)
   1.369 +	{
   1.370 +		if (error)
   1.371 +		{
   1.372 +			snprintf(error, maxlen, "This extension must be unpaused by SourceMod.");
   1.373 +		}
   1.374 +		return false;
   1.375 +	}
   1.376 +
   1.377 +	m_WeGotPauseChange = false;
   1.378 +
   1.379 +	return SDK_OnMetamodPauseChange(false, error, maxlen);
   1.380 +}
   1.381 +
   1.382 +const char *SDKExtension::GetAuthor()
   1.383 +{
   1.384 +	return GetExtensionAuthor();
   1.385 +}
   1.386 +
   1.387 +const char *SDKExtension::GetDate()
   1.388 +{
   1.389 +	return GetExtensionDateString();
   1.390 +}
   1.391 +
   1.392 +const char *SDKExtension::GetDescription()
   1.393 +{
   1.394 +	return GetExtensionDescription();
   1.395 +}
   1.396 +
   1.397 +const char *SDKExtension::GetLicense()
   1.398 +{
   1.399 +	return SMEXT_CONF_LICENSE;
   1.400 +}
   1.401 +
   1.402 +const char *SDKExtension::GetLogTag()
   1.403 +{
   1.404 +	return GetExtensionTag();
   1.405 +}
   1.406 +
   1.407 +const char *SDKExtension::GetName()
   1.408 +{
   1.409 +	return GetExtensionName();
   1.410 +}
   1.411 +
   1.412 +const char *SDKExtension::GetURL()
   1.413 +{
   1.414 +	return GetExtensionURL();
   1.415 +}
   1.416 +
   1.417 +const char *SDKExtension::GetVersion()
   1.418 +{
   1.419 +	return GetExtensionVerString();
   1.420 +}
   1.421 +
   1.422 +bool SDKExtension::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlength, bool late)
   1.423 +{
   1.424 +	return true;
   1.425 +}
   1.426 +
   1.427 +bool SDKExtension::SDK_OnMetamodUnload(char *error, size_t maxlength)
   1.428 +{
   1.429 +	return true;
   1.430 +}
   1.431 +
   1.432 +bool SDKExtension::SDK_OnMetamodPauseChange(bool paused, char *error, size_t maxlength)
   1.433 +{
   1.434 +	return true;
   1.435 +}
   1.436 +
   1.437 +#endif
   1.438 +
   1.439 +/* Overload a few things to prevent libstdc++ linking */
   1.440 +#if defined __linux__
   1.441 +extern "C" void __cxa_pure_virtual(void)
   1.442 +{
   1.443 +}
   1.444 +
   1.445 +void *operator new(size_t size)
   1.446 +{
   1.447 +	return malloc(size);
   1.448 +}
   1.449 +
   1.450 +void *operator new[](size_t size) 
   1.451 +{
   1.452 +	return malloc(size);
   1.453 +}
   1.454 +
   1.455 +void operator delete(void *ptr) 
   1.456 +{
   1.457 +	free(ptr);
   1.458 +}
   1.459 +
   1.460 +void operator delete[](void * ptr)
   1.461 +{
   1.462 +	free(ptr);
   1.463 +}
   1.464 +#endif