core/sourcemm_api.cpp
author Matt Woodrow <pred@alliedmods.net>
Tue Mar 11 23:52:56 2008 +0000 (2008-03-11)
changeset 1921 f993e100e5a3
parent 1668 e1700463d1d8
child 1947 e73a1e51833b
permissions -rw-r--r--
Fixed bug in GetAdjustedTime()
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 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 "sourcemod.h"
    33 #include "sourcemm_api.h"
    34 #include "sm_version.h"
    35 #include "Logger.h"
    36 #include "ExtensionSys.h"
    37 #include "concmd_cleaner.h"
    38 #include "compat_wrappers.h"
    39 
    40 SourceMod_Core g_SourceMod_Core;
    41 IVEngineServer *engine = NULL;
    42 IServerGameDLL *gamedll = NULL;
    43 IServerGameClients *serverClients = NULL;
    44 ISmmPluginManager *g_pMMPlugins = NULL;
    45 CGlobalVars *gpGlobals = NULL;
    46 ICvar *icvar = NULL;
    47 IGameEventManager2 *gameevents = NULL;
    48 IUniformRandomStream *engrandom = NULL;
    49 CallClass<IVEngineServer> *enginePatch = NULL;
    50 CallClass<IServerGameDLL> *gamedllPatch = NULL;
    51 IPlayerInfoManager *playerinfo = NULL;
    52 IBaseFileSystem *basefilesystem = NULL;
    53 IEngineSound *enginesound = NULL;
    54 IServerPluginHelpers *serverpluginhelpers = NULL;
    55 IServerPluginCallbacks *vsp_interface = NULL;
    56 int vsp_version = 0;
    57 
    58 PLUGIN_EXPOSE(SourceMod, g_SourceMod_Core);
    59 
    60 bool SourceMod_Core::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
    61 {
    62 	PLUGIN_SAVEVARS();
    63 
    64 	GET_V_IFACE_ANY(GetServerFactory, gamedll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
    65 	GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
    66 	GET_V_IFACE_CURRENT(GetServerFactory, serverClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
    67 	GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
    68 	GET_V_IFACE_CURRENT(GetEngineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
    69 	GET_V_IFACE_CURRENT(GetEngineFactory, engrandom, IUniformRandomStream, VENGINE_SERVER_RANDOM_INTERFACE_VERSION);
    70 	GET_V_IFACE_CURRENT(GetFileSystemFactory, basefilesystem, IBaseFileSystem, BASEFILESYSTEM_INTERFACE_VERSION);
    71 	GET_V_IFACE_CURRENT(GetEngineFactory, enginesound, IEngineSound, IENGINESOUND_SERVER_INTERFACE_VERSION);
    72 	GET_V_IFACE_CURRENT(GetEngineFactory, serverpluginhelpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
    73 
    74 	/* :TODO: Make this optional and... make it find earlier versions [?] */
    75 	GET_V_IFACE_CURRENT(GetServerFactory, playerinfo, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
    76 
    77 	if ((g_pMMPlugins = (ISmmPluginManager *)g_SMAPI->MetaFactory(MMIFACE_PLMANAGER, NULL, NULL)) == NULL)
    78 	{
    79 		if (error)
    80 		{
    81 			snprintf(error, maxlen, "Unable to find interface %s", MMIFACE_PLMANAGER);
    82 		}
    83 		return false;
    84 	}
    85 
    86 	gpGlobals = ismm->GetCGlobals();
    87 	
    88 	ismm->AddListener(this, this);
    89 
    90 #if defined METAMOD_PLAPI_VERSION
    91 	if ((vsp_interface = g_SMAPI->GetVSPInfo(&vsp_version)) == NULL)
    92 #endif
    93 	{
    94 		g_SMAPI->EnableVSPListener();
    95 	}
    96 
    97 	return g_SourceMod.InitializeSourceMod(error, maxlen, late);
    98 }
    99 
   100 bool SourceMod_Core::Unload(char *error, size_t maxlen)
   101 {
   102 	g_SourceMod.CloseSourceMod();
   103 	return true;
   104 }
   105 
   106 bool SourceMod_Core::Pause(char *error, size_t maxlen)
   107 {
   108 	return true;
   109 }
   110 
   111 bool SourceMod_Core::Unpause(char *error, size_t maxlen)
   112 {
   113 	return true;
   114 }
   115 
   116 void SourceMod_Core::AllPluginsLoaded()
   117 {
   118 	g_SourceMod.AllPluginsLoaded();
   119 }
   120 
   121 const char *SourceMod_Core::GetAuthor()
   122 {
   123 	return "AlliedModders LLC";
   124 }
   125 
   126 const char *SourceMod_Core::GetName()
   127 {
   128 	return "SourceMod";
   129 }
   130 
   131 const char *SourceMod_Core::GetDescription()
   132 {
   133 	return "Extensible administration and scripting system";
   134 }
   135 
   136 const char *SourceMod_Core::GetURL()
   137 {
   138 	return "http://www.sourcemod.net/";
   139 }
   140 
   141 const char *SourceMod_Core::GetLicense()
   142 {
   143 	return "See LICENSE.txt";
   144 }
   145 
   146 const char *SourceMod_Core::GetVersion()
   147 {
   148 	return SVN_FULL_VERSION;
   149 }
   150 
   151 const char *SourceMod_Core::GetDate()
   152 {
   153 	return __DATE__;
   154 }
   155 
   156 const char *SourceMod_Core::GetLogTag()
   157 {
   158 	return "SM";
   159 }
   160 
   161 void SourceMod_Core::OnVSPListening(IServerPluginCallbacks *iface)
   162 {
   163 	/* This shouldn't happen */
   164 	if (!iface)
   165 	{
   166 		g_Logger.LogFatal("Metamod:Source version is out of date. SourceMod requires 1.4.2 or greater.");
   167 		return;
   168 	}
   169 
   170 	if (vsp_interface == NULL)
   171 	{
   172 		vsp_interface = iface;
   173 	}
   174 
   175 	if (!g_Loaded)
   176 	{
   177 		return;
   178 	}
   179 
   180 #if defined METAMOD_PLAPI_VERSION
   181 	if (vsp_version == 0)
   182 	{
   183 		g_SMAPI->GetVSPInfo(&vsp_version);
   184 	}
   185 #else
   186 	if (vsp_version == 0)
   187 	{
   188 		if (strcmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
   189 		{
   190 			vsp_version = 1;
   191 		}
   192 		else
   193 		{
   194 			vsp_version = 2;
   195 		}
   196 	}
   197 #endif
   198 
   199 	/* Notify! */
   200 	SMGlobalClass *pBase = SMGlobalClass::head;
   201 	while (pBase)
   202 	{
   203 		pBase->OnSourceModVSPReceived();
   204 		pBase = pBase->m_pGlobalClassNext;
   205 	}
   206 }
   207 
   208 #if defined METAMOD_PLAPI_VERSION
   209 
   210 void SourceMod_Core::OnUnlinkConCommandBase(PluginId id, ConCommandBase *pCommand)
   211 {
   212 	Global_OnUnlinkConCommandBase(pCommand);
   213 }
   214 
   215 #else
   216 
   217 void SourceMod_Core::OnPluginUnload(PluginId id)
   218 {
   219 	Global_OnUnlinkConCommandBase(NULL);
   220 }
   221 
   222 #endif
   223 
   224 void *SourceMod_Core::OnMetamodQuery(const char *iface, int *ret)
   225 {
   226 	void *ptr = NULL;
   227 
   228 	if (strcmp(iface, SOURCEMOD_INTERFACE_EXTENSIONS) == 0)
   229 	{
   230 		ptr = (IExtensionManager *)&g_Extensions;
   231 	}
   232 
   233 	if (ret != NULL)
   234 	{
   235 		*ret = (ptr == NULL) ? IFACE_FAILED : IFACE_OK;
   236 	}
   237 
   238 	return ptr;
   239 }
   240