core/sourcemm_api.cpp
author Borja Ferrer <faluco@alliedmods.net>
Fri Jul 04 19:04:33 2008 +0000 (2008-07-04)
changeset 2155 3f1fd7ab3d86
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
fixed amb1801, IsFakeClient() wasn't working for TF2 causing false results and crashes
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include "sourcemod.h"
ds@1952
    33
#include "sourcemm_api.h"
ds@1952
    34
#include "sm_version.h"
ds@1952
    35
#include "Logger.h"
ds@1952
    36
#include "ExtensionSys.h"
ds@1952
    37
#include "concmd_cleaner.h"
ds@1952
    38
#include "compat_wrappers.h"
ds@1952
    39
ds@1952
    40
SourceMod_Core g_SourceMod_Core;
ds@1952
    41
IVEngineServer *engine = NULL;
ds@1952
    42
IServerGameDLL *gamedll = NULL;
ds@1952
    43
IServerGameClients *serverClients = NULL;
ds@1952
    44
ISmmPluginManager *g_pMMPlugins = NULL;
ds@1952
    45
CGlobalVars *gpGlobals = NULL;
ds@1952
    46
ICvar *icvar = NULL;
ds@1952
    47
IGameEventManager2 *gameevents = NULL;
ds@1952
    48
IUniformRandomStream *engrandom = NULL;
ds@1952
    49
CallClass<IVEngineServer> *enginePatch = NULL;
ds@1952
    50
CallClass<IServerGameDLL> *gamedllPatch = NULL;
ds@1952
    51
IPlayerInfoManager *playerinfo = NULL;
ds@1952
    52
IBaseFileSystem *basefilesystem = NULL;
ds@1952
    53
IEngineSound *enginesound = NULL;
ds@1952
    54
IServerPluginHelpers *serverpluginhelpers = NULL;
ds@1952
    55
IServerPluginCallbacks *vsp_interface = NULL;
ds@1952
    56
int vsp_version = 0;
ds@1952
    57
ds@1952
    58
PLUGIN_EXPOSE(SourceMod, g_SourceMod_Core);
ds@1952
    59
ds@1952
    60
