extensions/sdktools/extension.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Nov 14 09:18:30 2008 -0600 (2008-11-14)
changeset 2400 76b7b4575e21
parent 2306 9150da197062
child 2503 602bd1bc1739
permissions -rw-r--r--
Added preliminary support for Left 4 Dead; some things may not yet work.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod SDKTools Extension
     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 "extension.h"
    33 #include <compat_wrappers.h>
    34 #include "vcallbuilder.h"
    35 #include "vnatives.h"
    36 #include "vhelpers.h"
    37 #include "vglobals.h"
    38 #include "tempents.h"
    39 #include "vsound.h"
    40 #include "output.h"
    41 
    42 #if SOURCE_ENGINE == SE_LEFT4DEAD
    43 	#define SDKTOOLS_GAME_FILE		"sdktools.games.l4d"
    44 #elif SOURCE_ENGINE == SE_ORANGEBOX
    45 	#define SDKTOOLS_GAME_FILE		"sdktools.games.ep2"
    46 #else
    47 	#define SDKTOOLS_GAME_FILE		"sdktools.games"
    48 #endif
    49 
    50 /**
    51  * @file extension.cpp
    52  * @brief Implements SDK Tools extension code.
    53  */
    54 
    55 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
    56 
    57 SDKTools g_SdkTools;		/**< Global singleton for extension's main interface */
    58 IServerGameEnts *gameents = NULL;
    59 IEngineTrace *enginetrace = NULL;
    60 IEngineSound *engsound = NULL;
    61 INetworkStringTableContainer *netstringtables = NULL;
    62 IServerPluginHelpers *pluginhelpers = NULL;
    63 IBinTools *g_pBinTools = NULL;
    64 IGameConfig *g_pGameConf = NULL;
    65 IGameHelpers *g_pGameHelpers = NULL;
    66 IServerGameClients *serverClients = NULL;
    67 IVoiceServer *voiceserver = NULL;
    68 IPlayerInfoManager *playerinfomngr = NULL;
    69 ICvar *icvar = NULL;
    70 IServer *iserver = NULL;
    71 CGlobalVars *gpGlobals;
    72 SourceHook::CallClass<IVEngineServer> *enginePatch = NULL;
    73 SourceHook::CallClass<IEngineSound> *enginesoundPatch = NULL;
    74 HandleType_t g_CallHandle = 0;
    75 HandleType_t g_TraceHandle = 0;
    76 
    77 SMEXT_LINK(&g_SdkTools);
    78 
    79 extern sp_nativeinfo_t g_CallNatives[];
    80 extern sp_nativeinfo_t g_TENatives[];
    81 extern sp_nativeinfo_t g_TRNatives[];
    82 extern sp_nativeinfo_t g_StringTableNatives[];
    83 extern sp_nativeinfo_t g_VoiceNatives[];
    84 extern sp_nativeinfo_t g_EntInputNatives[];
    85 extern sp_nativeinfo_t g_TeamNatives[];
    86 
    87 bool SDKTools::SDK_OnLoad(char *error, size_t maxlength, bool late)
    88 {
    89 	HandleError err;
    90 
    91 	if (!gameconfs->LoadGameConfigFile(SDKTOOLS_GAME_FILE, &g_pGameConf, error, maxlength))
    92 	{
    93 		return false;
    94 	}
    95 
    96 	sharesys->AddDependency(myself, "bintools.ext", true, true);
    97 	sharesys->AddNatives(myself, g_CallNatives);
    98 	sharesys->AddNatives(myself, g_Natives);
    99 	sharesys->AddNatives(myself, g_TENatives);
   100 	sharesys->AddNatives(myself, g_SoundNatives);
   101 	sharesys->AddNatives(myself, g_TRNatives);
   102 	sharesys->AddNatives(myself, g_StringTableNatives);
   103 	sharesys->AddNatives(myself, g_VoiceNatives);
   104 	sharesys->AddNatives(myself, g_EntInputNatives);
   105 	sharesys->AddNatives(myself, g_TeamNatives);
   106 	sharesys->AddNatives(myself, g_EntOutputNatives);
   107 
   108 	SM_GET_IFACE(GAMEHELPERS, g_pGameHelpers);
   109 
   110 	playerhelpers->AddClientListener(&g_SdkTools);
   111 	g_CallHandle = handlesys->CreateType("ValveCall", this, 0, NULL, NULL, myself->GetIdentity(), &err);
   112 	if (g_CallHandle == 0)
   113 	{
   114 		snprintf(error, maxlength, "Could not create call handle type (err: %d)", err);	
   115 		return false;
   116 	}
   117 
   118 	TypeAccess TraceAccess;
   119 	handlesys->InitAccessDefaults(&TraceAccess, NULL);
   120 	TraceAccess.ident = myself->GetIdentity();
   121 	TraceAccess.access[HTypeAccess_Create] = true;
   122 	TraceAccess.access[HTypeAccess_Inherit] = true;
   123 	g_TraceHandle = handlesys->CreateType("TraceRay", this, 0, &TraceAccess, NULL, myself->GetIdentity(), &err);
   124 	if (g_TraceHandle == 0)
   125 	{
   126 		handlesys->RemoveType(g_CallHandle, myself->GetIdentity());
   127 		g_CallHandle = 0;
   128 		snprintf(error, maxlength, "Could not create traceray handle type (err: %d)", err);
   129 		return false;
   130 	}
   131 
   132 #if SOURCE_ENGINE >= SE_ORANGEBOX
   133 	g_pCVar = icvar;
   134 #endif
   135 	CONVAR_REGISTER(this);
   136 
   137 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
   138 
   139 	playerhelpers->RegisterCommandTargetProcessor(this);
   140 
   141 	MathLib_Init(2.2f, 2.2f, 0.0f, 2);
   142 
   143 	spengine = g_pSM->GetScriptingEngine();
   144 
   145 	plsys->AddPluginsListener(&g_OutputManager);
   146 
   147 	g_OutputManager.Init();
   148 
   149 	VoiceInit();
   150 
   151 	return true;
   152 }
   153 
   154 void SDKTools::OnHandleDestroy(HandleType_t type, void *object)
   155 {
   156 	if (type == g_CallHandle)
   157 	{
   158 		ValveCall *v = (ValveCall *)object;
   159 		delete v;
   160 	}
   161 	else if (type == g_TraceHandle)
   162 	{
   163 		trace_t *tr = (trace_t *)object;
   164 		delete tr;
   165 	}
   166 }
   167 
   168 void SDKTools::SDK_OnUnload()
   169 {
   170 	SourceHook::List<ValveCall *>::iterator iter;
   171 	for (iter = g_RegCalls.begin();
   172 		 iter != g_RegCalls.end();
   173 		 iter++)
   174 	{
   175 		delete (*iter);
   176 	}
   177 	g_RegCalls.clear();
   178 	ShutdownHelpers();
   179 
   180 	if (g_pAcceptInput)
   181 	{
   182 		g_pAcceptInput->Destroy();
   183 		g_pAcceptInput = NULL;
   184 	}
   185 
   186 	g_TEManager.Shutdown();
   187 	s_TempEntHooks.Shutdown();
   188 	s_SoundHooks.Shutdown();
   189 
   190 	gameconfs->CloseGameConfigFile(g_pGameConf);
   191 	playerhelpers->RemoveClientListener(&g_SdkTools);
   192 	playerhelpers->UnregisterCommandTargetProcessor(this);
   193 	plsys->RemovePluginsListener(&g_OutputManager);
   194 
   195 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
   196 
   197 	if (enginePatch)
   198 	{
   199 		SH_RELEASE_CALLCLASS(enginePatch);
   200 		enginePatch = NULL;
   201 	}
   202 	if (enginesoundPatch)
   203 	{
   204 		SH_RELEASE_CALLCLASS(enginesoundPatch);
   205 		enginesoundPatch = NULL;
   206 	}
   207 
   208 	bool err;
   209 	if (g_CallHandle != 0)
   210 	{
   211 		if ((err = handlesys->RemoveType(g_CallHandle, myself->GetIdentity())) != true)
   212 		{
   213 			g_pSM->LogError(myself, "Could not remove call handle (type=%x, err=%d)", g_CallHandle, err);
   214 		}
   215 	}
   216 
   217 	if (g_TraceHandle != 0)
   218 	{
   219 		if ((err = handlesys->RemoveType(g_TraceHandle, myself->GetIdentity())) != true)
   220 		{
   221 			g_pSM->LogError(myself, "Could not remove trace handle (type=%x, err=%d)", g_TraceHandle, err);
   222 		}
   223 	}
   224 }
   225 
   226 bool SDKTools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
   227 {
   228 	GET_V_IFACE_ANY(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
   229 	GET_V_IFACE_ANY(GetEngineFactory, engsound, IEngineSound, IENGINESOUND_SERVER_INTERFACE_VERSION);
   230 	GET_V_IFACE_ANY(GetEngineFactory, enginetrace, IEngineTrace, INTERFACEVERSION_ENGINETRACE_SERVER);
   231 	GET_V_IFACE_ANY(GetEngineFactory, netstringtables, INetworkStringTableContainer, INTERFACENAME_NETWORKSTRINGTABLESERVER);
   232 	GET_V_IFACE_ANY(GetEngineFactory, pluginhelpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
   233 	GET_V_IFACE_ANY(GetServerFactory, serverClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
   234 	GET_V_IFACE_ANY(GetEngineFactory, voiceserver, IVoiceServer, INTERFACEVERSION_VOICESERVER);
   235 	GET_V_IFACE_ANY(GetServerFactory, playerinfomngr, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
   236 	GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
   237 
   238 	gpGlobals = ismm->GetCGlobals();
   239 	enginePatch = SH_GET_CALLCLASS(engine);
   240 	enginesoundPatch = SH_GET_CALLCLASS(engsound);
   241 
   242 	return true;
   243 }
   244 
   245 void SDKTools::SDK_OnAllLoaded()
   246 {
   247 	SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
   248 
   249 	if (!g_pBinTools)
   250 	{
   251 		return;
   252 	}
   253 
   254 	g_TEManager.Initialize();
   255 	s_TempEntHooks.Initialize();
   256 	s_SoundHooks.Initialize();
   257 	InitializeValveGlobals();
   258 	GetIServer();
   259 }
   260 
   261 bool SDKTools::QueryRunning(char *error, size_t maxlength)
   262 {
   263 	SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
   264 
   265 	return true;
   266 }
   267 
   268 bool SDKTools::QueryInterfaceDrop(SMInterface *pInterface)
   269 {
   270 	if (pInterface == g_pBinTools)
   271 	{
   272 		return false;
   273 	}
   274 
   275 	return IExtensionInterface::QueryInterfaceDrop(pInterface);
   276 }
   277 
   278 void SDKTools::NotifyInterfaceDrop(SMInterface *pInterface)
   279 {
   280 	SourceHook::List<ValveCall *>::iterator iter;
   281 	for (iter = g_RegCalls.begin();
   282 		iter != g_RegCalls.end();
   283 		iter++)
   284 	{
   285 		delete (*iter);
   286 	}
   287 	g_RegCalls.clear();
   288 	ShutdownHelpers();
   289 
   290 	g_TEManager.Shutdown();
   291 	s_TempEntHooks.Shutdown();
   292 
   293 	if (g_pAcceptInput)
   294 	{
   295 		g_pAcceptInput->Destroy();
   296 		g_pAcceptInput = NULL;
   297 	}
   298 }
   299 
   300 bool SDKTools::RegisterConCommandBase(ConCommandBase *pVar)
   301 {
   302 #if defined METAMOD_PLAPI_VERSION
   303 	return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
   304 #else
   305 	return g_SMAPI->RegisterConCmdBase(g_PLAPI, pVar);
   306 #endif
   307 }
   308 
   309 bool SDKTools::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
   310 {
   311 	const char *name;
   312 	char key[32];
   313 	int count, n = 1;
   314 
   315 	if (!(name=g_pGameConf->GetKeyValue("SlapSoundCount")))
   316 	{
   317 		RETURN_META_VALUE(MRES_IGNORED, true);
   318 	}
   319 
   320 	count = atoi(name);
   321 
   322 	while (n <= count)
   323 	{
   324 		snprintf(key, sizeof(key), "SlapSound%d", n);
   325 		if ((name=g_pGameConf->GetKeyValue(key)))
   326 		{
   327 			engsound->PrecacheSound(name, true);
   328 		}
   329 		n++;
   330 	}
   331 
   332 	RETURN_META_VALUE(MRES_IGNORED, true);
   333 }
   334 
   335 bool SDKTools::ProcessCommandTarget(cmd_target_info_t *info)
   336 {
   337 	if (strcmp(info->pattern, "@aim") != 0)
   338 	{
   339 		return false;
   340 	}
   341 
   342 	IGamePlayer *pAdmin = info->admin ? playerhelpers->GetGamePlayer(info->admin) : NULL;
   343 
   344 	/* The server can't aim, of course. */
   345 	if (pAdmin == NULL)
   346 	{
   347 		return false;
   348 	}
   349 
   350 	int player_index;
   351 	if ((player_index = GetClientAimTarget(pAdmin->GetEdict(), true)) < 1)
   352 	{
   353 		info->reason = COMMAND_TARGET_NONE;
   354 		info->num_targets = 0;
   355 		return true;
   356 	}
   357 
   358 	IGamePlayer *pTarget = playerhelpers->GetGamePlayer(player_index);
   359 
   360 	if (pTarget == NULL)
   361 	{
   362 		info->reason = COMMAND_TARGET_NONE;
   363 		info->num_targets = 0;
   364 		return true;
   365 	}
   366 
   367 	info->reason = playerhelpers->FilterCommandTarget(pAdmin, pTarget, info->flags);
   368 	if (info->reason != COMMAND_TARGET_VALID)
   369 	{
   370 		info->num_targets = 0;
   371 		return true;
   372 	}
   373 
   374 	info->targets[0] = player_index;
   375 	info->num_targets = 1;
   376 	info->reason = COMMAND_TARGET_VALID;
   377 	info->target_name_style = COMMAND_TARGETNAME_RAW;
   378 	snprintf(info->target_name, info->target_name_maxlength, "%s", pTarget->GetName());
   379 
   380 	return true;
   381 }