plugins/basecommands/map.sp
author David Anderson <dvander@alliedmods.net>
Tue Oct 16 18:37:24 2007 +0000 (2007-10-16)
changeset 1592 ff10cfeaf5d2
parent 1580 e5198a9a458b
child 1606 4fb3959c2c96
permissions -rw-r--r--
overhaul of the phrases for consistency
renamed config files for adminmenu
cleaned up execcfg menu code
     1 
     2 public MenuHandler_ChangeMap(Handle:menu, MenuAction:action, param1, param2)
     3 {
     4 	if (action == MenuAction_Cancel)
     5 	{
     6 		if (param2 == MenuCancel_ExitBack && hTopMenu != INVALID_HANDLE)
     7 		{
     8 			DisplayTopMenu(hTopMenu, param1, TopMenuPosition_LastCategory);
     9 		}
    10 	}
    11 	else if (action == MenuAction_Select)
    12 	{
    13 		decl String:map[64];
    14 		
    15 		GetMenuItem(menu, param2, map, sizeof(map));
    16 	
    17 		ShowActivity(param1, "%t", "Changing map", map);
    18 
    19 		LogAction(param1, -1, "\"%L\" changed map to \"%s\"", param1, map);
    20 
    21 		new Handle:dp;
    22 		CreateDataTimer(3.0, Timer_ChangeMap, dp);
    23 		WritePackString(dp, map);
    24 
    25 	}
    26 }
    27 
    28 public AdminMenu_Map(Handle:topmenu, 
    29 							  TopMenuAction:action,
    30 							  TopMenuObject:object_id,
    31 							  param,
    32 							  String:buffer[],
    33 							  maxlength)
    34 {
    35 	if (action == TopMenuAction_DisplayOption)
    36 	{
    37 		Format(buffer, maxlength, "%T", "Choose Map", param);
    38 	}
    39 	else if (action == TopMenuAction_SelectOption)
    40 	{
    41 		DisplayMenu(g_MapList, param, MENU_TIME_FOREVER);
    42 	}
    43 }
    44 
    45 public Action:Command_Map(client, args)
    46 {
    47 	if (args < 1)
    48 	{
    49 		ReplyToCommand(client, "[SM] Usage: sm_map <map>");
    50 		return Plugin_Handled;
    51 	}
    52 
    53 	decl String:map[64];
    54 	GetCmdArg(1, map, sizeof(map));
    55 
    56 	if (!IsMapValid(map))
    57 	{
    58 		ReplyToCommand(client, "[SM] %t", "Map was not found", map);
    59 		return Plugin_Handled;
    60 	}
    61 
    62 	ShowActivity(client, "%t", "Changing map", map);
    63 
    64 	LogAction(client, -1, "\"%L\" changed map to \"%s\"", client, map);
    65 
    66 	new Handle:dp;
    67 	CreateDataTimer(3.0, Timer_ChangeMap, dp);
    68 	WritePackString(dp, map);
    69 
    70 	return Plugin_Handled;
    71 }
    72 
    73 public Action:Timer_ChangeMap(Handle:timer, Handle:dp)
    74 {
    75 	decl String:map[65];
    76 
    77 	ResetPack(dp);
    78 	ReadPackString(dp, map, sizeof(map));
    79 
    80 	ServerCommand("changelevel \"%s\"", map);
    81 
    82 	return Plugin_Stop;
    83 }
    84 
    85 LoadMaps(Handle:menu)
    86 {
    87 	decl String:mapPath[256];
    88 	BuildPath(Path_SM, mapPath, sizeof(mapPath), "configs/adminmenu_maplist.ini");
    89 	
    90 	if (!FileExists(mapPath))
    91 	{	
    92 		if (g_MapList != INVALID_HANDLE)
    93 		{
    94 			RemoveAllMenuItems(menu);
    95 		}
    96 		
    97 		return LoadMapFolder(menu);		
    98 	}
    99 
   100 	// If the file hasn't changed, there's no reason to reload
   101 	// all of the maps.
   102 	new fileTime =  GetFileTime(mapPath, FileTime_LastChange);
   103 	if (g_mapFileTime == fileTime)
   104 	{
   105 		return GetMenuItemCount(menu);
   106 	}
   107 	
   108 	g_mapFileTime = fileTime;
   109 	
   110 	// Reset the array
   111 	if (g_MapList != INVALID_HANDLE)
   112 	{
   113 		RemoveAllMenuItems(menu);
   114 	}
   115 
   116 	new Handle:file = OpenFile(mapPath, "rt");
   117 	if (file == INVALID_HANDLE)
   118 	{
   119 		LogError("[SM] Could not open file: %s, reverting to map folder", mapPath);
   120 		return LoadMapFolder(menu);
   121 	}
   122 	
   123 	decl String:buffer[64], len;
   124 	while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
   125 	{
   126 		TrimString(buffer);
   127 
   128 		if ((len = StrContains(buffer, ".bsp", false)) != -1)
   129 		{
   130 			buffer[len] = '\0';
   131 		}
   132 
   133 		if (buffer[0] == '\0' || !IsValidConVarChar(buffer[0]) || !IsMapValid(buffer))
   134 		{
   135 			continue;
   136 		}
   137 
   138 		AddMenuItem(menu, buffer, buffer);
   139 	}
   140 
   141 	CloseHandle(file);
   142 	
   143 	new count = GetMenuItemCount(menu);
   144 	
   145 	if (!count)
   146 		return LoadMapFolder(menu);
   147 	else
   148 		return count;
   149 }
   150 
   151 LoadMapFolder(Handle:menu)
   152 {
   153 	LogMessage("[SM] Loading menu map list from maps folder");
   154 	
   155 	new Handle:mapDir = OpenDirectory("maps/");
   156 	
   157 	if (mapDir == INVALID_HANDLE)
   158 	{
   159 		LogError("[SM] Could not open map directory for reading");
   160 		return 0;
   161 	}
   162 	
   163 	new String:mapName[64];
   164 	new String:buffer[64];
   165 	new FileType:fileType;
   166 	new len;
   167 	
   168 	while(ReadDirEntry(mapDir, mapName, sizeof(mapName), fileType))
   169 	{
   170 		if(fileType == FileType_File)
   171 		{
   172 			len = strlen(mapName);
   173 
   174 			if(SplitString(mapName, ".bsp", buffer, sizeof(buffer)) == len)
   175 			{
   176 				AddMenuItem(menu, buffer, buffer);
   177 			}
   178 		}
   179   	}
   180   	
   181   	CloseHandle(mapDir);
   182   	
   183   	return GetMenuItemCount(menu);
   184 }