reworked antiflood. it now has some logic in core to take care of loading order nastiness, and to fully prevent trigger spamming
3 * =============================================================================
5 * Copyright (C) 2004-2007 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>.
34 #include "GameConfigs.h"
35 #include "sm_stringutil.h"
36 #include "sourcemod.h"
37 #include "sourcemm_api.h"
38 #include "HalfLife2.h"
41 #include "MemoryUtils.h"
42 #include "LibrarySys.h"
43 #include "HandleSys.h"
46 #if defined PLATFORM_LINUX
50 GameConfigManager g_GameConfigs;
51 IGameConfig *g_pGameConf = NULL;
53 char g_GameDesc[256] = {'!', '\0'};
54 char g_GameName[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
65 #define PSTATE_GAMEDEFS_CRC 9
66 #define PSTATE_GAMEDEFS_CRC_BINARY 10
68 #if defined PLATFORM_WINDOWS
69 #define PLATFORM_NAME "windows"
70 #define PLATFORM_SERVER_BINARY "server.dll"
71 #elif defined PLATFORM_LINUX
72 #define PLATFORM_NAME "linux"
73 #define PLATFORM_SERVER_BINARY "server_i486.so"
86 unsigned int s_ServerBinCRC;
87 bool s_ServerBinCRC_Ok = false;
89 CGameConfig::CGameConfig(const char *file)
91 m_pFile = sm_strdup(file);
92 m_pOffsets = sm_trie_create();
93 m_pProps = sm_trie_create();
94 m_pKeys = sm_trie_create();
95 m_pSigs = sm_trie_create();
96 m_pStrings = new BaseStringTable(512);
100 CGameConfig::~CGameConfig()
102 sm_trie_destroy(m_pOffsets);
103 sm_trie_destroy(m_pProps);
104 sm_trie_destroy(m_pKeys);
105 sm_trie_destroy(m_pSigs);
110 SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *name)
115 return SMCResult_Continue;
118 switch (m_ParseState)
122 if (strcmp(name, "Games") == 0)
124 m_ParseState = PSTATE_GAMES;
132 if ((strcmp(name, "*") == 0)
133 || (strcmp(name, "#default") == 0)
134 || (strcasecmp(name, g_Game) == 0)
135 || (strcasecmp(name, g_GameDesc) == 0)
136 || (strcasecmp(name, g_GameName) == 0))
138 bShouldBeReadingDefault = true;
139 m_ParseState = PSTATE_GAMEDEFS;
140 strncopy(m_Game, name, sizeof(m_Game));
146 case PSTATE_GAMEDEFS:
148 if (strcmp(name, "Offsets") == 0)
150 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
152 else if (strcmp(name, "Keys") == 0)
154 m_ParseState = PSTATE_GAMEDEFS_KEYS;
156 else if ((strcmp(name, "#supported") == 0)
157 && (strcmp(m_Game, "#default") == 0))
159 m_ParseState = PSTATE_GAMEDEFS_SUPPORTED;
160 /* Ignore this section unless we get a game. */
161 bShouldBeReadingDefault = false;
163 else if (strcmp(name, "Signatures") == 0)
165 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
167 else if (strcmp(name, "CRC") == 0)
169 m_ParseState = PSTATE_GAMEDEFS_CRC;
170 bShouldBeReadingDefault = false;
178 case PSTATE_GAMEDEFS_OFFSETS:
182 strncopy(m_offset, name, sizeof(m_offset));
183 m_ParseState = PSTATE_GAMEDEFS_OFFSETS_OFFSET;
186 case PSTATE_GAMEDEFS_SIGNATURES:
188 strncopy(m_offset, name, sizeof(m_offset));
190 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES_SIG;
193 case PSTATE_GAMEDEFS_CRC:
197 if (strcmp(name, "server") != 0)
199 UTIL_Format(error, sizeof(error), "Unrecognized library \"%s\"", name);
201 else if (!s_ServerBinCRC_Ok)
204 char path[PLATFORM_MAX_PATH];
206 g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "bin/" PLATFORM_SERVER_BINARY);
207 if ((fp = fopen(path, "rb")) == NULL)
209 UTIL_Format(error, sizeof(error), "Could not open binary: %s", path);
214 fseek(fp, 0, SEEK_END);
216 fseek(fp, 0, SEEK_SET);
218 buffer = malloc(size);
219 fread(buffer, size, 1, fp);
220 s_ServerBinCRC = UTIL_CRC32(buffer, size);
222 s_ServerBinCRC_Ok = true;
225 if (error[0] != '\0')
228 g_Logger.LogError("[SM] Error while parsing CRC section for \"%s\" (%s):", m_Game, m_pFile);
229 g_Logger.LogError("[SM] %s", error);
231 m_ParseState = PSTATE_GAMEDEFS_CRC_BINARY;
235 /* No sub-sections allowed:
236 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
237 case PSTATE_GAMEDEFS_KEYS:
238 case PSTATE_GAMEDEFS_SUPPORTED:
239 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
240 case PSTATE_GAMEDEFS_CRC_BINARY:
244 /* If we don't know what we got, start ignoring */
250 return SMCResult_Continue;
253 SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
257 return SMCResult_Continue;
260 if (m_ParseState == PSTATE_GAMEDEFS_OFFSETS_OFFSET)
262 if (strcmp(key, "class") == 0)
264 strncopy(m_Class, value, sizeof(m_Class));
265 } else if (strcmp(key, "prop") == 0) {
266 strncopy(m_Prop, value, sizeof(m_Prop));
267 } else if (strcmp(key, PLATFORM_NAME) == 0) {
268 int val = atoi(value);
269 sm_trie_replace(m_pOffsets, m_offset, (void *)val);
271 } else if (m_ParseState == PSTATE_GAMEDEFS_KEYS) {
272 int id = m_pStrings->AddString(value);
273 sm_trie_replace(m_pKeys, key, (void *)id);
274 } else if (m_ParseState == PSTATE_GAMEDEFS_SUPPORTED) {
275 if (strcmp(key, "game") == 0
276 && (strcasecmp(value, g_Game) == 0
277 || strcasecmp(value, g_GameDesc) == 0
278 || strcasecmp(value, g_GameName) == 0)
281 bShouldBeReadingDefault = true;
283 } else if (m_ParseState == PSTATE_GAMEDEFS_SIGNATURES_SIG) {
284 if (strcmp(key, PLATFORM_NAME) == 0)
286 strncopy(s_TempSig.sig, value, sizeof(s_TempSig.sig));
287 } else if (strcmp(key, "library") == 0) {
288 strncopy(s_TempSig.library, value, sizeof(s_TempSig.library));
290 } else if (m_ParseState == PSTATE_GAMEDEFS_CRC_BINARY) {
291 if (strcmp(key, PLATFORM_NAME) == 0
293 && !bShouldBeReadingDefault)
295 unsigned int crc = 0;
296 sscanf(value, "%08X", &crc);
297 if (s_ServerBinCRC == crc)
299 bShouldBeReadingDefault = true;
304 return SMCResult_Continue;
307 SMCResult CGameConfig::ReadSMC_LeavingSection(const SMCStates *states)
312 return SMCResult_Continue;
315 switch (m_ParseState)
319 m_ParseState = PSTATE_NONE;
322 case PSTATE_GAMEDEFS:
324 m_ParseState = PSTATE_GAMES;
327 case PSTATE_GAMEDEFS_KEYS:
328 case PSTATE_GAMEDEFS_OFFSETS:
330 m_ParseState = PSTATE_GAMEDEFS;
333 case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
335 /* Parse the offset... */
336 if (m_Class[0] != '\0'
337 && m_Prop[0] != '\0')
339 SendProp *pProp = g_HL2.FindInSendTable(m_Class, m_Prop);
342 int val = pProp->GetOffset();
343 sm_trie_replace(m_pOffsets, m_offset, (void *)val);
344 sm_trie_replace(m_pProps, m_offset, pProp);
346 /* Check if it's a non-default game and no offsets exist */
347 if (((strcmp(m_Game, "*") != 0) && strcmp(m_Game, "#default") != 0)
348 && (!sm_trie_retrieve(m_pOffsets, m_offset, NULL)))
350 g_Logger.LogError("[SM] Unable to find property %s.%s (file \"%s\") (mod \"%s\")",
358 m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
361 case PSTATE_GAMEDEFS_CRC:
362 case PSTATE_GAMEDEFS_SUPPORTED:
364 if (!bShouldBeReadingDefault)
366 /* If we shouldn't read the rest of this section, set the ignore level. */
368 m_ParseState = PSTATE_GAMES;
370 m_ParseState = PSTATE_GAMEDEFS;
374 case PSTATE_GAMEDEFS_CRC_BINARY:
376 m_ParseState = PSTATE_GAMEDEFS_CRC;
379 case PSTATE_GAMEDEFS_SIGNATURES:
381 m_ParseState = PSTATE_GAMEDEFS;
384 case PSTATE_GAMEDEFS_SIGNATURES_SIG:
386 if (s_TempSig.library[0] == '\0')
389 strncopy(s_TempSig.library, "server", sizeof(s_TempSig.library));
391 void *addrInBase = NULL;
392 if (strcmp(s_TempSig.library, "server") == 0)
394 addrInBase = (void *)g_SMAPI->GetServerFactory(false);
395 } else if (strcmp(s_TempSig.library, "engine") == 0) {
396 addrInBase = (void *)g_SMAPI->GetEngineFactory(false);
398 void *final_addr = NULL;
399 if (addrInBase == NULL)
401 g_Logger.LogError("[SM] Unrecognized library \"%s\" (gameconf \"%s\")",
405 #if defined PLATFORM_LINUX
406 if (s_TempSig.sig[0] == '@')
409 /* GNU only: returns 0 on error, inconsistent! >:[ */
410 if (dladdr(addrInBase, &info) != 0)
412 void *handle = dlopen(info.dli_fname, RTLD_NOW);
415 final_addr = dlsym(handle, &s_TempSig.sig[1]);
418 g_Logger.LogError("[SM] Unable to load library \"%s\" (gameconf \"%s\")",
423 g_Logger.LogError("[SM] Unable to find library \"%s\" in memory (gameconf \"%s\")",
433 /* First, preprocess the signature */
439 length = strlen(s_TempSig.sig);
441 for (size_t i=0; i<length; i++)
443 if (real_bytes >= sizeof(real_sig))
447 real_sig[real_bytes++] = s_TempSig.sig[i];
448 if (s_TempSig.sig[i] == '\\'
449 && s_TempSig.sig[i+1] == 'x')
455 /* Get the hex part */
458 s_byte[0] = s_TempSig.sig[i+2];
459 s_byte[1] = s_TempSig.sig[i+3];
461 /* Read it as an integer */
462 sscanf(s_byte, "%x", &r_byte);
464 real_sig[real_bytes-1] = r_byte;
472 final_addr = g_MemUtils.FindPattern(addrInBase, real_sig, real_bytes);
476 #if defined PLATFORM_LINUX
479 sm_trie_replace(m_pSigs, m_offset, final_addr);
480 m_ParseState = PSTATE_GAMEDEFS_SIGNATURES;
486 return SMCResult_Continue;
489 bool CGameConfig::Reparse(char *error, size_t maxlength)
492 SMCStates state = {0, 0};
494 char path[PLATFORM_MAX_PATH];
495 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "gamedata/%s.txt", m_pFile);
496 /* Backwards compatibility */
497 if (!g_LibSys.PathExists(path))
499 g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "configs/gamedata/%s.txt", m_pFile);
502 /* Initialize parse states */
504 bShouldBeReadingDefault = true;
505 m_ParseState = PSTATE_NONE;
506 /* Reset cached data */
508 sm_trie_clear(m_pOffsets);
509 sm_trie_clear(m_pProps);
510 sm_trie_clear(m_pKeys);
512 if ((err=textparsers->ParseSMCFile(path, this, &state, error, maxlength))
517 msg = textparsers->GetSMCErrorString(err);
519 g_Logger.LogError("[SM] Error parsing gameconfig file \"%s\":", path);
520 g_Logger.LogError("[SM] Error %d on line %d, col %d: %s",
524 msg ? msg : "Unknown error");
531 bool CGameConfig::GetOffset(const char *key, int *value)
535 if (!sm_trie_retrieve(m_pOffsets, key, &obj))
545 const char *CGameConfig::GetKeyValue(const char *key)
548 if (!sm_trie_retrieve(m_pKeys, key, &obj))
552 return m_pStrings->GetString((int)obj);
555 SendProp *CGameConfig::GetSendProp(const char *key)
559 if (!sm_trie_retrieve(m_pProps, key, (void **)&pProp))
567 bool CGameConfig::GetMemSig(const char *key, void **addr)
569 return sm_trie_retrieve(m_pSigs, key, addr);
572 void CGameConfig::IncRefCount()
577 unsigned int CGameConfig::DecRefCount()
583 GameConfigManager::GameConfigManager()
585 m_pLookup = sm_trie_create();
588 GameConfigManager::~GameConfigManager()
590 sm_trie_destroy(m_pLookup);
593 void GameConfigManager::OnSourceModStartup(bool late)
595 LoadGameConfigFile("core.games", &g_pGameConf, NULL, 0);
597 strncopy(g_Game, g_SourceMod.GetGameFolderName(), sizeof(g_Game));
598 strncopy(g_GameDesc + 1, SERVER_CALL(GetGameDescription)(), sizeof(g_GameDesc) - 1);
600 KeyValues *pGameInfo = new KeyValues("GameInfo");
601 if (pGameInfo->LoadFromFile(basefilesystem, "gameinfo.txt"))
604 if ((str = pGameInfo->GetString("game", NULL)) != NULL)
606 strncopy(g_GameName + 1, str, sizeof(g_GameName) - 1);
609 pGameInfo->deleteThis();
612 void GameConfigManager::OnSourceModAllInitialized()
614 /* NOW initialize the game file */
615 CGameConfig *pGameConf = (CGameConfig *)g_pGameConf;
618 if (!pGameConf->Reparse(error, sizeof(error)))
623 g_ShareSys.AddInterface(NULL, this);
626 void GameConfigManager::OnSourceModAllShutdown()
628 CloseGameConfigFile(g_pGameConf);
631 bool GameConfigManager::LoadGameConfigFile(const char *file, IGameConfig **_pConfig, char *error, size_t maxlength)
633 CGameConfig *pConfig;
635 if (sm_trie_retrieve(m_pLookup, file, (void **)&pConfig))
637 pConfig->IncRefCount();
642 pConfig = new CGameConfig(file);
644 /* :HACKHACK: Don't parse the main config file */
646 if (_pConfig != &g_pGameConf)
648 retval = pConfig->Reparse(error, maxlength);
651 m_cfgs.push_back(pConfig);
652 sm_trie_insert(m_pLookup, file, pConfig);
654 pConfig->IncRefCount();
661 void GameConfigManager::CloseGameConfigFile(IGameConfig *cfg)
663 CGameConfig *pConfig = (CGameConfig *)cfg;
665 if (pConfig->DecRefCount() == 0)
667 sm_trie_delete(m_pLookup, pConfig->m_pFile);
672 extern HandleType_t g_GameConfigsType;
674 IGameConfig *GameConfigManager::ReadHandle(Handle_t hndl, IdentityToken_t *ident, HandleError *err)
676 HandleSecurity sec(ident, g_pCoreIdent);
677 IGameConfig *conf = NULL;
678 HandleError _err = g_HandleSys.ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&conf);