• Main Page
  • Related Pages
  • Classes
  • Files
  • File List
  • File Members

public/sample_ext/sdk/smsdk_ext.cpp

Go to the documentation of this file.
00001 /**
00002  * vim: set ts=4 :
00003  * =============================================================================
00004  * SourceMod Base Extension Code
00005  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
00006  * =============================================================================
00007  *
00008  * This program is free software; you can redistribute it and/or modify it under
00009  * the terms of the GNU General Public License, version 3.0, as published by the
00010  * Free Software Foundation.
00011  * 
00012  * This program is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00015  * details.
00016  *
00017  * You should have received a copy of the GNU General Public License along with
00018  * this program.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  * As a special exception, AlliedModders LLC gives you permission to link the
00021  * code of this program (as well as its derivative works) to "Half-Life 2," the
00022  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
00023  * by the Valve Corporation.  You must obey the GNU General Public License in
00024  * all respects for all other code used.  Additionally, AlliedModders LLC grants
00025  * this exception to all derivative works.  AlliedModders LLC defines further
00026  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
00027  * or <http://www.sourcemod.net/license.php>.
00028  *
00029  * Version: $Id$
00030  */
00031 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include "smsdk_ext.h"
00035 
00036 /**
00037  * @file smsdk_ext.cpp
00038  * @brief Contains wrappers for making Extensions easier to write.
00039  */
00040 
00041 IExtension *myself = NULL;                                  /**< Ourself */
00042 IShareSys *g_pShareSys = NULL;                              /**< Share system */
00043 IShareSys *sharesys = NULL;                                 /**< Share system */
00044 ISourceMod *g_pSM = NULL;                                   /**< SourceMod helpers */
00045 ISourceMod *smutils = NULL;                                 /**< SourceMod helpers */
00046 
00047 #if defined SMEXT_ENABLE_FORWARDSYS
00048 IForwardManager *g_pForwards = NULL;    /**< Forward system */
00049 IForwardManager *forwards = NULL;                 /**< Forward system */
00050 #endif
00051 #if defined SMEXT_ENABLE_HANDLESYS
00052 IHandleSys *g_pHandleSys = NULL;                  /**< Handle system */
00053 IHandleSys *handlesys = NULL;                     /**< Handle system */
00054 #endif
00055 #if defined SMEXT_ENABLE_PLAYERHELPERS
00056 IPlayerManager *playerhelpers = NULL;   /**< Player helpers */
00057 #endif //SMEXT_ENABLE_PLAYERHELPERS
00058 #if defined SMEXT_ENABLE_DBMANAGER
00059 IDBManager *dbi = NULL;                                               /**< DB Manager */
00060 #endif //SMEXT_ENABLE_DBMANAGER
00061 #if defined SMEXT_ENABLE_GAMECONF
00062 IGameConfigManager *gameconfs = NULL;   /**< Game config manager */
00063 #endif //SMEXT_ENABLE_DBMANAGER
00064 #if defined SMEXT_ENABLE_MEMUTILS
00065 IMemoryUtils *memutils = NULL;
00066 #endif //SMEXT_ENABLE_DBMANAGER
00067 #if defined SMEXT_ENABLE_GAMEHELPERS
00068 IGameHelpers *gamehelpers = NULL;
00069 #endif
00070 #if defined SMEXT_ENABLE_TIMERSYS
00071 ITimerSystem *timersys = NULL;
00072 #endif
00073 #if defined SMEXT_ENABLE_ADTFACTORY
00074 IADTFactory *adtfactory = NULL;
00075 #endif
00076 #if defined SMEXT_ENABLE_THREADER
00077 IThreader *threader = NULL;
00078 #endif
00079 #if defined SMEXT_ENABLE_LIBSYS
00080 ILibrarySys *libsys = NULL;
00081 #endif
00082 #if defined SMEXT_ENABLE_PLUGINSYS
00083 SourceMod::IPluginManager *plsys;
00084 #endif
00085 #if defined SMEXT_ENABLE_MENUS
00086 IMenuManager *menus = NULL;
00087 #endif
00088 #if defined SMEXT_ENABLE_ADMINSYS
00089 IAdminSystem *adminsys = NULL;
00090 #endif
00091 #if defined SMEXT_ENABLE_TEXTPARSERS
00092 ITextParsers *textparsers = NULL;
00093 #endif
00094 #if defined SMEXT_ENABLE_USERMSGS
00095 IUserMessages *usermsgs = NULL;
00096 #endif
00097 #if defined SMEXT_ENABLE_TRANSLATOR
00098 ITranslator *translator = NULL;
00099 #endif
00100 #if defined SMEXT_ENABLE_NINVOKE
00101 INativeInterface *ninvoke = NULL;
00102 #endif
00103 #if defined SMEXT_ENABLE_ROOTCONSOLEMENU
00104 IRootConsole *rootconsole = NULL;
00105 #endif
00106 
00107 /** Exports the main interface */
00108 PLATFORM_EXTERN_C IExtensionInterface *GetSMExtAPI()
00109 {
00110           return g_pExtensionIface;
00111 }
00112 
00113 SDKExtension::SDKExtension()
00114 {
00115 #if defined SMEXT_CONF_METAMOD
00116           m_SourceMMLoaded = false;
00117           m_WeAreUnloaded = false;
00118           m_WeGotPauseChange = false;
00119 #endif
00120 }
00121 
00122 bool SDKExtension::OnExtensionLoad(IExtension *me, IShareSys *sys, char *error, size_t maxlength, bool late)
00123 {
00124           g_pShareSys = sharesys = sys;
00125           myself = me;
00126 
00127 #if defined SMEXT_CONF_METAMOD
00128           m_WeAreUnloaded = true;
00129 
00130           if (!m_SourceMMLoaded)
00131           {
00132                     if (error)
00133                     {
00134                               snprintf(error, maxlength, "Metamod attach failed");
00135                     }
00136                     return false;
00137           }
00138 #endif
00139           SM_GET_IFACE(SOURCEMOD, g_pSM);
00140           smutils = g_pSM;
00141 #if defined SMEXT_ENABLE_HANDLESYS
00142           SM_GET_IFACE(HANDLESYSTEM, g_pHandleSys);
00143           handlesys = g_pHandleSys;
00144 #endif
00145 #if defined SMEXT_ENABLE_FORWARDSYS
00146           SM_GET_IFACE(FORWARDMANAGER, g_pForwards);
00147           forwards = g_pForwards;
00148 #endif
00149 #if defined SMEXT_ENABLE_PLAYERHELPERS
00150           SM_GET_IFACE(PLAYERMANAGER, playerhelpers);
00151 #endif
00152 #if defined SMEXT_ENABLE_DBMANAGER
00153           SM_GET_IFACE(DBI, dbi);
00154 #endif
00155 #if defined SMEXT_ENABLE_GAMECONF
00156           SM_GET_IFACE(GAMECONFIG, gameconfs);
00157 #endif
00158 #if defined SMEXT_ENABLE_MEMUTILS
00159           SM_GET_IFACE(MEMORYUTILS, memutils);
00160 #endif
00161 #if defined SMEXT_ENABLE_GAMEHELPERS
00162           SM_GET_IFACE(GAMEHELPERS, gamehelpers);
00163 #endif
00164 #if defined SMEXT_ENABLE_TIMERSYS
00165           SM_GET_IFACE(TIMERSYS, timersys);
00166 #endif
00167 #if defined SMEXT_ENABLE_ADTFACTORY
00168           SM_GET_IFACE(ADTFACTORY, adtfactory);
00169 #endif
00170 #if defined SMEXT_ENABLE_THREADER
00171           SM_GET_IFACE(THREADER, threader);
00172 #endif
00173 #if defined SMEXT_ENABLE_LIBSYS
00174           SM_GET_IFACE(LIBRARYSYS, libsys);
00175 #endif
00176 #if defined SMEXT_ENABLE_PLUGINSYS
00177           SM_GET_IFACE(PLUGINSYSTEM, plsys);
00178 #endif
00179 #if defined SMEXT_ENABLE_MENUS
00180           SM_GET_IFACE(MENUMANAGER, menus);
00181 #endif
00182 #if defined SMEXT_ENABLE_ADMINSYS
00183           SM_GET_IFACE(ADMINSYS, adminsys);
00184 #endif
00185 #if defined SMEXT_ENABLE_TEXTPARSERS
00186           SM_GET_IFACE(TEXTPARSERS, textparsers);
00187 #endif
00188 #if defined SMEXT_ENABLE_USERMSGS
00189           SM_GET_IFACE(USERMSGS, usermsgs);
00190 #endif
00191 #if defined SMEXT_ENABLE_TRANSLATOR
00192           SM_GET_IFACE(TRANSLATOR, translator);
00193 #endif
00194 #if defined SMEXT_ENABLE_ROOTCONSOLEMENU
00195           SM_GET_IFACE(ROOTCONSOLE, rootconsole);
00196 #endif
00197 
00198           if (SDK_OnLoad(error, maxlength, late))
00199           {
00200 #if defined SMEXT_CONF_METAMOD
00201                     m_WeAreUnloaded = true;
00202 #endif
00203                     return true;
00204           }
00205 
00206           return false;
00207 }
00208 
00209 bool SDKExtension::IsMetamodExtension()
00210 {
00211 #if defined SMEXT_CONF_METAMOD
00212           return true;
00213 #else
00214           return false;
00215 #endif
00216 }
00217 
00218 void SDKExtension::OnExtensionPauseChange(bool state)
00219 {
00220 #if defined SMEXT_CONF_METAMOD
00221           m_WeGotPauseChange = true;
00222 #endif
00223           SDK_OnPauseChange(state);
00224 }
00225 
00226 void SDKExtension::OnExtensionsAllLoaded()
00227 {
00228           SDK_OnAllLoaded();
00229 }
00230 
00231 void SDKExtension::OnExtensionUnload()
00232 {
00233 #if defined SMEXT_CONF_METAMOD
00234           m_WeAreUnloaded = true;
00235 #endif
00236           SDK_OnUnload();
00237 }
00238 
00239 const char *SDKExtension::GetExtensionAuthor()
00240 {
00241           return SMEXT_CONF_AUTHOR;
00242 }
00243 
00244 const char *SDKExtension::GetExtensionDateString()
00245 {
00246           return SMEXT_CONF_DATESTRING;
00247 }
00248 
00249 const char *SDKExtension::GetExtensionDescription()
00250 {
00251           return SMEXT_CONF_DESCRIPTION;
00252 }
00253 
00254 const char *SDKExtension::GetExtensionVerString()
00255 {
00256           return SMEXT_CONF_VERSION;
00257 }
00258 
00259 const char *SDKExtension::GetExtensionName()
00260 {
00261           return SMEXT_CONF_NAME;
00262 }
00263 
00264 const char *SDKExtension::GetExtensionTag()
00265 {
00266           return SMEXT_CONF_LOGTAG;
00267 }
00268 
00269 const char *SDKExtension::GetExtensionURL()
00270 {
00271           return SMEXT_CONF_URL;
00272 }
00273 
00274 bool SDKExtension::SDK_OnLoad(char *error, size_t maxlength, bool late)
00275 {
00276           return true;
00277 }
00278 
00279 void SDKExtension::SDK_OnUnload()
00280 {
00281 }
00282 
00283 void SDKExtension::SDK_OnPauseChange(bool paused)
00284 {
00285 }
00286 
00287 void SDKExtension::SDK_OnAllLoaded()
00288 {
00289 }
00290 
00291 #if defined SMEXT_CONF_METAMOD
00292 
00293 PluginId g_PLID = 0;                                                            /**< Metamod plugin ID */
00294 ISmmPlugin *g_PLAPI = NULL;                                           /**< Metamod plugin API */
00295 SourceHook::ISourceHook *g_SHPtr = NULL;          /**< SourceHook pointer */
00296 ISmmAPI *g_SMAPI = NULL;                                              /**< SourceMM API pointer */
00297 
00298 IVEngineServer *engine = NULL;                                        /**< IVEngineServer pointer */
00299 IServerGameDLL *gamedll = NULL;                                       /**< IServerGameDLL pointer */
00300 
00301 /** Exposes the extension to Metamod */
00302 SMM_API void *PL_EXPOSURE(const char *name, int *code)
00303 {
00304 #if defined METAMOD_PLAPI_VERSION
00305           if (name && !strcmp(name, METAMOD_PLAPI_NAME))
00306 #else
00307           if (name && !strcmp(name, PLAPI_NAME))
00308 #endif
00309           {
00310                     if (code)
00311                     {
00312                               *code = IFACE_OK;
00313                     }
00314                     return static_cast<void *>(g_pExtensionIface);
00315           }
00316 
00317           if (code)
00318           {
00319                     *code = IFACE_FAILED;
00320           }
00321 
00322           return NULL;
00323 }
00324 
00325 bool SDKExtension::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
00326 {
00327           PLUGIN_SAVEVARS();
00328 
00329 #if !defined METAMOD_PLAPI_VERSION
00330           GET_V_IFACE_ANY(serverFactory, gamedll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
00331           GET_V_IFACE_CURRENT(engineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
00332 #else
00333           GET_V_IFACE_ANY(GetServerFactory, gamedll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
00334           GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
00335 #endif
00336 
00337           m_SourceMMLoaded = true;
00338 
00339           return SDK_OnMetamodLoad(ismm, error, maxlen, late);
00340 }
00341 
00342 bool SDKExtension::Unload(char *error, size_t maxlen)
00343 {
00344           if (!m_WeAreUnloaded)
00345           {
00346                     if (error)
00347                     {
00348                               snprintf(error, maxlen, "This extension must be unloaded by SourceMod.");
00349                     }
00350                     return false;
00351           }
00352 
00353           return SDK_OnMetamodUnload(error, maxlen);
00354 }
00355 
00356 bool SDKExtension::Pause(char *error, size_t maxlen)
00357 {
00358           if (!m_WeGotPauseChange)
00359           {
00360                     if (error)
00361                     {
00362                               snprintf(error, maxlen, "This extension must be paused by SourceMod.");
00363                     }
00364                     return false;
00365           }
00366 
00367           m_WeGotPauseChange = false;
00368 
00369           return SDK_OnMetamodPauseChange(true, error, maxlen);
00370 }
00371 
00372 bool SDKExtension::Unpause(char *error, size_t maxlen)
00373 {
00374           if (!m_WeGotPauseChange)
00375           {
00376                     if (error)
00377                     {
00378                               snprintf(error, maxlen, "This extension must be unpaused by SourceMod.");
00379                     }
00380                     return false;
00381           }
00382 
00383           m_WeGotPauseChange = false;
00384 
00385           return SDK_OnMetamodPauseChange(false, error, maxlen);
00386 }
00387 
00388 const char *SDKExtension::GetAuthor()
00389 {
00390           return GetExtensionAuthor();
00391 }
00392 
00393 const char *SDKExtension::GetDate()
00394 {
00395           return GetExtensionDateString();
00396 }
00397 
00398 const char *SDKExtension::GetDescription()
00399 {
00400           return GetExtensionDescription();
00401 }
00402 
00403 const char *SDKExtension::GetLicense()
00404 {
00405           return SMEXT_CONF_LICENSE;
00406 }
00407 
00408 const char *SDKExtension::GetLogTag()
00409 {
00410           return GetExtensionTag();
00411 }
00412 
00413 const char *SDKExtension::GetName()
00414 {
00415           return GetExtensionName();
00416 }
00417 
00418 const char *SDKExtension::GetURL()
00419 {
00420           return GetExtensionURL();
00421 }
00422 
00423 const char *SDKExtension::GetVersion()
00424 {
00425           return GetExtensionVerString();
00426 }
00427 
00428 bool SDKExtension::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlength, bool late)
00429 {
00430           return true;
00431 }
00432 
00433 bool SDKExtension::SDK_OnMetamodUnload(char *error, size_t maxlength)
00434 {
00435           return true;
00436 }
00437 
00438 bool SDKExtension::SDK_OnMetamodPauseChange(bool paused, char *error, size_t maxlength)
00439 {
00440           return true;
00441 }
00442 
00443 #endif
00444 
00445 /* Overload a few things to prevent libstdc++ linking */
00446 #if defined __linux__ || defined __APPLE__
00447 extern "C" void __cxa_pure_virtual(void)
00448 {
00449 }
00450 
00451 void *operator new(size_t size)
00452 {
00453           return malloc(size);
00454 }
00455 
00456 void *operator new[](size_t size) 
00457 {
00458           return malloc(size);
00459 }
00460 
00461 void operator delete(void *ptr) 
00462 {
00463           free(ptr);
00464 }
00465 
00466 void operator delete[](void * ptr)
00467 {
00468           free(ptr);
00469 }
00470 #endif
00471 

Generated on Wed Dec 7 2011 18:50:03 for SourceMod SDK by  doxygen 1.7.1