extensions/sdktools/extension.cpp
author David Anderson <dvander@alliedmods.net>
Wed Feb 25 20:07:12 2009 -0500 (2009-02-25)
changeset 2607 f3b8bbe17f85
parent 2306 9150da197062
child 2400 76b7b4575e21
permissions -rw-r--r--
Backed out changeset fa286305bd3f
     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 	HandleError err;
    86 
    87 	if (!gameconfs->LoadGameConfigFile(SDKTOOLS_GAME_FILE, &g_pGameConf, error, maxlength))
    88 	{
    89 		return false;
    90 	}
    91 
    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);
   103 
   104 	SM_GET_IFACE(GAMEHELPERS, g_pGameHelpers);
   105 
   106 	playerhelpers->AddClientListener(&g_SdkTools);
   107 	g_CallHandle = handlesys->CreateType("ValveCall", this, 0, NULL, NULL, myself->GetIdentity(), &err);
   108 	if (g_CallHandle == 0)
   109 	{
   110 		snprintf(error, maxlength, "Could not create call handle type (err: %d)", err);	
   111 		return false;
   112 	}
   113 
   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)
   121 	{
   122 		handlesys->RemoveType(g_CallHandle, myself->GetIdentity());
   123 		g_CallHandle = 0;
   124 		snprintf(error, maxlength, "Could not create traceray handle type (err: %d)", err);
   125 		return false;
   126 	}
   127 
   128 #if defined ORANGEBOX_BUILD
   129 	g_pCVar = icvar;
   130 #endif
   131 	CONVAR_REGISTER(this);
   132 
   133 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
   134 
   135 	playerhelpers->RegisterCommandTargetProcessor(this);
   136 
   137 	MathLib_Init(2.2f, 2.2f, 0.0f, 2);
   138 
   139 	spengine = g_pSM->GetScriptingEngine();
   140 
   141 	plsys->AddPluginsListener(&g_OutputManager);
   142 
   143 	g_OutputManager.Init();
   144 
   145 	VoiceInit();
   146 
   147 	return true;
   148 }
   149 
   150 void SDKTools::OnHandleDestroy(HandleType_t type, void *object)
   151 {
   152 	if (type == g_CallHandle)
   153 	{
   154 		ValveCall *v = (ValveCall *)object;
   155 		delete v;
   156 	}
   157 	else if (type == g_TraceHandle)
   158 	{
   159 		trace_t *tr = (trace_t *)object;
   160 		delete tr;
   161 	}
   162 }
   163 
   164 void SDKTools::SDK_OnUnload()
   165 {
   166 	SourceHook::List<ValveCall *>::iterator iter;
   167 	for (iter = g_RegCalls.begin();
   168 		 iter != g_RegCalls.end();
   169 		 iter++)
   170 	{
   171 		delete (*iter);
   172 	}
   173 	g_RegCalls.clear();
   174 	ShutdownHelpers();
   175 
   176 	if (g_pAcceptInput)
   177 	{
   178 		g_pAcceptInput->Destroy();
   179 		g_pAcceptInput = NULL;
   180 	}
   181 
   182 	g_TEManager.Shutdown();
   183 	s_TempEntHooks.Shutdown();
   184 	s_SoundHooks.Shutdown();
   185 
   186 	gameconfs->CloseGameConfigFile(g_pGameConf);
   187 	playerhelpers->RemoveClientListener(&g_SdkTools);
   188 	playerhelpers->UnregisterCommandTargetProcessor(this);
   189 	plsys->RemovePluginsListener(&g_OutputManager);
   190 
   191 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
   192 
   193 	if (enginePatch)
   194 	{
   195 		SH_RELEASE_CALLCLASS(enginePatch);
   196 		enginePatch = NULL;
   197 	}
   198 	if (enginesoundPatch)
   199 	{
   200 		SH_RELEASE_CALLCLASS(enginesoundPatch);
   201 		enginesoundPatch = NULL;
   202 	}
   203 
   204 	bool err;
   205 	if (g_CallHandle != 0)
   206 	{
   207 		if ((err = handlesys->RemoveType(g_CallHandle, myself->GetIdentity())) != true)
   208 		{
   209 			g_pSM->LogError(myself, "Could not remove call handle (type=%x, err=%d)", g_CallHandle, err);
   210 		}
   211 	}
   212 
   213 	if (g_TraceHandle != 0)
   214 	{
   215 		if ((err = handlesys->RemoveType(g_TraceHandle, myself->GetIdentity())) != true)
   216 		{
   217 			g_pSM->LogError(myself, "Could not remove trace handle (type=%x, err=%d)", g_TraceHandle, err);
   218 		}
   219 	}
   220 }
   221 
   222 bool SDKTools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
   223 {
   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);
   233 
   234 	enginePatch = SH_GET_CALLCLASS(engine);
   235 	enginesoundPatch = SH_GET_CALLCLASS(engsound);
   236 
   237 	return true;
   238 }
   239 
   240 void SDKTools::SDK_OnAllLoaded()
   241 {
   242 	SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
   243 
   244 	if (!g_pBinTools)
   245 	{
   246 		return;
   247 	}
   248 
   249 	g_TEManager.Initialize();
   250 	s_TempEntHooks.Initialize();
   251 	s_SoundHooks.Initialize();
   252 	InitializeValveGlobals();
   253 	GetIServer();
   254 }
   255 
   256 bool SDKTools::QueryRunning(char *error, size_t maxlength)
   257 {
   258 	SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
   259 
   260 	return true;
   261 }
   262 
   263 bool SDKTools::QueryInterfaceDrop(SMInterface *pInterface)
   264 {
   265 	if (pInterface == g_pBinTools)
   266 	{
   267 		return false;
   268 	}
   269 
   270 	return IExtensionInterface::QueryInterfaceDrop(pInterface);
   271 }
   272 
   273 void SDKTools::NotifyInterfaceDrop(SMInterface *pInterface)
   274 {
   275 	SourceHook::List<ValveCall *>::iterator iter;
   276 	for (iter = g_RegCalls.begin();
   277 		iter != g_RegCalls.end();
   278 		iter++)
   279 	{
   280 		delete (*iter);
   281 	}
   282 	g_RegCalls.clear();
   283 	ShutdownHelpers();
   284 
   285 	g_TEManager.Shutdown();
   286 	s_TempEntHooks.Shutdown();
   287 
   288 	if (g_pAcceptInput)
   289 	{
   290 		g_pAcceptInput->Destroy();
   291 		g_pAcceptInput = NULL;
   292 	}
   293 }
   294 
   295 bool SDKTools::RegisterConCommandBase(ConCommandBase *pVar)
   296 {
   297 #if defined METAMOD_PLAPI_VERSION
   298 	return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
   299 #else
   300 	return g_SMAPI->RegisterConCmdBase(g_PLAPI, pVar);
   301 #endif
   302 }
   303 
   304 bool SDKTools::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
   305 {
   306 	const char *name;
   307 	char key[32];
   308 	int count, n = 1;
   309 
   310 	if (!(name=g_pGameConf->GetKeyValue("SlapSoundCount")))
   311 	{
   312 		RETURN_META_VALUE(MRES_IGNORED, true);
   313 	}
   314 
   315 	count = atoi(name);
   316 
   317 	while (n <= count)
   318 	{
   319 		snprintf(key, sizeof(key), "SlapSound%d", n);
   320 		if ((name=g_pGameConf->GetKeyValue(key)))
   321 		{
   322 			engsound->PrecacheSound(name, true);
   323 		}
   324 		n++;
   325 	}
   326 
   327 	RETURN_META_VALUE(MRES_IGNORED, true);
   328 }
   329 
   330 bool SDKTools::ProcessCommandTarget(cmd_target_info_t *info)
   331 {
   332 	if (strcmp(info->pattern, "@aim") != 0)
   333 	{
   334 		return false;
   335 	}
   336 
   337 	IGamePlayer *pAdmin = info->admin ? playerhelpers->GetGamePlayer(info->admin) : NULL;
   338 
   339 	/* The server can't aim, of course. */
   340 	if (pAdmin == NULL)
   341 	{
   342 		return false;
   343 	}
   344 
   345 	int player_index;
   346 	if ((player_index = GetClientAimTarget(pAdmin->GetEdict(), true)) < 1)
   347 	{
   348 		info->reason = COMMAND_TARGET_NONE;
   349 		info->num_targets = 0;
   350 		return true;
   351 	}
   352 
   353 	IGamePlayer *pTarget = playerhelpers->GetGamePlayer(player_index);
   354 
   355 	if (pTarget == NULL)
   356 	{
   357 		info->reason = COMMAND_TARGET_NONE;
   358 		info->num_targets = 0;
   359 		return true;
   360 	}
   361 
   362 	info->reason = playerhelpers->FilterCommandTarget(pAdmin, pTarget, info->flags);
   363 	if (info->reason != COMMAND_TARGET_VALID)
   364 	{
   365 		info->num_targets = 0;
   366 		return true;
   367 	}
   368 
   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());
   374 
   375 	return true;
   376 }