2019-06-29 08:46:31 +05:30
|
|
|
#include "qtcommon.h"
|
2018-12-17 20:48:14 -05:00
|
|
|
#include "extension.h"
|
2019-06-03 19:29:37 -04:00
|
|
|
#include "ui_regexfilter.h"
|
2020-02-28 02:33:51 -07:00
|
|
|
#include "module.h"
|
|
|
|
#include "blockmarkup.h"
|
|
|
|
#include <fstream>
|
2018-12-17 20:48:14 -05:00
|
|
|
|
2019-02-27 11:33:17 -05:00
|
|
|
extern const char* REGEX_FILTER;
|
|
|
|
extern const char* INVALID_REGEX;
|
|
|
|
extern const char* CURRENT_FILTER;
|
|
|
|
|
2020-02-28 02:33:51 -07:00
|
|
|
const char* REGEX_SAVE_FILE = "SavedRegexFilters.txt";
|
|
|
|
|
|
|
|
std::optional<std::wregex> regex;
|
2019-01-23 13:59:34 -05:00
|
|
|
std::shared_mutex m;
|
2020-03-02 23:49:31 -07:00
|
|
|
DWORD (*GetSelectedProcessId)() = nullptr;
|
2018-12-17 20:48:14 -05:00
|
|
|
|
2020-03-16 02:56:04 -06:00
|
|
|
class Window : public QDialog
|
2018-12-21 14:18:43 -05:00
|
|
|
{
|
2019-06-21 01:29:48 -04:00
|
|
|
public:
|
|
|
|
Window()
|
2020-03-16 02:56:04 -06:00
|
|
|
: QDialog(nullptr, Qt::WindowMinMaxButtonsHint)
|
2018-12-17 20:48:14 -05:00
|
|
|
{
|
2021-01-15 06:07:23 -07:00
|
|
|
Localize();
|
2019-06-03 19:29:37 -04:00
|
|
|
ui.setupUi(this);
|
2019-06-21 01:29:48 -04:00
|
|
|
|
|
|
|
connect(ui.input, &QLineEdit::textEdited, this, &Window::setRegex);
|
2020-02-28 02:33:51 -07:00
|
|
|
connect(ui.save, &QPushButton::clicked, this, &Window::saveRegex);
|
2019-06-21 01:29:48 -04:00
|
|
|
|
2018-12-21 14:18:43 -05:00
|
|
|
setWindowTitle(REGEX_FILTER);
|
2019-06-21 01:29:48 -04:00
|
|
|
QMetaObject::invokeMethod(this, &QWidget::show, Qt::QueuedConnection);
|
2018-12-17 20:48:14 -05:00
|
|
|
}
|
2019-06-03 19:29:37 -04:00
|
|
|
|
2019-06-29 08:46:31 +05:30
|
|
|
void setRegex(QString regex)
|
2018-12-17 20:48:14 -05:00
|
|
|
{
|
2020-02-28 02:33:51 -07:00
|
|
|
ui.input->setText(regex);
|
2021-01-15 06:07:23 -07:00
|
|
|
std::scoped_lock lock(m);
|
2020-02-28 02:33:51 -07:00
|
|
|
if (!regex.isEmpty()) try { ::regex = S(regex); }
|
2019-06-21 01:29:48 -04:00
|
|
|
catch (std::regex_error) { return ui.output->setText(INVALID_REGEX); }
|
2020-02-28 02:33:51 -07:00
|
|
|
else ::regex = std::nullopt;
|
2019-06-29 08:46:31 +05:30
|
|
|
ui.output->setText(QString(CURRENT_FILTER).arg(regex));
|
2018-12-17 20:48:14 -05:00
|
|
|
}
|
2019-06-21 01:29:48 -04:00
|
|
|
|
2020-02-28 02:33:51 -07:00
|
|
|
private:
|
|
|
|
void saveRegex()
|
|
|
|
{
|
|
|
|
auto formatted = FormatString(
|
|
|
|
L"\xfeff|PROCESS|%s|FILTER|%s|END|\r\n",
|
2020-03-02 23:49:31 -07:00
|
|
|
GetModuleFilename(GetSelectedProcessId()).value_or(FormatString(L"Error getting name of process 0x%X", GetSelectedProcessId())),
|
2020-02-28 02:33:51 -07:00
|
|
|
S(ui.input->text())
|
|
|
|
);
|
|
|
|
std::ofstream(REGEX_SAVE_FILE, std::ios::binary | std::ios::app).write((const char*)formatted.c_str(), formatted.size() * sizeof(wchar_t));
|
|
|
|
}
|
|
|
|
|
2019-06-21 01:29:48 -04:00
|
|
|
Ui::FilterWindow ui;
|
|
|
|
} window;
|
2018-12-17 20:48:14 -05:00
|
|
|
|
|
|
|
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
|
|
|
|
{
|
2020-03-02 23:49:31 -07:00
|
|
|
static auto _ = GetSelectedProcessId = (DWORD(*)())sentenceInfo["DWORD (*GetSelectedProcessId)()"];
|
2019-02-21 13:09:44 -05:00
|
|
|
if (sentenceInfo["text number"] == 0) return false;
|
2020-03-02 23:49:31 -07:00
|
|
|
if (sentenceInfo["current select"] && !regex) if (auto processName = GetModuleFilename(sentenceInfo["process id"]))
|
2020-02-28 02:33:51 -07:00
|
|
|
{
|
2020-03-02 23:49:31 -07:00
|
|
|
std::ifstream stream(REGEX_SAVE_FILE, std::ios::binary);
|
|
|
|
BlockMarkupIterator savedFilters(stream, Array<std::wstring_view>{ L"|PROCESS|", L"|FILTER|" });
|
|
|
|
std::vector<std::wstring> regexes;
|
|
|
|
while (auto read = savedFilters.Next()) if (read->at(0) == processName) regexes.push_back(std::move(read->at(1)));
|
|
|
|
if (!regexes.empty()) QMetaObject::invokeMethod(&window, [regex = S(regexes.back())] { window.setRegex(regex); }, Qt::BlockingQueuedConnection);
|
2020-02-28 02:33:51 -07:00
|
|
|
}
|
2019-06-21 01:29:48 -04:00
|
|
|
std::shared_lock l(m);
|
2020-02-28 02:33:51 -07:00
|
|
|
if (regex) sentence = std::regex_replace(sentence, regex.value(), L"");
|
2018-12-17 20:48:14 -05:00
|
|
|
return true;
|
|
|
|
}
|