extensions/cstrike/extension.cpp
author Matt Woodrow <pred@alliedmods.net>
Sat May 24 04:08:29 2008 +0000 (2008-05-24)
changeset 2069 80d56f1d58fe
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Added a load-block for incorrect mods to TF2 and cstrike extensions.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod Counter-Strike:Source Extension
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include "extension.h"
ds@1952
    33
#include "RegNatives.h"
ds@1952
    34
#include "timeleft.h"
ds@1952
    35
#include "iplayerinfo.h"
ds@1952
    36
ds@1952
    37
/**
ds@1952
    38
 * @file extension.cpp
ds@1952
    39
 * @brief Implement extension code here.
ds@1952
    40
 */
ds@1952
    41
ds@1952
    42
SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
ds@1952
    43
ds@1952
    44
CStrike g_CStrike;		/**< Global singleton for extension's main interface */
ds@1952
    45
IBinTools *g_pBinTools = NULL;
ds@1952
    46
IGameConfig *g_pGameConf = NULL;
ds@1952
    47
IGameEventManager2 *gameevents = NULL;
ds@1952
    48
bool hooked_everything = false;
ds@1952
    49
int g_msgHintText = -1;
ds@1952
    50
ds@1952
    51
SMEXT_LINK(&g_CStrike);
ds@1952
    52
ds@1952
    53
extern sp_nativeinfo_t g_CSNatives[];
ds@1952
    54
ds@1952
    55
bool CStrike::SDK_OnLoad(char *error, size_t maxlength, bool late)
ds@1952
    56
{
pred@2069
    57
	if (strcmp(g_pSM->GetGameFolderName(), "cstrike") != 0)
pred@2069
    58
	{
pred@2069
    59
		snprintf(error, maxlength, "Cannot Load Cstrike Extension on mods other than CS:S");
pred@2069
    60
		return false;
pred@2069
    61
	}
pred@2069
    62
ds@1952
    63
	sharesys->AddDependency(myself, "bintools.ext", true, true);
ds@1952
    64
ds@1952
    65
	char conf_error[255];
ds@1952
    66
	if (!gameconfs->LoadGameConfigFile("sm-cstrike.games", &g_pGameConf, conf_error, sizeof(conf_error)))
ds@1952
    67
	{
ds@1952
    68
		if (error)
ds@1952
    69
		{
ds@1952
    70
			snprintf(error, maxlength, "Could not read sm-cstrike.games.txt: %s", conf_error);
ds@1952
    71
		}
ds@1952
    72
		return false;
ds@1952
    73
	}
ds@1952
    74
ds@1952
    75
	sharesys->AddNatives(myself, g_CSNatives);
ds@1952
    76
	sharesys->RegisterLibrary(myself, "cstrike");
ds@1952
    77
ds@1952
    78
	if ((g_msgHintText = usermsgs->GetMessageIndex("HintText")) != -1)
ds@1952
    79
	{
ds@1952
    80
		sharesys->OverrideNatives(myself, g_CS_PrintHintText);
ds@1952
    81
	}
ds@1952
    82
ds@1952
    83
	playerhelpers->RegisterCommandTargetProcessor(this);
ds@1952
    84
ds@1952
    85
	return true;
ds@1952
    86
}
ds@1952
    87
ds@1952
    88
bool CStrike::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
ds@1952
    89
{
ds@1952
    90
	GET_V_IFACE_CURRENT(GetEngineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
ds@1952
    91
	GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
ds@1952
    92
ds@1952
    93
	return true;
ds@1952
    94
}
ds@1952
    95
ds@1952
    96
void CStrike::SDK_OnUnload()
ds@1952
    97
{
ds@1952
    98
	if (hooked_everything)
ds@1952
    99
	{
ds@1952
   100
		gameevents->RemoveListener(&g_TimeLeftEvents);
ds@1952
   101
		SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, &g_TimeLeftEvents, &TimeLeftEvents::LevelInit, true);
ds@1952
   102
		hooked_everything = false;
ds@1952
   103
	}
ds@1952
   104
	g_RegNatives.UnregisterAll();
ds@1952
   105
	gameconfs->CloseGameConfigFile(g_pGameConf);
ds@1952
   106
	playerhelpers->UnregisterCommandTargetProcessor(this);
ds@1952
   107
}
ds@1952
   108
ds@1952
   109
void CStrike::SDK_OnAllLoaded()
ds@1952
   110
{
ds@1952
   111
	gameevents->AddListener(&g_TimeLeftEvents, "round_start", true);
ds@1952
   112
	gameevents->AddListener(&g_TimeLeftEvents, "round_end", true);
ds@1952
   113
	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, &g_TimeLeftEvents, &TimeLeftEvents::LevelInit, true);
ds@1952
   114
	hooked_everything = true;
ds@1952
   115
ds@1952
   116
	SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
ds@1952
   117
}
ds@1952
   118
ds@1952
   119
bool CStrike::QueryRunning(char *error, size_t maxlength)
ds@1952
   120
{
ds@1952
   121
	SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
ds@1952
   122
ds@1952
   123
	return true;
ds@1952
   124
}
ds@1952
   125
ds@1952
   126
