-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapConfig.hpp
More file actions
171 lines (151 loc) · 5.54 KB
/
Copy pathMapConfig.hpp
File metadata and controls
171 lines (151 loc) · 5.54 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
#pragma once
#include "common.h"
#include "logger.hpp"
#include "fmt/base.h"
#include "fmt/ranges.h"
#include "fmt/ostream.h"
#include "quill/HelperMacros.h"
#define TOML_ENABLE_FORMATTERS 0
#define TOML_EXCEPTIONS 0
#include "toml++/toml.hpp"
#include "keymap.hpp"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <iterator>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
template <>
struct fmt::formatter<toml::node_type> : fmt::ostream_formatter {};
template <>
struct fmt::formatter<toml::parse_error> : fmt::ostream_formatter {};
template <>
struct fmt::formatter<toml::source_region> : fmt::ostream_formatter {};
QUILL_LOGGABLE_DEFERRED_FORMAT(toml::node_type);
QUILL_LOGGABLE_DEFERRED_FORMAT(toml::parse_error);
QUILL_LOGGABLE_DEFERRED_FORMAT(toml::source_region);
class MapConfig {
private:
bool found_priority = false;
int32_t last_priority = 0;
std::vector<std::pair<int32_t, KeyMapEntry>> mapConfigEntries;
public:
int32_t default_priority = 1000;
std::vector<std::string> config_path_list;
MapConfig(std::vector<std::string> configPathList) : config_path_list(configPathList) {}
void prefix_path_output(std::string_view config_path) const {
INTERCEPT_LOGE_N_ERR(g_Logger, "[path: {}] ", config_path);
}
void prefix_source_output(toml::source_region source) const {
INTERCEPT_LOGE_N_ERR(g_Logger, "[source: At {}]", source);
}
void move_map_config_entries_to_mapped_entries() {
for (auto& map : mapConfigEntries) {
g_MapEntries.emplace_back(std::move(map.second));
}
}
void load_config() {
assert(!config_path_list.empty() && "Config path list cannot be empty");
for (const auto& config_path : config_path_list) {
toml::parse_result result = toml::parse_file(config_path);
if (!result) {
INTERCEPT_LOGE_N_ERR(g_Logger, "Parsing failed for file: {}\nError: {}", config_path, result.error());
continue;
}
INTERCEPT_LOGD_N_OUT(g_Logger, "Parsed file: {}", config_path);
toml::array* mappings = result["mappings"].as_array();
if (!mappings) {
prefix_path_output(config_path);
INTERCEPT_LOGE_N_ERR(g_Logger, "No 'mappings' array found in file");
continue;
}
int mapping_index = -1;
for (auto& mapping : *mappings) {
++mapping_index;
toml::table* map_table = mapping.as_table();
if (!map_table) {
prefix_path_output(config_path);
INTERCEPT_LOGE_N_ERR(g_Logger, "Mapping[{}] is not a table. Type: {}", mapping_index, mapping.type());
prefix_source_output(mapping.source());
continue;
}
toml::array* from_arr = (*map_table)["from"].as_array();
toml::array* to_arr = (*map_table)["to"].as_array();
if (!from_arr || !to_arr) {
prefix_path_output(config_path);
INTERCEPT_LOGE_N_ERR(g_Logger, "Mapping[{}] must contain both 'from' and 'to' arrays. "
"Incase of disabling the keystrokes, put an empty 'to' array. This helps remove ambiguity if 'to' is actually disabled.", mapping_index);
prefix_source_output(map_table->source());
continue;
}
if (from_arr->empty()) {
prefix_path_output(config_path);
INTERCEPT_LOGE_N_ERR(g_Logger, "Mapping[{}] 'from' array is empty.", mapping_index);
prefix_source_output(from_arr->source());
continue;
}
if (!(from_arr->size() <= 5 && from_arr->is_homogeneous<int64_t>()) ||
!(to_arr->size() <= 5 && to_arr->is_homogeneous<int64_t>()))
{
prefix_path_output(config_path);
INTERCEPT_LOGE_N_ERR(g_Logger, "Invalid 'from' or 'to' arrays in mapping[{}]. "
"Expected max 5 keys and all integer values...", mapping_index);
prefix_source_output(from_arr->source());
prefix_source_output(to_arr->source());
continue;
}
int32_t priority = default_priority;
if ((*map_table)["priority"].is_integer()) {
found_priority = true;
last_priority = priority = (*map_table)["priority"].as_integer()->get();
}
else if (found_priority) {
last_priority = priority = last_priority + 1;
}
std::vector<int> from_entries = toml_array_to_vector<int>(from_arr);
std::vector<int> to_entries = toml_array_to_vector<int>(to_arr);
KeyMapEntry entry(from_entries, to_entries);
auto entry_loaded_template = fmt::format("{}. {} -> {}", mapping_index, entry.raw_from_keys(), entry.raw_to_keys());
INTERCEPT_LOGD_N_OUT(g_Logger, "{}", entry_loaded_template);
mapConfigEntries.emplace_back(std::move(std::pair(priority, entry)));
}
}
// Sort based on priority
std::sort(mapConfigEntries.begin(), mapConfigEntries.end(),
[](std::pair<int32_t, KeyMapEntry>& e1,
std::pair<int32_t, KeyMapEntry>& e2) {
return e1.first < e2.first;
});
deduplicate();
move_map_config_entries_to_mapped_entries();
}
void deduplicate() {
// Deduplicate if 'from' keys are same
// Keep the only one with highest priority
// Iterate from the end, check for similarities, erase and shift
// Also, sorting is preserved
for (auto it = mapConfigEntries.rbegin(),
rend_it = mapConfigEntries.rend(); it != rend_it; ++it) {
auto it2 = std::next(it);
while (it2 != rend_it && KeyMapEntry::are_from_keys_same(it->second, it2->second)) {
it2 = std::next(it2);
it = std::next(it);
it = decltype(it)(mapConfigEntries.erase(it.base()));
// 'it' still points to where it was pointing before and valid
}
if (it2 == rend_it) break;
}
}
template<typename T>
std::vector<T> toml_array_to_vector(const toml::array* arr) {
std::vector<T> vec;
if (!arr) return vec;
for (const auto& node : *arr) {
if (auto val = node.value<T>())
vec.push_back(*val);
}
return vec;
}
};