From bd58aa930b3a3400fc654002c4b6504fbc92af96 Mon Sep 17 00:00:00 2001 From: Michael McKoy Date: Thu, 18 Oct 2007 01:16:42 +0000 Subject: [PATCH] Added new stock to helpers.inc: LoadMaps() Adjusted all plugins that use LoadMaps() Nextmap no longer worries about mapcyclefile until the map changes RTV/MC/RC use OnConfigsExecuted() instead of OnMapStart() now RTV/MC/RC will no longer "permanently" exclude the current map being played when the map file is loaded. --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401612 --- plugins/basecommands.sp | 2 +- plugins/basecommands/map.sp | 2 +- plugins/basevotes.sp | 2 +- plugins/basevotes/votemap.sp | 2 +- plugins/include/helpers.inc | 83 ++++++++++++++++++++++++++++ plugins/mapchooser.sp | 102 ++++++----------------------------- plugins/nextmap.sp | 79 ++------------------------- plugins/randomcycle.sp | 82 +--------------------------- plugins/rockthevote.sp | 86 +---------------------------- 9 files changed, 112 insertions(+), 328 deletions(-) diff --git a/plugins/basecommands.sp b/plugins/basecommands.sp index 0d720b80..2da8ea6f 100644 --- a/plugins/basecommands.sp +++ b/plugins/basecommands.sp @@ -86,7 +86,7 @@ public OnPluginStart() public OnMapStart() { - LoadMaps(g_MapList); + LoadMapList(g_MapList); ParseConfigs(); } diff --git a/plugins/basecommands/map.sp b/plugins/basecommands/map.sp index 768e60ec..d7ed57a9 100644 --- a/plugins/basecommands/map.sp +++ b/plugins/basecommands/map.sp @@ -82,7 +82,7 @@ public Action:Timer_ChangeMap(Handle:timer, Handle:dp) return Plugin_Stop; } -LoadMaps(Handle:menu) +LoadMapList(Handle:menu) { decl String:mapPath[256]; BuildPath(Path_SM, mapPath, sizeof(mapPath), "configs/adminmenu_maplist.ini"); diff --git a/plugins/basevotes.sp b/plugins/basevotes.sp index 238732d2..c2c9e650 100644 --- a/plugins/basevotes.sp +++ b/plugins/basevotes.sp @@ -136,7 +136,7 @@ public OnPluginStart() public OnMapStart() { - g_mapCount = LoadMaps(g_MapList); + g_mapCount = LoadMapList(g_MapList); } public OnAdminMenuReady(Handle:topmenu) diff --git a/plugins/basevotes/votemap.sp b/plugins/basevotes/votemap.sp index 5c4c79a1..63a7adcd 100644 --- a/plugins/basevotes/votemap.sp +++ b/plugins/basevotes/votemap.sp @@ -199,7 +199,7 @@ public Action:Command_Votemap(client, args) return Plugin_Handled; } -LoadMaps(Handle:menu) +LoadMapList(Handle:menu) { decl String:mapPath[256]; BuildPath(Path_SM, mapPath, sizeof(mapPath), "configs/adminmenu_maplist.ini"); diff --git a/plugins/include/helpers.inc b/plugins/include/helpers.inc index c4c7368f..1ed03664 100644 --- a/plugins/include/helpers.inc +++ b/plugins/include/helpers.inc @@ -203,3 +203,86 @@ stock FindTarget(client, const String:target[], bool:nobots = false, bool:immuni return clients[0]; } + + +/** + * Loads a specified array with maps. The maps will be either loaded from mapcyclefile, or if supplied + * a cvar containing a file name. If the file in the cvar is bad, it will use mapcyclefile. The fileTime + * parameter is used to store a timestamp of the file. If specified, the file will only be reloaded if it + * has changed. + * + * @param array Valid array handle, should be created with CreateArray(33) or larger. + * @param fileTime Variable containing the "last changed" time of the file. Used to avoid needless reloading. + * @param fileCvar CVAR set to the file to be loaded. Optional. + * @return Number of maps loaded or 0 if in error. + */ + stock LoadMaps(Handle:array, &fileTime = 0, Handle:fileCvar = INVALID_HANDLE) + { + decl String:mapPath[256], String:mapFile[64]; + new bool:fileFound = false; + + if (fileCvar != INVALID_HANDLE) + { + GetConVarString(fileCvar, mapFile, 64); + BuildPath(Path_SM, mapPath, sizeof(mapPath), mapFile); + fileFound = FileExists(mapPath); + } + + if (!fileFound) + { + new Handle:mapCycleFile = FindConVar("mapcyclefile"); + GetConVarString(mapCycleFile, mapPath, sizeof(mapPath)); + fileFound = FileExists(mapPath); + } + + if (!fileFound) + { + LogError("Failed to find a file to load maps from. No maps loaded."); + ClearArray(array); + + return 0; + } + + // If the file hasn't changed, there's no reason to reload + // all of the maps. + new newTime = GetFileTime(mapPath, FileTime_LastChange); + if (fileTime == newTime) + { + return GetArraySize(array); + } + + fileTime = newTime; + + ClearArray(array); + + new Handle:file = OpenFile(mapPath, "rt"); + if (file == INVALID_HANDLE) + { + LogError("Could not open file: %s", mapPath); + + return 0; + } + + LogMessage("Loading maps from file: %s", mapPath); + + decl String:buffer[64], 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' || !IsValidConVarChar(buffer[0]) || !IsMapValid(buffer)) + { + continue; + } + + PushArrayString(array, buffer); + } + + CloseHandle(file); + return GetArraySize(array); +} diff --git a/plugins/mapchooser.sp b/plugins/mapchooser.sp index c199d659..1d6884ae 100644 --- a/plugins/mapchooser.sp +++ b/plugins/mapchooser.sp @@ -71,8 +71,8 @@ new Handle:g_RetryTimer = INVALID_HANDLE; new Handle:g_MapList = INVALID_HANDLE; new Handle:g_OldMapList = INVALID_HANDLE; new Handle:g_NextMapList = INVALID_HANDLE; -new Handle:g_VoteMenu = INVALID_HANDLE; new Handle:g_TeamScores = INVALID_HANDLE; +new Handle:g_VoteMenu = INVALID_HANDLE; new bool:g_HasVoteStarted; new g_mapFileTime; @@ -122,7 +122,7 @@ public OnPluginStart() AutoExecConfig(true, "mapchooser"); } -public OnMapStart() +public OnConfigsExecuted() { g_Cvar_Nextmap = FindConVar("sm_nextmap"); @@ -132,7 +132,7 @@ public OnMapStart() SetFailState("sm_nextmap not found"); } - if (LoadMaps()) + if (LoadMaps(g_MapList, g_mapFileTime, g_Cvar_Mapfile)) { CreateNextVote(); SetupTimeleftTimer(); @@ -143,8 +143,18 @@ public OnMapStart() public OnMapEnd() { g_HasVoteStarted = false; - g_RetryTimer = INVALID_HANDLE; - g_VoteTimer = INVALID_HANDLE; + + if (g_VoteTimer != INVALID_HANDLE) + { + KillTimer(g_VoteTimer); + g_VoteTimer = INVALID_HANDLE; + } + + if (g_RetryTimer != INVALID_HANDLE) + { + KillTimer(g_RetryTimer); + g_RetryTimer = INVALID_HANDLE; + } } public OnMapTimeLeftChanged() @@ -173,7 +183,7 @@ SetupTimeleftTimer() g_VoteTimer = INVALID_HANDLE; } - g_VoteTimer = CreateTimer(float(time - startTime), Timer_StartMapVote, TIMER_FLAG_NO_MAPCHANGE); + g_VoteTimer = CreateTimer(float(time - startTime), Timer_StartMapVote); } } } @@ -301,7 +311,7 @@ InitiateVote() if (IsVoteInProgress()) { // Can't start a vote, try again in 5 seconds. - g_RetryTimer = CreateTimer(5.0, Timer_StartMapVote, TIMER_FLAG_NO_MAPCHANGE); + g_RetryTimer = CreateTimer(5.0, Timer_StartMapVote); return; } @@ -536,82 +546,4 @@ CreateNextVote() PushArrayString(g_NextMapList, map); RemoveFromArray(tempMaps, b); } -} - -LoadMaps() -{ - new bool:fileFound; - - decl String:mapPath[256], String:mapFile[64]; - GetConVarString(g_Cvar_Mapfile, mapFile, 64); - BuildPath(Path_SM, mapPath, sizeof(mapPath), mapFile); - fileFound = FileExists(mapPath); - if (!fileFound) - { - new Handle:mapCycleFile = FindConVar("mapcyclefile"); - GetConVarString(mapCycleFile, mapPath, sizeof(mapPath)); - fileFound = FileExists(mapPath); - } - - if (!fileFound) - { - LogError("Unable to locate sm_mapvote_file or mapcyclefile, no maps loaded."); - - if (g_MapList != INVALID_HANDLE) - { - ClearArray(g_MapList); - } - - return 0; - } - - // 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) - { - return GetArraySize(g_MapList); - } - - g_mapFileTime = fileTime; - - // Reset the array - if (g_MapList != INVALID_HANDLE) - { - ClearArray(g_MapList); - } - - LogMessage("[SM] Loading mapchooser map file [%s]", mapPath); - - new Handle:file = OpenFile(mapPath, "rt"); - if (file == INVALID_HANDLE) - { - LogError("[SM] Could not open file: %s", mapPath); - return 0; - } - - decl String:currentMap[32]; - GetCurrentMap(currentMap, sizeof(currentMap)); - - decl String:buffer[64], 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' || !IsValidConVarChar(buffer[0]) || !IsMapValid(buffer) - || strcmp(currentMap, buffer, false) == 0) - { - continue; - } - - PushArrayString(g_MapList, buffer); - } - - CloseHandle(file); - return GetArraySize(g_MapList); } \ No newline at end of file diff --git a/plugins/nextmap.sp b/plugins/nextmap.sp index 45ab97d4..160c2bce 100644 --- a/plugins/nextmap.sp +++ b/plugins/nextmap.sp @@ -49,7 +49,6 @@ new UserMsg:g_VGUIMenu; new Handle:g_Cvar_Chattime; new Handle:g_Cvar_Nextmap; -new Handle:g_Cvar_Mapcycle; new g_MapPos = -1; new Handle:g_MapList = INVALID_HANDLE; @@ -67,21 +66,18 @@ public OnPluginStart() SetFailState("VGUIMenu Not Found"); } - g_Cvar_Chattime = FindConVar("mp_chattime"); - g_Cvar_Mapcycle = FindConVar("mapcyclefile"); - g_MapList = CreateArray(32); - if (!LoadMaps()) + if (!LoadMaps(g_MapList, g_mapFileTime)) { LogError("FATAL: Cannot load map cycle. Nextmap not loaded."); SetFailState("Mapcycle Not Found"); } HookUserMessage(g_VGUIMenu, UserMsg_VGUIMenu); - HookConVarChange(g_Cvar_Mapcycle, ConVarChange_Mapcyclefile); g_Cvar_Nextmap = CreateConVar("sm_nextmap", "", "Sets the Next Map", FCVAR_NOTIFY); + g_Cvar_Chattime = FindConVar("mp_chattime"); RegConsoleCmd("say", Command_Say); RegConsoleCmd("say_team", Command_Say); @@ -106,7 +102,7 @@ public OnMapStart() // not in mapcyclefile. So we keep it set to the last expected nextmap. - ferret if (strcmp(lastMap, currentMap) == 0) { - if (!LoadMaps()) + if (!LoadMaps(g_MapList, g_mapFileTime)) { LogError("FATAL: Cannot load map cycle. Nextmap not loaded."); SetFailState("Mapcycle Not Found"); @@ -120,21 +116,7 @@ public OnMapEnd() { g_IntermissionCalled = false; } - -public ConVarChange_Mapcyclefile(Handle:convar, const String:oldValue[], const String:newValue[]) -{ - if (strcmp(oldValue, newValue, false) != 0) - { - if (!LoadMaps()) - { - LogError("FATAL: Cannot load map cycle. Nextmap not loaded."); - SetFailState("Mapcycle Not Found"); - } - - FindAndSetNextMap(); - } -} - + public Action:Command_Say(client, args) { decl String:text[192]; @@ -254,59 +236,6 @@ public Action:Timer_ChangeMap(Handle:timer, Handle:dp) return Plugin_Stop; } -LoadMaps() -{ - decl String:mapCycle[64]; - GetConVarString(g_Cvar_Mapcycle, mapCycle, 64); - - if (!FileExists(mapCycle)) - { - LogError("[SM] Could not find file: %s", mapCycle); - return 0; - } - - new fileTime = GetFileTime(mapCycle, FileTime_LastChange); - if (g_mapFileTime == fileTime) - { - return GetArraySize(g_MapList); - } - - g_mapFileTime = fileTime; - - new Handle:file = OpenFile(mapCycle, "r"); - - if (file == INVALID_HANDLE) - { - LogError("[SM] Could not open file: %s", mapCycle); - return 0; - } - - g_MapPos = -1; - if (g_MapList != INVALID_HANDLE) - { - ClearArray(g_MapList); - } - - decl String:buffer[255]; - while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer))) - { - TrimString(buffer); - if (buffer[0] == '\0' || buffer[0] == ';') - { - continue; - } - - if (IsMapValid(buffer)) - { - PushArrayString(g_MapList, buffer); - } - } - - CloseHandle(file); - - return GetArraySize(g_MapList); -} - FindAndSetNextMap() { new mapCount = GetArraySize(g_MapList); diff --git a/plugins/randomcycle.sp b/plugins/randomcycle.sp index 9a6726ee..bbd7cda9 100644 --- a/plugins/randomcycle.sp +++ b/plugins/randomcycle.sp @@ -62,7 +62,7 @@ public OnPluginStart() AutoExecConfig(true, "randomcycle"); } -public OnMapStart() +public OnConfigsExecuted() { g_Cvar_Nextmap = FindConVar("sm_nextmap"); @@ -72,7 +72,7 @@ public OnMapStart() SetFailState("sm_nextmap not found"); } - if (LoadMaps()) + if (LoadMaps(g_MapList, g_mapFileTime, g_Cvar_Mapfile)) { CreateTimer(5.0, Timer_RandomizeNextmap); // Small delay to give Nextmap time to complete OnMapStart() } @@ -108,82 +108,4 @@ public Action:Timer_RandomizeNextmap(Handle:timer) LogMessage("RandomCycle has chosen %s for the nextmap.", map); return Plugin_Stop; -} - -LoadMaps() -{ - new bool:fileFound; - - decl String:mapPath[256], String:mapFile[64]; - GetConVarString(g_Cvar_Mapfile, mapFile, 64); - BuildPath(Path_SM, mapPath, sizeof(mapPath), mapFile); - fileFound = FileExists(mapPath); - if (!fileFound) - { - new Handle:mapCycleFile = FindConVar("mapcyclefile"); - GetConVarString(mapCycleFile, mapPath, sizeof(mapPath)); - fileFound = FileExists(mapPath); - } - - if (!fileFound) - { - LogError("Unable to locate sm_randomcycle_file or mapcyclefile, no maps loaded."); - - if (g_MapList != INVALID_HANDLE) - { - ClearArray(g_MapList); - } - - return 0; - } - - // 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) - { - return GetArraySize(g_MapList); - } - - g_mapFileTime = fileTime; - - // Reset the array - if (g_MapList != INVALID_HANDLE) - { - ClearArray(g_MapList); - } - - LogMessage("[SM] Loading Random Cycle map file [%s]", mapPath); - - new Handle:file = OpenFile(mapPath, "rt"); - if (file == INVALID_HANDLE) - { - LogError("[SM] Could not open file: %s", mapPath); - return 0; - } - - decl String:currentMap[32]; - GetCurrentMap(currentMap, sizeof(currentMap)); - - decl String:buffer[64], 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' || !IsValidConVarChar(buffer[0]) || !IsMapValid(buffer) - || strcmp(currentMap, buffer, false) == 0) - { - continue; - } - - PushArrayString(g_MapList, buffer); - } - - CloseHandle(file); - return GetArraySize(g_MapList); } \ No newline at end of file diff --git a/plugins/rockthevote.sp b/plugins/rockthevote.sp index e3feb924..ac618e69 100644 --- a/plugins/rockthevote.sp +++ b/plugins/rockthevote.sp @@ -88,7 +88,7 @@ public OnPluginStart() AutoExecConfig(true, "rtv"); } -public OnMapStart() +public OnConfigsExecuted() { if (g_RTVMapList != INVALID_HANDLE) { @@ -101,16 +101,12 @@ public OnMapStart() g_RTVStarted = false; g_RTVEnded = false; - if (LoadMaps()) + if (LoadMaps(g_MapList, g_mapFileTime, g_Cvar_File)) { BuildMapMenu(); g_CanRTV = true; CreateTimer(30.0, Timer_DelayRTV); } - else - { - LogMessage("[SM] Cannot find map cycle file, RTV not active."); - } } public OnMapEnd() @@ -528,82 +524,4 @@ BuildMapMenu() } SetMenuExitButton(g_MapMenu, false); -} - -LoadMaps() -{ - new bool:fileFound; - - decl String:mapPath[256], String:mapFile[64]; - GetConVarString(g_Cvar_File, mapFile, 64); - BuildPath(Path_SM, mapPath, sizeof(mapPath), mapFile); - fileFound = FileExists(mapPath); - if (!fileFound) - { - new Handle:mapCycleFile = FindConVar("mapcyclefile"); - GetConVarString(mapCycleFile, mapPath, sizeof(mapPath)); - fileFound = FileExists(mapPath); - } - - if (!fileFound) - { - LogError("Unable to locate sm_rtv_file or mapcyclefile, no maps loaded."); - - if (g_MapList != INVALID_HANDLE) - { - ClearArray(g_MapList); - } - - return 0; - } - - // 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) - { - return GetArraySize(g_MapList); - } - - g_mapFileTime = fileTime; - - // Reset the array - if (g_MapList != INVALID_HANDLE) - { - ClearArray(g_MapList); - } - - LogMessage("[SM] Loading RTV map file [%s]", mapPath); - - decl String:currentMap[32]; - GetCurrentMap(currentMap, sizeof(currentMap)); - - new Handle:file = OpenFile(mapPath, "rt"); - if (file == INVALID_HANDLE) - { - LogError("[SM] Could not open file: %s", mapPath); - return 0; - } - - decl String:buffer[64], 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' || !IsValidConVarChar(buffer[0]) || !IsMapValid(buffer) - || strcmp(currentMap, buffer, false) == 0) - { - continue; - } - - PushArrayString(g_MapList, buffer); - } - - CloseHandle(file); - return GetArraySize(g_MapList); } \ No newline at end of file