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 "vcallbuilder.h"
41 #if defined ORANGEBOX_BUILD
42 #define SDKTOOLS_GAME_FILE "sdktools.games.ep2"
44 #define SDKTOOLS_GAME_FILE "sdktools.games"
49 * @brief Implements SDK Tools extension code.
52 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
54 SDKTools g_SdkTools; /**< Global singleton for extension's main interface */
55 IServerGameEnts *gameents = NULL;
56 IEngineTrace *enginetrace = NULL;
57 IEngineSound *engsound = NULL;
58 INetworkStringTableContainer *netstringtables = NULL;
59 IServerPluginHelpers *pluginhelpers = NULL;
60 IBinTools *g_pBinTools = NULL;
61 IGameConfig *g_pGameConf = NULL;
62 IGameHelpers *g_pGameHelpers = NULL;
63 IServerGameClients *serverClients = NULL;
64 IVoiceServer *voiceserver = NULL;
65 IPlayerInfoManager *playerinfomngr = NULL;
67 IServer *iserver = NULL;
68 SourceHook::CallClass<IVEngineServer> *enginePatch = NULL;
69 SourceHook::CallClass<IEngineSound> *enginesoundPatch = NULL;
70 HandleType_t g_CallHandle = 0;
71 HandleType_t g_TraceHandle = 0;
73 SMEXT_LINK(&g_SdkTools);
75 extern sp_nativeinfo_t g_CallNatives[];
76 extern sp_nativeinfo_t g_TENatives[];
77 extern sp_nativeinfo_t g_TRNatives[];
78 extern sp_nativeinfo_t g_StringTableNatives[];
79 extern sp_nativeinfo_t g_VoiceNatives[];
80 extern sp_nativeinfo_t g_EntInputNatives[];
81 extern sp_nativeinfo_t g_TeamNatives[];
83 bool SDKTools::SDK_OnLoad(char *error, size_t maxlength, bool late)
87 if (!gameconfs->LoadGameConfigFile(SDKTOOLS_GAME_FILE, &g_pGameConf, error, maxlength))
92 sharesys->AddDependency(myself, "bintools.ext", true, true);
93 sharesys->AddNatives(myself, g_CallNatives);
94 sharesys->AddNatives(myself, g_Natives);
95 sharesys->AddNatives(myself, g_TENatives);
96 sharesys->AddNatives(myself, g_SoundNatives);
97 sharesys->AddNatives(myself, g_TRNatives);
98 sharesys->AddNatives(myself, g_StringTableNatives);
99 sharesys->AddNatives(myself, g_VoiceNatives);
100 sharesys->AddNatives(myself, g_EntInputNatives);
101 sharesys->AddNatives(myself, g_TeamNatives);
102 sharesys->AddNatives(myself, g_EntOutputNatives);
104 SM_GET_IFACE(GAMEHELPERS, g_pGameHelpers);
106 playerhelpers->AddClientListener(&g_SdkTools);
107 g_CallHandle = handlesys->CreateType("ValveCall", this, 0, NULL, NULL, myself->GetIdentity(), &err);
108 if (g_CallHandle == 0)
110 snprintf(error, maxlength, "Could not create call handle type (err: %d)", err);
114 TypeAccess TraceAccess;
115 handlesys->InitAccessDefaults(&TraceAccess, NULL);
116 TraceAccess.ident = myself->GetIdentity();
117 TraceAccess.access[HTypeAccess_Create] = true;
118 TraceAccess.access[HTypeAccess_Inherit] = true;
119 g_TraceHandle = handlesys->CreateType("TraceRay", this, 0, &TraceAccess, NULL, myself->GetIdentity(), &err);
120 if (g_TraceHandle == 0)
122 handlesys->RemoveType(g_CallHandle, myself->GetIdentity());
124 snprintf(error, maxlength, "Could not create traceray handle type (err: %d)", err);
128 #if defined ORANGEBOX_BUILD
131 CONVAR_REGISTER(this);
133 SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
135 playerhelpers->RegisterCommandTargetProcessor(this);
137 MathLib_Init(2.2f, 2.2f, 0.0f, 2);
139 spengine = g_pSM->GetScriptingEngine();
141 plsys->AddPluginsListener(&g_OutputManager);
143 g_OutputManager.Init();
150 void SDKTools::OnHandleDestroy(HandleType_t type, void *object)
152 if (type == g_CallHandle)
154 ValveCall *v = (ValveCall *)object;
157 else if (type == g_TraceHandle)
159 trace_t *tr = (trace_t *)object;
164 void SDKTools::SDK_OnUnload()
166 SourceHook::List<ValveCall *>::iterator iter;
167 for (iter = g_RegCalls.begin();
168 iter != g_RegCalls.end();
178 g_pAcceptInput->Destroy();
179 g_pAcceptInput = NULL;
182 g_TEManager.Shutdown();
183 s_TempEntHooks.Shutdown();
184 s_SoundHooks.Shutdown();
186 gameconfs->CloseGameConfigFile(g_pGameConf);
187 playerhelpers->RemoveClientListener(&g_SdkTools);
188 playerhelpers->UnregisterCommandTargetProcessor(this);
189 plsys->RemovePluginsListener(&g_OutputManager);
191 SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
195 SH_RELEASE_CALLCLASS(enginePatch);
198 if (enginesoundPatch)
200 SH_RELEASE_CALLCLASS(enginesoundPatch);
201 enginesoundPatch = NULL;
205 if (g_CallHandle != 0)
207 if ((err = handlesys->RemoveType(g_CallHandle, myself->GetIdentity())) != true)
209 g_pSM->LogError(myself, "Could not remove call handle (type=%x, err=%d)", g_CallHandle, err);
213 if (g_TraceHandle != 0)
215 if ((err = handlesys->RemoveType(g_TraceHandle, myself->GetIdentity())) != true)
217 g_pSM->LogError(myself, "Could not remove trace handle (type=%x, err=%d)", g_TraceHandle, err);
222 bool SDKTools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
224 GET_V_IFACE_ANY(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
225 GET_V_IFACE_ANY(GetEngineFactory, engsound, IEngineSound, IENGINESOUND_SERVER_INTERFACE_VERSION);
226 GET_V_IFACE_ANY(GetEngineFactory, enginetrace, IEngineTrace, INTERFACEVERSION_ENGINETRACE_SERVER);
227 GET_V_IFACE_ANY(GetEngineFactory, netstringtables, INetworkStringTableContainer, INTERFACENAME_NETWORKSTRINGTABLESERVER);
228 GET_V_IFACE_ANY(GetEngineFactory, pluginhelpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
229 GET_V_IFACE_ANY(GetServerFactory, serverClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
230 GET_V_IFACE_ANY(GetEngineFactory, voiceserver, IVoiceServer, INTERFACEVERSION_VOICESERVER);
231 GET_V_IFACE_ANY(GetServerFactory, playerinfomngr, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
232 GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
234 enginePatch = SH_GET_CALLCLASS(engine);
235 enginesoundPatch = SH_GET_CALLCLASS(engsound);
240 void SDKTools::SDK_OnAllLoaded()
242 SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
249 g_TEManager.Initialize();
250 s_TempEntHooks.Initialize();
251 s_SoundHooks.Initialize();
252 InitializeValveGlobals();
256 bool SDKTools::QueryRunning(char *error, size_t maxlength)
258 SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
263 bool SDKTools::QueryInterfaceDrop(SMInterface *pInterface)
265 if (pInterface == g_pBinTools)
270 return IExtensionInterface::QueryInterfaceDrop(pInterface);
273 void SDKTools::NotifyInterfaceDrop(SMInterface *pInterface)
275 SourceHook::List<ValveCall *>::iterator iter;
276 for (iter = g_RegCalls.begin();
277 iter != g_RegCalls.end();
285 g_TEManager.Shutdown();
286 s_TempEntHooks.Shutdown();
290 g_pAcceptInput->Destroy();
291 g_pAcceptInput = NULL;
295 bool SDKTools::RegisterConCommandBase(ConCommandBase *pVar)
297 #if defined METAMOD_PLAPI_VERSION
298 return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
300 return g_SMAPI->RegisterConCmdBase(g_PLAPI, pVar);
304 bool SDKTools::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
310 if (!(name=g_pGameConf->GetKeyValue("SlapSoundCount")))
312 RETURN_META_VALUE(MRES_IGNORED, true);
319 snprintf(key, sizeof(key), "SlapSound%d", n);
320 if ((name=g_pGameConf->GetKeyValue(key)))
322 engsound->PrecacheSound(name, true);
327 RETURN_META_VALUE(MRES_IGNORED, true);
330 bool SDKTools::ProcessCommandTarget(cmd_target_info_t *info)
332 if (strcmp(info->pattern, "@aim") != 0)
337 IGamePlayer *pAdmin = info->admin ? playerhelpers->GetGamePlayer(info->admin) : NULL;
339 /* The server can't aim, of course. */
346 if ((player_index = GetClientAimTarget(pAdmin->GetEdict(), true)) < 1)
348 info->reason = COMMAND_TARGET_NONE;
349 info->num_targets = 0;
353 IGamePlayer *pTarget = playerhelpers->GetGamePlayer(player_index);
357 info->reason = COMMAND_TARGET_NONE;
358 info->num_targets = 0;
362 info->reason = playerhelpers->FilterCommandTarget(pAdmin, pTarget, info->flags);
363 if (info->reason != COMMAND_TARGET_VALID)
365 info->num_targets = 0;
369 info->targets[0] = player_index;
370 info->num_targets = 1;
371 info->reason = COMMAND_TARGET_VALID;
372 info->target_name_style = COMMAND_TARGETNAME_RAW;
373 snprintf(info->target_name, info->target_name_maxlength, "%s", pTarget->GetName());