From e967bf23980c2309eb6c3cb0a5c490330c221800 Mon Sep 17 00:00:00 2001 From: wincent Date: Tue, 7 Dec 2021 21:03:55 +0100 Subject: [PATCH] Updated solution --- dChatFilter/dChatFilter.cpp | 34 +++++++++++++++++++--------------- dChatFilter/dChatFilter.h | 4 ++-- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/dChatFilter/dChatFilter.cpp b/dChatFilter/dChatFilter.cpp index 65fb6ea0..7f8187a5 100644 --- a/dChatFilter/dChatFilter.cpp +++ b/dChatFilter/dChatFilter.cpp @@ -21,8 +21,9 @@ dChatFilter::dChatFilter(const std::string& filepath, bool dontGenerateDCF) { ReadWordlistPlaintext(filepath + ".txt"); if (!m_DontGenerateDCF) ExportWordlistToDCF(filepath + ".dcf"); } - else { - ReadWordlistDCF(filepath + ".dcf"); + else if (!ReadWordlistDCF(filepath + ".dcf")) { + ReadWordlistPlaintext(filepath + ".txt"); + ExportWordlistToDCF(filepath + ".dcf"); } //Read player names that are ok as well: @@ -46,24 +47,24 @@ void dChatFilter::ReadWordlistPlaintext(const std::string& filepath) { if (file) { std::string line; while (std::getline(file, line)) { + line.erase(std::remove(line.begin(), line.end(), '\r'), line.end()); std::transform(line.begin(), line.end(), line.begin(), ::tolower); //Transform to lowercase m_Words.push_back(CalculateHash(line)); } } } -void dChatFilter::ReadWordlistDCF(const std::string& filepath) { +bool dChatFilter::ReadWordlistDCF(const std::string& filepath) { std::ifstream file(filepath, std::ios::binary); if (file) { fileHeader hdr; BinaryIO::BinaryRead(file, hdr); if (hdr.header != header) { - std::cout << "Wrong file header!" << std::endl; file.close(); - return; + return false; } - if (hdr.formatVersion <= formatVersion) { + if (hdr.formatVersion == formatVersion) { size_t wordsToRead = 0; BinaryIO::BinaryRead(file, wordsToRead); m_Words.reserve(wordsToRead); @@ -73,17 +74,20 @@ void dChatFilter::ReadWordlistDCF(const std::string& filepath) { BinaryIO::BinaryRead(file, word); m_Words.push_back(word); } + + return true; } else { - std::cout << "Newer file or corrupt" << std::endl; file.close(); - return; + return false; } } + + return false; } void dChatFilter::ExportWordlistToDCF(const std::string& filepath) { - std::ofstream file(filepath, std::ios::binary | std::ios_base::out ); + std::ofstream file(filepath, std::ios::binary | std::ios_base::out); if (file) { BinaryIO::BinaryWrite(file, uint32_t(dChatFilterDCF::header)); BinaryIO::BinaryWrite(file, uint32_t(dChatFilterDCF::formatVersion)); @@ -102,14 +106,14 @@ bool dChatFilter::IsSentenceOkay(const std::string& message, int gmLevel) { if (message.empty()) return true; std::stringstream sMessage(message); - std::string line; + std::string segment; std::regex reg("(!*|\\?*|\\;*|\\.*|\\,*)"); - while (std::getline(sMessage, line)) { - line.erase(std::remove(line.begin(), line.end(), '\r'), line.end()); //Remove nix line-endings - std::transform(line.begin(), line.end(), line.begin(), ::tolower); //Transform to lowercase - line = std::regex_replace(line, reg, ""); - size_t hash = CalculateHash(line); + while (std::getline(sMessage, segment, ' ')) { + std::transform(segment.begin(), segment.end(), segment.begin(), ::tolower); //Transform to lowercase + segment = std::regex_replace(segment, reg, ""); + + size_t hash = CalculateHash(segment); if (std::find(m_UserUnapprovedWordCache.begin(), m_UserUnapprovedWordCache.end(), hash) != m_UserUnapprovedWordCache.end()) { return false; diff --git a/dChatFilter/dChatFilter.h b/dChatFilter/dChatFilter.h index 644f78d7..e8ae67d0 100644 --- a/dChatFilter/dChatFilter.h +++ b/dChatFilter/dChatFilter.h @@ -6,7 +6,7 @@ namespace dChatFilterDCF { static const uint32_t header = ('D' + ('C' << 8) + ('F' << 16) + ('B' << 24)); - static const uint32_t formatVersion = 1; + static const uint32_t formatVersion = 2; struct fileHeader { uint32_t header; @@ -21,7 +21,7 @@ public: ~dChatFilter(); void ReadWordlistPlaintext(const std::string & filepath); - void ReadWordlistDCF(const std::string & filepath); + bool ReadWordlistDCF(const std::string & filepath); void ExportWordlistToDCF(const std::string & filepath); bool IsSentenceOkay(const std::string& message, int gmLevel);