extensions/bintools/sdk/smsdk_ext.cpp
author Matt Woodrow <pred@alliedmods.net>
Thu Jan 08 18:17:17 2009 +1300 (2009-01-08)
changeset 2504 74fea7db93e4
parent 2471 194be299b700
permissions -rw-r--r--
Added Dynamic Hooking to BinTools (bug 2616 r=dvander,DS)
     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 #if defined SMEXT_ENABLE_PLUGINSYS
   101 SourceMod::IPluginManager *plsys;
   102 #endif
   103 #if defined SMEXT_ENABLE_MENUS
   104 IMenuManager *menus = NULL;
   105 #endif
   106 #if defined SMEXT_ENABLE_ADMINSYS
   107 IAdminSystem *adminsys = NULL;
   108 #endif
   109 #if defined SMEXT_ENABLE_TEXTPARSERS
   110 ITextParsers *textparsers = NULL;
   111 #endif
   112 #if defined SMEXT_ENABLE_USERMSGS
   113 IUserMessages *usermsgs = NULL;
   114 #endif
   115 #if defined SMEXT_ENABLE_TRANSLATOR
   116 ITranslator *translator = NULL;
   117 #endif
   118 
   119 /** Exports the main interface */
   120 PLATFORM_EXTERN_C IExtensionInterface *GetSMExtAPI()
   121 {
   122 	return g_pExtensionIface;
   123 }
   124 
   125 SDKExtension::SDKExtension()
   126 {
   127 #if defined SMEXT_CONF_METAMOD
   128 	m_SourceMMLoaded = false;
   129 	m_WeAreUnloaded = false;
   130 	m_WeGotPauseChange = false;
   131 #endif
   132 }
   133 
   134 bool SDKExtension::OnExtensionLoad(IExtension *me, IShareSys *sys, char *error, size_t maxlength, bool late)
   135 {
   136 	g_pShareSys = sharesys = sys;
   137 	myself = me;
   138 
   139 #if defined SMEXT_CONF_METAMOD
   140 	m_WeAreUnloaded = true;
   141 
   142 	if (!m_SourceMMLoaded)
   143 	{
   144 		if (error)
   145 		{
   146 			snprintf(error, maxlength, "Metamod attach failed");
   147 		}
   148 		return false;
   149 	}
   150 #endif
   151 	SM_GET_IFACE(SOURCEMOD, g_pSM);
   152 	smutils = g_pSM;
   153 #if defined SMEXT_ENABLE_HANDLESYS
   154 	SM_GET_IFACE(HANDLESYSTEM, g_pHandleSys);
   155 	handlesys = g_pHandleSys;
   156 #endif
   157 #if defined SMEXT_ENABLE_FORWARDSYS
   158 	SM_GET_IFACE(FORWARDMANAGER, g_pForwards);
   159 	forwards = g_pForwards;
   160 #endif
   161 #if defined SMEXT_ENABLE_PLAYERHELPERS
   162 	SM_GET_IFACE(PLAYERMANAGER, playerhelpers);
   163 #endif
   164 #if defined SMEXT_ENABLE_DBMANAGER
   165 	SM_GET_IFACE(DBI, dbi);
   166 #endif
   167 #if defined SMEXT_ENABLE_GAMECONF
   168 	SM_GET_IFACE(GAMECONFIG, gameconfs);
   169 #endif
   170 #if defined SMEXT_ENABLE_MEMUTILS
   171 	SM_GET_IFACE(MEMORYUTILS, memutils);
   172 #endif
   173 #if defined SMEXT_ENABLE_GAMEHELPERS
   174 	SM_GET_IFACE(GAMEHELPERS, gamehelpers);
   175 #endif
   176 #if defined SMEXT_ENABLE_TIMERSYS
   177 	SM_GET_IFACE(TIMERSYS, timersys);
   178 #endif
   179 #if defined SMEXT_ENABLE_ADTFACTORY
   180 	SM_GET_IFACE(ADTFACTORY, adtfactory);
   181 #endif
   182 #if defined SMEXT_ENABLE_THREADER
   183 	SM_GET_IFACE(THREADER, threader);
   184 #endif
   185 #if defined SMEXT_ENABLE_LIBSYS
   186 	SM_GET_IFACE(LIBRARYSYS, libsys);
   187 #endif
   188 #if defined SMEXT_ENABLE_PLUGINSYS
   189 	SM_GET_IFACE(PLUGINSYSTEM, plsys);
   190 #endif
   191 #if defined SMEXT_ENABLE_MENUS
   192 	SM_GET_IFACE(MENUMANAGER, menus);
   193 #endif
   194 #if defined SMEXT_ENABLE_ADMINSYS
   195 	SM_GET_IFACE(ADMINSYS, adminsys);
   196 #endif
   197 #if defined SMEXT_ENABLE_TEXTPARSERS
   198 	SM_GET_IFACE(TEXTPARSERS, textparsers);
   199 #endif
   200 #if defined SMEXT_ENABLE_USERMSGS
   201 	SM_GET_IFACE(USERMSGS, usermsgs);
   202 #endif
   203 #if defined SMEXT_ENABLE_TRANSLATOR
   204 	SM_GET_IFACE(TRANSLATOR, translator);
   205 #endif
   206 #if defined SMEXT_ENABLE_PLUGINSYS
   207 	SM_GET_IFACE(PLUGINSYSTEM, plsys);
   208 #endif
   209 #if defined SMEXT_ENABLE_MENUS
   210 	SM_GET_IFACE(MENUMANAGER, menus);
   211 #endif
   212 #if defined SMEXT_ENABLE_ADMINSYS
   213 	SM_GET_IFACE(ADMINSYS, adminsys);
   214 #endif
   215 #if defined SMEXT_ENABLE_TEXTPARSERS
   216 	SM_GET_IFACE(TEXTPARSERS, textparsers);
   217 #endif
   218 #if defined SMEXT_ENABLE_USERMSGS
   219 	SM_GET_IFACE(USERMSGS, usermsgs);
   220 #endif
   221 #if defined SMEXT_ENABLE_TRANSLATOR
   222 	SM_GET_IFACE(TRANSLATOR, translator);
   223 #endif
   224 
   225 	if (SDK_OnLoad(error, maxlength, late))
   226 	{
   227 #if defined SMEXT_CONF_METAMOD
   228 		m_WeAreUnloaded = true;
   229 #endif
   230 		return true;
   231 	}
   232 
   233 	return false;
   234 }
   235 
   236 bool SDKExtension::IsMetamodExtension()
   237 {
   238 #if defined SMEXT_CONF_METAMOD
   239 	return true;
   240 #else
   241 	return false;
   242 #endif
   243 }
   244 
   245 void SDKExtension::OnExtensionPauseChange(bool state)
   246 {
   247 #if defined SMEXT_CONF_METAMOD
   248 	m_WeGotPauseChange = true;
   249 #endif
   250 	SDK_OnPauseChange(state);
   251 }
   252 
   253 void SDKExtension::OnExtensionsAllLoaded()
   254 {
   255 	SDK_OnAllLoaded();
   256 }
   257 
   258 void SDKExtension::OnExtensionUnload()
   259 {
   260 #if defined SMEXT_CONF_METAMOD
   261 	m_WeAreUnloaded = true;
   262 #endif
   263 	SDK_OnUnload();
   264 }
   265 
   266 const char *SDKExtension::GetExtensionAuthor()
   267 {
   268 	return SMEXT_CONF_AUTHOR;
   269 }
   270 
   271 const char *SDKExtension::GetExtensionDateString()
   272 {
   273 	return SMEXT_CONF_DATESTRING;
   274 }
   275 
   276 const char *SDKExtension::GetExtensionDescription()
   277 {
   278 	return SMEXT_CONF_DESCRIPTION;
   279 }
   280 
   281 const char *SDKExtension::GetExtensionVerString()
   282 {
   283 	return SMEXT_CONF_VERSION;
   284 }
   285 
   286 const char *SDKExtension::GetExtensionName()
   287 {
   288 	return SMEXT_CONF_NAME;
   289 }
   290 
   291 const char *SDKExtension::GetExtensionTag()
   292 {
   293 	return SMEXT_CONF_LOGTAG;
   294 }
   295 
   296 const char *SDKExtension::GetExtensionURL()
   297 {
   298 	return SMEXT_CONF_URL;
   299 }
   300 
   301 bool SDKExtension::SDK_OnLoad(char *error, size_t maxlength, bool late)
   302 {
   303 	return true;
   304 }
   305 
   306 void SDKExtension::SDK_OnUnload()
   307 {
   308 }
   309 
   310 void SDKExtension::SDK_OnPauseChange(bool paused)
   311 {
   312 }
   313 
   314 void SDKExtension::SDK_OnAllLoaded()
   315 {
   316 }
   317 
   318 #if defined SMEXT_CONF_METAMOD
   319 
   320 PluginId g_PLID = 0;						/**< Metamod plugin ID */
   321 ISmmPlugin *g_PLAPI = NULL;					/**< Metamod plugin API */
   322 SourceHook::ISourceHook *g_SHPtr = NULL;	/**< SourceHook pointer */
   323 ISmmAPI *g_SMAPI = NULL;					/**< SourceMM API pointer */
   324 
   325 IVEngineServer *engine = NULL;				/**< IVEngineServer pointer */
   326 IServerGameDLL *gamedll = NULL;				/**< IServerGameDLL pointer */
   327 
   328 /** Exposes the extension to Metamod */
   329 SMM_API void *PL_EXPOSURE(const char *name, int *code)
   330 {
   331 #if defined METAMOD_PLAPI_VERSION
   332 	if (name && !strcmp(name, METAMOD_PLAPI_NAME))
   333 #else
   334 	if (name && !strcmp(name, PLAPI_NAME))
   335 #endif
   336 	{
   337 		if (code)
   338 		{
   339 			*code = IFACE_OK;
   340 		}
   341 		return static_cast<void *>(g_pExtensionIface);
   342 	}
   343 
   344 	if (code)
   345 	{
   346 		*code = IFACE_FAILED;
   347 	}
   348 
   349 	return NULL;
   350 }
   351 
   352 bool SDKExtension::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
   353 {
   354 	PLUGIN_SAVEVARS();
   355 
   356 #if !defined METAMOD_PLAPI_VERSION
   357 	GET_V_IFACE_ANY(serverFactory, gamedll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
   358 	GET_V_IFACE_CURRENT(engineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
   359 #else
   360 	GET_V_IFACE_ANY(GetServerFactory, gamedll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
   361 	GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
   362 #endif
   363 
   364 	m_SourceMMLoaded = true;
   365 
   366 	return SDK_OnMetamodLoad(ismm, error, maxlen, late);
   367 }
   368 
   369 bool SDKExtension::Unload(char *error, size_t maxlen)
   370 {
   371 	if (!m_WeAreUnloaded)
   372 	{
   373 		if (error)
   374 		{
   375 			snprintf(error, maxlen, "This extension must be unloaded by SourceMod.");
   376 		}
   377 		return false;
   378 	}
   379 
   380 	return SDK_OnMetamodUnload(error, maxlen);
   381 }
   382 
   383 bool SDKExtension::Pause(char *error, size_t maxlen)
   384 {
   385 	if (!m_WeGotPauseChange)
   386 	{
   387 		if (error)
   388 		{
   389 			snprintf(error, maxlen, "This extension must be paused by SourceMod.");
   390 		}
   391 		return false;
   392 	}
   393 
   394 	m_WeGotPauseChange = false;
   395 
   396 	return SDK_OnMetamodPauseChange(true, error, maxlen);
   397 }
   398 
   399 bool SDKExtension::Unpause(char *error, size_t maxlen)
   400 {
   401 	if (!m_WeGotPauseChange)
   402 	{
   403 		if (error)
   404 		{
   405 			snprintf(error, maxlen, "This extension must be unpaused by SourceMod.");
   406 		}
   407 		return false;
   408 	}
   409 
   410 	m_WeGotPauseChange = false;
   411 
   412 	return SDK_OnMetamodPauseChange(false, error, maxlen);
   413 }
   414 
   415 const char *SDKExtension::GetAuthor()
   416 {
   417 	return GetExtensionAuthor();
   418 }
   419 
   420 const char *SDKExtension::GetDate()
   421 {
   422 	return GetExtensionDateString();
   423 }
   424 
   425 const char *SDKExtension::GetDescription()
   426 {
   427 	return GetExtensionDescription();
   428 }
   429 
   430 const char *SDKExtension::GetLicense()
   431 {
   432 	return SMEXT_CONF_LICENSE;
   433 }
   434 
   435 const char *SDKExtension::GetLogTag()
   436 {
   437 	return GetExtensionTag();
   438 }
   439 
   440 const char *SDKExtension::GetName()
   441 {
   442 	return GetExtensionName();
   443 }
   444 
   445 const char *SDKExtension::GetURL()
   446 {
   447 	return GetExtensionURL();
   448 }
   449 
   450 const char *SDKExtension::GetVersion()
   451 {
   452 	return GetExtensionVerString();
   453 }
   454 
   455 bool SDKExtension::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlength, bool late)
   456 {
   457 	return true;
   458 }
   459 
   460 bool SDKExtension::SDK_OnMetamodUnload(char *error, size_t maxlength)
   461 {
   462 	return true;
   463 }
   464 
   465 bool SDKExtension::SDK_OnMetamodPauseChange(bool paused, char *error, size_t maxlength)
   466 {
   467 	return true;
   468 }
   469 
   470 #endif
   471 
   472 /* Overload a few things to prevent libstdc++ linking */
   473 #if defined __linux__ || defined __APPLE__
   474 extern "C" void __cxa_pure_virtual(void)
   475 {
   476 }
   477 
   478 void *operator new(size_t size)
   479 {
   480 	return malloc(size);
   481 }
   482 
   483 void *operator new[](size_t size) 
   484 {
   485 	return malloc(size);
   486 }
   487 
   488 void operator delete(void *ptr) 
   489 {
   490 	free(ptr);
   491 }
   492 
   493 void operator delete[](void * ptr)
   494 {
   495 	free(ptr);
   496 }
   497 #endif
   498