1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sdktools/extension.cpp Sun Mar 30 07:00:22 2008 +0000
1.3 @@ -0,0 +1,331 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod SDKTools Extension
1.8 + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id$
1.33 + */
1.34 +
1.35 +#include "extension.h"
1.36 +#include "vcallbuilder.h"
1.37 +#include "vnatives.h"
1.38 +#include "vhelpers.h"
1.39 +#include "vglobals.h"
1.40 +#include "tempents.h"
1.41 +#include "vsound.h"
1.42 +
1.43 +#if defined ORANGEBOX_BUILD
1.44 + #define SDKTOOLS_GAME_FILE "sdktools.games.ep2"
1.45 +#else
1.46 + #define SDKTOOLS_GAME_FILE "sdktools.games"
1.47 +#endif
1.48 +
1.49 +/**
1.50 + * @file extension.cpp
1.51 + * @brief Implements SDK Tools extension code.
1.52 + */
1.53 +
1.54 +SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
1.55 +SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int, int);
1.56 +
1.57 +SDKTools g_SdkTools; /**< Global singleton for extension's main interface */
1.58 +IServerGameEnts *gameents = NULL;
1.59 +IEngineTrace *enginetrace = NULL;
1.60 +IEngineSound *engsound = NULL;
1.61 +INetworkStringTableContainer *netstringtables = NULL;
1.62 +IServerPluginHelpers *pluginhelpers = NULL;
1.63 +IBinTools *g_pBinTools = NULL;
1.64 +IGameConfig *g_pGameConf = NULL;
1.65 +IGameHelpers *g_pGameHelpers = NULL;
1.66 +IServerGameClients *serverClients = NULL;
1.67 +IVoiceServer *voiceserver = NULL;
1.68 +IPlayerInfoManager *playerinfomngr = NULL;
1.69 +ICvar *icvar = NULL;
1.70 +IServer *iserver = NULL;
1.71 +SourceHook::CallClass<IVEngineServer> *enginePatch = NULL;
1.72 +SourceHook::CallClass<IEngineSound> *enginesoundPatch = NULL;
1.73 +HandleType_t g_CallHandle = 0;
1.74 +HandleType_t g_TraceHandle = 0;
1.75 +
1.76 +SMEXT_LINK(&g_SdkTools);
1.77 +
1.78 +extern sp_nativeinfo_t g_CallNatives[];
1.79 +extern sp_nativeinfo_t g_TENatives[];
1.80 +extern sp_nativeinfo_t g_TRNatives[];
1.81 +extern sp_nativeinfo_t g_StringTableNatives[];
1.82 +extern sp_nativeinfo_t g_VoiceNatives[];
1.83 +extern sp_nativeinfo_t g_EntInputNatives[];
1.84 +extern sp_nativeinfo_t g_TeamNatives[];
1.85 +
1.86 +bool SDKTools::SDK_OnLoad(char *error, size_t maxlength, bool late)
1.87 +{
1.88 + sharesys->AddDependency(myself, "bintools.ext", true, true);
1.89 + sharesys->AddNatives(myself, g_CallNatives);
1.90 + sharesys->AddNatives(myself, g_Natives);
1.91 + sharesys->AddNatives(myself, g_TENatives);
1.92 + sharesys->AddNatives(myself, g_SoundNatives);
1.93 + sharesys->AddNatives(myself, g_TRNatives);
1.94 + sharesys->AddNatives(myself, g_StringTableNatives);
1.95 + sharesys->AddNatives(myself, g_VoiceNatives);
1.96 + sharesys->AddNatives(myself, g_EntInputNatives);
1.97 + sharesys->AddNatives(myself, g_TeamNatives);
1.98 +
1.99 + SM_GET_IFACE(GAMEHELPERS, g_pGameHelpers);
1.100 +
1.101 + if (!gameconfs->LoadGameConfigFile(SDKTOOLS_GAME_FILE, &g_pGameConf, error, maxlength))
1.102 + {
1.103 + return false;
1.104 + }
1.105 +
1.106 + playerhelpers->AddClientListener(&g_SdkTools);
1.107 + g_CallHandle = handlesys->CreateType("ValveCall", this, 0, NULL, NULL, myself->GetIdentity(), NULL);
1.108 + g_TraceHandle = handlesys->CreateType("TraceRay", this, 0, NULL, NULL, myself->GetIdentity(), NULL);
1.109 +
1.110 +#if defined ORANGEBOX_BUILD
1.111 + g_pCVar = icvar;
1.112 +#endif
1.113 + CONVAR_REGISTER(this);
1.114 +
1.115 + SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
1.116 + SH_ADD_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, gamedll, this, &SDKTools::OnServerActivate, false);
1.117 +
1.118 + playerhelpers->RegisterCommandTargetProcessor(this);
1.119 +
1.120 + MathLib_Init(2.2f, 2.2f, 0.0f, 2);
1.121 +
1.122 + return true;
1.123 +}
1.124 +
1.125 +void SDKTools::OnHandleDestroy(HandleType_t type, void *object)
1.126 +{
1.127 + if (type == g_CallHandle)
1.128 + {
1.129 + ValveCall *v = (ValveCall *)object;
1.130 + delete v;
1.131 + }
1.132 + else if (type == g_TraceHandle)
1.133 + {
1.134 + trace_t *tr = (trace_t *)object;
1.135 + delete tr;
1.136 + }
1.137 +}
1.138 +
1.139 +void SDKTools::SDK_OnUnload()
1.140 +{
1.141 + SourceHook::List<ValveCall *>::iterator iter;
1.142 + for (iter = g_RegCalls.begin();
1.143 + iter != g_RegCalls.end();
1.144 + iter++)
1.145 + {
1.146 + delete (*iter);
1.147 + }
1.148 + g_RegCalls.clear();
1.149 + ShutdownHelpers();
1.150 +
1.151 + if (g_pAcceptInput)
1.152 + {
1.153 + g_pAcceptInput->Destroy();
1.154 + g_pAcceptInput = NULL;
1.155 + }
1.156 +
1.157 + g_TEManager.Shutdown();
1.158 + s_TempEntHooks.Shutdown();
1.159 + s_SoundHooks.Shutdown();
1.160 +
1.161 + gameconfs->CloseGameConfigFile(g_pGameConf);
1.162 + playerhelpers->RemoveClientListener(&g_SdkTools);
1.163 + playerhelpers->UnregisterCommandTargetProcessor(this);
1.164 +
1.165 + SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
1.166 + SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, gamedll, this, &SDKTools::OnServerActivate, false);
1.167 +
1.168 + if (enginePatch)
1.169 + {
1.170 + SH_RELEASE_CALLCLASS(enginePatch);
1.171 + enginePatch = NULL;
1.172 + }
1.173 + if (enginesoundPatch)
1.174 + {
1.175 + SH_RELEASE_CALLCLASS(enginesoundPatch);
1.176 + enginesoundPatch = NULL;
1.177 + }
1.178 +}
1.179 +
1.180 +bool SDKTools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
1.181 +{
1.182 + GET_V_IFACE_ANY(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
1.183 + GET_V_IFACE_ANY(GetEngineFactory, engsound, IEngineSound, IENGINESOUND_SERVER_INTERFACE_VERSION);
1.184 + GET_V_IFACE_ANY(GetEngineFactory, enginetrace, IEngineTrace, INTERFACEVERSION_ENGINETRACE_SERVER);
1.185 + GET_V_IFACE_ANY(GetEngineFactory, netstringtables, INetworkStringTableContainer, INTERFACENAME_NETWORKSTRINGTABLESERVER);
1.186 + GET_V_IFACE_ANY(GetEngineFactory, pluginhelpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
1.187 + GET_V_IFACE_ANY(GetServerFactory, serverClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
1.188 + GET_V_IFACE_ANY(GetEngineFactory, voiceserver, IVoiceServer, INTERFACEVERSION_VOICESERVER);
1.189 + GET_V_IFACE_ANY(GetServerFactory, playerinfomngr, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
1.190 + GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
1.191 +
1.192 + enginePatch = SH_GET_CALLCLASS(engine);
1.193 + enginesoundPatch = SH_GET_CALLCLASS(engsound);
1.194 +
1.195 + return true;
1.196 +}
1.197 +
1.198 +void SDKTools::SDK_OnAllLoaded()
1.199 +{
1.200 + SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
1.201 +
1.202 + if (!g_pBinTools)
1.203 + {
1.204 + return;
1.205 + }
1.206 +
1.207 + g_TEManager.Initialize();
1.208 + s_TempEntHooks.Initialize();
1.209 + s_SoundHooks.Initialize();
1.210 + InitializeValveGlobals();
1.211 + GetIServer();
1.212 +}
1.213 +
1.214 +bool SDKTools::QueryRunning(char *error, size_t maxlength)
1.215 +{
1.216 + SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
1.217 +
1.218 + return true;
1.219 +}
1.220 +
1.221 +bool SDKTools::QueryInterfaceDrop(SMInterface *pInterface)
1.222 +{
1.223 + if (pInterface == g_pBinTools)
1.224 + {
1.225 + return false;
1.226 + }
1.227 +
1.228 + return IExtensionInterface::QueryInterfaceDrop(pInterface);
1.229 +}
1.230 +
1.231 +void SDKTools::NotifyInterfaceDrop(SMInterface *pInterface)
1.232 +{
1.233 + SourceHook::List<ValveCall *>::iterator iter;
1.234 + for (iter = g_RegCalls.begin();
1.235 + iter != g_RegCalls.end();
1.236 + iter++)
1.237 + {
1.238 + delete (*iter);
1.239 + }
1.240 + g_RegCalls.clear();
1.241 + ShutdownHelpers();
1.242 +
1.243 + g_TEManager.Shutdown();
1.244 + s_TempEntHooks.Shutdown();
1.245 +
1.246 + if (g_pAcceptInput)
1.247 + {
1.248 + g_pAcceptInput->Destroy();
1.249 + g_pAcceptInput = NULL;
1.250 + }
1.251 +}
1.252 +
1.253 +bool SDKTools::RegisterConCommandBase(ConCommandBase *pVar)
1.254 +{
1.255 +#if defined METAMOD_PLAPI_VERSION
1.256 + return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
1.257 +#else
1.258 + return g_SMAPI->RegisterConCmdBase(g_PLAPI, pVar);
1.259 +#endif
1.260 +}
1.261 +
1.262 +bool SDKTools::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
1.263 +{
1.264 + const char *name;
1.265 + char key[32];
1.266 + int count, n = 1;
1.267 +
1.268 + if (!(name=g_pGameConf->GetKeyValue("SlapSoundCount")))
1.269 + {
1.270 + RETURN_META_VALUE(MRES_IGNORED, true);
1.271 + }
1.272 +
1.273 + count = atoi(name);
1.274 +
1.275 + while (n <= count)
1.276 + {
1.277 + snprintf(key, sizeof(key), "SlapSound%d", n);
1.278 + if ((name=g_pGameConf->GetKeyValue(key)))
1.279 + {
1.280 + engsound->PrecacheSound(name, true);
1.281 + }
1.282 + n++;
1.283 + }
1.284 +
1.285 + RETURN_META_VALUE(MRES_IGNORED, true);
1.286 +}
1.287 +
1.288 +bool SDKTools::ProcessCommandTarget(cmd_target_info_t *info)
1.289 +{
1.290 + if (strcmp(info->pattern, "@aim") != 0)
1.291 + {
1.292 + return false;
1.293 + }
1.294 +
1.295 + IGamePlayer *pAdmin = info->admin ? playerhelpers->GetGamePlayer(info->admin) : NULL;
1.296 +
1.297 + /* The server can't aim, of course. */
1.298 + if (pAdmin == NULL)
1.299 + {
1.300 + return false;
1.301 + }
1.302 +
1.303 + int player_index;
1.304 + if ((player_index = GetClientAimTarget(pAdmin->GetEdict(), true)) < 1)
1.305 + {
1.306 + info->reason = COMMAND_TARGET_NONE;
1.307 + info->num_targets = 0;
1.308 + return true;
1.309 + }
1.310 +
1.311 + IGamePlayer *pTarget = playerhelpers->GetGamePlayer(player_index);
1.312 +
1.313 + if (pTarget == NULL)
1.314 + {
1.315 + info->reason = COMMAND_TARGET_NONE;
1.316 + info->num_targets = 0;
1.317 + return true;
1.318 + }
1.319 +
1.320 + info->reason = playerhelpers->FilterCommandTarget(pAdmin, pTarget, info->flags);
1.321 + if (info->reason != COMMAND_TARGET_VALID)
1.322 + {
1.323 + info->num_targets = 0;
1.324 + return true;
1.325 + }
1.326 +
1.327 + info->targets[0] = player_index;
1.328 + info->num_targets = 1;
1.329 + info->reason = COMMAND_TARGET_VALID;
1.330 + info->target_name_style = COMMAND_TARGETNAME_RAW;
1.331 + snprintf(info->target_name, info->target_name_maxlength, "%s", pTarget->GetName());
1.332 +
1.333 + return true;
1.334 +}