plugins/include/helpers.inc
author Scott Ehlert <ds@alliedmods.net>
Fri Apr 11 17:16:36 2008 +0000 (2008-04-11)
changeset 1990 de135d1f05b1
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Added more missing svn:keywords properties to some files (this should be the last of them before I never have to do this again)
Moved sm_memtable files in sqlite extension to sdk directory (to be consistent with topmenus)
Some extension source files had an incorrect extension name in their headers
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     5
 * =============================================================================
ds@1952
     6
 *
ds@1952
     7
 * This file is part of the SourceMod/SourcePawn SDK.
ds@1952
     8
 *
ds@1952
     9
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
    10
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    11
 * Free Software Foundation.
ds@1952
    12
 * 
ds@1952
    13
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    14
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    15
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    16
 * details.
ds@1952
    17
 *
ds@1952
    18
 * You should have received a copy of the GNU General Public License along with
ds@1952
    19
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    20
 *
ds@1952
    21
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    22
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    23
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    24
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    25
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    26
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    27
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    28
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    29
 *
ds@1952
    30
 * Version: $Id$
ds@1952
    31
 */
ds@1952
    32
 
ds@1952
    33
#if defined _helpers_included
ds@1952
    34
 #endinput
ds@1952
    35
#endif
ds@1952
    36
#define _helpers_included
ds@1952
    37
ds@1952
    38
/**
ds@1952
    39
 * Formats a user's info as log text.  This is usually not needed because
ds@1952
    40
 * %L can be used to auto-format client information into a string.
ds@1952
    41
 *
ds@1952
    42
 * @param client		Client index.
ds@1952
    43
 * @param buffer		Buffer for text.
ds@1952
    44
 * @param maxlength		Maximum length of text.
ds@1952
    45
 */
ds@1952
    46
stock FormatUserLogText(client, String:buffer[], maxlength)
ds@1952
    47
{
ds@1952
    48
	decl String:auth[32];
ds@1952
    49
	decl String:name[40];
ds@1952
    50
	
ds@1952
    51
	new userid = GetClientUserId(client);
ds@1952
    52
	if (!GetClientAuthString(client, auth, sizeof(auth)))
ds@1952
    53
	{
ds@1952
    54
		strcopy(auth, sizeof(auth), "UNKNOWN");
ds@1952
    55
	}
ds@1952
    56
	if (!GetClientName(client, name, sizeof(name)))
ds@1952
    57
	{
ds@1952
    58
		strcopy(name, sizeof(name), "UNKNOWN");
ds@1952
    59
	}
ds@1952
    60
	
ds@1952
    61
	/** Currently, no team stuff ... */
ds@1952
    62
	
ds@1952
    63
	Format(buffer, maxlength, "\"%s<%d><%s><>\"", name, userid, auth);
ds@1952
    64
}
ds@1952
    65
ds@1952
    66
/**
ds@1952
    67
 * Returns plugin handle from plugin filename.
ds@1952
    68
 *
ds@1952
    69
 * @param filename		Filename of the plugin to search for.
ds@1952
    70
 * @return				Handle to plugin if found, INVALID_HANDLE otherwise.
ds@1952
    71
 */
ds@1952
    72
stock Handle:FindPluginByFile(const String:filename[])
ds@1952
    73
{
ds@1952
    74
	decl String:buffer[256];
ds@1952
    75
	
ds@1952
    76
	new Handle:iter = GetPluginIterator();
ds@1952
    77
	new Handle:pl;
ds@1952
    78
	
ds@1952
    79
	while (MorePlugins(iter))
ds@1952
    80
	{
ds@1952
    81
		pl = ReadPlugin(iter);
ds@1952
    82
		
ds@1952
    83
		GetPluginFilename(pl, buffer, sizeof(buffer));
ds@1952
    84
		if (strcmp(buffer, filename, false) == 0)
ds@1952
    85
		{
ds@1952
    86
			CloseHandle(iter);
ds@1952
    87
			return pl;
ds@1952
    88
		}
ds@1952
    89
	}
ds@1952
    90
	
ds@1952
    91
	CloseHandle(iter);
ds@1952
    92
	
ds@1952
    93
	return INVALID_HANDLE;
ds@1952
    94
}
ds@1952
    95
ds@1952
    96
/**
ds@1952
    97
 * @deprecated Use FindTarget() or ProcessTargetString().
ds@1952
    98
 */
ds@1952
    99
#pragma deprecated Use FindTarget() or ProcessTargetString()
ds@1952
   100
