From 0600be8c3ab40bc977c09b1ad2389c853500f678 Mon Sep 17 00:00:00 2001
From: Matt Woodrow <pred@alliedmods.net>
Date: Mon, 15 Oct 2007 21:12:52 +0000
Subject: [PATCH] Menu map lists (map and votemap) now fall back to the
 contents of maps folder if the menu_maplist.ini isn't found

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401586
---
 configs/menu_maplist.ini     |  1 +
 plugins/basecommands/map.sp  | 55 ++++++++++++++++++++++++++++++------
 plugins/basevotes/votemap.sp | 53 +++++++++++++++++++++++++++++-----
 3 files changed, 94 insertions(+), 15 deletions(-)

diff --git a/configs/menu_maplist.ini b/configs/menu_maplist.ini
index 457e7250..16144e7c 100644
--- a/configs/menu_maplist.ini
+++ b/configs/menu_maplist.ini
@@ -2,4 +2,5 @@
 //
 // List maps here to be added to the map and votemap sections of the admin menu
 //
+// Leave this file empty, or delete it, to use the contents of your /maps/ folder
 
diff --git a/plugins/basecommands/map.sp b/plugins/basecommands/map.sp
index 6a6571a5..94ce7351 100644
--- a/plugins/basecommands/map.sp
+++ b/plugins/basecommands/map.sp
@@ -88,15 +88,13 @@ LoadMaps(Handle:menu)
 	BuildPath(Path_SM, mapPath, sizeof(mapPath), "configs/menu_maplist.ini");
 	
 	if (!FileExists(mapPath))
-	{
-		LogError("Unable to locate menu_maplist, no maps loaded.");
-		
+	{	
 		if (g_MapList != INVALID_HANDLE)
 		{
 			RemoveAllMenuItems(menu);
 		}
 		
-		return;		
+		return LoadMapFolder(menu);		
 	}
 
 	// If the file hasn't changed, there's no reason to reload
@@ -104,7 +102,7 @@ LoadMaps(Handle:menu)
 	new fileTime =  GetFileTime(mapPath, FileTime_LastChange);
 	if (g_mapFileTime == fileTime)
 	{
-		return;
+		return GetMenuItemCount(menu);
 	}
 	
 	g_mapFileTime = fileTime;
@@ -120,8 +118,8 @@ LoadMaps(Handle:menu)
 	new Handle:file = OpenFile(mapPath, "rt");
 	if (file == INVALID_HANDLE)
 	{
-		LogError("[SM] Could not open file: %s", mapPath);
-		return;
+		LogError("[SM] Could not open file: %s, reverting to map folder", mapPath);
+		return LoadMapFolder(menu);
 	}
 	
 	decl String:buffer[64], len;
@@ -143,5 +141,46 @@ LoadMaps(Handle:menu)
 	}
 
 	CloseHandle(file);
-	return;
+	
+	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;
+	
+	while(ReadDirEntry(mapDir, mapName, sizeof(mapName), fileType))
+	{
+		if(fileType == FileType_File)
+		{
+			len = strlen(mapName);
+
+			if(SplitString(mapName, ".bsp", buffer, sizeof(buffer)) == len)
+			{
+				AddMenuItem(menu, buffer, buffer);
+			}
+		}
+  	}
+  	
+  	CloseHandle(mapDir);
+  	
+  	return GetMenuItemCount(menu);
 }
\ No newline at end of file
diff --git a/plugins/basevotes/votemap.sp b/plugins/basevotes/votemap.sp
index 5ecba0c4..bb14d825 100644
--- a/plugins/basevotes/votemap.sp
+++ b/plugins/basevotes/votemap.sp
@@ -203,15 +203,13 @@ LoadMaps(Handle:menu)
 	BuildPath(Path_SM, mapPath, sizeof(mapPath), "configs/menu_maplist.ini");
 	
 	if (!FileExists(mapPath))
-	{
-		LogError("Unable to locate menu_maplist, no maps loaded.");
-		
+	{	
 		if (g_MapList != INVALID_HANDLE)
 		{
 			RemoveAllMenuItems(menu);
 		}
 		
-		return 0;		
+		return LoadMapFolder(menu);		
 	}
 
 	// If the file hasn't changed, there's no reason to reload
@@ -235,8 +233,8 @@ LoadMaps(Handle:menu)
 	new Handle:file = OpenFile(mapPath, "rt");
 	if (file == INVALID_HANDLE)
 	{
-		LogError("[SM] Could not open file: %s", mapPath);
-		return 0;
+		LogError("[SM] Could not open file: %s, reverting to map folder", mapPath);
+		return LoadMapFolder(menu);
 	}
 	
 	decl String:buffer[64], len;
@@ -258,5 +256,46 @@ LoadMaps(Handle:menu)
 	}
 
 	CloseHandle(file);
-	return GetMenuItemCount(menu);
+	
+	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;
+	
+	while(ReadDirEntry(mapDir, mapName, sizeof(mapName), fileType))
+	{
+		if(fileType == FileType_File)
+		{
+			len = strlen(mapName);
+
+			if(SplitString(mapName, ".bsp", buffer, sizeof(buffer)) == len)
+			{
+				AddMenuItem(menu, buffer, buffer);
+			}
+		}
+  	}
+  	
+  	CloseHandle(mapDir);
+  	
+  	return GetMenuItemCount(menu);
 }
\ No newline at end of file