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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
/*
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 <fstream>
#include <sstream>
#include <list>
#include "auxiliary/functions.h"
#include "sys/sys.h"
#include "filesystem/filesystem.h"
#include "core/application.h"
#include "core/commandbuffer.h"
#include "core/gameconnection.h"
#include "core/func.h"
#include "core/info.h"
#include "core/cvar.h"
#include "core/loader.h"
#include "core/zone.h"
namespace core
{
std::stringstream CommandBuffer::cmdbuf(std::stringstream::in | std::stringstream::out);
void func_print(std::string const &args)
{
con_print << args << "\n";
}
void func_print_file(std::string const &args)
{
std::istringstream argstream(args);
std::string filename;
if (!(argstream >> filename))
return;
CommandBuffer::print_file(filename);
}
void func_list_info(std::string const &args)
{
Info *info = Info::find(args);
if (info)
info->print();
else
Info::list();
}
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_list_zone(std::string const &args)
{
Zone *zone = Zone::search(args);
if (zone) {
zone->print();
} else {
Zone::list();
}
}
void func_list_model(std::string const &args)
{
model::Model::list();
}
void func_list_module(std::string const &args)
{
Loader::list();
}
void func_set(std::string const &args)
{
std::istringstream argstream(args);
std::string varname;
if (!(argstream >> varname)) {
con_print << "Variable name expected!" << std::endl;
return;
}
aux::to_lowercase(varname);
std::string value;
if (!(argstream >> value)) {
Cvar *cvar = Cvar::find(varname);
if (cvar) {
con_print << " " << varname << " " << cvar->str() << " ^N" << cvar->info() << "\n";
}
return;
}
char c;
while (argstream.get(c))
value += c;
Cvar *cvar = Cvar::set(varname.c_str(), value.c_str(), Cvar::Archive);
if (cvar->flags() && Cvar::Info) {
localplayer()->set_dirty();
}
con_debug << " " << cvar->name() << " " << cvar->str() << "\n";
}
void func_toggle(std::string const &args)
{
std::istringstream argstream(args);
std::string varname;
if (!(argstream >> varname)) {
con_print << "Variable name expected!" << std::endl;
return;
}
aux::to_lowercase(varname);
Cvar *cvar = Cvar::find(varname);
if (!cvar) {
con_print << "Unknown variable '" << varname << "'\n";
return;
}
float valueone = 1.0f;
if (!(argstream >> valueone)) {
valueone = 1.0f;
}
if (cvar->value()) {
(*cvar) = 0.0f;
} else {
(*cvar) = valueone;
}
if (cvar->flags() && Cvar::Info) {
localplayer()->set_dirty();
}
con_debug << " " << cvar->name() << " " << cvar->str() << "\n";
}
void func_exec(std::string const &args)
{
std::istringstream argstream(args);
std::string filename;
if (!(argstream >> filename))
return;
CommandBuffer::exec_file(filename);
}
void func_remote(std::string const &args)
{
if (connection()) {
if ((args[0] == '\\') || (args[0] == '/')) {
connection()->forward(args.substr(1, args.size()-1));
} else {
connection()->forward(args);
}
} else {
cmd() << args;
CommandBuffer::exec();
}
}
void CommandBuffer::init()
{
//con_debug << "Initializing command buffer...\n";
Func *func = 0;
func = Func::add("list_ent", (FuncPtr)func_list_ent);
func->set_info("list entities");
func = Func::add("list_func", (FuncPtr)func_list_func);
func->set_info("list functions");
func = Func::add("list_info", (FuncPtr)func_list_info);
func->set_info("list info records");
func = Func::add("list_var", (FuncPtr)func_list_var);
func->set_info("list variables");
func = Func::add("list_zone", (FuncPtr)func_list_zone);
func->set_info("list zones");
Func::add("list_model", (FuncPtr) func_list_model);
func->set_info("list models");
Func::add("list_module", (FuncPtr) func_list_module);
func->set_info("list game modules");
func = Func::add("set", (FuncPtr)func_set);
func->set_info("[variable] [str] set variable value");
func = Func::add("toggle", (FuncPtr)func_toggle);
func->set_info("[variable] toggle a variable");
func = Func::add("print", func_print);
func->set_info("[str] print a message on the console");
func = Func::add("print_file", func_print_file);
func->set_info("[filename] print messages from file");
func = Func::add("exec", (FuncPtr)func_exec);
func->set_info("[filename] execute commands from file");
func = Func::add("remote", (FuncPtr)func_remote);
func->set_info("[command] send a command to the game server");
}
void CommandBuffer::shutdown()
{
//con_debug << "Shutting down command buffer...\n";
Func::remove("set");
Func::remove("toggle");
Func::remove("list_var");
Func::remove("list_func");
Func::remove("list_info");
Func::remove("list_ent");
Func::remove("list_model");
Func::remove("list_module");
Func::remove("list_zone");
Func::remove("print");
Func::remove("print_file");
Func::remove("exec");
}
void CommandBuffer::exec(std::string const &cmdline)
{
if (!cmdline.size())
return;
std::istringstream cmdstream(cmdline);
std::string command;
if (!(cmdstream >> command))
return;
aux::to_lowercase(command);
//con_debug << "Executing '" << cmdline << "'\n";
if ((command[0] == '\\') || (command[0] == '/')) {
command.erase(0, 1);
}
// is it a function
Func *f = Func::find(command);
if (f) {
// console can not execute game functions, and neither should rcon
if ((f->flags() & Func::Game) && (Cvar::sv_dedicated->value() == 0)) {
if (application()->connected()) {
if ((f->flags() & Func::Target)) {
// target function
unsigned int id = 0;
if ((cmdstream >> id)) {
//con_debug << "target function " << command << " " << id << std::endl;
Entity *entity = Entity::find(id);
if (entity)
f->exec(game()->localplayer(), entity);
}
} else {
// game function
std::string args;
char c;
if (cmdstream >> args) {
while (cmdstream.get(c))
args += c;
}
f->exec(game()->localplayer(), args);
}
}
} else {
// regular function
std::string args;
char c;
if (cmdstream >> args) {
while (cmdstream.get(c))
args += c;
}
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()->set_dirty();
}
}
con_print << " " << command << " " << cvar->str() << " ^N" << cvar->info() << "\n";
return;
}
// this gets forwarded to the server
if (connection()) {
if ((cmdline[0] == '\\') || (cmdline[0] == '/')) {
connection()->forward(cmdline.substr(1, cmdline.size()-1));
} else {
connection()->forward(cmdline);
}
} else {
con_print << "Unknown command '" << command << "^N'\n";
}
}
void CommandBuffer::exec()
{
if (cmdbuf.eof())
return;
std::string line;
char c;
bool quote = false;
while ( core::cmd().read(&c , 1) && c) {
if ( c =='\n') {
exec(line);
line.clear();
} else if (c == '"') {
quote = !quote;
} else if ((c == ';') && !quote) {
exec(line);
line.clear();
} else {
line +=c;
}
}
if (line.size())
exec(line);
cmdbuf.clear();
}
void CommandBuffer::complete(std::string &input, size_t &pos)
{
std::list<std::string> match;
size_t start = 0;
if (input.c_str()[0] == '/' || input.c_str()[0] == '\\')
start = 1;
std::string partial = input.substr(start, pos);
if (!partial.size())
return;
aux::to_lowercase(partial);
// search function registry for matches
for (Func::Registry::iterator 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
for (Cvar::Registry::iterator 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(start, pos, maxmatch);
pos = maxmatch.size();
}
}
void CommandBuffer::exec_file(std::string const & filename)
{
filesystem::File *f = filesystem::open(filename.c_str());
if (!f) {
con_warn << "Could not open " << filename << std::endl;
return;
}
std::string fn = f->path();
fn.append(f->name());
filesystem::close(f);
std::ifstream ifs;
ifs.open(fn.c_str());
if (!ifs.is_open()) {
con_warn << "Could not stream " << fn << "!\n";
return;
}
con_print << "Executing " << fn.c_str() << std::endl;
char line[MAXCMDSIZE];
while (ifs.getline(line, MAXCMDSIZE-1)) {
if (line[0] && line[0] != '#' && line[0] != ';')
exec(std::string(line));
}
ifs.close();
}
void CommandBuffer::print_file(std::string const & filename)
{
filesystem::File *f = filesystem::open(filename.c_str());
if (!f) {
con_warn << "Could not open " << filename << std::endl;
return;
}
std::string fn = f->path();
fn.append(f->name());
filesystem::close(f);
std::ifstream ifs;
ifs.open(fn.c_str());
if (!ifs.is_open()) {
con_warn << "Could not stream " << fn << "!\n";
return;
}
char line[MAXCMDSIZE];
while (ifs.getline(line, MAXCMDSIZE-1)) {
con_print << line << "\n";
}
ifs.close();
}
}
|