stock SearchForClients(const String:pattern[], clients[], maxClients)
ds@1952
   101
{
ds@1952
   102
	new maxclients = GetMaxClients();
ds@1952
   103
	new total = 0;
ds@1952
   104
	
ds@1952
   105
	if (maxClients == 0)
ds@1952
   106
	{
ds@1952
   107
		return 0;
ds@1952
   108
	}
ds@1952
   109
	
ds@1952
   110
	if (pattern[0] == '#')
ds@1952
   111
	{
ds@1952
   112
		new input = StringToInt(pattern[1]);
ds@1952
   113
		if (!input)
ds@1952
   114
		{
ds@1952
   115
			decl String:name[65]
ds@1952
   116
			for (new i=1; i<=maxclients; i++)
ds@1952
   117
			{
ds@1952
   118
				if (!IsClientInGame(i))
ds@1952
   119
				{
ds@1952
   120
					continue;
ds@1952
   121
				}
ds@1952
   122
				GetClientName(i, name, sizeof(name));
ds@1952
   123
				if (strcmp(name, pattern, false) == 0)
ds@1952
   124
				{
ds@1952
   125
					clients[0] = i;
ds@1952
   126
					return 1;
ds@1952
   127
				}
ds@1952
   128
			}
ds@1952
   129
		} else {
ds@1952
   130
			new client = GetClientOfUserId(input);
ds@1952
   131
			if (client)
ds@1952
   132
			{
ds@1952
   133
				clients[0] = client;
ds@1952
   134
				return 1;
ds@1952
   135
			}
ds@1952
   136
		}
ds@1952
   137
	}
ds@1952
   138
	
ds@1952
   139
	decl String:name[65]
ds@1952
   140
	for (new i=1; i<=maxclients; i++)
ds@1952
   141
	{
ds@1952
   142
		if (!IsClientInGame(i))
ds@1952
   143
		{
ds@1952
   144
			continue;
ds@1952
   145
		}
ds@1952
   146
		GetClientName(i, name, sizeof(name));
ds@1952
   147
		if (StrContains(name, pattern, false) != -1)
ds@1952
   148
		{
ds@1952
   149
			clients[total++] = i;
ds@1952
   150
			if (total >= maxClients)
ds@1952
   151
			{
ds@1952
   152
				break;
ds@1952
   153
			}
ds@1952
   154
		}
ds@1952
   155
	}
ds@1952
   156
	
ds@1952
   157
	return total;
ds@1952
   158
}
ds@1952
   159
ds@1952
   160
/**
ds@1952
   161
 * Wraps ProcessTargetString() and handles producing error messages for
ds@1952
   162
 * bad targets.
ds@1952
   163
 *
ds@1952
   164
 * @param client	Client who issued command
ds@1952
   165
 * @param target	Client's target argument
ds@1952
   166
 * @param nobots	Optional. Set to true if bots should NOT be targetted
ds@1952
   167
 * @param immunity	Optional. Set to false to ignore target immunity.
ds@1952
   168
 * @return			Index of target client, or -1 on error.
ds@1952
   169
 */
ds@1952
   170
stock FindTarget(client, const String:target[], bool:nobots = false, bool:immunity = true)
ds@1952
   171
{
ds@1952
   172
	decl String:target_name[MAX_TARGET_LENGTH];
ds@1952
   173
	decl target_list[1], target_count, bool:tn_is_ml;
ds@1952
   174
	
ds@1952
   175
	new flags = COMMAND_FILTER_NO_MULTI;
ds@1952
   176
	if (nobots)
ds@1952
   177
	{
ds@1952
   178
		flags |= COMMAND_FILTER_NO_BOTS;
ds@1952
   179
	}
ds@1952
   180
	if (!immunity)
ds@1952
   181
	{
ds@1952
   182
		flags |= COMMAND_FILTER_NO_IMMUNITY;
ds@1952
   183
	}
ds@1952
   184
	
ds@1952
   185
	if ((target_count = ProcessTargetString(
ds@1952
   186
			target,
ds@1952
   187
			client, 
ds@1952
   188
			target_list, 
ds@1952
   189
			1, 
ds@1952
   190
			flags,
ds@1952
   191
			target_name,
ds@1952
   192
			sizeof(target_name),
ds@1952
   193
			tn_is_ml)) > 0)
ds@1952
   194
	{
ds@1952
   195
		return target_list[0];
ds@1952
   196
	}
ds@1952
   197
	else
ds@1952
   198
	{
ds@1952
   199
		ReplyToTargetError(client, target_count);
ds@1952
   200
		return -1;
ds@1952
   201
	}
ds@1952
   202
}
ds@1952
   203
ds@1952
   204
