2 * vim: set ts=4 sw=4 tw=99 noet :
3 * =============================================================================
5 * Copyright (C) 2004-2009 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>.
30 #include "common_logic.h"
34 #include <sh_string.h>
35 #include "GameConfigs.h"
36 #include "stringutil.h"
37 #include <IGameHelpers.h>
38 #include <ILibrarySys.h>
39 #include <IHandleSys.h>
40 #include <IMemoryUtils.h>
41 #include <ISourceMod.h>
42 #include "common_logic.h"
44 #include "MemoryUtils.h"
46 #if defined PLATFORM_LINUX
50 using namespace SourceHook;
52 GameConfigManager g_GameConfigs;
53 IGameConfig *g_pGameConf = NULL;
54 static char g_Game[256];
55 static char g_GameDesc[256] = {'!', '\0'};
56 static char g_GameName[256] = {'$', '\0'};
59 #define PSTATE_GAMES 1
60 #define PSTATE_GAMEDEFS 2
61 #define PSTATE_GAMEDEFS_OFFSETS 3
62 #define PSTATE_GAMEDEFS_OFFSETS_OFFSET 4
63 #define PSTATE_GAMEDEFS_KEYS 5
64 #define PSTATE_GAMEDEFS_SUPPORTED 6
65 #define PSTATE_GAMEDEFS_SIGNATURES 7
66 #define PSTATE_GAMEDEFS_SIGNATURES_SIG 8
67 #define PSTATE_GAMEDEFS_CRC 9
68 #define PSTATE_GAMEDEFS_CRC_BINARY 10
69 #define PSTATE_GAMEDEFS_CUSTOM 11
70 #define PSTATE_GAMEDEFS_ADDRESSES 12
71 #define PSTATE_GAMEDEFS_ADDRESSES_ADDRESS 13
72 #define PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ 14
74 #if defined PLATFORM_WINDOWS
75 #define PLATFORM_NAME "windows"
76 #define PLATFORM_SERVER_BINARY "server.dll"
77 #elif defined PLATFORM_LINUX
78 #define PLATFORM_NAME "linux"
79 #define PLATFORM_SERVER_BINARY "server_i486.so"
80 #elif defined PLATFORM_APPLE
81 #define PLATFORM_NAME "undef"
82 #define PLATFORM_SERVER_BINARY "undef.dylib"
95 unsigned int s_ServerBinCRC;
96 bool s_ServerBinCRC_Ok = false;
98 static bool DoesGameMatch(const char *value)
100 #if defined PLATFORM_WINDOWS
101 if (strcasecmp(value, g_Game) == 0 ||
103 if (strcmp(value, g_Game) == 0 ||
105 strcmp(value, g_GameDesc) == 0 ||
106 strcmp(value, g_GameName) == 0)
113 static bool DoesEngineMatch(const char *value)
115 return strcmp(value, smcore.GetSourceEngineName()) == 0;
118 CGameConfig::CGameConfig(const char *file)
120 strncopy(m_File, file, sizeof(m_File));
121 m_pAddresses = new KTrie<AddressConf>();
122 m_pStrings = new BaseStringTable(512);
126 m_CustomHandler = NULL;
129 CGameConfig::~CGameConfig()
135 SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *name)
140 return SMCResult_Continue;
143 switch (m_ParseState)
147 if (strcmp(name, "Games") == 0)
149 m_ParseState = PSTATE_GAMES;
157 if (strcmp(name, "*") == 0 ||
158 strcmp(name, "#default") == 0 ||
161 bShouldBeReadingDefault = true;
162 m_ParseState = PSTATE_GAMEDEFS;
163 strncopy(m_Game, name, sizeof(m_Game));
169 case PSTATE_GAMEDEFS:
171 if (strcmp(name, "Offsets") == 0)
173 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
175 else if (strcmp(name, "Keys") == 0)
177 m_ParseState = PSTATE_GAMEDEFS_KEYS;
179 else if ((strcmp(name, "#supported") == 0) && (strcmp(m_Game, "#default") == 0))
181 m_ParseState = PSTATE_GAMEDEFS_SUPPORTED;
182 /* Ignore this section unless we get a game. */
183 bShouldBeReadingDefault = false;
185 matched_game = false;
187 matched_engine = false;
189 else if (strcmp(name, "Signatures") == 0)
191 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
193 else if (strcmp(name, "CRC") == 0)
195 m_ParseState = PSTATE_GAMEDEFS_CRC;
196 bShouldBeReadingDefault = false;
198 else if (strcmp(name, "Addresses") == 0)
200 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
204 ITextListener_SMC **pListen = g_GameConfigs.m_customHandlers.retrieve(name);
209 m_ParseState = PSTATE_GAMEDEFS_CUSTOM;
210 m_CustomHandler = *pListen;
211 m_CustomHandler->ReadSMC_ParseStart();
219 case PSTATE_GAMEDEFS_OFFSETS:
223 strncopy(m_offset, name, sizeof(m_offset));
224 m_ParseState = PSTATE_GAMEDEFS_OFFSETS_OFFSET;
227 case PSTATE_GAMEDEFS_SIGNATURES:
229 strncopy(m_offset, name, sizeof(m_offset));
231 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES_SIG;
234 case PSTATE_GAMEDEFS_CRC:
238 if (strcmp(name, "server") != 0)
240 smcore.Format(error, sizeof(error), "Unrecognized library \"%s\"", name);
242 else if (!s_ServerBinCRC_Ok)
245 char path[PLATFORM_MAX_PATH];
247 g_pSM->BuildPath(Path_Game, path, sizeof(path), "bin/" PLATFORM_SERVER_BINARY);
248 if ((fp = fopen(path, "rb")) == NULL)
250 smcore.Format(error, sizeof(error), "Could not open binary: %s", path);
255 fseek(fp, 0, SEEK_END);
257 fseek(fp, 0, SEEK_SET);
259 buffer = malloc(size);
260 fread(buffer, size, 1, fp);
261 s_ServerBinCRC = UTIL_CRC32(buffer, size);
263 s_ServerBinCRC_Ok = true;
267 if (error[0] != '\0')
270 smcore.LogError("[SM] Error while parsing CRC section for \"%s\" (%s):", m_Game, m_CurFile);
271 smcore.LogError("[SM] %s", error);
273 m_ParseState = PSTATE_GAMEDEFS_CRC_BINARY;
277 case PSTATE_GAMEDEFS_CUSTOM:
280 return m_CustomHandler->ReadSMC_NewSection(states, name);
283 case PSTATE_GAMEDEFS_ADDRESSES:
286 m_AddressSignature[0] = '\0';
287 m_AddressReadCount = 0;
289 strncopy(m_Address, name, sizeof(m_Address));
290 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
294 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS:
296 if (strcmp(name, PLATFORM_NAME) == 0)
298 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ;
302 if (strcmp(name, "linux") != 0 && strcmp(name, "windows") != 0)
304 smcore.LogError("[SM] Error while parsing Address section for \"%s\" (%s):", m_Address, m_CurFile);
305 smcore.LogError("[SM] Unrecognized platform \"%s\"", name);
311 /* No sub-sections allowed:
312 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
313 case PSTATE_GAMEDEFS_KEYS:
314 case PSTATE_GAMEDEFS_SUPPORTED:
315 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
316 case PSTATE_GAMEDEFS_CRC_BINARY:
317 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ:
321 /* If we don't know what we got, start ignoring */
327 return SMCResult_Continue;
330 SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
334 return SMCResult_Continue;
337 if (m_ParseState == PSTATE_GAMEDEFS_OFFSETS_OFFSET)
339 if (strcmp(key, "class") == 0)
341 strncopy(m_Class, value, sizeof(m_Class));
342 } else if (strcmp(key, "prop") == 0) {
343 strncopy(m_Prop, value, sizeof(m_Prop));
344 } else if (strcmp(key, PLATFORM_NAME) == 0) {
345 m_Offsets.replace(m_offset, atoi(value));
347 } else if (m_ParseState == PSTATE_GAMEDEFS_KEYS) {
348 m_Keys.replace(key, m_pStrings->AddString(value));
349 } else if (m_ParseState == PSTATE_GAMEDEFS_SUPPORTED) {
350 if (strcmp(key, "game") == 0)
353 if (DoesGameMatch(value))
357 if ((!had_engine && matched_game) || (matched_engine && matched_game))
359 bShouldBeReadingDefault = true;
362 else if (strcmp(key, "engine") == 0)
365 if (DoesEngineMatch(value))
367 matched_engine = true;
369 if ((!had_game && matched_engine) || (matched_game && matched_engine))
371 bShouldBeReadingDefault = true;
374 } else if (m_ParseState == PSTATE_GAMEDEFS_SIGNATURES_SIG) {
375 if (strcmp(key, PLATFORM_NAME) == 0)
377 strncopy(s_TempSig.sig, value, sizeof(s_TempSig.sig));
378 } else if (strcmp(key, "library") == 0) {
379 strncopy(s_TempSig.library, value, sizeof(s_TempSig.library));
381 } else if (m_ParseState == PSTATE_GAMEDEFS_CRC_BINARY) {
382 if (strcmp(key, PLATFORM_NAME) == 0
384 && !bShouldBeReadingDefault)
386 unsigned int crc = 0;
387 sscanf(value, "%08X", &crc);
388 if (s_ServerBinCRC == crc)
390 bShouldBeReadingDefault = true;
393 } else if (m_ParseState == PSTATE_GAMEDEFS_ADDRESSES_ADDRESS || m_ParseState == PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ) {
394 if (strcmp(key, "read") == 0) {
395 int limit = sizeof(m_AddressRead)/sizeof(m_AddressRead[0]);
396 if (m_AddressReadCount < limit)
398 m_AddressRead[m_AddressReadCount] = atoi(value);
399 m_AddressReadCount++;
403 smcore.LogError("[SM] Error parsing Address \"%s\", does not support more than %d read offsets (gameconf \"%s\")", m_Address, limit, m_CurFile);
405 } else if (strcmp(key, "signature") == 0) {
406 strncopy(m_AddressSignature, value, sizeof(m_AddressSignature));
408 } else if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM) {
409 return m_CustomHandler->ReadSMC_KeyValue(states, key, value);
412 return SMCResult_Continue;
415 SMCResult CGameConfig::ReadSMC_LeavingSection(const SMCStates *states)
420 return SMCResult_Continue;
426 m_CustomHandler->ReadSMC_LeavingSection(states);
427 return SMCResult_Continue;
430 switch (m_ParseState)
434 m_ParseState = PSTATE_NONE;
437 case PSTATE_GAMEDEFS:
439 m_ParseState = PSTATE_GAMES;
442 case PSTATE_GAMEDEFS_CUSTOM:
444 m_ParseState = PSTATE_GAMEDEFS;
445 m_CustomHandler->ReadSMC_ParseEnd(false, false);
448 case PSTATE_GAMEDEFS_KEYS:
449 case PSTATE_GAMEDEFS_OFFSETS:
451 m_ParseState = PSTATE_GAMEDEFS;
454 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
456 /* Parse the offset... */
457 if (m_Class[0] != '\0'
458 && m_Prop[0] != '\0')
460 SendProp *pProp = gamehelpers->FindInSendTable(m_Class, m_Prop);
463 int val = gamehelpers->GetSendPropOffset(pProp);
464 m_Offsets.replace(m_offset, val);
465 m_Props.replace(m_offset, pProp);
467 /* Check if it's a non-default game and no offsets exist */
468 if (((strcmp(m_Game, "*") != 0) && strcmp(m_Game, "#default") != 0)
469 && (!m_Offsets.retrieve(m_offset)))
471 smcore.LogError("[SM] Unable to find property %s.%s (file \"%s\") (mod \"%s\")",
479 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
482 case PSTATE_GAMEDEFS_CRC:
483 case PSTATE_GAMEDEFS_SUPPORTED:
485 if (!bShouldBeReadingDefault)
487 /* If we shouldn't read the rest of this section, set the ignore level. */
489 m_ParseState = PSTATE_GAMES;
491 m_ParseState = PSTATE_GAMEDEFS;
495 case PSTATE_GAMEDEFS_CRC_BINARY:
497 m_ParseState = PSTATE_GAMEDEFS_CRC;
500 case PSTATE_GAMEDEFS_SIGNATURES:
502 m_ParseState = PSTATE_GAMEDEFS;
505 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
507 if (s_TempSig.library[0] == '\0')
510 strncopy(s_TempSig.library, "server", sizeof(s_TempSig.library));
512 void *addrInBase = NULL;
513 if (strcmp(s_TempSig.library, "server") == 0)
515 addrInBase = smcore.serverFactory;
516 } else if (strcmp(s_TempSig.library, "engine") == 0) {
517 addrInBase = smcore.engineFactory;
519 void *final_addr = NULL;
520 if (addrInBase == NULL)
522 smcore.LogError("[SM] Unrecognized library \"%s\" (gameconf \"%s\")",
526 #if defined PLATFORM_LINUX
527 if (s_TempSig.sig[0] == '@')
530 /* GNU only: returns 0 on error, inconsistent! >:[ */
531 if (dladdr(addrInBase, &info) != 0)
533 void *handle = dlopen(info.dli_fname, RTLD_NOW);
536 if (smcore.SymbolsAreHidden())
537 final_addr = g_MemUtils.ResolveSymbol(handle, &s_TempSig.sig[1]);
539 final_addr = dlsym(handle, &s_TempSig.sig[1]);
542 smcore.LogError("[SM] Unable to load library \"%s\" (gameconf \"%s\")",
547 smcore.LogError("[SM] Unable to find library \"%s\" in memory (gameconf \"%s\")",
557 /* First, preprocess the signature */
558 unsigned char real_sig[511];
563 length = strlen(s_TempSig.sig);
565 real_bytes = UTIL_DecodeHexString(real_sig, sizeof(real_sig), s_TempSig.sig);
569 final_addr = g_MemUtils.FindPattern(addrInBase, (char*)real_sig, real_bytes);
573 #if defined PLATFORM_LINUX
576 m_Sigs.replace(m_offset, final_addr);
577 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
581 case PSTATE_GAMEDEFS_ADDRESSES:
583 m_ParseState = PSTATE_GAMEDEFS;
586 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS:
588 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
590 if (m_Address[0] == '\0')
592 smcore.LogError("[SM] Address sections must have names (gameconf \"%s\")", m_CurFile);
595 if (m_AddressSignature[0] == '\0')
597 smcore.LogError("[SM] Address section for \"%s\" did not specify a signature (gameconf \"%s\")", m_Address, m_CurFile);
601 AddressConf addrConf(m_AddressSignature, sizeof(m_AddressSignature), m_AddressReadCount, m_AddressRead);
602 m_pAddresses->replace(m_Address, addrConf);
606 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ:
608 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
613 return SMCResult_Continue;
616 #define MSTATE_NONE 0
617 #define MSTATE_MAIN 1
618 #define MSTATE_FILE 2
620 class MasterReader : public ITextListener_SMC
623 virtual void ReadSMC_ParseStart()
629 virtual SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name)
633 return SMCResult_Continue;
636 if (state == MSTATE_NONE)
638 if (strcmp(name, "Game Master") == 0)
647 else if (state == MSTATE_MAIN)
649 strncopy(cur_file, name, sizeof(cur_file));
651 matched_engine = false;
653 matched_game = false;
656 else if (state == MSTATE_FILE)
661 return SMCResult_Continue;
664 virtual SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
666 if (ignoreLevel || state != MSTATE_FILE)
668 return SMCResult_Continue;
671 if (strcmp(key, "engine") == 0)
674 if (DoesEngineMatch(value))
676 matched_engine = true;
679 else if (strcmp(key, "game") == 0)
682 if (DoesGameMatch(value))
688 return SMCResult_Continue;
691 virtual SMCResult ReadSMC_LeavingSection(const SMCStates *states)
696 return SMCResult_Continue;
699 if (state == MSTATE_FILE)
701 /* The four success conditions:
703 * 2. Needed game only.
704 * 3. Needed engine only.
705 * 4. Needed both engine and game.
706 * Final result is minimized via k-map.
709 if ((!had_engine && !had_game) ||
710 (!had_engine && (had_game && matched_game)) ||
711 (!had_game && (had_engine && matched_engine)) ||
712 ((had_game && had_engine) && (matched_game && matched_engine)))
714 if ((!had_engine && !had_game) ||
715 (!had_engine && matched_game) ||
716 (!had_game && matched_engine) ||
717 (matched_engine && matched_game))
719 fileList->push_back(cur_file);
723 else if (state == MSTATE_MAIN)
728 return SMCResult_Continue;
731 List<String> *fileList;
733 unsigned int ignoreLevel;
734 char cur_file[PLATFORM_MAX_PATH];
741 static MasterReader master_reader;
743 bool CGameConfig::Reparse(char *error, size_t maxlength)
745 /* Reset cached data */
750 m_pAddresses->clear();
752 char path[PLATFORM_MAX_PATH];
754 /* See if we can use the extended gamedata format. */
755 g_pSM->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/master.games.txt", m_File);
756 if (!libsys->PathExists(path))
758 /* Nope, use the old mechanism. */
759 smcore.Format(path, sizeof(path), "%s.txt", m_File);
760 return EnterFile(path, error, maxlength);
763 /* Otherwise, it's time to parse the master. */
765 SMCStates state = {0, 0};
766 List<String> fileList;
767 master_reader.fileList = &fileList;
769 err = textparsers->ParseSMCFile(path, &master_reader, &state, error, maxlength);
770 if (err != SMCError_Okay)
772 const char *msg = textparsers->GetSMCErrorString(err);
774 smcore.LogError("[SM] Error parsing master gameconf file \"%s\":", path);
775 smcore.LogError("[SM] Error %d on line %d, col %d: %s",
779 msg ? msg : "Unknown error");
783 /* Go through each file we found and parse it. */
784 List<String>::iterator iter;
785 for (iter = fileList.begin(); iter != fileList.end(); iter++)
787 smcore.Format(path, sizeof(path), "%s/%s", m_File, (*iter).c_str());
788 if (!EnterFile(path, error, maxlength))
794 /* Parse the contents of the 'custom' directory */
795 g_pSM->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/custom", m_File);
796 IDirectory *customDir = libsys->OpenDirectory(path);
803 while (customDir->MoreFiles())
805 if (!customDir->IsEntryFile())
807 customDir->NextEntry();
811 const char *curFile = customDir->GetEntryName();
813 /* Only allow .txt files */
814 int len = strlen(curFile);
815 if (len > 4 && strcmp(&curFile[len-4], ".txt") != 0)
817 customDir->NextEntry();
821 smcore.Format(path, sizeof(path), "%s/custom/%s", m_File, curFile);
822 if (!EnterFile(path, error, maxlength))
824 libsys->CloseDirectory(customDir);
828 customDir->NextEntry();
831 libsys->CloseDirectory(customDir);
835 bool CGameConfig::EnterFile(const char *file, char *error, size_t maxlength)
838 SMCStates state = {0, 0};
840 g_pSM->BuildPath(Path_SM, m_CurFile, sizeof(m_CurFile), "gamedata/%s", file);
842 /* Initialize parse states */
844 bShouldBeReadingDefault = true;
845 m_ParseState = PSTATE_NONE;
847 if ((err=textparsers->ParseSMCFile(m_CurFile, this, &state, error, maxlength))
852 msg = textparsers->GetSMCErrorString(err);
854 smcore.LogError("[SM] Error parsing gameconfig file \"%s\":", m_CurFile);
855 smcore.LogError("[SM] Error %d on line %d, col %d: %s",
859 msg ? msg : "Unknown error");
861 if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM)
863 //error occurred while parsing a custom section
864 m_CustomHandler->ReadSMC_ParseEnd(true, true);
865 m_CustomHandler = NULL;
875 bool CGameConfig::GetOffset(const char *key, int *value)
878 if ((pvalue = m_Offsets.retrieve(key)) == NULL)
885 const char *CGameConfig::GetKeyValue(const char *key)
888 if ((pkey = m_Keys.retrieve(key)) == NULL)
890 return m_pStrings->GetString(*pkey);
893 //memory addresses below 0x10000 are automatically considered invalid for dereferencing
894 #define VALID_MINIMUM_MEMORY_ADDRESS 0x10000
896 bool CGameConfig::GetAddress(const char *key, void **retaddr)
898 AddressConf *addrConf;
900 addrConf = m_pAddresses->retrieve(key);
908 if (!GetMemSig(addrConf->signatureName, &addr))
914 for (int i = 0; i < addrConf->readCount; ++i)
916 int offset = addrConf->read[i];
918 //NULLs in the middle of an indirection chain are bad, end NULL is ok
919 if (addr == NULL || reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
924 addr = *(reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(addr) + offset));
931 static inline unsigned min(unsigned a, unsigned b)
933 return a <= b ? a : b;
936 CGameConfig::AddressConf::AddressConf(char *sigName, unsigned sigLength, unsigned readCount, int *read)
938 unsigned readLimit = min(readCount, sizeof(this->read) / sizeof(this->read[0]));
940 strncopy(signatureName, sigName, sizeof(signatureName) / sizeof(signatureName[0]));
941 this->readCount = readLimit;
942 memcpy(&this->read[0], read, sizeof(this->read[0])*readLimit);
945 SendProp *CGameConfig::GetSendProp(const char *key)
949 if ((pProp = m_Props.retrieve(key)) == NULL)
955 bool CGameConfig::GetMemSig(const char *key, void **addr)
958 if ((paddr = m_Sigs.retrieve(key)) == NULL)
964 void CGameConfig::IncRefCount()
969 unsigned int CGameConfig::DecRefCount()
975 GameConfigManager::GameConfigManager()
979 GameConfigManager::~GameConfigManager()
983 void GameConfigManager::OnSourceModStartup(bool late)
985 LoadGameConfigFile("core.games", &g_pGameConf, NULL, 0);
987 strncopy(g_Game, g_pSM->GetGameFolderName(), sizeof(g_Game));
988 strncopy(g_GameDesc + 1, smcore.GetGameDescription(), sizeof(g_GameDesc) - 1);
989 smcore.GetGameName(g_GameName + 1, sizeof(g_GameName) - 1);
992 void GameConfigManager::OnSourceModAllInitialized()
994 /* NOW initialize the game file */
995 CGameConfig *pGameConf = (CGameConfig *)g_pGameConf;
998 if (!pGameConf->Reparse(error, sizeof(error)))
1003 sharesys->AddInterface(NULL, this);
1006 void GameConfigManager::OnSourceModAllShutdown()
1008 CloseGameConfigFile(g_pGameConf);
1011 bool GameConfigManager::LoadGameConfigFile(const char *file, IGameConfig **_pConfig, char *error, size_t maxlength)
1014 /* A crash was detected during last load - We block the gamedata loading so it hopefully won't happen again */
1015 if (g_blockGameDataLoad)
1017 UTIL_Format(error, maxlength, "GameData loaded blocked due to detected crash");
1022 CGameConfig *pConfig;
1023 CGameConfig **ppConfig;
1025 if ((ppConfig = m_Lookup.retrieve(file)) != NULL)
1027 pConfig = *ppConfig;
1028 pConfig->IncRefCount();
1029 *_pConfig = pConfig;
1033 pConfig = new CGameConfig(file);
1035 /* :HACKHACK: Don't parse the main config file */
1037 if (_pConfig != &g_pGameConf)
1039 retval = pConfig->Reparse(error, maxlength);
1042 m_cfgs.push_back(pConfig);
1043 m_Lookup.insert(file, pConfig);
1045 pConfig->IncRefCount();
1047 *_pConfig = pConfig;
1052 void GameConfigManager::CloseGameConfigFile(IGameConfig *cfg)
1054 CGameConfig *pConfig = (CGameConfig *)cfg;
1056 if (pConfig->DecRefCount() == 0)
1058 m_Lookup.remove(pConfig->m_File);
1063 extern Handle_t g_GameConfigsType;
1065 IGameConfig *GameConfigManager::ReadHandle(Handle_t hndl, IdentityToken_t *ident, HandleError *err)
1067 HandleSecurity sec(ident, g_pCoreIdent);
1068 IGameConfig *conf = NULL;
1069 HandleError _err = handlesys->ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&conf);
1079 void GameConfigManager::AddUserConfigHook(const char *sectionname, ITextListener_SMC *listener)
1081 m_customHandlers.insert(sectionname, listener);
1084 void GameConfigManager::RemoveUserConfigHook(const char *sectionname, ITextListener_SMC *listener)
1086 ITextListener_SMC **pListener = m_customHandlers.retrieve(sectionname);
1088 if (pListener == NULL)
1093 if (*pListener != listener)
1098 m_customHandlers.remove(sectionname);
1103 void GameConfigManager::AcquireLock()
1107 void GameConfigManager::ReleaseLock()