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