extensions/sdktools/extension.cpp
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/extensions/sdktools/extension.cpp	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,289 +0,0 @@
     1.4 -/**
     1.5 - * vim: set ts=4 :
     1.6 - * =============================================================================
     1.7 - * SourceMod SDKTools Extension
     1.8 - * Copyright (C) 2004-2007 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 -
    1.42 -#if defined ORANGEBOX_BUILD
    1.43 -	#define SDKTOOLS_GAME_FILE		"sdktools.games.ep2"
    1.44 -#else
    1.45 -	#define SDKTOOLS_GAME_FILE		"sdktools.games"
    1.46 -#endif
    1.47 -
    1.48 -/**
    1.49 - * @file extension.cpp
    1.50 - * @brief Implements SDK Tools extension code.
    1.51 - */
    1.52 -
    1.53 -SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
    1.54 -SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int, int);
    1.55 -
    1.56 -SDKTools g_SdkTools;		/**< Global singleton for extension's main interface */
    1.57 -IServerGameEnts *gameents = NULL;
    1.58 -IEngineTrace *enginetrace = NULL;
    1.59 -IEngineSound *engsound = NULL;
    1.60 -INetworkStringTableContainer *netstringtables = NULL;
    1.61 -IServerPluginHelpers *pluginhelpers = NULL;
    1.62 -IBinTools *g_pBinTools = NULL;
    1.63 -IGameConfig *g_pGameConf = NULL;
    1.64 -IGameHelpers *g_pGameHelpers = NULL;
    1.65 -IServerGameClients *serverClients = NULL;
    1.66 -IVoiceServer *voiceserver = NULL;
    1.67 -IPlayerInfoManager *playerinfomngr = NULL;
    1.68 -ICvar *icvar = NULL;
    1.69 -HandleType_t g_CallHandle = 0;
    1.70 -HandleType_t g_TraceHandle = 0;
    1.71 -
    1.72 -SMEXT_LINK(&g_SdkTools);
    1.73 -
    1.74 -extern sp_nativeinfo_t g_CallNatives[];
    1.75 -extern sp_nativeinfo_t g_TENatives[];
    1.76 -extern sp_nativeinfo_t g_TRNatives[];
    1.77 -extern sp_nativeinfo_t g_StringTableNatives[];
    1.78 -extern sp_nativeinfo_t g_VoiceNatives[];
    1.79 -extern sp_nativeinfo_t g_EntInputNatives[];
    1.80 -extern sp_nativeinfo_t g_TeamNatives[];
    1.81 -
    1.82 -bool SDKTools::SDK_OnLoad(char *error, size_t maxlength, bool late)
    1.83 -{
    1.84 -	sharesys->AddDependency(myself, "bintools.ext", true, true);
    1.85 -	sharesys->AddNatives(myself, g_CallNatives);
    1.86 -	sharesys->AddNatives(myself, g_Natives);
    1.87 -	sharesys->AddNatives(myself, g_TENatives);
    1.88 -	sharesys->AddNatives(myself, g_SoundNatives);
    1.89 -	sharesys->AddNatives(myself, g_TRNatives);
    1.90 -	sharesys->AddNatives(myself, g_StringTableNatives);
    1.91 -	sharesys->AddNatives(myself, g_VoiceNatives);
    1.92 -	sharesys->AddNatives(myself, g_EntInputNatives);
    1.93 -	sharesys->AddNatives(myself, g_TeamNatives);
    1.94 -
    1.95 -	SM_GET_IFACE(GAMEHELPERS, g_pGameHelpers);
    1.96 -
    1.97 -	if (!gameconfs->LoadGameConfigFile(SDKTOOLS_GAME_FILE, &g_pGameConf, error, maxlength))
    1.98 -	{
    1.99 -		return false;
   1.100 -	}
   1.101 -
   1.102 -	playerhelpers->AddClientListener(&g_SdkTools);
   1.103 -	g_CallHandle = handlesys->CreateType("ValveCall", this, 0, NULL, NULL, myself->GetIdentity(), NULL);
   1.104 -	g_TraceHandle = handlesys->CreateType("TraceRay", this, 0, NULL, NULL, myself->GetIdentity(), NULL);
   1.105 -
   1.106 -#if defined ORANGEBOX_BUILD
   1.107 -	g_pCVar = icvar;
   1.108 -#endif
   1.109 -	CONVAR_REGISTER(this);
   1.110 -
   1.111 -	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
   1.112 -	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, gamedll, this, &SDKTools::OnServerActivate, false);
   1.113 -
   1.114 -	playerhelpers->RegisterCommandTargetProcessor(this);
   1.115 -
   1.116 -	MathLib_Init(2.2f, 2.2f, 0.0f, 2);
   1.117 -
   1.118 -	return true;
   1.119 -}
   1.120 -
   1.121 -void SDKTools::OnHandleDestroy(HandleType_t type, void *object)
   1.122 -{
   1.123 -	if (type == g_CallHandle)
   1.124 -	{
   1.125 -		ValveCall *v = (ValveCall *)object;
   1.126 -		delete v;
   1.127 -	}
   1.128 -	else if (type == g_TraceHandle)
   1.129 -	{
   1.130 -		trace_t *tr = (trace_t *)object;
   1.131 -		delete tr;
   1.132 -	}
   1.133 -}
   1.134 -
   1.135 -void SDKTools::SDK_OnUnload()
   1.136 -{
   1.137 -	SourceHook::List<ValveCall *>::iterator iter;
   1.138 -	for (iter = g_RegCalls.begin();
   1.139 -		 iter != g_RegCalls.end();
   1.140 -		 iter++)
   1.141 -	{
   1.142 -		delete (*iter);
   1.143 -	}
   1.144 -	g_RegCalls.clear();
   1.145 -	ShutdownHelpers();
   1.146 -
   1.147 -	if (g_pAcceptInput)
   1.148 -	{
   1.149 -		g_pAcceptInput->Destroy();
   1.150 -		g_pAcceptInput = NULL;
   1.151 -	}
   1.152 -
   1.153 -	g_TEManager.Shutdown();
   1.154 -	s_TempEntHooks.Shutdown();
   1.155 -
   1.156 -	gameconfs->CloseGameConfigFile(g_pGameConf);
   1.157 -	playerhelpers->RemoveClientListener(&g_SdkTools);
   1.158 -	playerhelpers->UnregisterCommandTargetProcessor(this);
   1.159 -
   1.160 -	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
   1.161 -	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, ServerActivate, gamedll, this, &SDKTools::OnServerActivate, false);
   1.162 -}
   1.163 -
   1.164 -bool SDKTools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
   1.165 -{
   1.166 -	GET_V_IFACE_ANY(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
   1.167 -	GET_V_IFACE_ANY(GetEngineFactory, engsound, IEngineSound, IENGINESOUND_SERVER_INTERFACE_VERSION);
   1.168 -	GET_V_IFACE_ANY(GetEngineFactory, enginetrace, IEngineTrace, INTERFACEVERSION_ENGINETRACE_SERVER);
   1.169 -	GET_V_IFACE_ANY(GetEngineFactory, netstringtables, INetworkStringTableContainer, INTERFACENAME_NETWORKSTRINGTABLESERVER);
   1.170 -	GET_V_IFACE_ANY(GetEngineFactory, pluginhelpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
   1.171 -	GET_V_IFACE_ANY(GetServerFactory, serverClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
   1.172 -	GET_V_IFACE_ANY(GetEngineFactory, voiceserver, IVoiceServer, INTERFACEVERSION_VOICESERVER);
   1.173 -	GET_V_IFACE_ANY(GetServerFactory, playerinfomngr, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
   1.174 -	GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
   1.175 -
   1.176 -	return true;
   1.177 -}
   1.178 -
   1.179 -void SDKTools::SDK_OnAllLoaded()
   1.180 -{
   1.181 -	SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
   1.182 -
   1.183 -	g_TEManager.Initialize();
   1.184 -	s_TempEntHooks.Initialize();
   1.185 -	InitializeValveGlobals();
   1.186 -}
   1.187 -
   1.188 -bool SDKTools::QueryRunning(char *error, size_t maxlength)
   1.189 -{
   1.190 -	SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
   1.191 -
   1.192 -	return true;
   1.193 -}
   1.194 -
   1.195 -bool SDKTools::QueryInterfaceDrop(SMInterface *pInterface)
   1.196 -{
   1.197 -	if (pInterface == g_pBinTools)
   1.198 -	{
   1.199 -		return false;
   1.200 -	}
   1.201 -
   1.202 -	return IExtensionInterface::QueryInterfaceDrop(pInterface);
   1.203 -}
   1.204 -
   1.205 -void SDKTools::NotifyInterfaceDrop(SMInterface *pInterface)
   1.206 -{
   1.207 -	SourceHook::List<ValveCall *>::iterator iter;
   1.208 -	for (iter = g_RegCalls.begin();
   1.209 -		iter != g_RegCalls.end();
   1.210 -		iter++)
   1.211 -	{
   1.212 -		delete (*iter);
   1.213 -	}
   1.214 -	g_RegCalls.clear();
   1.215 -	ShutdownHelpers();
   1.216 -
   1.217 -	g_TEManager.Shutdown();
   1.218 -	s_TempEntHooks.Shutdown();
   1.219 -
   1.220 -	if (g_pAcceptInput)
   1.221 -	{
   1.222 -		g_pAcceptInput->Destroy();
   1.223 -		g_pAcceptInput = NULL;
   1.224 -	}
   1.225 -}
   1.226 -
   1.227 -bool SDKTools::RegisterConCommandBase(ConCommandBase *pVar)
   1.228 -{
   1.229 -#if defined METAMOD_PLAPI_VERSION
   1.230 -	return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
   1.231 -#else
   1.232 -	return g_SMAPI->RegisterConCmdBase(g_PLAPI, pVar);
   1.233 -#endif
   1.234 -}
   1.235 -
   1.236 -bool SDKTools::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
   1.237 -{
   1.238 -	const char *name;
   1.239 -	char key[32];
   1.240 -	int count, n = 1;
   1.241 -
   1.242 -	if (!(name=g_pGameConf->GetKeyValue("SlapSoundCount")))
   1.243 -	{
   1.244 -		RETURN_META_VALUE(MRES_IGNORED, true);
   1.245 -	}
   1.246 -
   1.247 -	count = atoi(name);
   1.248 -
   1.249 -	while (n <= count)
   1.250 -	{
   1.251 -		snprintf(key, sizeof(key), "SlapSound%d", n);
   1.252 -		if ((name=g_pGameConf->GetKeyValue(key)))
   1.253 -		{
   1.254 -			engsound->PrecacheSound(name, true);
   1.255 -		}
   1.256 -		n++;
   1.257 -	}
   1.258 -
   1.259 -	RETURN_META_VALUE(MRES_IGNORED, true);
   1.260 -}
   1.261 -
   1.262 -bool SDKTools::ProcessCommandTarget(cmd_target_info_t *info)
   1.263 -{
   1.264 -	if (strcmp(info->pattern, "@aim") != 0)
   1.265 -	{
   1.266 -		return false;
   1.267 -	}
   1.268 -
   1.269 -	IGamePlayer *pAdmin = info->admin ? playerhelpers->GetGamePlayer(info->admin) : NULL;
   1.270 -
   1.271 -	/* The server can't aim, of course. */
   1.272 -	if (pAdmin == NULL)
   1.273 -	{
   1.274 -		return false;
   1.275 -	}
   1.276 -
   1.277 -	int player_index;
   1.278 -	if ((player_index = GetClientAimTarget(pAdmin->GetEdict(), true)) < 1)
   1.279 -	{
   1.280 -		return false;
   1.281 -	}
   1.282 -
   1.283 -	IGamePlayer *pTarget = playerhelpers->GetGamePlayer(info->admin);
   1.284 -
   1.285 -	info->targets[0] = player_index;
   1.286 -	info->num_targets = 1;
   1.287 -	info->reason = COMMAND_TARGET_VALID;
   1.288 -	info->target_name_style = COMMAND_TARGETNAME_RAW;
   1.289 -	snprintf(info->target_name, info->target_name_maxlength, "%s", pTarget->GetName());
   1.290 -
   1.291 -	return true;
   1.292 -}