refactor extensions

This commit is contained in:
Akash Mozumdar 2018-09-22 15:08:31 -04:00
parent 665c5b3eea
commit 22c4f10c82
5 changed files with 28 additions and 32 deletions

View File

@ -1,8 +1,8 @@
#include "extensions.h" #include "extensions.h"
bool ProcessSentence(std::wstring& sentence, const InfoForExtension* miscInfo) bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{ {
if (GetProperty("current select", miscInfo) && GetProperty("hook address", miscInfo) != -1) if (sentenceInfo["current select"] == 1 && sentenceInfo["hook address"] != -1)
{ {
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (sentence.size() + 2) * 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)); memcpy(GlobalLock(hMem), sentence.c_str(), (sentence.size() + 2) * sizeof(wchar_t));

View File

@ -7,34 +7,29 @@
struct InfoForExtension struct InfoForExtension
{ {
const char* propertyName; const char* name;
int64_t propertyValue; int64_t value;
InfoForExtension* nextProperty; InfoForExtension* next;
}; };
// Traverses linked list to find info. struct SentenceInfo
int54_t GetProperty(const char* propertyName, const InfoForExtension* miscInfo)
{ {
const InfoForExtension* miscInfoTraverser = miscInfo; const InfoForExtension* list;
while (miscInfoTraverser != nullptr) // Traverse linked list to find info.
if (strcmp(propertyName, miscInfoTraverser->propertyName) == 0) return miscInfoTraverser->propertyValue; int64_t operator[](std::string propertyName)
else miscInfoTraverser = miscInfoTraverser->nextProperty; {
for (auto i = list; i != nullptr; i = i->next) if (propertyName == i->name) return i->value;
return 0; return 0;
} }
};
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo);
/** /**
* Param sentence: sentence received by NextHooker (UTF-16). * You shouldn't mess with this or even look at it unless you're certain you know what you're doing.
* 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). * 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. * You should not modify this sentence. If you want NextHooker to receive a modified sentence, copy it into your own buffer and return that.
* Please allocate the buffer using malloc() and not new[] or something else: NextHooker uses free() to free it.
* Param miscInfo: pointer to start of singly linked list containing misc info about the sentence. * 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 value: pointer to sentence NextHooker takes for future processing and display.
* Return 'sentence' unless you created a new sentence/buffer as mentioned above. * Return 'sentence' unless you created a new sentence/buffer as mentioned above.
@ -44,10 +39,11 @@ bool ProcessSentence(std::wstring& sentence, const InfoForExtension* miscInfo);
extern "C" __declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sentenceArr, const InfoForExtension* miscInfo) extern "C" __declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sentenceArr, const InfoForExtension* miscInfo)
{ {
std::wstring sentence(sentenceArr); std::wstring sentence(sentenceArr);
if (ProcessSentence(sentence, miscInfo)) if (ProcessSentence(sentence, SentenceInfo{ miscInfo }))
{ {
// No need to worry about freeing this: NextHooker does it for you.
wchar_t* newSentence = (wchar_t*)malloc((sentence.size() + 1) * sizeof(wchar_t*)); wchar_t* newSentence = (wchar_t*)malloc((sentence.size() + 1) * sizeof(wchar_t*));
wcscpy(newSentence, sentence.c_str()); wcscpy_s(newSentence, sentence.size() + 1, sentence.c_str());
return newSentence; return newSentence;
} }
else return sentenceArr; else return sentenceArr;

View File

@ -1,8 +1,8 @@
#include "extensions.h" #include "extensions.h"
bool ProcessSentence(std::wstring& sentence, const InfoForExtension* miscInfo) bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{ {
if (GetProperty("hook address", miscInfo) == -1) return false; if (sentenceInfo["hook address"] == -1) return false;
sentence += L"\r\n"; sentence += L"\r\n";
return true; return true;
} }

View File

@ -30,7 +30,7 @@ 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); 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);
} }
bool ProcessSentence(std::wstring& sentence, const InfoForExtension* miscInfo) bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{ {
static HINTERNET internet = NULL; static HINTERNET internet = NULL;
if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 NextHooker", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0); if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 NextHooker", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0);
@ -38,7 +38,7 @@ bool ProcessSentence(std::wstring& sentence, const InfoForExtension* miscInfo)
std::wstring translation(L""); std::wstring translation(L"");
if (GetProperty("hook address", miscInfo) == -1) return false; if (sentenceInfo["hook address"] == -1) return false;
if (internet) if (internet)
{ {

View File

@ -51,12 +51,12 @@ bool RemoveRepeatedSentences(std::wstring& sentence, int handle)
return false; return false;
} }
bool ProcessSentence(std::wstring& sentence, const InfoForExtension* miscInfo) bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{ {
if (GetProperty("hook address", miscInfo) == -1) return false; if (sentenceInfo["hook address"] == -1) return false;
bool ret = false; bool ret = false;
ret |= RemoveRepeatedChars(sentence); ret |= RemoveRepeatedChars(sentence);
ret |= RemoveCyclicRepeats(sentence); ret |= RemoveCyclicRepeats(sentence);
ret |= RemoveRepeatedSentences(sentence, GetProperty("text handle", miscInfo)); ret |= RemoveRepeatedSentences(sentence, sentenceInfo["text handle"]);
return ret; return ret;
} }