extensions/sdktools/extension.cpp
author Matt Woodrow <pred@alliedmods.net>
Sun Jun 29 05:04:41 2008 +0000 (2008-06-29)
changeset 2124 e3665367ce8d
parent 2116 0516892817e1
child 2289 44045ead61cd
permissions -rw-r--r--
Single most annoying and hard to find bug ive ever come across. I'd like to thank MSVC for not firing the one breakpoint I actually needed. Oh and a possible fix to sdktools.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod SDKTools Extension
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include "extension.h"
ds@1952
    33
#include "vcallbuilder.h"
ds@1952
    34
#include "vnatives.h"
ds@1952
    35
#include "vhelpers.h"
ds@1952
    36
#include "vglobals.h"
ds@1952
    37
#include "tempents.h"
ds@1952
    38
#include "vsound.h"
pred@1958
    39
#include "output.h"
ds@1952
    40
ds@1952
    41
#if defined ORANGEBOX_BUILD
ds@1952
    42
	#define SDKTOOLS_GAME_FILE		"sdktools.games.ep2"
ds@1952
    43
#else
ds@1952
    44
	#define SDKTOOLS_GAME_FILE		"sdktools.games"
ds@1952
    45
#endif
ds@1952
    46
ds@1952
    47
/**
ds@1952
    48
 * @file extension.cpp
ds@1952
    49
 * @brief Implements SDK Tools extension code.
ds@1952
    50
 */
ds@1952
    51
ds@1952
    52
SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
ds@1952
    53
ds@1952
    54
SDKTools g_SdkTools;		/**< Global singleton for extension's main interface */
ds@1952
    55
IServerGameEnts *gameents = NULL;
ds@1952
    56
IEngineTrace *enginetrace = NULL;
ds@1952
    57
IEngineSound *engsound = NULL;
ds@1952
    58
INetworkStringTableContainer *netstringtables = NULL;
ds@1952
    59
IServerPluginHelpers *pluginhelpers = NULL;
ds@1952
    60
IBinTools *g_pBinTools = NULL;
ds@1952
    61
IGameConfig *g_pGameConf = NULL;
ds@1952
    62
IGameHelpers *g_pGameHelpers = NULL;
ds@1952
    63
IServerGameClients *serverClients = NULL;
ds@1952
    64
IVoiceServer *voiceserver = NULL;
ds@1952
    65
IPlayerInfoManager *playerinfomngr = NULL;
ds@1952
    66
ICvar *icvar = NULL;
ds@1952
    67
IServer *iserver = NULL;
ds@1952
    68
SourceHook::CallClass<IVEngineServer> *enginePatch = NULL;
ds@1952
    69
SourceHook::CallClass<IEngineSound> *enginesoundPatch = NULL;
ds@1952
    70
HandleType_t g_CallHandle = 0;
ds@1952
    71
HandleType_t g_TraceHandle = 0;
ds@1952
    72
ds@1952
    73
SMEXT_LINK(&g_SdkTools);
ds@1952
    74
ds@1952
    75
extern sp_nativeinfo_t g_CallNatives[];
ds@1952
    76
extern sp_nativeinfo_t g_TENatives[];
ds@1952
    77
extern sp_nativeinfo_t g_TRNatives[];
ds@1952
    78
extern sp_nativeinfo_t g_StringTableNatives[];
ds@1952
    79
extern sp_nativeinfo_t g_VoiceNatives[];
ds@1952
    80
extern sp_nativeinfo_t g_EntInputNatives[];
ds@1952
    81
extern sp_nativeinfo_t g_TeamNatives[];
ds@1952
    82
ds@1952
    83
