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 <sourcemod_version.h>
33 #include "extension.h"
35 #include "RegNatives.h"
36 #include "iplayerinfo.h"
37 #include "sm_trie_tpl.h"
38 #include "criticals.h"
40 #include "CDetour/detours.h"
44 * @brief Implement extension code here.
49 IGameConfig *g_pGameConf = NULL;
51 IBinTools *g_pBinTools = NULL;
53 SMEXT_LINK(&g_TF2Tools);
57 CGlobalVars *gpGlobals = NULL;
59 sm_sendprop_info_t *playerSharedOffset;
61 extern sp_nativeinfo_t g_TFNatives[];
65 SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int , int);
67 bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late)
69 if (strcmp(g_pSM->GetGameFolderName(), "tf") != 0)
71 UTIL_Format(error, maxlength, "Cannot Load TF2 Extension on mods other than TF2");
75 ServerClass *sc = UTIL_FindServerClass("CTFPlayer");
78 UTIL_Format(error, maxlength, "Could not find CTFPlayer server class");
82 playerSharedOffset = new sm_sendprop_info_t;
84 if (!UTIL_FindDataTable(sc->m_pTable, "DT_TFPlayerShared", playerSharedOffset, 0))
86 UTIL_Format(error, maxlength, "Could not find DT_TFPlayerShared data table");
90 sharesys->AddDependency(myself, "bintools.ext", true, true);
92 char conf_error[255] = "";
93 if (!gameconfs->LoadGameConfigFile("sm-tf2.games", &g_pGameConf, conf_error, sizeof(conf_error)))
97 UTIL_Format(error, maxlength, "Could not read sm-tf2.games.txt: %s", conf_error);
102 CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);
104 sharesys->AddNatives(myself, g_TFNatives);
105 sharesys->RegisterLibrary(myself, "tf2");
107 plsys->AddPluginsListener(this);
109 playerhelpers->RegisterCommandTargetProcessor(this);
111 g_critForward = forwards->CreateForward("TF2_CalcIsAttackCritical", ET_Hook, 4, NULL, Param_Cell, Param_Cell, Param_String, Param_CellByRef);
112 g_getHolidayForward = forwards->CreateForward("TF2_OnGetHoliday", ET_Event, 1, NULL, Param_CellByRef);
116 m_CritDetoursEnabled = false;
117 m_GetHolidayDetourEnabled = false;
122 const char *TF2Tools::GetExtensionVerString()
124 return SM_FULL_VERSION;
127 const char *TF2Tools::GetExtensionDateString()
129 return SM_BUILD_TIMESTAMP;
132 bool TF2Tools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
134 GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
136 gpGlobals = ismm->GetCGlobals();
138 SH_ADD_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
140 GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
142 GET_V_IFACE_CURRENT(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
144 GET_V_IFACE_CURRENT(GetEngineFactory, m_GameEventManager, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
145 m_GameEventManager->AddListener(this, "teamplay_restart_round", true);
150 void TF2Tools::SDK_OnUnload()
152 SH_REMOVE_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
154 g_RegNatives.UnregisterAll();
155 gameconfs->CloseGameConfigFile(g_pGameConf);
156 playerhelpers->UnregisterCommandTargetProcessor(this);
158 plsys->RemovePluginsListener(this);
160 forwards->ReleaseForward(g_critForward);
161 forwards->ReleaseForward(g_getHolidayForward);
164 void TF2Tools::SDK_OnAllLoaded()
166 SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
169 bool TF2Tools::RegisterConCommandBase(ConCommandBase *pVar)
171 return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
174 void TF2Tools::FireGameEvent( IGameEvent *event )
176 timersys->NotifyOfGameStart();
177 timersys->MapTimeLeftChanged();
180 bool TF2Tools::QueryRunning(char *error, size_t maxlength)
182 SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
187 bool TF2Tools::QueryInterfaceDrop(SMInterface *pInterface)
189 if (pInterface == g_pBinTools)
194 return IExtensionInterface::QueryInterfaceDrop(pInterface);
197 void TF2Tools::NotifyInterfaceDrop(SMInterface *pInterface)
199 g_RegNatives.UnregisterAll();
202 void OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
204 g_resourceEntity = FindResourceEntity();
207 bool TF2Tools::ProcessCommandTarget(cmd_target_info_t *info)
211 unsigned int team_index = 0;
212 IGamePlayer *pPlayer, *pAdmin;
214 if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
221 if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
225 if (!pAdmin->IsInGame())
235 if (strcmp(info->pattern, "@red") == 0 )
239 else if (strcmp(info->pattern, "@blue") == 0)
248 info->num_targets = 0;
250 max_clients = playerhelpers->GetMaxClients();
252 i <= max_clients && (cell_t)info->num_targets < info->max_targets;
255 if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
259 if (!pPlayer->IsInGame())
263 if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
267 if (pInfo->GetTeamIndex() != (int)team_index)
271 if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags)
272 != COMMAND_TARGET_VALID)
276 info->targets[info->num_targets] = i;
280 if (info->num_targets == 0)
282 info->reason = COMMAND_TARGET_EMPTY_FILTER;
286 info->reason = COMMAND_TARGET_VALID;
289 info->target_name_style = COMMAND_TARGETNAME_RAW;
292 UTIL_Format(info->target_name, info->target_name_maxlength, "Red Team");
294 else if (team_index == 3)
296 UTIL_Format(info->target_name, info->target_name_maxlength, "Blue Team");
302 void TF2Tools::OnPluginLoaded(IPlugin *plugin)
304 if (!m_CritDetoursEnabled && g_critForward->GetFunctionCount())
306 InitialiseCritDetours();
307 m_CritDetoursEnabled = true;
309 if (!m_GetHolidayDetourEnabled && g_getHolidayForward->GetFunctionCount())
311 InitialiseGetHolidayDetour();
312 m_GetHolidayDetourEnabled = true;
316 void TF2Tools::OnPluginUnloaded(IPlugin *plugin)
318 if (m_CritDetoursEnabled && !g_critForward->GetFunctionCount())
321 m_CritDetoursEnabled = false;
323 if (m_GetHolidayDetourEnabled && !g_getHolidayForward->GetFunctionCount())
325 RemoveGetHolidayDetour();
326 m_GetHolidayDetourEnabled = false;
329 int FindResourceEntity()
331 return FindEntityByNetClass(-1, "CTFPlayerResource");
335 int FindEntityByNetClass(int start, const char *classname)
339 for (int i = ((start != -1) ? start : 0); i < gpGlobals->maxEntities; i++)
341 current = engine->PEntityOfEntIndex(i);
347 IServerNetworkable *network = current->GetNetworkable();
354 ServerClass *sClass = network->GetServerClass();
355 const char *name = sClass->GetName();
358 if (strcmp(name, classname) == 0)
367 TFClassType ClassnameToType(const char *classname)
369 static KTrie<TFClassType> trie;
370 static bool filled = false;
374 trie.insert("scout", TFClass_Scout);
375 trie.insert("sniper", TFClass_Sniper);
376 trie.insert("soldier", TFClass_Soldier);
377 trie.insert("demoman", TFClass_DemoMan);
378 trie.insert("demo", TFClass_DemoMan);
379 trie.insert("medic", TFClass_Medic);
380 trie.insert("heavy", TFClass_Heavy);
381 trie.insert("hwg", TFClass_Heavy);
382 trie.insert("pyro", TFClass_Pyro);
383 trie.insert("spy", TFClass_Spy);
384 trie.insert("engineer", TFClass_Engineer);
385 trie.insert("engy", TFClass_Engineer);
392 if (!(value = trie.retrieve(classname)))
394 return TFClass_Unknown;
403 * A picture of a blue crab given to me as a gift and stored here for safe keeping
405 * http://www.democracycellproject.net/blog/archives/Clown%20car.jpg