From 86b2014a12ba46bfaf4e37b338935ee11d8f7db1 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Fri, 14 Dec 2018 23:14:30 -0500 Subject: [PATCH] fix crash when saving hooks. refactor other things to compile time --- GUI/extenwindow.cpp | 11 ++++---- GUI/extenwindow.h | 1 - GUI/mainwindow.cpp | 61 +++++++++++++++++++++------------------------ GUI/mainwindow.h | 3 --- GUI/setdialog.cpp | 7 ++---- include/types.h | 2 ++ 6 files changed, 38 insertions(+), 47 deletions(-) diff --git a/GUI/extenwindow.cpp b/GUI/extenwindow.cpp index d201045..0effa7a 100644 --- a/GUI/extenwindow.cpp +++ b/GUI/extenwindow.cpp @@ -81,8 +81,7 @@ ExtenWindow::ExtenWindow(QWidget* parent) : { ui->setupUi(this); - extenList = findChild("extenList"); - extenList->installEventFilter(this); + ui->extenList->installEventFilter(this); for (auto extenName : QString(QAutoFile(EXTEN_SAVE_FILE, QIODevice::ReadOnly)->readAll()).split(">")) Load(extenName); Sync(); @@ -95,12 +94,12 @@ ExtenWindow::~ExtenWindow() void ExtenWindow::Sync() { - extenList->clear(); + ui->extenList->clear(); QAutoFile extenSaveFile(EXTEN_SAVE_FILE, QIODevice::WriteOnly | QIODevice::Truncate); std::shared_lock sharedLock(extenMutex); for (auto extenName : extenNames) { - extenList->addItem(extenName); + ui->extenList->addItem(extenName); extenSaveFile->write((extenName + ">").toUtf8()); } } @@ -119,7 +118,7 @@ bool ExtenWindow::eventFilter(QObject* target, QEvent* event) if (event->type() == QEvent::ChildRemoved) { QStringList extenNames; - for (int i = 0; i < extenList->count(); ++i) extenNames.push_back(extenList->item(i)->text()); + for (int i = 0; i < ui->extenList->count(); ++i) extenNames.push_back(ui->extenList->item(i)->text()); Reorder(extenNames); Sync(); } @@ -143,6 +142,6 @@ void ExtenWindow::on_addButton_clicked() void ExtenWindow::on_rmvButton_clicked() { - if (auto extenName = extenList->currentItem()) Unload(extenName->text()); + if (auto extenName = ui->extenList->currentItem()) Unload(extenName->text()); Sync(); } diff --git a/GUI/extenwindow.h b/GUI/extenwindow.h index 4876c1e..d311d93 100644 --- a/GUI/extenwindow.h +++ b/GUI/extenwindow.h @@ -32,5 +32,4 @@ private: void dropEvent(QDropEvent* event); Ui::ExtenWindow* ui; - QListWidget* extenList; }; diff --git a/GUI/mainwindow.cpp b/GUI/mainwindow.cpp index 96d56b3..2ee38d5 100644 --- a/GUI/mainwindow.cpp +++ b/GUI/mainwindow.cpp @@ -18,9 +18,7 @@ MainWindow::MainWindow(QWidget *parent) : extenWindow(new ExtenWindow(this)) { ui->setupUi(this); - QFrame* processFrame = findChild("processFrame"); - QVBoxLayout* processLayout = findChild("processLayout"); - for (auto[text, slot] : std::initializer_list>{ + for (auto[text, slot] : Array>{ { ATTACH, &MainWindow::on_attachButton_clicked }, { DETACH, &MainWindow::on_detachButton_clicked }, { ADD_HOOK, &MainWindow::on_hookButton_clicked }, @@ -29,16 +27,12 @@ MainWindow::MainWindow(QWidget *parent) : { EXTENSIONS, &MainWindow::on_extenButton_clicked } }) { - QPushButton* button = new QPushButton(processFrame); + QPushButton* button = new QPushButton(ui->processFrame); connect(button, &QPushButton::clicked, this, slot); button->setText(text); - processLayout->addWidget(button); + ui->processLayout->addWidget(button); } - processLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding)); - - processCombo = findChild("processCombo"); - ttCombo = findChild("ttCombo"); - textOutput = findChild("textOutput"); + ui->processLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding)); if (settings.contains(WINDOW)) setGeometry(settings.value(WINDOW).toRect()); if (settings.contains(FLUSH_DELAY)) TextThread::flushDelay = settings.value(FLUSH_DELAY).toInt(); @@ -97,7 +91,7 @@ void MainWindow::ProcessConnected(DWORD processId) InvokeOnMainThread([&, processId] { QString process = S(Util::GetModuleFileName(processId).value()); - processCombo->addItem(QString::number(processId, 16).toUpper() + ": " + QFileInfo(process).fileName()); + ui->processCombo->addItem(QString::number(processId, 16).toUpper() + ": " + QFileInfo(process).fileName()); QStringList allProcesses = QString(QAutoFile(HOOK_SAVE_FILE, QIODevice::ReadOnly)->readAll()).split("\r", QString::SkipEmptyParts); // Can't use QFileInfo::absoluteFilePath since hook save file has '\\' as path separator @@ -110,13 +104,13 @@ void MainWindow::ProcessConnected(DWORD processId) void MainWindow::ProcessDisconnected(DWORD processId) { - InvokeOnMainThread([&, processId] { processCombo->removeItem(processCombo->findText(QString::number(processId, 16).toUpper() + ":", Qt::MatchStartsWith)); }); + InvokeOnMainThread([&, processId] { ui->processCombo->removeItem(ui->processCombo->findText(QString::number(processId, 16).toUpper() + ":", Qt::MatchStartsWith)); }); } void MainWindow::ThreadAdded(TextThread* thread) { QString ttString = TextThreadString(thread) + S(thread->name) + " (" + GenerateCode(thread->hp, thread->tp.processId) + ")"; - InvokeOnMainThread([&, ttString] { ttCombo->addItem(ttString); }); + InvokeOnMainThread([&, ttString] { ui->ttCombo->addItem(ttString); }); } void MainWindow::ThreadRemoved(TextThread* thread) @@ -124,13 +118,13 @@ void MainWindow::ThreadRemoved(TextThread* thread) QString ttString = TextThreadString(thread); InvokeOnMainThread([&, ttString] { - int threadIndex = ttCombo->findText(ttString, Qt::MatchStartsWith); - if (threadIndex == ttCombo->currentIndex()) + int threadIndex = ui->ttCombo->findText(ttString, Qt::MatchStartsWith); + if (threadIndex == ui->ttCombo->currentIndex()) { - ttCombo->setCurrentIndex(0); + ui->ttCombo->setCurrentIndex(0); on_ttCombo_activated(0); } - ttCombo->removeItem(threadIndex); + ui->ttCombo->removeItem(threadIndex); }); } @@ -142,11 +136,11 @@ bool MainWindow::SentenceReceived(TextThread* thread, std::wstring& sentence) QString ttString = TextThreadString(thread); InvokeOnMainThread([&, ttString, sentence] { - if (ttCombo->currentText().startsWith(ttString)) + if (ui->ttCombo->currentText().startsWith(ttString)) { - textOutput->moveCursor(QTextCursor::End); - textOutput->insertPlainText(S(sentence)); - textOutput->moveCursor(QTextCursor::End); + ui->textOutput->moveCursor(QTextCursor::End); + ui->textOutput->insertPlainText(S(sentence)); + ui->textOutput->moveCursor(QTextCursor::End); } }); return true; @@ -174,14 +168,14 @@ ThreadParam MainWindow::ParseTextThreadString(QString ttString) DWORD MainWindow::GetSelectedProcessId() { - return processCombo->currentText().split(":")[0].toULong(nullptr, 16); + return ui->processCombo->currentText().split(":")[0].toULong(nullptr, 16); } std::unordered_map MainWindow::GetMiscInfo(TextThread* thread) { return { - { "current select", ttCombo->currentText().startsWith(TextThreadString(thread)) }, + { "current select", ui->ttCombo->currentText().startsWith(TextThreadString(thread)) }, { "text number", thread->handle }, { "process id", thread->tp.processId }, { "hook address", thread->tp.addr }, @@ -223,15 +217,18 @@ void MainWindow::on_hookButton_clicked() void MainWindow::on_saveButton_clicked() { - QHash hookCodes; - for (int i = 0; i < ttCombo->count(); ++i) + if (auto processName = Util::GetModuleFileName(GetSelectedProcessId())) { - 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); + QHash hookCodes; + for (int i = 0; i < ui->ttCombo->count(); ++i) + { + ThreadParam tp = ParseTextThreadString(ui->ttCombo->itemText(i)); + if (tp.processId == GetSelectedProcessId() && !(Host::GetHookParam(tp).type & HOOK_ENGINE)) hookCodes[tp.addr] = GenerateCode(Host::GetHookParam(tp), tp.processId); + } + QString hookList = S(processName.value()); + for (auto hookCode : hookCodes) hookList += " , " + hookCode; + QAutoFile(HOOK_SAVE_FILE, QIODevice::Append)->write((hookList + "\r\n").toUtf8()); } - QString hookList = S(Util::GetModuleFileName(GetSelectedProcessId()).value()); - for (auto hookCode : hookCodes) hookList += " , " + hookCode; - QAutoFile(HOOK_SAVE_FILE, QIODevice::Append)->write((hookList + "\r\n").toUtf8()); } void MainWindow::on_setButton_clicked() @@ -247,6 +244,6 @@ void MainWindow::on_extenButton_clicked() void MainWindow::on_ttCombo_activated(int index) { - textOutput->setPlainText(S(Host::GetThread(ParseTextThreadString(ttCombo->itemText(index)))->GetStorage())); - textOutput->moveCursor(QTextCursor::End); + ui->textOutput->setPlainText(S(Host::GetThread(ParseTextThreadString(ui->ttCombo->itemText(index)))->GetStorage())); + ui->textOutput->moveCursor(QTextCursor::End); } diff --git a/GUI/mainwindow.h b/GUI/mainwindow.h index da9a7bb..d6d22fb 100644 --- a/GUI/mainwindow.h +++ b/GUI/mainwindow.h @@ -44,8 +44,5 @@ private: Ui::MainWindow* ui; QSettings settings = QSettings(CONFIG_FILE, QSettings::IniFormat); - QComboBox* processCombo; - QComboBox* ttCombo; - QPlainTextEdit* textOutput; QWidget* extenWindow; }; diff --git a/GUI/setdialog.cpp b/GUI/setdialog.cpp index 66cdaa6..9700ddf 100644 --- a/GUI/setdialog.cpp +++ b/GUI/setdialog.cpp @@ -10,10 +10,7 @@ SetDialog::SetDialog(QWidget* parent) : ui(new Ui::SetDialog) { ui->setupUi(this); - - QFormLayout* layout = findChild("layout"); - - for (auto[spinBox, value, label] : std::initializer_list>{ + for (auto[spinBox, value, label] : Array>{ { flushDelay, TextThread::flushDelay, FLUSH_DELAY }, { maxBufferSize, TextThread::maxBufferSize, MAX_BUFFER_SIZE }, { defaultCodepage, TextThread::defaultCodepage, DEFAULT_CODEPAGE } @@ -22,7 +19,7 @@ SetDialog::SetDialog(QWidget* parent) : spinBox = new QSpinBox(this); spinBox->setMaximum(INT_MAX); spinBox->setValue(value); - layout->insertRow(0, label, spinBox); + ui->layout->insertRow(0, label, spinBox); } } diff --git a/include/types.h b/include/types.h index 828d4d8..4f3a4a2 100644 --- a/include/types.h +++ b/include/types.h @@ -3,6 +3,8 @@ #include "common.h" #include "const.h" +template using Array = T[]; + template typename P = std::unique_ptr> class ThreadSafePtr {