-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConsole.cpp
More file actions
421 lines (386 loc) · 10 KB
/
Console.cpp
File metadata and controls
421 lines (386 loc) · 10 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
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
// @Begin License@
// This file is part of Coldest.
//
// Coldest is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Coldest is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Coldest. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2008-2012 Ben Nemec
// @End License@
#include "Console.h"
#include "globals.h"
#include "serverdefs.h"
// Ditto the comment in the Log class. At this time Console is created once and destroyed only at exit,
// so we're not properly handling copy/destruction for the mutex.
Console::Console() : consoleout(NULL)
{
mutex = SDL_CreateMutex();
}
int Console::GetInt(const string& key)
{
int retval = 0;
Lock();
if (values.find(key) != values.end())
{
istringstream get(values[key]);
get >> retval;
}
else
logout << "Console Warning: Key not found " << key << endl;
Unlock();
return retval;
}
float Console::GetFloat(const string& key)
{
float retval = 0.f;
Lock();
if (values.find(key) != values.end())
{
istringstream get(values[key]);
get >> retval;
}
else
logout << "Console Warning: Key not found " << key << endl;
Unlock();
return retval;
}
string Console::GetString(const string& key)
{
string retval = "";
Lock();
if (values.find(key) != values.end())
{
retval = values[key];
}
else
logout << "Console Warning: Key not found " << key << endl;
Unlock();
return values[key];
}
bool Console::GetBool(const string& key)
{
Lock();
if (values.find(key) != values.end())
{
string temp;
istringstream get(values[key]);
get >> temp;
if (temp == "0" || temp == "false")
{
Unlock();
return false;
}
Unlock();
return true;
}
logout << "Console Warning: Key not found " << key << endl;
Unlock();
return false;
}
// Some very stupid things were done implementing this function - particularly reimplementing parts of stringstream
// I've removed some of it, but I'm leaving parts too because they work well enough for the time being
void Console::Parse(const string& line, bool echo)
{
string simple = SimplifyWhitespace(line);
if (echo)
WriteToConsole(simple);
int ntokens = NumTokens(simple);
if (ntokens == 0 ||
Token(simple, 0).substr(0, 1) == "#")
return;
if (Token(simple, 0) == "set" && NumTokens(simple) >= 3)
{
Lock();
values[Token(simple, 1)] = Token(simple, 2, true);
Unlock();
Action(Token(simple, 1) + " action");
}
if (Token(simple, 0) == "setsave" && NumTokens(simple) >= 3)
{
Lock();
saveval.insert(Token(simple, 1));
values[Token(simple, 1)] = Token(simple, 2, true);
Unlock();
Action(Token(simple, 1) + " action");
}
#ifndef DEDICATED
if (Token(simple, 0) == "send")
{
string remote;
for (size_t i = 1; i < NumTokens(simple); ++i)
{
remote += Token(simple, i);
if (i != NumTokens(simple) - 1)
remote += " ";
}
netcode->SendCommand(remote);
}
if (Token(simple, 0) == "auth")
{
netcode->SendPassword(Token(simple, 1));
}
if (Token(simple, 0) == "include")
{
// This is now a no-op because it doesn't work well with the current console
}
#endif
else Action(Token(simple, 0));
}
// Just counts spaces, make sure to run through simplifywhitespace first
// This should have been done with a stringstream
size_t Console::NumTokens(const string& str)
{
int count = 0;
for (size_t i = 0; i < str.size(); i++)
{
if (str.substr(i, 1) == " ")
count++;
}
return count + 1;
}
// To get the token and the rest of the line, set line to true.
string Console::Token(const string& str, int tokennum, bool line)
{
istringstream gettoken(str);
string currtoken = "";
ssize_t stop = tokennum;
if (line)
--stop;
for (ssize_t i = 0; i <= stop; ++i)
{
gettoken >> currtoken;
}
if (line)
{
if (tokennum != 0)
gettoken.ignore(); // There should be a leftover whitespace character
getline(gettoken, currtoken);
}
return currtoken;
}
// Note: Does not strip all trailing spaces.
// This shouldn't be necessary, but due to the improper way NumTokens was written it is
string Console::SimplifyWhitespace(const string& str)
{
string newstr = "";
bool inwhitespace = true;
for (size_t i = 0; i < str.size(); i++)
{
if (str.substr(i, 1) == " ")
{
if (!inwhitespace)
newstr += str[i];
inwhitespace = true;
}
else
{
inwhitespace = false;
newstr += str[i];
}
}
return newstr;
}
#ifndef DEDICATED
void Console::InitWidget(TextArea& co)
{
Lock();
consoleout = &co;
deque<string>::iterator i = consolebuffer.begin();
while (i != consolebuffer.end())
{
consoleout->Append(*i);
i = consolebuffer.erase(i);
}
Unlock();
}
#endif
/* The console needs to be written to in the process of loading autoexec.cfg, which means
we can't have instantiated the console GUI yet, so if that is the case then we write to a
temporary buffer that can later be loaded into the GUI portion.
*/
void Console::WriteToConsole(const string& line)
{
Lock();
logout << line << endl;
#ifndef DEDICATED
if (consoleout)
{
consoleout->Append(line + "\n");
Unlock();
return;
}
#endif
consolebuffer.push_back(line + "\n");
Unlock();
}
void Console::SaveToFile(const string& filename, const bool forcesave)
{
Lock();
ifstream read(filename.c_str());
string buffer;
deque<string> lines;
set<string> saved;
while(getline(read, buffer))
{
string simple = SimplifyWhitespace(buffer);
if (Token(simple, 0) == "set" || Token(simple, 0) == "setsave")
{
if (saveval.find(Token(simple, 1)) != saveval.end())
{
string newcommand = Token(simple, 0);
newcommand += " " + Token(simple, 1) + " " + values[Token(simple, 1)] + '\n';
lines.push_back(newcommand);
saved.insert(Token(simple, 1));
}
else lines.push_back(simple + '\n');
}
else
lines.push_back(simple + '\n');
}
read.close();
if (forcesave)
{
for (map<string, string>::iterator i = values.begin(); i != values.end(); ++i)
{
saveval.insert(i->first);
}
}
ofstream save(filename.c_str());
while (lines.size())
{
save << lines.front();
lines.pop_front();
}
set<string> remaining;
set_difference(saveval.begin(), saveval.end(), saved.begin(), saved.end(), inserter(remaining, remaining.end()));
for (set<string>::iterator i = remaining.begin(); i != remaining.end(); ++i)
{
save << "set " << *i << " " << values[*i] << endl;
}
if (forcesave) // Then we're writing a new config file
{
save << "# restartgl must be included to initialize OpenGL\n";
save << "restartgl\n";
}
save.close();
Unlock();
}
void Console::Action(const string& action)
{
if (action == "help")
{
WriteToConsole(string("set <variable name> <value>"));
WriteToConsole(string("help"));
}
else if (action == "map action")
{
if (server)
{
servermapname = GetString("map");
consoleloadmap = 1;
}
}
else if (action == "connect")
{
#ifndef DEDICATED
netcode->Connect();
#endif
}
else if (action == "restartgl")
{
SetupSDL();
SetupOpenGL();
}
else if (action == "reloadres")
{
LoadMap(GetString("map"));
}
else if (action == "laghax action")
{
Packet::laghax = GetInt("laghax");
}
else if (action == "shadowres action")
{
int value = GetInt("shadowres");
if (value >= 8 && value <= 8192)
{
int shadowres = value;
#ifndef DEDICATED
#ifndef DEBUGSMT
shadowmapfbo = FBO(shadowres, shadowres, true, &resman.texhand);
#else
shadowmapfbo = FBO(shadowres, shadowres, false, &resman.texhand);
#endif
#endif
}
else WriteToConsole(string("Invalid value"));
}
else if (action == "reflectionres action")
{
int value = GetInt("reflectionres");
if (value >= 8 && value <= 8192)
{
int reflectionres = value;
#ifndef DEDICATED
reflectionfbo = FBO(reflectionres, reflectionres, false, &resman.texhand);
#endif
}
else WriteToConsole(string("Invalid value"));
}
else if (action == "cloudres action")
{
int value = GetInt("cloudres");
if (value >= 8 && value <= 8192)
{
int cloudres = value;
#ifndef DEDICATED
cloudfbo = FBO(cloudres, cloudres, false, &resman.texhand);
#endif
}
else WriteToConsole(string("Invalid value"));
}
else if (action == "name action")
{
// If SDL wasn't initialized yet, we won't have created any of these objects yet so we can't use them, but that's okay
// because they'll be properly initialized later.
if (SDL_WasInit(0))
{
if (player.size())
player[0].name = GetString("name");
}
}
else if (action == "af action")
{
#ifndef DEDICATED
resman.texhand.af = GetFloat("af");
#endif
}
else if (action == "shadows action" || action == "softshadows action" || action == "reloadshaders")
{
InitShaders();
}
else if (action == "record action")
{
if (netcode && netcode->Connected())
recorder->SetActive(GetBool("record"));
}
else if (action == "kill")
{
#ifndef DEDICATED
netcode->SendKill();
#endif
}
else if (action == "exit" || action == "quit")
{
running = false;
}
}