extensions/tf2/extension.cpp
author Matt Woodrow <pred@alliedmods.net>
Sat May 24 04:08:29 2008 +0000 (2008-05-24)
changeset 2069 80d56f1d58fe
parent 1979 d36fac1a8203
child 2075 ffc871fb7cc8
permissions -rw-r--r--
Added a load-block for incorrect mods to TF2 and cstrike extensions.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
pred@1963
     4
 * SourceMod Team Fortress 2 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"
pred@1963
    33
#include "util.h"
ds@1952
    34
#include "RegNatives.h"
ds@1952
    35
#include "iplayerinfo.h"
pred@1963
    36
#include "sm_trie_tpl.h"
pred@1979
    37
#include "criticals.h"
ds@1952
    38
ds@1952
    39
/**
ds@1952
    40
 * @file extension.cpp
ds@1952
    41
 * @brief Implement extension code here.
ds@1952
    42
 */
ds@1952
    43
ds@1952
    44
ds@1952
    45
TF2Tools g_TF2Tools;
ds@1952
    46
IGameConfig *g_pGameConf = NULL;
ds@1952
    47
ds@1952
    48
IBinTools *g_pBinTools = NULL;
ds@1952
    49
ds@1952
    50
SMEXT_LINK(&g_TF2Tools);
ds@1952
    51
pred@1979
    52
ICvar *icvar = NULL;
pred@1979
    53
pred@1963
    54
CGlobalVars *gpGlobals = NULL;
pred@1963
    55
pred@1963
    56
sm_sendprop_info_t *playerSharedOffset;
ds@1952
    57
ds@1952
    58
extern sp_nativeinfo_t g_TFNatives[];
ds@1952
    59
pred@1963
    60
int g_resourceEntity;
pred@1963
    61
pred@1963
    62
SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int , int);
pred@1963
    63
ds@1952
    64
bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late)
ds@1952
    65
{
pred@2069
    66
	if (strcmp(g_pSM->GetGameFolderName(), "tf") != 0)
pred@2069
    67
	{
pred@2069
    68
		snprintf(error, maxlength, "Cannot Load TF2 Extension on mods other than TF2");
pred@2069
    69
		return false;
pred@2069
    70
	}
pred@2069
    71
pred@1963
    72
	ServerClass *sc = UTIL_FindServerClass("CTFPlayer");
pred@1963
    73
	if (sc == NULL)
pred@1963
    74
	{
pred@1963
    75
		snprintf(error, maxlength, "Could not find CTFPlayer server class");
pred@1963
    76
		return false;
pred@1963
    77
	}
pred@1963
    78
pred@1963
    79
	playerSharedOffset = new sm_sendprop_info_t;
pred@1963
    80
pred@1963
    81
	if (!UTIL_FindDataTable(sc->m_pTable, "DT_TFPlayerShared", playerSharedOffset, 0))
pred@1963
    82
	{
pred@1963
    83
		snprintf(error, maxlength, "Could not find DT_TFPlayerShared data table");
pred@1963
    84
		return false;
pred@1963
    85
	}
pred@1963
    86
ds@1952
    87
	sharesys->AddDependency(myself, "bintools.ext", true, true);
ds@1952
    88
ds@1952
    89
	char conf_error[255];
ds@1952
    90
	if (!gameconfs->LoadGameConfigFile("sm-tf2.games", &g_pGameConf, conf_error, sizeof(conf_error)))
ds@1952
    91
	{
pred@2069
    92
		if (conf_error)
ds@1952
    93
		{
ds@1952
    94
			snprintf(error, maxlength, "Could not read sm-tf2.games.txt: %s", conf_error);
ds@1952
    95
		}
ds@1952
    96
		return false;
ds@1952
    97
	}
ds@1952
    98
ds@1952
    99
	sharesys->AddNatives(myself, g_TFNatives);
ds@1952
   100
	sharesys->RegisterLibrary(myself, "tf2");
ds@1952
   101
ds@1952
   102
	playerhelpers->RegisterCommandTargetProcessor(this);
ds@1952
   103
pred@1979
   104
	spengine = g_pSM->GetScriptingEngine();
pred@1979
   105
pred@1979
   106
	g_pCVar = icvar;
pred@1979
   107
ds@1952
   108
	return true;
ds@1952
   109
}
ds@1952
   110
ds@1952
   111
