Added preliminary support for Left 4 Dead; some things may not yet work.
3 * =============================================================================
4 * SourceMod SDKTools Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "extension.h"
33 #include <compat_wrappers.h>
34 #include "vcallbuilder.h"
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"
47 #define SDKTOOLS_GAME_FILE "sdktools.games"
52 * @brief Implements SDK Tools extension code.
55 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
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;
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;
77 SMEXT_LINK(&g_SdkTools);
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[];
87 bool SDKTools::SDK_OnLoad(char *error, size_t maxlength, bool late)
91 if (!gameconfs->LoadGameConfigFile(SDKTOOLS_GAME_FILE, &g_pGameConf, error, maxlength))
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);
108 SM_GET_IFACE(GAMEHELPERS, g_pGameHelpers);
110 playerhelpers->AddClientListener(&g_SdkTools);
111 g_CallHandle = handlesys->CreateType("ValveCall", this, 0, NULL, NULL, myself->GetIdentity(), &err);
112 if (g_CallHandle == 0)
114 snprintf(error, maxlength, "Could not create call handle type (err: %d)", err);
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)
126 handlesys->RemoveType(g_CallHandle, myself->GetIdentity());
128 snprintf(error, maxlength, "Could not create traceray handle type (err: %d)", err);
132 #if SOURCE_ENGINE >= SE_ORANGEBOX
135 CONVAR_REGISTER(this);
137 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
139 playerhelpers->RegisterCommandTargetProcessor(this);
141 MathLib_Init(2.2f, 2.2f, 0.0f, 2);
143 spengine = g_pSM->GetScriptingEngine();
145 plsys->AddPluginsListener(&g_OutputManager);
147 g_OutputManager.Init();
154 void SDKTools::OnHandleDestroy(HandleType_t type, void *object)
156 if (type == g_CallHandle)
158 ValveCall *v = (ValveCall *)object;
161 else if (type == g_TraceHandle)
163 trace_t *tr = (trace_t *)object;
168 void SDKTools::SDK_OnUnload()
170 SourceHook::List<ValveCall *>::iterator iter;
171 for (iter = g_RegCalls.begin();
172 iter != g_RegCalls.end();
182 g_pAcceptInput->Destroy();
183 g_pAcceptInput = NULL;
186 g_TEManager.Shutdown();
187 s_TempEntHooks.Shutdown();
188 s_SoundHooks.Shutdown();
190 gameconfs->CloseGameConfigFile(g_pGameConf);
191 playerhelpers->RemoveClientListener(&g_SdkTools);
192 playerhelpers->UnregisterCommandTargetProcessor(this);
193 plsys->RemovePluginsListener(&g_OutputManager);
195 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
199 SH_RELEASE_CALLCLASS(enginePatch);
202 if (enginesoundPatch)
204 SH_RELEASE_CALLCLASS(enginesoundPatch);
205 enginesoundPatch = NULL;
209 if (g_CallHandle != 0)
211 if ((err = handlesys->RemoveType(g_CallHandle, myself->GetIdentity())) != true)
213 g_pSM->LogError(myself, "Could not remove call handle (type=%x, err=%d)", g_CallHandle, err);
217 if (g_TraceHandle != 0)
219 if ((err = handlesys->RemoveType(g_TraceHandle, myself->GetIdentity())) != true)
221 g_pSM->LogError(myself, "Could not remove trace handle (type=%x, err=%d)", g_TraceHandle, err);
226 bool SDKTools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
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);
238 gpGlobals = ismm->GetCGlobals();
239 enginePatch = SH_GET_CALLCLASS(engine);
240 enginesoundPatch = SH_GET_CALLCLASS(engsound);
245 void SDKTools::SDK_OnAllLoaded()
247 SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
254 g_TEManager.Initialize();
255 s_TempEntHooks.Initialize();
256 s_SoundHooks.Initialize();
257 InitializeValveGlobals();
261 bool SDKTools::QueryRunning(char *error, size_t maxlength)
263 SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
268 bool SDKTools::QueryInterfaceDrop(SMInterface *pInterface)
270 if (pInterface == g_pBinTools)
275 return IExtensionInterface::QueryInterfaceDrop(pInterface);
278 void SDKTools::NotifyInterfaceDrop(SMInterface *pInterface)
280 SourceHook::List<ValveCall *>::iterator iter;
281 for (iter = g_RegCalls.begin();
282 iter != g_RegCalls.end();
290 g_TEManager.Shutdown();
291 s_TempEntHooks.Shutdown();
295 g_pAcceptInput->Destroy();
296 g_pAcceptInput = NULL;
300 bool SDKTools::RegisterConCommandBase(ConCommandBase *pVar)
302 #if defined METAMOD_PLAPI_VERSION
303 return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
305 return g_SMAPI->RegisterConCmdBase(g_PLAPI, pVar);
309 bool SDKTools::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
315 if (!(name=g_pGameConf->GetKeyValue("SlapSoundCount")))
317 RETURN_META_VALUE(MRES_IGNORED, true);
324 snprintf(key, sizeof(key), "SlapSound%d", n);
325 if ((name=g_pGameConf->GetKeyValue(key)))
327 engsound->PrecacheSound(name, true);
332 RETURN_META_VALUE(MRES_IGNORED, true);
335 bool SDKTools::ProcessCommandTarget(cmd_target_info_t *info)
337 if (strcmp(info->pattern, "@aim") != 0)
342 IGamePlayer *pAdmin = info->admin ? playerhelpers->GetGamePlayer(info->admin) : NULL;
344 /* The server can't aim, of course. */
351 if ((player_index = GetClientAimTarget(pAdmin->GetEdict(), true)) < 1)
353 info->reason = COMMAND_TARGET_NONE;
354 info->num_targets = 0;
358 IGamePlayer *pTarget = playerhelpers->GetGamePlayer(player_index);
362 info->reason = COMMAND_TARGET_NONE;
363 info->num_targets = 0;
367 info->reason = playerhelpers->FilterCommandTarget(pAdmin, pTarget, info->flags);
368 if (info->reason != COMMAND_TARGET_VALID)
370 info->num_targets = 0;
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());