Added check to make sure player are alive to perform sm_slap, sm_burn, sm_slay.
3 * ================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License,
10 * version 3.0, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to
21 * link the code of this program (as well as its derivative works) to
22 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
23 * Game MODs that run on software by the Valve Corporation. You must
24 * obey the GNU General Public License in all respects for all other
25 * code used. Additionally, AlliedModders LLC grants this exception
26 * to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version
28 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
35 #include "GameConfigs.h"
36 #include "TextParsers.h"
37 #include "sm_stringutil.h"
38 #include "sourcemod.h"
39 #include "sourcemm_api.h"
40 #include "HalfLife2.h"
43 #include "MemoryUtils.h"
44 #include "LibrarySys.h"
45 #include "HandleSys.h"
47 #if defined PLATFORM_LINUX
51 GameConfigManager g_GameConfigs;
52 IGameConfig *g_pGameConf = NULL;
54 char g_GameDesc[256] = {'!', '\0'};
57 #define PSTATE_GAMES 1
58 #define PSTATE_GAMEDEFS 2
59 #define PSTATE_GAMEDEFS_OFFSETS 3
60 #define PSTATE_GAMEDEFS_OFFSETS_OFFSET 4
61 #define PSTATE_GAMEDEFS_KEYS 5
62 #define PSTATE_GAMEDEFS_SUPPORTED 6
63 #define PSTATE_GAMEDEFS_SIGNATURES 7
64 #define PSTATE_GAMEDEFS_SIGNATURES_SIG 8
66 #if defined PLATFORM_WINDOWS
67 #define PLATFORM_NAME "windows"
68 #elif defined PLATFORM_LINUX
69 #define PLATFORM_NAME "linux"
83 CGameConfig::CGameConfig(const char *file)
85 m_pFile = sm_strdup(file);
86 m_pOffsets = sm_trie_create();
87 m_pProps = sm_trie_create();
88 m_pKeys = sm_trie_create();
89 m_pSigs = sm_trie_create();
90 m_pStrings = new BaseStringTable(512);
94 CGameConfig::~CGameConfig()
96 sm_trie_destroy(m_pOffsets);
97 sm_trie_destroy(m_pProps);
98 sm_trie_destroy(m_pKeys);
99 sm_trie_destroy(m_pSigs);
104 SMCParseResult CGameConfig::ReadSMC_NewSection(const char *name, bool opt_quotes)
109 return SMCParse_Continue;
112 switch (m_ParseState)
116 if (strcmp(name, "Games") == 0)
118 m_ParseState = PSTATE_GAMES;
126 if ((strcmp(name, "*") == 0)
127 || (strcmp(name, "#default") == 0)
128 || (strcasecmp(name, g_Game) == 0)
129 || (strcasecmp(name, g_GameDesc) == 0))
131 m_ParseState = PSTATE_GAMEDEFS;
132 strncopy(m_Game, name, sizeof(m_Game));
138 case PSTATE_GAMEDEFS:
140 if (strcmp(name, "Offsets") == 0)
142 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
144 else if (strcmp(name, "Keys") == 0)
146 m_ParseState = PSTATE_GAMEDEFS_KEYS;
148 else if ((strcmp(name, "#supported") == 0)
149 && (strcmp(m_Game, "#default") == 0))
151 m_ParseState = PSTATE_GAMEDEFS_SUPPORTED;
152 /* Ignore this section unless we get a game. */
153 bShouldBeReadingDefault = false;
155 else if (strcmp(name, "Signatures") == 0)
157 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
165 case PSTATE_GAMEDEFS_OFFSETS:
169 strncopy(m_offset, name, sizeof(m_offset));
170 m_ParseState = PSTATE_GAMEDEFS_OFFSETS_OFFSET;
173 case PSTATE_GAMEDEFS_SIGNATURES:
175 strncopy(m_offset, name, sizeof(m_offset));
177 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES_SIG;
180 /* No sub-sections allowed:
181 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
182 case PSTATE_GAMEDEFS_KEYS:
183 case PSTATE_GAMEDEFS_SUPPORTED:
184 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
188 /* If we don't know what we got, start ignoring */
194 return SMCParse_Continue;
197 SMCParseResult CGameConfig::ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes)
201 return SMCParse_Continue;
204 if (m_ParseState == PSTATE_GAMEDEFS_OFFSETS_OFFSET)
206 if (strcmp(key, "class") == 0)
208 strncopy(m_Class, value, sizeof(m_Class));
209 } else if (strcmp(key, "prop") == 0) {
210 strncopy(m_Prop, value, sizeof(m_Prop));
211 } else if (strcmp(key, PLATFORM_NAME) == 0) {
212 int val = atoi(value);
213 sm_trie_replace(m_pOffsets, m_offset, (void *)val);
215 } else if (m_ParseState == PSTATE_GAMEDEFS_KEYS) {
216 int id = m_pStrings->AddString(value);
217 sm_trie_replace(m_pKeys, key, (void *)id);
218 } else if (m_ParseState == PSTATE_GAMEDEFS_SUPPORTED) {
219 if (strcmp(key, "game") == 0
220 && (strcasecmp(value, g_Game) == 0 || strcasecmp(value, g_GameDesc) == 0))
222 bShouldBeReadingDefault = true;
224 } else if (m_ParseState == PSTATE_GAMEDEFS_SIGNATURES_SIG) {
225 if (strcmp(key, PLATFORM_NAME) == 0)
227 strncopy(s_TempSig.sig, value, sizeof(s_TempSig.sig));
228 } else if (strcmp(key, "library") == 0) {
229 strncopy(s_TempSig.library, value, sizeof(s_TempSig.library));
233 return SMCParse_Continue;
236 SMCParseResult CGameConfig::ReadSMC_LeavingSection()
241 return SMCParse_Continue;
244 switch (m_ParseState)
248 m_ParseState = PSTATE_NONE;
251 case PSTATE_GAMEDEFS:
253 m_ParseState = PSTATE_GAMES;
256 case PSTATE_GAMEDEFS_KEYS:
257 case PSTATE_GAMEDEFS_OFFSETS:
259 m_ParseState = PSTATE_GAMEDEFS;
262 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
264 /* Parse the offset... */
265 if (m_Class[0] != '\0'
266 && m_Prop[0] != '\0')
268 SendProp *pProp = g_HL2.FindInSendTable(m_Class, m_Prop);
271 int val = pProp->GetOffset();
272 sm_trie_replace(m_pOffsets, m_offset, (void *)val);
273 sm_trie_replace(m_pProps, m_offset, pProp);
275 /* Check if it's a non-default game and no offsets exist */
276 if (((strcmp(m_Game, "*") != 0) && strcmp(m_Game, "#default") != 0)
277 && (!sm_trie_retrieve(m_pOffsets, m_offset, NULL)))
279 g_Logger.LogError("[SM] Unable to find property %s.%s (file \"%s\") (mod \"%s\")",
287 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
290 case PSTATE_GAMEDEFS_SUPPORTED:
292 if (!bShouldBeReadingDefault)
294 /* If we shouldn't read the rest of this section, set the ignore level. */
296 m_ParseState = PSTATE_GAMES;
298 m_ParseState = PSTATE_GAMEDEFS;
302 case PSTATE_GAMEDEFS_SIGNATURES:
304 m_ParseState = PSTATE_GAMEDEFS;
307 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
309 if (s_TempSig.library[0] == '\0')
312 strncopy(s_TempSig.library, "server", sizeof(s_TempSig.library));
314 void *addrInBase = NULL;
315 if (strcmp(s_TempSig.library, "server") == 0)
317 addrInBase = (void *)g_SMAPI->serverFactory(false);
318 } else if (strcmp(s_TempSig.library, "engine") == 0) {
319 addrInBase = (void *)g_SMAPI->engineFactory(false);
321 void *final_addr = NULL;
322 if (addrInBase == NULL)
324 g_Logger.LogError("[SM] Unrecognized library \"%s\" (gameconf \"%s\")",
328 #if defined PLATFORM_LINUX
329 if (s_TempSig.sig[0] == '@')
332 /* GNU only: returns 0 on error, inconsistent! >:[ */
333 if (dladdr(addrInBase, &info) != 0)
335 void *handle = dlopen(info.dli_fname, RTLD_NOW);
338 final_addr = dlsym(handle, &s_TempSig.sig[1]);
341 g_Logger.LogError("[SM] Unable to load library \"%s\" (gameconf \"%s\")",
346 g_Logger.LogError("[SM] Unable to find library \"%s\" in memory (gameconf \"%s\")",
356 /* First, preprocess the signature */
362 length = strlen(s_TempSig.sig);
364 for (size_t i=0; i<length; i++)
366 if (real_bytes >= sizeof(real_sig))
370 real_sig[real_bytes++] = s_TempSig.sig[i];
371 if (s_TempSig.sig[i] == '\\'
372 && s_TempSig.sig[i+1] == 'x')
378 /* Get the hex part */
381 s_byte[0] = s_TempSig.sig[i+2];
382 s_byte[1] = s_TempSig.sig[i+3];
384 /* Read it as an integer */
385 sscanf(s_byte, "%x", &r_byte);
387 real_sig[real_bytes-1] = r_byte;
395 final_addr = g_MemUtils.FindPattern(addrInBase, real_sig, real_bytes);
399 #if defined PLATFORM_LINUX
402 sm_trie_replace(m_pSigs, m_offset, final_addr);
403 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
409 return SMCParse_Continue;
412 bool CGameConfig::Reparse(char *error, size_t maxlength)
416 char path[PLATFORM_MAX_PATH];
417 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "gamedata/%s.txt", m_pFile);
418 /* Backwards compatibility */
419 if (!g_LibSys.PathExists(path))
421 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "configs/gamedata/%s.txt", m_pFile);
424 /* Initialize parse states */
426 bShouldBeReadingDefault = true;
427 m_ParseState = PSTATE_NONE;
428 /* Reset cached data */
430 sm_trie_clear(m_pOffsets);
431 sm_trie_clear(m_pProps);
432 sm_trie_clear(m_pKeys);
434 if ((err=g_TextParser.ParseFile_SMC(path, this, NULL, NULL))
437 if (error && (err != SMCParse_Custom))
439 const char *str = g_TextParser.GetSMCErrorString(err);
440 snprintf(error, maxlength, "%s", str);
448 bool CGameConfig::GetOffset(const char *key, int *value)
452 if (!sm_trie_retrieve(m_pOffsets, key, &obj))
462 const char *CGameConfig::GetKeyValue(const char *key)
465 if (!sm_trie_retrieve(m_pKeys, key, &obj))
469 return m_pStrings->GetString((int)obj);
472 SendProp *CGameConfig::GetSendProp(const char *key)
476 if (!sm_trie_retrieve(m_pProps, key, (void **)&pProp))
484 bool CGameConfig::GetMemSig(const char *key, void **addr)
486 return sm_trie_retrieve(m_pSigs, key, addr);
489 void CGameConfig::IncRefCount()
494 unsigned int CGameConfig::DecRefCount()
500 GameConfigManager::GameConfigManager()
502 m_pLookup = sm_trie_create();
505 GameConfigManager::~GameConfigManager()
507 sm_trie_destroy(m_pLookup);
510 void GameConfigManager::OnSourceModStartup(bool late)
512 LoadGameConfigFile("core.games", &g_pGameConf, NULL, 0);
514 strncopy(g_Game, g_SourceMod.GetGameFolderName(), sizeof(g_Game));
515 strncopy(g_GameDesc + 1, SERVER_CALL(GetGameDescription)(), sizeof(g_GameDesc) - 1);
518 void GameConfigManager::OnSourceModAllInitialized()
520 /* NOW initialize the game file */
521 CGameConfig *pGameConf = (CGameConfig *)g_pGameConf;
524 if (!pGameConf->Reparse(error, sizeof(error)))
529 g_ShareSys.AddInterface(NULL, this);
532 void GameConfigManager::OnSourceModAllShutdown()
534 CloseGameConfigFile(g_pGameConf);
537 bool GameConfigManager::LoadGameConfigFile(const char *file, IGameConfig **_pConfig, char *error, size_t maxlength)
539 CGameConfig *pConfig;
541 if (sm_trie_retrieve(m_pLookup, file, (void **)&pConfig))
543 pConfig->IncRefCount();
548 pConfig = new CGameConfig(file);
550 /* :HACKHACK: Don't parse the main config file */
552 if (_pConfig != &g_pGameConf)
554 retval = pConfig->Reparse(error, maxlength);
557 m_cfgs.push_back(pConfig);
558 sm_trie_insert(m_pLookup, file, pConfig);
560 pConfig->IncRefCount();
567 void GameConfigManager::CloseGameConfigFile(IGameConfig *cfg)
569 CGameConfig *pConfig = (CGameConfig *)cfg;
571 if (pConfig->DecRefCount() == 0)
573 sm_trie_delete(m_pLookup, pConfig->m_pFile);
578 extern HandleType_t g_GameConfigsType;
580 IGameConfig *GameConfigManager::ReadHandle(Handle_t hndl, IdentityToken_t *ident, HandleError *err)
582 HandleSecurity sec(ident, g_pCoreIdent);
583 IGameConfig *conf = NULL;
584 HandleError _err = g_HandleSys.ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&conf);