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
|
/*
core/commandbuffer.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#include <string>
#include <sstream>
#include <list>
#include "sys/sys.h"
#include "core/application.h"
#include "core/commandbuffer.h"
#include "core/gameconnection.h"
#include "core/func.h"
#include "core/cvar.h"
namespace core
{
void func_list_func(std::string const &args)
{
Func::list();
}
void func_list_var(std::string const &args)
{
Cvar::list();
}
void func_list_ent(std::string const &args)
{
Entity::list();
}
void func_set(std::string const &args)
{
std::istringstream argstream(args);
std::string varname;
if (!(argstream >> varname))
return;
// we're setting a new value
std::string value;
if (!(argstream >> value))
return;
char c;
while (argstream.get(c))
value += c;
Cvar *cvar = Cvar::set(varname.c_str(), value.c_str(), Cvar::Archive);
con_print << cvar->name() << " " << cvar->str() << "\n";
if (cvar->flags() && Cvar::Info) {
localplayer()->player_dirty = true;
}
return;
}
std::stringstream CommandBuffer::cmdbuf(std::stringstream::in | std::stringstream::out);
void CommandBuffer::init()
{
//con_debug << "Initializing command buffer...\n";
Func::add("list_ent", (FuncPtr)func_list_ent);
Func::add("list_func", (FuncPtr)func_list_func);
Func::add("list_var", (FuncPtr)func_list_var);
Func::add("set", (FuncPtr)func_set);
}
void CommandBuffer::shutdown()
{
//con_debug << "Shutting down command buffer...\n";
Func::remove("set");
Func::remove("list_var");
Func::remove("list_func");
Func::remove("list_ent");
}
void CommandBuffer::exec(std::string const &cmdline)
{
if (!cmdline.size())
return;
std::istringstream cmdstream(cmdline);
std::string command;
if (!(cmdstream >> command))
return;
//con_debug << "Executing '" << cmdline << "'\n";
// is it a function
Func *f = Func::find(command);
if (f) {
std::string args;
char c;
if (cmdstream >> args) {
while (cmdstream.get(c))
args += c;
}
if ((f->flags() & Func::Game)) {
if (application()->connected()) {
f->exec(game()->localplayer(), args);
}
} else {
f->exec(args);
}
return;
}
// is it a cvar
Cvar *cvar = Cvar::find(command);
if (cvar) {
// cvar exists
std::string value;
if (((cvar->flags() & Cvar::ReadOnly) == 0) && (cmdstream >> value)) {
// we're setting a new value
char c;
while (cmdstream.get(c))
value += c;
(*cvar) = value;
if (cvar->flags() && Cvar::Info) {
localplayer()->player_dirty = true;
}
}
con_print << command << " " << cvar->str() << "\n";
return;
}
// TODO this must get forwarded to the server
if (connection())
connection()->forward(cmdline);
else
con_print << "Unknown command '" << command << "'\n";
}
void CommandBuffer::exec()
{
if (cmdbuf.eof())
return;
char line[MAXCMDSIZE];
while (core::cmd().getline(line, MAXCMDSIZE-1)) {
exec(std::string(line));
}
cmdbuf.clear();
}
void CommandBuffer::complete(std::string &input, size_t &pos)
{
std::list<std::string> match;
std::string partial = input.substr(0, pos);
if (!partial.size())
return;
// search function registry for matches
std::map<std::string, Func *>::iterator f;
for (f = Func::registry.begin(); f != Func::registry.end(); f++) {
if (partial == (*f).first.substr(0, partial.size())) {
match.push_back((*f).first);
//con_print << " " << (*f).first << "\n";
}
}
// search cvar registry for matches
std::map<std::string, Cvar *>::iterator c;
for (c = Cvar::registry.begin(); c != Cvar::registry.end(); c++) {
if (partial == (*c).first.substr(0, partial.size())) {
match.push_back((*c).first);
//con_print << " " << (*c).first << "\n";
}
}
if (!match.size())
return;
std::string maxmatch(*match.begin());
if (match.size() > 1) {
std::list<std::string>::iterator l;
for (l = match.begin(); l !=match.end(); l++) {
if (maxmatch.size()) {
size_t i =0;
while ((i < maxmatch.size() && i < (*l).size()) && (maxmatch[i] == (*l)[i])) {
i++;
}
if (i < maxmatch.size())
maxmatch.erase(i);
}
con_print << " " << (*l) << "\n";
}
con_print << match.size() << " matches\n";
}
if (maxmatch.size() > partial.size()) {
if (match.size()==1) maxmatch += ' ';
input.replace(0, pos, maxmatch);
pos = maxmatch.size();
}
}
}
|