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