More Windows build fix.
3 * =============================================================================
4 * SourceMod Team Fortress 2 Extension
5 * Copyright (C) 2004-2011 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 "conditions.h"
41 #include "gameplayrules.h"
42 #include "teleporter.h"
43 #include "CDetour/detours.h"
47 * @brief Implement extension code here.
52 IGameConfig *g_pGameConf = NULL;
54 IBinTools *g_pBinTools = NULL;
56 SMEXT_LINK(&g_TF2Tools);
60 CGlobalVars *gpGlobals = NULL;
62 sm_sendprop_info_t *playerSharedOffset;
64 extern sp_nativeinfo_t g_TFNatives[];
68 SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int , int);
70 bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late)
72 if (strcmp(g_pSM->GetGameFolderName(), "tf") != 0)
74 UTIL_Format(error, maxlength, "Cannot Load TF2 Extension on mods other than TF2");
78 ServerClass *sc = UTIL_FindServerClass("CTFPlayer");
81 UTIL_Format(error, maxlength, "Could not find CTFPlayer server class");
85 playerSharedOffset = new sm_sendprop_info_t;
87 if (!UTIL_FindDataTable(sc->m_pTable, "DT_TFPlayerShared", playerSharedOffset, 0))
89 UTIL_Format(error, maxlength, "Could not find DT_TFPlayerShared data table");
93 sharesys->AddDependency(myself, "bintools.ext", true, true);
95 char conf_error[255] = "";
96 if (!gameconfs->LoadGameConfigFile("sm-tf2.games", &g_pGameConf, conf_error, sizeof(conf_error)))
100 UTIL_Format(error, maxlength, "Could not read sm-tf2.games.txt: %s", conf_error);
105 CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);
107 sharesys->AddNatives(myself, g_TFNatives);
108 sharesys->RegisterLibrary(myself, "tf2");
110 plsys->AddPluginsListener(this);
112 playerhelpers->RegisterCommandTargetProcessor(this);
113 playerhelpers->AddClientListener(this);
115 g_critForward = forwards->CreateForward("TF2_CalcIsAttackCritical", ET_Hook, 4, NULL, Param_Cell, Param_Cell, Param_String, Param_CellByRef);
116 g_isHolidayForward = forwards->CreateForward("TF2_OnIsHolidayActive", ET_Event, 2, NULL, Param_Cell, Param_CellByRef);
117 g_addCondForward = forwards->CreateForward("TF2_OnConditionAdded", ET_Ignore, 2, NULL, Param_Cell, Param_Cell);
118 g_removeCondForward = forwards->CreateForward("TF2_OnConditionRemoved", ET_Ignore, 2, NULL, Param_Cell, Param_Cell);
119 g_waitingPlayersStartForward = forwards->CreateForward("TF2_OnWaitingForPlayersStart", ET_Ignore, 0, NULL);
120 g_waitingPlayersEndForward = forwards->CreateForward("TF2_OnWaitingForPlayersEnd", ET_Ignore, 0, NULL);
121 g_teleportForward = forwards->CreateForward("TF2_OnPlayerTeleport", ET_Hook, 3, NULL, Param_Cell, Param_Cell, Param_CellByRef);
125 m_CritDetoursEnabled = false;
126 m_IsHolidayDetourEnabled = false;
127 m_CondChecksEnabled = false;
128 m_RulesDetoursEnabled = false;
129 m_TeleportDetourEnabled = false;
134 const char *TF2Tools::GetExtensionVerString()
136 return SM_VERSION_STRING;
139 const char *TF2Tools::GetExtensionDateString()
141 return SM_BUILD_TIMESTAMP;
144 bool TF2Tools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
146 GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
148 gpGlobals = ismm->GetCGlobals();
150 SH_ADD_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
152 GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
154 GET_V_IFACE_CURRENT(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
156 GET_V_IFACE_CURRENT(GetEngineFactory, m_GameEventManager, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
157 m_GameEventManager->AddListener(this, "teamplay_restart_round", true);
162 void TF2Tools::SDK_OnUnload()
164 SH_REMOVE_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
166 g_RegNatives.UnregisterAll();
167 gameconfs->CloseGameConfigFile(g_pGameConf);
168 playerhelpers->UnregisterCommandTargetProcessor(this);
169 playerhelpers->RemoveClientListener(this);
171 plsys->RemovePluginsListener(this);
173 forwards->ReleaseForward(g_critForward);
174 forwards->ReleaseForward(g_isHolidayForward);
175 forwards->ReleaseForward(g_addCondForward);
176 forwards->ReleaseForward(g_removeCondForward);
177 forwards->ReleaseForward(g_waitingPlayersStartForward);
178 forwards->ReleaseForward(g_waitingPlayersEndForward);
179 forwards->ReleaseForward(g_teleportForward);
182 void TF2Tools::SDK_OnAllLoaded()
184 SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
187 bool TF2Tools::RegisterConCommandBase(ConCommandBase *pVar)
189 return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
192 void TF2Tools::FireGameEvent( IGameEvent *event )
194 timersys->NotifyOfGameStart();
195 timersys->MapTimeLeftChanged();
198 bool TF2Tools::QueryRunning(char *error, size_t maxlength)
200 SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
205 bool TF2Tools::QueryInterfaceDrop(SMInterface *pInterface)
207 if (pInterface == g_pBinTools)
212 return IExtensionInterface::QueryInterfaceDrop(pInterface);
215 void TF2Tools::NotifyInterfaceDrop(SMInterface *pInterface)
217 g_RegNatives.UnregisterAll();
220 void OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
222 g_resourceEntity = FindResourceEntity();
225 bool TF2Tools::ProcessCommandTarget(cmd_target_info_t *info)
229 unsigned int team_index = 0;
230 IGamePlayer *pPlayer, *pAdmin;
232 if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
239 if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
243 if (!pAdmin->IsInGame())
253 if (strcmp(info->pattern, "@red") == 0 )
257 else if (strcmp(info->pattern, "@blue") == 0)
266 info->num_targets = 0;
268 max_clients = playerhelpers->GetMaxClients();
270 i <= max_clients && (cell_t)info->num_targets < info->max_targets;
273 if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
277 if (!pPlayer->IsInGame())
281 if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
285 if (pInfo->GetTeamIndex() != (int)team_index)
289 if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags)
290 != COMMAND_TARGET_VALID)
294 info->targets[info->num_targets] = i;
298 if (info->num_targets == 0)
300 info->reason = COMMAND_TARGET_EMPTY_FILTER;
304 info->reason = COMMAND_TARGET_VALID;
307 info->target_name_style = COMMAND_TARGETNAME_RAW;
310 UTIL_Format(info->target_name, info->target_name_maxlength, "Red Team");
312 else if (team_index == 3)
314 UTIL_Format(info->target_name, info->target_name_maxlength, "Blue Team");
320 void TF2Tools::OnPluginLoaded(IPlugin *plugin)
322 if (!m_CritDetoursEnabled && g_critForward->GetFunctionCount())
324 m_CritDetoursEnabled = InitialiseCritDetours();
327 if (!m_IsHolidayDetourEnabled && g_isHolidayForward->GetFunctionCount())
329 m_IsHolidayDetourEnabled = InitialiseIsHolidayDetour();
332 if (!m_CondChecksEnabled
333 && ( g_addCondForward->GetFunctionCount() || g_removeCondForward->GetFunctionCount() )
336 m_CondChecksEnabled = InitialiseConditionChecks();
339 if (!m_RulesDetoursEnabled
340 && ( g_waitingPlayersStartForward->GetFunctionCount() || g_waitingPlayersEndForward->GetFunctionCount() )
343 m_RulesDetoursEnabled = InitialiseRulesDetours();
346 if (!m_TeleportDetourEnabled && g_teleportForward->GetFunctionCount())
348 m_TeleportDetourEnabled = InitialiseTeleporterDetour();
352 void TF2Tools::OnPluginUnloaded(IPlugin *plugin)
354 if (m_CritDetoursEnabled && !g_critForward->GetFunctionCount())
357 m_CritDetoursEnabled = false;
359 if (m_IsHolidayDetourEnabled && !g_isHolidayForward->GetFunctionCount())
361 RemoveIsHolidayDetour();
362 m_IsHolidayDetourEnabled = false;
364 if (m_CondChecksEnabled)
366 if (!g_addCondForward->GetFunctionCount() && !g_removeCondForward->GetFunctionCount())
368 RemoveConditionChecks();
369 m_CondChecksEnabled = false;
372 if (m_RulesDetoursEnabled)
374 if (!g_waitingPlayersStartForward->GetFunctionCount() && !g_waitingPlayersEndForward->GetFunctionCount())
376 RemoveRulesDetours();
377 m_RulesDetoursEnabled = false;
380 if (m_TeleportDetourEnabled && !g_teleportForward->GetFunctionCount())
382 RemoveTeleporterDetour();
383 m_TeleportDetourEnabled = false;
387 void TF2Tools::OnClientPutInServer(int client)
389 Conditions_OnClientPutInServer(client);
392 int FindResourceEntity()
394 return FindEntityByNetClass(-1, "CTFPlayerResource");
398 int FindEntityByNetClass(int start, const char *classname)
402 for (int i = ((start != -1) ? start : 0); i < gpGlobals->maxEntities; i++)
404 current = engine->PEntityOfEntIndex(i);
410 IServerNetworkable *network = current->GetNetworkable();
417 ServerClass *sClass = network->GetServerClass();
418 const char *name = sClass->GetName();
421 if (strcmp(name, classname) == 0)
430 TFClassType ClassnameToType(const char *classname)
432 static KTrie<TFClassType> trie;
433 static bool filled = false;
437 trie.insert("scout", TFClass_Scout);
438 trie.insert("sniper", TFClass_Sniper);
439 trie.insert("soldier", TFClass_Soldier);
440 trie.insert("demoman", TFClass_DemoMan);
441 trie.insert("demo", TFClass_DemoMan);
442 trie.insert("medic", TFClass_Medic);
443 trie.insert("heavy", TFClass_Heavy);
444 trie.insert("heavyweap", TFClass_Heavy);
445 trie.insert("heavyweapons", TFClass_Heavy);
446 trie.insert("hwg", TFClass_Heavy);
447 trie.insert("pyro", TFClass_Pyro);
448 trie.insert("spy", TFClass_Spy);
449 trie.insert("engineer", TFClass_Engineer);
450 trie.insert("engy", TFClass_Engineer);
457 if (!(value = trie.retrieve(classname)))
459 return TFClass_Unknown;
468 * A picture of a blue crab given to me as a gift and stored here for safe keeping
470 * http://sourcemod.net/Clown%20car.jpg