From 3b9ca65e39c9e444a90dd27e271e787d51c5f523 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Wed, 10 Oct 2018 08:16:14 -0400 Subject: [PATCH] refactor --- GUI/extensions.cpp | 8 ++------ GUI/host/host.cc | 2 +- GUI/mainwindow.cpp | 37 ++++++++++++++++--------------------- 3 files changed, 19 insertions(+), 28 deletions(-) diff --git a/GUI/extensions.cpp b/GUI/extensions.cpp index cc836d5..01e6586 100644 --- a/GUI/extensions.cpp +++ b/GUI/extensions.cpp @@ -7,15 +7,12 @@ std::optional LoadExtension(QString file) // Extension file format: {NUMBER}_{NAME}.dll and exports "OnNewSentence" QRegularExpressionMatch parsedFile = QRegularExpression("^(\\d+)_(.+).dll$").match(file); if (!parsedFile.hasMatch()) return {}; - HMODULE module = GetModuleHandleW(file.toStdWString().c_str()); if (!module) module = LoadLibraryW(file.toStdWString().c_str()); if (!module) return {}; - - auto callback = (wchar_t*(*)(const wchar_t*, const InfoForExtension*))GetProcAddress(module, "OnNewSentence"); + FARPROC callback = GetProcAddress(module, "OnNewSentence"); if (!callback) return {}; - - return Extension{ parsedFile.captured(1).toInt(), parsedFile.captured(2), callback }; + return Extension{ parsedFile.captured(1).toInt(), parsedFile.captured(2), (wchar_t*(*)(const wchar_t*, const InfoForExtension*))callback }; } std::shared_mutex extenMutex; @@ -27,7 +24,6 @@ std::set LoadExtensions() QStringList files = QDir().entryList(); for (auto file : files) if (auto extension = LoadExtension(file)) newExtensions.insert(extension.value()); - std::unique_lock extenLock(extenMutex); return extensions = newExtensions; } diff --git a/GUI/host/host.cc b/GUI/host/host.cc index 7d566e5..a497ca3 100644 --- a/GUI/host/host.cc +++ b/GUI/host/host.cc @@ -52,7 +52,7 @@ namespace //delete i.second; // Artikash 7/24/2018: FIXME: Qt GUI updates on another thread, so I can't delete this yet. removedThreads.push_back(i.first); } - for (auto i : removedThreads) textThreadsByParams.erase(i); + for (auto thread : removedThreads) textThreadsByParams.erase(thread); } void RegisterProcess(DWORD pid, HANDLE hostPipe) diff --git a/GUI/mainwindow.cpp b/GUI/mainwindow.cpp index e3275c7..c493544 100644 --- a/GUI/mainwindow.cpp +++ b/GUI/mainwindow.cpp @@ -54,14 +54,12 @@ void MainWindow::AddProcess(unsigned processId) QFile file("SavedHooks.txt"); if (!file.open(QIODevice::ReadOnly)) return; QString processName = GetFullModuleName(processId); - QString allData = file.readAll(); - QStringList allProcesses = allData.split("\r", QString::SkipEmptyParts); - for (int i = allProcesses.size() - 1; i >= 0; --i) - if (allProcesses[i].contains(processName)) + QStringList allProcesses = QString(file.readAll()).split("\r", QString::SkipEmptyParts); + for (auto hooks = allProcesses.rbegin(); hooks != allProcesses.rend(); ++hooks) + if (hooks->contains(processName)) { - QStringList hooks = allProcesses[i].split(" , "); - for (int j = 1; j < hooks.size(); ++j) - Host::InsertHook(processId, ParseCode(hooks[j]).value_or(HookParam())); + for (auto hook : hooks->split(" , ")) + if (auto hp = ParseCode(hook)) Host::InsertHook(processId, hp.value()); return; } } @@ -185,7 +183,7 @@ void MainWindow::on_attachButton_clicked() bool injected = false; if (!ok) return; if (process.toInt(nullptr, 0)) injected |= Host::InjectProcess(process.toInt(nullptr, 0)); - else for (auto i : allProcesses.values(process)) injected |= Host::InjectProcess(i); + else for (auto processId : allProcesses.values(process)) injected |= Host::InjectProcess(processId); if (!injected) Host::AddConsoleOutput(L"failed to inject"); } @@ -206,17 +204,14 @@ void MainWindow::on_hookButton_clicked() void MainWindow::on_unhookButton_clicked() { QVector hooks = GetAllHooks(GetSelectedProcessId()); - if (hooks.size() == 0) - { - Host::AddConsoleOutput(L"no hooks detected"); - return; - } + if (hooks.size() == 0) return Host::AddConsoleOutput(L"no hooks detected"); QStringList hookList; - for (auto i : hooks) hookList.push_back( - QString::fromStdWString(Host::GetHookName(GetSelectedProcessId(), i.address)) + - ": " + - GenerateCode(i, GetSelectedProcessId()) - ); + for (auto hook : hooks) + hookList.push_back( + QString::fromStdWString(Host::GetHookName(GetSelectedProcessId(), hook.address)) + + ": " + + GenerateCode(hook, GetSelectedProcessId()) + ); bool ok; QString hook = QInputDialog::getItem(this, "Unhook", "Which hook to remove?", hookList, 0, false, &ok); if (ok) Host::RemoveHook(GetSelectedProcessId(), hooks.at(hookList.indexOf(hook)).address); @@ -226,9 +221,9 @@ void MainWindow::on_saveButton_clicked() { QVector hooks = GetAllHooks(GetSelectedProcessId()); QString hookList = GetFullModuleName(GetSelectedProcessId()); - for (auto i : hooks) - if (!(i.type & HOOK_ENGINE)) - hookList += " , " + GenerateCode(i, GetSelectedProcessId()); + for (auto hook : hooks) + if (!(hook.type & HOOK_ENGINE)) + hookList += " , " + GenerateCode(hook, GetSelectedProcessId()); QFile file("SavedHooks.txt"); if (!file.open(QIODevice::Append | QIODevice::Text)) return; file.write((hookList + "\r\n").toUtf8());