-
Notifications
You must be signed in to change notification settings - Fork 3
/
better_console.patch
252 lines (239 loc) · 11.6 KB
/
better_console.patch
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
diff --git src/engine/console.cpp src/engine/console.cpp
index 3664668..ffe61ef 100644
--- src/engine/console.cpp
+++ src/engine/console.cpp
@@ -9,7 +9,7 @@ reversequeue<cline, MAXCONLINES> conlines;
int commandmillis = -1;
string commandbuf;
char *commandaction = NULL, *commandprompt = NULL;
-enum { CF_COMPLETE = 1<<0, CF_EXECUTE = 1<<1 };
+enum { CF_COMPLETE = 1<<0, CF_EXECUTE = 1<<1, CF_GAMECOMPLETE = 1<<2 };
int commandflags = 0, commandpos = -1;
VARFP(maxcon, 10, 200, MAXCONLINES, { while(conlines.length() > maxcon) delete[] conlines.pop().line; });
@@ -258,7 +258,7 @@ ICOMMAND(searchbinds, "s", (char *action), searchbinds(action, keym::ACTION_
ICOMMAND(searchspecbinds, "s", (char *action), searchbinds(action, keym::ACTION_SPECTATOR));
ICOMMAND(searcheditbinds, "s", (char *action), searchbinds(action, keym::ACTION_EDITING));
-void inputcommand(char *init, char *action = NULL, char *prompt = NULL, char *flags = NULL) // turns input to the command line on or off
+void inputcommand(char *init, char *action = NULL, char *prompt = NULL, const char *flags = NULL) // turns input to the command line on or off
{
commandmillis = init ? totalmillis : -1;
textinput(commandmillis >= 0, TI_CONSOLE);
@@ -273,13 +273,14 @@ void inputcommand(char *init, char *action = NULL, char *prompt = NULL, char *fl
if(flags) while(*flags) switch(*flags++)
{
case 'c': commandflags |= CF_COMPLETE; break;
+ case 'g': commandflags |= CF_GAMECOMPLETE; break;
case 'x': commandflags |= CF_EXECUTE; break;
case 's': commandflags |= CF_COMPLETE|CF_EXECUTE; break;
}
- else if(init) commandflags |= CF_COMPLETE|CF_EXECUTE;
+ if(init && init[0] == '/') commandflags |= CF_COMPLETE|CF_EXECUTE;
}
-ICOMMAND(saycommand, "C", (char *init), inputcommand(init));
+ICOMMAND(saycommand, "C", (char *init), inputcommand(init, NULL, NULL, "g"));
COMMAND(inputcommand, "ssss");
void pasteconsole()
@@ -336,7 +337,7 @@ struct hline
void run()
{
- if(flags&CF_EXECUTE && buf[0]=='/') execute(buf+1);
+ if(flags&CF_EXECUTE || buf[0]=='/') execute(buf[0]=='/' ? buf+1 : buf);
else if(action)
{
alias("commandbuf", buf);
@@ -504,9 +505,15 @@ bool consolekey(int code, bool isdown)
break;
case SDLK_TAB:
- if(commandflags&CF_COMPLETE)
+ if(commandflags&CF_COMPLETE || (commandflags&CF_GAMECOMPLETE && commandbuf[0]=='/'))
+ {
+ bool prefixslash = commandbuf[0]=='/' || (!commandaction && !(commandflags&CF_GAMECOMPLETE));
+ complete(commandbuf, sizeof(commandbuf), prefixslash ? "/" : NULL);
+ if(commandpos>=0 && commandpos>=(int)strlen(commandbuf)) commandpos = -1;
+ }
+ else if(commandflags&CF_GAMECOMPLETE)
{
- complete(commandbuf, sizeof(commandbuf), commandflags&CF_EXECUTE ? "/" : NULL);
+ game::complete(commandbuf, commandpos, sizeof(commandbuf));
if(commandpos>=0 && commandpos>=(int)strlen(commandbuf)) commandpos = -1;
}
break;
@@ -536,12 +541,14 @@ bool consolekey(int code, bool isdown)
}
histpos = history.length();
inputcommand(NULL);
+ resetcomplete();
if(h) h->run();
}
else if(code==SDLK_ESCAPE)
{
histpos = history.length();
inputcommand(NULL);
+ resetcomplete();
}
}
@@ -668,7 +677,7 @@ static hashtable<char *, filesval *> completions;
int completesize = 0;
char *lastcomplete = NULL;
-void resetcomplete() { completesize = 0; }
+void resetcomplete() { completesize = 0; game::resetcomplete(); }
void addcomplete(char *command, int type, char *dir, char *ext)
{
diff --git src/fpsgame/client.cpp src/fpsgame/client.cpp
index 1e3163e..af68b0c 100644
--- src/fpsgame/client.cpp
+++ src/fpsgame/client.cpp
@@ -1492,6 +1492,8 @@ namespace game
broadcastp1xbratenversion();
}
copystring(d->name, text, MAXNAMELEN+1);
+ filternonalphanum(text, text, MAXNAMELEN);
+ copystring(d->alphanumname, text, MAXNAMELEN);
getstring(text, p);
filtertext(d->team, text, false, false, MAXTEAMLEN);
d->playermodel = getint(p);
@@ -1508,6 +1510,8 @@ namespace game
{
if(!isignored(d->clientnum)) conoutf(CON_INFO + (d->state==CS_SPECTATOR ? CON_NONZEN : 0), "%s is now known as %s", colorname(d), colorname(d, text));
copystring(d->name, text, MAXNAMELEN+1);
+ filternonalphanum(text, text, MAXNAMELEN);
+ copystring(d->alphanumname, text, MAXNAMELEN);
}
}
break;
diff --git src/fpsgame/game.h src/fpsgame/game.h
index 843dc15..49ad01b 100644
--- src/fpsgame/game.h
+++ src/fpsgame/game.h
@@ -563,6 +563,7 @@ struct fpsent : dynent, fpsstate
int smoothmillis;
string name, team, info;
+ string alphanumname;
int playermodel;
ai::aiinfo *ai;
int ownernum, lastnode;
@@ -572,7 +573,7 @@ struct fpsent : dynent, fpsstate
fpsent() : weight(100), clientnum(-1), privilege(PRIV_NONE), lastupdate(0), plag(0), ping(0), lifesequence(0), respawned(-1), suicided(-1), lastpain(0), attacksound(-1), attackchan(-1), idlesound(-1), idlechan(-1), frags(0), flags(0), deaths(0), totaldamage(0), totalshots(0), suicides(0), edit(NULL), smoothmillis(-1), playermodel(-1), ai(NULL), ownernum(-1), p1xbratenversion(0, 0, 0), muzzle(-1, -1, -1)
{
- name[0] = team[0] = info[0] = 0;
+ name[0] = team[0] = info[0] = alphanumname[0] = 0;
respawn();
}
~fpsent()
@@ -780,6 +781,7 @@ namespace game
extern void sendposition(fpsent *d, bool reliable = false);
extern void broadcastp1xbratenversion();
extern bool setp1xbratenversion(fpsent *d, int version);
+ extern void filternonalphanum(char *dst, const char *src, size_t len);
// monster
struct monster;
diff --git src/p1xbraten/namecomplete.cpp src/p1xbraten/namecomplete.cpp
new file mode 100644
index 0000000..90dc855
--- /dev/null
+++ src/p1xbraten/namecomplete.cpp
@@ -0,0 +1,92 @@
+#include "game.h"
+
+namespace game
+{
+ char *completeword = NULL; // substring of commandbuf/s to be completed
+ const char *lastcompletealphanum = NULL; // points to alphanum version of last suggested ci->name
+ size_t lastcompletelen = 0; // strlen of last suggested ci->name
+ extern int playersearch; // same threshold as parseplayer()
+
+ void complete(char *s, int cursor, int maxlen) // completes client names
+ {
+ if(!completeword && cursor>-1 && s[cursor]!=' ') return; // only start suggesting when followed by space
+
+ static string alphanumword = ""; // alphanum version of completeword
+ static size_t comparelen = 0; // strlen of alphanumword
+ if(!completeword) // true on first Tab press
+ {
+ char prevchar;
+ if(cursor>=0) { prevchar = s[cursor]; s[cursor] = 0; } // temporarily shorten s to end at cursor
+ completeword = strrchr(s, ' ');
+ if(!completeword) completeword = s; // no space in front of cursor -> use whole commandbuf
+ else completeword++; // move to first char, behind the space we found
+ lastcompletelen = strlen(completeword); // we will replace this many chars with our first suggestion
+ filternonalphanum(alphanumword, completeword, maxlen); // used for matching
+ comparelen = strlen(alphanumword); // used for matching
+ if(cursor>=0) { s[cursor] = prevchar; } // restore s
+ }
+
+ const char *nextcomplete = NULL, *nextcompletealphanum = NULL; // will contain ci->name / ci->alphanumname of next suggestion
+ static bool skipprefixcheck = false; // to remember wether we are completing by substring yet
+ if(!skipprefixcheck) loopv(clients) // try matching prefix (ignoring case)
+ {
+ fpsent *ci = clients[i];
+ if(!ci) continue;
+ if(cubecaseequal(ci->alphanumname, alphanumword, comparelen) && // match prefix
+ (!lastcompletealphanum || cubecasecmp(ci->alphanumname, lastcompletealphanum) > 0) && // ensure it (alphabetically) comes after last suggestion
+ (!nextcompletealphanum || cubecasecmp(ci->alphanumname, nextcompletealphanum) < 0) // only pick as next suggestion when it comes before current pick
+ )
+ {
+ nextcompletealphanum = ci->alphanumname;
+ nextcomplete = ci->name;
+ }
+ }
+ if(!skipprefixcheck) lastcompletealphanum = NULL;
+ if(!nextcomplete && (int)comparelen>=playersearch) loopv(clients) // only if prefix matching didn't produce a new suggestion, and enough chars given: try matching substring (ignoring case)
+ {
+ fpsent *ci = clients[i];
+ if(!ci) continue;
+ if(
+ cubecasefind(ci->alphanumname, alphanumword) && // match substring
+ (!lastcompletealphanum || cubecasecmp(ci->alphanumname, lastcompletealphanum) > 0) && // ensure it (alphabetically) comes after last suggestion
+ (!nextcompletealphanum || cubecasecmp(ci->alphanumname, nextcompletealphanum) < 0) // only pick as next suggestion when it comes before current pick
+ )
+ {
+ nextcompletealphanum = ci->alphanumname;
+ nextcomplete = ci->name;
+ skipprefixcheck = true; // next suggestion should also come from substring (not prefix) matching
+ }
+ }
+ lastcompletealphanum = NULL;
+ if(!nextcomplete) skipprefixcheck = false; // nothing found -> start prefix matching again
+ if(nextcomplete)
+ {
+ size_t nextcompletelen = strlen(nextcomplete);
+ size_t suffixlen = strlen(completeword+lastcompletelen); // length of text after completeword
+ size_t prefixlen = completeword-s;
+ if(suffixlen>0) memmove(completeword+nextcompletelen, completeword+lastcompletelen, min(suffixlen, maxlen-prefixlen)); // make sure we don't overwrite text after completeword
+ memcpy(completeword, nextcomplete, min(nextcompletelen, maxlen-prefixlen)); // copy suggestion into commandbuf
+ s[prefixlen+nextcompletelen+suffixlen] = 0;
+ lastcompletealphanum = nextcompletealphanum;
+ lastcompletelen = nextcompletelen;
+ }
+ }
+
+ void resetcomplete() { completeword = NULL; lastcompletealphanum = NULL; }
+
+ void filternonalphanum(char *dst, const char *src, size_t len)
+ {
+ for(int c = uchar(*src); c; c = uchar(*++src))
+ {
+ if(c == '\f')
+ {
+ if(!*++src) break;
+ continue;
+ }
+ if(!iscubealnum(c)) continue;
+ *dst++ = c;
+ if(!--len) break;
+ }
+ *dst = '\0';
+ }
+}
diff --git src/shared/igame.h src/shared/igame.h
index 1c532c1..37a3537 100644
--- src/shared/igame.h
+++ src/shared/igame.h
@@ -50,6 +50,8 @@ namespace game
extern const char *savedservers();
extern void loadconfigs();
extern int writeembeddedcfgs();
+ extern void complete(char *s, int cursor, int maxlen);
+ extern void resetcomplete();
extern void updateworld();
extern void initclient();