forked from MUME/MMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserActionManager.cpp
More file actions
228 lines (205 loc) · 6.94 KB
/
Copy pathUserActionManager.cpp
File metadata and controls
228 lines (205 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 The MMapper Authors
#include "UserActionManager.h"
#include "../configuration/configuration.h"
#include "../global/TextUtils.h"
#include "../global/logging.h"
#include <algorithm>
#include <regex>
std::string UserAction::serialize() const
{
std::string typeStr;
switch (type) {
case UserActionType::Regex:
typeStr = "regex";
break;
case UserActionType::StartsWith:
typeStr = "starts";
break;
case UserActionType::EndsWith:
typeStr = "ends";
break;
}
return typeStr + ":" + command;
}
std::unique_ptr<UserAction> UserAction::deserialize(const std::string &pattern,
const std::string &serialized)
{
size_t colon = serialized.find(':');
if (colon == std::string::npos) {
return nullptr;
}
std::string typeStr = serialized.substr(0, colon);
std::string command = serialized.substr(colon + 1);
UserActionType type;
if (typeStr == "regex") {
type = UserActionType::Regex;
} else if (typeStr == "starts") {
type = UserActionType::StartsWith;
} else if (typeStr == "ends") {
type = UserActionType::EndsWith;
} else {
return nullptr;
}
return std::make_unique<UserAction>(UserAction{type, pattern, command});
}
CompiledUserAction::CompiledUserAction(UserAction a)
: action(std::move(a))
{
using namespace char_consts;
switch (action.type) {
case UserActionType::Regex:
try {
regex.emplace(action.pattern, std::regex::optimize);
if (action.pattern.length() > 2 && action.pattern[0] == C_CARET
&& action.pattern[1] != C_BACKSLASH && action.pattern[1] != C_OPEN_PARENS) {
hint = action.pattern[1];
}
} catch (const std::regex_error &) {
// ignore
}
break;
case UserActionType::StartsWith:
if (!action.pattern.empty()) {
hint = action.pattern[0];
}
break;
case UserActionType::EndsWith:
break;
}
}
UserActionManager::UserActionManager()
{
setConfig().actions.registerChangeCallback(m_configLifetime, [this]() { syncFromConfig(); });
setConfig().actions.registerResetCallback(m_configLifetime, [this]() {
m_actions.clear();
m_actionMap.clear();
setConfig().actions.setData(QVariantMap());
});
syncFromConfig();
}
void UserActionManager::syncFromConfig()
{
m_actions.clear();
m_actionMap.clear();
const QVariantMap &data = getConfig().actions.data();
for (auto it = data.begin(); it != data.end(); ++it) {
std::string pattern = mmqt::toStdStringUtf8(it.key());
std::string serialized = mmqt::toStdStringUtf8(it.value().toString());
if (auto action = UserAction::deserialize(pattern, serialized)) {
auto compiled = std::make_shared<CompiledUserAction>(std::move(*action));
m_actions[pattern] = compiled;
m_actionMap.emplace(compiled->hint, compiled);
} else {
MMLOG_WARNING() << "invalid action" << mmqt::toStdStringUtf8(it.key())
<< mmqt::toStdStringUtf8(it.value().toString());
}
}
}
bool UserActionManager::setAction(UserActionType type, std::string pattern, std::string command)
{
if (pattern.empty()) {
return false;
}
QVariantMap data = getConfig().actions.data();
UserAction action{type, pattern, command};
data[mmqt::toQStringUtf8(pattern)] = mmqt::toQStringUtf8(action.serialize());
setConfig().actions.setData(std::move(data));
return true;
}
bool UserActionManager::removeAction(const std::string &pattern)
{
QVariantMap data = getConfig().actions.data();
QString key = mmqt::toQStringUtf8(pattern);
if (!data.contains(key)) {
return false;
}
data.remove(key);
setConfig().actions.setData(std::move(data));
return true;
}
std::vector<const UserAction *> UserActionManager::getAllActions() const
{
std::vector<const UserAction *> result;
for (const auto &pair : m_actions) {
result.push_back(&pair.second->action);
}
return result;
}
std::string UserActionManager::substitute(const std::string &command,
const std::vector<std::string> &captures)
{
std::string result;
result.reserve(command.size());
for (size_t i = 0; i < command.size(); ++i) {
if (command[i] == '%' && i + 1 < command.size() && std::isdigit(command[i + 1])) {
size_t idx = static_cast<size_t>(command[i + 1] - '0');
if (idx < captures.size()) {
result += captures[idx];
}
++i;
} else {
result += command[i];
}
}
return result;
}
void UserActionManager::evaluate(
StringView line, const std::function<void(const std::string &)> &executeCallback) const
{
if (line.empty()) {
return;
}
auto runMatch = [&](const std::shared_ptr<CompiledUserAction> &compiled) {
const UserAction &action = compiled->action;
bool matched = false;
std::vector<std::string> captures;
switch (action.type) {
case UserActionType::Regex: {
if (compiled->regex) {
std::cmatch match;
const std::string_view sv = line.getStdStringView();
if (std::regex_search(sv.data(), sv.data() + sv.size(), match, *compiled->regex)) {
matched = true;
captures.reserve(match.size());
for (size_t i = 0; i < match.size(); ++i) {
const auto &sub = match[i];
if (sub.matched) {
captures.emplace_back(sub.first, static_cast<size_t>(sub.length()));
} else {
captures.emplace_back();
}
}
}
}
break;
}
case UserActionType::StartsWith:
if (line.startsWith(std::string_view(action.pattern))) {
matched = true;
captures.emplace_back(line.toStdString());
}
break;
case UserActionType::EndsWith:
if (line.endsWith(std::string_view(action.pattern))) {
matched = true;
captures.emplace_back(line.toStdString());
}
break;
}
if (matched) {
executeCallback(substitute(action.command, captures));
}
};
const char firstChar = line.firstChar();
auto range = m_actionMap.equal_range(firstChar);
for (auto it = range.first; it != range.second; ++it) {
runMatch(it->second);
}
if (firstChar != 0) {
range = m_actionMap.equal_range(0);
for (auto it = range.first; it != range.second; ++it) {
runMatch(it->second);
}
}
}