1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sdktools/extension.cpp Sun Jul 06 04:49:55 2008 +0000
1.3 @@ -0,0 +1,345 @@
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 +#include "output.h"
1.43 +
1.44 +#if defined ORANGEBOX_BUILD
1.45 + #define SDKTOOLS_GAME_FILE "sdktools.games.ep2"
1.46 +#else
1.47 + #define SDKTOOLS_GAME_FILE "sdktools.games"
1.48 +#endif
1.49 +
1.50 +/**
1.51 + * @file extension.cpp
1.52 + * @brief Implements SDK Tools extension code.
1.53 + */
1.54 +
1.55 +SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
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 + if (!gameconfs->LoadGameConfigFile(SDKTOOLS_GAME_FILE, &g_pGameConf, error, maxlength))
1.89 + {
1.90 + return false;
1.91 + }
1.92 +
1.93 + sharesys->AddDependency(myself, "bintools.ext", true, true);
1.94 + sharesys->AddNatives(myself, g_CallNatives);
1.95 + sharesys->AddNatives(myself, g_Natives);
1.96 + sharesys->AddNatives(myself, g_TENatives);
1.97 + sharesys->AddNatives(myself, g_SoundNatives);
1.98 + sharesys->AddNatives(myself, g_TRNatives);
1.99 + sharesys->AddNatives(myself, g_StringTableNatives);
1.100 + sharesys->AddNatives(myself, g_VoiceNatives);
1.101 + sharesys->AddNatives(myself, g_EntInputNatives);
1.102 + sharesys->AddNatives(myself, g_TeamNatives);
1.103 + sharesys->AddNatives(myself, g_EntOutputNatives);
1.104 +
1.105 + SM_GET_IFACE(GAMEHELPERS, g_pGameHelpers);
1.106 +
1.107 + playerhelpers->AddClientListener(&g_SdkTools);
1.108 + g_CallHandle = handlesys->CreateType("ValveCall", this, 0, NULL, NULL, myself->GetIdentity(), NULL);
1.109 +
1.110 + TypeAccess TraceAccess;
1.111 + handlesys->InitAccessDefaults(&TraceAccess, NULL);
1.112 + TraceAccess.ident = myself->GetIdentity();
1.113 + TraceAccess.access[HTypeAccess_Create] = true;
1.114 + TraceAccess.access[HTypeAccess_Inherit] = true;
1.115 + g_TraceHandle = handlesys->CreateType("TraceRay", this, 0, &TraceAccess, NULL, myself->GetIdentity(), NULL);
1.116 +
1.117 +#if defined ORANGEBOX_BUILD
1.118 + g_pCVar = icvar;
1.119 +#endif
1.120 + CONVAR_REGISTER(this);
1.121 +
1.122 + SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
1.123 +
1.124 + playerhelpers->RegisterCommandTargetProcessor(this);
1.125 +
1.126 + MathLib_Init(2.2f, 2.2f, 0.0f, 2);
1.127 +
1.128 + spengine = g_pSM->GetScriptingEngine();
1.129 +
1.130 + plsys->AddPluginsListener(&g_OutputManager);
1.131 +
1.132 + g_OutputManager.Init();
1.133 +
1.134 + VoiceInit();
1.135 +
1.136 + return true;
1.137 +}
1.138 +
1.139 +void SDKTools::OnHandleDestroy(HandleType_t type, void *object)
1.140 +{
1.141 + if (type == g_CallHandle)
1.142 + {
1.143 + ValveCall *v = (ValveCall *)object;
1.144 + delete v;
1.145 + }
1.146 + else if (type == g_TraceHandle)
1.147 + {
1.148 + trace_t *tr = (trace_t *)object;
1.149 + delete tr;
1.150 + }
1.151 +}
1.152 +
1.153 +void SDKTools::SDK_OnUnload()
1.154 +{
1.155 + SourceHook::List<ValveCall *>::iterator iter;
1.156 + for (iter = g_RegCalls.begin();
1.157 + iter != g_RegCalls.end();
1.158 + iter++)
1.159 + {
1.160 + delete (*iter);
1.161 + }
1.162 + g_RegCalls.clear();
1.163 + ShutdownHelpers();
1.164 +
1.165 + if (g_pAcceptInput)
1.166 + {
1.167 + g_pAcceptInput->Destroy();
1.168 + g_pAcceptInput = NULL;
1.169 + }
1.170 +
1.171 + g_TEManager.Shutdown();
1.172 + s_TempEntHooks.Shutdown();
1.173 + s_SoundHooks.Shutdown();
1.174 +
1.175 + gameconfs->CloseGameConfigFile(g_pGameConf);
1.176 + playerhelpers->RemoveClientListener(&g_SdkTools);
1.177 + playerhelpers->UnregisterCommandTargetProcessor(this);
1.178 + plsys->RemovePluginsListener(&g_OutputManager);
1.179 +
1.180 + SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
1.181 +
1.182 + if (enginePatch)
1.183 + {
1.184 + SH_RELEASE_CALLCLASS(enginePatch);
1.185 + enginePatch = NULL;
1.186 + }
1.187 + if (enginesoundPatch)
1.188 + {
1.189 + SH_RELEASE_CALLCLASS(enginesoundPatch);
1.190 + enginesoundPatch = NULL;
1.191 + }
1.192 +}
1.193 +
1.194 +bool SDKTools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
1.195 +{
1.196 + GET_V_IFACE_ANY(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
1.197 + GET_V_IFACE_ANY(GetEngineFactory, engsound, IEngineSound, IENGINESOUND_SERVER_INTERFACE_VERSION);
1.198 + GET_V_IFACE_ANY(GetEngineFactory, enginetrace, IEngineTrace, INTERFACEVERSION_ENGINETRACE_SERVER);
1.199 + GET_V_IFACE_ANY(GetEngineFactory, netstringtables, INetworkStringTableContainer, INTERFACENAME_NETWORKSTRINGTABLESERVER);
1.200 + GET_V_IFACE_ANY(GetEngineFactory, pluginhelpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
1.201 + GET_V_IFACE_ANY(GetServerFactory, serverClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
1.202 + GET_V_IFACE_ANY(GetEngineFactory, voiceserver, IVoiceServer, INTERFACEVERSION_VOICESERVER);
1.203 + GET_V_IFACE_ANY(GetServerFactory, playerinfomngr, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
1.204 + GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
1.205 +
1.206 + enginePatch = SH_GET_CALLCLASS(engine);
1.207 + enginesoundPatch = SH_GET_CALLCLASS(engsound);
1.208 +
1.209 + return true;
1.210 +}
1.211 +
1.212 +void SDKTools::SDK_OnAllLoaded()
1.213 +{
1.214 + SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
1.215 +
1.216 + if (!g_pBinTools)
1.217 + {
1.218 + return;
1.219 + }
1.220 +
1.221 + g_TEManager.Initialize();
1.222 + s_TempEntHooks.Initialize();
1.223 + s_SoundHooks.Initialize();
1.224 + InitializeValveGlobals();
1.225 + GetIServer();
1.226 +}
1.227 +
1.228 +bool SDKTools::QueryRunning(char *error, size_t maxlength)
1.229 +{
1.230 + SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
1.231 +
1.232 + return true;
1.233 +}
1.234 +
1.235 +bool SDKTools::QueryInterfaceDrop(SMInterface *pInterface)
1.236 +{
1.237 + if (pInterface == g_pBinTools)
1.238 + {
1.239 + return false;
1.240 + }
1.241 +
1.242 + return IExtensionInterface::QueryInterfaceDrop(pInterface);
1.243 +}
1.244 +
1.245 +void SDKTools::NotifyInterfaceDrop(SMInterface *pInterface)
1.246 +{
1.247 + SourceHook::List<ValveCall *>::iterator iter;
1.248 + for (iter = g_RegCalls.begin();
1.249 + iter != g_RegCalls.end();
1.250 + iter++)
1.251 + {
1.252 + delete (*iter);
1.253 + }
1.254 + g_RegCalls.clear();
1.255 + ShutdownHelpers();
1.256 +
1.257 + g_TEManager.Shutdown();
1.258 + s_TempEntHooks.Shutdown();
1.259 +
1.260 + if (g_pAcceptInput)
1.261 + {
1.262 + g_pAcceptInput->Destroy();
1.263 + g_pAcceptInput = NULL;
1.264 + }
1.265 +}
1.266 +
1.267 +bool SDKTools::RegisterConCommandBase(ConCommandBase *pVar)
1.268 +{
1.269 +#if defined METAMOD_PLAPI_VERSION
1.270 + return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
1.271 +#else
1.272 + return g_SMAPI->RegisterConCmdBase(g_PLAPI, pVar);
1.273 +#endif
1.274 +}
1.275 +
1.276 +bool SDKTools::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
1.277 +{
1.278 + const char *name;
1.279 + char key[32];
1.280 + int count, n = 1;
1.281 +
1.282 + if (!(name=g_pGameConf->GetKeyValue("SlapSoundCount")))
1.283 + {
1.284 + RETURN_META_VALUE(MRES_IGNORED, true);
1.285 + }
1.286 +
1.287 + count = atoi(name);
1.288 +
1.289 + while (n <= count)
1.290 + {
1.291 + snprintf(key, sizeof(key), "SlapSound%d", n);
1.292 + if ((name=g_pGameConf->GetKeyValue(key)))
1.293 + {
1.294 + engsound->PrecacheSound(name, true);
1.295 + }
1.296 + n++;
1.297 + }
1.298 +
1.299 + RETURN_META_VALUE(MRES_IGNORED, true);
1.300 +}
1.301 +
1.302 +bool SDKTools::ProcessCommandTarget(cmd_target_info_t *info)
1.303 +{
1.304 + if (strcmp(info->pattern, "@aim") != 0)
1.305 + {
1.306 + return false;
1.307 + }
1.308 +
1.309 + IGamePlayer *pAdmin = info->admin ? playerhelpers->GetGamePlayer(info->admin) : NULL;
1.310 +
1.311 + /* The server can't aim, of course. */
1.312 + if (pAdmin == NULL)
1.313 + {
1.314 + return false;
1.315 + }
1.316 +
1.317 + int player_index;
1.318 + if ((player_index = GetClientAimTarget(pAdmin->GetEdict(), true)) < 1)
1.319 + {
1.320 + info->reason = COMMAND_TARGET_NONE;
1.321 + info->num_targets = 0;
1.322 + return true;
1.323 + }
1.324 +
1.325 + IGamePlayer *pTarget = playerhelpers->GetGamePlayer(player_index);
1.326 +
1.327 + if (pTarget == NULL)
1.328 + {
1.329 + info->reason = COMMAND_TARGET_NONE;
1.330 + info->num_targets = 0;
1.331 + return true;
1.332 + }
1.333 +
1.334 + info->reason = playerhelpers->FilterCommandTarget(pAdmin, pTarget, info->flags);
1.335 + if (info->reason != COMMAND_TARGET_VALID)
1.336 + {
1.337 + info->num_targets = 0;
1.338 + return true;
1.339 + }
1.340 +
1.341 + info->targets[0] = player_index;
1.342 + info->num_targets = 1;
1.343 + info->reason = COMMAND_TARGET_VALID;
1.344 + info->target_name_style = COMMAND_TARGETNAME_RAW;
1.345 + snprintf(info->target_name, info->target_name_maxlength, "%s", pTarget->GetName());
1.346 +
1.347 + return true;
1.348 +}