Updated solution

This commit is contained in:
wincent 2021-12-07 21:03:55 +01:00
parent eede772b53
commit e967bf2398
2 changed files with 21 additions and 17 deletions

View File

@ -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;

View File

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