bool SDKTools::SDK_OnLoad(char *error, size_t maxlength, bool late)
ds@1952
    84
{
pred@2124
    85
	if (!gameconfs->LoadGameConfigFile(SDKTOOLS_GAME_FILE, &g_pGameConf, error, maxlength))
pred@2124
    86
	{
pred@2124
    87
		return false;
pred@2124
    88
	}
pred@2124
    89
ds@1952
    90
	sharesys->AddDependency(myself, "bintools.ext", true, true);
ds@1952
    91
	sharesys->AddNatives(myself, g_CallNatives);
ds@1952
    92
	sharesys->AddNatives(myself, g_Natives);
ds@1952
    93
	sharesys->AddNatives(myself, g_TENatives);
ds@1952
    94
	sharesys->AddNatives(myself, g_SoundNatives);
ds@1952
    95
	sharesys->AddNatives(myself, g_TRNatives);
ds@1952
    96
	sharesys->AddNatives(myself, g_StringTableNatives);
ds@1952
    97
	sharesys->AddNatives(myself, g_VoiceNatives);
ds@1952
    98
	sharesys->AddNatives(myself, g_EntInputNatives);
ds@1952
    99
	sharesys->AddNatives(myself, g_TeamNatives);
pred@1958
   100
	sharesys->AddNatives(myself, g_EntOutputNatives);
ds@1952
   101
ds@1952
   102
	SM_GET_IFACE(GAMEHELPERS, g_pGameHelpers);
ds@1952
   103
ds@1952
   104
	playerhelpers->AddClientListener(&g_SdkTools);
ds@1952
   105
	g_CallHandle = handlesys->CreateType("ValveCall", this, 0, NULL, NULL, myself->GetIdentity(), NULL);
pred@2108
   106
pred@2108
   107
	TypeAccess TraceAccess;
pred@2108
   108
	handlesys->InitAccessDefaults(&TraceAccess, NULL);
pred@2116
   109
	TraceAccess.ident = myself->GetIdentity();
pred@2108
   110
	TraceAccess.access[HTypeAccess_Create] = true;
pred@2116
   111
	TraceAccess.access[HTypeAccess_Inherit] = true;
pred@2108
   112
	g_TraceHandle = handlesys->CreateType("TraceRay", this, 0, &TraceAccess, NULL, myself->GetIdentity(), NULL);
ds@1952
   113
ds@1952
   114
#if defined ORANGEBOX_BUILD
ds@1952
   115
	g_pCVar = icvar;
ds@1952
   116
#endif
ds@1952
   117
	CONVAR_REGISTER(this);
ds@1952
   118
ds@1952
   119
	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
ds@1952
   120
ds@1952
   121
	playerhelpers->RegisterCommandTargetProcessor(this);
ds@1952
   122
ds@1952
   123
	MathLib_Init(2.2f, 2.2f, 0.0f, 2);
ds@1952
   124
pred@1958
   125
	spengine = g_pSM->GetScriptingEngine();
pred@1958
   126
pred@1958
   127
	plsys->AddPluginsListener(&g_OutputManager);
pred@1958
   128
pred@1958
   129
	g_OutputManager.Init();
pred@1958
   130
dvander@2044
   131
	VoiceInit();
dvander@2044
   132
ds@1952
   133
	return true;
ds@1952
   134
}
ds@1952
   135
ds@1952
   136
void SDKTools::OnHandleDestroy(HandleType_t type, void *object)
ds@1952
   137
{
ds@1952
   138
	if (type == g_CallHandle)
ds@1952
   139
	{
ds@1952
   140
		ValveCall *v = (ValveCall *)object;
ds@1952
   141
		delete v;
ds@1952
   142
	}
ds@1952
   143
	else if (type == g_TraceHandle)
ds@1952
   144
	{
ds@1952
   145
		trace_t *tr = (trace_t *)object;
ds@1952
   146
		delete tr;
ds@1952
   147
	}
ds@1952
   148
}
ds@1952
   149
ds@1952
   150
void SDKTools::SDK_OnUnload()
ds@1952
   151
{
ds@1952
   152
	SourceHook::List<ValveCall *>::iterator iter;
ds@1952
   153
	for (iter = g_RegCalls.begin();
ds@1952
   154
		 iter != g_RegCalls.end();
ds@1952
   155
		 iter++)
ds@1952
   156
	{
ds@1952
   157
		delete (*iter);
ds@1952
   158
	}
ds@1952
   159
	g_RegCalls.clear();
ds@1952
   160
	ShutdownHelpers();
ds@1952
   161
ds@1952
   162
	if (g_pAcceptInput)
ds@1952
   163
	{
ds@1952
   164
		g_pAcceptInput->Destroy();
ds@1952
   165
		g_pAcceptInput = NULL;
ds@1952
   166
	}
ds@1952
   167
ds@1952
   168
	g_TEManager.Shutdown();
ds@1952
   169
	s_TempEntHooks.Shutdown();
ds@1952
   170
	s_SoundHooks.Shutdown();
ds@1952
   171
ds@1952
   172
	gameconfs->CloseGameConfigFile(g_pGameConf);
ds@1952
   173
	playerhelpers->RemoveClientListener(&g_SdkTools);
ds@1952
   174
	playerhelpers->UnregisterCommandTargetProcessor(this);
dvander@2060
   175
	plsys->RemovePluginsListener(&g_OutputManager);
ds@1952
   176
ds@1952
   177
	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SDKTools::LevelInit, true);
