std::optional is beautiful

This commit is contained in:
Akash Mozumdar 2018-08-24 14:24:46 -04:00
parent 61fb3248fe
commit a8cebd1b86
4 changed files with 9 additions and 12 deletions

View File

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

View File

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

View File

@ -38,7 +38,7 @@ DWORD Hash(QString module)
return hash;
}
HookParam ParseRCode(QString RCode)
std::optional<HookParam> 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<HookParam> 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<HookParam> ParseCode(QString code)
{
code = code.toUpper();
if (code.startsWith("/H")) return ParseHCode(code.remove(0, 2));

View File

@ -3,11 +3,12 @@
#include "qtcommon.h"
#include "types.h"
#include <optional>
QString GetFullModuleName(DWORD processId, HMODULE module = NULL);
QString GetModuleName(DWORD processId, HMODULE module = NULL);
std::unordered_map<std::wstring, DWORD> GetAllProcesses();
HookParam ParseCode(QString HCode);
std::optional<HookParam> ParseCode(QString HCode);
QString GenerateCode(HookParam hp, DWORD processId);
static QString CodeInfoDump =