3237: Create fallback wordlist in sourcecode (fixes #3206) r=Learath2 a=def-

<!-- What is the motivation for the changes of this pull request -->

## Checklist

- [x] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test if it works standalone, system.c especially
- [x] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [x] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: def <dennis@felsin9.de>
This commit is contained in:
bors[bot] 2020-11-03 23:45:20 +00:00 committed by GitHub
commit dcfdd05911
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 16 deletions

View file

@ -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")

9
scripts/wordlist.py Normal file
View file

@ -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")

View file

@ -11,6 +11,7 @@
#include <engine/shared/console.h>
#include <engine/shared/linereader.h>
#include <engine/storage.h>
#include <game/generated/wordlist.h>
#include <algorithm>
#include <cstring>
@ -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");
}