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));
124 m_CustomHandler = NULL;
127 m_pEngine = smcore.GetSourceEngineName();
131 if (strcmp(m_pEngine, "css") == 0 || strcmp(m_pEngine, "dods") == 0 || strcmp(m_pEngine, "hl2dm") == 0 || strcmp(m_pEngine, "tf2") == 0)
132 this->SetBaseEngine("orangebox_valve");
133 else if (strcmp(m_pEngine, "nucleardawn") == 0)
134 this->SetBaseEngine("left4dead2");
136 this->SetBaseEngine(NULL);
139 CGameConfig::~CGameConfig()
141 g_GameConfigs.RemoveCachedConfig(this);
144 SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *name)
149 return SMCResult_Continue;
152 switch (m_ParseState)
156 if (strcmp(name, "Games") == 0)
158 m_ParseState = PSTATE_GAMES;
166 if (strcmp(name, "*") == 0 ||
167 strcmp(name, "#default") == 0 ||
170 bShouldBeReadingDefault = true;
171 m_ParseState = PSTATE_GAMEDEFS;
172 strncopy(m_Game, name, sizeof(m_Game));
178 case PSTATE_GAMEDEFS:
180 if (strcmp(name, "Offsets") == 0)
182 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
184 else if (strcmp(name, "Keys") == 0)
186 m_ParseState = PSTATE_GAMEDEFS_KEYS;
188 else if ((strcmp(name, "#supported") == 0) && (strcmp(m_Game, "#default") == 0))
190 m_ParseState = PSTATE_GAMEDEFS_SUPPORTED;
191 /* Ignore this section unless we get a game. */
192 bShouldBeReadingDefault = false;
194 matched_game = false;
196 matched_engine = false;
198 else if (strcmp(name, "Signatures") == 0)
200 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
202 else if (strcmp(name, "CRC") == 0)
204 m_ParseState = PSTATE_GAMEDEFS_CRC;
205 bShouldBeReadingDefault = false;
207 else if (strcmp(name, "Addresses") == 0)
209 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
213 if (g_GameConfigs.m_customHandlers.retrieve(name, &m_CustomHandler))
216 m_ParseState = PSTATE_GAMEDEFS_CUSTOM;
217 m_CustomHandler->ReadSMC_ParseStart();
225 case PSTATE_GAMEDEFS_OFFSETS:
229 strncopy(m_offset, name, sizeof(m_offset));
230 m_ParseState = PSTATE_GAMEDEFS_OFFSETS_OFFSET;
233 case PSTATE_GAMEDEFS_SIGNATURES:
235 strncopy(m_offset, name, sizeof(m_offset));
237 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES_SIG;
240 case PSTATE_GAMEDEFS_CRC:
244 if (strcmp(name, "server") != 0)
246 smcore.Format(error, sizeof(error), "Unrecognized library \"%s\"", name);
248 else if (!s_ServerBinCRC_Ok)
251 char path[PLATFORM_MAX_PATH];
253 g_pSM->BuildPath(Path_Game, path, sizeof(path), "bin/" PLATFORM_SERVER_BINARY);
254 if ((fp = fopen(path, "rb")) == NULL)
256 smcore.Format(error, sizeof(error), "Could not open binary: %s", path);
261 fseek(fp, 0, SEEK_END);
263 fseek(fp, 0, SEEK_SET);
265 buffer = malloc(size);
266 fread(buffer, size, 1, fp);
267 s_ServerBinCRC = UTIL_CRC32(buffer, size);
269 s_ServerBinCRC_Ok = true;
273 if (error[0] != '\0')
276 smcore.LogError("[SM] Error while parsing CRC section for \"%s\" (%s):", m_Game, m_CurFile);
277 smcore.LogError("[SM] %s", error);
279 m_ParseState = PSTATE_GAMEDEFS_CRC_BINARY;
283 case PSTATE_GAMEDEFS_CUSTOM:
286 return m_CustomHandler->ReadSMC_NewSection(states, name);
289 case PSTATE_GAMEDEFS_ADDRESSES:
292 m_AddressSignature[0] = '\0';
293 m_AddressReadCount = 0;
295 strncopy(m_Address, name, sizeof(m_Address));
296 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
300 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS:
302 if (strcmp(name, PLATFORM_NAME) == 0)
304 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ;
308 if (strcmp(name, "linux") != 0 && strcmp(name, "windows") != 0 && strcmp(name, "mac") != 0)
310 smcore.LogError("[SM] Error while parsing Address section for \"%s\" (%s):", m_Address, m_CurFile);
311 smcore.LogError("[SM] Unrecognized platform \"%s\"", name);
317 /* No sub-sections allowed:
318 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
319 case PSTATE_GAMEDEFS_KEYS:
320 case PSTATE_GAMEDEFS_SUPPORTED:
321 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
322 case PSTATE_GAMEDEFS_CRC_BINARY:
323 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ:
327 /* If we don't know what we got, start ignoring */
333 return SMCResult_Continue;
336 SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
340 return SMCResult_Continue;
343 if (m_ParseState == PSTATE_GAMEDEFS_OFFSETS_OFFSET)
345 if (strcmp(key, "class") == 0)
347 strncopy(m_Class, value, sizeof(m_Class));
348 } else if (strcmp(key, "prop") == 0) {
349 strncopy(m_Prop, value, sizeof(m_Prop));
350 } else if (strcmp(key, PLATFORM_NAME) == 0) {
351 m_Offsets.replace(m_offset, atoi(value));
353 } else if (m_ParseState == PSTATE_GAMEDEFS_KEYS) {
354 ke::AString vstr(value);
355 m_Keys.replace(key, ke::Move(vstr));
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 */
765 char path[PLATFORM_MAX_PATH];
767 /* See if we can use the extended gamedata format. */
768 g_pSM->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/master.games.txt", m_File);
769 if (!libsys->PathExists(path))
771 /* Nope, use the old mechanism. */
772 smcore.Format(path, sizeof(path), "%s.txt", m_File);
773 return EnterFile(path, error, maxlength);
776 /* Otherwise, it's time to parse the master. */
778 SMCStates state = {0, 0};
779 List<String> fileList;
780 master_reader.fileList = &fileList;
781 const char *pEngine[2] = { m_pBaseEngine, m_pEngine };
783 for (unsigned char iter = 0; iter < SM_ARRAYSIZE(pEngine); ++iter)
785 if (pEngine[iter] == NULL)
790 this->SetParseEngine(pEngine[iter]);
791 err = textparsers->ParseSMCFile(path, &master_reader, &state, error, maxlength);
792 if (err != SMCError_Okay)
794 const char *msg = textparsers->GetSMCErrorString(err);
796 smcore.LogError("[SM] Error parsing master gameconf file \"%s\":", path);
797 smcore.LogError("[SM] Error %d on line %d, col %d: %s",
801 msg ? msg : "Unknown error");
806 /* Go through each file we found and parse it. */
807 List<String>::iterator iter;
808 for (iter = fileList.begin(); iter != fileList.end(); iter++)
810 smcore.Format(path, sizeof(path), "%s/%s", m_File, (*iter).c_str());
811 if (!EnterFile(path, error, maxlength))
817 /* Parse the contents of the 'custom' directory */
818 g_pSM->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/custom", m_File);
819 IDirectory *customDir = libsys->OpenDirectory(path);
826 while (customDir->MoreFiles())
828 if (!customDir->IsEntryFile())
830 customDir->NextEntry();
834 const char *curFile = customDir->GetEntryName();
836 /* Only allow .txt files */
837 int len = strlen(curFile);
838 if (len > 4 && strcmp(&curFile[len-4], ".txt") != 0)
840 customDir->NextEntry();
844 smcore.Format(path, sizeof(path), "%s/custom/%s", m_File, curFile);
845 if (!EnterFile(path, error, maxlength))
847 libsys->CloseDirectory(customDir);
851 customDir->NextEntry();
854 libsys->CloseDirectory(customDir);
858 bool CGameConfig::EnterFile(const char *file, char *error, size_t maxlength)
861 SMCStates state = {0, 0};
863 g_pSM->BuildPath(Path_SM, m_CurFile, sizeof(m_CurFile), "gamedata/%s", file);
865 /* Initialize parse states */
867 bShouldBeReadingDefault = true;
868 m_ParseState = PSTATE_NONE;
869 const char *pEngine[2] = { m_pBaseEngine, m_pEngine };
871 for (unsigned char iter = 0; iter < SM_ARRAYSIZE(pEngine); ++iter)
873 if (pEngine[iter] == NULL)
878 this->SetParseEngine(pEngine[iter]);
879 if ((err=textparsers->ParseSMCFile(m_CurFile, this, &state, error, maxlength))
882 const char *msg = textparsers->GetSMCErrorString(err);
884 smcore.LogError("[SM] Error parsing gameconfig file \"%s\":", m_CurFile);
885 smcore.LogError("[SM] Error %d on line %d, col %d: %s",
889 msg ? msg : "Unknown error");
891 if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM)
893 //error occurred while parsing a custom section
894 m_CustomHandler->ReadSMC_ParseEnd(true, true);
895 m_CustomHandler = NULL;
906 void CGameConfig::SetBaseEngine(const char *engine)
908 m_pBaseEngine = engine;
911 void CGameConfig::SetParseEngine(const char *engine)
913 g_pParseEngine = engine;
916 bool CGameConfig::GetOffset(const char *key, int *value)
918 return m_Offsets.retrieve(key, value);
921 const char *CGameConfig::GetKeyValue(const char *key)
923 StringHashMap<ke::AString>::Result r = m_Keys.find(key);
926 return r->value.chars();
929 //memory addresses below 0x10000 are automatically considered invalid for dereferencing
930 #define VALID_MINIMUM_MEMORY_ADDRESS 0x10000
932 bool CGameConfig::GetAddress(const char *key, void **retaddr)
934 StringHashMap<AddressConf>::Result r = m_Addresses.find(key);
941 AddressConf &addrConf = r->value;
944 if (!GetMemSig(addrConf.signatureName, &addr))
950 for (int i = 0; i < addrConf.readCount; ++i)
952 int offset = addrConf.read[i];
954 //NULLs in the middle of an indirection chain are bad, end NULL is ok
955 if (addr == NULL || reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
960 addr = *(reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(addr) + offset));
967 static inline unsigned minOf(unsigned a, unsigned b)
969 return a <= b ? a : b;
972 CGameConfig::AddressConf::AddressConf(char *sigName, unsigned sigLength, unsigned readCount, int *read)
974 unsigned readLimit = minOf(readCount, sizeof(this->read) / sizeof(this->read[0]));
976 strncopy(signatureName, sigName, sizeof(signatureName) / sizeof(signatureName[0]));
977 this->readCount = readLimit;
978 memcpy(&this->read[0], read, sizeof(this->read[0])*readLimit);
981 SendProp *CGameConfig::GetSendProp(const char *key)
984 if (!m_Props.retrieve(key, &prop))
989 bool CGameConfig::GetMemSig(const char *key, void **addr)
991 return m_Sigs.retrieve(key, addr);
994 GameConfigManager::GameConfigManager()
998 GameConfigManager::~GameConfigManager()
1002 void GameConfigManager::OnSourceModStartup(bool late)
1004 LoadGameConfigFile("core.games", &g_pGameConf, NULL, 0);
1006 strncopy(g_Game, g_pSM->GetGameFolderName(), sizeof(g_Game));
1007 strncopy(g_GameDesc + 1, smcore.GetGameDescription(), sizeof(g_GameDesc) - 1);
1008 smcore.GetGameName(g_GameName + 1, sizeof(g_GameName) - 1);
1011 void GameConfigManager::OnSourceModAllInitialized()
1013 /* NOW initialize the game file */
1014 CGameConfig *pGameConf = (CGameConfig *)g_pGameConf;
1017 if (!pGameConf->Reparse(error, sizeof(error)))
1022 sharesys->AddInterface(NULL, this);
1025 void GameConfigManager::OnSourceModAllShutdown()
1027 CloseGameConfigFile(g_pGameConf);
1030 bool GameConfigManager::LoadGameConfigFile(const char *file, IGameConfig **_pConfig, char *error, size_t maxlength)
1033 /* A crash was detected during last load - We block the gamedata loading so it hopefully won't happen again */
1034 if (g_blockGameDataLoad)
1036 UTIL_Format(error, maxlength, "GameData loaded blocked due to detected crash");
1041 CGameConfig *pConfig;
1043 if (m_Lookup.retrieve(file, &pConfig))
1046 *_pConfig = pConfig;
1050 pConfig = new CGameConfig(file);
1052 /* :HACKHACK: Don't parse the main config file */
1054 if (_pConfig != &g_pGameConf)
1056 retval = pConfig->Reparse(error, maxlength);
1059 m_Lookup.insert(file, pConfig);
1061 *_pConfig = pConfig;
1065 void GameConfigManager::CloseGameConfigFile(IGameConfig *cfg)
1067 CGameConfig *pConfig = (CGameConfig *)cfg;
1071 extern Handle_t g_GameConfigsType;
1073 IGameConfig *GameConfigManager::ReadHandle(Handle_t hndl, IdentityToken_t *ident, HandleError *err)
1075 HandleSecurity sec(ident, g_pCoreIdent);
1076 IGameConfig *conf = NULL;
1077 HandleError _err = handlesys->ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&conf);
1087 void GameConfigManager::AddUserConfigHook(const char *sectionname, ITextListener_SMC *listener)
1089 m_customHandlers.insert(sectionname, listener);
1092 void GameConfigManager::RemoveUserConfigHook(const char *sectionname, ITextListener_SMC *aListener)
1094 ITextListener_SMC *listener;
1095 if (!m_customHandlers.retrieve(sectionname, &listener))
1098 if (listener != aListener)
1101 m_customHandlers.remove(sectionname);
1105 void GameConfigManager::AcquireLock()
1109 void GameConfigManager::ReleaseLock()
1113 void GameConfigManager::RemoveCachedConfig(CGameConfig *config)
1115 m_Lookup.remove(config->m_File);