From 8711332f7739ae97d21f95264b9d9028ff3b6f39 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Sun, 19 Aug 2018 00:13:19 -0400 Subject: [PATCH] update build stuff and add the starter extensions --- CMakeLists.txt | 3 ++- CMakeSettings.json | 4 +-- extensions/CMakeLists.txt | 5 ++++ extensions/copyclipboard.cpp | 28 +++++++++++++++++++++ extensions/extensions.h | 21 ++++++++++++++++ extensions/extranewlines.cpp | 21 ++++++++++++++++ extensions/googletranslate.cpp | 17 +++++++++++++ extensions/removerepeat.cpp | 46 ++++++++++++++++++++++++++++++++++ host/CMakeLists.txt | 2 -- vnrhook/CMakeLists.txt | 12 ++++----- vnrhook/src/main.cc | 2 +- 11 files changed, 148 insertions(+), 13 deletions(-) create mode 100644 extensions/CMakeLists.txt create mode 100644 extensions/copyclipboard.cpp create mode 100644 extensions/extensions.h create mode 100644 extensions/extranewlines.cpp create mode 100644 extensions/googletranslate.cpp create mode 100644 extensions/removerepeat.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 838c872..d5aad7a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,5 +29,6 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Build) set(CMAKE_CONFIGURATION_TYPES Debug Release) add_subdirectory(host) -add_subdirectory(vnrhook) add_subdirectory(GUI) +add_subdirectory(vnrhook) +add_subdirectory(extensions) diff --git a/CMakeSettings.json b/CMakeSettings.json index c275a98..b4ac618 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -30,7 +30,7 @@ "inheritEnvironments": [ "msvc_x64" ], "buildRoot": "${workspaceRoot}\\Builds\\${name}", "installRoot": "${workspaceRoot}\\CMakeBuilds\\${workspaceHash}\\install\\${name}", - "cmakeCommandArgs": "-Dx64=1", + "cmakeCommandArgs": "", "buildCommandArgs": "-v", "ctestCommandArgs": "" }, @@ -41,7 +41,7 @@ "inheritEnvironments": [ "msvc_x64" ], "buildRoot": "${workspaceRoot}\\Builds\\${name}", "installRoot": "${workspaceRoot}\\CMakeBuilds\\${workspaceHash}\\install\\${name}", - "cmakeCommandArgs": "-Dx64=1", + "cmakeCommandArgs": "", "buildCommandArgs": "-v", "ctestCommandArgs": "" } diff --git a/extensions/CMakeLists.txt b/extensions/CMakeLists.txt new file mode 100644 index 0000000..267d99d --- /dev/null +++ b/extensions/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_policy(SET CMP0037 OLD) +add_library(1_Remove\ Repetition_nexthooker_extension SHARED removerepeat.cpp) +add_library(2_Copy\ to\ Clipboard_nexthooker_extension SHARED copyclipboard.cpp) +#add_library("3_Google\ Translate_nexthooker_extension" SHARED googletranslate.cpp) +add_library(4_Extra\ Newlines_nexthooker_extension SHARED extranewlines.cpp) \ No newline at end of file diff --git a/extensions/copyclipboard.cpp b/extensions/copyclipboard.cpp new file mode 100644 index 0000000..44bee13 --- /dev/null +++ b/extensions/copyclipboard.cpp @@ -0,0 +1,28 @@ +#include "extensions.h" + +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) + { + if (GetProperty("current select", miscInfo) && GetProperty("text number", miscInfo) > 0) + { + HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (wcslen(sentence) + 1) * sizeof(wchar_t)); + memcpy(GlobalLock(hMem), sentence, (wcslen(sentence) + 1) * sizeof(wchar_t)); + GlobalUnlock(hMem); + OpenClipboard(0); + EmptyClipboard(); + SetClipboardData(CF_UNICODETEXT, hMem); + CloseClipboard(); + } + return sentence; + } +} \ No newline at end of file diff --git a/extensions/extensions.h b/extensions/extensions.h new file mode 100644 index 0000000..13194cd --- /dev/null +++ b/extensions/extensions.h @@ -0,0 +1,21 @@ +#define WIN32_LEAN_AND_MEAN +#include +#include + +struct InfoForExtension +{ + const char* propertyName; + int propertyValue; + InfoForExtension* nextProperty; +}; + +// Traverses linked list to find info. +int GetProperty(const char* propertyName, const InfoForExtension* miscInfo) +{ + const InfoForExtension* miscInfoTraverser = miscInfo; + while (miscInfoTraverser != nullptr) + if (strcmp(propertyName, miscInfoTraverser->propertyName) == 0) return miscInfoTraverser->propertyValue; + else miscInfoTraverser = miscInfoTraverser->nextProperty; + + return 0; +} \ No newline at end of file diff --git a/extensions/extranewlines.cpp b/extensions/extranewlines.cpp new file mode 100644 index 0000000..1be85b0 --- /dev/null +++ b/extensions/extranewlines.cpp @@ -0,0 +1,21 @@ +#include "extensions.h" + +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) + { + if (GetProperty("text number", miscInfo) == 0) 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; + } +} \ No newline at end of file diff --git a/extensions/googletranslate.cpp b/extensions/googletranslate.cpp new file mode 100644 index 0000000..8dd04d4 --- /dev/null +++ b/extensions/googletranslate.cpp @@ -0,0 +1,17 @@ +#include "extensions.h" + +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) + { + } +} \ No newline at end of file diff --git a/extensions/removerepeat.cpp b/extensions/removerepeat.cpp new file mode 100644 index 0000000..3b3b7ed --- /dev/null +++ b/extensions/removerepeat.cpp @@ -0,0 +1,46 @@ +#include "extensions.h" +#include +#include + +std::wstring remove_side_spaces(const std::wstring& str) +{ + 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) + if (i == prevChar) repeatNumber++; + else break; + + 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; + 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; + } +} \ No newline at end of file diff --git a/host/CMakeLists.txt b/host/CMakeLists.txt index 8748263..378a45e 100644 --- a/host/CMakeLists.txt +++ b/host/CMakeLists.txt @@ -1,5 +1,3 @@ -project(host) - set(vnrhost_src host.h pipe.h diff --git a/vnrhook/CMakeLists.txt b/vnrhook/CMakeLists.txt index 0a1bda1..f2e3deb 100644 --- a/vnrhook/CMakeLists.txt +++ b/vnrhook/CMakeLists.txt @@ -1,10 +1,8 @@ -project(vnrhook) +include_directories(.) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}) - -if (${x64}) +if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8") set(vnrhook_src - include/const.h + include/const.h include/defs.h include/types.h src/main.cc @@ -13,7 +11,7 @@ set(vnrhook_src src/util/ithsys/ithsys.cc src/hijack/texthook.cc ) -else(${x64}) +else() set(vnrhook_src include/const.h include/defs.h @@ -45,7 +43,7 @@ set(vnrhook_src src/util/mono/monoobject.h src/util/mono/monotype.h ) -endif(${x64}) +endif() include_directories(src/util) diff --git a/vnrhook/src/main.cc b/vnrhook/src/main.cc index f189cc4..57a7290 100644 --- a/vnrhook/src/main.cc +++ b/vnrhook/src/main.cc @@ -87,8 +87,8 @@ BOOL WINAPI DllMain(HINSTANCE hModule, DWORD fdwReason, LPVOID unused) VirtualQuery((void*)::processStopAddress, &info, sizeof(info)); ::processStopAddress = (DWORD)info.BaseAddress + info.RegionSize; } while (info.Protect > PAGE_NOACCESS); -#endif processStopAddress -= info.RegionSize; +#endif { wchar_t hm_mutex[0x100];