- added beefy new ReadMapList() native

- admin menu now uses ReadMapList()
- added UTIL_TrimWhitespace() to stringutils
- moved GetFileTime() implementation to ILibrarySys
- cleaned up sorting include a bit
- removed adminmenu_maplist.ini, since it's specified by maplists.cfg
- added maplists.cfg

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401744
This commit is contained in:
David Anderson
2007-12-02 02:10:37 +00:00
parent da9168082c
commit a8ecd7fea4
18 changed files with 880 additions and 131 deletions
+4 -1
View File
@@ -49,7 +49,6 @@ public Plugin:myinfo =
new Handle:hTopMenu = INVALID_HANDLE;
new Handle:g_MapList;
new g_mapFileTime;
#include "basecommands/kick.sp"
#include "basecommands/reloadadmins.sp"
@@ -82,6 +81,10 @@ public OnPluginStart()
g_MapList = CreateMenu(MenuHandler_ChangeMap);
SetMenuTitle(g_MapList, "Please select a map");
SetMenuExitBackButton(g_MapList, true);
decl String:mapListPath[PLATFORM_MAX_PATH];
BuildPath(Path_SM, mapListPath, sizeof(mapListPath), "configs/adminmenu_maplist.ini");
SetMapListCompatBind("admin menu", mapListPath);
}
public OnMapStart()
+21 -93
View File
@@ -82,109 +82,37 @@ public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
return Plugin_Stop;
}
new Handle:g_map_array = INVALID_HANDLE;
new g_map_serial = -1;
LoadMapList(Handle:menu)
{
decl String:mapPath[256];
BuildPath(Path_SM, mapPath, sizeof(mapPath), "configs/adminmenu_maplist.ini");
new Handle:map_array;
if (!FileExists(mapPath))
{
if (g_MapList != INVALID_HANDLE)
{
RemoveAllMenuItems(menu);
}
return LoadMapFolder(menu);
}
// If the file hasn't changed, there's no reason to reload
// all of the maps.
new fileTime = GetFileTime(mapPath, FileTime_LastChange);
if (g_mapFileTime == fileTime)
if ((map_array = ReadMapList(g_map_array,
g_map_serial,
"admin menu",
MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_NO_DEFAULT|MAPLIST_FLAG_MAPSFOLDER))
!= INVALID_HANDLE)
{
return GetMenuItemCount(menu);
g_map_array = map_array;
}
g_mapFileTime = fileTime;
// Reset the array
if (g_MapList != INVALID_HANDLE)
if (g_map_array == INVALID_HANDLE)
{
RemoveAllMenuItems(menu);
}
new Handle:file = OpenFile(mapPath, "rt");
if (file == INVALID_HANDLE)
{
LogError("[SM] Could not open file: %s, reverting to map folder", mapPath);
return LoadMapFolder(menu);
}
decl String:buffer[256], len;
while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
{
TrimString(buffer);
if ((len = StrContains(buffer, ".bsp", false)) != -1)
{
buffer[len] = '\0';
}
if (buffer[0] == '\0'
|| buffer[0] == ';'
|| buffer[0] == '/'
|| !IsValidConVarChar(buffer[0]))
{
continue;
}
if (IsMapValid(buffer))
{
AddMenuItem(menu, buffer, buffer);
}
}
CloseHandle(file);
new count = GetMenuItemCount(menu);
if (!count)
return LoadMapFolder(menu);
else
return count;
}
LoadMapFolder(Handle:menu)
{
LogMessage("[SM] Loading menu map list from maps folder");
new Handle:mapDir = OpenDirectory("maps/");
if (mapDir == INVALID_HANDLE)
{
LogError("[SM] Could not open map directory for reading");
return 0;
}
new String:mapName[64];
new String:buffer[64];
new FileType:fileType;
new len;
RemoveAllMenuItems(menu);
while(ReadDirEntry(mapDir, mapName, sizeof(mapName), fileType))
decl String:map_name[64];
new map_count = GetArraySize(g_map_array);
for (new i = 0; i < map_count; i++)
{
if(fileType == FileType_File)
{
len = strlen(mapName);
if(SplitString(mapName, ".bsp", buffer, sizeof(buffer)) == len)
{
AddMenuItem(menu, buffer, buffer);
}
}
}
CloseHandle(mapDir);
return GetMenuItemCount(menu);
GetArrayString(g_map_array, i, map_name, sizeof(map_name));
AddMenuItem(menu, map_name, map_name);
}
return map_count;
}
+3 -3
View File
@@ -48,8 +48,8 @@ enum SortOrder
/**
* Data types for ADT Array Sorts
*/
enum SortType
{
enum SortType
{
Sort_Integer = 0,
Sort_Float,
Sort_String,
@@ -147,7 +147,7 @@ native SortCustom2D(array[][], array_size, SortFunc2D:sortfunc, Handle:hndl=INVA
* @param type Data type stored in the ADT Array
* @noreturn
*/
native SortADTArray(Handle:array, SortOrder:order = Sort_Ascending, SortType:type = Sort_Integer);
native SortADTArray(Handle:array, SortOrder:order, SortType:type);
/**
* Sort comparison function for ADT Array elements. Function provides you with
+64
View File
@@ -490,6 +490,70 @@ forward OnLibraryAdded(const String:name[]);
*/
forward OnLibraryRemoved(const String:name[]);
#define MAPLIST_FLAG_MAPSFOLDER (1<<0) /**< On failure, use all maps in the maps folder. */
#define MAPLIST_FLAG_CLEARARRAY (1<<1) /**< If an input array is specified, clear it before adding. */
#define MAPLIST_FLAG_NO_DEFAULT (1<<2) /**< Do not read "default" or "mapcyclefile" on failure. */
/**
* Loads a map list to an ADT Array.
*
* A map list is a list of maps from a file. SourceMod allows easy configuration of
* maplists through addons/sourcemod/configs/maplists.cfg. Each entry is given a
* name and a file (for example, "rtv" => "rtv.cfg"), or a name and a redirection
* (for example, "rtv" => "default"). This native will read a map list entry,
* cache the file, and return the list of maps it holds.
*
* Serial change numbers are used to identify if a map list has changed. Thus, if
* you pass a serial change number and it's equal to what SourceMod currently knows
* about the map list, then SourceMod won't reparse the file.
*
* If the maps end up being read from the maps folder (MAPLIST_FLAG_MAPSFOLDER), they
* are automatically sorted in alphabetical, ascending order.
*
* @param array Array to store the map list. If INVALID_HANDLE, a new blank
* array will be created. The blocksize should be at least 16;
* otherwise results may be truncated. Items are added to the array
* as strings. The array is never checked for duplicates, and it is
* not read beforehand. Only the serial number is used to detect
* changes.
* @param str Config name, or "default" for the default map list. Config names
* should be somewhat descriptive. For example, the admin menu uses
* a config name of "admin menu". The list names can be configured
* by users in addons/sourcemod/configs/maplists.cfg.
* @param serial Serial number to identify last known map list change. If -1, the
* the value will not be checked. If the map list has since changed,
* the serial is updated (even if -1 was passed). If there is an error
* finding a valid maplist, then the serial is set to -1.
* @param flags MAPLIST_FLAG flags.
* @return On failure:
* INVALID_HANDLE is returned, the serial is set to -1, and the input
* array (if any) is left unchanged.
* On no change:
INVALID_HANDLE is returned, the serial is unchanged, and the input
array (if any) is left unchanged.
* On success:
* A valid array Handle is returned, containing at least one map string.
* If an array was passed, the return value is equal to the passed Array
* Handle. If the passed array was not cleared, it will have grown by at
* least one item. The serial number is updated to a positive number.
* @error Invalid array Handle that is not INVALID_HANDLE.
*/
native Handle:ReadMapList(Handle:array=INVALID_HANDLE,
&serial=-1,
const String:str[]="default",
flags=MAPLIST_FLAG_CLEARARRAY);
/**
* Makes a compatibility binding for map lists. For example, if a function previously used
* "clam.cfg" for map lists, this function will insert a "fake" binding to "clam.cfg" that
* will be overridden if it's in the maplists.cfg file.
*
* @param name Configuration name that would be used with ReadMapList().
* @param file Default file to use.
* @noreturn
*/
native SetMapListCompatBind(const String:name[], const String:file[]);
#include <helpers>
#include <entity>