bool TF2Tools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
ds@1952
   112
{
ds@1952
   113
	GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
ds@1952
   114
pred@1963
   115
	gpGlobals = ismm->GetCGlobals();
pred@1963
   116
pred@1963
   117
	SH_ADD_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
pred@1963
   118
pred@1979
   119
	GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
pred@1979
   120
pred@1979
   121
	GET_V_IFACE_CURRENT(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
pred@1979
   122
ds@1952
   123
	return true;
ds@1952
   124
}
ds@1952
   125
ds@1952
   126
void TF2Tools::SDK_OnUnload()
ds@1952
   127
{
pred@1963
   128
	SH_REMOVE_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
pred@1963
   129
ds@1952
   130
	g_RegNatives.UnregisterAll();
ds@1952
   131
	gameconfs->CloseGameConfigFile(g_pGameConf);
pred@1963
   132
	playerhelpers->UnregisterCommandTargetProcessor(this);
ds@1952
   133
}
ds@1952
   134
ds@1952
   135
void TF2Tools::SDK_OnAllLoaded()
ds@1952
   136
{
ds@1952
   137
	SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
pred@1979
   138
pred@1979
   139
	g_CriticalHitManager.Init();
ds@1952
   140
}
ds@1952
   141
pred@1979
   142
bool TF2Tools::RegisterConCommandBase(ConCommandBase *pVar)
pred@1979
   143
{
pred@1979
   144
	return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
pred@1979
   145
}
pred@1979
   146
pred@1979
   147
ds@1952
   148
bool TF2Tools::QueryRunning(char *error, size_t maxlength)
ds@1952
   149
{
ds@1952
   150
	SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
ds@1952
   151
ds@1952
   152
	return true;
ds@1952
   153
}
ds@1952
   154
ds@1952
   155
bool TF2Tools::QueryInterfaceDrop(SMInterface *pInterface)
ds@1952
   156
{
ds@1952
   157
	if (pInterface == g_pBinTools)
ds@1952
   158
	{
ds@1952
   159
		return false;
ds@1952
   160
	}
ds@1952
   161
ds@1952
   162
	return IExtensionInterface::QueryInterfaceDrop(pInterface);
ds@1952
   163
}
ds@1952
   164
ds@1952
   165
void TF2Tools::NotifyInterfaceDrop(SMInterface *pInterface)
ds@1952
   166
{
ds@1952
   167
	g_RegNatives.UnregisterAll();
ds@1952
   168
}
ds@1952
   169
pred@1963
   170
void OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
ds@1952
   171
{
pred@1963
   172
	g_resourceEntity = FindResourceEntity();
pred@1963
   173
}
ds@1952
   174
ds@1952
   175
ds@1952
   176
bool TF2Tools::ProcessCommandTarget(cmd_target_info_t *info)
ds@1952
   177
{
ds@1952
   178
	int max_clients;
ds@1952
   179
	IPlayerInfo *pInfo;
ds@1952
   180
	unsigned int team_index = 0;
ds@1952
   181
	IGamePlayer *pPlayer, *pAdmin;
ds@1952
   182
ds@1952
   183
	if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
ds@1952
   184
	{
ds@1952
   185
		return false;
ds@1952
   186
	}
ds@1952
   187
ds@1952
   188
	if (info->admin)
ds@1952
   189
	{
ds@1952
   190
		if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
ds@1952
   191
		{
ds@1952
   192
			return false;
ds@1952
   193
		}
ds@1952
   194
		if (!pAdmin->IsInGame())
ds@1952
   195
		{
ds@1952
   196
			return false;
ds@1952
   197
		}
ds@1952
   198
	}
ds@1952
   199
	else
ds@1952
   200
	{
ds@1952
   201
		pAdmin = NULL;
ds@1952
   202
	}
ds@1952
   203
ds@1952
   204
	if (strcmp(info->pattern, "@red") == 0 )
ds@1952
   205
	{
ds@1952
   206
		team_index = 2;
ds@1952
   207
	}
ds@1952
   208
	else if (strcmp(info->pattern, "@blue") == 0)
ds@1952
   209
	{
ds@1952
   210
		team_index = 3;
ds@1952
   211
	}
ds@1952
   212
	else
ds@1952
   213
	{
ds@1952
   214
		return false;
ds@1952
   215
	}
ds@1952
   216
ds@1952
   217
	info->num_targets = 0;
ds@1952
   218
ds@1952
   219
	max_clients = playerhelpers->GetMaxClients();
ds@1952
   220
	for (int i = 1; 
ds@1952
   221
		 i <= max_clients && (cell_t)info->num_targets < info->max_targets; 
ds@1952
   222
		 i++)
ds@1952
   223
	{
ds@1952
   224
		if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
ds@1952
   225
		{
ds@1952
   226
			continue;
ds@1952
   227
		}
ds@1952
   228
		if (!pPlayer->IsInGame())
ds@1952
   229
		{
ds@1952
   230
			continue;
ds@1952
   231
		}
ds@1952
   232
		if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
ds@1952
   233
		{
ds@1952
   234
			continue;
ds@1952
   235
		}
ds@1952
   236
		if (pInfo->GetTeamIndex() != (int)team_index)
ds@1952
   237
		{
ds@1952
   238
			continue;
ds@1952
   239
		}
ds@1952
   240
		if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags) 
ds@1952
   241
			!= COMMAND_TARGET_VALID)
ds@1952
   242
		{
ds@1952
   243
			continue;
ds@1952
   244
		}
ds@1952
   245
		info->targets[info->num_targets] = i;
ds@1952
   246
		info->num_targets++;
ds@1952
   247
	}