bool CStrike::QueryInterfaceDrop(SMInterface *pInterface)
ds@1952
   127
{
ds@1952
   128
	if (pInterface == g_pBinTools)
ds@1952
   129
	{
ds@1952
   130
		return false;
ds@1952
   131
	}
ds@1952
   132
ds@1952
   133
	return IExtensionInterface::QueryInterfaceDrop(pInterface);
ds@1952
   134
}
ds@1952
   135
ds@1952
   136
void CStrike::NotifyInterfaceDrop(SMInterface *pInterface)
ds@1952
   137
{
ds@1952
   138
	g_RegNatives.UnregisterAll();
ds@1952
   139
}
ds@1952
   140
ds@1952
   141
size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
ds@1952
   142
{
ds@1952
   143
	va_list ap;
ds@1952
   144
	va_start(ap, fmt);
ds@1952
   145
	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
ds@1952
   146
	va_end(ap);
ds@1952
   147
ds@1952
   148
	if (len >= maxlength)
ds@1952
   149
	{
ds@1952
   150
		buffer[maxlength - 1] = '\0';
ds@1952
   151
		return (maxlength - 1);
ds@1952
   152
	}
ds@1952
   153
	else
ds@1952
   154
	{
ds@1952
   155
		return len;
ds@1952
   156
	}
ds@1952
   157
}
ds@1952
   158
ds@1952
   159
bool CStrike::ProcessCommandTarget(cmd_target_info_t *info)
ds@1952
   160
{
ds@1952
   161
	int max_clients;
ds@1952
   162
	IPlayerInfo *pInfo;
ds@1952
   163
	unsigned int team_index = 0;
ds@1952
   164
	IGamePlayer *pPlayer, *pAdmin;
ds@1952
   165
ds@1952
   166
	if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
ds@1952
   167
	{
ds@1952
   168
		return false;
ds@1952
   169
	}
ds@1952
   170
ds@1952
   171
	if (info->admin)
ds@1952
   172
	{
ds@1952
   173
		if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
ds@1952
   174
		{
ds@1952
   175
			return false;
ds@1952
   176
		}
ds@1952
   177
		if (!pAdmin->IsInGame())
ds@1952
   178
		{
ds@1952
   179
			return false;
ds@1952
   180
		}
ds@1952
   181
	}
ds@1952
   182
	else
ds@1952
   183
	{
ds@1952
   184
		pAdmin = NULL;
ds@1952
   185
	}
ds@1952
   186
ds@1952
   187
	if (strcmp(info->pattern, "@ct") == 0 || strcmp(info->pattern, "@cts") == 0)
ds@1952
   188
	{
ds@1952
   189
		team_index = 3;
ds@1952
   190
	}
ds@1952
   191
	else if (strcmp(info->pattern, "@t") == 0 || strcmp(info->pattern, "@ts") == 0)
ds@1952
   192
	{
ds@1952
   193
		team_index = 2;
ds@1952
   194
	}
ds@1952
   195
	else
ds@1952
   196
	{
ds@1952
   197
		return false;
ds@1952
   198
	}
ds@1952
   199
ds@1952
   200
	info->num_targets = 0;
ds@1952
   201
ds@1952
   202
	max_clients = playerhelpers->GetMaxClients();
ds@1952
   203
	for (int i = 1; 
ds@1952
   204
		 i <= max_clients && (cell_t)info->num_targets < info->max_targets; 
ds@1952
   205
		 i++)
ds@1952
   206
	{
ds@1952
   207
		if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
ds@1952
   208
		{
ds@1952
   209
			continue;
ds@1952
   210
		}
ds@1952
   211
		if (!pPlayer->IsInGame())
ds@1952
   212
		{
ds@1952
   213
			continue;
ds@1952
   214
		}
ds@1952
   215
		if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
ds@1952
   216
		{
ds@1952
   217
			continue;
ds@1952
   218
		}
ds@1952
   219
		if (pInfo->GetTeamIndex() != (int)team_index)
ds@1952
   220
		{
ds@1952
   221
			continue;
ds@1952
   222
		}
ds@1952
   223
		if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags) 
ds@1952
   224
			!= COMMAND_TARGET_VALID)
ds@1952
   225
		{
ds@1952
   226
			continue;
ds@1952
   227
		}
ds@1952
   228
		info->targets[info->num_targets] = i;
ds@1952
   229
		info->num_targets++;
ds@1952
   230
	}
ds@1952
   231
ds@1952
   232
	if (info->num_targets == 0)
ds@1952
   233
	{
ds@1952
   234
		info->reason = COMMAND_TARGET_EMPTY_FILTER;
ds@1952
   235
	}
ds@1952
   236
	else
ds@1952
   237
	{
ds@1952
   238
		info->reason = COMMAND_TARGET_VALID;
ds@1952
   239
	}
ds@1952
   240
ds@1952
   241
	info->target_name_style = COMMAND_TARGETNAME_RAW;
ds@1952
   242
	if (team_index == 2)
ds@1952
   243
	{
ds@1952
   244
		UTIL_Format(info->target_name, info->target_name_maxlength, "Terrorists");
ds@1952
   245
	}
ds@1952
   246
	else if (team_index == 3)
ds@1952
   247
	{
ds@1952
   248
		UTIL_Format(info->target_name, info->target_name_maxlength, "CTs");
ds@1952
   249
	}
ds@1952
   250
ds@1952
   251
	return true;
ds@1952
   252
}