bool SourceMod_Core::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
ds@1952
    61
{
ds@1952
    62
	PLUGIN_SAVEVARS();
ds@1952
    63
ds@1952
    64
	GET_V_IFACE_ANY(GetServerFactory, gamedll, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
ds@1952
    65
	GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
ds@1952
    66
	GET_V_IFACE_CURRENT(GetServerFactory, serverClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
ds@1952
    67
	GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
ds@1952
    68
	GET_V_IFACE_CURRENT(GetEngineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
ds@1952
    69
	GET_V_IFACE_CURRENT(GetEngineFactory, engrandom, IUniformRandomStream, VENGINE_SERVER_RANDOM_INTERFACE_VERSION);
ds@1952
    70
	GET_V_IFACE_CURRENT(GetFileSystemFactory, basefilesystem, IBaseFileSystem, BASEFILESYSTEM_INTERFACE_VERSION);
ds@1952
    71
	GET_V_IFACE_CURRENT(GetEngineFactory, enginesound, IEngineSound, IENGINESOUND_SERVER_INTERFACE_VERSION);
ds@1952
    72
	GET_V_IFACE_CURRENT(GetEngineFactory, serverpluginhelpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
ds@1952
    73
ds@1952
    74
	/* :TODO: Make this optional and... make it find earlier versions [?] */
ds@1952
    75
	GET_V_IFACE_CURRENT(GetServerFactory, playerinfo, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
ds@1952
    76
ds@1952
    77
	if ((g_pMMPlugins = (ISmmPluginManager *)g_SMAPI->MetaFactory(MMIFACE_PLMANAGER, NULL, NULL)) == NULL)
ds@1952
    78
	{
ds@1952
    79
		if (error)
ds@1952
    80
		{
ds@1952
    81
			snprintf(error, maxlen, "Unable to find interface %s", MMIFACE_PLMANAGER);
ds@1952
    82
		}
ds@1952
    83
		return false;
ds@1952
    84
	}
ds@1952
    85
ds@1952
    86
	gpGlobals = ismm->GetCGlobals();
ds@1952
    87
	
ds@1952
    88
	ismm->AddListener(this, this);
ds@1952
    89
ds@1952
    90
#if defined METAMOD_PLAPI_VERSION
ds@1952
    91
	if ((vsp_interface = g_SMAPI->GetVSPInfo(&vsp_version)) == NULL)
ds@1952
    92
#endif
ds@1952
    93
	{
ds@1952
    94
		g_SMAPI->EnableVSPListener();
ds@1952
    95
	}
ds@1952
    96
ds@1952
    97
	return g_SourceMod.InitializeSourceMod(error, maxlen, late);
ds@1952
    98
}
ds@1952
    99
ds@1952
   100
bool SourceMod_Core::Unload(char *error, size_t maxlen)
ds@1952
   101
{
ds@1952
   102
	g_SourceMod.CloseSourceMod();
ds@1952
   103
	return true;
ds@1952
   104
}
ds@1952
   105
ds@1952
   106
bool SourceMod_Core::Pause(char *error, size_t maxlen)
ds@1952
   107
{
ds@1952
   108
	return true;
ds@1952
   109
}
ds@1952
   110
ds@1952
   111
bool SourceMod_Core::Unpause(char *error, size_t maxlen)
ds@1952
   112
{
ds@1952
   113
	return true;
ds@1952
   114
}
ds@1952
   115
ds@1952
   116
void SourceMod_Core::AllPluginsLoaded()
ds@1952
   117
{
ds@1952
   118
	g_SourceMod.AllPluginsLoaded();
ds@1952
   119
}
ds@1952
   120
ds@1952
   121
const char *SourceMod_Core::GetAuthor()
ds@1952
   122
{
ds@1952
   123
	return "AlliedModders LLC";
ds@1952
   124
}
ds@1952
   125
ds@1952
   126
const char *SourceMod_Core::GetName()
ds@1952
   127
{
ds@1952
   128
	return "SourceMod";
ds@1952
   129
}
ds@1952
   130
ds@1952
   131
const char *SourceMod_Core::GetDescription()
ds@1952
   132
{
ds@1952
   133
	return "Extensible administration and scripting system";
ds@1952
   134
}
ds@1952
   135
ds@1952
   136
const char *SourceMod_Core::GetURL()
ds@1952
   137
{
ds@1952
   138
	return "http://www.sourcemod.net/";
ds@1952
   139
}
ds@1952
   140
ds@1952
   141
const char *SourceMod_Core::GetLicense()
ds@1952
   142
{
ds@1952
   143
	return "See LICENSE.txt";
ds@1952
   144
}
ds@1952
   145
ds@1952
   146
const char *SourceMod_Core::GetVersion()
ds@1952
   147
{
ds@1952
   148
	return SVN_FULL_VERSION;
ds@1952
   149
}
ds@1952
   150
ds@1952
   151
const char *SourceMod_Core::GetDate()
ds@1952
   152
{
ds@1952
   153
	return __DATE__;
ds@1952
   154
}
ds@1952
   155
ds@1952
   156
const char *SourceMod_Core::GetLogTag()
ds@1952
   157
{
ds@1952
   158
	return "SM";
ds@1952
   159
}
ds@1952
   160
ds@1952
   161
void SourceMod_Core::OnVSPListening(IServerPluginCallbacks *iface)
ds@1952
   162
{
ds@1952
   163
	/* This shouldn't happen */
ds@1952
   164
	if (!iface)
ds@1952
   165
	{
ds@1952
   166
		g_Logger.LogFatal("Metamod:Source version is out of date. SourceMod requires 1.4.2 or greater.");
ds@1952
   167
		return;
ds@1952
   168
	}
ds@1952
   169
ds@1952
   170
	if (vsp_interface == NULL)
ds@1952
   171
	{
ds@1952
   172
		vsp_interface = iface;
ds@1952
   173
	}
ds@1952
   174
ds@1952
   175
	if (!g_Loaded)
ds@1952
   176
	{
ds@1952
   177
		return;
ds@1952
   178
	}
ds@1952
   179
ds@1952
   180
#if defined METAMOD_PLAPI_VERSION
ds@1952
   181
	if (vsp_version == 0)
ds@1952
   182
	{
ds@1952
   183
		g_SMAPI->GetVSPInfo(&vsp_version);
ds@1952
   184
	}
ds@1952
   185
#else
ds@1952
   186
	if (vsp_version == 0)
ds@1952
   187
	{
ds@1952
   188
		if (strcmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
ds@1952
   189
		{
ds@1952
   190
			vsp_version = 1;
ds@1952
   191
		}
ds@1952
   192
		else
ds@1952
   193
		{
ds@1952
   194
			vsp_version = 2;
ds@1952
   195
		}
ds@1952
   196
	}
ds@1952
   197
#endif
ds@1952
   198
ds@1952
   199
	/* Notify! */
ds@1952
   200
	SMGlobalClass *pBase = SMGlobalClass::head;
ds@1952
   201
	while (pBase)
ds@1952
   202
	{
ds@1952
   203
		pBase->OnSourceModVSPReceived();
ds@1952
   204
		pBase = pBase->m_pGlobalClassNext;
ds@1952
   205
	}
ds@1952
   206
}
ds@1952
   207
ds@1952
   208
#if defined METAMOD_PLAPI_VERSION
ds@1952
   209
ds@1952
   210
void SourceMod_Core::OnUnlinkConCommandBase(PluginId id, ConCommandBase *pCommand)
ds@1952
   211
{
ds@1952
   212
	Global_OnUnlinkConCommandBase(pCommand);
ds@1952
   213
}
ds@1952
   214
ds@1952
   215
#else
ds@1952
   216
ds@1952
   217
void SourceMod_Core::OnPluginUnload(PluginId id)
ds@1952
   218
{
ds@1952
   219
	Global_OnUnlinkConCommandBase(NULL);
ds@1952
   220
}
ds@1952
   221
ds@1952
   222
#endif
ds@1952
   223
ds@1952
   224
void *SourceMod_Core::OnMetamodQuery(const char *iface, int *ret)
ds@1952
   225
{
ds@1952
   226
	void *ptr = NULL;
ds@1952
   227
ds@1952
   228
	if (strcmp(iface, SOURCEMOD_INTERFACE_EXTENSIONS) == 0)
ds@1952
   229
	{
ds@1952
   230
		ptr = (IExtensionManager *)&g_Extensions;
ds@1952
   231
	}
ds@1952
   232
ds@1952
   233
	if (ret != NULL)
ds@1952
   234
	{
ds@1952
   235
		*ret = (ptr == NULL) ? IFACE_FAILED : IFACE_OK;
ds@1952
   236
	}
ds@1952
   237
ds@1952
   238
	return ptr;
ds@1952
   239
}
ds@1952
   240