1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/cstrike/extension.cpp Sun Jul 06 04:49:55 2008 +0000
1.3 @@ -0,0 +1,252 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod Counter-Strike:Source Extension
1.8 + * Copyright (C) 2004-2008 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 "timeleft.h"
1.38 +#include "iplayerinfo.h"
1.39 +
1.40 +/**
1.41 + * @file extension.cpp
1.42 + * @brief Implement extension code here.
1.43 + */
1.44 +
1.45 +SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
1.46 +
1.47 +CStrike g_CStrike; /**< Global singleton for extension's main interface */
1.48 +IBinTools *g_pBinTools = NULL;
1.49 +IGameConfig *g_pGameConf = NULL;
1.50 +IGameEventManager2 *gameevents = NULL;
1.51 +bool hooked_everything = false;
1.52 +int g_msgHintText = -1;
1.53 +
1.54 +SMEXT_LINK(&g_CStrike);
1.55 +
1.56 +extern sp_nativeinfo_t g_CSNatives[];
1.57 +
1.58 +bool CStrike::SDK_OnLoad(char *error, size_t maxlength, bool late)
1.59 +{
1.60 + if (strcmp(g_pSM->GetGameFolderName(), "cstrike") != 0)
1.61 + {
1.62 + snprintf(error, maxlength, "Cannot Load Cstrike Extension on mods other than CS:S");
1.63 + return false;
1.64 + }
1.65 +
1.66 + sharesys->AddDependency(myself, "bintools.ext", true, true);
1.67 +
1.68 + char conf_error[255];
1.69 + if (!gameconfs->LoadGameConfigFile("sm-cstrike.games", &g_pGameConf, conf_error, sizeof(conf_error)))
1.70 + {
1.71 + if (error)
1.72 + {
1.73 + snprintf(error, maxlength, "Could not read sm-cstrike.games.txt: %s", conf_error);
1.74 + }
1.75 + return false;
1.76 + }
1.77 +
1.78 + sharesys->AddNatives(myself, g_CSNatives);
1.79 + sharesys->RegisterLibrary(myself, "cstrike");
1.80 +
1.81 + if ((g_msgHintText = usermsgs->GetMessageIndex("HintText")) != -1)
1.82 + {
1.83 + sharesys->OverrideNatives(myself, g_CS_PrintHintText);
1.84 + }
1.85 +
1.86 + playerhelpers->RegisterCommandTargetProcessor(this);
1.87 +
1.88 + return true;
1.89 +}
1.90 +
1.91 +bool CStrike::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
1.92 +{
1.93 + GET_V_IFACE_CURRENT(GetEngineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
1.94 + GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
1.95 +
1.96 + return true;
1.97 +}
1.98 +
1.99 +void CStrike::SDK_OnUnload()
1.100 +{
1.101 + if (hooked_everything)
1.102 + {
1.103 + gameevents->RemoveListener(&g_TimeLeftEvents);
1.104 + SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, &g_TimeLeftEvents, &TimeLeftEvents::LevelInit, true);
1.105 + hooked_everything = false;
1.106 + }
1.107 + g_RegNatives.UnregisterAll();
1.108 + gameconfs->CloseGameConfigFile(g_pGameConf);
1.109 + playerhelpers->UnregisterCommandTargetProcessor(this);
1.110 +}
1.111 +
1.112 +void CStrike::SDK_OnAllLoaded()
1.113 +{
1.114 + gameevents->AddListener(&g_TimeLeftEvents, "round_start", true);
1.115 + gameevents->AddListener(&g_TimeLeftEvents, "round_end", true);
1.116 + SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, &g_TimeLeftEvents, &TimeLeftEvents::LevelInit, true);
1.117 + hooked_everything = true;
1.118 +
1.119 + SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
1.120 +}
1.121 +
1.122 +bool CStrike::QueryRunning(char *error, size_t maxlength)
1.123 +{
1.124 + SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
1.125 +
1.126 + return true;
1.127 +}
1.128 +
1.129 +bool CStrike::QueryInterfaceDrop(SMInterface *pInterface)
1.130 +{
1.131 + if (pInterface == g_pBinTools)
1.132 + {
1.133 + return false;
1.134 + }
1.135 +
1.136 + return IExtensionInterface::QueryInterfaceDrop(pInterface);
1.137 +}
1.138 +
1.139 +void CStrike::NotifyInterfaceDrop(SMInterface *pInterface)
1.140 +{
1.141 + g_RegNatives.UnregisterAll();
1.142 +}
1.143 +
1.144 +size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
1.145 +{
1.146 + va_list ap;
1.147 + va_start(ap, fmt);
1.148 + size_t len = vsnprintf(buffer, maxlength, fmt, ap);
1.149 + va_end(ap);
1.150 +
1.151 + if (len >= maxlength)
1.152 + {
1.153 + buffer[maxlength - 1] = '\0';
1.154 + return (maxlength - 1);
1.155 + }
1.156 + else
1.157 + {
1.158 + return len;
1.159 + }
1.160 +}
1.161 +
1.162 +bool CStrike::ProcessCommandTarget(cmd_target_info_t *info)
1.163 +{
1.164 + int max_clients;
1.165 + IPlayerInfo *pInfo;
1.166 + unsigned int team_index = 0;
1.167 + IGamePlayer *pPlayer, *pAdmin;
1.168 +
1.169 + if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
1.170 + {
1.171 + return false;
1.172 + }
1.173 +
1.174 + if (info->admin)
1.175 + {
1.176 + if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
1.177 + {
1.178 + return false;
1.179 + }
1.180 + if (!pAdmin->IsInGame())
1.181 + {
1.182 + return false;
1.183 + }
1.184 + }
1.185 + else
1.186 + {
1.187 + pAdmin = NULL;
1.188 + }
1.189 +
1.190 + if (strcmp(info->pattern, "@ct") == 0 || strcmp(info->pattern, "@cts") == 0)
1.191 + {
1.192 + team_index = 3;
1.193 + }
1.194 + else if (strcmp(info->pattern, "@t") == 0 || strcmp(info->pattern, "@ts") == 0)
1.195 + {
1.196 + team_index = 2;
1.197 + }
1.198 + else
1.199 + {
1.200 + return false;
1.201 + }
1.202 +
1.203 + info->num_targets = 0;
1.204 +
1.205 + max_clients = playerhelpers->GetMaxClients();
1.206 + for (int i = 1;
1.207 + i <= max_clients && (cell_t)info->num_targets < info->max_targets;
1.208 + i++)
1.209 + {
1.210 + if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
1.211 + {
1.212 + continue;
1.213 + }
1.214 + if (!pPlayer->IsInGame())
1.215 + {
1.216 + continue;
1.217 + }
1.218 + if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
1.219 + {
1.220 + continue;
1.221 + }
1.222 + if (pInfo->GetTeamIndex() != (int)team_index)
1.223 + {
1.224 + continue;
1.225 + }
1.226 + if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags)
1.227 + != COMMAND_TARGET_VALID)
1.228 + {
1.229 + continue;
1.230 + }
1.231 + info->targets[info->num_targets] = i;
1.232 + info->num_targets++;
1.233 + }
1.234 +
1.235 + if (info->num_targets == 0)
1.236 + {
1.237 + info->reason = COMMAND_TARGET_EMPTY_FILTER;
1.238 + }
1.239 + else
1.240 + {
1.241 + info->reason = COMMAND_TARGET_VALID;
1.242 + }
1.243 +
1.244 + info->target_name_style = COMMAND_TARGETNAME_RAW;
1.245 + if (team_index == 2)
1.246 + {
1.247 + UTIL_Format(info->target_name, info->target_name_maxlength, "Terrorists");
1.248 + }
1.249 + else if (team_index == 3)
1.250 + {
1.251 + UTIL_Format(info->target_name, info->target_name_maxlength, "CTs");
1.252 + }
1.253 +
1.254 + return true;
1.255 +}