3 * =============================================================================
4 * SourceMod Counter-Strike:Source 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"
34 #include "RegNatives.h"
36 #include "iplayerinfo.h"
37 #include "ISDKTools.h"
42 * @brief Implement extension code here.
45 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
47 CStrike g_CStrike; /**< Global singleton for extension's main interface */
48 IBinTools *g_pBinTools = NULL;
49 IGameConfig *g_pGameConf = NULL;
50 IGameEventManager2 *gameevents = NULL;
51 bool hooked_everything = false;
52 int g_msgHintText = -1;
54 SMEXT_LINK(&g_CStrike);
56 extern sp_nativeinfo_t g_CSNatives[];
58 ISDKTools *g_pSDKTools = NULL;
60 bool CStrike::SDK_OnLoad(char *error, size_t maxlength, bool late)
62 if (strcmp(g_pSM->GetGameFolderName(), "cstrike") != 0 && strcmp(g_pSM->GetGameFolderName(), "csgo") != 0)
64 snprintf(error, maxlength, "Cannot Load Cstrike Extension on mods other than CS:S and CS:GO");
68 sharesys->AddDependency(myself, "bintools.ext", true, true);
69 sharesys->AddDependency(myself, "sdktools.ext", false, true);
72 if (!gameconfs->LoadGameConfigFile("sm-cstrike.games", &g_pGameConf, conf_error, sizeof(conf_error)))
76 snprintf(error, maxlength, "Could not read sm-cstrike.games: %s", conf_error);
81 sharesys->AddNatives(myself, g_CSNatives);
82 sharesys->RegisterLibrary(myself, "cstrike");
83 plsys->AddPluginsListener(this);
85 playerhelpers->RegisterCommandTargetProcessor(this);
87 CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);
89 g_pHandleBuyForward = forwards->CreateForward("CS_OnBuyCommand", ET_Event, 2, NULL, Param_Cell, Param_String);
90 g_pPriceForward = forwards->CreateForward("CS_OnGetWeaponPrice", ET_Event, 3, NULL, Param_Cell, Param_String, Param_CellByRef);
91 g_pTerminateRoundForward = forwards->CreateForward("CS_OnTerminateRound", ET_Event, 2, NULL, Param_FloatByRef, Param_CellByRef);
92 g_pCSWeaponDropForward = forwards->CreateForward("CS_OnCSWeaponDrop", ET_Event, 2, NULL, Param_Cell, Param_Cell);
95 m_TerminateRoundDetourEnabled = false;
96 m_WeaponPriceDetourEnabled = false;
97 m_HandleBuyDetourEnabled = false;
98 m_CSWeaponDetourEnabled = false;
103 bool CStrike::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
105 GET_V_IFACE_CURRENT(GetEngineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
106 GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
111 void CStrike::SDK_OnUnload()
113 if (hooked_everything)
115 gameevents->RemoveListener(&g_TimeLeftEvents);
116 SH_REMOVE_HOOK(IServerGameDLL, LevelInit, gamedll, SH_MEMBER(&g_TimeLeftEvents, &TimeLeftEvents::LevelInit), true);
117 hooked_everything = false;
119 g_RegNatives.UnregisterAll();
120 gameconfs->CloseGameConfigFile(g_pGameConf);
121 plsys->RemovePluginsListener(this);
122 playerhelpers->UnregisterCommandTargetProcessor(this);
123 forwards->ReleaseForward(g_pHandleBuyForward);
124 forwards->ReleaseForward(g_pPriceForward);
125 forwards->ReleaseForward(g_pTerminateRoundForward);
126 forwards->ReleaseForward(g_pCSWeaponDropForward);
129 void CStrike::SDK_OnAllLoaded()
131 SM_GET_LATE_IFACE(SDKTOOLS, g_pSDKTools);
132 if (g_pSDKTools == NULL)
134 smutils->LogError(myself, "SDKTools interface not found. TerminateRound native disabled.");
136 else if (g_pSDKTools->GetInterfaceVersion() < 2)
138 //<psychonic> THIS ISN'T DA LIMBO STICK. LOW IS BAD
139 smutils->LogError(myself, "SDKTools interface is outdated. TerminateRound native disabled.");
141 gameevents->AddListener(&g_TimeLeftEvents, "round_start", true);
142 gameevents->AddListener(&g_TimeLeftEvents, "round_end", true);
143 SH_ADD_HOOK(IServerGameDLL, LevelInit, gamedll, SH_MEMBER(&g_TimeLeftEvents, &TimeLeftEvents::LevelInit), true);
144 hooked_everything = true;
146 SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
149 bool CStrike::QueryRunning(char *error, size_t maxlength)
151 SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
156 bool CStrike::QueryInterfaceDrop(SMInterface *pInterface)
158 if (pInterface == g_pBinTools)
163 return IExtensionInterface::QueryInterfaceDrop(pInterface);
166 void CStrike::NotifyInterfaceDrop(SMInterface *pInterface)
168 g_RegNatives.UnregisterAll();
171 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
175 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
178 if (len >= maxlength)
180 buffer[maxlength - 1] = '\0';
181 return (maxlength - 1);
189 bool CStrike::ProcessCommandTarget(cmd_target_info_t *info)
193 unsigned int team_index = 0;
194 IGamePlayer *pPlayer, *pAdmin;
196 if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
203 if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
207 if (!pAdmin->IsInGame())
217 if (strcmp(info->pattern, "@ct") == 0 || strcmp(info->pattern, "@cts") == 0)
221 else if (strcmp(info->pattern, "@t") == 0 || strcmp(info->pattern, "@ts") == 0)
230 info->num_targets = 0;
232 max_clients = playerhelpers->GetMaxClients();
234 i <= max_clients && (cell_t)info->num_targets < info->max_targets;
237 if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
241 if (!pPlayer->IsInGame())
245 if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
249 if (pInfo->GetTeamIndex() != (int)team_index)
253 if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags)
254 != COMMAND_TARGET_VALID)
258 info->targets[info->num_targets] = i;
262 if (info->num_targets == 0)
264 info->reason = COMMAND_TARGET_EMPTY_FILTER;
268 info->reason = COMMAND_TARGET_VALID;
271 info->target_name_style = COMMAND_TARGETNAME_RAW;
274 UTIL_Format(info->target_name, info->target_name_maxlength, "Terrorists");
276 else if (team_index == 3)
278 UTIL_Format(info->target_name, info->target_name_maxlength, "CTs");
284 const char *CStrike::GetExtensionVerString()
286 return SM_VERSION_STRING;
289 const char *CStrike::GetExtensionDateString()
291 return SM_BUILD_TIMESTAMP;
294 void CStrike::OnPluginLoaded(IPlugin *plugin)
296 if (!m_WeaponPriceDetourEnabled && g_pPriceForward->GetFunctionCount())
298 m_WeaponPriceDetourEnabled = CreateWeaponPriceDetour();
299 if (m_WeaponPriceDetourEnabled)
300 m_HandleBuyDetourEnabled = true;
302 if (!m_TerminateRoundDetourEnabled && g_pTerminateRoundForward->GetFunctionCount())
304 m_TerminateRoundDetourEnabled = CreateTerminateRoundDetour();
306 if (!m_HandleBuyDetourEnabled && g_pHandleBuyForward->GetFunctionCount())
308 m_HandleBuyDetourEnabled = CreateHandleBuyDetour();
310 if (!m_CSWeaponDetourEnabled && g_pCSWeaponDropForward->GetFunctionCount())
312 m_CSWeaponDetourEnabled = CreateCSWeaponDropDetour();
316 void CStrike::OnPluginUnloaded(IPlugin *plugin)
318 if (m_WeaponPriceDetourEnabled && !g_pPriceForward->GetFunctionCount())
320 RemoveWeaponPriceDetour();
321 m_WeaponPriceDetourEnabled = false;
323 if (m_TerminateRoundDetourEnabled && !g_pTerminateRoundForward->GetFunctionCount())
325 RemoveTerminateRoundDetour();
326 m_TerminateRoundDetourEnabled = false;
328 if (m_HandleBuyDetourEnabled && !g_pHandleBuyForward->GetFunctionCount())
330 RemoveHandleBuyDetour();
331 m_HandleBuyDetourEnabled = false;
333 if (m_CSWeaponDetourEnabled && !g_pCSWeaponDropForward->GetFunctionCount())
335 RemoveCSWeaponDropDetour();
336 m_CSWeaponDetourEnabled = false;;