Update CS:GO team score gamedata.
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"
44 #include "ISDKHooks.h"
48 * @brief Implement extension code here.
53 IGameConfig *g_pGameConf = NULL;
55 IBinTools *g_pBinTools = NULL;
56 ISDKHooks *g_pSDKHooks = NULL;
58 SMEXT_LINK(&g_TF2Tools);
62 CGlobalVars *gpGlobals = NULL;
64 sm_sendprop_info_t *playerSharedOffset;
66 extern sp_nativeinfo_t g_TFNatives[];
70 SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int , int);
72 bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late)
74 if (strcmp(g_pSM->GetGameFolderName(), "tf") != 0)
76 UTIL_Format(error, maxlength, "Cannot Load TF2 Extension on mods other than TF2");
80 ServerClass *sc = UTIL_FindServerClass("CTFPlayer");
83 UTIL_Format(error, maxlength, "Could not find CTFPlayer server class");
87 playerSharedOffset = new sm_sendprop_info_t;
89 if (!UTIL_FindDataTable(sc->m_pTable, "DT_TFPlayerShared", playerSharedOffset, 0))
91 UTIL_Format(error, maxlength, "Could not find DT_TFPlayerShared data table");
95 sharesys->AddDependency(myself, "bintools.ext", true, true);
96 sharesys->AddDependency(myself, "sdkhooks.ext", true, true);
98 char conf_error[255] = "";
99 if (!gameconfs->LoadGameConfigFile("sm-tf2.games", &g_pGameConf, conf_error, sizeof(conf_error)))
103 UTIL_Format(error, maxlength, "Could not read sm-tf2.games.txt: %s", conf_error);
108 CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);
110 sharesys->AddNatives(myself, g_TFNatives);
111 sharesys->RegisterLibrary(myself, "tf2");
113 plsys->AddPluginsListener(this);
115 playerhelpers->RegisterCommandTargetProcessor(this);
116 playerhelpers->AddClientListener(this);
118 g_critForward = forwards->CreateForward("TF2_CalcIsAttackCritical", ET_Hook, 4, NULL, Param_Cell, Param_Cell, Param_String, Param_CellByRef);
119 g_isHolidayForward = forwards->CreateForward("TF2_OnIsHolidayActive", ET_Event, 2, NULL, Param_Cell, Param_CellByRef);
120 g_addCondForward = forwards->CreateForward("TF2_OnConditionAdded", ET_Ignore, 2, NULL, Param_Cell, Param_Cell);
121 g_removeCondForward = forwards->CreateForward("TF2_OnConditionRemoved", ET_Ignore, 2, NULL, Param_Cell, Param_Cell);
122 g_waitingPlayersStartForward = forwards->CreateForward("TF2_OnWaitingForPlayersStart", ET_Ignore, 0, NULL);
123 g_waitingPlayersEndForward = forwards->CreateForward("TF2_OnWaitingForPlayersEnd", ET_Ignore, 0, NULL);
124 g_teleportForward = forwards->CreateForward("TF2_OnPlayerTeleport", ET_Hook, 3, NULL, Param_Cell, Param_Cell, Param_CellByRef);
128 m_CritDetoursEnabled = false;
129 m_IsHolidayDetourEnabled = false;
130 m_CondChecksEnabled = false;
131 m_RulesDetoursEnabled = false;
132 m_TeleportDetourEnabled = false;
137 const char *TF2Tools::GetExtensionVerString()
139 return SOURCEMOD_VERSION;
142 const char *TF2Tools::GetExtensionDateString()
144 return SOURCEMOD_BUILD_TIME;
147 bool TF2Tools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
149 GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
151 gpGlobals = ismm->GetCGlobals();
153 SH_ADD_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
155 GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
157 GET_V_IFACE_CURRENT(GetEngineFactory, m_GameEventManager, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
158 m_GameEventManager->AddListener(this, "teamplay_restart_round", true);
163 void TF2Tools::SDK_OnUnload()
165 SH_REMOVE_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
167 g_RegNatives.UnregisterAll();
168 gameconfs->CloseGameConfigFile(g_pGameConf);
169 playerhelpers->UnregisterCommandTargetProcessor(this);
170 playerhelpers->RemoveClientListener(this);
172 plsys->RemovePluginsListener(this);
174 forwards->ReleaseForward(g_critForward);
175 forwards->ReleaseForward(g_isHolidayForward);
176 forwards->ReleaseForward(g_addCondForward);
177 forwards->ReleaseForward(g_removeCondForward);
178 forwards->ReleaseForward(g_waitingPlayersStartForward);
179 forwards->ReleaseForward(g_waitingPlayersEndForward);
180 forwards->ReleaseForward(g_teleportForward);
182 if (g_pSDKHooks != NULL)
184 g_pSDKHooks->RemoveEntityListener(&g_CritManager);
188 void TF2Tools::SDK_OnAllLoaded()
190 SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
191 SM_GET_LATE_IFACE(SDKHOOKS, g_pSDKHooks);
193 if (g_pSDKHooks != NULL)
195 g_pSDKHooks->AddEntityListener(&g_CritManager);
199 bool TF2Tools::RegisterConCommandBase(ConCommandBase *pVar)
201 return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
204 void TF2Tools::FireGameEvent( IGameEvent *event )
206 timersys->NotifyOfGameStart();
207 timersys->MapTimeLeftChanged();
210 bool TF2Tools::QueryRunning(char *error, size_t maxlength)
212 SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
213 SM_GET_LATE_IFACE(SDKHOOKS, g_pSDKHooks);
218 bool TF2Tools::QueryInterfaceDrop(SMInterface *pInterface)
220 if (pInterface == g_pBinTools)
225 if (pInterface == g_pSDKHooks)
230 return IExtensionInterface::QueryInterfaceDrop(pInterface);
233 void TF2Tools::NotifyInterfaceDrop(SMInterface *pInterface)
235 g_RegNatives.UnregisterAll();
238 void OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
240 g_resourceEntity = FindResourceEntity();
243 bool TF2Tools::ProcessCommandTarget(cmd_target_info_t *info)
247 unsigned int team_index = 0;
248 IGamePlayer *pPlayer, *pAdmin;
250 if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
257 if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
261 if (!pAdmin->IsInGame())
271 if (strcmp(info->pattern, "@red") == 0 )
275 else if (strcmp(info->pattern, "@blue") == 0)
284 info->num_targets = 0;
286 max_clients = playerhelpers->GetMaxClients();
288 i <= max_clients && (cell_t)info->num_targets < info->max_targets;
291 if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
295 if (!pPlayer->IsInGame())
299 if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
303 if (pInfo->GetTeamIndex() != (int)team_index)
307 if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags)
308 != COMMAND_TARGET_VALID)
312 info->targets[info->num_targets] = i;
316 if (info->num_targets == 0)
318 info->reason = COMMAND_TARGET_EMPTY_FILTER;
322 info->reason = COMMAND_TARGET_VALID;
325 info->target_name_style = COMMAND_TARGETNAME_RAW;
328 UTIL_Format(info->target_name, info->target_name_maxlength, "Red Team");
330 else if (team_index == 3)
332 UTIL_Format(info->target_name, info->target_name_maxlength, "Blue Team");
338 void TF2Tools::OnPluginLoaded(IPlugin *plugin)
340 if (!m_CritDetoursEnabled && g_critForward->GetFunctionCount())
342 m_CritDetoursEnabled = g_CritManager.TryEnable();
345 if (!m_IsHolidayDetourEnabled && g_isHolidayForward->GetFunctionCount())
347 m_IsHolidayDetourEnabled = InitialiseIsHolidayDetour();
350 if (!m_CondChecksEnabled
351 && ( g_addCondForward->GetFunctionCount() || g_removeCondForward->GetFunctionCount() )
354 m_CondChecksEnabled = InitialiseConditionChecks();
357 if (!m_RulesDetoursEnabled
358 && ( g_waitingPlayersStartForward->GetFunctionCount() || g_waitingPlayersEndForward->GetFunctionCount() )
361 m_RulesDetoursEnabled = InitialiseRulesDetours();
364 if (!m_TeleportDetourEnabled && g_teleportForward->GetFunctionCount())
366 m_TeleportDetourEnabled = InitialiseTeleporterDetour();
370 void TF2Tools::OnPluginUnloaded(IPlugin *plugin)
372 if (m_CritDetoursEnabled && !g_critForward->GetFunctionCount())
374 g_CritManager.Disable();
375 m_CritDetoursEnabled = false;
377 if (m_IsHolidayDetourEnabled && !g_isHolidayForward->GetFunctionCount())
379 RemoveIsHolidayDetour();
380 m_IsHolidayDetourEnabled = false;
382 if (m_CondChecksEnabled)
384 if (!g_addCondForward->GetFunctionCount() && !g_removeCondForward->GetFunctionCount())
386 RemoveConditionChecks();
387 m_CondChecksEnabled = false;
390 if (m_RulesDetoursEnabled)
392 if (!g_waitingPlayersStartForward->GetFunctionCount() && !g_waitingPlayersEndForward->GetFunctionCount())
394 RemoveRulesDetours();
395 m_RulesDetoursEnabled = false;
398 if (m_TeleportDetourEnabled && !g_teleportForward->GetFunctionCount())
400 RemoveTeleporterDetour();
401 m_TeleportDetourEnabled = false;
405 void TF2Tools::OnClientPutInServer(int client)
407 Conditions_OnClientPutInServer(client);
410 int FindResourceEntity()
412 return FindEntityByNetClass(-1, "CTFPlayerResource");
416 int FindEntityByNetClass(int start, const char *classname)
420 for (int i = ((start != -1) ? start : 0); i < gpGlobals->maxEntities; i++)
422 current = engine->PEntityOfEntIndex(i);
428 IServerNetworkable *network = current->GetNetworkable();
435 ServerClass *sClass = network->GetServerClass();
436 const char *name = sClass->GetName();
439 if (strcmp(name, classname) == 0)
448 TFClassType ClassnameToType(const char *classname)
450 static KTrie<TFClassType> trie;
451 static bool filled = false;
455 trie.insert("scout", TFClass_Scout);
456 trie.insert("sniper", TFClass_Sniper);
457 trie.insert("soldier", TFClass_Soldier);
458 trie.insert("demoman", TFClass_DemoMan);
459 trie.insert("demo", TFClass_DemoMan);
460 trie.insert("medic", TFClass_Medic);
461 trie.insert("heavy", TFClass_Heavy);
462 trie.insert("heavyweap", TFClass_Heavy);
463 trie.insert("heavyweapons", TFClass_Heavy);
464 trie.insert("hwg", TFClass_Heavy);
465 trie.insert("pyro", TFClass_Pyro);
466 trie.insert("spy", TFClass_Spy);
467 trie.insert("engineer", TFClass_Engineer);
468 trie.insert("engy", TFClass_Engineer);
475 if (!(value = trie.retrieve(classname)))
477 return TFClass_Unknown;
486 * A picture of a blue crab given to me as a gift and stored here for safe keeping
488 * http://sourcemod.net/Clown%20car.jpg