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
This commit is contained in:
Michael McKoy 2007-10-18 01:16:42 +00:00
parent 0b66a6acee
commit bd58aa930b
9 changed files with 112 additions and 328 deletions

View File

@ -86,7 +86,7 @@ public OnPluginStart()
public OnMapStart()
{
LoadMaps(g_MapList);
LoadMapList(g_MapList);
ParseConfigs();
}

View File

@ -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");

View File

@ -136,7 +136,7 @@ public OnPluginStart()
public OnMapStart()
{
g_mapCount = LoadMaps(g_MapList);
g_mapCount = LoadMapList(g_MapList);
}
public OnAdminMenuReady(Handle:topmenu)

View File

@ -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");

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}