ds@1952
   178
ds@1952
   179
	if (enginePatch)
ds@1952
   180
	{
ds@1952
   181
		SH_RELEASE_CALLCLASS(enginePatch);
ds@1952
   182
		enginePatch = NULL;
ds@1952
   183
	}
ds@1952
   184
	if (enginesoundPatch)
ds@1952
   185
	{
ds@1952
   186
		SH_RELEASE_CALLCLASS(enginesoundPatch);
ds@1952
   187
		enginesoundPatch = NULL;
ds@1952
   188
	}
ds@1952
   189
}
ds@1952
   190
ds@1952
   191
bool SDKTools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
ds@1952
   192
{
ds@1952
   193
	GET_V_IFACE_ANY(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
ds@1952
   194
	GET_V_IFACE_ANY(GetEngineFactory, engsound, IEngineSound, IENGINESOUND_SERVER_INTERFACE_VERSION);
ds@1952
   195
	GET_V_IFACE_ANY(GetEngineFactory, enginetrace, IEngineTrace, INTERFACEVERSION_ENGINETRACE_SERVER);
ds@1952
   196
	GET_V_IFACE_ANY(GetEngineFactory, netstringtables, INetworkStringTableContainer, INTERFACENAME_NETWORKSTRINGTABLESERVER);
ds@1952
   197
	GET_V_IFACE_ANY(GetEngineFactory, pluginhelpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
ds@1952
   198
	GET_V_IFACE_ANY(GetServerFactory, serverClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
ds@1952
   199
	GET_V_IFACE_ANY(GetEngineFactory, voiceserver, IVoiceServer, INTERFACEVERSION_VOICESERVER);
ds@1952
   200
	GET_V_IFACE_ANY(GetServerFactory, playerinfomngr, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
ds@1952
   201
	GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
ds@1952
   202
ds@1952
   203
	enginePatch = SH_GET_CALLCLASS(engine);
ds@1952
   204
	enginesoundPatch = SH_GET_CALLCLASS(engsound);
ds@1952
   205
ds@1952
   206
	return true;
ds@1952
   207
}
ds@1952
   208
ds@1952
   209
void SDKTools::SDK_OnAllLoaded()
ds@1952
   210
{
ds@1952
   211
	SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
ds@1952
   212
ds@1952
   213
	if (!g_pBinTools)
ds@1952
   214
	{
ds@1952
   215
		return;
ds@1952
   216
	}
ds@1952
   217
ds@1952
   218
	g_TEManager.Initialize();
ds@1952
   219
	s_TempEntHooks.Initialize();
ds@1952
   220
	s_SoundHooks.Initialize();
ds@1952
   221
	InitializeValveGlobals();
ds@1952
   222
	GetIServer();
ds@1952
   223
}
ds@1952
   224
ds@1952
   225
bool SDKTools::QueryRunning(char *error, size_t maxlength)
ds@1952
   226
{
ds@1952
   227
	SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
ds@1952
   228
ds@1952
   229
	return true;
ds@1952
   230
}
ds@1952
   231
ds@1952
   232
bool SDKTools::QueryInterfaceDrop(SMInterface *pInterface)
ds@1952
   233
{
ds@1952
   234
	if (pInterface == g_pBinTools)
ds@1952
   235
	{
ds@1952
   236
		return false;
ds@1952
   237
	}
ds@1952
   238
ds@1952
   239
	return IExtensionInterface::QueryInterfaceDrop(pInterface);
ds@1952
   240
}
ds@1952
   241
ds@1952
   242
void SDKTools::NotifyInterfaceDrop(SMInterface *pInterface)
ds@1952
   243
{
ds@1952
   244
	SourceHook::List<ValveCall *>::iterator iter;
ds@1952
   245
	for (iter = g_RegCalls.begin();
ds@1952
   246
		iter != g_RegCalls.end();
ds@1952
   247
		iter++)
ds@1952
   248
	{
ds@1952
   249
		delete (*iter);
ds@1952
   250
	}
ds@1952
   251
	g_RegCalls.clear();
ds@1952
   252
	ShutdownHelpers();
ds@1952
   253
ds@1952
   254
	g_TEManager.Shutdown();
ds@1952
   255
	s_TempEntHooks.Shutdown();
ds@1952
   256
ds@1952
   257
	if (g_pAcceptInput)
ds@1952
   258
	{
ds@1952
   259
		g_pAcceptInput->Destroy();
ds@1952
   260
		g_pAcceptInput = NULL;
ds@1952
   261
	}
ds@1952
   262
}
ds@1952
   263
ds@1952
   264
bool SDKTools::RegisterConCommandBase(ConCommandBase *pVar)
ds@1952
   265
{
ds@1952
   266
#if defined METAMOD_PLAPI_VERSION
ds@1952
   267
	return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
ds@1952
   268
#else
ds@1952
   269
	return g_SMAPI->RegisterConCmdBase(g_PLAPI, pVar);
ds@1952
   270
#endif
ds@1952
   271
}
ds@1952
   272
ds@1952
   273
bool SDKTools::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
ds@1952
   274
{
ds@1952
   275
	const char *name;
ds@1952
   276
	char key[32];
ds@1952
   277
	int count, n = 1;
ds@1952
   278
ds@1952
   279
	if (!(name=g_pGameConf->GetKeyValue("SlapSoundCount")))
ds@1952
   280
	{
ds@1952
   281
		RETURN_META_VALUE(MRES_IGNORED, true);
ds@1952
   282
	}
ds@1952
   283
ds@1952
   284
	count = atoi(name);
ds@1952
   285
ds@1952
   286
	while (n <= count)
ds@1952
   287
	{
ds@1952
   288
		snprintf(key, sizeof(key), "SlapSound%d", n);
ds@1952
   289
		if ((name=g_pGameConf->GetKeyValue(key)))
ds@1952
   290
		{
ds@1952
   291
			engsound->PrecacheSound(name, true);
ds@1952
   292
		}
ds@1952
   293
		n++;
ds@1952
   294
	}
ds@1952
   295
ds@1952
   296
	RETURN_META_VALUE(MRES_IGNORED, true);
ds@1952
   297
}
ds@1952
   298
ds@1952
   299
bool SDKTools::ProcessCommandTarget(cmd_target_info_t *info)
ds@1952
   300
{
ds@1952
   301
	if (strcmp(info->pattern, "@aim") != 0)
ds@1952
   302
	{
ds@1952
   303
		return false;
ds@1952
   304
	}
ds@1952
   305
ds@1952
   306
	IGamePlayer *pAdmin = info->admin ? playerhelpers->GetGamePlayer(info->admin) : NULL;
ds@1952
   307
ds@1952
   308
	/* The server can't aim, of course. */
ds@1952
   309
	if (pAdmin == NULL)
ds@1952
   310
	{
ds@1952
   311
		return false;
ds@1952
   312
	}
ds@1952
   313
ds@1952
   314
	int player_index;
ds@1952
   315
	if ((player_index = GetClientAimTarget(pAdmin->GetEdict(), true)) < 1)
ds@1952
   316
	{
ds@1952
   317
		info->reason = COMMAND_TARGET_NONE;
ds@1952
   318
		info->num_targets = 0;
ds@1952
   319
		return true;
ds@1952
   320
	}
ds@1952
   321
ds@1952
   322
	IGamePlayer *pTarget = playerhelpers->GetGamePlayer(player_index);
ds@1952
   323
ds@1952
   324
	if (pTarget == NULL)
ds@1952
   325
	{
ds@1952
   326
		info->reason = COMMAND_TARGET_NONE;
ds@1952
   327
		info->num_targets = 0;
ds@1952
   328
		return true;
ds@1952
   329
	}
ds@1952
   330
ds@1952
   331
	info->reason = playerhelpers->FilterCommandTarget(pAdmin, pTarget, info->flags);
ds@1952
   332
	if (info->reason != COMMAND_TARGET_VALID)
ds@1952
   333
	{
ds@1952
   334
		info->num_targets = 0;
ds@1952
   335
		return true;
ds@1952
   336
	}
ds@1952
   337
ds@1952
   338
	info->targets[0] = player_index;
ds@1952
   339
	info->num_targets = 1;
ds@1952
   340
	info->reason = COMMAND_TARGET_VALID;
ds@1952
   341
	info->target_name_style = COMMAND_TARGETNAME_RAW;
ds@1952
   342
	snprintf(info->target_name, info->target_name_maxlength, "%s", pTarget->GetName());
ds@1952
   343
ds@1952
   344
	return true;
ds@1952
   345
}