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 "gameplayrules.h"
41 #include "CDetour/detours.h"
45 * @brief Implement extension code here.
50 IGameConfig *g_pGameConf = NULL;
52 IBinTools *g_pBinTools = NULL;
54 SMEXT_LINK(&g_TF2Tools);
58 CGlobalVars *gpGlobals = NULL;
60 sm_sendprop_info_t *playerSharedOffset;
62 extern sp_nativeinfo_t g_TFNatives[];
66 SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int , int);
68 bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late)
70 if (strcmp(g_pSM->GetGameFolderName(), "tf") != 0)
72 UTIL_Format(error, maxlength, "Cannot Load TF2 Extension on mods other than TF2");
76 ServerClass *sc = UTIL_FindServerClass("CTFPlayer");
79 UTIL_Format(error, maxlength, "Could not find CTFPlayer server class");
83 playerSharedOffset = new sm_sendprop_info_t;
85 if (!UTIL_FindDataTable(sc->m_pTable, "DT_TFPlayerShared", playerSharedOffset, 0))
87 UTIL_Format(error, maxlength, "Could not find DT_TFPlayerShared data table");
91 sharesys->AddDependency(myself, "bintools.ext", true, true);
93 char conf_error[255] = "";
94 if (!gameconfs->LoadGameConfigFile("sm-tf2.games", &g_pGameConf, conf_error, sizeof(conf_error)))
98 UTIL_Format(error, maxlength, "Could not read sm-tf2.games.txt: %s", conf_error);
103 CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);
105 sharesys->AddNatives(myself, g_TFNatives);
106 sharesys->RegisterLibrary(myself, "tf2");
108 plsys->AddPluginsListener(this);
110 playerhelpers->RegisterCommandTargetProcessor(this);
112 g_critForward = forwards->CreateForward("TF2_CalcIsAttackCritical", ET_Hook, 4, NULL, Param_Cell, Param_Cell, Param_String, Param_CellByRef);
113 g_getHolidayForward = forwards->CreateForward("TF2_OnGetHoliday", ET_Event, 1, NULL, Param_CellByRef);
114 g_waitingPlayersStartForward = forwards->CreateForward("TF2_OnWaitingForPlayersStart", ET_Ignore, 0, NULL);
115 g_waitingPlayersEndForward = forwards->CreateForward("TF2_OnWaitingForPlayersEnd", ET_Ignore, 0, NULL);
119 m_CritDetoursEnabled = false;
120 m_GetHolidayDetourEnabled = false;
121 m_RulesDetoursEnabled = false;
126 const char *TF2Tools::GetExtensionVerString()
128 return SM_FULL_VERSION;
131 const char *TF2Tools::GetExtensionDateString()
133 return SM_BUILD_TIMESTAMP;
136 bool TF2Tools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
138 GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
140 gpGlobals = ismm->GetCGlobals();
142 SH_ADD_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
144 GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
146 GET_V_IFACE_CURRENT(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
148 GET_V_IFACE_CURRENT(GetEngineFactory, m_GameEventManager, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
149 m_GameEventManager->AddListener(this, "teamplay_restart_round", true);
154 void TF2Tools::SDK_OnUnload()
156 SH_REMOVE_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
158 g_RegNatives.UnregisterAll();
159 gameconfs->CloseGameConfigFile(g_pGameConf);
160 playerhelpers->UnregisterCommandTargetProcessor(this);
162 plsys->RemovePluginsListener(this);
164 forwards->ReleaseForward(g_critForward);
165 forwards->ReleaseForward(g_getHolidayForward);
166 forwards->ReleaseForward(g_waitingPlayersStartForward);
167 forwards->ReleaseForward(g_waitingPlayersEndForward);
170 void TF2Tools::SDK_OnAllLoaded()
172 SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
175 bool TF2Tools::RegisterConCommandBase(ConCommandBase *pVar)
177 return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
180 void TF2Tools::FireGameEvent( IGameEvent *event )
182 timersys->NotifyOfGameStart();
183 timersys->MapTimeLeftChanged();
186 bool TF2Tools::QueryRunning(char *error, size_t maxlength)
188 SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
193 bool TF2Tools::QueryInterfaceDrop(SMInterface *pInterface)
195 if (pInterface == g_pBinTools)
200 return IExtensionInterface::QueryInterfaceDrop(pInterface);
203 void TF2Tools::NotifyInterfaceDrop(SMInterface *pInterface)
205 g_RegNatives.UnregisterAll();
208 void OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
210 g_resourceEntity = FindResourceEntity();
213 bool TF2Tools::ProcessCommandTarget(cmd_target_info_t *info)
217 unsigned int team_index = 0;
218 IGamePlayer *pPlayer, *pAdmin;
220 if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
227 if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
231 if (!pAdmin->IsInGame())
241 if (strcmp(info->pattern, "@red") == 0 )
245 else if (strcmp(info->pattern, "@blue") == 0)
254 info->num_targets = 0;
256 max_clients = playerhelpers->GetMaxClients();
258 i <= max_clients && (cell_t)info->num_targets < info->max_targets;
261 if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
265 if (!pPlayer->IsInGame())
269 if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
273 if (pInfo->GetTeamIndex() != (int)team_index)
277 if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags)
278 != COMMAND_TARGET_VALID)
282 info->targets[info->num_targets] = i;
286 if (info->num_targets == 0)
288 info->reason = COMMAND_TARGET_EMPTY_FILTER;
292 info->reason = COMMAND_TARGET_VALID;
295 info->target_name_style = COMMAND_TARGETNAME_RAW;
298 UTIL_Format(info->target_name, info->target_name_maxlength, "Red Team");
300 else if (team_index == 3)
302 UTIL_Format(info->target_name, info->target_name_maxlength, "Blue Team");
308 void TF2Tools::OnPluginLoaded(IPlugin *plugin)
310 if (!m_CritDetoursEnabled && g_critForward->GetFunctionCount())
312 InitialiseCritDetours();
313 m_CritDetoursEnabled = true;
315 if (!m_GetHolidayDetourEnabled && g_getHolidayForward->GetFunctionCount())
317 InitialiseGetHolidayDetour();
318 m_GetHolidayDetourEnabled = true;
320 if (!m_RulesDetoursEnabled)
322 if(g_waitingPlayersStartForward->GetFunctionCount() || g_waitingPlayersEndForward->GetFunctionCount())
324 InitialiseRulesDetours();
325 m_RulesDetoursEnabled = true;
330 void TF2Tools::OnPluginUnloaded(IPlugin *plugin)
332 if (m_CritDetoursEnabled && !g_critForward->GetFunctionCount())
335 m_CritDetoursEnabled = false;
337 if (m_GetHolidayDetourEnabled && !g_getHolidayForward->GetFunctionCount())
339 RemoveGetHolidayDetour();
340 m_GetHolidayDetourEnabled = false;
342 if (m_RulesDetoursEnabled)
344 if(!g_waitingPlayersStartForward->GetFunctionCount() || !g_waitingPlayersEndForward->GetFunctionCount())
346 RemoveRulesDetours();
347 m_RulesDetoursEnabled = false;
351 int FindResourceEntity()
353 return FindEntityByNetClass(-1, "CTFPlayerResource");
357 int FindEntityByNetClass(int start, const char *classname)
361 for (int i = ((start != -1) ? start : 0); i < gpGlobals->maxEntities; i++)
363 current = engine->PEntityOfEntIndex(i);
369 IServerNetworkable *network = current->GetNetworkable();
376 ServerClass *sClass = network->GetServerClass();
377 const char *name = sClass->GetName();
380 if (strcmp(name, classname) == 0)
389 TFClassType ClassnameToType(const char *classname)
391 static KTrie<TFClassType> trie;
392 static bool filled = false;
396 trie.insert("scout", TFClass_Scout);
397 trie.insert("sniper", TFClass_Sniper);
398 trie.insert("soldier", TFClass_Soldier);
399 trie.insert("demoman", TFClass_DemoMan);
400 trie.insert("demo", TFClass_DemoMan);
401 trie.insert("medic", TFClass_Medic);
402 trie.insert("heavy", TFClass_Heavy);
403 trie.insert("hwg", TFClass_Heavy);
404 trie.insert("pyro", TFClass_Pyro);
405 trie.insert("spy", TFClass_Spy);
406 trie.insert("engineer", TFClass_Engineer);
407 trie.insert("engy", TFClass_Engineer);
414 if (!(value = trie.retrieve(classname)))
416 return TFClass_Unknown;
425 * A picture of a blue crab given to me as a gift and stored here for safe keeping
427 * http://www.democracycellproject.net/blog/archives/Clown%20car.jpg