mirror of
https://github.com/HIllya51/LunaHook.git
synced 2025-01-13 05:13:58 +08:00
Update textthread.cpp
This commit is contained in:
parent
de10b46b30
commit
fa45bfac5c
@ -12,16 +12,17 @@ static bool RemoveRepetition(std::wstring& text)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
TextThread::TextThread(ThreadParam tp, HookParam hp, std::optional<std::wstring> name) :
|
TextThread::TextThread(ThreadParam tp, HookParam hp, std::optional<std::wstring> name) : handle(threadCounter++),
|
||||||
handle(threadCounter++),
|
|
||||||
name(name.value_or(StringToWideString(hp.name))),
|
name(name.value_or(StringToWideString(hp.name))),
|
||||||
tp(tp),
|
tp(tp),
|
||||||
hp(hp)
|
hp(hp)
|
||||||
{}
|
{
|
||||||
|
}
|
||||||
|
|
||||||
void TextThread::Start()
|
void TextThread::Start()
|
||||||
{
|
{
|
||||||
CreateTimerQueueTimer(&timer, NULL, [](void* This, auto) { ((TextThread*)This)->Flush(); }, this, 10, 10, WT_EXECUTELONGFUNCTION);
|
CreateTimerQueueTimer(&timer, NULL, [](void *This, auto)
|
||||||
|
{ ((TextThread *)This)->Flush(); }, this, 10, 10, WT_EXECUTELONGFUNCTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextThread::Stop()
|
void TextThread::Stop()
|
||||||
@ -36,7 +37,8 @@ void TextThread::AddSentence(std::wstring sentence)
|
|||||||
|
|
||||||
void TextThread::Push(BYTE *data, int length)
|
void TextThread::Push(BYTE *data, int length)
|
||||||
{
|
{
|
||||||
if (length < 0) return;
|
if (length < 0)
|
||||||
|
return;
|
||||||
std::scoped_lock lock(bufferMutex);
|
std::scoped_lock lock(bufferMutex);
|
||||||
|
|
||||||
BYTE doubleByteChar[2];
|
BYTE doubleByteChar[2];
|
||||||
@ -60,15 +62,19 @@ void TextThread::Push(BYTE* data, int length)
|
|||||||
if (converted)
|
if (converted)
|
||||||
{
|
{
|
||||||
buffer.append(converted.value());
|
buffer.append(converted.value());
|
||||||
if (hp.type & FULL_STRING && converted.value().size()>1) buffer.push_back(L'\n');
|
if (hp.type & FULL_STRING && converted.value().size() > 1)
|
||||||
|
buffer.push_back(L'\n');
|
||||||
}
|
}
|
||||||
else Host::AddConsoleOutput(INVALID_CODEPAGE);
|
else
|
||||||
|
Host::AddConsoleOutput(INVALID_CODEPAGE);
|
||||||
|
|
||||||
lastPushTime = GetTickCount64();
|
lastPushTime = GetTickCount64();
|
||||||
|
|
||||||
if (filterRepetition)
|
if (filterRepetition)
|
||||||
{
|
{
|
||||||
if (std::all_of(buffer.begin(), buffer.end(), [&](wchar_t ch) { return repeatingChars.find(ch) != repeatingChars.end(); })) buffer.clear();
|
if (std::all_of(buffer.begin(), buffer.end(), [&](wchar_t ch)
|
||||||
|
{ 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());
|
||||||
@ -77,7 +83,7 @@ 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();
|
||||||
@ -96,7 +102,8 @@ void TextThread::Flush()
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
auto storage = this->storage.Acquire();
|
auto storage = this->storage.Acquire();
|
||||||
if (storage->size() > maxHistorySize) storage->erase(0, storage->size() - maxHistorySize); // https://github.com/Artikash/Textractor/issues/127#issuecomment-486882983
|
if (storage->size() > maxHistorySize)
|
||||||
|
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;
|
||||||
@ -106,11 +113,13 @@ void TextThread::Flush()
|
|||||||
{
|
{
|
||||||
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)) storage->append(sentence+L"\n");
|
if (Output(*this, sentence))
|
||||||
|
storage->append(sentence + L"\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::scoped_lock lock(bufferMutex);
|
std::scoped_lock lock(bufferMutex);
|
||||||
if (buffer.empty()) return;
|
if (buffer.empty())
|
||||||
|
return;
|
||||||
if (buffer.size() > maxBufferSize || GetTickCount64() - lastPushTime > flushDelay)
|
if (buffer.size() > maxBufferSize || GetTickCount64() - lastPushTime > flushDelay)
|
||||||
{
|
{
|
||||||
AddSentence(std::move(buffer));
|
AddSentence(std::move(buffer));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user