refactor. remove unneeded abstraction, alias verbose conversions

This commit is contained in:
Akash Mozumdar 2018-12-13 08:37:37 -05:00
parent c92a67dbe3
commit a3ac850bf4
7 changed files with 51 additions and 47 deletions

View File

@ -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<std
if (nextBuffer != sentenceBuffer) HeapFree(GetProcessHeap(), 0, sentenceBuffer);
sentenceBuffer = nextBuffer;
}
sentence = std::wstring(sentenceBuffer);
sentence = sentenceBuffer;
HeapFree(GetProcessHeap(), 0, sentenceBuffer);
return success;

View File

@ -1,4 +1,5 @@
#include "mainwindow.h"
#include "misc.h"
#include "host/util.h"
#include <sstream>
#include <QApplication>
@ -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;

View File

@ -5,6 +5,8 @@
#include "setdialog.h"
#include "misc.h"
#include "host/util.h"
#include <Psapi.h>
#include <winhttp.h>
#include <QInputDialog>
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<InternetHandleCloser> internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0))
if (AutoHandle<InternetHandleCloser> connection = WinHttpConnect(internet, L"api.github.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
if (AutoHandle<InternetHandleCloser> 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<std::string, int64_t> MainWindow::GetMiscInfo(TextThread* thr
void MainWindow::on_attachButton_clicked()
{
auto allProcesses = GetAllProcesses();
QMultiHash<QString, DWORD> 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);
}

View File

@ -2,17 +2,16 @@
#include "const.h"
#include "host/util.h"
#include <Psapi.h>
#include <Winhttp.h>
#include <QTextStream>
QMultiHash<QString, DWORD> GetAllProcesses()
std::wstring S(const QString& S)
{
DWORD allProcessIds[5000] = {}, spaceUsed = 0;
EnumProcesses(allProcessIds, sizeof(allProcessIds), &spaceUsed);
QMultiHash<QString, DWORD> 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<MAX_MODULE_SIZE>(hp.module, addressPieces.at(1).toStdWString().c_str());
wcscpy_s<MAX_MODULE_SIZE>(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<InternetHandleCloser> internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0))
if (AutoHandle<InternetHandleCloser> connection = WinHttpConnect(internet, L"api.github.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
if (AutoHandle<InternetHandleCloser> 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;
}

View File

@ -12,7 +12,7 @@ private:
QFile f;
};
QMultiHash<QString, DWORD> GetAllProcesses();
std::wstring S(const QString& S);
QString S(const std::wstring& S);
std::optional<HookParam> ParseCode(QString HCode);
QString GenerateCode(HookParam hp, DWORD processId);
bool UpdateAvailable(std::string currentVersion);

View File

@ -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)
{

View File

@ -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";