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