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>.
32 #include "GameConfigs.h"
33 #include "sm_stringutil.h"
34 #include "sourcemod.h"
35 #include "sourcemm_api.h"
36 #include "HalfLife2.h"
39 #include "MemoryUtils.h"
40 #include "LibrarySys.h"
41 #include "HandleSys.h"
42 #include "logic_bridge.h"
44 #if defined PLATFORM_LINUX
48 GameConfigManager g_GameConfigs;
49 IGameConfig *g_pGameConf = NULL;
50 static char g_Game[256];
51 static char g_GameDesc[256] = {'!', '\0'};
52 static char g_GameName[256] = {'$', '\0'};
55 #define PSTATE_GAMES 1
56 #define PSTATE_GAMEDEFS 2
57 #define PSTATE_GAMEDEFS_OFFSETS 3
58 #define PSTATE_GAMEDEFS_OFFSETS_OFFSET 4
59 #define PSTATE_GAMEDEFS_KEYS 5
60 #define PSTATE_GAMEDEFS_SUPPORTED 6
61 #define PSTATE_GAMEDEFS_SIGNATURES 7
62 #define PSTATE_GAMEDEFS_SIGNATURES_SIG 8
63 #define PSTATE_GAMEDEFS_CRC 9
64 #define PSTATE_GAMEDEFS_CRC_BINARY 10
65 #define PSTATE_GAMEDEFS_CUSTOM 11
66 #define PSTATE_GAMEDEFS_ADDRESSES 12
67 #define PSTATE_GAMEDEFS_ADDRESSES_ADDRESS 13
68 #define PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ 14
70 #if defined PLATFORM_WINDOWS
71 #define PLATFORM_NAME "windows"
72 #define PLATFORM_SERVER_BINARY "server.dll"
73 #elif defined PLATFORM_LINUX
74 #define PLATFORM_NAME "linux"
75 #define PLATFORM_SERVER_BINARY "server_i486.so"
76 #elif defined PLATFORM_APPLE
77 #define PLATFORM_NAME "undef"
78 #define PLATFORM_SERVER_BINARY "undef.dylib"
91 unsigned int s_ServerBinCRC;
92 bool s_ServerBinCRC_Ok = false;
94 static bool DoesGameMatch(const char *value)
96 #if defined PLATFORM_WINDOWS
97 if (strcasecmp(value, g_Game) == 0 ||
99 if (strcmp(value, g_Game) == 0 ||
101 strcmp(value, g_GameDesc) == 0 ||
102 strcmp(value, g_GameName) == 0)
109 static bool DoesEngineMatch(const char *value)
111 #if SOURCE_ENGINE == SE_EPISODEONE
112 if (strcmp(value, "original") == 0)
113 #elif SOURCE_ENGINE == SE_DARKMESSIAH
114 if (strcmp(value, "darkmessiah") == 0)
115 #elif SOURCE_ENGINE == SE_ORANGEBOX
116 if (strcmp(value, "orangebox") == 0)
117 #elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
118 if (strcmp(value, "orangebox_valve") == 0)
119 #elif SOURCE_ENGINE == SE_LEFT4DEAD
120 if (strcmp(value, "left4dead") == 0)
121 #elif SOURCE_ENGINE == SE_LEFT4DEAD2
122 if (strcmp(value, "left4dead2") == 0)
124 #error "Unknown engine type"
132 CGameConfig::CGameConfig(const char *file)
134 strncopy(m_File, file, sizeof(m_File));
135 m_pOffsets = sm_trie_create();
136 m_pProps = sm_trie_create();
137 m_pKeys = sm_trie_create();
138 m_pAddresses = new KTrie<AddressConf>();
139 m_pSigs = sm_trie_create();
140 m_pStrings = new BaseStringTable(512);
144 m_CustomHandler = NULL;
147 CGameConfig::~CGameConfig()
149 sm_trie_destroy(m_pOffsets);
150 sm_trie_destroy(m_pProps);
151 sm_trie_destroy(m_pKeys);
153 sm_trie_destroy(m_pSigs);
157 SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *name)
162 return SMCResult_Continue;
165 switch (m_ParseState)
169 if (strcmp(name, "Games") == 0)
171 m_ParseState = PSTATE_GAMES;
179 if (strcmp(name, "*") == 0 ||
180 strcmp(name, "#default") == 0 ||
183 bShouldBeReadingDefault = true;
184 m_ParseState = PSTATE_GAMEDEFS;
185 strncopy(m_Game, name, sizeof(m_Game));
191 case PSTATE_GAMEDEFS:
193 if (strcmp(name, "Offsets") == 0)
195 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
197 else if (strcmp(name, "Keys") == 0)
199 m_ParseState = PSTATE_GAMEDEFS_KEYS;
201 else if ((strcmp(name, "#supported") == 0) && (strcmp(m_Game, "#default") == 0))
203 m_ParseState = PSTATE_GAMEDEFS_SUPPORTED;
204 /* Ignore this section unless we get a game. */
205 bShouldBeReadingDefault = false;
207 matched_game = false;
209 matched_engine = false;
211 else if (strcmp(name, "Signatures") == 0)
213 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
215 else if (strcmp(name, "CRC") == 0)
217 m_ParseState = PSTATE_GAMEDEFS_CRC;
218 bShouldBeReadingDefault = false;
220 else if (strcmp(name, "Addresses") == 0)
222 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
226 ITextListener_SMC **pListen = g_GameConfigs.m_customHandlers.retrieve(name);
231 m_ParseState = PSTATE_GAMEDEFS_CUSTOM;
232 m_CustomHandler = *pListen;
233 m_CustomHandler->ReadSMC_ParseStart();
241 case PSTATE_GAMEDEFS_OFFSETS:
245 strncopy(m_offset, name, sizeof(m_offset));
246 m_ParseState = PSTATE_GAMEDEFS_OFFSETS_OFFSET;
249 case PSTATE_GAMEDEFS_SIGNATURES:
251 strncopy(m_offset, name, sizeof(m_offset));
253 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES_SIG;
256 case PSTATE_GAMEDEFS_CRC:
260 if (strcmp(name, "server") != 0)
262 UTIL_Format(error, sizeof(error), "Unrecognized library \"%s\"", name);
264 else if (!s_ServerBinCRC_Ok)
267 char path[PLATFORM_MAX_PATH];
269 g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "bin/" PLATFORM_SERVER_BINARY);
270 if ((fp = fopen(path, "rb")) == NULL)
272 UTIL_Format(error, sizeof(error), "Could not open binary: %s", path);
277 fseek(fp, 0, SEEK_END);
279 fseek(fp, 0, SEEK_SET);
281 buffer = malloc(size);
282 fread(buffer, size, 1, fp);
283 s_ServerBinCRC = UTIL_CRC32(buffer, size);
285 s_ServerBinCRC_Ok = true;
289 if (error[0] != '\0')
292 g_Logger.LogError("[SM] Error while parsing CRC section for \"%s\" (%s):", m_Game, m_CurFile);
293 g_Logger.LogError("[SM] %s", error);
295 m_ParseState = PSTATE_GAMEDEFS_CRC_BINARY;
299 case PSTATE_GAMEDEFS_CUSTOM:
302 return m_CustomHandler->ReadSMC_NewSection(states, name);
305 case PSTATE_GAMEDEFS_ADDRESSES:
308 m_AddressSignature[0] = '\0';
309 m_AddressReadCount = 0;
311 strncopy(m_Address, name, sizeof(m_Address));
312 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
316 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS:
318 if (strcmp(name, PLATFORM_NAME) == 0)
320 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ;
324 if (strcmp(name, "linux") != 0 && strcmp(name, "windows") != 0)
326 g_Logger.LogError("[SM] Error while parsing Address section for \"%s\" (%s):", m_Address, m_CurFile);
327 g_Logger.LogError("[SM] Unrecognized platform \"%s\"", name);
333 /* No sub-sections allowed:
334 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
335 case PSTATE_GAMEDEFS_KEYS:
336 case PSTATE_GAMEDEFS_SUPPORTED:
337 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
338 case PSTATE_GAMEDEFS_CRC_BINARY:
339 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ:
343 /* If we don't know what we got, start ignoring */
349 return SMCResult_Continue;
352 SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
356 return SMCResult_Continue;
359 if (m_ParseState == PSTATE_GAMEDEFS_OFFSETS_OFFSET)
361 if (strcmp(key, "class") == 0)
363 strncopy(m_Class, value, sizeof(m_Class));
364 } else if (strcmp(key, "prop") == 0) {
365 strncopy(m_Prop, value, sizeof(m_Prop));
366 } else if (strcmp(key, PLATFORM_NAME) == 0) {
367 int val = atoi(value);
368 sm_trie_replace(m_pOffsets, m_offset, (void *)val);
370 } else if (m_ParseState == PSTATE_GAMEDEFS_KEYS) {
371 int id = m_pStrings->AddString(value);
372 sm_trie_replace(m_pKeys, key, (void *)id);
373 } else if (m_ParseState == PSTATE_GAMEDEFS_SUPPORTED) {
374 if (strcmp(key, "game") == 0)
377 if (DoesGameMatch(value))
381 if ((!had_engine && matched_game) || (matched_engine && matched_game))
383 bShouldBeReadingDefault = true;
386 else if (strcmp(key, "engine") == 0)
389 if (DoesEngineMatch(value))
391 matched_engine = true;
393 if ((!had_game && matched_engine) || (matched_game && matched_engine))
395 bShouldBeReadingDefault = true;
398 } else if (m_ParseState == PSTATE_GAMEDEFS_SIGNATURES_SIG) {
399 if (strcmp(key, PLATFORM_NAME) == 0)
401 strncopy(s_TempSig.sig, value, sizeof(s_TempSig.sig));
402 } else if (strcmp(key, "library") == 0) {
403 strncopy(s_TempSig.library, value, sizeof(s_TempSig.library));
405 } else if (m_ParseState == PSTATE_GAMEDEFS_CRC_BINARY) {
406 if (strcmp(key, PLATFORM_NAME) == 0
408 && !bShouldBeReadingDefault)
410 unsigned int crc = 0;
411 sscanf(value, "%08X", &crc);
412 if (s_ServerBinCRC == crc)
414 bShouldBeReadingDefault = true;
417 } else if (m_ParseState == PSTATE_GAMEDEFS_ADDRESSES_ADDRESS || m_ParseState == PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ) {
418 if (strcmp(key, "read") == 0) {
419 int limit = sizeof(m_AddressRead)/sizeof(m_AddressRead[0]);
420 if (m_AddressReadCount < limit)
422 m_AddressRead[m_AddressReadCount] = atoi(value);
423 m_AddressReadCount++;
427 g_Logger.LogError("[SM] Error parsing Address \"%s\", does not support more than %d read offsets (gameconf \"%s\")", m_Address, limit, m_CurFile);
429 } else if (strcmp(key, "signature") == 0) {
430 strncopy(m_AddressSignature, value, sizeof(m_AddressSignature));
432 } else if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM) {
433 return m_CustomHandler->ReadSMC_KeyValue(states, key, value);
436 return SMCResult_Continue;
439 SMCResult CGameConfig::ReadSMC_LeavingSection(const SMCStates *states)
444 return SMCResult_Continue;
450 m_CustomHandler->ReadSMC_LeavingSection(states);
451 return SMCResult_Continue;
454 switch (m_ParseState)
458 m_ParseState = PSTATE_NONE;
461 case PSTATE_GAMEDEFS:
463 m_ParseState = PSTATE_GAMES;
466 case PSTATE_GAMEDEFS_CUSTOM:
468 m_ParseState = PSTATE_GAMEDEFS;
469 m_CustomHandler->ReadSMC_ParseEnd(false, false);
472 case PSTATE_GAMEDEFS_KEYS:
473 case PSTATE_GAMEDEFS_OFFSETS:
475 m_ParseState = PSTATE_GAMEDEFS;
478 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
480 /* Parse the offset... */
481 if (m_Class[0] != '\0'
482 && m_Prop[0] != '\0')
484 SendProp *pProp = g_HL2.FindInSendTable(m_Class, m_Prop);
487 int val = pProp->GetOffset();
488 sm_trie_replace(m_pOffsets, m_offset, (void *)val);
489 sm_trie_replace(m_pProps, m_offset, pProp);
491 /* Check if it's a non-default game and no offsets exist */
492 if (((strcmp(m_Game, "*") != 0) && strcmp(m_Game, "#default") != 0)
493 && (!sm_trie_retrieve(m_pOffsets, m_offset, NULL)))
495 g_Logger.LogError("[SM] Unable to find property %s.%s (file \"%s\") (mod \"%s\")",
503 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
506 case PSTATE_GAMEDEFS_CRC:
507 case PSTATE_GAMEDEFS_SUPPORTED:
509 if (!bShouldBeReadingDefault)
511 /* If we shouldn't read the rest of this section, set the ignore level. */
513 m_ParseState = PSTATE_GAMES;
515 m_ParseState = PSTATE_GAMEDEFS;
519 case PSTATE_GAMEDEFS_CRC_BINARY:
521 m_ParseState = PSTATE_GAMEDEFS_CRC;
524 case PSTATE_GAMEDEFS_SIGNATURES:
526 m_ParseState = PSTATE_GAMEDEFS;
529 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
531 if (s_TempSig.library[0] == '\0')
534 strncopy(s_TempSig.library, "server", sizeof(s_TempSig.library));
536 void *addrInBase = NULL;
537 if (strcmp(s_TempSig.library, "server") == 0)
539 addrInBase = (void *)g_SMAPI->GetServerFactory(false);
540 } else if (strcmp(s_TempSig.library, "engine") == 0) {
541 addrInBase = (void *)g_SMAPI->GetEngineFactory(false);
543 void *final_addr = NULL;
544 if (addrInBase == NULL)
546 g_Logger.LogError("[SM] Unrecognized library \"%s\" (gameconf \"%s\")",
550 #if defined PLATFORM_LINUX
551 if (s_TempSig.sig[0] == '@')
554 /* GNU only: returns 0 on error, inconsistent! >:[ */
555 if (dladdr(addrInBase, &info) != 0)
557 void *handle = dlopen(info.dli_fname, RTLD_NOW);
560 #if (SOURCE_ENGINE == SE_ORANGEBOXVALVE) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
561 final_addr = g_MemUtils.ResolveSymbol(handle, &s_TempSig.sig[1]);
563 final_addr = dlsym(handle, &s_TempSig.sig[1]);
567 g_Logger.LogError("[SM] Unable to load library \"%s\" (gameconf \"%s\")",
572 g_Logger.LogError("[SM] Unable to find library \"%s\" in memory (gameconf \"%s\")",
582 /* First, preprocess the signature */
583 unsigned char real_sig[511];
588 length = strlen(s_TempSig.sig);
590 real_bytes = UTIL_DecodeHexString(real_sig, sizeof(real_sig), s_TempSig.sig);
594 final_addr = g_MemUtils.FindPattern(addrInBase, (char*)real_sig, real_bytes);
598 #if defined PLATFORM_LINUX
601 sm_trie_replace(m_pSigs, m_offset, final_addr);
602 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
606 case PSTATE_GAMEDEFS_ADDRESSES:
608 m_ParseState = PSTATE_GAMEDEFS;
611 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS:
613 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
615 if (m_Address[0] == '\0')
617 g_Logger.LogError("[SM] Address sections must have names (gameconf \"%s\")", m_CurFile);
620 if (m_AddressSignature[0] == '\0')
622 g_Logger.LogError("[SM] Address section for \"%s\" did not specify a signature (gameconf \"%s\")", m_Address, m_CurFile);
626 AddressConf addrConf(m_AddressSignature, sizeof(m_AddressSignature), m_AddressReadCount, m_AddressRead);
627 m_pAddresses->replace(m_Address, addrConf);
631 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ:
633 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
638 return SMCResult_Continue;
641 #define MSTATE_NONE 0
642 #define MSTATE_MAIN 1
643 #define MSTATE_FILE 2
645 class MasterReader : public ITextListener_SMC
648 virtual void ReadSMC_ParseStart()
654 virtual SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name)
658 return SMCResult_Continue;
661 if (state == MSTATE_NONE)
663 if (strcmp(name, "Game Master") == 0)
672 else if (state == MSTATE_MAIN)
674 strncopy(cur_file, name, sizeof(cur_file));
676 matched_engine = false;
678 matched_game = false;
681 else if (state == MSTATE_FILE)
686 return SMCResult_Continue;
689 virtual SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
691 if (ignoreLevel || state != MSTATE_FILE)
693 return SMCResult_Continue;
696 if (strcmp(key, "engine") == 0)
699 if (DoesEngineMatch(value))
701 matched_engine = true;
704 else if (strcmp(key, "game") == 0)
707 if (DoesGameMatch(value))
713 return SMCResult_Continue;
716 virtual SMCResult ReadSMC_LeavingSection(const SMCStates *states)
721 return SMCResult_Continue;
724 if (state == MSTATE_FILE)
726 /* The four success conditions:
728 * 2. Needed game only.
729 * 3. Needed engine only.
730 * 4. Needed both engine and game.
731 * Final result is minimized via k-map.
734 if ((!had_engine && !had_game) ||
735 (!had_engine && (had_game && matched_game)) ||
736 (!had_game && (had_engine && matched_engine)) ||
737 ((had_game && had_engine) && (matched_game && matched_engine)))
739 if ((!had_engine && !had_game) ||
740 (!had_engine && matched_game) ||
741 (!had_game && matched_engine) ||
742 (matched_engine && matched_game))
744 fileList->push_back(cur_file);
748 else if (state == MSTATE_MAIN)
753 return SMCResult_Continue;
756 List<String> *fileList;
758 unsigned int ignoreLevel;
759 char cur_file[PLATFORM_MAX_PATH];
766 static MasterReader master_reader;
768 bool CGameConfig::Reparse(char *error, size_t maxlength)
770 /* Reset cached data */
772 sm_trie_clear(m_pOffsets);
773 sm_trie_clear(m_pProps);
774 sm_trie_clear(m_pKeys);
775 m_pAddresses->clear();
777 char path[PLATFORM_MAX_PATH];
779 /* See if we can use the extended gamedata format. */
780 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/master.games.txt", m_File);
781 if (!g_LibSys.PathExists(path))
783 /* Nope, use the old mechanism. */
784 UTIL_Format(path, sizeof(path), "%s.txt", m_File);
785 return EnterFile(path, error, maxlength);
788 /* Otherwise, it's time to parse the master. */
790 SMCStates state = {0, 0};
791 List<String> fileList;
792 master_reader.fileList = &fileList;
794 err = textparsers->ParseSMCFile(path, &master_reader, &state, error, maxlength);
795 if (err != SMCError_Okay)
797 const char *msg = textparsers->GetSMCErrorString(err);
799 g_Logger.LogError("[SM] Error parsing master gameconf file \"%s\":", path);
800 g_Logger.LogError("[SM] Error %d on line %d, col %d: %s",
804 msg ? msg : "Unknown error");
808 /* Go through each file we found and parse it. */
809 List<String>::iterator iter;
810 for (iter = fileList.begin(); iter != fileList.end(); iter++)
812 UTIL_Format(path, sizeof(path), "%s/%s", m_File, (*iter).c_str());
813 if (!EnterFile(path, error, maxlength))
819 /* Parse the contents of the 'custom' directory */
820 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/custom", m_File);
821 IDirectory *customDir = g_LibSys.OpenDirectory(path);
828 while (customDir->MoreFiles())
830 if (!customDir->IsEntryFile())
832 customDir->NextEntry();
836 const char *curFile = customDir->GetEntryName();
838 /* Only allow .txt files */
839 int len = strlen(curFile);
840 if (len > 4 && strcmp(&curFile[len-4], ".txt") != 0)
842 customDir->NextEntry();
846 UTIL_Format(path, sizeof(path), "%s/custom/%s", m_File, curFile);
847 if (!EnterFile(path, error, maxlength))
849 g_LibSys.CloseDirectory(customDir);
853 customDir->NextEntry();
856 g_LibSys.CloseDirectory(customDir);
860 bool CGameConfig::EnterFile(const char *file, char *error, size_t maxlength)
863 SMCStates state = {0, 0};
865 g_SourceMod.BuildPath(Path_SM, m_CurFile, sizeof(m_CurFile), "gamedata/%s", file);
867 /* Initialize parse states */
869 bShouldBeReadingDefault = true;
870 m_ParseState = PSTATE_NONE;
872 if ((err=textparsers->ParseSMCFile(m_CurFile, this, &state, error, maxlength))
877 msg = textparsers->GetSMCErrorString(err);
879 g_Logger.LogError("[SM] Error parsing gameconfig file \"%s\":", m_CurFile);
880 g_Logger.LogError("[SM] Error %d on line %d, col %d: %s",
884 msg ? msg : "Unknown error");
886 if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM)
888 //error occurred while parsing a custom section
889 m_CustomHandler->ReadSMC_ParseEnd(true, true);
890 m_CustomHandler = NULL;
900 bool CGameConfig::GetOffset(const char *key, int *value)
904 if (!sm_trie_retrieve(m_pOffsets, key, &obj))
914 const char *CGameConfig::GetKeyValue(const char *key)
917 if (!sm_trie_retrieve(m_pKeys, key, &obj))
921 return m_pStrings->GetString((int)obj);
924 //memory addresses below 0x10000 are automatically considered invalid for dereferencing
925 #define VALID_MINIMUM_MEMORY_ADDRESS 0x10000
927 bool CGameConfig::GetAddress(const char *key, void **retaddr)
929 AddressConf *addrConf;
931 addrConf = m_pAddresses->retrieve(key);
939 if (!GetMemSig(addrConf->signatureName, &addr))
945 for (int i = 0; i < addrConf->readCount; ++i)
947 int offset = addrConf->read[i];
949 //NULLs in the middle of an indirection chain are bad, end NULL is ok
950 if (addr == NULL || reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
955 addr = *(reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(addr) + offset));
962 CGameConfig::AddressConf::AddressConf(char *sigName, unsigned sigLength, unsigned readCount, int *read)
964 unsigned readLimit = min(readCount, sizeof(this->read) / sizeof(this->read[0]));
966 strncopy(signatureName, sigName, sizeof(signatureName) / sizeof(signatureName[0]));
967 this->readCount = readLimit;
968 memcpy(&this->read[0], read, sizeof(this->read[0])*readLimit);
971 SendProp *CGameConfig::GetSendProp(const char *key)
975 if (!sm_trie_retrieve(m_pProps, key, (void **)&pProp))
983 bool CGameConfig::GetMemSig(const char *key, void **addr)
985 return sm_trie_retrieve(m_pSigs, key, addr);
988 void CGameConfig::IncRefCount()
993 unsigned int CGameConfig::DecRefCount()
999 GameConfigManager::GameConfigManager()
1001 m_pLookup = sm_trie_create();
1004 GameConfigManager::~GameConfigManager()
1006 sm_trie_destroy(m_pLookup);
1009 void GameConfigManager::OnSourceModStartup(bool late)
1011 LoadGameConfigFile("core.games", &g_pGameConf, NULL, 0);
1013 strncopy(g_Game, g_SourceMod.GetGameFolderName(), sizeof(g_Game));
1014 strncopy(g_GameDesc + 1, SERVER_CALL(GetGameDescription)(), sizeof(g_GameDesc) - 1);
1016 KeyValues *pGameInfo = new KeyValues("GameInfo");
1017 if (g_HL2.KVLoadFromFile(pGameInfo, basefilesystem, "gameinfo.txt"))
1020 if ((str = pGameInfo->GetString("game", NULL)) != NULL)
1022 strncopy(g_GameName + 1, str, sizeof(g_GameName) - 1);
1025 pGameInfo->deleteThis();
1028 void GameConfigManager::OnSourceModAllInitialized()
1030 /* NOW initialize the game file */
1031 CGameConfig *pGameConf = (CGameConfig *)g_pGameConf;
1034 if (!pGameConf->Reparse(error, sizeof(error)))
1039 g_ShareSys.AddInterface(NULL, this);
1042 void GameConfigManager::OnSourceModAllShutdown()
1044 CloseGameConfigFile(g_pGameConf);
1047 bool GameConfigManager::LoadGameConfigFile(const char *file, IGameConfig **_pConfig, char *error, size_t maxlength)
1050 /* A crash was detected during last load - We block the gamedata loading so it hopefully won't happen again */
1051 if (g_blockGameDataLoad)
1053 UTIL_Format(error, maxlength, "GameData loaded blocked due to detected crash");
1058 CGameConfig *pConfig;
1060 if (sm_trie_retrieve(m_pLookup, file, (void **)&pConfig))
1062 pConfig->IncRefCount();
1063 *_pConfig = pConfig;
1067 pConfig = new CGameConfig(file);
1069 /* :HACKHACK: Don't parse the main config file */
1071 if (_pConfig != &g_pGameConf)
1073 retval = pConfig->Reparse(error, maxlength);
1076 m_cfgs.push_back(pConfig);
1077 sm_trie_insert(m_pLookup, file, pConfig);
1079 pConfig->IncRefCount();
1081 *_pConfig = pConfig;
1086 void GameConfigManager::CloseGameConfigFile(IGameConfig *cfg)
1088 CGameConfig *pConfig = (CGameConfig *)cfg;
1090 if (pConfig->DecRefCount() == 0)
1092 sm_trie_delete(m_pLookup, pConfig->m_File);
1097 extern HandleType_t g_GameConfigsType;
1099 IGameConfig *GameConfigManager::ReadHandle(Handle_t hndl, IdentityToken_t *ident, HandleError *err)
1101 HandleSecurity sec(ident, g_pCoreIdent);
1102 IGameConfig *conf = NULL;
1103 HandleError _err = g_HandleSys.ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&conf);
1113 void GameConfigManager::AddUserConfigHook(const char *sectionname, ITextListener_SMC *listener)
1115 m_customHandlers.insert(sectionname, listener);
1118 void GameConfigManager::RemoveUserConfigHook(const char *sectionname, ITextListener_SMC *listener)
1120 ITextListener_SMC **pListener = m_customHandlers.retrieve(sectionname);
1122 if (pListener == NULL)
1127 if (*pListener != listener)
1132 m_customHandlers.remove(sectionname);
1137 void GameConfigManager::AcquireLock()
1141 void GameConfigManager::ReleaseLock()