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"
39 #include "CDetour/detours.h"
43 * @brief Implement extension code here.
48 IGameConfig *g_pGameConf = NULL;
50 IBinTools *g_pBinTools = NULL;
52 SMEXT_LINK(&g_TF2Tools);
56 CGlobalVars *gpGlobals = NULL;
58 sm_sendprop_info_t *playerSharedOffset;
60 extern sp_nativeinfo_t g_TFNatives[];
64 SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int , int);
66 bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late)
68 if (strcmp(g_pSM->GetGameFolderName(), "tf") != 0)
70 UTIL_Format(error, maxlength, "Cannot Load TF2 Extension on mods other than TF2");
74 ServerClass *sc = UTIL_FindServerClass("CTFPlayer");
77 UTIL_Format(error, maxlength, "Could not find CTFPlayer server class");
81 playerSharedOffset = new sm_sendprop_info_t;
83 if (!UTIL_FindDataTable(sc->m_pTable, "DT_TFPlayerShared", playerSharedOffset, 0))
85 UTIL_Format(error, maxlength, "Could not find DT_TFPlayerShared data table");
89 sharesys->AddDependency(myself, "bintools.ext", true, true);
91 char conf_error[255] = "";
92 if (!gameconfs->LoadGameConfigFile("sm-tf2.games", &g_pGameConf, conf_error, sizeof(conf_error)))
96 UTIL_Format(error, maxlength, "Could not read sm-tf2.games.txt: %s", conf_error);
101 CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);
103 sharesys->AddNatives(myself, g_TFNatives);
104 sharesys->RegisterLibrary(myself, "tf2");
106 plsys->AddPluginsListener(this);
108 playerhelpers->RegisterCommandTargetProcessor(this);
110 g_critForward = forwards->CreateForward("TF2_CalcIsAttackCritical", ET_Hook, 4, NULL, Param_Cell, Param_Cell, Param_String, Param_CellByRef);
114 m_DetoursEnabled = false;
119 const char *TF2Tools::GetExtensionVerString()
121 return SM_FULL_VERSION;
124 const char *TF2Tools::GetExtensionDateString()
126 return SM_BUILD_TIMESTAMP;
129 bool TF2Tools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
131 GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
133 gpGlobals = ismm->GetCGlobals();
135 SH_ADD_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
137 GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
139 GET_V_IFACE_CURRENT(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
141 GET_V_IFACE_CURRENT(GetEngineFactory, m_GameEventManager, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
142 m_GameEventManager->AddListener(this, "teamplay_restart_round", true);
147 void TF2Tools::SDK_OnUnload()
149 SH_REMOVE_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
151 g_RegNatives.UnregisterAll();
152 gameconfs->CloseGameConfigFile(g_pGameConf);
153 playerhelpers->UnregisterCommandTargetProcessor(this);
155 plsys->RemovePluginsListener(this);
157 forwards->ReleaseForward(g_critForward);
160 void TF2Tools::SDK_OnAllLoaded()
162 SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
165 bool TF2Tools::RegisterConCommandBase(ConCommandBase *pVar)
167 return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
170 void TF2Tools::FireGameEvent( IGameEvent *event )
172 timersys->NotifyOfGameStart();
173 timersys->MapTimeLeftChanged();
176 bool TF2Tools::QueryRunning(char *error, size_t maxlength)
178 SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
183 bool TF2Tools::QueryInterfaceDrop(SMInterface *pInterface)
185 if (pInterface == g_pBinTools)
190 return IExtensionInterface::QueryInterfaceDrop(pInterface);
193 void TF2Tools::NotifyInterfaceDrop(SMInterface *pInterface)
195 g_RegNatives.UnregisterAll();
198 void OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
200 g_resourceEntity = FindResourceEntity();
203 bool TF2Tools::ProcessCommandTarget(cmd_target_info_t *info)
207 unsigned int team_index = 0;
208 IGamePlayer *pPlayer, *pAdmin;
210 if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
217 if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
221 if (!pAdmin->IsInGame())
231 if (strcmp(info->pattern, "@red") == 0 )
235 else if (strcmp(info->pattern, "@blue") == 0)
244 info->num_targets = 0;
246 max_clients = playerhelpers->GetMaxClients();
248 i <= max_clients && (cell_t)info->num_targets < info->max_targets;
251 if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
255 if (!pPlayer->IsInGame())
259 if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
263 if (pInfo->GetTeamIndex() != (int)team_index)
267 if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags)
268 != COMMAND_TARGET_VALID)
272 info->targets[info->num_targets] = i;
276 if (info->num_targets == 0)
278 info->reason = COMMAND_TARGET_EMPTY_FILTER;
282 info->reason = COMMAND_TARGET_VALID;
285 info->target_name_style = COMMAND_TARGETNAME_RAW;
288 UTIL_Format(info->target_name, info->target_name_maxlength, "Red Team");
290 else if (team_index == 3)
292 UTIL_Format(info->target_name, info->target_name_maxlength, "Blue Team");
298 void TF2Tools::OnPluginLoaded(IPlugin *plugin)
300 if (!m_DetoursEnabled && g_critForward->GetFunctionCount())
303 m_DetoursEnabled = true;
307 void TF2Tools::OnPluginUnloaded(IPlugin *plugin)
309 if (m_DetoursEnabled && !g_critForward->GetFunctionCount())
312 m_DetoursEnabled = false;
315 int FindResourceEntity()
317 return FindEntityByNetClass(-1, "CTFPlayerResource");
321 int FindEntityByNetClass(int start, const char *classname)
325 for (int i = ((start != -1) ? start : 0); i < gpGlobals->maxEntities; i++)
327 current = engine->PEntityOfEntIndex(i);
333 IServerNetworkable *network = current->GetNetworkable();
340 ServerClass *sClass = network->GetServerClass();
341 const char *name = sClass->GetName();
344 if (strcmp(name, classname) == 0)
353 TFClassType ClassnameToType(const char *classname)
355 static KTrie<TFClassType> trie;
356 static bool filled = false;
360 trie.insert("scout", TFClass_Scout);
361 trie.insert("sniper", TFClass_Sniper);
362 trie.insert("soldier", TFClass_Soldier);
363 trie.insert("demoman", TFClass_DemoMan);
364 trie.insert("demo", TFClass_DemoMan);
365 trie.insert("medic", TFClass_Medic);
366 trie.insert("heavy", TFClass_Heavy);
367 trie.insert("hwg", TFClass_Heavy);
368 trie.insert("pyro", TFClass_Pyro);
369 trie.insert("spy", TFClass_Spy);
370 trie.insert("engineer", TFClass_Engineer);
371 trie.insert("engy", TFClass_Engineer);
378 if (!(value = trie.retrieve(classname)))
380 return TFClass_Unknown;
389 * A picture of a blue crab given to me as a gift and stored here for safe keeping
391 * http://www.democracycellproject.net/blog/archives/Clown%20car.jpg