refactor all extensions. move repetition detection to extension
This commit is contained in:
parent
ca749691f8
commit
d31748c105
@ -54,8 +54,6 @@ void TextThread::AddSentence(std::wstring sentence)
|
||||
void TextThread::AddText(const BYTE *con, int len)
|
||||
{
|
||||
LOCK(ttMutex);
|
||||
// Artikash 8/27/2018: add repetition filter
|
||||
if (len > 6 && buffer.data() && (strstr(buffer.data(), (const char*)con) || wcsstr((const wchar_t*)buffer.data(), (const wchar_t*)con))) return;
|
||||
buffer.insert(buffer.end(), con, con + len);
|
||||
timestamp = GetTickCount();
|
||||
}
|
||||
|
@ -1,28 +1,16 @@
|
||||
#include "extensions.h"
|
||||
|
||||
extern "C"
|
||||
bool ProcessSentence(std::wstring& sentence, const InfoForExtension* miscInfo)
|
||||
{
|
||||
/**
|
||||
* Param sentence: pointer to sentence received by NextHooker (UTF-16).
|
||||
* You should not modify this sentence. If you want NextHooker to receive a modified sentence, copy it into your own buffer and return that.
|
||||
* Param miscInfo: pointer to start of singly linked list containing misc info about the sentence.
|
||||
* Return value: pointer to sentence NextHooker takes for future processing and display.
|
||||
* Return 'sentence' unless you created a new sentence/buffer as mentioned above.
|
||||
* NextHooker will display the sentence after all extensions have had a chance to process and/or modify it.
|
||||
* THIS FUNCTION MAY BE RUN SEVERAL TIMES CONCURRENTLY: PLEASE ENSURE THAT IT IS THREAD SAFE!
|
||||
*/
|
||||
__declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sentence, const InfoForExtension* miscInfo)
|
||||
{
|
||||
if (GetProperty("current select", miscInfo) && GetProperty("hook address", miscInfo) != -1)
|
||||
{
|
||||
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (wcslen(sentence) + 1) * sizeof(wchar_t));
|
||||
memcpy(GlobalLock(hMem), sentence, (wcslen(sentence) + 1) * sizeof(wchar_t));
|
||||
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (sentence.size() + 2) * sizeof(wchar_t));
|
||||
memcpy(GlobalLock(hMem), sentence.c_str(), (sentence.size() + 2) * sizeof(wchar_t));
|
||||
GlobalUnlock(hMem);
|
||||
OpenClipboard(0);
|
||||
EmptyClipboard();
|
||||
SetClipboardData(CF_UNICODETEXT, hMem);
|
||||
CloseClipboard();
|
||||
}
|
||||
return sentence;
|
||||
}
|
||||
return false;
|
||||
}
|
@ -21,3 +21,33 @@ int GetProperty(const char* propertyName, const InfoForExtension* miscInfo)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Param sentence: entence received by NextHooker (UTF-16).
|
||||
* Param miscInfo: pointer to start of singly linked list containing misc info about the sentence.
|
||||
* Return value: whether the sentence was modified.
|
||||
* NextHooker will display the sentence after all extensions have had a chance to process and/or modify it.
|
||||
* THIS FUNCTION MAY BE RUN SEVERAL TIMES CONCURRENTLY: PLEASE ENSURE THAT IT IS THREAD SAFE!
|
||||
*/
|
||||
bool ProcessSentence(std::wstring& sentence, const InfoForExtension* miscInfo);
|
||||
|
||||
/**
|
||||
* Param sentence: pointer to sentence received by NextHooker (UTF-16).
|
||||
* You should not modify this sentence. If you want NextHooker to receive a modified sentence, copy it into your own buffer and return that.
|
||||
* Param miscInfo: pointer to start of singly linked list containing misc info about the sentence.
|
||||
* Return value: pointer to sentence NextHooker takes for future processing and display.
|
||||
* Return 'sentence' unless you created a new sentence/buffer as mentioned above.
|
||||
* NextHooker will display the sentence after all extensions have had a chance to process and/or modify it.
|
||||
* THIS FUNCTION MAY BE RUN SEVERAL TIMES CONCURRENTLY: PLEASE ENSURE THAT IT IS THREAD SAFE!
|
||||
*/
|
||||
extern "C" __declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sentenceArr, const InfoForExtension* miscInfo)
|
||||
{
|
||||
std::wstring sentence(sentenceArr);
|
||||
if (ProcessSentence(sentence, miscInfo))
|
||||
{
|
||||
wchar_t* newSentence = (wchar_t*)malloc((sentence.size() + 1) * sizeof(wchar_t*));
|
||||
wcscpy(newSentence, sentence.c_str());
|
||||
return newSentence;
|
||||
}
|
||||
else return sentenceArr;
|
||||
}
|
@ -1,21 +1,8 @@
|
||||
#include "extensions.h"
|
||||
|
||||
extern "C"
|
||||
bool ProcessSentence(std::wstring& sentence, const InfoForExtension* miscInfo)
|
||||
{
|
||||
/**
|
||||
* Param sentence: pointer to sentence received by NextHooker (UTF-16).
|
||||
* You should not modify this sentence. If you want NextHooker to receive a modified sentence, copy it into your own buffer and return that.
|
||||
* Param miscInfo: pointer to start of singly linked list containing misc info about the sentence.
|
||||
* Return value: pointer to sentence NextHooker takes for future processing and display.
|
||||
* Return 'sentence' unless you created a new sentence/buffer as mentioned above.
|
||||
* NextHooker will display the sentence after all extensions have had a chance to process and/or modify it.
|
||||
* THIS FUNCTION MAY BE RUN SEVERAL TIMES CONCURRENTLY: PLEASE ENSURE THAT IT IS THREAD SAFE!
|
||||
*/
|
||||
__declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sentence, const InfoForExtension* miscInfo)
|
||||
{
|
||||
if (GetProperty("hook address", miscInfo) == -1) return sentence;
|
||||
wchar_t* newSentence = (wchar_t*)malloc((wcslen(sentence) + 6) * sizeof(wchar_t));
|
||||
swprintf(newSentence, wcslen(sentence) + 6, L"%s\r\n", sentence);
|
||||
return newSentence;
|
||||
}
|
||||
if (GetProperty("hook address", miscInfo) == -1) return false;
|
||||
sentence += L"\r\n";
|
||||
return true;
|
||||
}
|
@ -30,28 +30,15 @@ std::wstring GetTranslationUri(const wchar_t* text, unsigned int TKK)
|
||||
return std::wstring(L"/translate_a/single?client=t&dt=ld&dt=rm&dt=t&tk=") + std::to_wstring(a) + L"." + std::to_wstring(b) + L"&q=" + std::wstring(text);
|
||||
}
|
||||
|
||||
extern "C"
|
||||
bool ProcessSentence(std::wstring& sentence, const InfoForExtension* miscInfo)
|
||||
{
|
||||
/**
|
||||
* Param sentence: pointer to sentence received by NextHooker (UTF-16).
|
||||
* You should not modify this sentence. If you want NextHooker to receive a modified sentence, copy it into your own buffer and return that.
|
||||
* Param miscInfo: pointer to start of singly linked list containing misc info about the sentence.
|
||||
* Return value: pointer to sentence NextHooker takes for future processing and display.
|
||||
* Return 'sentence' unless you created a new sentence/buffer as mentioned above.
|
||||
* NextHooker will display the sentence after all extensions have had a chance to process and/or modify it.
|
||||
* THIS FUNCTION MAY BE RUN SEVERAL TIMES CONCURRENTLY: PLEASE ENSURE THAT IT IS THREAD SAFE!
|
||||
*/
|
||||
__declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sentence, const InfoForExtension* miscInfo)
|
||||
{
|
||||
static HINTERNET internet = NULL;
|
||||
if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 NextHooker", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0);
|
||||
static unsigned int TKK = 0;
|
||||
|
||||
wchar_t error[] = L"Error while translating.";
|
||||
std::wstring translation(L"");
|
||||
const wchar_t* message = error;
|
||||
|
||||
if (wcslen(sentence) > 2000 || GetProperty("hook address", miscInfo) == -1) return sentence;
|
||||
if (GetProperty("hook address", miscInfo) == -1) return false;
|
||||
|
||||
if (internet)
|
||||
{
|
||||
@ -75,7 +62,7 @@ extern "C"
|
||||
|
||||
if (HINTERNET connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
|
||||
{
|
||||
if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", GetTranslationUri(sentence, TKK).c_str(), NULL, NULL, NULL, WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE))
|
||||
if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", GetTranslationUri(sentence.c_str(), TKK).c_str(), NULL, NULL, NULL, WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE))
|
||||
{
|
||||
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
|
||||
{
|
||||
@ -97,7 +84,6 @@ extern "C"
|
||||
response = results.suffix().str();
|
||||
}
|
||||
for (auto& c : translation) if (c == L'\\') c = 0x200b;
|
||||
message = translation.c_str();
|
||||
}
|
||||
}
|
||||
WinHttpCloseHandle(request);
|
||||
@ -106,8 +92,7 @@ extern "C"
|
||||
}
|
||||
}
|
||||
|
||||
wchar_t* newSentence = (wchar_t*)malloc((wcslen(sentence) + 3 + wcslen(message)) * sizeof(wchar_t));
|
||||
swprintf(newSentence, wcslen(sentence) + 3 + wcslen(message), L"%s%s%s", sentence, L"\r\n", message);
|
||||
return newSentence;
|
||||
}
|
||||
if (translation == L"") translation = L"Error while translating.";
|
||||
sentence += L"\r\n" + translation;
|
||||
return true;
|
||||
}
|
@ -1,46 +1,60 @@
|
||||
#include "extensions.h"
|
||||
#include <set>
|
||||
#include <mutex>
|
||||
#include <algorithm>
|
||||
#include <cwctype>
|
||||
|
||||
std::wstring remove_side_spaces(const std::wstring& str)
|
||||
bool RemoveRepeatedChars(std::wstring& sentence)
|
||||
{
|
||||
auto begin = std::find_if_not(str.begin(), str.end(), std::iswspace);
|
||||
if (begin == str.end()) return L"";
|
||||
auto end = std::find_if_not(str.rbegin(), str.rend(), std::iswspace);
|
||||
return std::wstring(begin, end.base());
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
/**
|
||||
* Param sentence: pointer to sentence received by NextHooker (UTF-16).
|
||||
* You should not modify this sentence. If you want NextHooker to receive a modified sentence, copy it into your own buffer and return that.
|
||||
* Param miscInfo: pointer to start of singly linked list containing misc info about the sentence.
|
||||
* Return value: pointer to sentence NextHooker takes for future processing and display.
|
||||
* Return 'sentence' unless you created a new sentence/buffer as mentioned above.
|
||||
* NextHooker will display the sentence after all extensions have had a chance to process and/or modify it.
|
||||
* THIS FUNCTION MAY BE RUN SEVERAL TIMES CONCURRENTLY: PLEASE ENSURE THAT IT IS THREAD SAFE!
|
||||
*/
|
||||
__declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sentence, const InfoForExtension* miscInfo)
|
||||
{
|
||||
std::wstring sentenceStr = remove_side_spaces(std::wstring(sentence));
|
||||
unsigned long repeatNumber = 0;
|
||||
wchar_t prevChar = sentenceStr[0];
|
||||
for (auto i : sentenceStr)
|
||||
unsigned int repeatNumber = 0;
|
||||
wchar_t prevChar = sentence[0];
|
||||
for (auto i : sentence)
|
||||
if (i == prevChar) repeatNumber++;
|
||||
else break;
|
||||
if (repeatNumber == 1) return false;
|
||||
|
||||
for (int i = 0; i < sentenceStr.size(); i += repeatNumber)
|
||||
for (int j = i; j < sentenceStr.size(); ++j)
|
||||
if (sentenceStr[j] != sentenceStr[i])
|
||||
if ((j - i) % repeatNumber != 0) return sentence;
|
||||
for (int i = 0; i < sentence.size(); i += repeatNumber)
|
||||
for (int j = i; j < sentence.size(); ++j)
|
||||
if (sentence[j] != sentence[i])
|
||||
if ((j - i) % repeatNumber != 0) return false;
|
||||
else break;
|
||||
|
||||
if (repeatNumber == 1) return sentence;
|
||||
sentenceStr.erase(std::remove_if(sentenceStr.begin(), sentenceStr.end(), [&](const wchar_t& c) {return (&c - &*sentenceStr.begin()) % repeatNumber != 0; }), sentenceStr.end());
|
||||
|
||||
wchar_t* newSentence = (wchar_t*)malloc((sentenceStr.size() + 2) * sizeof(wchar_t));
|
||||
wcscpy(newSentence, sentenceStr.c_str());
|
||||
return newSentence;
|
||||
}
|
||||
// Removes every repeatNumber'th character.
|
||||
sentence.erase(std::remove_if(sentence.begin(), sentence.end(), [&](const wchar_t& c) {return (&c - &*sentence.begin()) % repeatNumber != 0; }), sentence.end());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RemoveCyclicRepeats(std::wstring& sentence)
|
||||
{
|
||||
unsigned int realLength = 6; // If the first 6 characters appear later on, there's probably a repetition issue.
|
||||
if (sentence.size() < realLength) return false;
|
||||
wchar_t realSentence[2000] = {};
|
||||
memcpy(realSentence, sentence.c_str(), realLength * sizeof(wchar_t));
|
||||
while (wcsstr(sentence.c_str() + realLength, realSentence))
|
||||
{
|
||||
realSentence[realLength] = sentence[realLength];
|
||||
if (++realLength >= 2000) return false;
|
||||
}
|
||||
if (realLength > 7)
|
||||
{
|
||||
sentence = std::wstring(realSentence);
|
||||
RemoveCyclicRepeats(sentence);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RemoveRepeatedSentences(std::wstring& sentence, int threadHandle)
|
||||
{
|
||||
static std::set<std::pair<int, std::wstring>> seenSentences;
|
||||
static std::mutex m;
|
||||
std::lock_guard<std::mutex> l(m);
|
||||
if (seenSentences.count({ threadHandle, sentence }) != 0) throw std::exception();
|
||||
seenSentences.insert({ threadHandle, sentence });
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ProcessSentence(std::wstring& sentence, const InfoForExtension* miscInfo)
|
||||
{
|
||||
if (GetProperty("hook address", miscInfo) == -1) return false;
|
||||
return RemoveRepeatedChars(sentence) | RemoveCyclicRepeats(sentence) | RemoveRepeatedSentences(sentence, GetProperty("thread handle", miscInfo));
|
||||
}
|
Loading…
Reference in New Issue
Block a user