Revert "Update textthread.cpp"

This reverts commit fa45bfac5c.
This commit is contained in:
恍兮惚兮 2024-09-02 17:30:20 +08:00
parent fa45bfac5c
commit e6c618cf64

View File

@ -1,28 +1,27 @@
#include "textthread.h" #include "textthread.h"
#include "host.h" #include "host.h"
#include "Lang/Lang.h" #include"Lang/Lang.h"
// return true if repetition found (see https://github.com/Artikash/Textractor/issues/40) // return true if repetition found (see https://github.com/Artikash/Textractor/issues/40)
static bool RemoveRepetition(std::wstring &text) static bool RemoveRepetition(std::wstring& text)
{ {
wchar_t *end = text.data() + text.size(); wchar_t* end = text.data() + text.size();
for (int length = text.size() / 3; length > 6; --length) for (int length = text.size() / 3; length > 6; --length)
if (memcmp(end - length * 3, end - length * 2, length * sizeof(wchar_t)) == 0 && memcmp(end - length * 3, end - length * 1, length * sizeof(wchar_t)) == 0) if (memcmp(end - length * 3, end - length * 2, length * sizeof(wchar_t)) == 0 && memcmp(end - length * 3, end - length * 1, length * sizeof(wchar_t)) == 0)
return RemoveRepetition(text = std::wstring(end - length, length)), true; return RemoveRepetition(text = std::wstring(end - length, length)), true;
return false; return false;
} }
TextThread::TextThread(ThreadParam tp, HookParam hp, std::optional<std::wstring> name) : handle(threadCounter++), TextThread::TextThread(ThreadParam tp, HookParam hp, std::optional<std::wstring> name) :
name(name.value_or(StringToWideString(hp.name))), handle(threadCounter++),
tp(tp), name(name.value_or(StringToWideString(hp.name))),
hp(hp) tp(tp),
{ hp(hp)
} {}
void TextThread::Start() void TextThread::Start()
{ {
CreateTimerQueueTimer(&timer, NULL, [](void *This, auto) CreateTimerQueueTimer(&timer, NULL, [](void* This, auto) { ((TextThread*)This)->Flush(); }, this, 10, 10, WT_EXECUTELONGFUNCTION);
{ ((TextThread *)This)->Flush(); }, this, 10, 10, WT_EXECUTELONGFUNCTION);
} }
void TextThread::Stop() void TextThread::Stop()
@ -35,10 +34,9 @@ void TextThread::AddSentence(std::wstring sentence)
queuedSentences->emplace_back(std::move(sentence)); queuedSentences->emplace_back(std::move(sentence));
} }
void TextThread::Push(BYTE *data, int length) void TextThread::Push(BYTE* data, int length)
{ {
if (length < 0) if (length < 0) return;
return;
std::scoped_lock lock(bufferMutex); std::scoped_lock lock(bufferMutex);
BYTE doubleByteChar[2]; BYTE doubleByteChar[2];
@ -58,23 +56,19 @@ void TextThread::Push(BYTE *data, int length)
length = 0; length = 0;
} }
} }
auto converted = commonparsestring(data, length, &hp, Host::defaultCodepage); auto converted = commonparsestring(data,length,&hp,Host::defaultCodepage);
if (converted) if(converted)
{ {
buffer.append(converted.value()); buffer.append(converted.value());
if (hp.type & FULL_STRING && converted.value().size() > 1) if (hp.type & FULL_STRING && converted.value().size()>1) buffer.push_back(L'\n');
buffer.push_back(L'\n');
} }
else else Host::AddConsoleOutput(INVALID_CODEPAGE);
Host::AddConsoleOutput(INVALID_CODEPAGE);
lastPushTime = GetTickCount64(); lastPushTime = GetTickCount64();
if (filterRepetition) if (filterRepetition)
{ {
if (std::all_of(buffer.begin(), buffer.end(), [&](wchar_t ch) if (std::all_of(buffer.begin(), buffer.end(), [&](wchar_t ch) { return repeatingChars.find(ch) != repeatingChars.end(); })) buffer.clear();
{ return repeatingChars.find(ch) != repeatingChars.end(); }))
buffer.clear();
if (RemoveRepetition(buffer)) // sentence repetition detected, which means the entire sentence has already been received if (RemoveRepetition(buffer)) // sentence repetition detected, which means the entire sentence has already been received
{ {
repeatingChars = std::unordered_set(buffer.begin(), buffer.end()); repeatingChars = std::unordered_set(buffer.begin(), buffer.end());
@ -83,14 +77,14 @@ void TextThread::Push(BYTE *data, int length)
} }
} }
if (flushDelay == 0) // && hp.type & FULL_STRING) if (flushDelay == 0 && hp.type & FULL_STRING)
{ {
AddSentence(std::move(buffer)); AddSentence(std::move(buffer));
buffer.clear(); buffer.clear();
} }
} }
void TextThread::Push(const wchar_t *data) void TextThread::Push(const wchar_t* data)
{ {
std::scoped_lock lock(bufferMutex); std::scoped_lock lock(bufferMutex);
// not sure if this should filter repetition // not sure if this should filter repetition
@ -102,24 +96,21 @@ void TextThread::Flush()
{ {
{ {
auto storage = this->storage.Acquire(); auto storage = this->storage.Acquire();
if (storage->size() > maxHistorySize) if (storage->size() > maxHistorySize) storage->erase(0, storage->size() - maxHistorySize); // https://github.com/Artikash/Textractor/issues/127#issuecomment-486882983
storage->erase(0, storage->size() - maxHistorySize); // https://github.com/Artikash/Textractor/issues/127#issuecomment-486882983
} }
std::vector<std::wstring> sentences; std::vector<std::wstring> sentences;
queuedSentences->swap(sentences); queuedSentences->swap(sentences);
int totalSize = 0; int totalSize = 0;
for (auto &sentence : sentences) for (auto& sentence : sentences)
{ {
totalSize += sentence.size(); totalSize += sentence.size();
sentence.erase(std::remove(sentence.begin(), sentence.end(), 0), sentence.end()); sentence.erase(std::remove(sentence.begin(), sentence.end(), 0), sentence.end());
if (Output(*this, sentence)) if (Output(*this, sentence)) storage->append(sentence+L"\n");
storage->append(sentence + L"\n");
} }
std::scoped_lock lock(bufferMutex); std::scoped_lock lock(bufferMutex);
if (buffer.empty()) if (buffer.empty()) return;
return;
if (buffer.size() > maxBufferSize || GetTickCount64() - lastPushTime > flushDelay) if (buffer.size() > maxBufferSize || GetTickCount64() - lastPushTime > flushDelay)
{ {
AddSentence(std::move(buffer)); AddSentence(std::move(buffer));