Updated some CS:GO gamedata.
3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
33 #include <sm_trie_tpl.h>
34 #include "common_logic.h"
35 #include "CellArray.h"
36 #include <IGameHelpers.h>
37 #include <ILibrarySys.h>
38 #include <ITextParsers.h>
39 #include <ISourceMod.h>
40 #include "stringutil.h"
42 using namespace SourceHook;
48 char name[PLATFORM_MAX_PATH];
49 char path[PLATFORM_MAX_PATH];
50 time_t last_modified_time;
55 #define MAPLIST_FLAG_MAPSFOLDER (1<<0) /**< On failure, use all maps in the maps folder. */
56 #define MAPLIST_FLAG_CLEARARRAY (1<<1) /**< If an input array is specified, clear it before adding. */
57 #define MAPLIST_FLAG_NO_DEFAULT (1<<2) /**< Do not read "default" or "mapcyclefile" on failure. */
59 class MapLists : public SMGlobalClass, public ITextListener_SMC
71 m_pMapCycleFile = NULL;
72 m_ConfigLastChanged = 0;
75 void OnSourceModAllInitialized()
77 g_pSM->BuildPath(Path_SM, m_ConfigFile, sizeof(m_ConfigFile), "configs/maplists.cfg");
79 void OnSourceModShutdown()
83 void GetMapCycleFilePath(char *pBuffer, int maxlen)
85 const char *pEngineName = smcore.GetSourceEngineName();
86 const char *pMapCycleFileName = m_pMapCycleFile ? smcore.GetCvarString(m_pMapCycleFile) : "mapcycle.txt";
88 if (strcmp(pEngineName, "tf2") == 0 || strcmp(pEngineName, "css") == 0
89 || strcmp(pEngineName, "dods") == 0 || strcmp(pEngineName, "hl2dm") == 0)
91 // These four games and Source SDK 2013 do a lookup in this order; so shall we.
92 g_pSM->BuildPath(Path_Game,
98 if (!libsys->PathExists(pBuffer))
100 g_pSM->BuildPath(Path_Game,
106 if (!libsys->PathExists(pBuffer))
108 g_pSM->BuildPath(Path_Game,
111 "cfg/mapcycle_default.txt");
117 g_pSM->BuildPath(Path_Game,
124 void AddOrUpdateDefault(const char *name, const char *file)
126 char path[PLATFORM_MAX_PATH];
127 maplist_info_t *pMapList, **ppMapList;
129 if ((ppMapList = m_ListLookup.retrieve(name)) == NULL)
131 pMapList = new maplist_info_t;
132 pMapList->bIsCompat = true;
133 pMapList->bIsPath = true;
134 pMapList->last_modified_time = 0;
135 smcore.strncopy(pMapList->name, name, sizeof(pMapList->name));
136 pMapList->pArray = NULL;
137 g_pSM->BuildPath(Path_Game, pMapList->path, sizeof(pMapList->path), "%s", file);
138 pMapList->serial = 0;
139 m_ListLookup.insert(name, pMapList);
140 m_MapLists.push_back(pMapList);
144 pMapList = *ppMapList;
146 /* Don't modify if it's from the config file */
147 if (!pMapList->bIsCompat)
152 g_pSM->BuildPath(Path_Game, path, sizeof(path), "%s", file);
154 /* If the path matches, don't reset the serial/time */
155 if (strcmp(path, pMapList->path) == 0)
160 smcore.strncopy(pMapList->path, path, sizeof(pMapList->path));
161 pMapList->bIsPath = true;
162 pMapList->last_modified_time = 0;
163 pMapList->serial = 0;
170 SMCStates states = {0, 0};
172 fileFound = libsys->FileTime(m_ConfigFile, FileTime_LastChange, &fileTime);
174 /* If the file is found and hasn't changed, bail out now. */
175 if (fileFound && fileTime == m_ConfigLastChanged)
180 /* If the file wasn't found, and we already have entries, we bail out too.
181 * This case lets us optimize when a user deletes the config file, so we
182 * don't reparse every single time the function is called.
184 if (!fileFound && m_MapLists.size() > 0)
189 m_pMapCycleFile = smcore.FindConVar("mapcyclefile");
191 /* Dump everything we know about. */
192 List<maplist_info_t *> compat;
195 /* All this is to add the default entry back in. */
196 maplist_info_t *pDefList = new maplist_info_t;
198 pDefList->bIsPath = true;
199 smcore.strncopy(pDefList->name, "mapcyclefile", sizeof(pDefList->name));
201 GetMapCycleFilePath(pDefList->path, sizeof(pDefList->path));
203 pDefList->last_modified_time = 0;
204 pDefList->pArray = NULL;
205 pDefList->serial = 0;
207 m_ListLookup.insert("mapcyclefile", pDefList);
208 m_MapLists.push_back(pDefList);
210 /* Now parse the config file even if we don't know about it.
211 * This will give us a nice error message.
213 if ((error = textparser->ParseFile_SMC(m_ConfigFile, this, &states))
216 const char *errmsg = textparser->GetSMCErrorString(error);
219 errmsg = "Unknown error";
221 smcore.LogError("[SM] Could not parse file \"%s\"", m_ConfigFile);
222 smcore.LogError("[SM] Error on line %d (col %d): %s", states.line, states.col, errmsg);
226 m_ConfigLastChanged = fileTime;
229 /* Now, re-add compat stuff back in if we can. */
230 List<maplist_info_t *>::iterator iter = compat.begin();
231 while (iter != compat.end())
233 if (m_ListLookup.retrieve((*iter)->name) != NULL)
235 /* The compatibility shim is no longer needed. */
236 delete (*iter)->pArray;
241 m_ListLookup.insert((*iter)->name, (*iter));
242 m_MapLists.push_back((*iter));
244 iter = compat.erase(iter);
247 void ReadSMC_ParseStart()
249 m_CurState = MPS_NONE;
251 m_pCurMapList = NULL;
253 SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name)
258 return SMCResult_Continue;
261 if (m_CurState == MPS_NONE)
263 if (strcmp(name, "MapLists") == 0)
265 m_CurState = MPS_GLOBAL;
272 else if (m_CurState == MPS_GLOBAL)
274 m_pCurMapList = new maplist_info_t;
276 memset(m_pCurMapList, 0, sizeof(maplist_info_t));
277 smcore.strncopy(m_pCurMapList->name, name, sizeof(m_pCurMapList->name));
279 m_CurState = MPS_MAPLIST;
281 else if (m_CurState == MPS_MAPLIST)
286 return SMCResult_Continue;
288 SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
290 if (m_IgnoreLevel || m_pCurMapList == NULL)
292 return SMCResult_Continue;
295 if (strcmp(key, "file") == 0)
297 g_pSM->BuildPath(Path_Game,
299 sizeof(m_pCurMapList->path),
302 m_pCurMapList->bIsPath = true;
304 else if (strcmp(key, "target") == 0)
306 smcore.strncopy(m_pCurMapList->path, value, sizeof(m_pCurMapList->path));
307 m_pCurMapList->bIsPath = false;
310 return SMCResult_Continue;
312 SMCResult ReadSMC_LeavingSection(const SMCStates *states)
317 return SMCResult_Continue;
320 if (m_CurState == MPS_MAPLIST)
322 if (m_pCurMapList != NULL
323 && m_pCurMapList->path[0] != '\0'
324 && m_ListLookup.retrieve(m_pCurMapList->name) == NULL)
326 m_ListLookup.insert(m_pCurMapList->name, m_pCurMapList);
327 m_MapLists.push_back(m_pCurMapList);
328 m_pCurMapList = NULL;
332 delete m_pCurMapList;
333 m_pCurMapList = NULL;
335 m_CurState = MPS_GLOBAL;
337 else if (m_CurState == MPS_GLOBAL)
339 m_CurState = MPS_NONE;
342 return SMCResult_Continue;
344 void ReadSMC_ParseEnd(bool halted, bool failed)
346 delete m_pCurMapList;
347 m_pCurMapList = NULL;
349 static int sort_maps_in_adt_array(const void *str1, const void *str2)
351 return strcmp((char *)str1, (char *)str2);
353 CellArray *UpdateMapList(CellArray *pUseArray, const char *name, int *pSerial, unsigned int flags)
356 CellArray *pNewArray = NULL;
357 bool success, free_new_array;
359 free_new_array = false;
361 if ((success = GetMapList(&pNewArray, name, &change_serial)) == false)
363 if ((flags & MAPLIST_FLAG_NO_DEFAULT) != MAPLIST_FLAG_NO_DEFAULT)
365 /* If this list failed, and it's not the default, try the default.
367 if (strcmp(name, "default") != 0)
369 success = GetMapList(&pNewArray, name, &change_serial);
371 /* If either of the last two conditions failed, try again if we can. */
372 if (!success && strcmp(name, "mapcyclefile") != 0)
374 success = GetMapList(&pNewArray, "mapcyclefile", &change_serial);
379 /* If there was a success, and the serial has not changed, bail out. */
380 if (success && *pSerial == change_serial)
386 * If there was a success but no map list, we need to look in the maps folder.
387 * If there was a failure and the flag is specified, we need to look in the maps folder.
389 if ((success && pNewArray == NULL)
390 || (!success && ((flags & MAPLIST_FLAG_MAPSFOLDER) == MAPLIST_FLAG_MAPSFOLDER)))
392 pNewArray = new CellArray(64);
393 free_new_array = true;
397 FileFindHandle_t findHandle;
398 const char *fileName = smcore.filesystem->FindFirstEx("maps/*.bsp", "GAME", &findHandle);
402 char buffer[PLATFORM_MAX_PATH];
404 UTIL_StripExtension(fileName, buffer, sizeof(buffer));
406 if (!engine->IsMapValid(buffer))
408 fileName = smcore.filesystem->FindNext(findHandle);
412 if ((blk = pNewArray->push()) == NULL)
414 fileName = smcore.filesystem->FindNext(findHandle);
418 smcore.strncopy((char *)blk, buffer, 255);
420 fileName = smcore.filesystem->FindNext(findHandle);
423 smcore.filesystem->FindClose(findHandle);
425 /* Remove the array if there were no items. */
426 if (pNewArray->size() == 0)
433 qsort(pNewArray->base(),
435 pNewArray->blocksize() * sizeof(cell_t),
436 sort_maps_in_adt_array);
442 /* If there is still no array by this point, bail out. */
443 if (pNewArray == NULL)
449 *pSerial = change_serial;
451 /* If there is no input array, return something temporary. */
452 if (pUseArray == NULL)
460 return pNewArray->clone();
464 /* Clear the input array if necessary. */
465 if ((flags & MAPLIST_FLAG_CLEARARRAY) == MAPLIST_FLAG_CLEARARRAY)
473 for (size_t i = 0; i < pNewArray->size(); i++)
475 blk_dst = pUseArray->push();
476 blk_src = pNewArray->at(i);
477 smcore.strncopy((char *)blk_dst, (char *)blk_src, pUseArray->blocksize() * sizeof(cell_t));
480 /* Free resources if necessary. */
486 /* Return the array we were given. */
490 bool GetMapList(CellArray **ppArray, const char *name, int *pSerial)
493 maplist_info_t *pMapList, **ppMapList;
495 if ((ppMapList = m_ListLookup.retrieve(name)) == NULL)
500 pMapList = *ppMapList;
502 if (!pMapList->bIsPath)
504 return GetMapList(ppArray, pMapList->path, pSerial);
507 /* If it is a path, and the path is "*", assume all files must be used. */
508 if (strcmp(pMapList->path, "*") == 0)
514 if (m_pMapCycleFile != NULL && strcmp(name, "mapcyclefile") == 0)
516 char path[PLATFORM_MAX_PATH];
517 GetMapCycleFilePath(path, sizeof(path));
519 if (strcmp(path, pMapList->path) != 0)
521 smcore.strncopy(pMapList->path, path, sizeof(pMapList->path));
522 pMapList->last_modified_time = 0;
526 if (!libsys->FileTime(pMapList->path, FileTime_LastChange, &last_time)
527 || last_time > pMapList->last_modified_time)
534 if ((fp = fopen(pMapList->path, "rt")) == NULL)
539 delete pMapList->pArray;
540 pMapList->pArray = new CellArray(64);
542 while (!feof(fp) && fgets(buffer, sizeof(buffer), fp) != NULL)
544 size_t len = strlen(buffer);
545 char *ptr = smcore.TrimWhitespace(buffer, len);
548 || strncmp(ptr, "//", 2) == 0)
552 if (!gamehelpers->IsMapValid(ptr))
556 if ((blk = pMapList->pArray->push()) != NULL)
558 smcore.strncopy((char *)blk, ptr, 255);
564 pMapList->last_modified_time = last_time;
565 pMapList->serial = ++m_nSerialChange;
568 if (pMapList->pArray == NULL || pMapList->pArray->size() == 0)
573 *pSerial = pMapList->serial;
574 *ppArray = pMapList->pArray;
578 void DumpCache(List<maplist_info_t *> *compat_list)
580 m_ListLookup.clear();
582 List<maplist_info_t *>::iterator iter = m_MapLists.begin();
583 while (iter != m_MapLists.end())
585 if (compat_list != NULL && (*iter)->bIsCompat)
587 compat_list->push_back((*iter));
591 delete (*iter)->pArray;
594 iter = m_MapLists.erase(iter);
598 char m_ConfigFile[PLATFORM_MAX_PATH];
599 time_t m_ConfigLastChanged;
600 ConVar *m_pMapCycleFile;
601 KTrie<maplist_info_t *> m_ListLookup;
602 List<maplist_info_t *> m_MapLists;
603 MapListState m_CurState;
604 unsigned int m_IgnoreLevel;
605 maplist_info_t *m_pCurMapList;
609 static cell_t LoadMapList(IPluginContext *pContext, const cell_t *params)
614 CellArray *pArray, *pNewArray;
617 pContext->LocalToPhysAddr(params[2], &addr);
618 pContext->LocalToString(params[3], &str);
621 /* Make sure the input Handle is valid */
623 if (hndl != BAD_HANDLE)
626 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
628 if ((err = handlesys->ReadHandle(hndl, htCellArray, &sec, (void **)&pArray))
631 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
635 /* Make sure the map list cache is up to date at the root */
636 s_MapLists.UpdateCache();
638 /* Try to get the map list. */
639 if ((pNewArray = s_MapLists.UpdateMapList(pArray, str, addr, flags)) == NULL)
644 /* If the user wanted a new array, create it now. */
645 if (hndl == BAD_HANDLE)
647 if ((hndl = handlesys->CreateHandle(htCellArray, pNewArray, pContext->GetIdentity(), g_pCoreIdent, NULL))
659 static cell_t SetMapListCompatBind(IPluginContext *pContext, const cell_t *params)
663 pContext->LocalToString(params[1], &name);
664 pContext->LocalToString(params[2], &file);
666 s_MapLists.UpdateCache();
667 s_MapLists.AddOrUpdateDefault(name, file);
672 REGISTER_NATIVES(mapListNatives)
674 {"ReadMapList", LoadMapList},
675 {"SetMapListCompatBind", SetMapListCompatBind},