extensions/sqlite/sdk/smsdk_ext.cpp
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/sqlite/sdk/smsdk_ext.cpp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,415 @@
     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$
    1.33 + */
    1.34 +#include <stdio.h>
    1.35 +#include <malloc.h>
    1.36 +#include "smsdk_ext.h"
    1.37 +
    1.38 +/**
    1.39 + * @file smsdk_ext.cpp
    1.40 + * @brief Contains wrappers for making Extensions easier to write.
    1.41 + */
    1.42 +
    1.43 +IExtension *myself = NULL;				/**< Ourself */
    1.44 +IShareSys *g_pShareSys = NULL;			/**< Share system */
    1.45 +IShareSys *sharesys = NULL;				/**< Share system */
    1.46 +ISourceMod *g_pSM = NULL;				/**< SourceMod helpers */
    1.47 +ISourceMod *smutils = NULL;				/**< SourceMod helpers */
    1.48 +
    1.49 +#if defined SMEXT_ENABLE_FORWARDSYS
    1.50 +IForwardManager *g_pForwards = NULL;	/**< Forward system */
    1.51 +IForwardManager *forwards = NULL;		/**< Forward system */
    1.52 +#endif
    1.53 +#if defined SMEXT_ENABLE_HANDLESYS
    1.54 +IHandleSys *g_pHandleSys = NULL;		/**< Handle system */
    1.55 +IHandleSys *handlesys = NULL;			/**< Handle system */
    1.56 +#endif
    1.57 +#if defined SMEXT_ENABLE_PLAYERHELPERS
    1.58 +IPlayerManager *playerhelpers = NULL;	/**< Player helpers */
    1.59 +#endif //SMEXT_ENABLE_PLAYERHELPERS
    1.60 +#if defined SMEXT_ENABLE_DBMANAGER
    1.61 +IDBManager *dbi = NULL;					/**< DB Manager */
    1.62 +#endif //SMEXT_ENABLE_DBMANAGER
    1.63 +#if defined SMEXT_ENABLE_GAMECONF
    1.64 +IGameConfigManager *gameconfs = NULL;	/**< Game config manager */
    1.65 +#endif //SMEXT_ENABLE_DBMANAGER
    1.66 +#if defined SMEXT_ENABLE_MEMUTILS
    1.67 +IMemoryUtils *memutils = NULL;
    1.68 +#endif //SMEXT_ENABLE_DBMANAGER
    1.69 +#if defined SMEXT_ENABLE_GAMEHELPERS
    1.70 +IGameHelpers *gamehelpers = NULL;
    1.71 +#endif
    1.72 +#if defined SMEXT_ENABLE_TIMERSYS
    1.73 +ITimerSystem *timersys = NULL;
    1.74 +#endif
    1.75 +#if defined SMEXT_ENABLE_ADTFACTORY
    1.76 +IADTFactory *adtfactory = NULL;
    1.77 +#endif
    1.78 +#if defined SMEXT_ENABLE_THREADER
    1.79 +IThreader *threader = NULL;
    1.80 +#endif
    1.81 +#if defined SMEXT_ENABLE_LIBSYS
    1.82 +ILibrarySys *libsys = NULL;
    1.83 +#endif
    1.84 +
    1.85 +/** Exports the main interface */
    1.86 +PLATFORM_EXTERN_C IExtensionInterface *GetSMExtAPI()
    1.87 +{
    1.88 +	return g_pExtensionIface;
    1.89 +}
    1.90 +
    1.91 +SDKExtension::SDKExtension()
    1.92 +{
    1.93 +#if defined SMEXT_CONF_METAMOD
    1.94 +	m_SourceMMLoaded = false;
    1.95 +	m_WeAreUnloaded = false;
    1.96 +	m_WeGotPauseChange = false;
    1.97 +#endif
    1.98 +}
    1.99 +
   1.100 +bool SDKExtension::OnExtensionLoad(IExtension *me, IShareSys *sys, char *error, size_t maxlength, bool late)
   1.101 +{
   1.102 +	g_pShareSys = sharesys = sys;
   1.103 +	myself = me;
   1.104 +
   1.105 +#if defined SMEXT_CONF_METAMOD
   1.106 +	m_WeAreUnloaded = true;
   1.107 +
   1.108 +	if (!m_SourceMMLoaded)
   1.109 +	{
   1.110 +		if (error)
   1.111 +		{
   1.112 +			snprintf(error, maxlength, "Metamod attach failed");
   1.113 +		}
   1.114 +		return false;
   1.115 +	}
   1.116 +#endif
   1.117 +	SM_GET_IFACE(SOURCEMOD, g_pSM);
   1.118 +	smutils = g_pSM;
   1.119 +#if defined SMEXT_ENABLE_HANDLESYS
   1.120 +	SM_GET_IFACE(HANDLESYSTEM, g_pHandleSys);
   1.121 +	handlesys = g_pHandleSys;
   1.122 +#endif
   1.123 +#if defined SMEXT_ENABLE_FORWARDSYS
   1.124 +	SM_GET_IFACE(FORWARDMANAGER, g_pForwards);
   1.125 +	forwards = g_pForwards;
   1.126 +#endif
   1.127 +#if defined SMEXT_ENABLE_PLAYERHELPERS
   1.128 +	SM_GET_IFACE(PLAYERMANAGER, playerhelpers);
   1.129 +#endif
   1.130 +#if defined SMEXT_ENABLE_DBMANAGER
   1.131 +	SM_GET_IFACE(DBI, dbi);
   1.132 +#endif
   1.133 +#if defined SMEXT_ENABLE_GAMECONF
   1.134 +	SM_GET_IFACE(GAMECONFIG, gameconfs);
   1.135 +#endif
   1.136 +#if defined SMEXT_ENABLE_MEMUTILS
   1.137 +	SM_GET_IFACE(MEMORYUTILS, memutils);
   1.138 +#endif
   1.139 +#if defined SMEXT_ENABLE_GAMEHELPERS
   1.140 +	SM_GET_IFACE(GAMEHELPERS, gamehelpers);
   1.141 +#endif
   1.142 +#if defined SMEXT_ENABLE_TIMERSYS
   1.143 +	SM_GET_IFACE(TIMERSYS, timersys);
   1.144 +#endif
   1.145 +#if defined SMEXT_ENABLE_ADTFACTORY
   1.146 +	SM_GET_IFACE(ADTFACTORY, adtfactory);
   1.147 +#endif
   1.148 +#if defined SMEXT_ENABLE_THREADER
   1.149 +	SM_GET_IFACE(THREADER, threader);
   1.150 +#endif
   1.151 +#if defined SMEXT_ENABLE_LIBSYS
   1.152 +	SM_GET_IFACE(LIBRARYSYS, libsys);
   1.153 +#endif
   1.154 +
   1.155 +	if (SDK_OnLoad(error, maxlength, late))
   1.156 +	{
   1.157 +#if defined SMEXT_CONF_METAMOD
   1.158 +		m_WeAreUnloaded = true;
   1.159 +#endif
   1.160 +		return true;
   1.161 +	}
   1.162 +
   1.163 +	return false;
   1.164 +}
   1.165 +
   1.166 +bool SDKExtension::IsMetamodExtension()
   1.167 +{
   1.168 +#if defined SMEXT_CONF_METAMOD
   1.169 +	return true;
   1.170 +#else
   1.171 +	return false;
   1.172 +#endif
   1.173 +}
   1.174 +
   1.175 +void SDKExtension::OnExtensionPauseChange(bool state)
   1.176 +{
   1.177 +#if defined SMEXT_CONF_METAMOD
   1.178 +	m_WeGotPauseChange = true;
   1.179 +#endif
   1.180 +	SDK_OnPauseChange(state);
   1.181 +}
   1.182 +
   1.183 +void SDKExtension::OnExtensionsAllLoaded()
   1.184 +{
   1.185 +	SDK_OnAllLoaded();
   1.186 +}
   1.187 +
   1.188 +void SDKExtension::OnExtensionUnload()
   1.189 +{
   1.190 +#if defined SMEXT_CONF_METAMOD
   1.191 +	m_WeAreUnloaded = true;
   1.192 +#endif
   1.193 +	SDK_OnUnload();
   1.194 +}
   1.195 +
   1.196 +const char *SDKExtension::GetExtensionAuthor()
   1.197 +{
   1.198 +	return SMEXT_CONF_AUTHOR;
   1.199 +}
   1.200 +
   1.201 +const char *SDKExtension::GetExtensionDateString()
   1.202 +{
   1.203 +	return SMEXT_CONF_DATESTRING;
   1.204 +}
   1.205 +
   1.206 +const char *SDKExtension::GetExtensionDescription()
   1.207 +{
   1.208 +	return SMEXT_CONF_DESCRIPTION;
   1.209 +}
   1.210 +
   1.211 +const char *SDKExtension::GetExtensionVerString()
   1.212 +{
   1.213 +	return SMEXT_CONF_VERSION;
   1.214 +}
   1.215 +
   1.216 +const char *SDKExtension::GetExtensionName()
   1.217 +{
   1.218 +	return SMEXT_CONF_NAME;
   1.219 +}
   1.220 +
   1.221 +const char *SDKExtension::GetExtensionTag()
   1.222 +{
   1.223 +	return SMEXT_CONF_LOGTAG;
   1.224 +}
   1.225 +
   1.226 +const char *SDKExtension::GetExtensionURL()
   1.227 +{
   1.228 +	return SMEXT_CONF_URL;
   1.229 +}
   1.230 +
   1.231 +bool SDKExtension::SDK_OnLoad(char *error, size_t maxlength, bool late)
   1.232 +{
   1.233 +	return true;
   1.234 +}
   1.235 +
   1.236 +void SDKExtension::SDK_OnUnload()
   1.237 +{
   1.238 +}
   1.239 +
   1.240 +void SDKExtension::SDK_OnPauseChange(bool paused)
   1.241 +{
   1.242 +}
   1.243 +
   1.244 +void SDKExtension::SDK_OnAllLoaded()
   1.245 +{
   1.246 +}
   1.247 +
   1.248 +#if defined SMEXT_CONF_METAMOD
   1.249 +
   1.250 +PluginId g_PLID = 0;						/**< Metamod plugin ID */
   1.251 +ISmmPlugin *g_PLAPI = NULL;					/**< Metamod plugin API */
   1.252 +SourceHook::ISourceHook *g_SHPtr = NULL;	/**< SourceHook pointer */
   1.253 +ISmmAPI *g_SMAPI = NULL;					/**< SourceMM API pointer */
   1.254 +
   1.255 +IVEngineServer *engine = NULL;				/**< IVEngineServer pointer */
   1.256 +IServerGameDLL *gamedll = NULL;				/**< IServerGameDLL pointer */
   1.257 +
   1.258 +/** Exposes the extension to Metamod */
   1.259 +SMM_API void *PL_EXPOSURE(const char *name, int *code)
   1.260 +{
   1.261 +	if (name && !strcmp(name, PLAPI_NAME))
   1.262 +	{
   1.263 +		if (code)
   1.264 +		{
   1.265 +			*code = IFACE_OK;
   1.266 +		}
   1.267 +		return static_cast<void *>(g_pExtensionIface);
   1.268 +	}
   1.269 +
   1.270 +	if (code)
   1.271 +	{
   1.272 +		*code = IFACE_FAILED;
   1.273 +	}
   1.274 +
   1.275 +	return NULL;
   1.276 +}
   1.277 +
   1.278 +bool SDKExtension::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
   1.279 +{
   1.280 +	PLUGIN_SAVEVARS();
   1.281 +
   1.282 +	GET_V_IFACE_ANY(serverFactory, gamedll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
   1.283 +	GET_V_IFACE_CURRENT(engineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
   1.284 +
   1.285 +	m_SourceMMLoaded = true;
   1.286 +
   1.287 +	return SDK_OnMetamodLoad(ismm, error, maxlen, late);
   1.288 +}
   1.289 +
   1.290 +bool SDKExtension::Unload(char *error, size_t maxlen)
   1.291 +{
   1.292 +	if (!m_WeAreUnloaded)
   1.293 +	{
   1.294 +		if (error)
   1.295 +		{
   1.296 +			snprintf(error, maxlen, "This extension must be unloaded by SourceMod.");
   1.297 +		}
   1.298 +		return false;
   1.299 +	}
   1.300 +
   1.301 +	return SDK_OnMetamodUnload(error, maxlen);
   1.302 +}
   1.303 +
   1.304 +bool SDKExtension::Pause(char *error, size_t maxlen)
   1.305 +{
   1.306 +	if (!m_WeGotPauseChange)
   1.307 +	{
   1.308 +		if (error)
   1.309 +		{
   1.310 +			snprintf(error, maxlen, "This extension must be paused by SourceMod.");
   1.311 +		}
   1.312 +		return false;
   1.313 +	}
   1.314 +
   1.315 +	m_WeGotPauseChange = false;
   1.316 +
   1.317 +	return SDK_OnMetamodPauseChange(true, error, maxlen);
   1.318 +}
   1.319 +
   1.320 +bool SDKExtension::Unpause(char *error, size_t maxlen)
   1.321 +{
   1.322 +	if (!m_WeGotPauseChange)
   1.323 +	{
   1.324 +		if (error)
   1.325 +		{
   1.326 +			snprintf(error, maxlen, "This extension must be unpaused by SourceMod.");
   1.327 +		}
   1.328 +		return false;
   1.329 +	}
   1.330 +
   1.331 +	m_WeGotPauseChange = false;
   1.332 +
   1.333 +	return SDK_OnMetamodPauseChange(false, error, maxlen);
   1.334 +}
   1.335 +
   1.336 +const char *SDKExtension::GetAuthor()
   1.337 +{
   1.338 +	return GetExtensionAuthor();
   1.339 +}
   1.340 +
   1.341 +const char *SDKExtension::GetDate()
   1.342 +{
   1.343 +	return GetExtensionDateString();
   1.344 +}
   1.345 +
   1.346 +const char *SDKExtension::GetDescription()
   1.347 +{
   1.348 +	return GetExtensionDescription();
   1.349 +}
   1.350 +
   1.351 +const char *SDKExtension::GetLicense()
   1.352 +{
   1.353 +	return SMEXT_CONF_LICENSE;
   1.354 +}
   1.355 +
   1.356 +const char *SDKExtension::GetLogTag()
   1.357 +{
   1.358 +	return GetExtensionTag();
   1.359 +}
   1.360 +
   1.361 +const char *SDKExtension::GetName()
   1.362 +{
   1.363 +	return GetExtensionName();
   1.364 +}
   1.365 +
   1.366 +const char *SDKExtension::GetURL()
   1.367 +{
   1.368 +	return GetExtensionURL();
   1.369 +}
   1.370 +
   1.371 +const char *SDKExtension::GetVersion()
   1.372 +{
   1.373 +	return GetExtensionVerString();
   1.374 +}
   1.375 +
   1.376 +bool SDKExtension::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlength, bool late)
   1.377 +{
   1.378 +	return true;
   1.379 +}
   1.380 +
   1.381 +bool SDKExtension::SDK_OnMetamodUnload(char *error, size_t maxlength)
   1.382 +{
   1.383 +	return true;
   1.384 +}
   1.385 +
   1.386 +bool SDKExtension::SDK_OnMetamodPauseChange(bool paused, char *error, size_t maxlength)
   1.387 +{
   1.388 +	return true;
   1.389 +}
   1.390 +
   1.391 +#endif
   1.392 +
   1.393 +/* Overload a few things to prevent libstdc++ linking */
   1.394 +#if defined __linux__
   1.395 +extern "C" void __cxa_pure_virtual(void)
   1.396 +{
   1.397 +}
   1.398 +
   1.399 +void *operator new(size_t size)
   1.400 +{
   1.401 +	return malloc(size);
   1.402 +}
   1.403 +
   1.404 +void *operator new[](size_t size) 
   1.405 +{
   1.406 +	return malloc(size);
   1.407 +}
   1.408 +
   1.409 +void operator delete(void *ptr) 
   1.410 +{
   1.411 +	free(ptr);
   1.412 +}
   1.413 +
   1.414 +void operator delete[](void * ptr)
   1.415 +{
   1.416 +	free(ptr);
   1.417 +}
   1.418 +#endif