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_pStrings = new BaseStringTable(512);
125 m_CustomHandler = NULL;
128 m_pEngine = smcore.GetSourceEngineName();
132 if (strcmp(m_pEngine, "css") == 0 || strcmp(m_pEngine, "dods") == 0 || strcmp(m_pEngine, "hl2dm") == 0 || strcmp(m_pEngine, "tf2") == 0)
133 this->SetBaseEngine("orangebox_valve");
134 else if (strcmp(m_pEngine, "nucleardawn") == 0)
135 this->SetBaseEngine("left4dead2");
137 this->SetBaseEngine(NULL);
140 CGameConfig::~CGameConfig()
142 g_GameConfigs.RemoveCachedConfig(this);
145 SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *name)
150 return SMCResult_Continue;
153 switch (m_ParseState)
157 if (strcmp(name, "Games") == 0)
159 m_ParseState = PSTATE_GAMES;
167 if (strcmp(name, "*") == 0 ||
168 strcmp(name, "#default") == 0 ||
171 bShouldBeReadingDefault = true;
172 m_ParseState = PSTATE_GAMEDEFS;
173 strncopy(m_Game, name, sizeof(m_Game));
179 case PSTATE_GAMEDEFS:
181 if (strcmp(name, "Offsets") == 0)
183 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
185 else if (strcmp(name, "Keys") == 0)
187 m_ParseState = PSTATE_GAMEDEFS_KEYS;
189 else if ((strcmp(name, "#supported") == 0) && (strcmp(m_Game, "#default") == 0))
191 m_ParseState = PSTATE_GAMEDEFS_SUPPORTED;
192 /* Ignore this section unless we get a game. */
193 bShouldBeReadingDefault = false;
195 matched_game = false;
197 matched_engine = false;
199 else if (strcmp(name, "Signatures") == 0)
201 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
203 else if (strcmp(name, "CRC") == 0)
205 m_ParseState = PSTATE_GAMEDEFS_CRC;
206 bShouldBeReadingDefault = false;
208 else if (strcmp(name, "Addresses") == 0)
210 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
214 if (g_GameConfigs.m_customHandlers.retrieve(name, &m_CustomHandler))
217 m_ParseState = PSTATE_GAMEDEFS_CUSTOM;
218 m_CustomHandler->ReadSMC_ParseStart();
226 case PSTATE_GAMEDEFS_OFFSETS:
230 strncopy(m_offset, name, sizeof(m_offset));
231 m_ParseState = PSTATE_GAMEDEFS_OFFSETS_OFFSET;
234 case PSTATE_GAMEDEFS_SIGNATURES:
236 strncopy(m_offset, name, sizeof(m_offset));
238 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES_SIG;
241 case PSTATE_GAMEDEFS_CRC:
245 if (strcmp(name, "server") != 0)
247 smcore.Format(error, sizeof(error), "Unrecognized library \"%s\"", name);
249 else if (!s_ServerBinCRC_Ok)
252 char path[PLATFORM_MAX_PATH];
254 g_pSM->BuildPath(Path_Game, path, sizeof(path), "bin/" PLATFORM_SERVER_BINARY);
255 if ((fp = fopen(path, "rb")) == NULL)
257 smcore.Format(error, sizeof(error), "Could not open binary: %s", path);
262 fseek(fp, 0, SEEK_END);
264 fseek(fp, 0, SEEK_SET);
266 buffer = malloc(size);
267 fread(buffer, size, 1, fp);
268 s_ServerBinCRC = UTIL_CRC32(buffer, size);
270 s_ServerBinCRC_Ok = true;
274 if (error[0] != '\0')
277 smcore.LogError("[SM] Error while parsing CRC section for \"%s\" (%s):", m_Game, m_CurFile);
278 smcore.LogError("[SM] %s", error);
280 m_ParseState = PSTATE_GAMEDEFS_CRC_BINARY;
284 case PSTATE_GAMEDEFS_CUSTOM:
287 return m_CustomHandler->ReadSMC_NewSection(states, name);
290 case PSTATE_GAMEDEFS_ADDRESSES:
293 m_AddressSignature[0] = '\0';
294 m_AddressReadCount = 0;
296 strncopy(m_Address, name, sizeof(m_Address));
297 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
301 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS:
303 if (strcmp(name, PLATFORM_NAME) == 0)
305 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ;
309 if (strcmp(name, "linux") != 0 && strcmp(name, "windows") != 0 && strcmp(name, "mac") != 0)
311 smcore.LogError("[SM] Error while parsing Address section for \"%s\" (%s):", m_Address, m_CurFile);
312 smcore.LogError("[SM] Unrecognized platform \"%s\"", name);
318 /* No sub-sections allowed:
319 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
320 case PSTATE_GAMEDEFS_KEYS:
321 case PSTATE_GAMEDEFS_SUPPORTED:
322 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
323 case PSTATE_GAMEDEFS_CRC_BINARY:
324 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ:
328 /* If we don't know what we got, start ignoring */
334 return SMCResult_Continue;
337 SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
341 return SMCResult_Continue;
344 if (m_ParseState == PSTATE_GAMEDEFS_OFFSETS_OFFSET)
346 if (strcmp(key, "class") == 0)
348 strncopy(m_Class, value, sizeof(m_Class));
349 } else if (strcmp(key, "prop") == 0) {
350 strncopy(m_Prop, value, sizeof(m_Prop));
351 } else if (strcmp(key, PLATFORM_NAME) == 0) {
352 m_Offsets.replace(m_offset, atoi(value));
354 } else if (m_ParseState == PSTATE_GAMEDEFS_KEYS) {
355 m_Keys.replace(key, m_pStrings->AddString(value));
356 } else if (m_ParseState == PSTATE_GAMEDEFS_SUPPORTED) {
357 if (strcmp(key, "game") == 0)
360 if (DoesGameMatch(value))
364 if ((!had_engine && matched_game) || (matched_engine && matched_game))
366 bShouldBeReadingDefault = true;
369 else if (strcmp(key, "engine") == 0)
372 if (DoesEngineMatch(value))
374 matched_engine = true;
376 if ((!had_game && matched_engine) || (matched_game && matched_engine))
378 bShouldBeReadingDefault = true;
381 } else if (m_ParseState == PSTATE_GAMEDEFS_SIGNATURES_SIG) {
382 if (strcmp(key, PLATFORM_NAME) == 0)
384 strncopy(s_TempSig.sig, value, sizeof(s_TempSig.sig));
385 } else if (strcmp(key, "library") == 0) {
386 strncopy(s_TempSig.library, value, sizeof(s_TempSig.library));
388 } else if (m_ParseState == PSTATE_GAMEDEFS_CRC_BINARY) {
389 if (strcmp(key, PLATFORM_NAME) == 0
391 && !bShouldBeReadingDefault)
393 unsigned int crc = 0;
394 sscanf(value, "%08X", &crc);
395 if (s_ServerBinCRC == crc)
397 bShouldBeReadingDefault = true;
400 } else if (m_ParseState == PSTATE_GAMEDEFS_ADDRESSES_ADDRESS || m_ParseState == PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ) {
401 if (strcmp(key, "read") == 0) {
402 int limit = sizeof(m_AddressRead)/sizeof(m_AddressRead[0]);
403 if (m_AddressReadCount < limit)
405 m_AddressRead[m_AddressReadCount] = atoi(value);
406 m_AddressReadCount++;
410 smcore.LogError("[SM] Error parsing Address \"%s\", does not support more than %d read offsets (gameconf \"%s\")", m_Address, limit, m_CurFile);
412 } else if (strcmp(key, "signature") == 0) {
413 strncopy(m_AddressSignature, value, sizeof(m_AddressSignature));
415 } else if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM) {
416 return m_CustomHandler->ReadSMC_KeyValue(states, key, value);
419 return SMCResult_Continue;
422 SMCResult CGameConfig::ReadSMC_LeavingSection(const SMCStates *states)
427 return SMCResult_Continue;
433 m_CustomHandler->ReadSMC_LeavingSection(states);
434 return SMCResult_Continue;
437 switch (m_ParseState)
441 m_ParseState = PSTATE_NONE;
444 case PSTATE_GAMEDEFS:
446 m_ParseState = PSTATE_GAMES;
449 case PSTATE_GAMEDEFS_CUSTOM:
451 m_ParseState = PSTATE_GAMEDEFS;
452 m_CustomHandler->ReadSMC_ParseEnd(false, false);
455 case PSTATE_GAMEDEFS_KEYS:
456 case PSTATE_GAMEDEFS_OFFSETS:
458 m_ParseState = PSTATE_GAMEDEFS;
461 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
463 /* Parse the offset... */
464 if (m_Class[0] != '\0'
465 && m_Prop[0] != '\0')
467 SendProp *pProp = gamehelpers->FindInSendTable(m_Class, m_Prop);
470 int val = gamehelpers->GetSendPropOffset(pProp);
471 m_Offsets.replace(m_offset, val);
472 m_Props.replace(m_offset, pProp);
474 /* Check if it's a non-default game and no offsets exist */
475 if (((strcmp(m_Game, "*") != 0) && strcmp(m_Game, "#default") != 0)
476 && (!m_Offsets.retrieve(m_offset)))
478 smcore.LogError("[SM] Unable to find property %s.%s (file \"%s\") (mod \"%s\")",
486 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
489 case PSTATE_GAMEDEFS_CRC:
490 case PSTATE_GAMEDEFS_SUPPORTED:
492 if (!bShouldBeReadingDefault)
494 /* If we shouldn't read the rest of this section, set the ignore level. */
496 m_ParseState = PSTATE_GAMES;
498 m_ParseState = PSTATE_GAMEDEFS;
502 case PSTATE_GAMEDEFS_CRC_BINARY:
504 m_ParseState = PSTATE_GAMEDEFS_CRC;
507 case PSTATE_GAMEDEFS_SIGNATURES:
509 m_ParseState = PSTATE_GAMEDEFS;
512 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
514 if (s_TempSig.library[0] == '\0')
517 strncopy(s_TempSig.library, "server", sizeof(s_TempSig.library));
519 void *addrInBase = NULL;
520 if (strcmp(s_TempSig.library, "server") == 0)
522 addrInBase = smcore.serverFactory;
523 } else if (strcmp(s_TempSig.library, "engine") == 0) {
524 addrInBase = smcore.engineFactory;
525 } else if (strcmp(s_TempSig.library, "matchmaking_ds") == 0) {
526 addrInBase = smcore.matchmakingDSFactory;
528 void *final_addr = NULL;
529 if (addrInBase == NULL)
531 smcore.LogError("[SM] Unrecognized library \"%s\" (gameconf \"%s\")",
535 if (s_TempSig.sig[0] == '@')
537 #if defined PLATFORM_WINDOWS
538 MEMORY_BASIC_INFORMATION mem;
540 if (VirtualQuery(addrInBase, &mem, sizeof(mem)))
541 final_addr = g_MemUtils.ResolveSymbol(mem.AllocationBase, &s_TempSig.sig[1]);
543 smcore.LogError("[SM] Unable to find library \"%s\" in memory (gameconf \"%s\")", s_TempSig.library, m_File);
544 #elif defined PLATFORM_POSIX
546 /* GNU only: returns 0 on error, inconsistent! >:[ */
547 if (dladdr(addrInBase, &info) != 0)
549 void *handle = dlopen(info.dli_fname, RTLD_NOW);
552 if (smcore.SymbolsAreHidden())
553 final_addr = g_MemUtils.ResolveSymbol(handle, &s_TempSig.sig[1]);
555 final_addr = dlsym(handle, &s_TempSig.sig[1]);
558 smcore.LogError("[SM] Unable to load library \"%s\" (gameconf \"%s\")",
563 smcore.LogError("[SM] Unable to find library \"%s\" in memory (gameconf \"%s\")",
573 /* First, preprocess the signature */
574 unsigned char real_sig[511];
579 length = strlen(s_TempSig.sig);
581 real_bytes = UTIL_DecodeHexString(real_sig, sizeof(real_sig), s_TempSig.sig);
585 final_addr = g_MemUtils.FindPattern(addrInBase, (char*)real_sig, real_bytes);
590 m_Sigs.replace(m_offset, final_addr);
591 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
595 case PSTATE_GAMEDEFS_ADDRESSES:
597 m_ParseState = PSTATE_GAMEDEFS;
600 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS:
602 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
604 if (m_Address[0] == '\0')
606 smcore.LogError("[SM] Address sections must have names (gameconf \"%s\")", m_CurFile);
609 if (m_AddressSignature[0] == '\0')
611 smcore.LogError("[SM] Address section for \"%s\" did not specify a signature (gameconf \"%s\")", m_Address, m_CurFile);
615 AddressConf addrConf(m_AddressSignature, sizeof(m_AddressSignature), m_AddressReadCount, m_AddressRead);
616 m_Addresses.replace(m_Address, addrConf);
620 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ:
622 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
627 return SMCResult_Continue;
630 #define MSTATE_NONE 0
631 #define MSTATE_MAIN 1
632 #define MSTATE_FILE 2
634 class MasterReader : public ITextListener_SMC
637 virtual void ReadSMC_ParseStart()
643 virtual SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name)
647 return SMCResult_Continue;
650 if (state == MSTATE_NONE)
652 if (strcmp(name, "Game Master") == 0)
661 else if (state == MSTATE_MAIN)
663 strncopy(cur_file, name, sizeof(cur_file));
665 matched_engine = false;
667 matched_game = false;
670 else if (state == MSTATE_FILE)
675 return SMCResult_Continue;
678 virtual SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
680 if (ignoreLevel || state != MSTATE_FILE)
682 return SMCResult_Continue;
685 if (strcmp(key, "engine") == 0)
688 if (DoesEngineMatch(value))
690 matched_engine = true;
693 else if (strcmp(key, "game") == 0)
696 if (DoesGameMatch(value))
702 return SMCResult_Continue;
705 virtual SMCResult ReadSMC_LeavingSection(const SMCStates *states)
710 return SMCResult_Continue;
713 if (state == MSTATE_FILE)
715 /* The four success conditions:
717 * 2. Needed game only.
718 * 3. Needed engine only.
719 * 4. Needed both engine and game.
720 * Final result is minimized via k-map.
723 if ((!had_engine && !had_game) ||
724 (!had_engine && (had_game && matched_game)) ||
725 (!had_game && (had_engine && matched_engine)) ||
726 ((had_game && had_engine) && (matched_game && matched_engine)))
728 if ((!had_engine && !had_game) ||
729 (!had_engine && matched_game) ||
730 (!had_game && matched_engine) ||
731 (matched_engine && matched_game))
733 fileList->push_back(cur_file);
737 else if (state == MSTATE_MAIN)
742 return SMCResult_Continue;
745 List<String> *fileList;
747 unsigned int ignoreLevel;
748 char cur_file[PLATFORM_MAX_PATH];
755 static MasterReader master_reader;
757 bool CGameConfig::Reparse(char *error, size_t maxlength)
759 /* Reset cached data */
766 char path[PLATFORM_MAX_PATH];
768 /* See if we can use the extended gamedata format. */
769 g_pSM->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/master.games.txt", m_File);
770 if (!libsys->PathExists(path))
772 /* Nope, use the old mechanism. */
773 smcore.Format(path, sizeof(path), "%s.txt", m_File);
774 return EnterFile(path, error, maxlength);
777 /* Otherwise, it's time to parse the master. */
779 SMCStates state = {0, 0};
780 List<String> fileList;
781 master_reader.fileList = &fileList;
782 const char *pEngine[2] = { m_pBaseEngine, m_pEngine };
784 for (unsigned char iter = 0; iter < SM_ARRAYSIZE(pEngine); ++iter)
786 if (pEngine[iter] == NULL)
791 this->SetParseEngine(pEngine[iter]);
792 err = textparsers->ParseSMCFile(path, &master_reader, &state, error, maxlength);
793 if (err != SMCError_Okay)
795 const char *msg = textparsers->GetSMCErrorString(err);
797 smcore.LogError("[SM] Error parsing master gameconf file \"%s\":", path);
798 smcore.LogError("[SM] Error %d on line %d, col %d: %s",
802 msg ? msg : "Unknown error");
807 /* Go through each file we found and parse it. */
808 List<String>::iterator iter;
809 for (iter = fileList.begin(); iter != fileList.end(); iter++)
811 smcore.Format(path, sizeof(path), "%s/%s", m_File, (*iter).c_str());
812 if (!EnterFile(path, error, maxlength))
818 /* Parse the contents of the 'custom' directory */
819 g_pSM->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/custom", m_File);
820 IDirectory *customDir = libsys->OpenDirectory(path);
827 while (customDir->MoreFiles())
829 if (!customDir->IsEntryFile())
831 customDir->NextEntry();
835 const char *curFile = customDir->GetEntryName();
837 /* Only allow .txt files */
838 int len = strlen(curFile);
839 if (len > 4 && strcmp(&curFile[len-4], ".txt") != 0)
841 customDir->NextEntry();
845 smcore.Format(path, sizeof(path), "%s/custom/%s", m_File, curFile);
846 if (!EnterFile(path, error, maxlength))
848 libsys->CloseDirectory(customDir);
852 customDir->NextEntry();
855 libsys->CloseDirectory(customDir);
859 bool CGameConfig::EnterFile(const char *file, char *error, size_t maxlength)
862 SMCStates state = {0, 0};
864 g_pSM->BuildPath(Path_SM, m_CurFile, sizeof(m_CurFile), "gamedata/%s", file);
866 /* Initialize parse states */
868 bShouldBeReadingDefault = true;
869 m_ParseState = PSTATE_NONE;
870 const char *pEngine[2] = { m_pBaseEngine, m_pEngine };
872 for (unsigned char iter = 0; iter < SM_ARRAYSIZE(pEngine); ++iter)
874 if (pEngine[iter] == NULL)
879 this->SetParseEngine(pEngine[iter]);
880 if ((err=textparsers->ParseSMCFile(m_CurFile, this, &state, error, maxlength))
883 const char *msg = textparsers->GetSMCErrorString(err);
885 smcore.LogError("[SM] Error parsing gameconfig file \"%s\":", m_CurFile);
886 smcore.LogError("[SM] Error %d on line %d, col %d: %s",
890 msg ? msg : "Unknown error");
892 if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM)
894 //error occurred while parsing a custom section
895 m_CustomHandler->ReadSMC_ParseEnd(true, true);
896 m_CustomHandler = NULL;
907 void CGameConfig::SetBaseEngine(const char *engine)
909 m_pBaseEngine = engine;
912 void CGameConfig::SetParseEngine(const char *engine)
914 g_pParseEngine = engine;
917 bool CGameConfig::GetOffset(const char *key, int *value)
919 return m_Offsets.retrieve(key, value);
922 const char *CGameConfig::GetKeyValue(const char *key)
925 if (!m_Keys.retrieve(key, &address))
927 return m_pStrings->GetString(address);
930 //memory addresses below 0x10000 are automatically considered invalid for dereferencing
931 #define VALID_MINIMUM_MEMORY_ADDRESS 0x10000
933 bool CGameConfig::GetAddress(const char *key, void **retaddr)
935 StringHashMap<AddressConf>::Result r = m_Addresses.find(key);
942 AddressConf &addrConf = r->value;
945 if (!GetMemSig(addrConf.signatureName, &addr))
951 for (int i = 0; i < addrConf.readCount; ++i)
953 int offset = addrConf.read[i];
955 //NULLs in the middle of an indirection chain are bad, end NULL is ok
956 if (addr == NULL || reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
961 addr = *(reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(addr) + offset));
968 static inline unsigned minOf(unsigned a, unsigned b)
970 return a <= b ? a : b;
973 CGameConfig::AddressConf::AddressConf(char *sigName, unsigned sigLength, unsigned readCount, int *read)
975 unsigned readLimit = minOf(readCount, sizeof(this->read) / sizeof(this->read[0]));
977 strncopy(signatureName, sigName, sizeof(signatureName) / sizeof(signatureName[0]));
978 this->readCount = readLimit;
979 memcpy(&this->read[0], read, sizeof(this->read[0])*readLimit);
982 SendProp *CGameConfig::GetSendProp(const char *key)
985 if (!m_Props.retrieve(key, &prop))
990 bool CGameConfig::GetMemSig(const char *key, void **addr)
992 return m_Sigs.retrieve(key, addr);
995 GameConfigManager::GameConfigManager()
999 GameConfigManager::~GameConfigManager()
1003 void GameConfigManager::OnSourceModStartup(bool late)
1005 LoadGameConfigFile("core.games", &g_pGameConf, NULL, 0);
1007 strncopy(g_Game, g_pSM->GetGameFolderName(), sizeof(g_Game));
1008 strncopy(g_GameDesc + 1, smcore.GetGameDescription(), sizeof(g_GameDesc) - 1);
1009 smcore.GetGameName(g_GameName + 1, sizeof(g_GameName) - 1);
1012 void GameConfigManager::OnSourceModAllInitialized()
1014 /* NOW initialize the game file */
1015 CGameConfig *pGameConf = (CGameConfig *)g_pGameConf;
1018 if (!pGameConf->Reparse(error, sizeof(error)))
1023 sharesys->AddInterface(NULL, this);
1026 void GameConfigManager::OnSourceModAllShutdown()
1028 CloseGameConfigFile(g_pGameConf);
1031 bool GameConfigManager::LoadGameConfigFile(const char *file, IGameConfig **_pConfig, char *error, size_t maxlength)
1034 /* A crash was detected during last load - We block the gamedata loading so it hopefully won't happen again */
1035 if (g_blockGameDataLoad)
1037 UTIL_Format(error, maxlength, "GameData loaded blocked due to detected crash");
1042 CGameConfig *pConfig;
1044 if (m_Lookup.retrieve(file, &pConfig))
1047 *_pConfig = pConfig;
1051 pConfig = new CGameConfig(file);
1053 /* :HACKHACK: Don't parse the main config file */
1055 if (_pConfig != &g_pGameConf)
1057 retval = pConfig->Reparse(error, maxlength);
1060 m_Lookup.insert(file, pConfig);
1062 *_pConfig = pConfig;
1066 void GameConfigManager::CloseGameConfigFile(IGameConfig *cfg)
1068 CGameConfig *pConfig = (CGameConfig *)cfg;
1072 extern Handle_t g_GameConfigsType;
1074 IGameConfig *GameConfigManager::ReadHandle(Handle_t hndl, IdentityToken_t *ident, HandleError *err)
1076 HandleSecurity sec(ident, g_pCoreIdent);
1077 IGameConfig *conf = NULL;
1078 HandleError _err = handlesys->ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&conf);
1088 void GameConfigManager::AddUserConfigHook(const char *sectionname, ITextListener_SMC *listener)
1090 m_customHandlers.insert(sectionname, listener);
1093 void GameConfigManager::RemoveUserConfigHook(const char *sectionname, ITextListener_SMC *aListener)
1095 ITextListener_SMC *listener;
1096 if (!m_customHandlers.retrieve(sectionname, &listener))
1099 if (listener != aListener)
1102 m_customHandlers.remove(sectionname);
1106 void GameConfigManager::AcquireLock()
1110 void GameConfigManager::ReleaseLock()
1114 void GameConfigManager::RemoveCachedConfig(CGameConfig *config)
1116 m_Lookup.remove(config->m_File);