Textractor_test/GUI/mainwindow.cpp

523 lines
20 KiB
C++
Raw Normal View History

2019-04-23 01:35:40 +08:00
#include "mainwindow.h"
2018-07-22 06:40:16 +08:00
#include "ui_mainwindow.h"
2018-12-19 01:14:54 +08:00
#include "defs.h"
#include "host/util.h"
2019-02-19 11:39:04 +08:00
#include <shellapi.h>
#include <QDialogButtonBox>
#include <QFileDialog>
2018-07-24 03:25:02 +08:00
2019-02-28 00:33:17 +08:00
extern const char* ATTACH;
extern const char* LAUNCH;
extern const char* DETACH;
extern const char* FORGET;
2019-02-28 00:33:17 +08:00
extern const char* ADD_HOOK;
2019-06-10 13:49:11 +08:00
extern const char* REMOVE_HOOKS;
2019-02-28 00:33:17 +08:00
extern const char* SAVE_HOOKS;
extern const char* SEARCH_FOR_HOOKS;
2019-02-28 00:33:17 +08:00
extern const char* SETTINGS;
extern const char* EXTENSIONS;
extern const char* SELECT_PROCESS;
extern const char* ATTACH_INFO;
extern const char* SEARCH_GAME;
extern const char* PROCESSES;
extern const char* CODE_INFODUMP;
2019-06-09 18:19:54 +08:00
extern const char* HOOK_SEARCH_UNSTABLE_WARNING;
extern const char* SEARCH_CJK;
2019-06-02 14:09:17 +08:00
extern const char* SEARCH_PATTERN;
extern const char* SEARCH_DURATION;
extern const char* PATTERN_OFFSET;
extern const char* MIN_ADDRESS;
extern const char* MAX_ADDRESS;
extern const char* STRING_OFFSET;
extern const char* HOOK_SEARCH_FILTER;
2019-06-02 14:09:17 +08:00
extern const char* START_HOOK_SEARCH;
extern const char* SAVE_SEARCH_RESULTS;
extern const char* TEXT_FILES;
2019-06-11 10:47:58 +08:00
extern const char* DOUBLE_CLICK_TO_REMOVE_HOOK;
2019-02-28 00:33:17 +08:00
extern const char* SAVE_SETTINGS;
extern const char* USE_JP_LOCALE;
extern const char* FILTER_REPETITION;
extern const char* AUTO_ATTACH;
extern const char* ATTACH_SAVED_ONLY;
2019-02-28 00:33:17 +08:00
extern const char* DEFAULT_CODEPAGE;
extern const char* FLUSH_DELAY;
extern const char* MAX_BUFFER_SIZE;
extern const wchar_t* ABOUT;
extern const wchar_t* CL_OPTIONS;
extern const wchar_t* LAUNCH_FAILED;
extern const wchar_t* INVALID_CODE;
2018-07-22 06:40:16 +08:00
MainWindow::MainWindow(QWidget *parent) :
2018-07-26 01:46:59 +08:00
QMainWindow(parent),
2018-11-01 22:38:14 +08:00
ui(new Ui::MainWindow),
2018-11-10 14:17:02 +08:00
extenWindow(new ExtenWindow(this))
2018-07-22 06:40:16 +08:00
{
2018-07-26 01:46:59 +08:00
ui->setupUi(this);
for (auto [text, slot] : Array<std::tuple<QString, void(MainWindow::*)()>>{
2018-12-19 01:14:54 +08:00
{ ATTACH, &MainWindow::AttachProcess },
{ LAUNCH, &MainWindow::LaunchProcess },
2018-12-19 01:14:54 +08:00
{ DETACH, &MainWindow::DetachProcess },
{ FORGET, &MainWindow::ForgetProcess },
2018-12-19 01:14:54 +08:00
{ ADD_HOOK, &MainWindow::AddHook },
2019-06-10 13:49:11 +08:00
{ REMOVE_HOOKS, &MainWindow::RemoveHooks },
2018-12-19 01:14:54 +08:00
{ SAVE_HOOKS, &MainWindow::SaveHooks },
{ SEARCH_FOR_HOOKS, &MainWindow::FindHooks },
2018-12-19 01:14:54 +08:00
{ SETTINGS, &MainWindow::Settings },
{ EXTENSIONS, &MainWindow::Extensions }
2018-12-15 11:26:49 +08:00
})
{
2019-02-25 14:06:35 +08:00
auto button = new QPushButton(ui->processFrame);
2018-12-15 11:26:49 +08:00
connect(button, &QPushButton::clicked, this, slot);
button->setText(text);
ui->processLayout->addWidget(button);
2018-12-15 11:26:49 +08:00
}
ui->processLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
2018-08-22 10:43:30 +08:00
connect(ui->ttCombo, qOverload<int>(&QComboBox::activated), this, &MainWindow::ViewThread);
2019-01-23 03:57:13 +08:00
connect(ui->textOutput, &QPlainTextEdit::selectionChanged, [this] { if (!(QApplication::mouseButtons() & Qt::LeftButton)) ui->textOutput->copy(); });
2018-12-19 01:14:54 +08:00
QSettings settings(CONFIG_FILE, QSettings::IniFormat);
2019-01-06 13:07:20 +08:00
if (settings.contains(WINDOW)) setGeometry(settings.value(WINDOW).toRect());
2019-02-05 04:53:13 +08:00
TextThread::filterRepetition = settings.value(FILTER_REPETITION, TextThread::filterRepetition).toBool();
autoAttach = settings.value(AUTO_ATTACH, autoAttach).toBool();
autoAttachSavedOnly = settings.value(ATTACH_SAVED_ONLY, autoAttachSavedOnly).toBool();
2019-01-01 04:06:47 +08:00
TextThread::flushDelay = settings.value(FLUSH_DELAY, TextThread::flushDelay).toInt();
TextThread::maxBufferSize = settings.value(MAX_BUFFER_SIZE, TextThread::maxBufferSize).toInt();
2019-01-05 16:47:32 +08:00
Host::defaultCodepage = settings.value(DEFAULT_CODEPAGE, Host::defaultCodepage).toInt();
2018-08-22 10:43:30 +08:00
Host::Start(
2018-12-19 01:55:11 +08:00
[this](DWORD processId) { ProcessConnected(processId); },
[this](DWORD processId) { ProcessDisconnected(processId); },
2019-02-05 04:18:47 +08:00
[this](TextThread& thread) { ThreadAdded(thread); },
[this](TextThread& thread) { ThreadRemoved(thread); },
[this](TextThread& thread, std::wstring& output) { return SentenceReceived(thread, output); }
2018-08-22 10:43:30 +08:00
);
2019-02-09 13:30:38 +08:00
current = &Host::GetThread(Host::console);
2018-11-04 15:13:51 +08:00
Host::AddConsoleOutput(ABOUT);
2019-02-22 02:18:37 +08:00
AttachConsole(ATTACH_PARENT_PROCESS);
2019-06-04 05:58:30 +08:00
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), CL_OPTIONS, wcslen(CL_OPTIONS), DUMMY, NULL);
auto processes = Util::GetAllProcesses();
2019-02-19 11:39:04 +08:00
int argc;
std::unique_ptr<LPWSTR[], Functor<LocalFree>> argv(CommandLineToArgvW(GetCommandLineW(), &argc));
for (int i = 0; i < argc; ++i)
if (std::wstring arg = argv[i]; arg[0] == L'/' || arg[0] == L'-')
2019-06-14 00:06:15 +08:00
if (arg[1] == L'p' || arg[1] == L'P')
2019-02-19 11:39:04 +08:00
if (DWORD processId = _wtoi(arg.substr(2).c_str())) Host::InjectProcess(processId);
else for (auto [processId, processName] : processes)
if (processName.value_or(L"").find(L"\\" + arg.substr(2)) != std::wstring::npos) Host::InjectProcess(processId);
2019-02-19 11:39:04 +08:00
std::thread([this]
{
for (; ; Sleep(10000))
{
std::unordered_set<std::wstring> attachTargets;
if (autoAttach)
for (auto process : QString(QTextFile(GAME_SAVE_FILE, QIODevice::ReadOnly).readAll()).split("\n", QString::SkipEmptyParts))
attachTargets.insert(S(process));
if (autoAttachSavedOnly)
for (auto process : QString(QTextFile(HOOK_SAVE_FILE, QIODevice::ReadOnly).readAll()).split("\n", QString::SkipEmptyParts))
attachTargets.insert(S(process.split(" , ")[0]));
if (!attachTargets.empty())
for (auto [processId, processName] : Util::GetAllProcesses())
if (processName && attachTargets.count(processName.value()) > 0 && alreadyAttached.count(processId) == 0) Host::InjectProcess(processId);
}
}).detach();
2018-07-22 06:40:16 +08:00
}
MainWindow::~MainWindow()
{
QSettings(CONFIG_FILE, QSettings::IniFormat).setValue(WINDOW, geometry());
2019-06-21 13:29:48 +08:00
CleanupExtensions();
2018-12-28 07:52:59 +08:00
SetErrorMode(SEM_NOGPFAULTERRORBOX);
2018-12-04 07:31:00 +08:00
ExitProcess(0);
2018-07-22 06:40:16 +08:00
}
2018-07-23 07:53:51 +08:00
void MainWindow::closeEvent(QCloseEvent*)
{
QCoreApplication::quit(); // Need to do this to kill any windows that might've been made by extensions
}
void MainWindow::ProcessConnected(DWORD processId)
2018-07-24 13:57:54 +08:00
{
alreadyAttached.insert(processId);
2019-02-03 21:49:58 +08:00
QString process = S(Util::GetModuleFilename(processId).value_or(L"???"));
QMetaObject::invokeMethod(this, [this, process, processId]
{
ui->processCombo->addItem(QString::number(processId, 16).toUpper() + ": " + QFileInfo(process).fileName());
2019-02-03 21:49:58 +08:00
});
if (process == "???") return;
2019-04-23 01:35:40 +08:00
// This does add (potentially tons of) duplicates to the file, but as long as I don't perform Ω(N^2) operations it shouldn't be an issue
2019-02-03 21:49:58 +08:00
QTextFile(GAME_SAVE_FILE, QIODevice::WriteOnly | QIODevice::Append).write((process + "\n").toUtf8());
2019-02-03 21:49:58 +08:00
QStringList allProcesses = QString(QTextFile(HOOK_SAVE_FILE, QIODevice::ReadOnly).readAll()).split("\n", QString::SkipEmptyParts);
// Can't use QFileInfo::absoluteFilePath since hook save file has '\\' as path separator
auto hookList = std::find_if(allProcesses.rbegin(), allProcesses.rend(), [&](QString hookList) { return hookList.contains(process); });
if (hookList != allProcesses.rend())
for (auto hookInfo : hookList->split(" , "))
2019-02-05 04:18:47 +08:00
if (auto hp = Util::ParseCode(S(hookInfo))) Host::InsertHook(processId, hp.value());
else swscanf_s(S(hookInfo).c_str(), L"|%I64d:%I64d:%[^\n]", &savedThreadCtx, &savedThreadCtx2, savedThreadCode, (unsigned)std::size(savedThreadCode));
2018-07-24 13:57:54 +08:00
}
void MainWindow::ProcessDisconnected(DWORD processId)
2018-07-24 13:57:54 +08:00
{
2019-01-06 15:57:52 +08:00
QMetaObject::invokeMethod(this, [this, processId]
{
ui->processCombo->removeItem(ui->processCombo->findText(QString::number(processId, 16).toUpper() + ":", Qt::MatchStartsWith));
}, Qt::BlockingQueuedConnection);
2018-07-24 13:57:54 +08:00
}
2019-02-05 04:18:47 +08:00
void MainWindow::ThreadAdded(TextThread& thread)
2018-07-24 13:57:54 +08:00
{
2019-02-05 04:18:47 +08:00
std::wstring threadCode = Util::GenerateCode(thread.hp, thread.tp.processId);
QString ttString = TextThreadString(thread) + S(thread.name) + " (" + S(threadCode) + ")";
bool savedMatch = savedThreadCtx == thread.tp.ctx && savedThreadCtx2 == thread.tp.ctx2 && savedThreadCode == threadCode;
if (savedMatch) savedThreadCtx = savedThreadCtx2 = savedThreadCode[0] = 0;
2019-02-03 21:49:58 +08:00
QMetaObject::invokeMethod(this, [this, ttString, savedMatch]
2019-01-06 15:57:52 +08:00
{
ui->ttCombo->addItem(ttString);
2019-02-03 21:49:58 +08:00
if (savedMatch) ViewThread(ui->ttCombo->count() - 1);
2019-01-06 15:57:52 +08:00
});
}
2019-02-05 04:18:47 +08:00
void MainWindow::ThreadRemoved(TextThread& thread)
{
QString ttString = TextThreadString(thread);
2018-12-19 01:55:11 +08:00
QMetaObject::invokeMethod(this, [this, ttString]
2018-07-26 01:46:59 +08:00
{
int threadIndex = ui->ttCombo->findText(ttString, Qt::MatchStartsWith);
2019-02-03 21:49:58 +08:00
if (threadIndex == ui->ttCombo->currentIndex()) ViewThread(0);
ui->ttCombo->removeItem(threadIndex);
2019-01-06 15:57:52 +08:00
}, Qt::BlockingQueuedConnection);
2018-07-24 13:57:54 +08:00
}
2019-02-05 04:18:47 +08:00
bool MainWindow::SentenceReceived(TextThread& thread, std::wstring& sentence)
2018-10-08 12:26:43 +08:00
{
2019-02-09 13:30:38 +08:00
if (!DispatchSentenceToExtensions(sentence, GetMiscInfo(thread).data())) return false;
sentence += L'\n';
if (current == &thread) QMetaObject::invokeMethod(this, [this, sentence]
2018-10-08 12:26:43 +08:00
{
2019-02-09 13:30:38 +08:00
ui->textOutput->moveCursor(QTextCursor::End);
ui->textOutput->insertPlainText(S(sentence));
ui->textOutput->moveCursor(QTextCursor::End);
});
return true;
2018-10-08 12:26:43 +08:00
}
2019-02-05 04:18:47 +08:00
QString MainWindow::TextThreadString(TextThread& thread)
2018-08-23 03:11:40 +08:00
{
2018-09-22 10:25:37 +08:00
return QString("%1:%2:%3:%4:%5: ").arg(
2019-02-05 04:18:47 +08:00
QString::number(thread.handle, 16),
QString::number(thread.tp.processId, 16),
QString::number(thread.tp.addr, 16),
QString::number(thread.tp.ctx, 16),
QString::number(thread.tp.ctx2, 16)
2018-08-23 03:11:40 +08:00
).toUpper();
}
ThreadParam MainWindow::ParseTextThreadString(QString ttString)
2018-08-23 03:11:40 +08:00
{
QStringList threadParam = ttString.split(":");
return { threadParam[1].toUInt(nullptr, 16), threadParam[2].toULongLong(nullptr, 16), threadParam[3].toULongLong(nullptr, 16), threadParam[4].toULongLong(nullptr, 16) };
2018-08-23 03:11:40 +08:00
}
DWORD MainWindow::GetSelectedProcessId()
{
return ui->processCombo->currentText().split(":")[0].toULong(nullptr, 16);
2018-08-23 03:11:40 +08:00
}
2019-02-09 13:30:38 +08:00
std::array<InfoForExtension, 10> MainWindow::GetMiscInfo(TextThread& thread)
{
void(*AddSentence)(MainWindow*, int64_t, const wchar_t*) = [](MainWindow* This, int64_t number, const wchar_t* sentence)
{
2019-06-21 13:29:48 +08:00
std::wstring copy = sentence;
// pointer from Host::GetThread may not stay valid unless on main thread
2019-06-21 13:29:48 +08:00
QMetaObject::invokeMethod(This, [=]() mutable { if (TextThread* thread = Host::GetThread(number)) thread->AddSentence(std::move(copy)); });
};
2018-12-29 01:14:56 +08:00
return
2019-02-09 13:30:38 +08:00
{ {
{ "current select", &thread == current },
2019-02-05 04:18:47 +08:00
{ "text number", thread.handle },
{ "process id", thread.tp.processId },
2019-02-09 13:30:38 +08:00
{ "hook address", (int64_t)thread.tp.addr },
2019-02-05 04:18:47 +08:00
{ "text handle", thread.handle },
2019-02-09 13:30:38 +08:00
{ "text name", (int64_t)thread.name.c_str() },
{ "this", (int64_t)this },
{ "void (*AddSentence)(void* this, int64_t number, const wchar_t* sentence)", (int64_t)AddSentence },
2019-02-09 13:30:38 +08:00
{ nullptr, 0 } // nullptr marks end of info array
} };
}
2018-12-19 01:14:54 +08:00
void MainWindow::AttachProcess()
2018-07-23 07:53:51 +08:00
{
QMultiHash<QString, DWORD> allProcesses;
for (auto [processId, processName] : Util::GetAllProcesses())
if (processName) allProcesses.insert(QFileInfo(S(processName.value())).fileName(), processId);
2018-09-10 10:37:48 +08:00
QStringList processList(allProcesses.uniqueKeys());
2018-08-21 02:30:50 +08:00
processList.sort(Qt::CaseInsensitive);
if (QString process = QInputDialog::getItem(this, SELECT_PROCESS, ATTACH_INFO, processList, 0, true, &ok, Qt::WindowCloseButtonHint); ok)
if (process.toInt(nullptr, 0)) Host::InjectProcess(process.toInt(nullptr, 0));
else for (auto processId : allProcesses.values(process)) Host::InjectProcess(processId);
2018-07-24 13:57:54 +08:00
}
void MainWindow::LaunchProcess()
{
QStringList savedProcesses = QString::fromUtf8(QTextFile(GAME_SAVE_FILE, QIODevice::ReadOnly).readAll()).split("\n", QString::SkipEmptyParts);
2019-02-03 05:50:28 +08:00
std::reverse(savedProcesses.begin(), savedProcesses.end());
2019-02-14 05:22:49 +08:00
savedProcesses.removeDuplicates();
savedProcesses.push_back(SEARCH_GAME);
std::wstring process = S(QInputDialog::getItem(this, SELECT_PROCESS, "", savedProcesses, 0, true, &ok, Qt::WindowCloseButtonHint));
if (!ok) return;
if (S(process) == SEARCH_GAME) process = S(QDir::toNativeSeparators(QFileDialog::getOpenFileName(this, SELECT_PROCESS, "C:\\", PROCESSES)));
2019-01-23 00:23:35 +08:00
if (process.find(L'\\') == std::wstring::npos) return;
std::wstring path = std::wstring(process).erase(process.rfind(L'\\'));
PROCESS_INFORMATION info = {};
2019-06-13 15:19:08 +08:00
if (!x64 && QMessageBox::question(this, SELECT_PROCESS, USE_JP_LOCALE) == QMessageBox::Yes)
2019-06-10 13:49:11 +08:00
{
if (HMODULE localeEmulator = LoadLibraryOnce(L"LoaderDll"))
{
// see https://github.com/xupefei/Locale-Emulator/blob/aa99dec3b25708e676c90acf5fed9beaac319160/LEProc/LoaderWrapper.cs#L252
struct
{
ULONG AnsiCodePage = SHIFT_JIS;
ULONG OemCodePage = SHIFT_JIS;
ULONG LocaleID = LANG_JAPANESE;
ULONG DefaultCharset = SHIFTJIS_CHARSET;
ULONG HookUiLanguageApi = FALSE;
WCHAR DefaultFaceName[LF_FACESIZE] = {};
TIME_ZONE_INFORMATION Timezone;
ULONG64 Unused = 0;
} LEB;
GetTimeZoneInformation(&LEB.Timezone);
((LONG(__stdcall*)(decltype(&LEB), LPCWSTR appName, LPWSTR commandLine, LPCWSTR currentDir, void*, void*, PROCESS_INFORMATION*, void*, void*, void*, void*))
2019-01-23 00:23:35 +08:00
GetProcAddress(localeEmulator, "LeCreateProcess"))(&LEB, process.c_str(), NULL, path.c_str(), NULL, NULL, &info, NULL, NULL, NULL, NULL);
}
2019-06-10 13:49:11 +08:00
}
if (info.hProcess == NULL)
{
STARTUPINFOW DUMMY = { sizeof(DUMMY) };
2019-01-23 00:23:35 +08:00
CreateProcessW(process.c_str(), NULL, nullptr, nullptr, FALSE, 0, nullptr, path.c_str(), &DUMMY, &info);
}
if (info.hProcess == NULL) return Host::AddConsoleOutput(LAUNCH_FAILED);
Host::InjectProcess(info.dwProcessId);
CloseHandle(info.hProcess);
CloseHandle(info.hThread);
}
2018-12-19 01:14:54 +08:00
void MainWindow::DetachProcess()
2018-07-24 13:57:54 +08:00
{
2019-06-22 10:55:29 +08:00
try { Host::DetachProcess(GetSelectedProcessId()); } catch (std::out_of_range) {}
2018-07-24 13:57:54 +08:00
}
void MainWindow::ForgetProcess()
{
if (auto processName = Util::GetModuleFilename(GetSelectedProcessId()))
{
for (auto file : { GAME_SAVE_FILE, HOOK_SAVE_FILE })
{
QStringList lines = QString::fromUtf8(QTextFile(file, QIODevice::ReadOnly).readAll()).split("\n", QString::SkipEmptyParts);
lines.erase(std::remove_if(lines.begin(), lines.end(), [&](auto line) { return line.contains(S(processName.value())); }), lines.end());
QTextFile(file, QIODevice::WriteOnly | QIODevice::Truncate).write(lines.join("\n").toUtf8());
}
}
DetachProcess();
}
2018-12-19 01:14:54 +08:00
void MainWindow::AddHook()
2018-07-26 12:48:18 +08:00
{
if (QString hookCode = QInputDialog::getText(this, ADD_HOOK, CODE_INFODUMP, QLineEdit::Normal, "", &ok, Qt::WindowCloseButtonHint); ok)
2019-06-14 07:29:30 +08:00
if (auto hp = Util::ParseCode(S(hookCode))) try { Host::InsertHook(GetSelectedProcessId(), hp.value()); } catch (std::out_of_range) {}
else Host::AddConsoleOutput(INVALID_CODE);
2018-07-26 12:48:18 +08:00
}
2019-06-10 13:49:11 +08:00
void MainWindow::RemoveHooks()
{
DWORD processId = GetSelectedProcessId();
std::unordered_map<uint64_t, HookParam> hooks;
for (int i = 0; i < ui->ttCombo->count(); ++i)
{
ThreadParam tp = ParseTextThreadString(ui->ttCombo->itemText(i));
if (tp.processId == GetSelectedProcessId()) hooks[tp.addr] = Host::GetThread(tp).hp;
2019-06-10 13:49:11 +08:00
}
auto hookList = new QListWidget(this);
2019-06-11 10:47:58 +08:00
hookList->setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint);
hookList->setAttribute(Qt::WA_DeleteOnClose);
2019-06-11 10:47:58 +08:00
hookList->setMinimumSize({ 300, 50 });
hookList->setWindowTitle(DOUBLE_CLICK_TO_REMOVE_HOOK);
for (auto [address, hp] : hooks)
2019-06-10 13:49:11 +08:00
new QListWidgetItem(QString(hp.name) + "@" + QString::number(address, 16), hookList);
connect(hookList, &QListWidget::itemDoubleClicked, [processId, hookList](QListWidgetItem* item)
{
try
{
Host::RemoveHook(processId, item->text().split("@")[1].toULongLong(nullptr, 16));
delete item;
}
catch (std::out_of_range) { hookList->close(); }
});
hookList->show();
}
2018-12-19 01:14:54 +08:00
void MainWindow::SaveHooks()
2018-07-27 13:42:21 +08:00
{
auto processName = Util::GetModuleFilename(GetSelectedProcessId());
if (!processName) return;
QHash<uint64_t, QString> hookCodes;
for (int i = 0; i < ui->ttCombo->count(); ++i)
2018-11-28 05:57:47 +08:00
{
ThreadParam tp = ParseTextThreadString(ui->ttCombo->itemText(i));
if (tp.processId == GetSelectedProcessId())
{
HookParam hp = Host::GetThread(tp).hp;
if (!(hp.type & HOOK_ENGINE)) hookCodes[tp.addr] = S(Util::GenerateCode(hp, tp.processId));
}
2018-11-28 05:57:47 +08:00
}
auto hookInfo = QStringList() << S(processName.value()) << hookCodes.values();
ThreadParam tp = current->tp;
if (tp.processId == GetSelectedProcessId()) hookInfo << QString("|%1:%2:%3").arg(tp.ctx).arg(tp.ctx2).arg(S(Util::GenerateCode(current->hp, tp.processId)));
QTextFile(HOOK_SAVE_FILE, QIODevice::WriteOnly | QIODevice::Append).write((hookInfo.join(" , ") + "\n").toUtf8());
2018-07-27 13:42:21 +08:00
}
2019-06-02 14:09:17 +08:00
void MainWindow::FindHooks()
{
QMessageBox::information(this, SEARCH_FOR_HOOKS, HOOK_SEARCH_UNSTABLE_WARNING);
DWORD processId = GetSelectedProcessId();
SearchParam sp = {};
bool customSettings = false;
std::wregex filter(L".");
QDialog dialog(this, Qt::WindowCloseButtonHint);
QFormLayout layout(&dialog);
QCheckBox cjkCheckbox(&dialog);
layout.addRow(SEARCH_CJK, &cjkCheckbox);
QDialogButtonBox confirm(QDialogButtonBox::Ok | QDialogButtonBox::Help, &dialog);
layout.addRow(&confirm);
confirm.button(QDialogButtonBox::Ok)->setText(START_HOOK_SEARCH);
confirm.button(QDialogButtonBox::Help)->setText(SETTINGS);
connect(&confirm, &QDialogButtonBox::helpRequested, [&customSettings] { customSettings = true; });
connect(&confirm, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
connect(&confirm, &QDialogButtonBox::helpRequested, &dialog, &QDialog::accept);
dialog.setWindowTitle(SEARCH_FOR_HOOKS);
2019-06-21 13:29:48 +08:00
if (!dialog.exec()) return;
if (customSettings)
2019-06-02 14:09:17 +08:00
{
QDialog dialog(this, Qt::WindowCloseButtonHint);
QFormLayout layout(&dialog);
QLineEdit patternInput(x64 ? "CC CC 48 89" : "CC CC 55 8B EC", &dialog);
layout.addRow(SEARCH_PATTERN, &patternInput);
for (auto [value, label] : Array<std::tuple<int&, const char*>>{
{ sp.searchTime = 20000, SEARCH_DURATION },
{ sp.offset = 2, PATTERN_OFFSET },
})
2019-06-02 14:09:17 +08:00
{
auto spinBox = new QSpinBox(&dialog);
spinBox->setMaximum(INT_MAX);
spinBox->setValue(value);
layout.addRow(label, spinBox);
connect(spinBox, qOverload<int>(&QSpinBox::valueChanged), [&value] (int newValue) { value = newValue; });
}
for (auto [value, label] : Array<std::tuple<uintptr_t&, const char*>>{
{ sp.minAddress = 0, MIN_ADDRESS },
{ sp.maxAddress = -1ULL, MAX_ADDRESS },
{ sp.padding = 0, STRING_OFFSET }
})
{
auto input = new QLineEdit(QString::number(value, 16), &dialog);
layout.addRow(label, input);
connect(input, &QLineEdit::textEdited, [&value](QString input)
2019-06-02 14:09:17 +08:00
{
bool ok;
if (uintptr_t newValue = input.toULongLong(&ok, 16); ok) value = newValue;
2019-06-02 14:09:17 +08:00
});
}
QLineEdit filterInput(".", &dialog);
layout.addRow(HOOK_SEARCH_FILTER, &filterInput);
QPushButton startButton(START_HOOK_SEARCH, &dialog);
layout.addWidget(&startButton);
connect(&startButton, &QPushButton::clicked, &dialog, &QDialog::accept);
2019-06-21 13:29:48 +08:00
if (!dialog.exec()) return;
QByteArray pattern = QByteArray::fromHex(patternInput.text().replace("??", QString::number(XX, 16)).toUtf8());
memcpy(sp.pattern, pattern.data(), sp.length = min(pattern.size(), 25));
2019-06-29 11:16:31 +08:00
try { filter = S(filterInput.text()); } catch (std::regex_error) {}
}
else
{
// sp.length is 0 in this branch, so default will be used
2019-06-29 11:16:31 +08:00
filter = std::wregex(cjkCheckbox.isChecked() ? L"[\\u3000-\\ua000]{4,}" : L"[\\u0020-\\u1000]{4,}");
}
2019-06-02 14:09:17 +08:00
auto hooks = std::make_shared<QString>();
try
{
Host::FindHooks(processId, sp, [processId, hooks, filter](HookParam hp, const std::wstring& text)
{
if (std::regex_search(text, filter)) hooks->append(S(Util::GenerateCode(hp, processId)) + ": " + S(text) + "\n");
});
2019-06-18 12:39:50 +08:00
}
catch (std::out_of_range) { return; }
QString saveFile = QFileDialog::getSaveFileName(this, SAVE_SEARCH_RESULTS, "./Hooks.txt", TEXT_FILES);
if (saveFile.isEmpty()) saveFile = "Hooks.txt";
std::thread([hooks, saveFile]
{
for (int lastSize = 0; hooks->size() == 0 || hooks->size() != lastSize; Sleep(2000)) lastSize = hooks->size();
QTextFile(saveFile, QIODevice::WriteOnly | QIODevice::Truncate).write(hooks->toUtf8());
hooks->clear();
}).detach();
2019-06-02 14:09:17 +08:00
}
2018-12-19 01:14:54 +08:00
void MainWindow::Settings()
2018-11-10 18:13:59 +08:00
{
QDialog dialog(this, Qt::WindowCloseButtonHint);
QSettings settings(CONFIG_FILE, QSettings::IniFormat, &dialog);
QFormLayout layout(&dialog);
QPushButton saveButton(SAVE_SETTINGS, &dialog);
for (auto [value, label] : Array<std::tuple<bool&, const char*>>{
{ TextThread::filterRepetition, FILTER_REPETITION },
{ autoAttach, AUTO_ATTACH },
{ autoAttachSavedOnly, ATTACH_SAVED_ONLY },
})
{
auto checkBox = new QCheckBox(&dialog);
checkBox->setChecked(value);
layout.addRow(label, checkBox);
connect(&saveButton, &QPushButton::clicked, [checkBox, label, &settings, &value] { settings.setValue(label, value = checkBox->isChecked()); });
}
for (auto [value, label] : Array<std::tuple<int&, const char*>>{
{ TextThread::maxBufferSize, MAX_BUFFER_SIZE },
{ TextThread::flushDelay, FLUSH_DELAY },
{ Host::defaultCodepage, DEFAULT_CODEPAGE },
})
{
auto spinBox = new QSpinBox(&dialog);
spinBox->setMaximum(INT_MAX);
spinBox->setValue(value);
layout.addRow(label, spinBox);
connect(&saveButton, &QPushButton::clicked, [spinBox, label, &settings, &value] { settings.setValue(label, value = spinBox->value()); });
}
layout.addWidget(&saveButton);
connect(&saveButton, &QPushButton::clicked, &dialog, &QDialog::accept);
dialog.setWindowTitle(SETTINGS);
dialog.exec();
2018-11-10 18:13:59 +08:00
}
2018-12-19 01:14:54 +08:00
void MainWindow::Extensions()
2018-07-24 13:57:54 +08:00
{
extenWindow->activateWindow();
extenWindow->showNormal();
2018-07-23 07:53:51 +08:00
}
2018-07-27 13:42:21 +08:00
2018-12-19 01:14:54 +08:00
void MainWindow::ViewThread(int index)
2018-07-27 13:42:21 +08:00
{
2019-02-03 21:49:58 +08:00
ui->ttCombo->setCurrentIndex(index);
2019-02-09 13:30:38 +08:00
ui->textOutput->setPlainText(S((current = &Host::GetThread(ParseTextThreadString(ui->ttCombo->itemText(index))))->storage->c_str()));
ui->textOutput->moveCursor(QTextCursor::End);
2018-07-27 13:42:21 +08:00
}