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