core/smn_maplists.cpp
changeset 1738 cb3be0b31d99
child 1746 e8187a3f2f08
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/smn_maplists.cpp	Sun Dec 02 02:10:37 2007 +0000
     1.3 @@ -0,0 +1,659 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#include <sh_list.h>
    1.36 +#include "sm_globals.h"
    1.37 +#include "sm_trie_tpl.h"
    1.38 +#include "CellArray.h"
    1.39 +#include "convar.h"
    1.40 +#include "sourcemm_api.h"
    1.41 +#include "LibrarySys.h"
    1.42 +#include "TextParsers.h"
    1.43 +#include "sm_stringutil.h"
    1.44 +#include "sourcemod.h"
    1.45 +#include "Logger.h"
    1.46 +#include "HandleSys.h"
    1.47 +
    1.48 +using namespace SourceHook;
    1.49 +
    1.50 +struct maplist_info_t
    1.51 +{
    1.52 +	bool bIsCompat;
    1.53 +	bool bIsPath;
    1.54 +	char name[PLATFORM_MAX_PATH];
    1.55 +	char path[PLATFORM_MAX_PATH];
    1.56 +	time_t last_modified_time;
    1.57 +	CellArray *pArray;
    1.58 +	int serial;
    1.59 +};
    1.60 +
    1.61 +#define MAPLIST_FLAG_MAPSFOLDER		(1<<0)		/**< On failure, use all maps in the maps folder. */
    1.62 +#define MAPLIST_FLAG_CLEARARRAY		(1<<1)		/**< If an input array is specified, clear it before adding. */
    1.63 +#define MAPLIST_FLAG_NO_DEFAULT		(1<<2)		/**< Do not read "default" or "mapcyclefile" on failure. */
    1.64 +
    1.65 +class MapLists : public SMGlobalClass, public ITextListener_SMC
    1.66 +{
    1.67 +public:
    1.68 +	enum MapListState
    1.69 +	{
    1.70 +		MPS_NONE,
    1.71 +		MPS_GLOBAL,
    1.72 +		MPS_MAPLIST,
    1.73 +	};
    1.74 +public:
    1.75 +	MapLists()
    1.76 +	{
    1.77 +		m_pMapCycleFile = NULL;
    1.78 +		m_ConfigLastChanged = 0;
    1.79 +		m_nSerialChange = 0;
    1.80 +	}
    1.81 +	void OnSourceModAllInitialized()
    1.82 +	{
    1.83 +		m_pMapCycleFile = icvar->FindVar("mapcyclefile");
    1.84 +		g_SourceMod.BuildPath(Path_SM, m_ConfigFile, sizeof(m_ConfigFile), "configs/maplists.cfg");
    1.85 +	}
    1.86 +	void OnSourceModShutdown()
    1.87 +	{
    1.88 +		DumpCache(NULL);
    1.89 +	}
    1.90 +	void AddOrUpdateDefault(const char *name, const char *file)
    1.91 +	{
    1.92 +		char path[PLATFORM_MAX_PATH];
    1.93 +		maplist_info_t *pMapList, **ppMapList;
    1.94 +
    1.95 +		if ((ppMapList = m_ListLookup.retrieve(name)) == NULL)
    1.96 +		{
    1.97 +			pMapList = new maplist_info_t;
    1.98 +			pMapList->bIsCompat = true;
    1.99 +			pMapList->bIsPath = true;
   1.100 +			pMapList->last_modified_time = 0;
   1.101 +			strncopy(pMapList->name, name, sizeof(pMapList->name));
   1.102 +			pMapList->pArray = NULL;
   1.103 +			g_SourceMod.BuildPath(Path_Game, 
   1.104 +				pMapList->path,
   1.105 +				sizeof(pMapList->path),
   1.106 +				"%s",
   1.107 +				file);
   1.108 +			pMapList->serial = 0;
   1.109 +			m_ListLookup.insert(name, pMapList);
   1.110 +			m_MapLists.push_back(pMapList);
   1.111 +			return;
   1.112 +		}
   1.113 +
   1.114 +		pMapList = *ppMapList;
   1.115 +
   1.116 +		/* Don't modify if it's from the config file */
   1.117 +		if (!pMapList->bIsCompat)
   1.118 +		{
   1.119 +			return;
   1.120 +		}
   1.121 +
   1.122 +		g_SourceMod.BuildPath(Path_Game,
   1.123 +			path,
   1.124 +			sizeof(path),
   1.125 +			"%s",
   1.126 +			file);
   1.127 +
   1.128 +		/* If the path matches, don't reset the serial/time */
   1.129 +		if (strcmp(path, pMapList->path) == 0)
   1.130 +		{
   1.131 +			return;
   1.132 +		}
   1.133 +
   1.134 +		strncopy(pMapList->path, path, sizeof(pMapList->path));
   1.135 +		pMapList->bIsPath = true;
   1.136 +		pMapList->last_modified_time = 0;
   1.137 +		pMapList->serial = 0;
   1.138 +	}
   1.139 +	void UpdateCache()
   1.140 +	{
   1.141 +		bool fileFound;
   1.142 +		SMCError error;
   1.143 +		time_t fileTime;
   1.144 +		SMCStates states = {0, 0};
   1.145 +		
   1.146 +		fileFound = g_LibSys.FileTime(m_ConfigFile, FileTime_LastChange, &fileTime);
   1.147 +
   1.148 +		/* If the file is found and hasn't changed, bail out now. */
   1.149 +		if (fileFound && fileTime == m_ConfigLastChanged)
   1.150 +		{
   1.151 +			return;
   1.152 +		}
   1.153 +
   1.154 +		/* If the file wasn't found, and we already have entries, we bail out too. 
   1.155 +		 * This case lets us optimize when a user deletes the config file, so we 
   1.156 +		 * don't reparse every single time the function is called.
   1.157 +		 */
   1.158 +		if (!fileFound && m_MapLists.size() > 0)
   1.159 +		{
   1.160 +			return;
   1.161 +		}
   1.162 +
   1.163 +		/* Dump everything we know about. */
   1.164 +		List<maplist_info_t *> compat;
   1.165 +		DumpCache(&compat);
   1.166 +
   1.167 +		/* All this is to add the default entry back in. */
   1.168 +		maplist_info_t *pDefList = new maplist_info_t;
   1.169 +
   1.170 +		pDefList->bIsPath = true;
   1.171 +		strncopy(pDefList->name, "mapcyclefile", sizeof(pDefList->name));
   1.172 +		g_SourceMod.BuildPath(Path_Game,
   1.173 +			pDefList->path, 
   1.174 +			sizeof(pDefList->path),
   1.175 +			"%s",
   1.176 +			m_pMapCycleFile ? m_pMapCycleFile->GetString() : "mapcycle.txt");
   1.177 +		pDefList->last_modified_time = 0;
   1.178 +		pDefList->pArray = NULL;
   1.179 +		pDefList->serial = 0;
   1.180 +
   1.181 +		m_ListLookup.insert("mapcyclefile", pDefList);
   1.182 +		m_MapLists.push_back(pDefList);
   1.183 +
   1.184 +		/* Now parse the config file even if we don't know about it.
   1.185 +		 * This will give us a nice error message.
   1.186 +		 */
   1.187 +		if ((error = g_TextParser.ParseFile_SMC(m_ConfigFile, this, &states))
   1.188 +			!= SMCError_Okay)
   1.189 +		{
   1.190 +			const char *errmsg = g_TextParser.GetSMCErrorString(error);
   1.191 +			if (errmsg == NULL)
   1.192 +			{
   1.193 +				errmsg = "Unknown error";
   1.194 +			}
   1.195 +			g_Logger.LogError("[SM] Could not parse file \"%s\"", m_ConfigFile);
   1.196 +			g_Logger.LogError("[SM] Error on line %d (col %d): %s",
   1.197 +				states.line,
   1.198 +				states.col,
   1.199 +				errmsg);
   1.200 +		}
   1.201 +		else
   1.202 +		{
   1.203 +			m_ConfigLastChanged = fileTime;
   1.204 +		}
   1.205 +
   1.206 +		/* Now, re-add compat stuff back in if we can. */
   1.207 +		List<maplist_info_t *>::iterator iter = compat.begin();
   1.208 +		while (iter != compat.end())
   1.209 +		{
   1.210 +			if (m_ListLookup.retrieve((*iter)->name) != NULL)
   1.211 +			{
   1.212 +				/* The compatibility shim is no longer needed. */
   1.213 +				delete (*iter)->pArray;
   1.214 +				delete (*iter);
   1.215 +			}
   1.216 +			else
   1.217 +			{
   1.218 +				m_ListLookup.insert((*iter)->name, (*iter));
   1.219 +				m_MapLists.push_back((*iter));
   1.220 +			}
   1.221 +			iter = compat.erase(iter);
   1.222 +		}
   1.223 +	}
   1.224 +	void ReadSMC_ParseStart()
   1.225 +	{
   1.226 +		m_CurState = MPS_NONE;
   1.227 +		m_IgnoreLevel = 0;
   1.228 +		m_pCurMapList = NULL;
   1.229 +	}
   1.230 +	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name)
   1.231 +	{
   1.232 +		if (m_IgnoreLevel)
   1.233 +		{
   1.234 +			m_IgnoreLevel++;
   1.235 +			return SMCResult_Continue;
   1.236 +		}
   1.237 +
   1.238 +		if (m_CurState == MPS_NONE)
   1.239 +		{
   1.240 +			if (strcmp(name, "MapLists") == 0)
   1.241 +			{
   1.242 +				m_CurState = MPS_GLOBAL;
   1.243 +			}
   1.244 +			else
   1.245 +			{
   1.246 +				m_IgnoreLevel = 1;
   1.247 +			}
   1.248 +		}
   1.249 +		else if (m_CurState == MPS_GLOBAL)
   1.250 +		{
   1.251 +			m_pCurMapList = new maplist_info_t;
   1.252 +
   1.253 +			memset(m_pCurMapList, 0, sizeof(maplist_info_t));
   1.254 +			strncopy(m_pCurMapList->name, name, sizeof(m_pCurMapList->name));
   1.255 +
   1.256 +			m_CurState = MPS_MAPLIST;
   1.257 +		}
   1.258 +		else if (m_CurState == MPS_MAPLIST)
   1.259 +		{
   1.260 +			m_IgnoreLevel++;
   1.261 +		}
   1.262 +
   1.263 +		return SMCResult_Continue;
   1.264 +	}
   1.265 +	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
   1.266 +	{
   1.267 +		if (m_IgnoreLevel || m_pCurMapList == NULL)
   1.268 +		{
   1.269 +			return SMCResult_Continue;
   1.270 +		}
   1.271 +
   1.272 +		if (strcmp(key, "file") == 0)
   1.273 +		{
   1.274 +			g_SourceMod.BuildPath(Path_Game, 
   1.275 +				m_pCurMapList->path,
   1.276 +				sizeof(m_pCurMapList->path),
   1.277 +				"%s",
   1.278 +				value);
   1.279 +			m_pCurMapList->bIsPath = true;
   1.280 +		}
   1.281 +		else if (strcmp(key, "target") == 0)
   1.282 +		{
   1.283 +			strncopy(m_pCurMapList->path, value, sizeof(m_pCurMapList->path));
   1.284 +			m_pCurMapList->bIsPath = false;
   1.285 +		}
   1.286 +
   1.287 +		return SMCResult_Continue;
   1.288 +	}
   1.289 +	SMCResult ReadSMC_LeavingSection(const SMCStates *states)
   1.290 +	{
   1.291 +		if (m_IgnoreLevel)
   1.292 +		{
   1.293 +			m_IgnoreLevel--;
   1.294 +			return SMCResult_Continue;
   1.295 +		}
   1.296 +
   1.297 +		if (m_CurState == MPS_MAPLIST)
   1.298 +		{
   1.299 +			if (m_pCurMapList != NULL
   1.300 +				&& m_pCurMapList->path[0] != '\0'
   1.301 +				&& m_ListLookup.retrieve(m_pCurMapList->name) == NULL)
   1.302 +			{
   1.303 +				m_ListLookup.insert(m_pCurMapList->name, m_pCurMapList);
   1.304 +				m_MapLists.push_back(m_pCurMapList);
   1.305 +				m_pCurMapList = NULL;
   1.306 +			}
   1.307 +			else
   1.308 +			{
   1.309 +				delete m_pCurMapList;
   1.310 +				m_pCurMapList = NULL;
   1.311 +			}
   1.312 +			m_CurState = MPS_GLOBAL;
   1.313 +		}
   1.314 +		else if (m_CurState == MPS_GLOBAL)
   1.315 +		{
   1.316 +			m_CurState = MPS_NONE;
   1.317 +		}
   1.318 +
   1.319 +		return SMCResult_Continue;
   1.320 +	}
   1.321 +	void ReadSMC_ParseEnd(bool halted, bool failed)
   1.322 +	{
   1.323 +		delete m_pCurMapList;
   1.324 +		m_pCurMapList = NULL;
   1.325 +	}
   1.326 +	static int sort_maps_in_adt_array(const void *str1, const void *str2)
   1.327 +	{
   1.328 +		return strcmp((char *)str1, (char *)str2);
   1.329 +	}
   1.330 +	CellArray *UpdateMapList(CellArray *pUseArray, const char *name, int *pSerial, unsigned int flags)
   1.331 +	{
   1.332 +		int change_serial;
   1.333 +		CellArray *pNewArray;
   1.334 +		bool success, free_new_array;
   1.335 +
   1.336 +		free_new_array = false;
   1.337 +		
   1.338 +		if ((success = GetMapList(&pNewArray, name, &change_serial)) == false)
   1.339 +		{
   1.340 +			if ((flags & MAPLIST_FLAG_NO_DEFAULT) != MAPLIST_FLAG_NO_DEFAULT)
   1.341 +			{
   1.342 +				/* If this list failed, and it's not the default, try the default. 
   1.343 +				 */
   1.344 +				if (strcmp(name, "default") != 0)
   1.345 +				{
   1.346 +					success = GetMapList(&pNewArray, name, &change_serial);
   1.347 +				}
   1.348 +				/* If either of the last two conditions failed, try again if we can. */
   1.349 +				if (!success)
   1.350 +				{
   1.351 +					success = GetMapList(&pNewArray, "mapcyclefile", &change_serial);
   1.352 +				}
   1.353 +			}
   1.354 +		}
   1.355 +
   1.356 +		/* If there was a success, and the serial has not changed, bail out. */
   1.357 +		if (*pSerial == change_serial)
   1.358 +		{
   1.359 +			return NULL;
   1.360 +		}
   1.361 +
   1.362 +		/**
   1.363 +		 * If there was a success but no map list, we need to look in the maps folder.
   1.364 +		 * If there was a failure and the flag is specified, we need to look in the maps folder.
   1.365 +		 */
   1.366 +		if ((success && pNewArray == NULL)
   1.367 +			|| (!success && ((flags & MAPLIST_FLAG_MAPSFOLDER) == MAPLIST_FLAG_MAPSFOLDER)))
   1.368 +		{
   1.369 +			char path[255];
   1.370 +			IDirectory *pDir;
   1.371 +
   1.372 +			pNewArray = new CellArray(64);
   1.373 +			free_new_array = true;
   1.374 +			g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "maps");
   1.375 +
   1.376 +			if ((pDir = g_LibSys.OpenDirectory(path)) != NULL)
   1.377 +			{
   1.378 +				char *ptr;
   1.379 +				cell_t *blk;
   1.380 +				char buffer[PLATFORM_MAX_PATH];
   1.381 +
   1.382 +				while (pDir->MoreFiles())
   1.383 +				{
   1.384 +					if (!pDir->IsEntryFile()
   1.385 +						|| strcmp(pDir->GetEntryName(), ".") == 0
   1.386 +						|| strcmp(pDir->GetEntryName(), "..") == 0)
   1.387 +					{
   1.388 +						pDir->NextEntry();
   1.389 +						continue;
   1.390 +					}
   1.391 +					strncopy(buffer, pDir->GetEntryName(), sizeof(buffer));
   1.392 +					if ((ptr = strstr(buffer, ".bsp")) == NULL)
   1.393 +					{
   1.394 +						pDir->NextEntry();
   1.395 +						continue;
   1.396 +					}
   1.397 +					*ptr = '\0';
   1.398 +					if (!engine->IsMapValid(buffer))
   1.399 +					{
   1.400 +						pDir->NextEntry();
   1.401 +						continue;
   1.402 +					}
   1.403 +					if ((blk = pNewArray->push()) == NULL)
   1.404 +					{
   1.405 +						pDir->NextEntry();
   1.406 +						continue;
   1.407 +					}
   1.408 +					strncopy((char *)blk, buffer, 255);
   1.409 +					pDir->NextEntry();
   1.410 +				}
   1.411 +				g_LibSys.CloseDirectory(pDir);
   1.412 +			}
   1.413 +
   1.414 +			/* Remove the array if there were no items. */
   1.415 +			if (pNewArray->size() == 0)
   1.416 +			{
   1.417 +				delete pNewArray;
   1.418 +				pNewArray = NULL;
   1.419 +			}
   1.420 +			else
   1.421 +			{
   1.422 +				qsort(pNewArray->base(), 
   1.423 +					pNewArray->size(), 
   1.424 +					pNewArray->blocksize() * sizeof(cell_t), 
   1.425 +					sort_maps_in_adt_array);
   1.426 +			}
   1.427 +
   1.428 +			change_serial = -1;
   1.429 +		}
   1.430 +
   1.431 +		/* If there is still no array by this point, bail out. */
   1.432 +		if (pNewArray == NULL)
   1.433 +		{
   1.434 +			*pSerial = -1;
   1.435 +			return NULL;
   1.436 +		}
   1.437 +
   1.438 +		*pSerial = change_serial;
   1.439 +
   1.440 +		/* If there is no input array, return something temporary. */
   1.441 +		if (pUseArray == NULL)
   1.442 +		{
   1.443 +			if (free_new_array)
   1.444 +			{
   1.445 +				return pNewArray;
   1.446 +			}
   1.447 +			else
   1.448 +			{
   1.449 +				return pNewArray->clone();
   1.450 +			}
   1.451 +		}
   1.452 +
   1.453 +		/* Clear the input array if necessary. */
   1.454 +		if ((flags & MAPLIST_FLAG_CLEARARRAY) == MAPLIST_FLAG_CLEARARRAY)
   1.455 +		{
   1.456 +			pUseArray->clear();
   1.457 +		}
   1.458 +
   1.459 +		/* Copy. */
   1.460 +		cell_t *blk_dst;
   1.461 +		cell_t *blk_src;
   1.462 +		for (size_t i = 0; i < pNewArray->size(); i++)
   1.463 +		{
   1.464 +			blk_dst = pUseArray->push();
   1.465 +			blk_src = pNewArray->at(i);
   1.466 +			strncopy((char *)blk_dst, (char *)blk_src, pUseArray->blocksize() * sizeof(cell_t));
   1.467 +		}
   1.468 +
   1.469 +		/* Free resources if necessary. */
   1.470 +		if (free_new_array)
   1.471 +		{
   1.472 +			delete pNewArray;
   1.473 +		}
   1.474 +
   1.475 +		/* Return the array we were given. */
   1.476 +		return pUseArray;
   1.477 +	}
   1.478 +private:
   1.479 +	bool GetMapList(CellArray **ppArray, const char *name, int *pSerial)
   1.480 +	{
   1.481 +		time_t last_time;
   1.482 +		maplist_info_t *pMapList, **ppMapList;
   1.483 +
   1.484 +		if ((ppMapList = m_ListLookup.retrieve(name)) == NULL)
   1.485 +		{
   1.486 +			return false;
   1.487 +		}
   1.488 +
   1.489 +		pMapList = *ppMapList;
   1.490 +
   1.491 +		if (!pMapList->bIsPath)
   1.492 +		{
   1.493 +			return GetMapList(ppArray, pMapList->path, pSerial);
   1.494 +		}
   1.495 +
   1.496 +		/* If it is a path, and the path is "*", assume all files must be used. */
   1.497 +		if (strcmp(pMapList->path, "*") == 0)
   1.498 +		{
   1.499 +			*ppArray = NULL;
   1.500 +			return true;
   1.501 +		}
   1.502 +
   1.503 +		if (m_pMapCycleFile != NULL && strcmp(name, "mapcyclefile") == 0)
   1.504 +		{
   1.505 +			if (strcmp(m_pMapCycleFile->GetString(), pMapList->path) != 0)
   1.506 +			{
   1.507 +				strncopy(pMapList->path, m_pMapCycleFile->GetString(), sizeof(pMapList->path));
   1.508 +				pMapList->last_modified_time = 0;
   1.509 +			}
   1.510 +		}
   1.511 +
   1.512 +		if (!g_LibSys.FileTime(pMapList->path, FileTime_LastChange, &last_time)
   1.513 +			|| last_time > pMapList->last_modified_time)
   1.514 +		{
   1.515 +			/* Reparse */
   1.516 +			FILE *fp;
   1.517 +			cell_t *blk;
   1.518 +			char buffer[255];
   1.519 +
   1.520 +			if ((fp = fopen(pMapList->path, "rt")) == NULL)
   1.521 +			{
   1.522 +				return false;
   1.523 +			}
   1.524 +
   1.525 +			delete pMapList->pArray;
   1.526 +			pMapList->pArray = new CellArray(64);
   1.527 +
   1.528 +			while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
   1.529 +			{
   1.530 +				size_t len = strlen(buffer);
   1.531 +				char *ptr = UTIL_TrimWhitespace(buffer, len);
   1.532 +				if (*ptr == '\0'
   1.533 +					|| *ptr == ';'
   1.534 +					|| strncmp(ptr, "//", 2) == 0)
   1.535 +				{
   1.536 +					continue;
   1.537 +				}
   1.538 +				if (!engine->IsMapValid(ptr))
   1.539 +				{
   1.540 +					continue;
   1.541 +				}
   1.542 +				if ((blk = pMapList->pArray->push()) != NULL)
   1.543 +				{
   1.544 +					strncopy((char *)blk, ptr, 255);
   1.545 +				}
   1.546 +			}
   1.547 +
   1.548 +			fclose(fp);
   1.549 +
   1.550 +			pMapList->last_modified_time = last_time;
   1.551 +			pMapList->serial = ++m_nSerialChange;
   1.552 +		}
   1.553 +
   1.554 +		if (pMapList->pArray == NULL || pMapList->pArray->size() == 0)
   1.555 +		{
   1.556 +			return false;
   1.557 +		}
   1.558 +
   1.559 +		*pSerial = pMapList->serial;
   1.560 +		*ppArray = pMapList->pArray;
   1.561 +
   1.562 +		return true;
   1.563 +	}
   1.564 +	void DumpCache(List<maplist_info_t *> *compat_list)
   1.565 +	{
   1.566 +		m_ListLookup.clear();
   1.567 +
   1.568 +		List<maplist_info_t *>::iterator iter = m_MapLists.begin();
   1.569 +		while (iter != m_MapLists.end())
   1.570 +		{
   1.571 +			if (compat_list != NULL && (*iter)->bIsCompat)
   1.572 +			{
   1.573 +				compat_list->push_back((*iter));
   1.574 +			}
   1.575 +			else
   1.576 +			{
   1.577 +				delete (*iter)->pArray;
   1.578 +				delete (*iter);
   1.579 +			}
   1.580 +			iter = m_MapLists.erase(iter);
   1.581 +		}
   1.582 +	}
   1.583 +private:
   1.584 +	char m_ConfigFile[PLATFORM_MAX_PATH];
   1.585 +	time_t m_ConfigLastChanged;
   1.586 +	ConVar *m_pMapCycleFile;
   1.587 +	KTrie<maplist_info_t *> m_ListLookup;
   1.588 +	List<maplist_info_t *> m_MapLists;
   1.589 +	MapListState m_CurState;
   1.590 +	unsigned int m_IgnoreLevel;
   1.591 +	maplist_info_t *m_pCurMapList;
   1.592 +	int m_nSerialChange;
   1.593 +} s_MapLists;
   1.594 +
   1.595 +static cell_t LoadMapList(IPluginContext *pContext, const cell_t *params)
   1.596 +{
   1.597 +	char *str;
   1.598 +	Handle_t hndl;
   1.599 +	cell_t *addr, flags;
   1.600 +	CellArray *pArray, *pNewArray;
   1.601 +
   1.602 +	hndl = params[1];
   1.603 +	pContext->LocalToPhysAddr(params[2], &addr);
   1.604 +	pContext->LocalToString(params[3], &str);
   1.605 +	flags = params[4];
   1.606 +
   1.607 +	/* Make sure the input Handle is valid */
   1.608 +	pArray = NULL;
   1.609 +	if (hndl != BAD_HANDLE)
   1.610 +	{
   1.611 +		HandleError err;
   1.612 +		HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
   1.613 +
   1.614 +		if ((err = g_HandleSys.ReadHandle(hndl, htCellArray, &sec, (void **)&pArray))
   1.615 +			!= HandleError_None)
   1.616 +		{
   1.617 +			return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
   1.618 +		}
   1.619 +	}
   1.620 +
   1.621 +	/* Make sure the map list cache is up to date at the root */
   1.622 +	s_MapLists.UpdateCache();
   1.623 +
   1.624 +	/* Try to get the map list. */
   1.625 +	if ((pNewArray = s_MapLists.UpdateMapList(pArray, str, addr, flags)) == NULL)
   1.626 +	{
   1.627 +		return BAD_HANDLE;
   1.628 +	}
   1.629 +
   1.630 +	/* If the user wanted a new array, create it now. */
   1.631 +	if (hndl == BAD_HANDLE)
   1.632 +	{
   1.633 +		if ((hndl = g_HandleSys.CreateHandle(htCellArray, pNewArray, pContext->GetIdentity(), g_pCoreIdent, NULL))
   1.634 +			== BAD_HANDLE)
   1.635 +		{
   1.636 +			*addr = -1;
   1.637 +			delete pNewArray;
   1.638 +			return BAD_HANDLE;
   1.639 +		}
   1.640 +	}
   1.641 +
   1.642 +	return hndl;
   1.643 +}
   1.644 +
   1.645 +static cell_t SetMapListCompatBind(IPluginContext *pContext, const cell_t *params)
   1.646 +{
   1.647 +	char *name, *file;
   1.648 +
   1.649 +	pContext->LocalToString(params[1], &name);
   1.650 +	pContext->LocalToString(params[2], &file);
   1.651 +
   1.652 +	s_MapLists.AddOrUpdateDefault(name, file);
   1.653 +
   1.654 +	return 1;
   1.655 +}
   1.656 +
   1.657 +REGISTER_NATIVES(mapListNatives)
   1.658 +{
   1.659 +	{"ReadMapList",				LoadMapList},
   1.660 +	{"SetMapListCompatBind",	SetMapListCompatBind},
   1.661 +	{NULL,						NULL},
   1.662 +};