ds@1952
   248
ds@1952
   249
	if (info->num_targets == 0)
ds@1952
   250
	{
ds@1952
   251
		info->reason = COMMAND_TARGET_EMPTY_FILTER;
ds@1952
   252
	}
ds@1952
   253
	else
ds@1952
   254
	{
ds@1952
   255
		info->reason = COMMAND_TARGET_VALID;
ds@1952
   256
	}
ds@1952
   257
ds@1952
   258
	info->target_name_style = COMMAND_TARGETNAME_RAW;
ds@1952
   259
	if (team_index == 2)
ds@1952
   260
	{
ds@1952
   261
		UTIL_Format(info->target_name, info->target_name_maxlength, "Red Team");
ds@1952
   262
	}
ds@1952
   263
	else if (team_index == 3)
ds@1952
   264
	{
ds@1952
   265
		UTIL_Format(info->target_name, info->target_name_maxlength, "Blue Team");
ds@1952
   266
	}
ds@1952
   267
ds@1952
   268
	return true;
ds@1952
   269
}
ds@1952
   270
pred@1963
   271
int FindResourceEntity()
pred@1963
   272
{
pred@1963
   273
	return FindEntityByNetClass(-1, "CTFPlayerResource");
pred@1963
   274
}
pred@1963
   275
pred@1963
   276
pred@1963
   277
int FindEntityByNetClass(int start, const char *classname)
pred@1963
   278
{
pred@1963
   279
	edict_t *current;
pred@1963
   280
pred@1963
   281
	for (int i = ((start != -1) ? start : 0); i < gpGlobals->maxEntities; i++)
pred@1963
   282
	{
pred@1963
   283
		current = engine->PEntityOfEntIndex(i);
pred@1963
   284
		if (current == NULL)
pred@1963
   285
		{
pred@1963
   286
			continue;
pred@1963
   287
		}
pred@1963
   288
pred@1963
   289
		IServerNetworkable *network = current->GetNetworkable();
pred@1963
   290
pred@1963
   291
		if (network == NULL)
pred@1963
   292
		{
pred@1963
   293
			continue;
pred@1963
   294
		}
pred@1963
   295
pred@1963
   296
		ServerClass *sClass = network->GetServerClass();
pred@1963
   297
		const char *name = sClass->GetName();
pred@1963
   298
		
pred@1963
   299
pred@1963
   300
		if (strcmp(name, classname) == 0)
pred@1963
   301
		{
pred@1963
   302
			return i;
pred@1963
   303
		}
pred@1963
   304
	}
pred@1963
   305
pred@1963
   306
	return -1;
pred@1963
   307
}
pred@1963
   308
pred@1963
   309
TFClassType ClassnameToType(const char *classname)
pred@1963
   310
{
pred@1963
   311
	static KTrie<TFClassType> trie;
pred@1963
   312
	static bool filled = false;
pred@1963
   313
	
pred@1963
   314
	if (!filled)
pred@1963
   315
	{
pred@1963
   316
		trie.insert("scout", TFClass_Scout);
pred@1963
   317
		trie.insert("sniper", TFClass_Sniper);
pred@1963
   318
		trie.insert("soldier", TFClass_Soldier);
pred@1963
   319
		trie.insert("demoman", TFClass_DemoMan);
pred@1963
   320
		trie.insert("demo", TFClass_DemoMan);
pred@1963
   321
		trie.insert("medic", TFClass_Medic);
pred@1963
   322
		trie.insert("heavy", TFClass_Heavy);
pred@1963
   323
		trie.insert("hwg", TFClass_Heavy);
pred@1963
   324
		trie.insert("pyro", TFClass_Pyro);
pred@1963
   325
		trie.insert("spy", TFClass_Spy);
pred@1963
   326
		trie.insert("engineer", TFClass_Engineer);
pred@1963
   327
		trie.insert("engy", TFClass_Engineer);
pred@1963
   328
pred@1963
   329
		filled = true;	
pred@1963
   330
	}
pred@1963
   331
	
pred@1963
   332
	TFClassType *value;
pred@1963
   333
pred@1963
   334
	if (!(value = trie.retrieve(classname)))
pred@1963
   335
	{
pred@1963
   336
		return TFClass_Unknown;	
pred@1963
   337
	}
pred@1963
   338
pred@1963
   339
	return *value;
pred@1963
   340
}
pred@1963
   341
pred@1963
   342
pred@1963
   343
pred@1963
   344
/**
pred@1963
   345
 * A picture of a blue crab given to me as a gift and stored here for safe keeping
pred@1963
   346
 *
pred@1963
   347
 * http://www.democracycellproject.net/blog/archives/Clown%20car.jpg
pred@1963
   348
 */
pred@1963
   349