extensions/cstrike/extension.cpp
author Drifter <rgr_02@comcast.net>
Sat May 26 18:31:23 2012 -0400 (2012-05-26)
changeset 3519 a116c415c73e
parent 3406 99d8c8b824e1
child 3630 b72450a8be42
permissions -rw-r--r--
Added support for CS:GO to cstrike extension (bug 5299, r=asherkin).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Counter-Strike:Source Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <sourcemod_version.h>
    33 #include "extension.h"
    34 #include "RegNatives.h"
    35 #include "timeleft.h"
    36 #include "iplayerinfo.h"
    37 #include "ISDKTools.h"
    38 #include "forwards.h"
    39 
    40 /**
    41  * @file extension.cpp
    42  * @brief Implement extension code here.
    43  */
    44 
    45 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
    46 
    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;
    53 
    54 SMEXT_LINK(&g_CStrike);
    55 
    56 extern sp_nativeinfo_t g_CSNatives[];
    57 
    58 ISDKTools *g_pSDKTools = NULL;
    59 
    60 bool CStrike::SDK_OnLoad(char *error, size_t maxlength, bool late)
    61 {
    62 	if (strcmp(g_pSM->GetGameFolderName(), "cstrike") != 0 && strcmp(g_pSM->GetGameFolderName(), "csgo") != 0)
    63 	{
    64 		snprintf(error, maxlength, "Cannot Load Cstrike Extension on mods other than CS:S and CS:GO");
    65 		return false;
    66 	}
    67 
    68 	sharesys->AddDependency(myself, "bintools.ext", true, true);
    69 	sharesys->AddDependency(myself, "sdktools.ext", false, true);
    70 
    71 	char conf_error[255];
    72 	if (!gameconfs->LoadGameConfigFile("sm-cstrike.games", &g_pGameConf, conf_error, sizeof(conf_error)))
    73 	{
    74 		if (error)
    75 		{
    76 			snprintf(error, maxlength, "Could not read sm-cstrike.games: %s", conf_error);
    77 		}
    78 		return false;
    79 	}
    80 
    81 	sharesys->AddNatives(myself, g_CSNatives);
    82 	sharesys->RegisterLibrary(myself, "cstrike");
    83 	plsys->AddPluginsListener(this);
    84 
    85 	playerhelpers->RegisterCommandTargetProcessor(this);
    86 
    87 	CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);
    88 
    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);
    93 
    94 
    95 	m_TerminateRoundDetourEnabled = false;
    96 	m_WeaponPriceDetourEnabled = false;
    97 	m_HandleBuyDetourEnabled = false;
    98 	m_CSWeaponDetourEnabled = false;
    99 
   100 	return true;
   101 }
   102 
   103 bool CStrike::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
   104 {
   105 	GET_V_IFACE_CURRENT(GetEngineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
   106 	GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
   107 
   108 	return true;
   109 }
   110 
   111 void CStrike::SDK_OnUnload()
   112 {
   113 	if (hooked_everything)
   114 	{
   115 		gameevents->RemoveListener(&g_TimeLeftEvents);
   116 		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, &g_TimeLeftEvents, &TimeLeftEvents::LevelInit, true);
   117 		hooked_everything = false;
   118 	}
   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);
   127 }
   128 
   129 void CStrike::SDK_OnAllLoaded()
   130 {
   131 	SM_GET_LATE_IFACE(SDKTOOLS, g_pSDKTools);
   132 	if (g_pSDKTools == NULL)
   133 	{
   134 		smutils->LogError(myself, "SDKTools interface not found. TerminateRound native disabled.");
   135 	}
   136 	else if (g_pSDKTools->GetInterfaceVersion() < 2)
   137 	{
   138 		//<psychonic> THIS ISN'T DA LIMBO STICK. LOW IS BAD
   139 		smutils->LogError(myself, "SDKTools interface is outdated. TerminateRound native disabled.");
   140 	}
   141 	gameevents->AddListener(&g_TimeLeftEvents, "round_start", true);
   142 	gameevents->AddListener(&g_TimeLeftEvents, "round_end", true);
   143 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, &g_TimeLeftEvents, &TimeLeftEvents::LevelInit, true);
   144 	hooked_everything = true;
   145 
   146 	SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
   147 }
   148 
   149 bool CStrike::QueryRunning(char *error, size_t maxlength)
   150 {
   151 	SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
   152 
   153 	return true;
   154 }
   155 
   156 bool CStrike::QueryInterfaceDrop(SMInterface *pInterface)
   157 {
   158 	if (pInterface == g_pBinTools)
   159 	{
   160 		return false;
   161 	}
   162 
   163 	return IExtensionInterface::QueryInterfaceDrop(pInterface);
   164 }
   165 
   166 void CStrike::NotifyInterfaceDrop(SMInterface *pInterface)
   167 {
   168 	g_RegNatives.UnregisterAll();
   169 }
   170 
   171 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
   172 {
   173 	va_list ap;
   174 	va_start(ap, fmt);
   175 	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
   176 	va_end(ap);
   177 
   178 	if (len >= maxlength)
   179 	{
   180 		buffer[maxlength - 1] = '\0';
   181 		return (maxlength - 1);
   182 	}
   183 	else
   184 	{
   185 		return len;
   186 	}
   187 }
   188 
   189 bool CStrike::ProcessCommandTarget(cmd_target_info_t *info)
   190 {
   191 	int max_clients;
   192 	IPlayerInfo *pInfo;
   193 	unsigned int team_index = 0;
   194 	IGamePlayer *pPlayer, *pAdmin;
   195 
   196 	if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
   197 	{
   198 		return false;
   199 	}
   200 
   201 	if (info->admin)
   202 	{
   203 		if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
   204 		{
   205 			return false;
   206 		}
   207 		if (!pAdmin->IsInGame())
   208 		{
   209 			return false;
   210 		}
   211 	}
   212 	else
   213 	{
   214 		pAdmin = NULL;
   215 	}
   216 
   217 	if (strcmp(info->pattern, "@ct") == 0 || strcmp(info->pattern, "@cts") == 0)
   218 	{
   219 		team_index = 3;
   220 	}
   221 	else if (strcmp(info->pattern, "@t") == 0 || strcmp(info->pattern, "@ts") == 0)
   222 	{
   223 		team_index = 2;
   224 	}
   225 	else
   226 	{
   227 		return false;
   228 	}
   229 
   230 	info->num_targets = 0;
   231 
   232 	max_clients = playerhelpers->GetMaxClients();
   233 	for (int i = 1; 
   234 		 i <= max_clients && (cell_t)info->num_targets < info->max_targets; 
   235 		 i++)
   236 	{
   237 		if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
   238 		{
   239 			continue;
   240 		}
   241 		if (!pPlayer->IsInGame())
   242 		{
   243 			continue;
   244 		}
   245 		if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
   246 		{
   247 			continue;
   248 		}
   249 		if (pInfo->GetTeamIndex() != (int)team_index)
   250 		{
   251 			continue;
   252 		}
   253 		if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags) 
   254 			!= COMMAND_TARGET_VALID)
   255 		{
   256 			continue;
   257 		}
   258 		info->targets[info->num_targets] = i;
   259 		info->num_targets++;
   260 	}
   261 
   262 	if (info->num_targets == 0)
   263 	{
   264 		info->reason = COMMAND_TARGET_EMPTY_FILTER;
   265 	}
   266 	else
   267 	{
   268 		info->reason = COMMAND_TARGET_VALID;
   269 	}
   270 
   271 	info->target_name_style = COMMAND_TARGETNAME_RAW;
   272 	if (team_index == 2)
   273 	{
   274 		UTIL_Format(info->target_name, info->target_name_maxlength, "Terrorists");
   275 	}
   276 	else if (team_index == 3)
   277 	{
   278 		UTIL_Format(info->target_name, info->target_name_maxlength, "CTs");
   279 	}
   280 
   281 	return true;
   282 }
   283 
   284 const char *CStrike::GetExtensionVerString()
   285 {
   286 	return SM_FULL_VERSION;
   287 }
   288 
   289 const char *CStrike::GetExtensionDateString()
   290 {
   291 	return SM_BUILD_TIMESTAMP;
   292 }
   293 
   294 void CStrike::OnPluginLoaded(IPlugin *plugin)
   295 {
   296 	if (!m_WeaponPriceDetourEnabled && g_pPriceForward->GetFunctionCount())
   297 	{
   298 		m_WeaponPriceDetourEnabled = CreateWeaponPriceDetour();
   299 		if (m_WeaponPriceDetourEnabled)
   300 			m_HandleBuyDetourEnabled = true;
   301 	}
   302 	if (!m_TerminateRoundDetourEnabled && g_pTerminateRoundForward->GetFunctionCount())
   303 	{
   304 		m_TerminateRoundDetourEnabled = CreateTerminateRoundDetour();
   305 	}
   306 	if (!m_HandleBuyDetourEnabled && g_pHandleBuyForward->GetFunctionCount())
   307 	{
   308 		m_HandleBuyDetourEnabled = CreateHandleBuyDetour();
   309 	}
   310 	if (!m_CSWeaponDetourEnabled && g_pCSWeaponDropForward->GetFunctionCount())
   311 	{
   312 		m_CSWeaponDetourEnabled = CreateCSWeaponDropDetour();
   313 	}
   314 }
   315 
   316 void CStrike::OnPluginUnloaded(IPlugin *plugin)
   317 {
   318 	if (m_WeaponPriceDetourEnabled && !g_pPriceForward->GetFunctionCount())
   319 	{
   320 		RemoveWeaponPriceDetour();
   321 		m_WeaponPriceDetourEnabled = false;
   322 	}
   323 	if (m_TerminateRoundDetourEnabled && !g_pTerminateRoundForward->GetFunctionCount())
   324 	{
   325 		RemoveTerminateRoundDetour();
   326 		m_TerminateRoundDetourEnabled = false;
   327 	}
   328 	if (m_HandleBuyDetourEnabled && !g_pHandleBuyForward->GetFunctionCount())
   329 	{
   330 		RemoveHandleBuyDetour();
   331 		m_HandleBuyDetourEnabled = false;
   332 	}
   333 	if (m_CSWeaponDetourEnabled && !g_pCSWeaponDropForward->GetFunctionCount())
   334 	{
   335 		RemoveCSWeaponDropDetour();
   336 		m_CSWeaponDetourEnabled = false;;
   337 	}
   338 }