Updated some CS:GO gamedata.
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_POSIX
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'};
57 static const char *g_pParseEngine = NULL;
60 #define PSTATE_GAMES 1
61 #define PSTATE_GAMEDEFS 2
62 #define PSTATE_GAMEDEFS_OFFSETS 3
63 #define PSTATE_GAMEDEFS_OFFSETS_OFFSET 4
64 #define PSTATE_GAMEDEFS_KEYS 5
65 #define PSTATE_GAMEDEFS_SUPPORTED 6
66 #define PSTATE_GAMEDEFS_SIGNATURES 7
67 #define PSTATE_GAMEDEFS_SIGNATURES_SIG 8
68 #define PSTATE_GAMEDEFS_CRC 9
69 #define PSTATE_GAMEDEFS_CRC_BINARY 10
70 #define PSTATE_GAMEDEFS_CUSTOM 11
71 #define PSTATE_GAMEDEFS_ADDRESSES 12
72 #define PSTATE_GAMEDEFS_ADDRESSES_ADDRESS 13
73 #define PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ 14
75 #if defined PLATFORM_WINDOWS
76 #define PLATFORM_NAME "windows"
77 #define PLATFORM_SERVER_BINARY "server.dll"
78 #elif defined PLATFORM_LINUX
79 #define PLATFORM_NAME "linux"
80 #define PLATFORM_SERVER_BINARY "server_i486.so"
81 #elif defined PLATFORM_APPLE
82 #define PLATFORM_NAME "mac"
83 #define PLATFORM_SERVER_BINARY "server.dylib"
96 unsigned int s_ServerBinCRC;
97 bool s_ServerBinCRC_Ok = false;
99 static bool DoesGameMatch(const char *value)
101 #if defined PLATFORM_WINDOWS
102 if (strcasecmp(value, g_Game) == 0 ||
104 if (strcmp(value, g_Game) == 0 ||
106 strcmp(value, g_GameDesc) == 0 ||
107 strcmp(value, g_GameName) == 0)
114 static bool DoesEngineMatch(const char *value)
116 return strcmp(value, g_pParseEngine) == 0;
119 CGameConfig::CGameConfig(const char *file, const char *engine)
121 strncopy(m_File, file, sizeof(m_File));
122 m_pAddresses = new KTrie<AddressConf>();
123 m_pStrings = new BaseStringTable(512);
127 m_CustomHandler = NULL;
130 m_pEngine = smcore.GetSourceEngineName();
134 if (strcmp(m_pEngine, "css") == 0 || strcmp(m_pEngine, "dods") == 0 || strcmp(m_pEngine, "hl2dm") == 0 || strcmp(m_pEngine, "tf2") == 0)
135 this->SetBaseEngine("orangebox_valve");
136 else if (strcmp(m_pEngine, "nucleardawn") == 0)
137 this->SetBaseEngine("left4dead2");
139 this->SetBaseEngine(NULL);
142 CGameConfig::~CGameConfig()
148 SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *name)
153 return SMCResult_Continue;
156 switch (m_ParseState)
160 if (strcmp(name, "Games") == 0)
162 m_ParseState = PSTATE_GAMES;
170 if (strcmp(name, "*") == 0 ||
171 strcmp(name, "#default") == 0 ||
174 bShouldBeReadingDefault = true;
175 m_ParseState = PSTATE_GAMEDEFS;
176 strncopy(m_Game, name, sizeof(m_Game));
182 case PSTATE_GAMEDEFS:
184 if (strcmp(name, "Offsets") == 0)
186 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
188 else if (strcmp(name, "Keys") == 0)
190 m_ParseState = PSTATE_GAMEDEFS_KEYS;
192 else if ((strcmp(name, "#supported") == 0) && (strcmp(m_Game, "#default") == 0))
194 m_ParseState = PSTATE_GAMEDEFS_SUPPORTED;
195 /* Ignore this section unless we get a game. */
196 bShouldBeReadingDefault = false;
198 matched_game = false;
200 matched_engine = false;
202 else if (strcmp(name, "Signatures") == 0)
204 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
206 else if (strcmp(name, "CRC") == 0)
208 m_ParseState = PSTATE_GAMEDEFS_CRC;
209 bShouldBeReadingDefault = false;
211 else if (strcmp(name, "Addresses") == 0)
213 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
217 ITextListener_SMC **pListen = g_GameConfigs.m_customHandlers.retrieve(name);
222 m_ParseState = PSTATE_GAMEDEFS_CUSTOM;
223 m_CustomHandler = *pListen;
224 m_CustomHandler->ReadSMC_ParseStart();
232 case PSTATE_GAMEDEFS_OFFSETS:
236 strncopy(m_offset, name, sizeof(m_offset));
237 m_ParseState = PSTATE_GAMEDEFS_OFFSETS_OFFSET;
240 case PSTATE_GAMEDEFS_SIGNATURES:
242 strncopy(m_offset, name, sizeof(m_offset));
244 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES_SIG;
247 case PSTATE_GAMEDEFS_CRC:
251 if (strcmp(name, "server") != 0)
253 smcore.Format(error, sizeof(error), "Unrecognized library \"%s\"", name);
255 else if (!s_ServerBinCRC_Ok)
258 char path[PLATFORM_MAX_PATH];
260 g_pSM->BuildPath(Path_Game, path, sizeof(path), "bin/" PLATFORM_SERVER_BINARY);
261 if ((fp = fopen(path, "rb")) == NULL)
263 smcore.Format(error, sizeof(error), "Could not open binary: %s", path);
268 fseek(fp, 0, SEEK_END);
270 fseek(fp, 0, SEEK_SET);
272 buffer = malloc(size);
273 fread(buffer, size, 1, fp);
274 s_ServerBinCRC = UTIL_CRC32(buffer, size);
276 s_ServerBinCRC_Ok = true;
280 if (error[0] != '\0')
283 smcore.LogError("[SM] Error while parsing CRC section for \"%s\" (%s):", m_Game, m_CurFile);
284 smcore.LogError("[SM] %s", error);
286 m_ParseState = PSTATE_GAMEDEFS_CRC_BINARY;
290 case PSTATE_GAMEDEFS_CUSTOM:
293 return m_CustomHandler->ReadSMC_NewSection(states, name);
296 case PSTATE_GAMEDEFS_ADDRESSES:
299 m_AddressSignature[0] = '\0';
300 m_AddressReadCount = 0;
302 strncopy(m_Address, name, sizeof(m_Address));
303 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
307 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS:
309 if (strcmp(name, PLATFORM_NAME) == 0)
311 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ;
315 if (strcmp(name, "linux") != 0 && strcmp(name, "windows") != 0 && strcmp(name, "mac") != 0)
317 smcore.LogError("[SM] Error while parsing Address section for \"%s\" (%s):", m_Address, m_CurFile);
318 smcore.LogError("[SM] Unrecognized platform \"%s\"", name);
324 /* No sub-sections allowed:
325 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
326 case PSTATE_GAMEDEFS_KEYS:
327 case PSTATE_GAMEDEFS_SUPPORTED:
328 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
329 case PSTATE_GAMEDEFS_CRC_BINARY:
330 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ:
334 /* If we don't know what we got, start ignoring */
340 return SMCResult_Continue;
343 SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
347 return SMCResult_Continue;
350 if (m_ParseState == PSTATE_GAMEDEFS_OFFSETS_OFFSET)
352 if (strcmp(key, "class") == 0)
354 strncopy(m_Class, value, sizeof(m_Class));
355 } else if (strcmp(key, "prop") == 0) {
356 strncopy(m_Prop, value, sizeof(m_Prop));
357 } else if (strcmp(key, PLATFORM_NAME) == 0) {
358 m_Offsets.replace(m_offset, atoi(value));
360 } else if (m_ParseState == PSTATE_GAMEDEFS_KEYS) {
361 m_Keys.replace(key, m_pStrings->AddString(value));
362 } else if (m_ParseState == PSTATE_GAMEDEFS_SUPPORTED) {
363 if (strcmp(key, "game") == 0)
366 if (DoesGameMatch(value))
370 if ((!had_engine && matched_game) || (matched_engine && matched_game))
372 bShouldBeReadingDefault = true;
375 else if (strcmp(key, "engine") == 0)
378 if (DoesEngineMatch(value))
380 matched_engine = true;
382 if ((!had_game && matched_engine) || (matched_game && matched_engine))
384 bShouldBeReadingDefault = true;
387 } else if (m_ParseState == PSTATE_GAMEDEFS_SIGNATURES_SIG) {
388 if (strcmp(key, PLATFORM_NAME) == 0)
390 strncopy(s_TempSig.sig, value, sizeof(s_TempSig.sig));
391 } else if (strcmp(key, "library") == 0) {
392 strncopy(s_TempSig.library, value, sizeof(s_TempSig.library));
394 } else if (m_ParseState == PSTATE_GAMEDEFS_CRC_BINARY) {
395 if (strcmp(key, PLATFORM_NAME) == 0
397 && !bShouldBeReadingDefault)
399 unsigned int crc = 0;
400 sscanf(value, "%08X", &crc);
401 if (s_ServerBinCRC == crc)
403 bShouldBeReadingDefault = true;
406 } else if (m_ParseState == PSTATE_GAMEDEFS_ADDRESSES_ADDRESS || m_ParseState == PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ) {
407 if (strcmp(key, "read") == 0) {
408 int limit = sizeof(m_AddressRead)/sizeof(m_AddressRead[0]);
409 if (m_AddressReadCount < limit)
411 m_AddressRead[m_AddressReadCount] = atoi(value);
412 m_AddressReadCount++;
416 smcore.LogError("[SM] Error parsing Address \"%s\", does not support more than %d read offsets (gameconf \"%s\")", m_Address, limit, m_CurFile);
418 } else if (strcmp(key, "signature") == 0) {
419 strncopy(m_AddressSignature, value, sizeof(m_AddressSignature));
421 } else if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM) {
422 return m_CustomHandler->ReadSMC_KeyValue(states, key, value);
425 return SMCResult_Continue;
428 SMCResult CGameConfig::ReadSMC_LeavingSection(const SMCStates *states)
433 return SMCResult_Continue;
439 m_CustomHandler->ReadSMC_LeavingSection(states);
440 return SMCResult_Continue;
443 switch (m_ParseState)
447 m_ParseState = PSTATE_NONE;
450 case PSTATE_GAMEDEFS:
452 m_ParseState = PSTATE_GAMES;
455 case PSTATE_GAMEDEFS_CUSTOM:
457 m_ParseState = PSTATE_GAMEDEFS;
458 m_CustomHandler->ReadSMC_ParseEnd(false, false);
461 case PSTATE_GAMEDEFS_KEYS:
462 case PSTATE_GAMEDEFS_OFFSETS:
464 m_ParseState = PSTATE_GAMEDEFS;
467 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
469 /* Parse the offset... */
470 if (m_Class[0] != '\0'
471 && m_Prop[0] != '\0')
473 SendProp *pProp = gamehelpers->FindInSendTable(m_Class, m_Prop);
476 int val = gamehelpers->GetSendPropOffset(pProp);
477 m_Offsets.replace(m_offset, val);
478 m_Props.replace(m_offset, pProp);
480 /* Check if it's a non-default game and no offsets exist */
481 if (((strcmp(m_Game, "*") != 0) && strcmp(m_Game, "#default") != 0)
482 && (!m_Offsets.retrieve(m_offset)))
484 smcore.LogError("[SM] Unable to find property %s.%s (file \"%s\") (mod \"%s\")",
492 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
495 case PSTATE_GAMEDEFS_CRC:
496 case PSTATE_GAMEDEFS_SUPPORTED:
498 if (!bShouldBeReadingDefault)
500 /* If we shouldn't read the rest of this section, set the ignore level. */
502 m_ParseState = PSTATE_GAMES;
504 m_ParseState = PSTATE_GAMEDEFS;
508 case PSTATE_GAMEDEFS_CRC_BINARY:
510 m_ParseState = PSTATE_GAMEDEFS_CRC;
513 case PSTATE_GAMEDEFS_SIGNATURES:
515 m_ParseState = PSTATE_GAMEDEFS;
518 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
520 if (s_TempSig.library[0] == '\0')
523 strncopy(s_TempSig.library, "server", sizeof(s_TempSig.library));
525 void *addrInBase = NULL;
526 if (strcmp(s_TempSig.library, "server") == 0)
528 addrInBase = smcore.serverFactory;
529 } else if (strcmp(s_TempSig.library, "engine") == 0) {
530 addrInBase = smcore.engineFactory;
531 } else if (strcmp(s_TempSig.library, "matchmaking_ds") == 0) {
532 addrInBase = smcore.matchmakingDSFactory;
534 void *final_addr = NULL;
535 if (addrInBase == NULL)
537 smcore.LogError("[SM] Unrecognized library \"%s\" (gameconf \"%s\")",
541 if (s_TempSig.sig[0] == '@')
543 #if defined PLATFORM_WINDOWS
544 MEMORY_BASIC_INFORMATION mem;
546 if (VirtualQuery(addrInBase, &mem, sizeof(mem)))
547 final_addr = g_MemUtils.ResolveSymbol(mem.AllocationBase, &s_TempSig.sig[1]);
549 smcore.LogError("[SM] Unable to find library \"%s\" in memory (gameconf \"%s\")", s_TempSig.library, m_File);
550 #elif defined PLATFORM_POSIX
552 /* GNU only: returns 0 on error, inconsistent! >:[ */
553 if (dladdr(addrInBase, &info) != 0)
555 void *handle = dlopen(info.dli_fname, RTLD_NOW);
558 if (smcore.SymbolsAreHidden())
559 final_addr = g_MemUtils.ResolveSymbol(handle, &s_TempSig.sig[1]);
561 final_addr = dlsym(handle, &s_TempSig.sig[1]);
564 smcore.LogError("[SM] Unable to load library \"%s\" (gameconf \"%s\")",
569 smcore.LogError("[SM] Unable to find library \"%s\" in memory (gameconf \"%s\")",
579 /* First, preprocess the signature */
580 unsigned char real_sig[511];
585 length = strlen(s_TempSig.sig);
587 real_bytes = UTIL_DecodeHexString(real_sig, sizeof(real_sig), s_TempSig.sig);
591 final_addr = g_MemUtils.FindPattern(addrInBase, (char*)real_sig, real_bytes);
596 m_Sigs.replace(m_offset, final_addr);
597 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
601 case PSTATE_GAMEDEFS_ADDRESSES:
603 m_ParseState = PSTATE_GAMEDEFS;
606 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS:
608 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
610 if (m_Address[0] == '\0')
612 smcore.LogError("[SM] Address sections must have names (gameconf \"%s\")", m_CurFile);
615 if (m_AddressSignature[0] == '\0')
617 smcore.LogError("[SM] Address section for \"%s\" did not specify a signature (gameconf \"%s\")", m_Address, m_CurFile);
621 AddressConf addrConf(m_AddressSignature, sizeof(m_AddressSignature), m_AddressReadCount, m_AddressRead);
622 m_pAddresses->replace(m_Address, addrConf);
626 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ:
628 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
633 return SMCResult_Continue;
636 #define MSTATE_NONE 0
637 #define MSTATE_MAIN 1
638 #define MSTATE_FILE 2
640 class MasterReader : public ITextListener_SMC
643 virtual void ReadSMC_ParseStart()
649 virtual SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name)
653 return SMCResult_Continue;
656 if (state == MSTATE_NONE)
658 if (strcmp(name, "Game Master") == 0)
667 else if (state == MSTATE_MAIN)
669 strncopy(cur_file, name, sizeof(cur_file));
671 matched_engine = false;
673 matched_game = false;
676 else if (state == MSTATE_FILE)
681 return SMCResult_Continue;
684 virtual SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
686 if (ignoreLevel || state != MSTATE_FILE)
688 return SMCResult_Continue;
691 if (strcmp(key, "engine") == 0)
694 if (DoesEngineMatch(value))
696 matched_engine = true;
699 else if (strcmp(key, "game") == 0)
702 if (DoesGameMatch(value))
708 return SMCResult_Continue;
711 virtual SMCResult ReadSMC_LeavingSection(const SMCStates *states)
716 return SMCResult_Continue;
719 if (state == MSTATE_FILE)
721 /* The four success conditions:
723 * 2. Needed game only.
724 * 3. Needed engine only.
725 * 4. Needed both engine and game.
726 * Final result is minimized via k-map.
729 if ((!had_engine && !had_game) ||
730 (!had_engine && (had_game && matched_game)) ||
731 (!had_game && (had_engine && matched_engine)) ||
732 ((had_game && had_engine) && (matched_game && matched_engine)))
734 if ((!had_engine && !had_game) ||
735 (!had_engine && matched_game) ||
736 (!had_game && matched_engine) ||
737 (matched_engine && matched_game))
739 fileList->push_back(cur_file);
743 else if (state == MSTATE_MAIN)
748 return SMCResult_Continue;
751 List<String> *fileList;
753 unsigned int ignoreLevel;
754 char cur_file[PLATFORM_MAX_PATH];
761 static MasterReader master_reader;
763 bool CGameConfig::Reparse(char *error, size_t maxlength)
765 /* Reset cached data */
770 m_pAddresses->clear();
772 char path[PLATFORM_MAX_PATH];
774 /* See if we can use the extended gamedata format. */
775 g_pSM->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/master.games.txt", m_File);
776 if (!libsys->PathExists(path))
778 /* Nope, use the old mechanism. */
779 smcore.Format(path, sizeof(path), "%s.txt", m_File);
780 return EnterFile(path, error, maxlength);
783 /* Otherwise, it's time to parse the master. */
785 SMCStates state = {0, 0};
786 List<String> fileList;
787 master_reader.fileList = &fileList;
788 const char *pEngine[2] = { m_pBaseEngine, m_pEngine };
790 for (unsigned char iter = 0; iter < SM_ARRAYSIZE(pEngine); ++iter)
792 if (pEngine[iter] == NULL)
797 this->SetParseEngine(pEngine[iter]);
798 err = textparsers->ParseSMCFile(path, &master_reader, &state, error, maxlength);
799 if (err != SMCError_Okay)
801 const char *msg = textparsers->GetSMCErrorString(err);
803 smcore.LogError("[SM] Error parsing master gameconf file \"%s\":", path);
804 smcore.LogError("[SM] Error %d on line %d, col %d: %s",
808 msg ? msg : "Unknown error");
813 /* Go through each file we found and parse it. */
814 List<String>::iterator iter;
815 for (iter = fileList.begin(); iter != fileList.end(); iter++)
817 smcore.Format(path, sizeof(path), "%s/%s", m_File, (*iter).c_str());
818 if (!EnterFile(path, error, maxlength))
824 /* Parse the contents of the 'custom' directory */
825 g_pSM->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/custom", m_File);
826 IDirectory *customDir = libsys->OpenDirectory(path);
833 while (customDir->MoreFiles())
835 if (!customDir->IsEntryFile())
837 customDir->NextEntry();
841 const char *curFile = customDir->GetEntryName();
843 /* Only allow .txt files */
844 int len = strlen(curFile);
845 if (len > 4 && strcmp(&curFile[len-4], ".txt") != 0)
847 customDir->NextEntry();
851 smcore.Format(path, sizeof(path), "%s/custom/%s", m_File, curFile);
852 if (!EnterFile(path, error, maxlength))
854 libsys->CloseDirectory(customDir);
858 customDir->NextEntry();
861 libsys->CloseDirectory(customDir);
865 bool CGameConfig::EnterFile(const char *file, char *error, size_t maxlength)
868 SMCStates state = {0, 0};
870 g_pSM->BuildPath(Path_SM, m_CurFile, sizeof(m_CurFile), "gamedata/%s", file);
872 /* Initialize parse states */
874 bShouldBeReadingDefault = true;
875 m_ParseState = PSTATE_NONE;
876 const char *pEngine[2] = { m_pBaseEngine, m_pEngine };
878 for (unsigned char iter = 0; iter < SM_ARRAYSIZE(pEngine); ++iter)
880 if (pEngine[iter] == NULL)
885 this->SetParseEngine(pEngine[iter]);
886 if ((err=textparsers->ParseSMCFile(m_CurFile, this, &state, error, maxlength))
889 const char *msg = textparsers->GetSMCErrorString(err);
891 smcore.LogError("[SM] Error parsing gameconfig file \"%s\":", m_CurFile);
892 smcore.LogError("[SM] Error %d on line %d, col %d: %s",
896 msg ? msg : "Unknown error");
898 if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM)
900 //error occurred while parsing a custom section
901 m_CustomHandler->ReadSMC_ParseEnd(true, true);
902 m_CustomHandler = NULL;
913 void CGameConfig::SetBaseEngine(const char *engine)
915 m_pBaseEngine = engine;
918 void CGameConfig::SetParseEngine(const char *engine)
920 g_pParseEngine = engine;
923 bool CGameConfig::GetOffset(const char *key, int *value)
926 if ((pvalue = m_Offsets.retrieve(key)) == NULL)
933 const char *CGameConfig::GetKeyValue(const char *key)
936 if ((pkey = m_Keys.retrieve(key)) == NULL)
938 return m_pStrings->GetString(*pkey);
941 //memory addresses below 0x10000 are automatically considered invalid for dereferencing
942 #define VALID_MINIMUM_MEMORY_ADDRESS 0x10000
944 bool CGameConfig::GetAddress(const char *key, void **retaddr)
946 AddressConf *addrConf;
948 addrConf = m_pAddresses->retrieve(key);
956 if (!GetMemSig(addrConf->signatureName, &addr))
962 for (int i = 0; i < addrConf->readCount; ++i)
964 int offset = addrConf->read[i];
966 //NULLs in the middle of an indirection chain are bad, end NULL is ok
967 if (addr == NULL || reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
972 addr = *(reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(addr) + offset));
979 static inline unsigned minOf(unsigned a, unsigned b)
981 return a <= b ? a : b;
984 CGameConfig::AddressConf::AddressConf(char *sigName, unsigned sigLength, unsigned readCount, int *read)
986 unsigned readLimit = minOf(readCount, sizeof(this->read) / sizeof(this->read[0]));
988 strncopy(signatureName, sigName, sizeof(signatureName) / sizeof(signatureName[0]));
989 this->readCount = readLimit;
990 memcpy(&this->read[0], read, sizeof(this->read[0])*readLimit);
993 SendProp *CGameConfig::GetSendProp(const char *key)
997 if ((pProp = m_Props.retrieve(key)) == NULL)
1003 bool CGameConfig::GetMemSig(const char *key, void **addr)
1006 if ((paddr = m_Sigs.retrieve(key)) == NULL)
1012 void CGameConfig::IncRefCount()
1017 unsigned int CGameConfig::DecRefCount()
1023 GameConfigManager::GameConfigManager()
1027 GameConfigManager::~GameConfigManager()
1031 void GameConfigManager::OnSourceModStartup(bool late)
1033 LoadGameConfigFile("core.games", &g_pGameConf, NULL, 0);
1035 strncopy(g_Game, g_pSM->GetGameFolderName(), sizeof(g_Game));
1036 strncopy(g_GameDesc + 1, smcore.GetGameDescription(), sizeof(g_GameDesc) - 1);
1037 smcore.GetGameName(g_GameName + 1, sizeof(g_GameName) - 1);
1040 void GameConfigManager::OnSourceModAllInitialized()
1042 /* NOW initialize the game file */
1043 CGameConfig *pGameConf = (CGameConfig *)g_pGameConf;
1046 if (!pGameConf->Reparse(error, sizeof(error)))
1051 sharesys->AddInterface(NULL, this);
1054 void GameConfigManager::OnSourceModAllShutdown()
1056 CloseGameConfigFile(g_pGameConf);
1059 bool GameConfigManager::LoadGameConfigFile(const char *file, IGameConfig **_pConfig, char *error, size_t maxlength)
1062 /* A crash was detected during last load - We block the gamedata loading so it hopefully won't happen again */
1063 if (g_blockGameDataLoad)
1065 UTIL_Format(error, maxlength, "GameData loaded blocked due to detected crash");
1070 CGameConfig *pConfig;
1071 CGameConfig **ppConfig;
1073 if ((ppConfig = m_Lookup.retrieve(file)) != NULL)
1075 pConfig = *ppConfig;
1076 pConfig->IncRefCount();
1077 *_pConfig = pConfig;
1081 pConfig = new CGameConfig(file);
1083 /* :HACKHACK: Don't parse the main config file */
1085 if (_pConfig != &g_pGameConf)
1087 retval = pConfig->Reparse(error, maxlength);
1090 m_cfgs.push_back(pConfig);
1091 m_Lookup.insert(file, pConfig);
1093 pConfig->IncRefCount();
1095 *_pConfig = pConfig;
1100 void GameConfigManager::CloseGameConfigFile(IGameConfig *cfg)
1102 CGameConfig *pConfig = (CGameConfig *)cfg;
1104 if (pConfig->DecRefCount() == 0)
1106 m_Lookup.remove(pConfig->m_File);
1111 extern Handle_t g_GameConfigsType;
1113 IGameConfig *GameConfigManager::ReadHandle(Handle_t hndl, IdentityToken_t *ident, HandleError *err)
1115 HandleSecurity sec(ident, g_pCoreIdent);
1116 IGameConfig *conf = NULL;
1117 HandleError _err = handlesys->ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&conf);
1127 void GameConfigManager::AddUserConfigHook(const char *sectionname, ITextListener_SMC *listener)
1129 m_customHandlers.insert(sectionname, listener);
1132 void GameConfigManager::RemoveUserConfigHook(const char *sectionname, ITextListener_SMC *listener)
1134 ITextListener_SMC **pListener = m_customHandlers.retrieve(sectionname);
1136 if (pListener == NULL)
1141 if (*pListener != listener)
1146 m_customHandlers.remove(sectionname);
1151 void GameConfigManager::AcquireLock()
1155 void GameConfigManager::ReleaseLock()