From 06b534c07d1987174bbe71ad49c71c142a4048cd Mon Sep 17 00:00:00 2001 From: def Date: Tue, 3 Nov 2020 14:48:24 +0100 Subject: [PATCH] Create fallback wordlist in sourcecode (fixes #3206) --- CMakeLists.txt | 8 ++++++++ scripts/wordlist.py | 9 +++++++++ src/game/server/score.cpp | 37 +++++++++++++++++++++---------------- 3 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 scripts/wordlist.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 9768d4259..5c06e1bea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1410,6 +1410,13 @@ generate_source7("src/game/generated/client_data7.h" "client_content_header") generate_maps("src/game/generated/protocolglue.h") +add_custom_command(OUTPUT "src/game/generated/wordlist.h" + COMMAND ${PYTHON_EXECUTABLE} scripts/wordlist.py > ${PROJECT_BINARY_DIR}/src/game/generated/wordlist.h + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + DEPENDS + scripts/wordlist.py +) + ######################################################################## # SHARED ######################################################################## @@ -1976,6 +1983,7 @@ set_src(GAME_SERVER GLOB_RECURSE src/game/server set(GAME_GENERATED_SERVER "src/game/generated/server_data.cpp" "src/game/generated/server_data.h" + "src/game/generated/wordlist.h" ) set(SERVER_SRC ${ENGINE_SERVER} ${GAME_SERVER} ${GAME_GENERATED_SERVER}) if(TARGET_OS STREQUAL "windows") diff --git a/scripts/wordlist.py b/scripts/wordlist.py new file mode 100644 index 000000000..688c62d4f --- /dev/null +++ b/scripts/wordlist.py @@ -0,0 +1,9 @@ +print("#ifndef GENERATED_WORDLIST_H") +print("#define GENERATED_WORDLIST_H") +print("const char g_aFallbackWordlist[][32] = {") +with open("data/wordlist.txt") as f: + for line in f: + word = line.strip().split("\t")[1] + print("\t\"%s\", " % word) +print("};") +print("#endif // GENERATED_WORDLIST_H") diff --git a/src/game/server/score.cpp b/src/game/server/score.cpp index c64c01efc..86526a1c8 100644 --- a/src/game/server/score.cpp +++ b/src/game/server/score.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -149,33 +150,37 @@ CScore::CScore(CGameContext *pGameServer, CDbConnectionPool *pPool) : ((CGameControllerDDRace *)(pGameServer->m_pController))->m_pInitResult = InitResult; str_copy(Tmp->m_Map, g_Config.m_SvMap, sizeof(Tmp->m_Map)); - IOHANDLE File = GameServer()->Storage()->OpenFile("wordlist.txt", IOFLAG_READ, IStorage::TYPE_ALL); - if(!File) - { - dbg_msg("sql", "failed to open wordlist"); - Server()->SetErrorShutdown("sql open wordlist error"); - return; - } - uint64 aSeed[2]; secure_random_fill(aSeed, sizeof(aSeed)); m_Prng.Seed(aSeed); - CLineReader LineReader; - LineReader.Init(File); - char *pLine; - while((pLine = LineReader.Get())) + + IOHANDLE File = GameServer()->Storage()->OpenFile("wordlist.txt", IOFLAG_READ, IStorage::TYPE_ALL); + if(File) { - char Word[32] = {0}; - sscanf(pLine, "%*s %31s", Word); - Word[31] = 0; - m_aWordlist.push_back(Word); + CLineReader LineReader; + LineReader.Init(File); + char *pLine; + while((pLine = LineReader.Get())) + { + char Word[32] = {0}; + sscanf(pLine, "%*s %31s", Word); + Word[31] = 0; + m_aWordlist.push_back(Word); + } } + else + { + dbg_msg("sql", "failed to open wordlist, using fallback"); + m_aWordlist.assign(std::begin(g_aFallbackWordlist), std::end(g_aFallbackWordlist)); + } + if(m_aWordlist.size() < 1000) { dbg_msg("sql", "too few words in wordlist"); Server()->SetErrorShutdown("sql too few words in wordlist"); return; } + m_pPool->Execute(Init, std::move(Tmp), "load best time"); }