/**
ds@1952
   205
 * This function is no longer supported.  It has been replaced with ReadMapList(), 
ds@1952
   206
 * which uses a more unified caching and configuration mechanism.  This function also 
ds@1952
   207
 * has a bug where if the cvar contents changes, the fileTime change won't be recognized.
ds@1952
   208
 * 
ds@1952
   209
 * Loads a specified array with maps. The maps will be either loaded from mapcyclefile, or if supplied
ds@1952
   210
 * a cvar containing a file name. If the file in the cvar is bad, it will use mapcyclefile. The fileTime
ds@1952
   211
 * parameter is used to store a timestamp of the file. If specified, the file will only be reloaded if it
ds@1952
   212
 * has changed.
ds@1952
   213
 *
ds@1952
   214
 * @param array		Valid array handle, should be created with CreateArray(33) or larger. 
ds@1952
   215
 * @param fileTime	Variable containing the "last changed" time of the file. Used to avoid needless reloading.
ds@1952
   216
 * @param fileCvar	CVAR set to the file to be loaded. Optional.
ds@1952
   217
 * @return			Number of maps loaded or 0 if in error.
ds@1952
   218
 */
ds@1952
   219
#pragma deprecated Use ReadMapList() instead. 
ds@1952
   220
 stock LoadMaps(Handle:array, &fileTime = 0, Handle:fileCvar = INVALID_HANDLE)
ds@1952
   221
 { 
ds@1952
   222
 	decl String:mapPath[256], String:mapFile[64];
ds@1952
   223
 	new bool:fileFound = false;
ds@1952
   224
 	
ds@1952
   225
 	if (fileCvar != INVALID_HANDLE)
ds@1952
   226
 	{
ds@1952
   227
 		GetConVarString(fileCvar, mapFile, 64);
ds@1952
   228
	 	BuildPath(Path_SM, mapPath, sizeof(mapPath), mapFile);
ds@1952
   229
 		fileFound = FileExists(mapPath);
ds@1952
   230
 	}
ds@1952
   231
 
ds@1952
   232
 	if (!fileFound)
ds@1952
   233
 	{
ds@1952
   234
 		new Handle:mapCycleFile = FindConVar("mapcyclefile");
ds@1952
   235
 		GetConVarString(mapCycleFile, mapPath, sizeof(mapPath));
ds@1952
   236
 		fileFound = FileExists(mapPath);
ds@1952
   237
 	}
ds@1952
   238
 	
ds@1952
   239
 	if (!fileFound)
ds@1952
   240
 	{
ds@1952
   241
 		LogError("Failed to find a file to load maps from. No maps loaded.");
ds@1952
   242
 		ClearArray(array);
ds@1952
   243
 		
ds@1952
   244
 		return 0;		
ds@1952
   245
 	}
ds@1952
   246
 
ds@1952
   247
 	// If the file hasn't changed, there's no reason to reload
ds@1952
   248
 	// all of the maps.
ds@1952
   249
 	new newTime =  GetFileTime(mapPath, FileTime_LastChange);
ds@1952
   250
 	if (fileTime == newTime)
ds@1952
   251
 	{
ds@1952
   252
 		return GetArraySize(array);
ds@1952
   253
 	}
ds@1952
   254
 	
ds@1952
   255
 	fileTime = newTime;
ds@1952
   256
 	
ds@1952
   257
 	ClearArray(array);
ds@1952
   258
 
ds@1952
   259
 	new Handle:file = OpenFile(mapPath, "rt");
ds@1952
   260
 	if (file == INVALID_HANDLE)
ds@1952
   261
 	{
ds@1952
   262
 		LogError("Could not open file: %s", mapPath);
ds@1952
   263
 		
ds@1952
   264
 		return 0;
ds@1952
   265
 	}
ds@1952
   266
 
ds@1952
   267
	LogMessage("Loading maps from file: %s", mapPath);
ds@1952
   268
	
ds@1952
   269
 	decl String:buffer[64], len;
ds@1952
   270
 	while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
ds@1952
   271
 	{
ds@1952
   272
 		TrimString(buffer);
ds@1952
   273
 
ds@1952
   274
 		if ((len = StrContains(buffer, ".bsp", false)) != -1)
ds@1952
   275
 		{
ds@1952
   276
 			buffer[len] = '\0';
ds@1952
   277
 		}
ds@1952
   278
 
ds@1952
   279
 		if (buffer[0] == '\0' || !IsValidConVarChar(buffer[0]) || !IsMapValid(buffer))
ds@1952
   280
 		{
ds@1952
   281
 			continue;
ds@1952
   282
 		}
ds@1952
   283
 		
ds@1952
   284
 		if (FindStringInArray(array, buffer) != -1)
ds@1952
   285
 		{
ds@1952
   286
 			continue;
ds@1952
   287
 		}
ds@1952
   288
 
ds@1952
   289
 		PushArrayString(array, buffer);
ds@1952
   290
 	}
ds@1952
   291
 
ds@1952
   292
 	CloseHandle(file);
ds@1952
   293
 	return GetArraySize(array);
ds@1952
   294
}