core/GameConfigs.cpp
author Matt Woodrow <pred@alliedmods.net>
Fri Jul 04 22:54:14 2008 +0000 (2008-07-04)
changeset 2156 186f2f16564c
parent 2142 d9d876e97a66
child 2217 eca7057167a0
permissions -rw-r--r--
Nominations not blocking current map and excluded maps when used from console.
Removed error causing debug stuff from mapchooser.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <string.h>
    33 #include <stdlib.h>
    34 #include "GameConfigs.h"
    35 #include "sm_stringutil.h"
    36 #include "sourcemod.h"
    37 #include "sourcemm_api.h"
    38 #include "HalfLife2.h"
    39 #include "Logger.h"
    40 #include "ShareSys.h"
    41 #include "MemoryUtils.h"
    42 #include "LibrarySys.h"
    43 #include "HandleSys.h"
    44 #include "sm_crc32.h"
    45 #include "GameDataFetcher.h"
    46 
    47 #if defined PLATFORM_LINUX
    48 #include <dlfcn.h>
    49 #endif
    50 
    51 GameConfigManager g_GameConfigs;
    52 IGameConfig *g_pGameConf = NULL;
    53 char g_Game[256];
    54 char g_GameDesc[256] = {'!', '\0'};
    55 char g_GameName[256] = {'$', '\0'};
    56 
    57 #define PSTATE_NONE						0
    58 #define PSTATE_GAMES					1
    59 #define PSTATE_GAMEDEFS					2
    60 #define PSTATE_GAMEDEFS_OFFSETS			3
    61 #define PSTATE_GAMEDEFS_OFFSETS_OFFSET	4
    62 #define PSTATE_GAMEDEFS_KEYS			5
    63 #define PSTATE_GAMEDEFS_SUPPORTED		6
    64 #define PSTATE_GAMEDEFS_SIGNATURES		7
    65 #define PSTATE_GAMEDEFS_SIGNATURES_SIG	8
    66 #define PSTATE_GAMEDEFS_CRC				9
    67 #define PSTATE_GAMEDEFS_CRC_BINARY		10
    68 
    69 #if defined PLATFORM_WINDOWS
    70 #define PLATFORM_NAME				"windows"
    71 #define PLATFORM_SERVER_BINARY		"server.dll"
    72 #elif defined PLATFORM_LINUX
    73 #define PLATFORM_NAME				"linux"
    74 #define PLATFORM_SERVER_BINARY		"server_i486.so"
    75 #endif
    76 
    77 struct TempSigInfo
    78 {
    79 	void Reset()
    80 	{
    81 		library[0] = '\0';
    82 		sig[0] = '\0';
    83 	}
    84 	char sig[512];
    85 	char library[64];
    86 } s_TempSig;
    87 unsigned int s_ServerBinCRC;
    88 bool s_ServerBinCRC_Ok = false;
    89 
    90 CGameConfig::CGameConfig(const char *file)
    91 {
    92 	m_pFile = sm_strdup(file);
    93 	m_pOffsets = sm_trie_create();
    94 	m_pProps = sm_trie_create();
    95 	m_pKeys = sm_trie_create();
    96 	m_pSigs = sm_trie_create();
    97 	m_pStrings = new BaseStringTable(512);
    98 	m_RefCount = 0;
    99 }
   100 
   101 CGameConfig::~CGameConfig()
   102 {
   103 	sm_trie_destroy(m_pOffsets);
   104 	sm_trie_destroy(m_pProps);
   105 	sm_trie_destroy(m_pKeys);
   106 	sm_trie_destroy(m_pSigs);
   107 	delete [] m_pFile;
   108 	delete m_pStrings;
   109 }
   110 
   111 SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *name)
   112 {
   113 	if (m_IgnoreLevel)
   114 	{
   115 		m_IgnoreLevel++;
   116 		return SMCResult_Continue;
   117 	}
   118 
   119 	switch (m_ParseState)
   120 	{
   121 	case PSTATE_NONE:
   122 		{
   123 			if (strcmp(name, "Games") == 0)
   124 			{
   125 				m_ParseState = PSTATE_GAMES;
   126 			} else {
   127 				m_IgnoreLevel++;
   128 			}
   129 			break;
   130 		}
   131 	case PSTATE_GAMES:
   132 		{
   133 			if ((strcmp(name, "*") == 0)
   134 				|| (strcmp(name, "#default") == 0)
   135 				|| (strcasecmp(name, g_Game) == 0)
   136 				|| (strcasecmp(name, g_GameDesc) == 0)
   137 				|| (strcasecmp(name, g_GameName) == 0))
   138 			{
   139 				bShouldBeReadingDefault = true;
   140 				m_ParseState = PSTATE_GAMEDEFS;
   141 				strncopy(m_Game, name, sizeof(m_Game));
   142 			} else {
   143 				m_IgnoreLevel++;
   144 			}
   145 			break;
   146 		}
   147 	case PSTATE_GAMEDEFS:
   148 		{
   149 			if (strcmp(name, "Offsets") == 0)
   150 			{
   151 				m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
   152 			}
   153 			else if (strcmp(name, "Keys") == 0)
   154 			{
   155 				m_ParseState = PSTATE_GAMEDEFS_KEYS;
   156 			}
   157 			else if ((strcmp(name, "#supported") == 0)
   158 					 && (strcmp(m_Game, "#default") == 0))
   159 			{
   160 				m_ParseState = PSTATE_GAMEDEFS_SUPPORTED;
   161 				/* Ignore this section unless we get a game. */
   162 				bShouldBeReadingDefault = false;
   163 			}
   164 			else if (strcmp(name, "Signatures") == 0)
   165 			{
   166 				m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
   167 			}
   168 			else if (strcmp(name, "CRC") == 0)
   169 			{
   170 				m_ParseState = PSTATE_GAMEDEFS_CRC;
   171 				bShouldBeReadingDefault = false;
   172 			}
   173 			else
   174 			{
   175 				m_IgnoreLevel++;
   176 			}
   177 			break;
   178 		}
   179 	case PSTATE_GAMEDEFS_OFFSETS:
   180 		{
   181 			m_Prop[0] = '\0';
   182 			m_Class[0] = '\0';
   183 			strncopy(m_offset, name, sizeof(m_offset));
   184 			m_ParseState = PSTATE_GAMEDEFS_OFFSETS_OFFSET;
   185 			break;
   186 		}
   187 	case PSTATE_GAMEDEFS_SIGNATURES:
   188 		{
   189 			strncopy(m_offset, name, sizeof(m_offset));
   190 			s_TempSig.Reset();
   191 			m_ParseState = PSTATE_GAMEDEFS_SIGNATURES_SIG;
   192 			break;
   193 		}
   194 	case PSTATE_GAMEDEFS_CRC:
   195 		{
   196 			char error[255];
   197 			error[0] = '\0';
   198 			if (strcmp(name, "server") != 0)
   199 			{
   200 				UTIL_Format(error, sizeof(error), "Unrecognized library \"%s\"", name);
   201 			} 
   202 			else if (!s_ServerBinCRC_Ok)
   203 			{
   204 				FILE *fp;
   205 				char path[PLATFORM_MAX_PATH];
   206 
   207 				g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "bin/" PLATFORM_SERVER_BINARY);
   208 				if ((fp = fopen(path, "rb")) == NULL)
   209 				{
   210 					UTIL_Format(error, sizeof(error), "Could not open binary: %s", path);
   211 				} else {
   212 					size_t size;
   213 					void *buffer;
   214 
   215 					fseek(fp, 0, SEEK_END);
   216 					size = ftell(fp);
   217 					fseek(fp, 0, SEEK_SET);
   218 
   219 					buffer = malloc(size);
   220 					fread(buffer, size, 1, fp);
   221 					s_ServerBinCRC = UTIL_CRC32(buffer, size);
   222 					free(buffer);
   223 					s_ServerBinCRC_Ok = true;
   224 				}
   225 			}
   226 			if (error[0] != '\0')
   227 			{
   228 				m_IgnoreLevel = 1;
   229 				g_Logger.LogError("[SM] Error while parsing CRC section for \"%s\" (%s):", m_Game, m_pFile);
   230 				g_Logger.LogError("[SM] %s", error);
   231 			} else {
   232 				m_ParseState = PSTATE_GAMEDEFS_CRC_BINARY;
   233 			}
   234 			break;
   235 		}
   236 	/* No sub-sections allowed:
   237 	 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
   238 	 case PSTATE_GAMEDEFS_KEYS:
   239 	 case PSTATE_GAMEDEFS_SUPPORTED:
   240 	 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
   241 	 case PSTATE_GAMEDEFS_CRC_BINARY:
   242 	 */
   243 	default:
   244 		{
   245 			/* If we don't know what we got, start ignoring */
   246 			m_IgnoreLevel++;
   247 			break;
   248 		}
   249 	}
   250 
   251 	return SMCResult_Continue;
   252 }
   253 
   254 SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
   255 {
   256 	if (m_IgnoreLevel)
   257 	{
   258 		return SMCResult_Continue;
   259 	}
   260 
   261 	if (m_ParseState == PSTATE_GAMEDEFS_OFFSETS_OFFSET)
   262 	{
   263 		if (strcmp(key, "class") == 0)
   264 		{
   265 			strncopy(m_Class, value, sizeof(m_Class));
   266 		} else if (strcmp(key, "prop") == 0) {
   267 			strncopy(m_Prop, value, sizeof(m_Prop));
   268 		} else if (strcmp(key, PLATFORM_NAME) == 0) {
   269 			int val = atoi(value);
   270 			sm_trie_replace(m_pOffsets, m_offset, (void *)val);
   271 		}
   272 	} else if (m_ParseState == PSTATE_GAMEDEFS_KEYS) {
   273 		int id = m_pStrings->AddString(value);
   274 		sm_trie_replace(m_pKeys, key, (void *)id);
   275 	} else if (m_ParseState == PSTATE_GAMEDEFS_SUPPORTED) {
   276 		if (strcmp(key, "game") == 0
   277 			&& (strcasecmp(value, g_Game) == 0 
   278 				|| strcasecmp(value, g_GameDesc) == 0
   279 				|| strcasecmp(value, g_GameName) == 0)
   280 				)
   281 		{
   282 			bShouldBeReadingDefault = true;
   283 		}
   284 	} else if (m_ParseState == PSTATE_GAMEDEFS_SIGNATURES_SIG) {
   285 		if (strcmp(key, PLATFORM_NAME) == 0)
   286 		{
   287 			strncopy(s_TempSig.sig, value, sizeof(s_TempSig.sig));
   288 		} else if (strcmp(key, "library") == 0) {
   289 			strncopy(s_TempSig.library, value, sizeof(s_TempSig.library));
   290 		}
   291 	} else if (m_ParseState == PSTATE_GAMEDEFS_CRC_BINARY) {
   292 		if (strcmp(key, PLATFORM_NAME) == 0 
   293 			&& s_ServerBinCRC_Ok
   294 			&& !bShouldBeReadingDefault)
   295 		{
   296 			unsigned int crc = 0;
   297 			sscanf(value, "%08X", &crc);
   298 			if (s_ServerBinCRC == crc)
   299 			{
   300 				bShouldBeReadingDefault = true;
   301 			}
   302 		}
   303 	}
   304 
   305 	return SMCResult_Continue;
   306 }
   307 
   308 SMCResult CGameConfig::ReadSMC_LeavingSection(const SMCStates *states)
   309 {
   310 	if (m_IgnoreLevel)
   311 	{
   312 		m_IgnoreLevel--;
   313 		return SMCResult_Continue;
   314 	}
   315 
   316 	switch (m_ParseState)
   317 	{
   318 	case PSTATE_GAMES:
   319 		{
   320 			m_ParseState = PSTATE_NONE;
   321 			break;
   322 		}
   323 	case PSTATE_GAMEDEFS:
   324 		{
   325 			m_ParseState = PSTATE_GAMES;
   326 			break;
   327 		}
   328 	case PSTATE_GAMEDEFS_KEYS:
   329 	case PSTATE_GAMEDEFS_OFFSETS:
   330 		{
   331 			m_ParseState = PSTATE_GAMEDEFS;
   332 			break;
   333 		}
   334 	case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
   335 		{
   336 			/* Parse the offset... */
   337 			if (m_Class[0] != '\0'
   338 				&& m_Prop[0] != '\0')
   339 			{
   340 				SendProp *pProp = g_HL2.FindInSendTable(m_Class, m_Prop);
   341 				if (pProp)
   342 				{
   343 					int val = pProp->GetOffset();
   344 					sm_trie_replace(m_pOffsets, m_offset, (void *)val);
   345 					sm_trie_replace(m_pProps, m_offset, pProp);
   346 				} else {
   347 					/* Check if it's a non-default game and no offsets exist */
   348 					if (((strcmp(m_Game, "*") != 0) && strcmp(m_Game, "#default") != 0)
   349 						&& (!sm_trie_retrieve(m_pOffsets, m_offset, NULL)))
   350 					{
   351 						g_Logger.LogError("[SM] Unable to find property %s.%s (file \"%s\") (mod \"%s\")", 
   352 							m_Class,
   353 							m_Prop,
   354 							m_pFile,
   355 							m_Game);
   356 					}
   357 				}
   358 			}
   359 			m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
   360 			break;
   361 		}
   362 	case PSTATE_GAMEDEFS_CRC:
   363 	case PSTATE_GAMEDEFS_SUPPORTED:
   364 		{
   365 			if (!bShouldBeReadingDefault)
   366 			{
   367 				/* If we shouldn't read the rest of this section, set the ignore level. */
   368 				m_IgnoreLevel = 1;
   369 				m_ParseState = PSTATE_GAMES;
   370 			} else {
   371 				m_ParseState = PSTATE_GAMEDEFS;
   372 			}
   373 			break;
   374 		}
   375 	case PSTATE_GAMEDEFS_CRC_BINARY:
   376 		{
   377 			m_ParseState = PSTATE_GAMEDEFS_CRC;
   378 			break;
   379 		}
   380 	case PSTATE_GAMEDEFS_SIGNATURES:
   381 		{
   382 			m_ParseState = PSTATE_GAMEDEFS;
   383 			break;
   384 		}
   385 	case PSTATE_GAMEDEFS_SIGNATURES_SIG:
   386 		{
   387 			if (s_TempSig.library[0] == '\0')
   388 			{
   389 				/* assume server */
   390 				strncopy(s_TempSig.library, "server", sizeof(s_TempSig.library));
   391 			}
   392 			void *addrInBase = NULL;
   393 			if (strcmp(s_TempSig.library, "server") == 0)
   394 			{
   395 				addrInBase = (void *)g_SMAPI->GetServerFactory(false);
   396 			} else if (strcmp(s_TempSig.library, "engine") == 0) {
   397 				addrInBase = (void *)g_SMAPI->GetEngineFactory(false);
   398 			}
   399 			void *final_addr = NULL;
   400 			if (addrInBase == NULL)
   401 			{
   402 				g_Logger.LogError("[SM] Unrecognized library \"%s\" (gameconf \"%s\")", 
   403 					s_TempSig.library, 
   404 					m_pFile);
   405 			} else {
   406 #if defined PLATFORM_LINUX
   407 				if (s_TempSig.sig[0] == '@')
   408 				{
   409 					Dl_info info;
   410 					/* GNU only: returns 0 on error, inconsistent! >:[ */
   411 					if (dladdr(addrInBase, &info) != 0)
   412 					{
   413 						void *handle = dlopen(info.dli_fname, RTLD_NOW);
   414 						if (handle)
   415 						{
   416 							final_addr = dlsym(handle, &s_TempSig.sig[1]);
   417 							dlclose(handle);
   418 						} else {
   419 							g_Logger.LogError("[SM] Unable to load library \"%s\" (gameconf \"%s\")",
   420 								s_TempSig.library,
   421 								m_pFile);
   422 						}
   423 					} else {
   424 						g_Logger.LogError("[SM] Unable to find library \"%s\" in memory (gameconf \"%s\")",
   425 							s_TempSig.library,
   426 							m_pFile);
   427 					}
   428 				}
   429 				if (final_addr)
   430 				{
   431 					goto skip_find;
   432 				}
   433 #endif
   434 				/* First, preprocess the signature */
   435 				char real_sig[511];
   436 				size_t real_bytes;
   437 				size_t length;
   438 
   439 				real_bytes = 0;
   440 				length = strlen(s_TempSig.sig);
   441 
   442 				for (size_t i=0; i<length; i++)
   443 				{
   444 					if (real_bytes >= sizeof(real_sig))
   445 					{
   446 						break;
   447 					}
   448 					real_sig[real_bytes++] = s_TempSig.sig[i];
   449 					if (s_TempSig.sig[i] == '\\'
   450 						&& s_TempSig.sig[i+1] == 'x')
   451 					{
   452 						if (i + 3 >= length)
   453 						{
   454 							continue;
   455 						}
   456 						/* Get the hex part */
   457 						char s_byte[3];
   458 						int r_byte;
   459 						s_byte[0] = s_TempSig.sig[i+2];
   460 						s_byte[1] = s_TempSig.sig[i+3];
   461 						s_byte[2] = '\0';
   462 						/* Read it as an integer */
   463 						sscanf(s_byte, "%x", &r_byte);
   464 						/* Save the value */
   465 						real_sig[real_bytes-1] = r_byte;
   466 						/* Adjust index */
   467 						i += 3;
   468 					}
   469 				}
   470 
   471 				if (real_bytes >= 1)
   472 				{
   473 					final_addr = g_MemUtils.FindPattern(addrInBase, real_sig, real_bytes);
   474 				}
   475 			}
   476 
   477 #if defined PLATFORM_LINUX
   478 skip_find:
   479 #endif
   480 			sm_trie_replace(m_pSigs, m_offset, final_addr);
   481 			m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
   482 
   483 			break;
   484 		}
   485 	}
   486 
   487 	return SMCResult_Continue;
   488 }
   489 
   490 bool CGameConfig::Reparse(char *error, size_t maxlength)
   491 {
   492 	SMCError err;
   493 	SMCStates state = {0, 0};
   494 
   495 	char path[PLATFORM_MAX_PATH];
   496 	g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "gamedata/%s.txt", m_pFile);
   497 	/* Backwards compatibility */
   498 	if (!g_LibSys.PathExists(path))
   499 	{
   500 		g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "configs/gamedata/%s.txt", m_pFile);
   501 	}
   502 
   503 	/* Initialize parse states */
   504 	m_IgnoreLevel = 0;
   505 	bShouldBeReadingDefault = true;
   506 	m_ParseState = PSTATE_NONE;
   507 	/* Reset cached data */
   508 	m_pStrings->Reset();
   509 	sm_trie_clear(m_pOffsets);
   510 	sm_trie_clear(m_pProps);
   511 	sm_trie_clear(m_pKeys);
   512 
   513 	if ((err=textparsers->ParseSMCFile(path, this, &state, error, maxlength))
   514 		!= SMCError_Okay)
   515 	{
   516 		const char *msg;
   517 
   518 		msg = textparsers->GetSMCErrorString(err);
   519 
   520 		g_Logger.LogError("[SM] Error parsing gameconfig file \"%s\":", path);
   521 		g_Logger.LogError("[SM] Error %d on line %d, col %d: %s", 
   522 			err,
   523 			state.line,
   524 			state.col,
   525 			msg ? msg : "Unknown error");
   526 		return false;
   527 	}
   528 
   529 	return true;
   530 }
   531 
   532 bool CGameConfig::GetOffset(const char *key, int *value)
   533 {
   534 	void *obj;
   535 
   536 	if (!sm_trie_retrieve(m_pOffsets, key, &obj))
   537 	{
   538 		return false;
   539 	}
   540 
   541 	*value = (int)obj;
   542 	
   543 	return true;
   544 }
   545 
   546 const char *CGameConfig::GetKeyValue(const char *key)
   547 {
   548 	void *obj;
   549 	if (!sm_trie_retrieve(m_pKeys, key, &obj))
   550 	{
   551 		return NULL;
   552 	}
   553 	return m_pStrings->GetString((int)obj);
   554 }
   555 
   556 SendProp *CGameConfig::GetSendProp(const char *key)
   557 {
   558 	SendProp *pProp;
   559 
   560 	if (!sm_trie_retrieve(m_pProps, key, (void **)&pProp))
   561 	{
   562 		return NULL;
   563 	}
   564 
   565 	return pProp;
   566 }
   567 
   568 bool CGameConfig::GetMemSig(const char *key, void **addr)
   569 {
   570 	return sm_trie_retrieve(m_pSigs, key, addr);
   571 }
   572 
   573 void CGameConfig::IncRefCount()
   574 {
   575 	m_RefCount++;
   576 }
   577 
   578 unsigned int CGameConfig::DecRefCount()
   579 {
   580 	m_RefCount--;
   581 	return m_RefCount;
   582 }
   583 
   584 GameConfigManager::GameConfigManager()
   585 {
   586 	m_pLookup = sm_trie_create();
   587 }
   588 
   589 GameConfigManager::~GameConfigManager()
   590 {
   591 	sm_trie_destroy(m_pLookup);
   592 }
   593 
   594 void GameConfigManager::OnSourceModStartup(bool late)
   595 {
   596 	LoadGameConfigFile("core.games", &g_pGameConf, NULL, 0);
   597 
   598 	strncopy(g_Game, g_SourceMod.GetGameFolderName(), sizeof(g_Game));
   599 	strncopy(g_GameDesc + 1, SERVER_CALL(GetGameDescription)(), sizeof(g_GameDesc) - 1);
   600 
   601 	KeyValues *pGameInfo = new KeyValues("GameInfo");
   602 	if (g_HL2.KVLoadFromFile(pGameInfo, basefilesystem, "gameinfo.txt"))
   603 	{
   604 		const char *str;
   605 		if ((str = pGameInfo->GetString("game", NULL)) != NULL)
   606 		{
   607 			strncopy(g_GameName + 1, str, sizeof(g_GameName) - 1);
   608 		}
   609 	}
   610 	pGameInfo->deleteThis();
   611 }
   612 
   613 void GameConfigManager::OnSourceModAllInitialized()
   614 {
   615 	/* NOW initialize the game file */
   616 	CGameConfig *pGameConf = (CGameConfig *)g_pGameConf;
   617 	
   618 	char error[255];
   619 	if (!pGameConf->Reparse(error, sizeof(error)))
   620 	{
   621 		/* :TODO: log */
   622 	}
   623 
   624 	g_ShareSys.AddInterface(NULL, this);
   625 }
   626 
   627 void GameConfigManager::OnSourceModAllShutdown()
   628 {
   629 	CloseGameConfigFile(g_pGameConf);
   630 }
   631 
   632 bool GameConfigManager::LoadGameConfigFile(const char *file, IGameConfig **_pConfig, char *error, size_t maxlength)
   633 {
   634 	/* A crash was detected during last load - We block the gamedata loading so it hopefully won't happen again */
   635 	if (g_blockGameDataLoad)
   636 	{
   637 		UTIL_Format(error, maxlength, "GameData loaded blocked due to detected crash");
   638 		return false;
   639 	}
   640 
   641 	CGameConfig *pConfig;
   642 
   643 	if (sm_trie_retrieve(m_pLookup, file, (void **)&pConfig))
   644 	{
   645 		pConfig->IncRefCount();
   646 		*_pConfig = pConfig;
   647 		return true;
   648 	}
   649 
   650 	pConfig = new CGameConfig(file);
   651 
   652 	/* :HACKHACK: Don't parse the main config file */
   653 	bool retval = true;
   654 	if (_pConfig != &g_pGameConf)
   655 	{
   656 		retval = pConfig->Reparse(error, maxlength);
   657 	}
   658 
   659 	m_cfgs.push_back(pConfig);
   660 	sm_trie_insert(m_pLookup, file, pConfig);
   661 
   662 	pConfig->IncRefCount();
   663 
   664 	*_pConfig = pConfig;
   665 
   666 	return retval;
   667 }
   668 
   669 void GameConfigManager::CloseGameConfigFile(IGameConfig *cfg)
   670 {
   671 	CGameConfig *pConfig = (CGameConfig *)cfg;
   672 
   673 	if (pConfig->DecRefCount() == 0)
   674 	{
   675 		sm_trie_delete(m_pLookup, pConfig->m_pFile);
   676 		delete pConfig;
   677 	}
   678 }
   679 
   680 extern HandleType_t g_GameConfigsType;
   681 
   682 IGameConfig *GameConfigManager::ReadHandle(Handle_t hndl, IdentityToken_t *ident, HandleError *err)
   683 {
   684 	HandleSecurity sec(ident, g_pCoreIdent);
   685 	IGameConfig *conf = NULL;
   686 	HandleError _err = g_HandleSys.ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&conf);
   687 
   688 	if (err)
   689 	{
   690 		*err = _err;
   691 	}
   692 
   693 	return conf;
   694 }