Updated CDetour code in TF2 extension to prevent crashes with spies. Updated windows sig.
3 * =============================================================================
4 * SourceMod Team Fortress 2 Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "extension.h"
34 #include "RegNatives.h"
35 #include "iplayerinfo.h"
36 #include "sm_trie_tpl.h"
37 #include "criticals.h"
38 #include "CDetour/detours.h"
42 * @brief Implement extension code here.
47 IGameConfig *g_pGameConf = NULL;
49 IBinTools *g_pBinTools = NULL;
51 SMEXT_LINK(&g_TF2Tools);
55 CGlobalVars *gpGlobals = NULL;
57 sm_sendprop_info_t *playerSharedOffset;
59 extern sp_nativeinfo_t g_TFNatives[];
63 SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int , int);
65 bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late)
67 if (strcmp(g_pSM->GetGameFolderName(), "tf") != 0)
69 UTIL_Format(error, maxlength, "Cannot Load TF2 Extension on mods other than TF2");
73 ServerClass *sc = UTIL_FindServerClass("CTFPlayer");
76 UTIL_Format(error, maxlength, "Could not find CTFPlayer server class");
80 playerSharedOffset = new sm_sendprop_info_t;
82 if (!UTIL_FindDataTable(sc->m_pTable, "DT_TFPlayerShared", playerSharedOffset, 0))
84 UTIL_Format(error, maxlength, "Could not find DT_TFPlayerShared data table");
88 sharesys->AddDependency(myself, "bintools.ext", true, true);
90 char conf_error[255] = "";
91 if (!gameconfs->LoadGameConfigFile("sm-tf2.games", &g_pGameConf, conf_error, sizeof(conf_error)))
95 UTIL_Format(error, maxlength, "Could not read sm-tf2.games.txt: %s", conf_error);
100 CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);
102 sharesys->AddNatives(myself, g_TFNatives);
103 sharesys->RegisterLibrary(myself, "tf2");
105 playerhelpers->RegisterCommandTargetProcessor(this);
107 g_critForward = forwards->CreateForward("TF2_CalcIsAttackCritical", ET_Hook, 4, NULL, Param_Cell, Param_Cell, Param_String, Param_CellByRef);
114 bool TF2Tools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
116 GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
118 gpGlobals = ismm->GetCGlobals();
120 SH_ADD_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
122 GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
124 GET_V_IFACE_CURRENT(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
126 GET_V_IFACE_CURRENT(GetEngineFactory, m_GameEventManager, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
127 m_GameEventManager->AddListener(this, "teamplay_restart_round", true);
132 void TF2Tools::SDK_OnUnload()
134 SH_REMOVE_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
136 g_RegNatives.UnregisterAll();
137 gameconfs->CloseGameConfigFile(g_pGameConf);
138 playerhelpers->UnregisterCommandTargetProcessor(this);
140 forwards->ReleaseForward(g_critForward);
145 void TF2Tools::SDK_OnAllLoaded()
147 SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
152 bool TF2Tools::RegisterConCommandBase(ConCommandBase *pVar)
154 return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
157 void TF2Tools::FireGameEvent( IGameEvent *event )
159 timersys->NotifyOfGameStart();
160 timersys->MapTimeLeftChanged();
163 bool TF2Tools::QueryRunning(char *error, size_t maxlength)
165 SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
170 bool TF2Tools::QueryInterfaceDrop(SMInterface *pInterface)
172 if (pInterface == g_pBinTools)
177 return IExtensionInterface::QueryInterfaceDrop(pInterface);
180 void TF2Tools::NotifyInterfaceDrop(SMInterface *pInterface)
182 g_RegNatives.UnregisterAll();
185 void OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
187 g_resourceEntity = FindResourceEntity();
191 bool TF2Tools::ProcessCommandTarget(cmd_target_info_t *info)
195 unsigned int team_index = 0;
196 IGamePlayer *pPlayer, *pAdmin;
198 if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
205 if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
209 if (!pAdmin->IsInGame())
219 if (strcmp(info->pattern, "@red") == 0 )
223 else if (strcmp(info->pattern, "@blue") == 0)
232 info->num_targets = 0;
234 max_clients = playerhelpers->GetMaxClients();
236 i <= max_clients && (cell_t)info->num_targets < info->max_targets;
239 if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
243 if (!pPlayer->IsInGame())
247 if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
251 if (pInfo->GetTeamIndex() != (int)team_index)
255 if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags)
256 != COMMAND_TARGET_VALID)
260 info->targets[info->num_targets] = i;
264 if (info->num_targets == 0)
266 info->reason = COMMAND_TARGET_EMPTY_FILTER;
270 info->reason = COMMAND_TARGET_VALID;
273 info->target_name_style = COMMAND_TARGETNAME_RAW;
276 UTIL_Format(info->target_name, info->target_name_maxlength, "Red Team");
278 else if (team_index == 3)
280 UTIL_Format(info->target_name, info->target_name_maxlength, "Blue Team");
286 int FindResourceEntity()
288 return FindEntityByNetClass(-1, "CTFPlayerResource");
292 int FindEntityByNetClass(int start, const char *classname)
296 for (int i = ((start != -1) ? start : 0); i < gpGlobals->maxEntities; i++)
298 current = engine->PEntityOfEntIndex(i);
304 IServerNetworkable *network = current->GetNetworkable();
311 ServerClass *sClass = network->GetServerClass();
312 const char *name = sClass->GetName();
315 if (strcmp(name, classname) == 0)
324 TFClassType ClassnameToType(const char *classname)
326 static KTrie<TFClassType> trie;
327 static bool filled = false;
331 trie.insert("scout", TFClass_Scout);
332 trie.insert("sniper", TFClass_Sniper);
333 trie.insert("soldier", TFClass_Soldier);
334 trie.insert("demoman", TFClass_DemoMan);
335 trie.insert("demo", TFClass_DemoMan);
336 trie.insert("medic", TFClass_Medic);
337 trie.insert("heavy", TFClass_Heavy);
338 trie.insert("hwg", TFClass_Heavy);
339 trie.insert("pyro", TFClass_Pyro);
340 trie.insert("spy", TFClass_Spy);
341 trie.insert("engineer", TFClass_Engineer);
342 trie.insert("engy", TFClass_Engineer);
349 if (!(value = trie.retrieve(classname)))
351 return TFClass_Unknown;
360 * A picture of a blue crab given to me as a gift and stored here for safe keeping
362 * http://www.democracycellproject.net/blog/archives/Clown%20car.jpg