From a3ac850bf4273e5a26060223d49887c13f5b4ba4 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Thu, 13 Dec 2018 08:37:37 -0500 Subject: [PATCH] refactor. remove unneeded abstraction, alias verbose conversions --- GUI/extenwindow.cpp | 8 ++-- GUI/main.cpp | 3 +- GUI/mainwindow.cpp | 37 +++++++++++++++---- GUI/misc.cpp | 37 +++++-------------- GUI/misc.h | 4 +- .../googletranslate/googletranslate.cpp | 7 ++-- include/text.h | 2 +- 7 files changed, 51 insertions(+), 47 deletions(-) diff --git a/GUI/extenwindow.cpp b/GUI/extenwindow.cpp index db645a9..61bcf94 100644 --- a/GUI/extenwindow.cpp +++ b/GUI/extenwindow.cpp @@ -27,8 +27,8 @@ namespace { if (extenName == ITH_DLL) return; // Extension is dll and exports "OnNewSentence" - HMODULE module = GetModuleHandleW(extenName.toStdWString().c_str()); - if (!module) module = LoadLibraryW(extenName.toStdWString().c_str()); + HMODULE module = GetModuleHandleW(S(extenName).c_str()); + if (!module) module = LoadLibraryW(S(extenName).c_str()); if (!module) return; FARPROC callback = GetProcAddress(module, "OnNewSentence"); if (!callback) return; @@ -41,7 +41,7 @@ namespace { LOCK(extenMutex); extenNames.erase(std::remove(extenNames.begin(), extenNames.end(), extenName), extenNames.end()); - FreeLibrary(GetModuleHandleW(extenName.toStdWString().c_str())); + FreeLibrary(GetModuleHandleW(S(extenName).c_str())); } void Reorder(QStringList extenNames) @@ -69,7 +69,7 @@ bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map #include @@ -52,7 +53,7 @@ int main(int argc, char *argv[]) AddVectoredExceptionHandler(FALSE, ExceptionLogger); SetUnhandledExceptionFilter([](auto) -> LONG { Terminate(); }); - QDir::setCurrent(QFileInfo(QString::fromStdWString(Util::GetModuleFileName().value())).absolutePath()); + QDir::setCurrent(QFileInfo(S(Util::GetModuleFileName().value())).absolutePath()); QApplication a(argc, argv); MainWindow w; diff --git a/GUI/mainwindow.cpp b/GUI/mainwindow.cpp index 279b60e..96eba15 100644 --- a/GUI/mainwindow.cpp +++ b/GUI/mainwindow.cpp @@ -5,6 +5,8 @@ #include "setdialog.h" #include "misc.h" #include "host/util.h" +#include +#include #include MainWindow::MainWindow(QWidget *parent) : @@ -31,7 +33,23 @@ MainWindow::MainWindow(QWidget *parent) : [&](TextThread* thread, std::wstring& output) { return SentenceReceived(thread, output); } ); Host::AddConsoleOutput(ABOUT); - std::thread([] { if (UpdateAvailable(CURRENT_VERSION)) Host::AddConsoleOutput(UPDATE_AVAILABLE); }).detach(); + + std::thread([] + { + // Queries GitHub releases API https://developer.github.com/v3/repos/releases/ and checks the last release tag to check if it's the same + struct InternetHandleCloser { void operator()(void* h) { WinHttpCloseHandle(h); } }; + if (AutoHandle internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0)) + if (AutoHandle connection = WinHttpConnect(internet, L"api.github.com", INTERNET_DEFAULT_HTTPS_PORT, 0)) + if (AutoHandle request = WinHttpOpenRequest(connection, L"GET", L"/repos/Artikash/Textractor/releases", NULL, NULL, NULL, WINHTTP_FLAG_SECURE)) + if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL)) + { + DWORD bytesRead; + char buffer[1000] = {}; + WinHttpReceiveResponse(request, NULL); + WinHttpReadData(request, buffer, 1000, &bytesRead); + if (abs(strstr(buffer, "/tag/") - strstr(buffer, CURRENT_VERSION)) > 10) Host::AddConsoleOutput(UPDATE_AVAILABLE); + } + }).detach(); } MainWindow::~MainWindow() @@ -58,7 +76,7 @@ void MainWindow::ProcessConnected(DWORD processId) if (processId == 0) return; InvokeOnMainThread([&, processId] { - QString process = QString::fromStdWString(Util::GetModuleFileName(processId).value()); + QString process = S(Util::GetModuleFileName(processId).value()); processCombo->addItem(QString::number(processId, 16).toUpper() + ": " + QFileInfo(process).fileName()); QStringList allProcesses = QString(QAutoFile(HOOK_SAVE_FILE, QIODevice::ReadOnly)->readAll()).split("\r", QString::SkipEmptyParts); @@ -77,7 +95,7 @@ void MainWindow::ProcessDisconnected(DWORD processId) void MainWindow::ThreadAdded(TextThread* thread) { - QString ttString = TextThreadString(thread) + QString::fromStdWString(thread->name) + " (" + GenerateCode(thread->hp, thread->tp.processId) + ")"; + QString ttString = TextThreadString(thread) + S(thread->name) + " (" + GenerateCode(thread->hp, thread->tp.processId) + ")"; InvokeOnMainThread([&, ttString] { ttCombo->addItem(ttString); }); } @@ -107,7 +125,7 @@ bool MainWindow::SentenceReceived(TextThread* thread, std::wstring& sentence) if (ttCombo->currentText().startsWith(ttString)) { textOutput->moveCursor(QTextCursor::End); - textOutput->insertPlainText(QString::fromStdWString(sentence)); + textOutput->insertPlainText(S(sentence)); textOutput->moveCursor(QTextCursor::End); } }); @@ -154,7 +172,12 @@ std::unordered_map MainWindow::GetMiscInfo(TextThread* thr void MainWindow::on_attachButton_clicked() { - auto allProcesses = GetAllProcesses(); + QMultiHash allProcesses; + DWORD allProcessIds[5000] = {}, spaceUsed = 0; + EnumProcesses(allProcessIds, sizeof(allProcessIds), &spaceUsed); + for (int i = 0; i < spaceUsed / sizeof(DWORD); ++i) + if (auto processName = Util::GetModuleFileName(allProcessIds[i])) allProcesses.insert(QFileInfo(S(processName.value())).fileName(), allProcessIds[i]); + QStringList processList(allProcesses.uniqueKeys()); processList.sort(Qt::CaseInsensitive); bool ok; @@ -186,7 +209,7 @@ void MainWindow::on_saveButton_clicked() ThreadParam tp = ParseTextThreadString(ttCombo->itemText(i)); if (tp.processId == GetSelectedProcessId() && !(Host::GetHookParam(tp).type & HOOK_ENGINE)) hookCodes[tp.addr] = GenerateCode(Host::GetHookParam(tp), tp.processId); } - QString hookList = QString::fromStdWString(Util::GetModuleFileName(GetSelectedProcessId()).value()); + QString hookList = S(Util::GetModuleFileName(GetSelectedProcessId()).value()); for (auto hookCode : hookCodes) hookList += " , " + hookCode; QAutoFile(HOOK_SAVE_FILE, QIODevice::Append)->write((hookList + "\r\n").toUtf8()); } @@ -204,6 +227,6 @@ void MainWindow::on_extenButton_clicked() void MainWindow::on_ttCombo_activated(int index) { - textOutput->setPlainText(QString::fromStdWString(Host::GetThread(ParseTextThreadString(ttCombo->itemText(index)))->GetStorage())); + textOutput->setPlainText(S(Host::GetThread(ParseTextThreadString(ttCombo->itemText(index)))->GetStorage())); textOutput->moveCursor(QTextCursor::End); } diff --git a/GUI/misc.cpp b/GUI/misc.cpp index b75e541..058b2f0 100644 --- a/GUI/misc.cpp +++ b/GUI/misc.cpp @@ -2,17 +2,16 @@ #include "const.h" #include "host/util.h" #include -#include #include -QMultiHash GetAllProcesses() +std::wstring S(const QString& S) { - DWORD allProcessIds[5000] = {}, spaceUsed = 0; - EnumProcesses(allProcessIds, sizeof(allProcessIds), &spaceUsed); - QMultiHash ret; - for (int i = 0; i < spaceUsed / sizeof(DWORD); ++i) - if (auto processName = Util::GetModuleFileName(allProcessIds[i])) ret.insert(QFileInfo(QString::fromStdWString(processName.value())).fileName(), allProcessIds[i]); - return ret; + return S.toStdWString(); +} + +QString S(const std::wstring& S) +{ + return QString::fromStdWString(S); } namespace @@ -150,7 +149,7 @@ namespace if (addressPieces.size() > 1) { hp.type |= MODULE_OFFSET; - wcscpy_s(hp.module, addressPieces.at(1).toStdWString().c_str()); + wcscpy_s(hp.module, S(addressPieces.at(1)).c_str()); } if (addressPieces.size() > 2) { @@ -230,7 +229,7 @@ namespace } codeBuilder << "@" << hp.address; - if (hp.type & MODULE_OFFSET) codeBuilder << ":" << QString::fromWCharArray(hp.module); + if (hp.type & MODULE_OFFSET) codeBuilder << ":" << S(hp.module); if (hp.type & FUNCTION_OFFSET) codeBuilder << ":" << hp.function; return HCode; @@ -249,21 +248,3 @@ QString GenerateCode(HookParam hp, DWORD processId) if (hp.type & DIRECT_READ) return GenerateRCode(hp); else return GenerateHCode(hp, processId); } - -bool UpdateAvailable(std::string currentVersion) -{ - // Queries GitHub releases API https://developer.github.com/v3/repos/releases/ and checks the last release tag to check if it's the same - struct InternetHandleCloser { void operator()(void* h) { WinHttpCloseHandle(h); } }; - if (AutoHandle internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0)) - if (AutoHandle connection = WinHttpConnect(internet, L"api.github.com", INTERNET_DEFAULT_HTTPS_PORT, 0)) - if (AutoHandle request = WinHttpOpenRequest(connection, L"GET", L"/repos/Artikash/Textractor/releases", NULL, NULL, NULL, WINHTTP_FLAG_SECURE)) - if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL)) - { - DWORD bytesRead; - char buffer[1000] = {}; - WinHttpReceiveResponse(request, NULL); - WinHttpReadData(request, buffer, 1000, &bytesRead); - if (abs(strstr(buffer, "/tag/") - strstr(buffer, currentVersion.c_str())) > 10) return true; - } - return false; -} diff --git a/GUI/misc.h b/GUI/misc.h index 1c6d9cb..c0cf57f 100644 --- a/GUI/misc.h +++ b/GUI/misc.h @@ -12,7 +12,7 @@ private: QFile f; }; -QMultiHash GetAllProcesses(); +std::wstring S(const QString& S); +QString S(const std::wstring& S); std::optional ParseCode(QString HCode); QString GenerateCode(HookParam hp, DWORD processId); -bool UpdateAvailable(std::string currentVersion); \ No newline at end of file diff --git a/extensions/googletranslate/googletranslate.cpp b/extensions/googletranslate/googletranslate.cpp index 020e055..bb80501 100644 --- a/extensions/googletranslate/googletranslate.cpp +++ b/extensions/googletranslate/googletranslate.cpp @@ -133,10 +133,6 @@ std::wstring GetTranslationUri(std::wstring text, unsigned TKK) bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) { - static HINTERNET internet = NULL; - if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0); - static unsigned TKK = 0; - if (sentenceInfo["hook address"] == -1) return false; { @@ -152,6 +148,9 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) } } + static HINTERNET internet = NULL; + if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0); + static unsigned TKK = 0; std::wstring translation; if (internet) { diff --git a/include/text.h b/include/text.h index 4482554..9f85fae 100644 --- a/include/text.h +++ b/include/text.h @@ -1,6 +1,6 @@ #pragma once -#define CURRENT_VERSION "3.5.1" +#define CURRENT_VERSION "3.6.0" constexpr auto SELECT_PROCESS = u8"Select Process"; constexpr auto ATTACH_INFO = u8"If you don't see the process you want to attach, try running with admin rights\r\n" "You can also type in the process id";