forked from rafalh/dashfaction
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautocomplete.cpp
200 lines (179 loc) · 6.6 KB
/
autocomplete.cpp
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
#include "console.h"
#include "../main/main.h"
#include "../misc/player.h"
#include "../misc/vpackfile.h"
#include "../rf/player/player.h"
#include <patch_common/FunHook.h>
#include <cstring>
void console_show_cmd_help(rf::console::Command* cmd)
{
rf::console::run = 0;
rf::console::help = 1;
rf::console::status = 0;
auto handler = reinterpret_cast<void(__thiscall*)(rf::console::Command*)>(cmd->func);
handler(cmd);
}
int console_auto_complete_get_component(int offset, std::string& result)
{
const char *begin = rf::console::cmd_line + offset;
const char *end = nullptr;
const char *next;
if (begin[0] == '"') {
++begin;
end = std::strchr(begin, '"');
next = end ? std::strchr(end, ' ') : nullptr;
}
else
end = next = std::strchr(begin, ' ');
if (!end)
end = rf::console::cmd_line + rf::console::cmd_line_len;
size_t len = end - begin;
result.assign(begin, len);
return next ? next + 1 - rf::console::cmd_line : -1;
}
void console_auto_complete_put_component(int offset, const std::string& component, bool finished)
{
bool quote = component.find(' ') != std::string::npos;
int max_len = std::size(rf::console::cmd_line);
int len;
if (quote) {
len = snprintf(rf::console::cmd_line + offset, max_len - offset, "\"%s\"", component.c_str());
}
else {
len = snprintf(rf::console::cmd_line + offset, max_len - offset, "%s", component.c_str());
}
rf::console::cmd_line_len = offset + len;
if (finished)
rf::console::cmd_line_len += snprintf(rf::console::cmd_line + rf::console::cmd_line_len, max_len - rf::console::cmd_line_len, " ");
}
template<typename T, typename F>
void console_auto_complete_print_suggestions(T& suggestions, F mapping_fun)
{
for (auto& item : suggestions) {
rf::console::print("{}\n", mapping_fun(item));
}
}
void console_auto_complete_update_common_prefix(std::string& common_prefix, const std::string& value, bool& first,
bool case_sensitive = false)
{
if (first) {
first = false;
common_prefix = value;
}
if (common_prefix.size() > value.size())
common_prefix.resize(value.size());
for (size_t i = 0; i < common_prefix.size(); ++i) {
bool match;
if (case_sensitive) {
match = common_prefix[i] == value[i];
}
else {
char l = std::tolower(static_cast<unsigned char>(common_prefix[i]));
char r = std::tolower(static_cast<unsigned char>(value[i]));
match = l == r;
}
if (!match) {
common_prefix.resize(i);
break;
}
}
}
void console_auto_complete_level(int offset)
{
std::string level_name;
console_auto_complete_get_component(offset, level_name);
if (level_name.empty())
return;
bool first = true;
std::string common_prefix;
std::vector<std::string> matches;
vpackfile_find_matching_files(StringMatcher().prefix(level_name).suffix(".rfl"), [&](const char* name) {
auto* ext = std::strrchr(name, '.');
auto name_len = ext ? ext - name : std::strlen(name);
std::string name_without_ext(name, name_len);
matches.push_back(name_without_ext);
console_auto_complete_update_common_prefix(common_prefix, name_without_ext, first);
});
if (matches.size() == 1)
console_auto_complete_put_component(offset, matches[0], true);
else if (!matches.empty()) {
console_auto_complete_print_suggestions(matches, [](std::string& name) { return name.c_str(); });
console_auto_complete_put_component(offset, common_prefix, false);
}
}
void console_auto_complete_player(int offset)
{
std::string player_name;
console_auto_complete_get_component(offset, player_name);
if (player_name.empty())
return;
bool first = true;
std::string common_prefix;
std::vector<rf::Player*> matching_players;
find_player(StringMatcher().prefix(player_name), [&](rf::Player* player) {
matching_players.push_back(player);
console_auto_complete_update_common_prefix(common_prefix, player->name.c_str(), first);
});
if (matching_players.size() == 1)
console_auto_complete_put_component(offset, matching_players[0]->name.c_str(), true);
else if (!matching_players.empty()) {
console_auto_complete_print_suggestions(matching_players, [](rf::Player* player) {
// Print player names
return player->name.c_str();
});
console_auto_complete_put_component(offset, common_prefix, false);
}
}
void console_auto_complete_command(int offset)
{
std::string cmd_name;
int next_offset = console_auto_complete_get_component(offset, cmd_name);
if (cmd_name.empty())
return;
bool first = true;
std::string common_prefix;
std::vector<rf::console::Command*> matching_cmds;
for (int i = 0; i < rf::console::num_commands; ++i) {
rf::console::Command* cmd = g_commands_buffer[i];
if (!strnicmp(cmd->name, cmd_name.c_str(), cmd_name.size()) &&
(next_offset == -1 || !cmd->name[cmd_name.size()])) {
matching_cmds.push_back(cmd);
console_auto_complete_update_common_prefix(common_prefix, cmd->name, first);
}
}
if (next_offset != -1) {
if (!stricmp(cmd_name.c_str(), "level") || !stricmp(cmd_name.c_str(), "map"))
console_auto_complete_level(next_offset);
else if (!stricmp(cmd_name.c_str(), "kick") || !stricmp(cmd_name.c_str(), "ban"))
console_auto_complete_player(next_offset);
else if (!stricmp(cmd_name.c_str(), "rcon") || !stricmp(cmd_name.c_str(), "help"))
console_auto_complete_command(next_offset);
else if (matching_cmds.size() != 1)
return;
else
console_show_cmd_help(matching_cmds[0]);
}
else if (matching_cmds.size() > 1) {
for (auto* cmd : matching_cmds) {
if (cmd->help)
rf::console::print("{} - {}", cmd->name, cmd->help);
else
rf::console::print("{}", cmd->name);
}
console_auto_complete_put_component(offset, common_prefix, false);
}
else if (matching_cmds.size() == 1)
console_auto_complete_put_component(offset, matching_cmds[0]->name, true);
}
FunHook<void()> console_auto_complete_input_hook{
0x0050A620,
[]() {
// autocomplete on offset 0
console_auto_complete_command(0);
},
};
void console_auto_complete_apply_patches()
{
// Better console autocomplete
console_auto_complete_input_hook.install();
}