Skip to content

Commit

Permalink
refactor extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Artikash committed Sep 22, 2018
1 parent 665c5b3 commit 22c4f10
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 32 deletions.
4 changes: 2 additions & 2 deletions extensions/copyclipboard.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#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));
memcpy(GlobalLock(hMem), sentence.c_str(), (sentence.size() + 2) * sizeof(wchar_t));
Expand Down
42 changes: 19 additions & 23 deletions extensions/extensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,29 @@

struct InfoForExtension
{
const char* propertyName;
int64_t propertyValue;
InfoForExtension* nextProperty;
const char* name;
int64_t value;
InfoForExtension* next;
};

// Traverses linked list to find info.
int54_t GetProperty(const char* propertyName, const InfoForExtension* miscInfo)
struct SentenceInfo
{
const InfoForExtension* miscInfoTraverser = miscInfo;
while (miscInfoTraverser != nullptr)
if (strcmp(propertyName, miscInfoTraverser->propertyName) == 0) return miscInfoTraverser->propertyValue;
else miscInfoTraverser = miscInfoTraverser->nextProperty;
const InfoForExtension* list;
// Traverse linked list to find info.
int64_t operator[](std::string propertyName)
{
for (auto i = list; i != nullptr; i = i->next) if (propertyName == i->name) return i->value;
return 0;
}
};

return 0;
}

/**
* Param sentence: sentence 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);
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo);

/**
* You shouldn't mess with this or even look at it unless you're certain you know what you're doing.
* 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.
* 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.
* Return value: pointer to sentence NextHooker takes for future processing and display.
* Return 'sentence' unless you created a new sentence/buffer as mentioned above.
Expand All @@ -44,11 +39,12 @@ bool ProcessSentence(std::wstring& sentence, const InfoForExtension* miscInfo);
extern "C" __declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sentenceArr, const InfoForExtension* miscInfo)
{
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*));
wcscpy(newSentence, sentence.c_str());
wcscpy_s(newSentence, sentence.size() + 1, sentence.c_str());
return newSentence;
}
else return sentenceArr;
}
}
4 changes: 2 additions & 2 deletions extensions/extranewlines.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#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";
return true;
}
4 changes: 2 additions & 2 deletions extensions/googletranslate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +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);
}

bool ProcessSentence(std::wstring& sentence, const InfoForExtension* miscInfo)
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{
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;

std::wstring translation(L"");

if (GetProperty("hook address", miscInfo) == -1) return false;
if (sentenceInfo["hook address"] == -1) return false;

if (internet)
{
Expand Down
6 changes: 3 additions & 3 deletions extensions/removerepeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ bool RemoveRepeatedSentences(std::wstring& sentence, int handle)
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;
ret |= RemoveRepeatedChars(sentence);
ret |= RemoveCyclicRepeats(sentence);
ret |= RemoveRepeatedSentences(sentence, GetProperty("text handle", miscInfo));
ret |= RemoveRepeatedSentences(sentence, sentenceInfo["text handle"]);
return ret;
}

0 comments on commit 22c4f10

Please sign in to comment.