mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2024-11-22 13:37:22 +00:00
Updated solution
This commit is contained in:
parent
eede772b53
commit
e967bf2398
@ -21,8 +21,9 @@ dChatFilter::dChatFilter(const std::string& filepath, bool dontGenerateDCF) {
|
|||||||
ReadWordlistPlaintext(filepath + ".txt");
|
ReadWordlistPlaintext(filepath + ".txt");
|
||||||
if (!m_DontGenerateDCF) ExportWordlistToDCF(filepath + ".dcf");
|
if (!m_DontGenerateDCF) ExportWordlistToDCF(filepath + ".dcf");
|
||||||
}
|
}
|
||||||
else {
|
else if (!ReadWordlistDCF(filepath + ".dcf")) {
|
||||||
ReadWordlistDCF(filepath + ".dcf");
|
ReadWordlistPlaintext(filepath + ".txt");
|
||||||
|
ExportWordlistToDCF(filepath + ".dcf");
|
||||||
}
|
}
|
||||||
|
|
||||||
//Read player names that are ok as well:
|
//Read player names that are ok as well:
|
||||||
@ -46,24 +47,24 @@ void dChatFilter::ReadWordlistPlaintext(const std::string& filepath) {
|
|||||||
if (file) {
|
if (file) {
|
||||||
std::string line;
|
std::string line;
|
||||||
while (std::getline(file, 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
|
std::transform(line.begin(), line.end(), line.begin(), ::tolower); //Transform to lowercase
|
||||||
m_Words.push_back(CalculateHash(line));
|
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);
|
std::ifstream file(filepath, std::ios::binary);
|
||||||
if (file) {
|
if (file) {
|
||||||
fileHeader hdr;
|
fileHeader hdr;
|
||||||
BinaryIO::BinaryRead(file, hdr);
|
BinaryIO::BinaryRead(file, hdr);
|
||||||
if (hdr.header != header) {
|
if (hdr.header != header) {
|
||||||
std::cout << "Wrong file header!" << std::endl;
|
|
||||||
file.close();
|
file.close();
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hdr.formatVersion <= formatVersion) {
|
if (hdr.formatVersion == formatVersion) {
|
||||||
size_t wordsToRead = 0;
|
size_t wordsToRead = 0;
|
||||||
BinaryIO::BinaryRead(file, wordsToRead);
|
BinaryIO::BinaryRead(file, wordsToRead);
|
||||||
m_Words.reserve(wordsToRead);
|
m_Words.reserve(wordsToRead);
|
||||||
@ -73,17 +74,20 @@ void dChatFilter::ReadWordlistDCF(const std::string& filepath) {
|
|||||||
BinaryIO::BinaryRead(file, word);
|
BinaryIO::BinaryRead(file, word);
|
||||||
m_Words.push_back(word);
|
m_Words.push_back(word);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
std::cout << "Newer file or corrupt" << std::endl;
|
|
||||||
file.close();
|
file.close();
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dChatFilter::ExportWordlistToDCF(const std::string& filepath) {
|
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) {
|
if (file) {
|
||||||
BinaryIO::BinaryWrite(file, uint32_t(dChatFilterDCF::header));
|
BinaryIO::BinaryWrite(file, uint32_t(dChatFilterDCF::header));
|
||||||
BinaryIO::BinaryWrite(file, uint32_t(dChatFilterDCF::formatVersion));
|
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;
|
if (message.empty()) return true;
|
||||||
|
|
||||||
std::stringstream sMessage(message);
|
std::stringstream sMessage(message);
|
||||||
std::string line;
|
std::string segment;
|
||||||
std::regex reg("(!*|\\?*|\\;*|\\.*|\\,*)");
|
std::regex reg("(!*|\\?*|\\;*|\\.*|\\,*)");
|
||||||
|
|
||||||
while (std::getline(sMessage, line)) {
|
while (std::getline(sMessage, segment, ' ')) {
|
||||||
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end()); //Remove nix line-endings
|
std::transform(segment.begin(), segment.end(), segment.begin(), ::tolower); //Transform to lowercase
|
||||||
std::transform(line.begin(), line.end(), line.begin(), ::tolower); //Transform to lowercase
|
segment = std::regex_replace(segment, reg, "");
|
||||||
line = std::regex_replace(line, reg, "");
|
|
||||||
size_t hash = CalculateHash(line);
|
size_t hash = CalculateHash(segment);
|
||||||
|
|
||||||
if (std::find(m_UserUnapprovedWordCache.begin(), m_UserUnapprovedWordCache.end(), hash) != m_UserUnapprovedWordCache.end()) {
|
if (std::find(m_UserUnapprovedWordCache.begin(), m_UserUnapprovedWordCache.end(), hash) != m_UserUnapprovedWordCache.end()) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
namespace dChatFilterDCF {
|
namespace dChatFilterDCF {
|
||||||
static const uint32_t header = ('D' + ('C' << 8) + ('F' << 16) + ('B' << 24));
|
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 {
|
struct fileHeader {
|
||||||
uint32_t header;
|
uint32_t header;
|
||||||
@ -21,7 +21,7 @@ public:
|
|||||||
~dChatFilter();
|
~dChatFilter();
|
||||||
|
|
||||||
void ReadWordlistPlaintext(const std::string & filepath);
|
void ReadWordlistPlaintext(const std::string & filepath);
|
||||||
void ReadWordlistDCF(const std::string & filepath);
|
bool ReadWordlistDCF(const std::string & filepath);
|
||||||
void ExportWordlistToDCF(const std::string & filepath);
|
void ExportWordlistToDCF(const std::string & filepath);
|
||||||
bool IsSentenceOkay(const std::string& message, int gmLevel);
|
bool IsSentenceOkay(const std::string& message, int gmLevel);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user