Update gamedata for CS:S update.
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_COMPAT_ALT "mac" /* Alternate platform name if game data is missing for primary one */
81 #define PLATFORM_SERVER_BINARY "server_i486.so"
82 #elif defined PLATFORM_APPLE
83 #define PLATFORM_NAME "mac"
84 #define PLATFORM_COMPAT_ALT "linux"
85 #define PLATFORM_SERVER_BINARY "server.dylib"
98 unsigned int s_ServerBinCRC;
99 bool s_ServerBinCRC_Ok = false;
101 static bool DoesGameMatch(const char *value)
103 #if defined PLATFORM_WINDOWS
104 if (strcasecmp(value, g_Game) == 0 ||
106 if (strcmp(value, g_Game) == 0 ||
108 strcmp(value, g_GameDesc) == 0 ||
109 strcmp(value, g_GameName) == 0)
116 static bool DoesEngineMatch(const char *value)
118 return strcmp(value, g_pParseEngine) == 0;
121 static inline bool DoesPlatformMatch(const char *platform)
123 return strcmp(platform, PLATFORM_NAME) == 0;
126 static inline bool IsPlatformCompatible(const char *platform, bool *hadPrimaryMatch)
128 if (DoesPlatformMatch(platform))
130 #if defined PLATFORM_COMPAT_ALT
131 *hadPrimaryMatch = true;
136 #if defined PLATFORM_COMPAT_ALT
137 /* If entry hasn't been found for the primary platform name, check for compatible alternate */
138 if (!*hadPrimaryMatch)
139 return strcmp(platform, PLATFORM_COMPAT_ALT) == 0;
145 CGameConfig::CGameConfig(const char *file, const char *engine)
147 strncopy(m_File, file, sizeof(m_File));
150 m_CustomHandler = NULL;
153 m_pEngine = smcore.GetSourceEngineName();
157 if (strcmp(m_pEngine, "css") == 0 || strcmp(m_pEngine, "dods") == 0 || strcmp(m_pEngine, "hl2dm") == 0 || strcmp(m_pEngine, "tf2") == 0)
158 this->SetBaseEngine("orangebox_valve");
159 else if (strcmp(m_pEngine, "nucleardawn") == 0)
160 this->SetBaseEngine("left4dead2");
162 this->SetBaseEngine(NULL);
165 CGameConfig::~CGameConfig()
167 g_GameConfigs.RemoveCachedConfig(this);
170 SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *name)
175 return SMCResult_Continue;
178 switch (m_ParseState)
182 if (strcmp(name, "Games") == 0)
184 m_ParseState = PSTATE_GAMES;
192 if (strcmp(name, "*") == 0 ||
193 strcmp(name, "#default") == 0 ||
196 bShouldBeReadingDefault = true;
197 m_ParseState = PSTATE_GAMEDEFS;
198 strncopy(m_Game, name, sizeof(m_Game));
204 case PSTATE_GAMEDEFS:
206 if (strcmp(name, "Offsets") == 0)
208 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
210 else if (strcmp(name, "Keys") == 0)
212 m_ParseState = PSTATE_GAMEDEFS_KEYS;
214 else if ((strcmp(name, "#supported") == 0) && (strcmp(m_Game, "#default") == 0))
216 m_ParseState = PSTATE_GAMEDEFS_SUPPORTED;
217 /* Ignore this section unless we get a game. */
218 bShouldBeReadingDefault = false;
220 matched_game = false;
222 matched_engine = false;
224 else if (strcmp(name, "Signatures") == 0)
226 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
228 else if (strcmp(name, "CRC") == 0)
230 m_ParseState = PSTATE_GAMEDEFS_CRC;
231 bShouldBeReadingDefault = false;
233 else if (strcmp(name, "Addresses") == 0)
235 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
239 if (g_GameConfigs.m_customHandlers.retrieve(name, &m_CustomHandler))
242 m_ParseState = PSTATE_GAMEDEFS_CUSTOM;
243 m_CustomHandler->ReadSMC_ParseStart();
251 case PSTATE_GAMEDEFS_OFFSETS:
255 strncopy(m_offset, name, sizeof(m_offset));
256 m_ParseState = PSTATE_GAMEDEFS_OFFSETS_OFFSET;
257 matched_platform = false;
260 case PSTATE_GAMEDEFS_SIGNATURES:
262 strncopy(m_offset, name, sizeof(m_offset));
264 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES_SIG;
265 matched_platform = false;
268 case PSTATE_GAMEDEFS_CRC:
272 if (strcmp(name, "server") != 0)
274 smcore.Format(error, sizeof(error), "Unrecognized library \"%s\"", name);
276 else if (!s_ServerBinCRC_Ok)
279 char path[PLATFORM_MAX_PATH];
281 g_pSM->BuildPath(Path_Game, path, sizeof(path), "bin/" PLATFORM_SERVER_BINARY);
282 if ((fp = fopen(path, "rb")) == NULL)
284 smcore.Format(error, sizeof(error), "Could not open binary: %s", path);
289 fseek(fp, 0, SEEK_END);
291 fseek(fp, 0, SEEK_SET);
293 buffer = malloc(size);
294 fread(buffer, size, 1, fp);
295 s_ServerBinCRC = UTIL_CRC32(buffer, size);
297 s_ServerBinCRC_Ok = true;
301 if (error[0] != '\0')
304 smcore.LogError("[SM] Error while parsing CRC section for \"%s\" (%s):", m_Game, m_CurFile);
305 smcore.LogError("[SM] %s", error);
307 m_ParseState = PSTATE_GAMEDEFS_CRC_BINARY;
311 case PSTATE_GAMEDEFS_CUSTOM:
314 return m_CustomHandler->ReadSMC_NewSection(states, name);
317 case PSTATE_GAMEDEFS_ADDRESSES:
320 m_AddressSignature[0] = '\0';
321 m_AddressReadCount = 0;
323 strncopy(m_Address, name, sizeof(m_Address));
324 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
328 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS:
330 if (DoesPlatformMatch(name))
332 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ;
336 if (strcmp(name, "linux") != 0 && strcmp(name, "windows") != 0 && strcmp(name, "mac") != 0)
338 smcore.LogError("[SM] Error while parsing Address section for \"%s\" (%s):", m_Address, m_CurFile);
339 smcore.LogError("[SM] Unrecognized platform \"%s\"", name);
345 /* No sub-sections allowed:
346 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
347 case PSTATE_GAMEDEFS_KEYS:
348 case PSTATE_GAMEDEFS_SUPPORTED:
349 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
350 case PSTATE_GAMEDEFS_CRC_BINARY:
351 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ:
355 /* If we don't know what we got, start ignoring */
361 return SMCResult_Continue;
364 SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
368 return SMCResult_Continue;
371 if (m_ParseState == PSTATE_GAMEDEFS_OFFSETS_OFFSET)
373 if (strcmp(key, "class") == 0)
375 strncopy(m_Class, value, sizeof(m_Class));
376 } else if (strcmp(key, "prop") == 0) {
377 strncopy(m_Prop, value, sizeof(m_Prop));
378 } else if (IsPlatformCompatible(key, &matched_platform)) {
379 m_Offsets.replace(m_offset, atoi(value));
381 } else if (m_ParseState == PSTATE_GAMEDEFS_KEYS) {
382 ke::AString vstr(value);
383 m_Keys.replace(key, ke::Move(vstr));
384 } else if (m_ParseState == PSTATE_GAMEDEFS_SUPPORTED) {
385 if (strcmp(key, "game") == 0)
388 if (DoesGameMatch(value))
392 if ((!had_engine && matched_game) || (matched_engine && matched_game))
394 bShouldBeReadingDefault = true;
397 else if (strcmp(key, "engine") == 0)
400 if (DoesEngineMatch(value))
402 matched_engine = true;
404 if ((!had_game && matched_engine) || (matched_game && matched_engine))
406 bShouldBeReadingDefault = true;
409 } else if (m_ParseState == PSTATE_GAMEDEFS_SIGNATURES_SIG) {
410 if (IsPlatformCompatible(key, &matched_platform))
412 strncopy(s_TempSig.sig, value, sizeof(s_TempSig.sig));
413 } else if (strcmp(key, "library") == 0) {
414 strncopy(s_TempSig.library, value, sizeof(s_TempSig.library));
416 } else if (m_ParseState == PSTATE_GAMEDEFS_CRC_BINARY) {
417 if (DoesPlatformMatch(key)
419 && !bShouldBeReadingDefault)
421 unsigned int crc = 0;
422 sscanf(value, "%08X", &crc);
423 if (s_ServerBinCRC == crc)
425 bShouldBeReadingDefault = true;
428 } else if (m_ParseState == PSTATE_GAMEDEFS_ADDRESSES_ADDRESS || m_ParseState == PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ) {
429 if (strcmp(key, "read") == 0) {
430 int limit = sizeof(m_AddressRead)/sizeof(m_AddressRead[0]);
431 if (m_AddressReadCount < limit)
433 m_AddressRead[m_AddressReadCount] = atoi(value);
434 m_AddressReadCount++;
438 smcore.LogError("[SM] Error parsing Address \"%s\", does not support more than %d read offsets (gameconf \"%s\")", m_Address, limit, m_CurFile);
440 } else if (strcmp(key, "signature") == 0) {
441 strncopy(m_AddressSignature, value, sizeof(m_AddressSignature));
443 } else if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM) {
444 return m_CustomHandler->ReadSMC_KeyValue(states, key, value);
447 return SMCResult_Continue;
450 SMCResult CGameConfig::ReadSMC_LeavingSection(const SMCStates *states)
455 return SMCResult_Continue;
461 m_CustomHandler->ReadSMC_LeavingSection(states);
462 return SMCResult_Continue;
465 switch (m_ParseState)
469 m_ParseState = PSTATE_NONE;
472 case PSTATE_GAMEDEFS:
474 m_ParseState = PSTATE_GAMES;
477 case PSTATE_GAMEDEFS_CUSTOM:
479 m_ParseState = PSTATE_GAMEDEFS;
480 m_CustomHandler->ReadSMC_ParseEnd(false, false);
483 case PSTATE_GAMEDEFS_KEYS:
484 case PSTATE_GAMEDEFS_OFFSETS:
486 m_ParseState = PSTATE_GAMEDEFS;
489 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
491 /* Parse the offset... */
492 if (m_Class[0] != '\0'
493 && m_Prop[0] != '\0')
495 SendProp *pProp = gamehelpers->FindInSendTable(m_Class, m_Prop);
498 int val = gamehelpers->GetSendPropOffset(pProp);
499 m_Offsets.replace(m_offset, val);
500 m_Props.replace(m_offset, pProp);
502 /* Check if it's a non-default game and no offsets exist */
503 if (((strcmp(m_Game, "*") != 0) && strcmp(m_Game, "#default") != 0)
504 && (!m_Offsets.retrieve(m_offset)))
506 smcore.LogError("[SM] Unable to find property %s.%s (file \"%s\") (mod \"%s\")",
514 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
517 case PSTATE_GAMEDEFS_CRC:
518 case PSTATE_GAMEDEFS_SUPPORTED:
520 if (!bShouldBeReadingDefault)
522 /* If we shouldn't read the rest of this section, set the ignore level. */
524 m_ParseState = PSTATE_GAMES;
526 m_ParseState = PSTATE_GAMEDEFS;
530 case PSTATE_GAMEDEFS_CRC_BINARY:
532 m_ParseState = PSTATE_GAMEDEFS_CRC;
535 case PSTATE_GAMEDEFS_SIGNATURES:
537 m_ParseState = PSTATE_GAMEDEFS;
540 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
542 if (s_TempSig.library[0] == '\0')
545 strncopy(s_TempSig.library, "server", sizeof(s_TempSig.library));
547 void *addrInBase = NULL;
548 if (strcmp(s_TempSig.library, "server") == 0)
550 addrInBase = smcore.serverFactory;
551 } else if (strcmp(s_TempSig.library, "engine") == 0) {
552 addrInBase = smcore.engineFactory;
553 } else if (strcmp(s_TempSig.library, "matchmaking_ds") == 0) {
554 addrInBase = smcore.matchmakingDSFactory;
556 void *final_addr = NULL;
557 if (addrInBase == NULL)
559 smcore.LogError("[SM] Unrecognized library \"%s\" (gameconf \"%s\")",
563 else if (s_TempSig.sig[0])
565 if (s_TempSig.sig[0] == '@')
567 #if defined PLATFORM_WINDOWS
568 MEMORY_BASIC_INFORMATION mem;
570 if (VirtualQuery(addrInBase, &mem, sizeof(mem)))
571 final_addr = g_MemUtils.ResolveSymbol(mem.AllocationBase, &s_TempSig.sig[1]);
573 smcore.LogError("[SM] Unable to find library \"%s\" in memory (gameconf \"%s\")", s_TempSig.library, m_File);
574 #elif defined PLATFORM_POSIX
576 /* GNU only: returns 0 on error, inconsistent! >:[ */
577 if (dladdr(addrInBase, &info) != 0)
579 void *handle = dlopen(info.dli_fname, RTLD_NOW);
582 if (smcore.SymbolsAreHidden())
583 final_addr = g_MemUtils.ResolveSymbol(handle, &s_TempSig.sig[1]);
585 final_addr = dlsym(handle, &s_TempSig.sig[1]);
588 smcore.LogError("[SM] Unable to load library \"%s\" (gameconf \"%s\")",
593 smcore.LogError("[SM] Unable to find library \"%s\" in memory (gameconf \"%s\")",
602 /* First, preprocess the signature */
603 unsigned char real_sig[511];
608 length = strlen(s_TempSig.sig);
610 real_bytes = UTIL_DecodeHexString(real_sig, sizeof(real_sig), s_TempSig.sig);
614 final_addr = g_MemUtils.FindPattern(addrInBase, (char*) real_sig, real_bytes);
618 m_Sigs.replace(m_offset, final_addr);
621 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
625 case PSTATE_GAMEDEFS_ADDRESSES:
627 m_ParseState = PSTATE_GAMEDEFS;
630 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS:
632 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES;
634 if (m_Address[0] != '\0' && m_AddressSignature[0] != '\0')
636 AddressConf addrConf(m_AddressSignature, sizeof(m_AddressSignature), m_AddressReadCount, m_AddressRead);
637 m_Addresses.replace(m_Address, addrConf);
642 case PSTATE_GAMEDEFS_ADDRESSES_ADDRESS_READ:
644 m_ParseState = PSTATE_GAMEDEFS_ADDRESSES_ADDRESS;
649 return SMCResult_Continue;
652 #define MSTATE_NONE 0
653 #define MSTATE_MAIN 1
654 #define MSTATE_FILE 2
656 class MasterReader : public ITextListener_SMC
659 virtual void ReadSMC_ParseStart()
665 virtual SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name)
669 return SMCResult_Continue;
672 if (state == MSTATE_NONE)
674 if (strcmp(name, "Game Master") == 0)
683 else if (state == MSTATE_MAIN)
685 strncopy(cur_file, name, sizeof(cur_file));
687 matched_engine = false;
689 matched_game = false;
692 else if (state == MSTATE_FILE)
697 return SMCResult_Continue;
700 virtual SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
702 if (ignoreLevel || state != MSTATE_FILE)
704 return SMCResult_Continue;
707 if (strcmp(key, "engine") == 0)
710 if (DoesEngineMatch(value))
712 matched_engine = true;
715 else if (strcmp(key, "game") == 0)
718 if (DoesGameMatch(value))
724 return SMCResult_Continue;
727 virtual SMCResult ReadSMC_LeavingSection(const SMCStates *states)
732 return SMCResult_Continue;
735 if (state == MSTATE_FILE)
737 /* The four success conditions:
739 * 2. Needed game only.
740 * 3. Needed engine only.
741 * 4. Needed both engine and game.
742 * Final result is minimized via k-map.
745 if ((!had_engine && !had_game) ||
746 (!had_engine && (had_game && matched_game)) ||
747 (!had_game && (had_engine && matched_engine)) ||
748 ((had_game && had_engine) && (matched_game && matched_engine)))
750 if ((!had_engine && !had_game) ||
751 (!had_engine && matched_game) ||
752 (!had_game && matched_engine) ||
753 (matched_engine && matched_game))
755 fileList->push_back(cur_file);
759 else if (state == MSTATE_MAIN)
764 return SMCResult_Continue;
767 List<String> *fileList;
769 unsigned int ignoreLevel;
770 char cur_file[PLATFORM_MAX_PATH];
777 static MasterReader master_reader;
779 bool CGameConfig::Reparse(char *error, size_t maxlength)
781 /* Reset cached data */
787 char path[PLATFORM_MAX_PATH];
789 /* See if we can use the extended gamedata format. */
790 g_pSM->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/master.games.txt", m_File);
791 if (!libsys->PathExists(path))
793 /* Nope, use the old mechanism. */
794 smcore.Format(path, sizeof(path), "%s.txt", m_File);
795 if (!EnterFile(path, error, maxlength))
800 /* Allow customization. */
801 g_pSM->BuildPath(Path_SM, path, sizeof(path), "gamedata/custom/%s.txt", m_File);
802 if (libsys->PathExists(path))
804 smcore.Format(path, sizeof(path), "custom/%s.txt", m_File);
805 return EnterFile(path, error, maxlength);
810 /* Otherwise, it's time to parse the master. */
812 SMCStates state = {0, 0};
813 List<String> fileList;
814 master_reader.fileList = &fileList;
815 const char *pEngine[2] = { m_pBaseEngine, m_pEngine };
817 for (unsigned char iter = 0; iter < SM_ARRAYSIZE(pEngine); ++iter)
819 if (pEngine[iter] == NULL)
824 this->SetParseEngine(pEngine[iter]);
825 err = textparsers->ParseSMCFile(path, &master_reader, &state, error, maxlength);
826 if (err != SMCError_Okay)
828 const char *msg = textparsers->GetSMCErrorString(err);
830 smcore.LogError("[SM] Error parsing master gameconf file \"%s\":", path);
831 smcore.LogError("[SM] Error %d on line %d, col %d: %s",
835 msg ? msg : "Unknown error");
840 /* Go through each file we found and parse it. */
841 List<String>::iterator iter;
842 for (iter = fileList.begin(); iter != fileList.end(); iter++)
844 smcore.Format(path, sizeof(path), "%s/%s", m_File, (*iter).c_str());
845 if (!EnterFile(path, error, maxlength))
851 /* Parse the contents of the 'custom' directory */
852 g_pSM->BuildPath(Path_SM, path, sizeof(path), "gamedata/%s/custom", m_File);
853 IDirectory *customDir = libsys->OpenDirectory(path);
860 while (customDir->MoreFiles())
862 if (!customDir->IsEntryFile())
864 customDir->NextEntry();
868 const char *curFile = customDir->GetEntryName();
870 /* Only allow .txt files */
871 int len = strlen(curFile);
872 if (len > 4 && strcmp(&curFile[len-4], ".txt") != 0)
874 customDir->NextEntry();
878 smcore.Format(path, sizeof(path), "%s/custom/%s", m_File, curFile);
879 if (!EnterFile(path, error, maxlength))
881 libsys->CloseDirectory(customDir);
885 customDir->NextEntry();
888 libsys->CloseDirectory(customDir);
892 bool CGameConfig::EnterFile(const char *file, char *error, size_t maxlength)
895 SMCStates state = {0, 0};
897 g_pSM->BuildPath(Path_SM, m_CurFile, sizeof(m_CurFile), "gamedata/%s", file);
899 /* Initialize parse states */
901 bShouldBeReadingDefault = true;
902 m_ParseState = PSTATE_NONE;
903 const char *pEngine[2] = { m_pBaseEngine, m_pEngine };
905 for (unsigned char iter = 0; iter < SM_ARRAYSIZE(pEngine); ++iter)
907 if (pEngine[iter] == NULL)
912 this->SetParseEngine(pEngine[iter]);
913 if ((err=textparsers->ParseSMCFile(m_CurFile, this, &state, error, maxlength))
916 const char *msg = textparsers->GetSMCErrorString(err);
918 smcore.LogError("[SM] Error parsing gameconfig file \"%s\":", m_CurFile);
919 smcore.LogError("[SM] Error %d on line %d, col %d: %s",
923 msg ? msg : "Unknown error");
925 if (m_ParseState == PSTATE_GAMEDEFS_CUSTOM)
927 //error occurred while parsing a custom section
928 m_CustomHandler->ReadSMC_ParseEnd(true, true);
929 m_CustomHandler = NULL;
940 void CGameConfig::SetBaseEngine(const char *engine)
942 m_pBaseEngine = engine;
945 void CGameConfig::SetParseEngine(const char *engine)
947 g_pParseEngine = engine;
950 bool CGameConfig::GetOffset(const char *key, int *value)
952 return m_Offsets.retrieve(key, value);
955 const char *CGameConfig::GetKeyValue(const char *key)
957 StringHashMap<ke::AString>::Result r = m_Keys.find(key);
960 return r->value.chars();
963 //memory addresses below 0x10000 are automatically considered invalid for dereferencing
964 #define VALID_MINIMUM_MEMORY_ADDRESS 0x10000
966 bool CGameConfig::GetAddress(const char *key, void **retaddr)
968 StringHashMap<AddressConf>::Result r = m_Addresses.find(key);
975 AddressConf &addrConf = r->value;
978 if (!GetMemSig(addrConf.signatureName, &addr))
984 for (int i = 0; i < addrConf.readCount; ++i)
986 int offset = addrConf.read[i];
988 //NULLs in the middle of an indirection chain are bad, end NULL is ok
989 if (addr == NULL || reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
994 addr = *(reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(addr) + offset));
1001 static inline unsigned minOf(unsigned a, unsigned b)
1003 return a <= b ? a : b;
1006 CGameConfig::AddressConf::AddressConf(char *sigName, unsigned sigLength, unsigned readCount, int *read)
1008 unsigned readLimit = minOf(readCount, sizeof(this->read) / sizeof(this->read[0]));
1010 strncopy(signatureName, sigName, sizeof(signatureName) / sizeof(signatureName[0]));
1011 this->readCount = readLimit;
1012 memcpy(&this->read[0], read, sizeof(this->read[0])*readLimit);
1015 SendProp *CGameConfig::GetSendProp(const char *key)
1018 if (!m_Props.retrieve(key, &prop))
1023 bool CGameConfig::GetMemSig(const char *key, void **addr)
1025 return m_Sigs.retrieve(key, addr);
1028 GameConfigManager::GameConfigManager()
1032 GameConfigManager::~GameConfigManager()
1036 void GameConfigManager::OnSourceModStartup(bool late)
1038 LoadGameConfigFile("core.games", &g_pGameConf, NULL, 0);
1040 strncopy(g_Game, g_pSM->GetGameFolderName(), sizeof(g_Game));
1041 strncopy(g_GameDesc + 1, smcore.GetGameDescription(), sizeof(g_GameDesc) - 1);
1042 smcore.GetGameName(g_GameName + 1, sizeof(g_GameName) - 1);
1045 void GameConfigManager::OnSourceModAllInitialized()
1047 /* NOW initialize the game file */
1048 CGameConfig *pGameConf = (CGameConfig *)g_pGameConf;
1051 if (!pGameConf->Reparse(error, sizeof(error)))
1056 sharesys->AddInterface(NULL, this);
1059 void GameConfigManager::OnSourceModAllShutdown()
1061 CloseGameConfigFile(g_pGameConf);
1064 bool GameConfigManager::LoadGameConfigFile(const char *file, IGameConfig **_pConfig, char *error, size_t maxlength)
1067 /* A crash was detected during last load - We block the gamedata loading so it hopefully won't happen again */
1068 if (g_blockGameDataLoad)
1070 UTIL_Format(error, maxlength, "GameData loaded blocked due to detected crash");
1075 CGameConfig *pConfig;
1077 if (m_Lookup.retrieve(file, &pConfig))
1080 *_pConfig = pConfig;
1084 pConfig = new CGameConfig(file);
1086 /* :HACKHACK: Don't parse the main config file */
1088 if (_pConfig != &g_pGameConf)
1090 retval = pConfig->Reparse(error, maxlength);
1093 m_Lookup.insert(file, pConfig);
1095 *_pConfig = pConfig;
1099 void GameConfigManager::CloseGameConfigFile(IGameConfig *cfg)
1101 CGameConfig *pConfig = (CGameConfig *)cfg;
1105 extern Handle_t g_GameConfigsType;
1107 IGameConfig *GameConfigManager::ReadHandle(Handle_t hndl, IdentityToken_t *ident, HandleError *err)
1109 HandleSecurity sec(ident, g_pCoreIdent);
1110 IGameConfig *conf = NULL;
1111 HandleError _err = handlesys->ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&conf);
1121 void GameConfigManager::AddUserConfigHook(const char *sectionname, ITextListener_SMC *listener)
1123 m_customHandlers.insert(sectionname, listener);
1126 void GameConfigManager::RemoveUserConfigHook(const char *sectionname, ITextListener_SMC *aListener)
1128 ITextListener_SMC *listener;
1129 if (!m_customHandlers.retrieve(sectionname, &listener))
1132 if (listener != aListener)
1135 m_customHandlers.remove(sectionname);
1139 void GameConfigManager::AcquireLock()
1143 void GameConfigManager::ReleaseLock()
1147 void GameConfigManager::RemoveCachedConfig(CGameConfig *config)
1149 m_Lookup.remove(config->m_File);