More Windows build fix.
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");
137 this->SetBaseEngine(NULL);
140 CGameConfig::~CGameConfig()
146 SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *name)
151 return SMCResult_Continue;
154 switch (m_ParseState)
158 if (strcmp(name, "Games") == 0)
160 m_ParseState = PSTATE_GAMES;
168 if (strcmp(name, "*") == 0 ||
169 strcmp(name, "#default") == 0 ||
172 bShouldBeReadingDefault = true;
173 m_ParseState = PSTATE_GAMEDEFS;
174 strncopy(m_Game, name, sizeof(m_Game));
180 case PSTATE_GAMEDEFS:
182 if (strcmp(name, "Offsets") == 0)
184 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
186 else if (strcmp(name, "Keys") == 0)
188 m_ParseState = PSTATE_GAMEDEFS_KEYS;
190 else if ((strcmp(name, "#supported") == 0) && (strcmp(m_Game, "#default") == 0))
192 m_ParseState = PSTATE_GAMEDEFS_SUPPORTED;
193 /* Ignore this section unless we get a game. */
194 bShouldBeReadingDefault = false;
196 matched_game = false;
198 matched_engine = false;
200 else if (strcmp(name, "Signatures") == 0)
202 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
204 else if (strcmp(name, "CRC") == 0)
206 m_ParseState = PSTATE_GAMEDEFS_CRC;
207 bShouldBeReadingDefault = false;
209 else if (strcmp(name, "Addresses") == 0)
211 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
215 ITextListener_SMC **pListen = g_GameConfigs.m_customHandlers.retrieve(name);
220 m_ParseState = PSTATE_GAMEDEFS_CUSTOM;
221 m_CustomHandler = *pListen;
222 m_CustomHandler->ReadSMC_ParseStart();
230 case PSTATE_GAMEDEFS_OFFSETS:
234 strncopy(m_offset, name, sizeof(m_offset));
235 m_ParseState = PSTATE_GAMEDEFS_OFFSETS_OFFSET;
238 case PSTATE_GAMEDEFS_SIGNATURES:
240 strncopy(m_offset, name, sizeof(m_offset));
242 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES_SIG;
245 case PSTATE_GAMEDEFS_CRC:
249 if (strcmp(name, "server") != 0)
251 smcore.Format(error, sizeof(error), "Unrecognized library \"%s\"", name);
253 else if (!s_ServerBinCRC_Ok)
256 char path[PLATFORM_MAX_PATH];
258 g_pSM->BuildPath(Path_Game, path, sizeof(path), "bin/" PLATFORM_SERVER_BINARY);
259 if ((fp = fopen(path, "rb")) == NULL)
261 smcore.Format(error, sizeof(error), "Could not open binary: %s", path);
266 fseek(fp, 0, SEEK_END);
268 fseek(fp, 0, SEEK_SET);
270 buffer = malloc(size);
271 fread(buffer, size, 1, fp);
272 s_ServerBinCRC = UTIL_CRC32(buffer, size);
274 s_ServerBinCRC_Ok = true;
278 if (error[0] != '\0')
281 smcore.LogError("[SM] Error while parsing CRC section for \"%s\" (%s):", m_Game, m_CurFile);
282 smcore.LogError("[SM] %s", error);
284 m_ParseState = PSTATE_GAMEDEFS_CRC_BINARY;
288 case PSTATE_GAMEDEFS_CUSTOM:
291 return m_CustomHandler->ReadSMC_NewSection(states, name);
294 case PSTATE_GAMEDEFS_ADDRESSES:
297 m_AddressSignature[0] = '\0';
298 m_AddressReadCount = 0;
300 strncopy(m_Address, name, sizeof(m_Address));
301 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
305 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS:
307 if (strcmp(name, PLATFORM_NAME) == 0)
309 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ;
313 if (strcmp(name, "linux") != 0 && strcmp(name, "windows") != 0 && strcmp(name, "mac") != 0)
315 smcore.LogError("[SM] Error while parsing Address section for \"%s\" (%s):", m_Address, m_CurFile);
316 smcore.LogError("[SM] Unrecognized platform \"%s\"", name);
322 /* No sub-sections allowed:
323 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
324 case PSTATE_GAMEDEFS_KEYS:
325 case PSTATE_GAMEDEFS_SUPPORTED:
326 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
327 case PSTATE_GAMEDEFS_CRC_BINARY:
328 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ:
332 /* If we don't know what we got, start ignoring */
338 return SMCResult_Continue;
341 SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
345 return SMCResult_Continue;
348 if (m_ParseState == PSTATE_GAMEDEFS_OFFSETS_OFFSET)
350 if (strcmp(key, "class") == 0)
352 strncopy(m_Class, value, sizeof(m_Class));
353 } else if (strcmp(key, "prop") == 0) {
354 strncopy(m_Prop, value, sizeof(m_Prop));
355 } else if (strcmp(key, PLATFORM_NAME) == 0) {
356 m_Offsets.replace(m_offset, atoi(value));
358 } else if (m_ParseState == PSTATE_GAMEDEFS_KEYS) {
359 m_Keys.replace(key, m_pStrings->AddString(value));
360 } else if (m_ParseState == PSTATE_GAMEDEFS_SUPPORTED) {
361 if (strcmp(key, "game") == 0)
364 if (DoesGameMatch(value))
368 if ((!had_engine && matched_game) || (matched_engine && matched_game))
370 bShouldBeReadingDefault = true;
373 else if (strcmp(key, "engine") == 0)
376 if (DoesEngineMatch(value))
378 matched_engine = true;
380 if ((!had_game && matched_engine) || (matched_game && matched_engine))
382 bShouldBeReadingDefault = true;
385 } else if (m_ParseState == PSTATE_GAMEDEFS_SIGNATURES_SIG) {
386 if (strcmp(key, PLATFORM_NAME) == 0)
388 strncopy(s_TempSig.sig, value, sizeof(s_TempSig.sig));
389 } else if (strcmp(key, "library") == 0) {
390 strncopy(s_TempSig.library, value, sizeof(s_TempSig.library));
392 } else if (m_ParseState == PSTATE_GAMEDEFS_CRC_BINARY) {
393 if (strcmp(key, PLATFORM_NAME) == 0
395 && !bShouldBeReadingDefault)
397 unsigned int crc = 0;
398 sscanf(value, "%08X", &crc);
399 if (s_ServerBinCRC == crc)
401 bShouldBeReadingDefault = true;
404 } else if (m_ParseState == PSTATE_GAMEDEFS_ADDRESSES_ADDRESS || m_ParseState == PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ) {
405 if (strcmp(key, "read") == 0) {
406 int limit = sizeof(m_AddressRead)/sizeof(m_AddressRead[0]);
407 if (m_AddressReadCount < limit)
409 m_AddressRead[m_AddressReadCount] = atoi(value);
410 m_AddressReadCount++;
414 smcore.LogError("[SM] Error parsing Address \"%s\", does not support more than %d read offsets (gameconf \"%s\")", m_Address, limit, m_CurFile);
416 } else if (strcmp(key, "signature") == 0) {
417 strncopy(m_AddressSignature, value, sizeof(m_AddressSignature));
419 } else if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM) {
420 return m_CustomHandler->ReadSMC_KeyValue(states, key, value);
423 return SMCResult_Continue;
426 SMCResult CGameConfig::ReadSMC_LeavingSection(const SMCStates *states)
431 return SMCResult_Continue;
437 m_CustomHandler->ReadSMC_LeavingSection(states);
438 return SMCResult_Continue;
441 switch (m_ParseState)
445 m_ParseState = PSTATE_NONE;
448 case PSTATE_GAMEDEFS:
450 m_ParseState = PSTATE_GAMES;
453 case PSTATE_GAMEDEFS_CUSTOM:
455 m_ParseState = PSTATE_GAMEDEFS;
456 m_CustomHandler->ReadSMC_ParseEnd(false, false);
459 case PSTATE_GAMEDEFS_KEYS:
460 case PSTATE_GAMEDEFS_OFFSETS:
462 m_ParseState = PSTATE_GAMEDEFS;
465 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
467 /* Parse the offset... */
468 if (m_Class[0] != '\0'
469 && m_Prop[0] != '\0')
471 SendProp *pProp = gamehelpers->FindInSendTable(m_Class, m_Prop);
474 int val = gamehelpers->GetSendPropOffset(pProp);
475 m_Offsets.replace(m_offset, val);
476 m_Props.replace(m_offset, pProp);
478 /* Check if it's a non-default game and no offsets exist */
479 if (((strcmp(m_Game, "*") != 0) && strcmp(m_Game, "#default") != 0)
480 && (!m_Offsets.retrieve(m_offset)))
482 smcore.LogError("[SM] Unable to find property %s.%s (file \"%s\") (mod \"%s\")",
490 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
493 case PSTATE_GAMEDEFS_CRC:
494 case PSTATE_GAMEDEFS_SUPPORTED:
496 if (!bShouldBeReadingDefault)
498 /* If we shouldn't read the rest of this section, set the ignore level. */
500 m_ParseState = PSTATE_GAMES;
502 m_ParseState = PSTATE_GAMEDEFS;
506 case PSTATE_GAMEDEFS_CRC_BINARY:
508 m_ParseState = PSTATE_GAMEDEFS_CRC;
511 case PSTATE_GAMEDEFS_SIGNATURES:
513 m_ParseState = PSTATE_GAMEDEFS;
516 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
518 if (s_TempSig.library[0] == '\0')
521 strncopy(s_TempSig.library, "server", sizeof(s_TempSig.library));
523 void *addrInBase = NULL;
524 if (strcmp(s_TempSig.library, "server") == 0)
526 addrInBase = smcore.serverFactory;
527 } else if (strcmp(s_TempSig.library, "engine") == 0) {
528 addrInBase = smcore.engineFactory;
529 } else if (strcmp(s_TempSig.library, "matchmaking_ds") == 0) {
530 addrInBase = smcore.matchmakingDSFactory;
532 void *final_addr = NULL;
533 if (addrInBase == NULL)
535 smcore.LogError("[SM] Unrecognized library \"%s\" (gameconf \"%s\")",
539 if (s_TempSig.sig[0] == '@')
541 #if defined PLATFORM_WINDOWS
542 MEMORY_BASIC_INFORMATION mem;
544 if (VirtualQuery(addrInBase, &mem, sizeof(mem)))
545 final_addr = g_MemUtils.ResolveSymbol(mem.AllocationBase, &s_TempSig.sig[1]);
547 smcore.LogError("[SM] Unable to find library \"%s\" in memory (gameconf \"%s\")", s_TempSig.library, m_File);
548 #elif defined PLATFORM_POSIX
550 /* GNU only: returns 0 on error, inconsistent! >:[ */
551 if (dladdr(addrInBase, &info) != 0)
553 void *handle = dlopen(info.dli_fname, RTLD_NOW);
556 if (smcore.SymbolsAreHidden())
557 final_addr = g_MemUtils.ResolveSymbol(handle, &s_TempSig.sig[1]);
559 final_addr = dlsym(handle, &s_TempSig.sig[1]);
562 smcore.LogError("[SM] Unable to load library \"%s\" (gameconf \"%s\")",
567 smcore.LogError("[SM] Unable to find library \"%s\" in memory (gameconf \"%s\")",
577 /* First, preprocess the signature */
578 unsigned char real_sig[511];
583 length = strlen(s_TempSig.sig);
585 real_bytes = UTIL_DecodeHexString(real_sig, sizeof(real_sig), s_TempSig.sig);
589 final_addr = g_MemUtils.FindPattern(addrInBase, (char*)real_sig, real_bytes);
594 m_Sigs.replace(m_offset, final_addr);
595 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
599 case PSTATE_GAMEDEFS_ADDRESSES:
601 m_ParseState = PSTATE_GAMEDEFS;
604 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS:
606 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
608 if (m_Address[0] == '\0')
610 smcore.LogError("[SM] Address sections must have names (gameconf \"%s\")", m_CurFile);
613 if (m_AddressSignature[0] == '\0')
615 smcore.LogError("[SM] Address section for \"%s\" did not specify a signature (gameconf \"%s\")", m_Address, m_CurFile);
619 AddressConf addrConf(m_AddressSignature, sizeof(m_AddressSignature), m_AddressReadCount, m_AddressRead);
620 m_pAddresses->replace(m_Address, addrConf);
624 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ:
626 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
631 return SMCResult_Continue;
634 #define MSTATE_NONE 0
635 #define MSTATE_MAIN 1
636 #define MSTATE_FILE 2
638 class MasterReader : public ITextListener_SMC
641 virtual void ReadSMC_ParseStart()
647 virtual SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name)
651 return SMCResult_Continue;
654 if (state == MSTATE_NONE)
656 if (strcmp(name, "Game Master") == 0)
665 else if (state == MSTATE_MAIN)
667 strncopy(cur_file, name, sizeof(cur_file));
669 matched_engine = false;
671 matched_game = false;
674 else if (state == MSTATE_FILE)
679 return SMCResult_Continue;
682 virtual SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
684 if (ignoreLevel || state != MSTATE_FILE)
686 return SMCResult_Continue;
689 if (strcmp(key, "engine") == 0)
692 if (DoesEngineMatch(value))
694 matched_engine = true;
697 else if (strcmp(key, "game") == 0)
700 if (DoesGameMatch(value))
706 return SMCResult_Continue;
709 virtual SMCResult ReadSMC_LeavingSection(const SMCStates *states)
714 return SMCResult_Continue;
717 if (state == MSTATE_FILE)
719 /* The four success conditions:
721 * 2. Needed game only.
722 * 3. Needed engine only.
723 * 4. Needed both engine and game.
724 * Final result is minimized via k-map.
727 if ((!had_engine && !had_game) ||
728 (!had_engine && (had_game && matched_game)) ||
729 (!had_game && (had_engine && matched_engine)) ||
730 ((had_game && had_engine) && (matched_game && matched_engine)))
732 if ((!had_engine && !had_game) ||
733 (!had_engine && matched_game) ||
734 (!had_game && matched_engine) ||
735 (matched_engine && matched_game))
737 fileList->push_back(cur_file);
741 else if (state == MSTATE_MAIN)
746 return SMCResult_Continue;
749 List<String> *fileList;
751 unsigned int ignoreLevel;
752 char cur_file[PLATFORM_MAX_PATH];
759 static MasterReader master_reader;
761 bool CGameConfig::Reparse(char *error, size_t maxlength)
763 /* Reset cached data */
768 m_pAddresses->clear();
770 char path[PLATFORM_MAX_PATH];
772 /* See if we can use the extended gamedata format. */
773 g_pSM->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/master.games.txt", m_File);
774 if (!libsys->PathExists(path))
776 /* Nope, use the old mechanism. */
777 smcore.Format(path, sizeof(path), "%s.txt", m_File);
778 return EnterFile(path, error, maxlength);
781 /* Otherwise, it's time to parse the master. */
783 SMCStates state = {0, 0};
784 List<String> fileList;
785 master_reader.fileList = &fileList;
786 const char *pEngine[2] = { m_pBaseEngine, m_pEngine };
788 for (unsigned char iter = 0; iter < SM_ARRAYSIZE(pEngine); ++iter)
790 if (pEngine[iter] == NULL)
795 this->SetParseEngine(pEngine[iter]);
796 err = textparsers->ParseSMCFile(path, &master_reader, &state, error, maxlength);
797 if (err != SMCError_Okay)
799 const char *msg = textparsers->GetSMCErrorString(err);
801 smcore.LogError("[SM] Error parsing master gameconf file \"%s\":", path);
802 smcore.LogError("[SM] Error %d on line %d, col %d: %s",
806 msg ? msg : "Unknown error");
811 /* Go through each file we found and parse it. */
812 List<String>::iterator iter;
813 for (iter = fileList.begin(); iter != fileList.end(); iter++)
815 smcore.Format(path, sizeof(path), "%s/%s", m_File, (*iter).c_str());
816 if (!EnterFile(path, error, maxlength))
822 /* Parse the contents of the 'custom' directory */
823 g_pSM->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/custom", m_File);
824 IDirectory *customDir = libsys->OpenDirectory(path);
831 while (customDir->MoreFiles())
833 if (!customDir->IsEntryFile())
835 customDir->NextEntry();
839 const char *curFile = customDir->GetEntryName();
841 /* Only allow .txt files */
842 int len = strlen(curFile);
843 if (len > 4 && strcmp(&curFile[len-4], ".txt") != 0)
845 customDir->NextEntry();
849 smcore.Format(path, sizeof(path), "%s/custom/%s", m_File, curFile);
850 if (!EnterFile(path, error, maxlength))
852 libsys->CloseDirectory(customDir);
856 customDir->NextEntry();
859 libsys->CloseDirectory(customDir);
863 bool CGameConfig::EnterFile(const char *file, char *error, size_t maxlength)
866 SMCStates state = {0, 0};
868 g_pSM->BuildPath(Path_SM, m_CurFile, sizeof(m_CurFile), "gamedata/%s", file);
870 /* Initialize parse states */
872 bShouldBeReadingDefault = true;
873 m_ParseState = PSTATE_NONE;
874 const char *pEngine[2] = { m_pBaseEngine, m_pEngine };
876 for (unsigned char iter = 0; iter < SM_ARRAYSIZE(pEngine); ++iter)
878 if (pEngine[iter] == NULL)
883 this->SetParseEngine(pEngine[iter]);
884 if ((err=textparsers->ParseSMCFile(m_CurFile, this, &state, error, maxlength))
887 const char *msg = textparsers->GetSMCErrorString(err);
889 smcore.LogError("[SM] Error parsing gameconfig file \"%s\":", m_CurFile);
890 smcore.LogError("[SM] Error %d on line %d, col %d: %s",
894 msg ? msg : "Unknown error");
896 if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM)
898 //error occurred while parsing a custom section
899 m_CustomHandler->ReadSMC_ParseEnd(true, true);
900 m_CustomHandler = NULL;
911 void CGameConfig::SetBaseEngine(const char *engine)
913 m_pBaseEngine = engine;
916 void CGameConfig::SetParseEngine(const char *engine)
918 g_pParseEngine = engine;
921 bool CGameConfig::GetOffset(const char *key, int *value)
924 if ((pvalue = m_Offsets.retrieve(key)) == NULL)
931 const char *CGameConfig::GetKeyValue(const char *key)
934 if ((pkey = m_Keys.retrieve(key)) == NULL)
936 return m_pStrings->GetString(*pkey);
939 //memory addresses below 0x10000 are automatically considered invalid for dereferencing
940 #define VALID_MINIMUM_MEMORY_ADDRESS 0x10000
942 bool CGameConfig::GetAddress(const char *key, void **retaddr)
944 AddressConf *addrConf;
946 addrConf = m_pAddresses->retrieve(key);
954 if (!GetMemSig(addrConf->signatureName, &addr))
960 for (int i = 0; i < addrConf->readCount; ++i)
962 int offset = addrConf->read[i];
964 //NULLs in the middle of an indirection chain are bad, end NULL is ok
965 if (addr == NULL || reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
970 addr = *(reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(addr) + offset));
977 static inline unsigned minOf(unsigned a, unsigned b)
979 return a <= b ? a : b;
982 CGameConfig::AddressConf::AddressConf(char *sigName, unsigned sigLength, unsigned readCount, int *read)
984 unsigned readLimit = minOf(readCount, sizeof(this->read) / sizeof(this->read[0]));
986 strncopy(signatureName, sigName, sizeof(signatureName) / sizeof(signatureName[0]));
987 this->readCount = readLimit;
988 memcpy(&this->read[0], read, sizeof(this->read[0])*readLimit);
991 SendProp *CGameConfig::GetSendProp(const char *key)
995 if ((pProp = m_Props.retrieve(key)) == NULL)
1001 bool CGameConfig::GetMemSig(const char *key, void **addr)
1004 if ((paddr = m_Sigs.retrieve(key)) == NULL)
1010 void CGameConfig::IncRefCount()
1015 unsigned int CGameConfig::DecRefCount()
1021 GameConfigManager::GameConfigManager()
1025 GameConfigManager::~GameConfigManager()
1029 void GameConfigManager::OnSourceModStartup(bool late)
1031 LoadGameConfigFile("core.games", &g_pGameConf, NULL, 0);
1033 strncopy(g_Game, g_pSM->GetGameFolderName(), sizeof(g_Game));
1034 strncopy(g_GameDesc + 1, smcore.GetGameDescription(), sizeof(g_GameDesc) - 1);
1035 smcore.GetGameName(g_GameName + 1, sizeof(g_GameName) - 1);
1038 void GameConfigManager::OnSourceModAllInitialized()
1040 /* NOW initialize the game file */
1041 CGameConfig *pGameConf = (CGameConfig *)g_pGameConf;
1044 if (!pGameConf->Reparse(error, sizeof(error)))
1049 sharesys->AddInterface(NULL, this);
1052 void GameConfigManager::OnSourceModAllShutdown()
1054 CloseGameConfigFile(g_pGameConf);
1057 bool GameConfigManager::LoadGameConfigFile(const char *file, IGameConfig **_pConfig, char *error, size_t maxlength)
1060 /* A crash was detected during last load - We block the gamedata loading so it hopefully won't happen again */
1061 if (g_blockGameDataLoad)
1063 UTIL_Format(error, maxlength, "GameData loaded blocked due to detected crash");
1068 CGameConfig *pConfig;
1069 CGameConfig **ppConfig;
1071 if ((ppConfig = m_Lookup.retrieve(file)) != NULL)
1073 pConfig = *ppConfig;
1074 pConfig->IncRefCount();
1075 *_pConfig = pConfig;
1079 pConfig = new CGameConfig(file);
1081 /* :HACKHACK: Don't parse the main config file */
1083 if (_pConfig != &g_pGameConf)
1085 retval = pConfig->Reparse(error, maxlength);
1088 m_cfgs.push_back(pConfig);
1089 m_Lookup.insert(file, pConfig);
1091 pConfig->IncRefCount();
1093 *_pConfig = pConfig;
1098 void GameConfigManager::CloseGameConfigFile(IGameConfig *cfg)
1100 CGameConfig *pConfig = (CGameConfig *)cfg;
1102 if (pConfig->DecRefCount() == 0)
1104 m_Lookup.remove(pConfig->m_File);
1109 extern Handle_t g_GameConfigsType;
1111 IGameConfig *GameConfigManager::ReadHandle(Handle_t hndl, IdentityToken_t *ident, HandleError *err)
1113 HandleSecurity sec(ident, g_pCoreIdent);
1114 IGameConfig *conf = NULL;
1115 HandleError _err = handlesys->ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&conf);
1125 void GameConfigManager::AddUserConfigHook(const char *sectionname, ITextListener_SMC *listener)
1127 m_customHandlers.insert(sectionname, listener);
1130 void GameConfigManager::RemoveUserConfigHook(const char *sectionname, ITextListener_SMC *listener)
1132 ITextListener_SMC **pListener = m_customHandlers.retrieve(sectionname);
1134 if (pListener == NULL)
1139 if (*pListener != listener)
1144 m_customHandlers.remove(sectionname);
1149 void GameConfigManager::AcquireLock()
1153 void GameConfigManager::ReleaseLock()