Fixed copy-paste error in 116256b519b5.
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'};
59 #define PSTATE_GAMES 1
60 #define PSTATE_GAMEDEFS 2
61 #define PSTATE_GAMEDEFS_OFFSETS 3
62 #define PSTATE_GAMEDEFS_OFFSETS_OFFSET 4
63 #define PSTATE_GAMEDEFS_KEYS 5
64 #define PSTATE_GAMEDEFS_SUPPORTED 6
65 #define PSTATE_GAMEDEFS_SIGNATURES 7
66 #define PSTATE_GAMEDEFS_SIGNATURES_SIG 8
67 #define PSTATE_GAMEDEFS_CRC 9
68 #define PSTATE_GAMEDEFS_CRC_BINARY 10
69 #define PSTATE_GAMEDEFS_CUSTOM 11
70 #define PSTATE_GAMEDEFS_ADDRESSES 12
71 #define PSTATE_GAMEDEFS_ADDRESSES_ADDRESS 13
72 #define PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ 14
74 #if defined PLATFORM_WINDOWS
75 #define PLATFORM_NAME "windows"
76 #define PLATFORM_SERVER_BINARY "server.dll"
77 #elif defined PLATFORM_LINUX
78 #define PLATFORM_NAME "linux"
79 #define PLATFORM_SERVER_BINARY "server_i486.so"
80 #elif defined PLATFORM_APPLE
81 #define PLATFORM_NAME "mac"
82 #define PLATFORM_SERVER_BINARY "server.dylib"
95 unsigned int s_ServerBinCRC;
96 bool s_ServerBinCRC_Ok = false;
98 static bool DoesGameMatch(const char *value)
100 #if defined PLATFORM_WINDOWS
101 if (strcasecmp(value, g_Game) == 0 ||
103 if (strcmp(value, g_Game) == 0 ||
105 strcmp(value, g_GameDesc) == 0 ||
106 strcmp(value, g_GameName) == 0)
113 static bool DoesEngineMatch(const char *value)
115 return strcmp(value, smcore.GetSourceEngineName()) == 0;
118 CGameConfig::CGameConfig(const char *file)
120 strncopy(m_File, file, sizeof(m_File));
121 m_pAddresses = new KTrie<AddressConf>();
122 m_pStrings = new BaseStringTable(512);
126 m_CustomHandler = NULL;
129 CGameConfig::~CGameConfig()
135 SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *name)
140 return SMCResult_Continue;
143 switch (m_ParseState)
147 if (strcmp(name, "Games") == 0)
149 m_ParseState = PSTATE_GAMES;
157 if (strcmp(name, "*") == 0 ||
158 strcmp(name, "#default") == 0 ||
161 bShouldBeReadingDefault = true;
162 m_ParseState = PSTATE_GAMEDEFS;
163 strncopy(m_Game, name, sizeof(m_Game));
169 case PSTATE_GAMEDEFS:
171 if (strcmp(name, "Offsets") == 0)
173 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
175 else if (strcmp(name, "Keys") == 0)
177 m_ParseState = PSTATE_GAMEDEFS_KEYS;
179 else if ((strcmp(name, "#supported") == 0) && (strcmp(m_Game, "#default") == 0))
181 m_ParseState = PSTATE_GAMEDEFS_SUPPORTED;
182 /* Ignore this section unless we get a game. */
183 bShouldBeReadingDefault = false;
185 matched_game = false;
187 matched_engine = false;
189 else if (strcmp(name, "Signatures") == 0)
191 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
193 else if (strcmp(name, "CRC") == 0)
195 m_ParseState = PSTATE_GAMEDEFS_CRC;
196 bShouldBeReadingDefault = false;
198 else if (strcmp(name, "Addresses") == 0)
200 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
204 ITextListener_SMC **pListen = g_GameConfigs.m_customHandlers.retrieve(name);
209 m_ParseState = PSTATE_GAMEDEFS_CUSTOM;
210 m_CustomHandler = *pListen;
211 m_CustomHandler->ReadSMC_ParseStart();
219 case PSTATE_GAMEDEFS_OFFSETS:
223 strncopy(m_offset, name, sizeof(m_offset));
224 m_ParseState = PSTATE_GAMEDEFS_OFFSETS_OFFSET;
227 case PSTATE_GAMEDEFS_SIGNATURES:
229 strncopy(m_offset, name, sizeof(m_offset));
231 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES_SIG;
234 case PSTATE_GAMEDEFS_CRC:
238 if (strcmp(name, "server") != 0)
240 smcore.Format(error, sizeof(error), "Unrecognized library \"%s\"", name);
242 else if (!s_ServerBinCRC_Ok)
245 char path[PLATFORM_MAX_PATH];
247 g_pSM->BuildPath(Path_Game, path, sizeof(path), "bin/" PLATFORM_SERVER_BINARY);
248 if ((fp = fopen(path, "rb")) == NULL)
250 smcore.Format(error, sizeof(error), "Could not open binary: %s", path);
255 fseek(fp, 0, SEEK_END);
257 fseek(fp, 0, SEEK_SET);
259 buffer = malloc(size);
260 fread(buffer, size, 1, fp);
261 s_ServerBinCRC = UTIL_CRC32(buffer, size);
263 s_ServerBinCRC_Ok = true;
267 if (error[0] != '\0')
270 smcore.LogError("[SM] Error while parsing CRC section for \"%s\" (%s):", m_Game, m_CurFile);
271 smcore.LogError("[SM] %s", error);
273 m_ParseState = PSTATE_GAMEDEFS_CRC_BINARY;
277 case PSTATE_GAMEDEFS_CUSTOM:
280 return m_CustomHandler->ReadSMC_NewSection(states, name);
283 case PSTATE_GAMEDEFS_ADDRESSES:
286 m_AddressSignature[0] = '\0';
287 m_AddressReadCount = 0;
289 strncopy(m_Address, name, sizeof(m_Address));
290 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
294 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS:
296 if (strcmp(name, PLATFORM_NAME) == 0)
298 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ;
302 if (strcmp(name, "linux") != 0 && strcmp(name, "windows") != 0 && strcmp(name, "mac") != 0)
304 smcore.LogError("[SM] Error while parsing Address section for \"%s\" (%s):", m_Address, m_CurFile);
305 smcore.LogError("[SM] Unrecognized platform \"%s\"", name);
311 /* No sub-sections allowed:
312 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
313 case PSTATE_GAMEDEFS_KEYS:
314 case PSTATE_GAMEDEFS_SUPPORTED:
315 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
316 case PSTATE_GAMEDEFS_CRC_BINARY:
317 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ:
321 /* If we don't know what we got, start ignoring */
327 return SMCResult_Continue;
330 SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
334 return SMCResult_Continue;
337 if (m_ParseState == PSTATE_GAMEDEFS_OFFSETS_OFFSET)
339 if (strcmp(key, "class") == 0)
341 strncopy(m_Class, value, sizeof(m_Class));
342 } else if (strcmp(key, "prop") == 0) {
343 strncopy(m_Prop, value, sizeof(m_Prop));
344 } else if (strcmp(key, PLATFORM_NAME) == 0) {
345 m_Offsets.replace(m_offset, atoi(value));
347 } else if (m_ParseState == PSTATE_GAMEDEFS_KEYS) {
348 m_Keys.replace(key, m_pStrings->AddString(value));
349 } else if (m_ParseState == PSTATE_GAMEDEFS_SUPPORTED) {
350 if (strcmp(key, "game") == 0)
353 if (DoesGameMatch(value))
357 if ((!had_engine && matched_game) || (matched_engine && matched_game))
359 bShouldBeReadingDefault = true;
362 else if (strcmp(key, "engine") == 0)
365 if (DoesEngineMatch(value))
367 matched_engine = true;
369 if ((!had_game && matched_engine) || (matched_game && matched_engine))
371 bShouldBeReadingDefault = true;
374 } else if (m_ParseState == PSTATE_GAMEDEFS_SIGNATURES_SIG) {
375 if (strcmp(key, PLATFORM_NAME) == 0)
377 strncopy(s_TempSig.sig, value, sizeof(s_TempSig.sig));
378 } else if (strcmp(key, "library") == 0) {
379 strncopy(s_TempSig.library, value, sizeof(s_TempSig.library));
381 } else if (m_ParseState == PSTATE_GAMEDEFS_CRC_BINARY) {
382 if (strcmp(key, PLATFORM_NAME) == 0
384 && !bShouldBeReadingDefault)
386 unsigned int crc = 0;
387 sscanf(value, "%08X", &crc);
388 if (s_ServerBinCRC == crc)
390 bShouldBeReadingDefault = true;
393 } else if (m_ParseState == PSTATE_GAMEDEFS_ADDRESSES_ADDRESS || m_ParseState == PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ) {
394 if (strcmp(key, "read") == 0) {
395 int limit = sizeof(m_AddressRead)/sizeof(m_AddressRead[0]);
396 if (m_AddressReadCount < limit)
398 m_AddressRead[m_AddressReadCount] = atoi(value);
399 m_AddressReadCount++;
403 smcore.LogError("[SM] Error parsing Address \"%s\", does not support more than %d read offsets (gameconf \"%s\")", m_Address, limit, m_CurFile);
405 } else if (strcmp(key, "signature") == 0) {
406 strncopy(m_AddressSignature, value, sizeof(m_AddressSignature));
408 } else if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM) {
409 return m_CustomHandler->ReadSMC_KeyValue(states, key, value);
412 return SMCResult_Continue;
415 SMCResult CGameConfig::ReadSMC_LeavingSection(const SMCStates *states)
420 return SMCResult_Continue;
426 m_CustomHandler->ReadSMC_LeavingSection(states);
427 return SMCResult_Continue;
430 switch (m_ParseState)
434 m_ParseState = PSTATE_NONE;
437 case PSTATE_GAMEDEFS:
439 m_ParseState = PSTATE_GAMES;
442 case PSTATE_GAMEDEFS_CUSTOM:
444 m_ParseState = PSTATE_GAMEDEFS;
445 m_CustomHandler->ReadSMC_ParseEnd(false, false);
448 case PSTATE_GAMEDEFS_KEYS:
449 case PSTATE_GAMEDEFS_OFFSETS:
451 m_ParseState = PSTATE_GAMEDEFS;
454 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
456 /* Parse the offset... */
457 if (m_Class[0] != '\0'
458 && m_Prop[0] != '\0')
460 SendProp *pProp = gamehelpers->FindInSendTable(m_Class, m_Prop);
463 int val = gamehelpers->GetSendPropOffset(pProp);
464 m_Offsets.replace(m_offset, val);
465 m_Props.replace(m_offset, pProp);
467 /* Check if it's a non-default game and no offsets exist */
468 if (((strcmp(m_Game, "*") != 0) && strcmp(m_Game, "#default") != 0)
469 && (!m_Offsets.retrieve(m_offset)))
471 smcore.LogError("[SM] Unable to find property %s.%s (file \"%s\") (mod \"%s\")",
479 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
482 case PSTATE_GAMEDEFS_CRC:
483 case PSTATE_GAMEDEFS_SUPPORTED:
485 if (!bShouldBeReadingDefault)
487 /* If we shouldn't read the rest of this section, set the ignore level. */
489 m_ParseState = PSTATE_GAMES;
491 m_ParseState = PSTATE_GAMEDEFS;
495 case PSTATE_GAMEDEFS_CRC_BINARY:
497 m_ParseState = PSTATE_GAMEDEFS_CRC;
500 case PSTATE_GAMEDEFS_SIGNATURES:
502 m_ParseState = PSTATE_GAMEDEFS;
505 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
507 if (s_TempSig.library[0] == '\0')
510 strncopy(s_TempSig.library, "server", sizeof(s_TempSig.library));
512 void *addrInBase = NULL;
513 if (strcmp(s_TempSig.library, "server") == 0)
515 addrInBase = smcore.serverFactory;
516 } else if (strcmp(s_TempSig.library, "engine") == 0) {
517 addrInBase = smcore.engineFactory;
518 } else if (strcmp(s_TempSig.library, "matchmaking_ds") == 0) {
519 addrInBase = smcore.matchmakingDSFactory;
521 void *final_addr = NULL;
522 if (addrInBase == NULL)
524 smcore.LogError("[SM] Unrecognized library \"%s\" (gameconf \"%s\")",
528 if (s_TempSig.sig[0] == '@')
530 #if defined PLATFORM_WINDOWS
531 MEMORY_BASIC_INFORMATION mem;
533 if (VirtualQuery(addrInBase, &mem, sizeof(mem)))
534 final_addr = g_MemUtils.ResolveSymbol(mem.AllocationBase, &s_TempSig.sig[1]);
536 smcore.LogError("[SM] Unable to find library \"%s\" in memory (gameconf \"%s\")", s_TempSig.library, m_File);
537 #elif defined PLATFORM_POSIX
539 /* GNU only: returns 0 on error, inconsistent! >:[ */
540 if (dladdr(addrInBase, &info) != 0)
542 void *handle = dlopen(info.dli_fname, RTLD_NOW);
545 if (smcore.SymbolsAreHidden())
546 final_addr = g_MemUtils.ResolveSymbol(handle, &s_TempSig.sig[1]);
548 final_addr = dlsym(handle, &s_TempSig.sig[1]);
551 smcore.LogError("[SM] Unable to load library \"%s\" (gameconf \"%s\")",
556 smcore.LogError("[SM] Unable to find library \"%s\" in memory (gameconf \"%s\")",
566 /* First, preprocess the signature */
567 unsigned char real_sig[511];
572 length = strlen(s_TempSig.sig);
574 real_bytes = UTIL_DecodeHexString(real_sig, sizeof(real_sig), s_TempSig.sig);
578 final_addr = g_MemUtils.FindPattern(addrInBase, (char*)real_sig, real_bytes);
583 m_Sigs.replace(m_offset, final_addr);
584 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
588 case PSTATE_GAMEDEFS_ADDRESSES:
590 m_ParseState = PSTATE_GAMEDEFS;
593 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS:
595 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
597 if (m_Address[0] == '\0')
599 smcore.LogError("[SM] Address sections must have names (gameconf \"%s\")", m_CurFile);
602 if (m_AddressSignature[0] == '\0')
604 smcore.LogError("[SM] Address section for \"%s\" did not specify a signature (gameconf \"%s\")", m_Address, m_CurFile);
608 AddressConf addrConf(m_AddressSignature, sizeof(m_AddressSignature), m_AddressReadCount, m_AddressRead);
609 m_pAddresses->replace(m_Address, addrConf);
613 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ:
615 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
620 return SMCResult_Continue;
623 #define MSTATE_NONE 0
624 #define MSTATE_MAIN 1
625 #define MSTATE_FILE 2
627 class MasterReader : public ITextListener_SMC
630 virtual void ReadSMC_ParseStart()
636 virtual SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name)
640 return SMCResult_Continue;
643 if (state == MSTATE_NONE)
645 if (strcmp(name, "Game Master") == 0)
654 else if (state == MSTATE_MAIN)
656 strncopy(cur_file, name, sizeof(cur_file));
658 matched_engine = false;
660 matched_game = false;
663 else if (state == MSTATE_FILE)
668 return SMCResult_Continue;
671 virtual SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
673 if (ignoreLevel || state != MSTATE_FILE)
675 return SMCResult_Continue;
678 if (strcmp(key, "engine") == 0)
681 if (DoesEngineMatch(value))
683 matched_engine = true;
686 else if (strcmp(key, "game") == 0)
689 if (DoesGameMatch(value))
695 return SMCResult_Continue;
698 virtual SMCResult ReadSMC_LeavingSection(const SMCStates *states)
703 return SMCResult_Continue;
706 if (state == MSTATE_FILE)
708 /* The four success conditions:
710 * 2. Needed game only.
711 * 3. Needed engine only.
712 * 4. Needed both engine and game.
713 * Final result is minimized via k-map.
716 if ((!had_engine && !had_game) ||
717 (!had_engine && (had_game && matched_game)) ||
718 (!had_game && (had_engine && matched_engine)) ||
719 ((had_game && had_engine) && (matched_game && matched_engine)))
721 if ((!had_engine && !had_game) ||
722 (!had_engine && matched_game) ||
723 (!had_game && matched_engine) ||
724 (matched_engine && matched_game))
726 fileList->push_back(cur_file);
730 else if (state == MSTATE_MAIN)
735 return SMCResult_Continue;
738 List<String> *fileList;
740 unsigned int ignoreLevel;
741 char cur_file[PLATFORM_MAX_PATH];
748 static MasterReader master_reader;
750 bool CGameConfig::Reparse(char *error, size_t maxlength)
752 /* Reset cached data */
757 m_pAddresses->clear();
759 char path[PLATFORM_MAX_PATH];
761 /* See if we can use the extended gamedata format. */
762 g_pSM->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/master.games.txt", m_File);
763 if (!libsys->PathExists(path))
765 /* Nope, use the old mechanism. */
766 smcore.Format(path, sizeof(path), "%s.txt", m_File);
767 return EnterFile(path, error, maxlength);
770 /* Otherwise, it's time to parse the master. */
772 SMCStates state = {0, 0};
773 List<String> fileList;
774 master_reader.fileList = &fileList;
776 err = textparsers->ParseSMCFile(path, &master_reader, &state, error, maxlength);
777 if (err != SMCError_Okay)
779 const char *msg = textparsers->GetSMCErrorString(err);
781 smcore.LogError("[SM] Error parsing master gameconf file \"%s\":", path);
782 smcore.LogError("[SM] Error %d on line %d, col %d: %s",
786 msg ? msg : "Unknown error");
790 /* Go through each file we found and parse it. */
791 List<String>::iterator iter;
792 for (iter = fileList.begin(); iter != fileList.end(); iter++)
794 smcore.Format(path, sizeof(path), "%s/%s", m_File, (*iter).c_str());
795 if (!EnterFile(path, error, maxlength))
801 /* Parse the contents of the 'custom' directory */
802 g_pSM->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/custom", m_File);
803 IDirectory *customDir = libsys->OpenDirectory(path);
810 while (customDir->MoreFiles())
812 if (!customDir->IsEntryFile())
814 customDir->NextEntry();
818 const char *curFile = customDir->GetEntryName();
820 /* Only allow .txt files */
821 int len = strlen(curFile);
822 if (len > 4 && strcmp(&curFile[len-4], ".txt") != 0)
824 customDir->NextEntry();
828 smcore.Format(path, sizeof(path), "%s/custom/%s", m_File, curFile);
829 if (!EnterFile(path, error, maxlength))
831 libsys->CloseDirectory(customDir);
835 customDir->NextEntry();
838 libsys->CloseDirectory(customDir);
842 bool CGameConfig::EnterFile(const char *file, char *error, size_t maxlength)
845 SMCStates state = {0, 0};
847 g_pSM->BuildPath(Path_SM, m_CurFile, sizeof(m_CurFile), "gamedata/%s", file);
849 /* Initialize parse states */
851 bShouldBeReadingDefault = true;
852 m_ParseState = PSTATE_NONE;
854 if ((err=textparsers->ParseSMCFile(m_CurFile, this, &state, error, maxlength))
859 msg = textparsers->GetSMCErrorString(err);
861 smcore.LogError("[SM] Error parsing gameconfig file \"%s\":", m_CurFile);
862 smcore.LogError("[SM] Error %d on line %d, col %d: %s",
866 msg ? msg : "Unknown error");
868 if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM)
870 //error occurred while parsing a custom section
871 m_CustomHandler->ReadSMC_ParseEnd(true, true);
872 m_CustomHandler = NULL;
882 bool CGameConfig::GetOffset(const char *key, int *value)
885 if ((pvalue = m_Offsets.retrieve(key)) == NULL)
892 const char *CGameConfig::GetKeyValue(const char *key)
895 if ((pkey = m_Keys.retrieve(key)) == NULL)
897 return m_pStrings->GetString(*pkey);
900 //memory addresses below 0x10000 are automatically considered invalid for dereferencing
901 #define VALID_MINIMUM_MEMORY_ADDRESS 0x10000
903 bool CGameConfig::GetAddress(const char *key, void **retaddr)
905 AddressConf *addrConf;
907 addrConf = m_pAddresses->retrieve(key);
915 if (!GetMemSig(addrConf->signatureName, &addr))
921 for (int i = 0; i < addrConf->readCount; ++i)
923 int offset = addrConf->read[i];
925 //NULLs in the middle of an indirection chain are bad, end NULL is ok
926 if (addr == NULL || reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
931 addr = *(reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(addr) + offset));
938 static inline unsigned minOf(unsigned a, unsigned b)
940 return a <= b ? a : b;
943 CGameConfig::AddressConf::AddressConf(char *sigName, unsigned sigLength, unsigned readCount, int *read)
945 unsigned readLimit = minOf(readCount, sizeof(this->read) / sizeof(this->read[0]));
947 strncopy(signatureName, sigName, sizeof(signatureName) / sizeof(signatureName[0]));
948 this->readCount = readLimit;
949 memcpy(&this->read[0], read, sizeof(this->read[0])*readLimit);
952 SendProp *CGameConfig::GetSendProp(const char *key)
956 if ((pProp = m_Props.retrieve(key)) == NULL)
962 bool CGameConfig::GetMemSig(const char *key, void **addr)
965 if ((paddr = m_Sigs.retrieve(key)) == NULL)
971 void CGameConfig::IncRefCount()
976 unsigned int CGameConfig::DecRefCount()
982 GameConfigManager::GameConfigManager()
986 GameConfigManager::~GameConfigManager()
990 void GameConfigManager::OnSourceModStartup(bool late)
992 LoadGameConfigFile("core.games", &g_pGameConf, NULL, 0);
994 strncopy(g_Game, g_pSM->GetGameFolderName(), sizeof(g_Game));
995 strncopy(g_GameDesc + 1, smcore.GetGameDescription(), sizeof(g_GameDesc) - 1);
996 smcore.GetGameName(g_GameName + 1, sizeof(g_GameName) - 1);
999 void GameConfigManager::OnSourceModAllInitialized()
1001 /* NOW initialize the game file */
1002 CGameConfig *pGameConf = (CGameConfig *)g_pGameConf;
1005 if (!pGameConf->Reparse(error, sizeof(error)))
1010 sharesys->AddInterface(NULL, this);
1013 void GameConfigManager::OnSourceModAllShutdown()
1015 CloseGameConfigFile(g_pGameConf);
1018 bool GameConfigManager::LoadGameConfigFile(const char *file, IGameConfig **_pConfig, char *error, size_t maxlength)
1021 /* A crash was detected during last load - We block the gamedata loading so it hopefully won't happen again */
1022 if (g_blockGameDataLoad)
1024 UTIL_Format(error, maxlength, "GameData loaded blocked due to detected crash");
1029 CGameConfig *pConfig;
1030 CGameConfig **ppConfig;
1032 if ((ppConfig = m_Lookup.retrieve(file)) != NULL)
1034 pConfig = *ppConfig;
1035 pConfig->IncRefCount();
1036 *_pConfig = pConfig;
1040 pConfig = new CGameConfig(file);
1042 /* :HACKHACK: Don't parse the main config file */
1044 if (_pConfig != &g_pGameConf)
1046 retval = pConfig->Reparse(error, maxlength);
1049 m_cfgs.push_back(pConfig);
1050 m_Lookup.insert(file, pConfig);
1052 pConfig->IncRefCount();
1054 *_pConfig = pConfig;
1059 void GameConfigManager::CloseGameConfigFile(IGameConfig *cfg)
1061 CGameConfig *pConfig = (CGameConfig *)cfg;
1063 if (pConfig->DecRefCount() == 0)
1065 m_Lookup.remove(pConfig->m_File);
1070 extern Handle_t g_GameConfigsType;
1072 IGameConfig *GameConfigManager::ReadHandle(Handle_t hndl, IdentityToken_t *ident, HandleError *err)
1074 HandleSecurity sec(ident, g_pCoreIdent);
1075 IGameConfig *conf = NULL;
1076 HandleError _err = handlesys->ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&conf);
1086 void GameConfigManager::AddUserConfigHook(const char *sectionname, ITextListener_SMC *listener)
1088 m_customHandlers.insert(sectionname, listener);
1091 void GameConfigManager::RemoveUserConfigHook(const char *sectionname, ITextListener_SMC *listener)
1093 ITextListener_SMC **pListener = m_customHandlers.retrieve(sectionname);
1095 if (pListener == NULL)
1100 if (*pListener != listener)
1105 m_customHandlers.remove(sectionname);
1110 void GameConfigManager::AcquireLock()
1114 void GameConfigManager::ReleaseLock()