extensions/tf2/extension.cpp
changeset 1945 13106cdc0a3c
child 1947 e73a1e51833b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/tf2/extension.cpp	Wed Mar 26 03:52:37 2008 +0000
     1.3 @@ -0,0 +1,229 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod Counter-Strike:Source Extension
     1.8 + * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#include "extension.h"
    1.36 +#include "RegNatives.h"
    1.37 +#include "iplayerinfo.h"
    1.38 +
    1.39 +/**
    1.40 + * @file extension.cpp
    1.41 + * @brief Implement extension code here.
    1.42 + */
    1.43 +
    1.44 +
    1.45 +TF2Tools g_TF2Tools;
    1.46 +IGameConfig *g_pGameConf = NULL;
    1.47 +
    1.48 +IBinTools *g_pBinTools = NULL;
    1.49 +
    1.50 +SMEXT_LINK(&g_TF2Tools);
    1.51 +
    1.52 +SendProp *playerSharedOffset;
    1.53 +
    1.54 +extern sp_nativeinfo_t g_TFNatives[];
    1.55 +
    1.56 +bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late)
    1.57 +{
    1.58 +	sharesys->AddDependency(myself, "bintools.ext", true, true);
    1.59 +
    1.60 +	char conf_error[255];
    1.61 +	if (!gameconfs->LoadGameConfigFile("sm-tf2.games", &g_pGameConf, conf_error, sizeof(conf_error)))
    1.62 +	{
    1.63 +		if (error)
    1.64 +		{
    1.65 +			snprintf(error, maxlength, "Could not read sm-tf2.games.txt: %s", conf_error);
    1.66 +		}
    1.67 +		return false;
    1.68 +	}
    1.69 +
    1.70 +	sharesys->AddNatives(myself, g_TFNatives);
    1.71 +	sharesys->RegisterLibrary(myself, "tf2");
    1.72 +
    1.73 +	playerSharedOffset = gamehelpers->FindInSendTable("CTFPlayer", "DT_TFPlayerShared");
    1.74 +
    1.75 +	playerhelpers->RegisterCommandTargetProcessor(this);
    1.76 +
    1.77 +	return true;
    1.78 +}
    1.79 +
    1.80 +bool TF2Tools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
    1.81 +{
    1.82 +	GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
    1.83 +
    1.84 +	return true;
    1.85 +}
    1.86 +
    1.87 +void TF2Tools::SDK_OnUnload()
    1.88 +{
    1.89 +	g_RegNatives.UnregisterAll();
    1.90 +	gameconfs->CloseGameConfigFile(g_pGameConf);
    1.91 +}
    1.92 +
    1.93 +void TF2Tools::SDK_OnAllLoaded()
    1.94 +{
    1.95 +	SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
    1.96 +}
    1.97 +
    1.98 +bool TF2Tools::QueryRunning(char *error, size_t maxlength)
    1.99 +{
   1.100 +	SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
   1.101 +
   1.102 +	return true;
   1.103 +}
   1.104 +
   1.105 +bool TF2Tools::QueryInterfaceDrop(SMInterface *pInterface)
   1.106 +{
   1.107 +	if (pInterface == g_pBinTools)
   1.108 +	{
   1.109 +		return false;
   1.110 +	}
   1.111 +
   1.112 +	return IExtensionInterface::QueryInterfaceDrop(pInterface);
   1.113 +}
   1.114 +
   1.115 +void TF2Tools::NotifyInterfaceDrop(SMInterface *pInterface)
   1.116 +{
   1.117 +	g_RegNatives.UnregisterAll();
   1.118 +}
   1.119 +
   1.120 +size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
   1.121 +{
   1.122 +	va_list ap;
   1.123 +	va_start(ap, fmt);
   1.124 +	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
   1.125 +	va_end(ap);
   1.126 +
   1.127 +	if (len >= maxlength)
   1.128 +	{
   1.129 +		buffer[maxlength - 1] = '\0';
   1.130 +		return (maxlength - 1);
   1.131 +	}
   1.132 +	else
   1.133 +	{
   1.134 +		return len;
   1.135 +	}
   1.136 +}
   1.137 +
   1.138 +bool TF2Tools::ProcessCommandTarget(cmd_target_info_t *info)
   1.139 +{
   1.140 +	int max_clients;
   1.141 +	IPlayerInfo *pInfo;
   1.142 +	unsigned int team_index = 0;
   1.143 +	IGamePlayer *pPlayer, *pAdmin;
   1.144 +
   1.145 +	if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
   1.146 +	{
   1.147 +		return false;
   1.148 +	}
   1.149 +
   1.150 +	if (info->admin)
   1.151 +	{
   1.152 +		if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
   1.153 +		{
   1.154 +			return false;
   1.155 +		}
   1.156 +		if (!pAdmin->IsInGame())
   1.157 +		{
   1.158 +			return false;
   1.159 +		}
   1.160 +	}
   1.161 +	else
   1.162 +	{
   1.163 +		pAdmin = NULL;
   1.164 +	}
   1.165 +
   1.166 +	if (strcmp(info->pattern, "@red") == 0 )
   1.167 +	{
   1.168 +		team_index = 2;
   1.169 +	}
   1.170 +	else if (strcmp(info->pattern, "@blue") == 0)
   1.171 +	{
   1.172 +		team_index = 3;
   1.173 +	}
   1.174 +	else
   1.175 +	{
   1.176 +		return false;
   1.177 +	}
   1.178 +
   1.179 +	info->num_targets = 0;
   1.180 +
   1.181 +	max_clients = playerhelpers->GetMaxClients();
   1.182 +	for (int i = 1; 
   1.183 +		 i <= max_clients && (cell_t)info->num_targets < info->max_targets; 
   1.184 +		 i++)
   1.185 +	{
   1.186 +		if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
   1.187 +		{
   1.188 +			continue;
   1.189 +		}
   1.190 +		if (!pPlayer->IsInGame())
   1.191 +		{
   1.192 +			continue;
   1.193 +		}
   1.194 +		if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
   1.195 +		{
   1.196 +			continue;
   1.197 +		}
   1.198 +		if (pInfo->GetTeamIndex() != (int)team_index)
   1.199 +		{
   1.200 +			continue;
   1.201 +		}
   1.202 +		if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags) 
   1.203 +			!= COMMAND_TARGET_VALID)
   1.204 +		{
   1.205 +			continue;
   1.206 +		}
   1.207 +		info->targets[info->num_targets] = i;
   1.208 +		info->num_targets++;
   1.209 +	}
   1.210 +
   1.211 +	if (info->num_targets == 0)
   1.212 +	{
   1.213 +		info->reason = COMMAND_TARGET_EMPTY_FILTER;
   1.214 +	}
   1.215 +	else
   1.216 +	{
   1.217 +		info->reason = COMMAND_TARGET_VALID;
   1.218 +	}
   1.219 +
   1.220 +	info->target_name_style = COMMAND_TARGETNAME_RAW;
   1.221 +	if (team_index == 2)
   1.222 +	{
   1.223 +		UTIL_Format(info->target_name, info->target_name_maxlength, "Red Team");
   1.224 +	}
   1.225 +	else if (team_index == 3)
   1.226 +	{
   1.227 +		UTIL_Format(info->target_name, info->target_name_maxlength, "Blue Team");
   1.228 +	}
   1.229 +
   1.230 +	return true;
   1.231 +}
   1.232 +