From a8cebd1b86dd3e905d4edbc17ef75753e25fd1d1 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Fri, 24 Aug 2018 14:24:46 -0400 Subject: [PATCH] std::optional is beautiful --- CMakeLists.txt | 1 + GUI/mainwindow.cpp | 11 +++-------- GUI/misc.cpp | 6 +++--- GUI/misc.h | 3 ++- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b647da..86a2b40 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${MODULE_DIR}) project(NextHooker) add_compile_options( + /std:c++17 #/Zc:auto # config.pri /wd4819 # config.pri /MP diff --git a/GUI/mainwindow.cpp b/GUI/mainwindow.cpp index 63da01e..ebf1837 100644 --- a/GUI/mainwindow.cpp +++ b/GUI/mainwindow.cpp @@ -63,7 +63,7 @@ void MainWindow::AddProcess(unsigned int processId) { QStringList hooks = allProcesses.at(i).split(" , "); for (int j = 1; j < hooks.length(); ++j) - Host::InsertHook(processId, ParseCode(hooks.at(j))); + Host::InsertHook(processId, ParseCode(hooks.at(j)).value_or(HookParam())); return; } } @@ -200,13 +200,8 @@ void MainWindow::on_hookButton_clicked() bool ok; QString hookCode = QInputDialog::getText(this, "Add Hook", CodeInfoDump, QLineEdit::Normal, "", &ok); if (!ok) return; - HookParam toInsert = ParseCode(hookCode); - if (toInsert.type == 0 && toInsert.length_offset == 0) - { - Host::AddConsoleOutput(L"invalid code"); - return; - } - Host::InsertHook(GetSelectedProcessId(), ParseCode(hookCode)); + if (auto hp = ParseCode(hookCode)) Host::InsertHook(GetSelectedProcessId(), hp.value()); + else Host::AddConsoleOutput(L"invalid code"); } void MainWindow::on_unhookButton_clicked() diff --git a/GUI/misc.cpp b/GUI/misc.cpp index 4ae9358..8930ecb 100644 --- a/GUI/misc.cpp +++ b/GUI/misc.cpp @@ -38,7 +38,7 @@ DWORD Hash(QString module) return hash; } -HookParam ParseRCode(QString RCode) +std::optional ParseRCode(QString RCode) { HookParam hp = {}; hp.type |= DIRECT_READ; @@ -68,7 +68,7 @@ HookParam ParseRCode(QString RCode) return hp; } -HookParam ParseHCode(QString HCode) +std::optional ParseHCode(QString HCode) { HookParam hp = {}; switch (HCode.at(0).unicode()) @@ -145,7 +145,7 @@ HookParam ParseHCode(QString HCode) return hp; } -HookParam ParseCode(QString code) +std::optional ParseCode(QString code) { code = code.toUpper(); if (code.startsWith("/H")) return ParseHCode(code.remove(0, 2)); diff --git a/GUI/misc.h b/GUI/misc.h index 29afe99..5c540bb 100644 --- a/GUI/misc.h +++ b/GUI/misc.h @@ -3,11 +3,12 @@ #include "qtcommon.h" #include "types.h" +#include QString GetFullModuleName(DWORD processId, HMODULE module = NULL); QString GetModuleName(DWORD processId, HMODULE module = NULL); std::unordered_map GetAllProcesses(); -HookParam ParseCode(QString HCode); +std::optional ParseCode(QString HCode); QString GenerateCode(HookParam hp, DWORD processId); static QString CodeInfoDump =