extensions/sdktools/extension.cpp
author Matt Woodrow <pred@alliedmods.net>
Sun Jul 06 03:30:06 2008 +0000 (2008-07-06)
changeset 2165 ca3757e676f6
parent 2124 e3665367ce8d
child 2289 44045ead61cd
permissions -rw-r--r--
Fixed requiring dependencies at the wrong time.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod SDKTools Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "extension.h"
    33 #include "vcallbuilder.h"
    34 #include "vnatives.h"
    35 #include "vhelpers.h"
    36 #include "vglobals.h"
    37 #include "tempents.h"
    38 #include "vsound.h"
    39 #include "output.h"
    40 
    41 #if defined ORANGEBOX_BUILD
    42 	#define SDKTOOLS_GAME_FILE		"sdktools.games.ep2"
    43 #else
    44 	#define SDKTOOLS_GAME_FILE		"sdktools.games"
    45 #endif
    46 
    47 /**
    48  * @file extension.cpp
    49  * @brief Implements SDK Tools extension code.
    50  */
    51 
    52 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
    53 
    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;
    66 ICvar *icvar = 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;
    72 
    73 SMEXT_LINK(&g_SdkTools);
    74 
    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[];
    82 
    83 bool SDKTools::SDK_OnLoad(char *error, size_t maxlength, bool late)
    84 {
    85 	if (!gameconfs->LoadGameConfigFile(SDKTOOLS_GAME_FILE, &g_pGameConf, error, maxlength))
    86 	{
    87 		return false;
    88 	}
    89 
    90 	sharesys->AddDependency(myself, "bintools.ext", true, true);
    91 	sharesys->AddNatives(myself, g_CallNatives);
    92 	sharesys->AddNatives(myself, g_Natives);
    93 	sharesys->AddNatives(myself, g_TENatives);
    94 	sharesys->AddNatives(myself, g_SoundNatives);
    95 	sharesys->AddNatives(myself, g_TRNatives);
    96 	sharesys->AddNatives(myself, g_StringTableNatives);
    97 	sharesys->AddNatives(myself, g_VoiceNatives);
    98 	sharesys->AddNatives(myself, g_EntInputNatives);
    99 	sharesys->AddNatives(myself, g_TeamNatives);
   100 	sharesys->AddNatives(myself, g_EntOutputNatives);
   101 
   102 	SM_GET_IFACE(GAMEHELPERS, g_pGameHelpers);
   103 
   104 	playerhelpers->AddClientListener(&g_SdkTools);
   105 	g_CallHandle = handlesys->CreateType("ValveCall", this, 0, NULL, NULL, myself->GetIdentity(), NULL);
   106 
   107 	TypeAccess TraceAccess;
   108 	handlesys->InitAccessDefaults(&TraceAccess, NULL);
   109 	TraceAccess.ident = myself->GetIdentity();
   110 	TraceAccess.access[HTypeAccess_Create] = true;
   111 	TraceAccess.access[HTypeAccess_Inherit] = true;
   112 	g_TraceHandle = handlesys->CreateType("TraceRay", this, 0, &TraceAccess, NULL, myself->GetIdentity(), NULL);
   113 
   114 #if defined ORANGEBOX_BUILD
   115 	g_pCVar = icvar;
   116 #endif
   117 	CONVAR_REGISTER(this);
   118 
   119 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
   120 
   121 	playerhelpers->RegisterCommandTargetProcessor(this);
   122 
   123 	MathLib_Init(2.2f, 2.2f, 0.0f, 2);
   124 
   125 	spengine = g_pSM->GetScriptingEngine();
   126 
   127 	plsys->AddPluginsListener(&g_OutputManager);
   128 
   129 	g_OutputManager.Init();
   130 
   131 	VoiceInit();
   132 
   133 	return true;
   134 }
   135 
   136 void SDKTools::OnHandleDestroy(HandleType_t type, void *object)
   137 {
   138 	if (type == g_CallHandle)
   139 	{
   140 		ValveCall *v = (ValveCall *)object;
   141 		delete v;
   142 	}
   143 	else if (type == g_TraceHandle)
   144 	{
   145 		trace_t *tr = (trace_t *)object;
   146 		delete tr;
   147 	}
   148 }
   149 
   150 void SDKTools::SDK_OnUnload()
   151 {
   152 	SourceHook::List<ValveCall *>::iterator iter;
   153 	for (iter = g_RegCalls.begin();
   154 		 iter != g_RegCalls.end();
   155 		 iter++)
   156 	{
   157 		delete (*iter);
   158 	}
   159 	g_RegCalls.clear();
   160 	ShutdownHelpers();
   161 
   162 	if (g_pAcceptInput)
   163 	{
   164 		g_pAcceptInput->Destroy();
   165 		g_pAcceptInput = NULL;
   166 	}
   167 
   168 	g_TEManager.Shutdown();
   169 	s_TempEntHooks.Shutdown();
   170 	s_SoundHooks.Shutdown();
   171 
   172 	gameconfs->CloseGameConfigFile(g_pGameConf);
   173 	playerhelpers->RemoveClientListener(&g_SdkTools);
   174 	playerhelpers->UnregisterCommandTargetProcessor(this);
   175 	plsys->RemovePluginsListener(&g_OutputManager);
   176 
   177 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
   178 
   179 	if (enginePatch)
   180 	{
   181 		SH_RELEASE_CALLCLASS(enginePatch);
   182 		enginePatch = NULL;
   183 	}
   184 	if (enginesoundPatch)
   185 	{
   186 		SH_RELEASE_CALLCLASS(enginesoundPatch);
   187 		enginesoundPatch = NULL;
   188 	}
   189 }
   190 
   191 bool SDKTools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
   192 {
   193 	GET_V_IFACE_ANY(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
   194 	GET_V_IFACE_ANY(GetEngineFactory, engsound, IEngineSound, IENGINESOUND_SERVER_INTERFACE_VERSION);
   195 	GET_V_IFACE_ANY(GetEngineFactory, enginetrace, IEngineTrace, INTERFACEVERSION_ENGINETRACE_SERVER);
   196 	GET_V_IFACE_ANY(GetEngineFactory, netstringtables, INetworkStringTableContainer, INTERFACENAME_NETWORKSTRINGTABLESERVER);
   197 	GET_V_IFACE_ANY(GetEngineFactory, pluginhelpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
   198 	GET_V_IFACE_ANY(GetServerFactory, serverClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
   199 	GET_V_IFACE_ANY(GetEngineFactory, voiceserver, IVoiceServer, INTERFACEVERSION_VOICESERVER);
   200 	GET_V_IFACE_ANY(GetServerFactory, playerinfomngr, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
   201 	GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
   202 
   203 	enginePatch = SH_GET_CALLCLASS(engine);
   204 	enginesoundPatch = SH_GET_CALLCLASS(engsound);
   205 
   206 	return true;
   207 }
   208 
   209 void SDKTools::SDK_OnAllLoaded()
   210 {
   211 	SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
   212 
   213 	if (!g_pBinTools)
   214 	{
   215 		return;
   216 	}
   217 
   218 	g_TEManager.Initialize();
   219 	s_TempEntHooks.Initialize();
   220 	s_SoundHooks.Initialize();
   221 	InitializeValveGlobals();
   222 	GetIServer();
   223 }
   224 
   225 bool SDKTools::QueryRunning(char *error, size_t maxlength)
   226 {
   227 	SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
   228 
   229 	return true;
   230 }
   231 
   232 bool SDKTools::QueryInterfaceDrop(SMInterface *pInterface)
   233 {
   234 	if (pInterface == g_pBinTools)
   235 	{
   236 		return false;
   237 	}
   238 
   239 	return IExtensionInterface::QueryInterfaceDrop(pInterface);
   240 }
   241 
   242 void SDKTools::NotifyInterfaceDrop(SMInterface *pInterface)
   243 {
   244 	SourceHook::List<ValveCall *>::iterator iter;
   245 	for (iter = g_RegCalls.begin();
   246 		iter != g_RegCalls.end();
   247 		iter++)
   248 	{
   249 		delete (*iter);
   250 	}
   251 	g_RegCalls.clear();
   252 	ShutdownHelpers();
   253 
   254 	g_TEManager.Shutdown();
   255 	s_TempEntHooks.Shutdown();
   256 
   257 	if (g_pAcceptInput)
   258 	{
   259 		g_pAcceptInput->Destroy();
   260 		g_pAcceptInput = NULL;
   261 	}
   262 }
   263 
   264 bool SDKTools::RegisterConCommandBase(ConCommandBase *pVar)
   265 {
   266 #if defined METAMOD_PLAPI_VERSION
   267 	return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
   268 #else
   269 	return g_SMAPI->RegisterConCmdBase(g_PLAPI, pVar);
   270 #endif
   271 }
   272 
   273 bool SDKTools::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
   274 {
   275 	const char *name;
   276 	char key[32];
   277 	int count, n = 1;
   278 
   279 	if (!(name=g_pGameConf->GetKeyValue("SlapSoundCount")))
   280 	{
   281 		RETURN_META_VALUE(MRES_IGNORED, true);
   282 	}
   283 
   284 	count = atoi(name);
   285 
   286 	while (n <= count)
   287 	{
   288 		snprintf(key, sizeof(key), "SlapSound%d", n);
   289 		if ((name=g_pGameConf->GetKeyValue(key)))
   290 		{
   291 			engsound->PrecacheSound(name, true);
   292 		}
   293 		n++;
   294 	}
   295 
   296 	RETURN_META_VALUE(MRES_IGNORED, true);
   297 }
   298 
   299 bool SDKTools::ProcessCommandTarget(cmd_target_info_t *info)
   300 {
   301 	if (strcmp(info->pattern, "@aim") != 0)
   302 	{
   303 		return false;
   304 	}
   305 
   306 	IGamePlayer *pAdmin = info->admin ? playerhelpers->GetGamePlayer(info->admin) : NULL;
   307 
   308 	/* The server can't aim, of course. */
   309 	if (pAdmin == NULL)
   310 	{
   311 		return false;
   312 	}
   313 
   314 	int player_index;
   315 	if ((player_index = GetClientAimTarget(pAdmin->GetEdict(), true)) < 1)
   316 	{
   317 		info->reason = COMMAND_TARGET_NONE;
   318 		info->num_targets = 0;
   319 		return true;
   320 	}
   321 
   322 	IGamePlayer *pTarget = playerhelpers->GetGamePlayer(player_index);
   323 
   324 	if (pTarget == NULL)
   325 	{
   326 		info->reason = COMMAND_TARGET_NONE;
   327 		info->num_targets = 0;
   328 		return true;
   329 	}
   330 
   331 	info->reason = playerhelpers->FilterCommandTarget(pAdmin, pTarget, info->flags);
   332 	if (info->reason != COMMAND_TARGET_VALID)
   333 	{
   334 		info->num_targets = 0;
   335 		return true;
   336 	}
   337 
   338 	info->targets[0] = player_index;
   339 	info->num_targets = 1;
   340 	info->reason = COMMAND_TARGET_VALID;
   341 	info->target_name_style = COMMAND_TARGETNAME_RAW;
   342 	snprintf(info->target_name, info->target_name_maxlength, "%s", pTarget->GetName());
   343 
   344 	return true;
   345 }