-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathCvarSystem.cpp
More file actions
786 lines (635 loc) · 27 KB
/
CvarSystem.cpp
File metadata and controls
786 lines (635 loc) · 27 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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/*
===========================================================================
Daemon BSD Source Code
Copyright (c) 2013-2016, Daemon Developers
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Daemon developers nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
===========================================================================
*/
#include "CvarSystem.h"
#include "CommandSystem.h"
#include "qcommon/qcommon.h"
#include "qcommon/cvar.h"
//TODO: thread safety (not possible with the C API that doesn't care at all about this)
int cvar_modifiedFlags;
namespace Cvar {
// A cvar is some info alongside a potential proxy for the cvar
struct cvarRecord_t {
std::string value;
std::string resetValue;
Util::optional<std::string> latchedValue;
int flags;
std::string description;
CvarProxy* proxy;
cvar_t ccvar; // The state of the cvar_t used to emulate the C API
//DO: mutex?
inline bool IsArchived() const {
return (flags & USER_ARCHIVE) && !(flags & TEMPORARY);
}
};
//Functions that emulate the C API
void SetCStyleDescription(cvarRecord_t& cvar) {
if (cvar.proxy) {
return;
}
if (cvar.flags & CVAR_LATCH and cvar.ccvar.latchedString) {
cvar.description = Str::Format("\"%s^*\" - latched value \"%s^*\"", cvar.ccvar.string, cvar.ccvar.latchedString);
} else {
cvar.description = Str::Format("\"%s^*\"", cvar.value);
}
Cmd::ChangeDescription(cvar.ccvar.name, cvar.description);
}
void SetCCvar(cvarRecord_t& cvar) {
cvar_t& var = cvar.ccvar;
bool modified = false;
if (cvar.flags & CVAR_LATCH) {
if (not var.string) {
var.string = CopyString(cvar.value.c_str());
modified = true;
} else {
if (Q_stricmp(var.string, cvar.value.c_str())) {
Log::Notice("The change to %s will take effect after restart.", var.name);
if (var.latchedString) Z_Free(var.latchedString);
var.latchedString = CopyString(cvar.value.c_str());
modified = true;
} else if (var.latchedString) {
Z_Free(var.latchedString);
var.latchedString = nullptr;
}
}
} else {
if (var.string) {
if (Q_stricmp(var.string, cvar.value.c_str())) {
modified = true;
Z_Free(var.string);
var.string = CopyString(cvar.value.c_str());
}
} else {
var.string = CopyString(cvar.value.c_str());
modified = true;
}
}
var.value = atof(var.string);
var.integer = atoi(var.string);
if (modified) {
cvar_modifiedFlags |= var.flags;
}
SetCStyleDescription(cvar);
}
void GetCCvar(const std::string& name, cvarRecord_t& cvar) {
cvar_t& var = cvar.ccvar;
bool modified = false;
if (not var.name) {
var.name = CopyString(name.c_str());
}
if (var.resetString) Z_Free(var.resetString);
var.resetString = CopyString(cvar.resetValue.c_str());
var.flags = cvar.flags;
if (cvar.flags & CVAR_LATCH and var.latchedString) {
if (var.string) Z_Free(var.string);
var.string = var.latchedString;
var.latchedString = nullptr;
modified = true;
} else {
if (var.string) {
if (Q_stricmp(var.string, cvar.value.c_str())) {
modified = true;
Z_Free(var.string);
var.string = CopyString(cvar.value.c_str());
}
} else {
var.string = CopyString(cvar.value.c_str());
modified = true;
}
}
var.value = atof(var.string);
var.integer = atoi(var.string);
if (modified) {
cvar_modifiedFlags |= var.flags;
}
SetCStyleDescription(cvar);
}
using CvarMap = std::unordered_map<std::string, cvarRecord_t*, Str::IHash, Str::IEqual>;
static bool cheatsAllowed = true;
// The order in which static global variables are initialized is undefined and cvar
// can be registered before main. The first time this function is called the cvar map
// is initialized so we are sure it is initialized as soon as we need it.
CvarMap& GetCvarMap() {
static CvarMap cvars;
return cvars;
}
void Shutdown() {
CvarMap &cvars = GetCvarMap();
for (const auto &it: cvars) {
cvarRecord_t *cvar = it.second;
cvar_t &ccvar = cvar->ccvar;
if (ccvar.name)
{
Z_Free(ccvar.name);
}
if (ccvar.resetString)
{
Z_Free(ccvar.resetString);
}
if (ccvar.latchedString)
{
Z_Free(ccvar.latchedString);
}
if (ccvar.string)
{
Z_Free(ccvar.string);
}
delete cvar;
}
cvars.clear();
}
// A command created for each cvar, used for /<cvar>
class CvarCommand : public Cmd::CmdBase {
public:
CvarCommand() : Cmd::CmdBase(Cmd::CVAR) {
}
void Run(const Cmd::Args& args) const override {
CvarMap& cvars = GetCvarMap();
const std::string& name = args.Argv(0);
cvarRecord_t* var = cvars[name];
if (args.Argc() < 2) {
Print("\"%s\" - %s ^*- default: \"%s^*\"", name, var->description, var->resetValue);
} else {
//TODO forward the print part of the environment
SetValue(name, args.Argv(1));
}
}
};
static CvarCommand cvarCommand;
void ChangeCvarDescription(Str::StringRef cvarName, cvarRecord_t* cvar, Str::StringRef description) {
std::string realDescription = Str::Format("\"%s^*\" - %s", cvar->value, description);
Cmd::ChangeDescription(cvarName, "cvar - " + realDescription);
cvar->description = std::move(realDescription);
}
void InternalSetValue(const std::string& cvarName, std::string value, int flags, bool rom, bool warnRom) {
CvarMap& cvars = GetCvarMap();
auto it = cvars.find(cvarName);
//TODO: rom means the cvar should have been created before?
if (it == cvars.end()) {
if (!Cmd::IsValidCvarName(cvarName)) {
Log::Notice("Invalid cvar name '%s'", cvarName.c_str());
return;
}
//The user creates a new cvar through a command.
cvars[cvarName] = new cvarRecord_t{value, value, Util::nullopt, flags | CVAR_USER_CREATED, "user created", nullptr, {}};
Cmd::AddCommand(cvarName, cvarCommand, "cvar - user created");
GetCCvar(cvarName, *cvars[cvarName]);
} else {
cvarRecord_t* cvar = it->second;
if (not (cvar->flags & CVAR_USER_CREATED)) {
if (cvar->flags & CVAR_ROM and not rom) {
Log::Notice("%s is read only.", cvarName.c_str());
return;
}
if (cvar->flags & INIT and not rom) {
Log::Notice("%s can only be set at program initalization.", cvarName);
return;
}
if (rom and warnRom and not (cvar->flags & (CVAR_ROM | INIT))) {
Log::Notice("SetValueForce called on non-ROM cvar '%s'", cvarName.c_str());
}
if (not cheatsAllowed && cvar->flags & CHEAT) {
Log::Notice("%s is cheat-protected.", cvarName.c_str());
return;
}
}
cvar->flags |= flags;
// mark for archival if flagged as archive-on-change
if (cvar->flags & ARCHIVE) {
cvar->flags |= USER_ARCHIVE;
}
if (cvar->proxy) {
//Tell the cvar proxy about the new value
OnValueChangedResult result = cvar->proxy->OnValueChanged(value);
if (result.success) {
if (cvar->flags & INTERNAL_LATCH && value != cvar->value) {
ChangeCvarDescription(cvarName, cvar, Str::Format("%s - latched value \"%s^*\"", result.description, value));
OnValueChangedResult undo = cvar->proxy->OnValueChanged(cvar->value);
if (!undo.success) {
Sys::Error("error testing new value for latched cvar %s", cvarName);
}
Log::Notice("The change to %s will take effect after restart.", cvarName);
cvar->latchedValue = value;
return;
}
cvar->latchedValue = Util::nullopt;
cvar->value = std::move(value);
ChangeCvarDescription(cvarName, cvar, result.description);
} else {
Log::Notice("Value '%s^*' is not valid for cvar %s: %s", value, cvarName, result.description);
return;
}
} else {
cvar->value = std::move(value);
}
SetCCvar(*cvar);
}
}
// Simple proxies for InternalSetValue
void SetValue(const std::string& cvarName, const std::string& value) {
InternalSetValue(cvarName, value, 0, false, true);
}
void SetValueForce(const std::string& cvarName, const std::string& value) {
InternalSetValue(cvarName, value, 0, true, true);
}
std::string GetValue(const std::string& cvarName) {
CvarMap& cvars = GetCvarMap();
std::string result = "";
auto iter = cvars.find(cvarName);
if (iter != cvars.end()) {
result = iter->second->value;
}
return result;
}
bool Register(CvarProxy* proxy, const std::string& name, std::string description, int flags, const std::string& defaultValue) {
CvarMap& cvars = GetCvarMap();
cvarRecord_t* cvar;
auto it = cvars.find(name);
if (it == cvars.end()) {
if (!Cmd::IsValidCvarName(name)) {
Log::Notice("Invalid cvar name '%s'", name.c_str());
return false;
}
//Create the cvar and parse its default value
description = Str::Format("\"%s^*\" - %s", defaultValue, description);
cvar = new cvarRecord_t{defaultValue, defaultValue, Util::nullopt, flags, description, proxy, {}};
cvars[name] = cvar;
Cmd::AddCommand(name, cvarCommand, "cvar - " + description);
} else {
cvar = it->second;
if (proxy && cvar->proxy) {
Log::Warn("Cvar %s cannot be registered twice", name.c_str());
return false;
}
// Register the cvar with the previous user_created value
cvar->flags &= ~CVAR_USER_CREATED;
cvar->flags |= flags;
cvar->proxy = proxy;
if ((flags & ROM) || (!cheatsAllowed && (flags & CHEAT))) {
cvar->value = defaultValue;
}
cvar->resetValue = defaultValue;
cvar->description.clear();
if (proxy) { //TODO replace me with an assert once we do not need to support the C API
OnValueChangedResult result = proxy->OnValueChanged(cvar->value);
if (result.success) {
ChangeCvarDescription(name, cvar, result.description);
} else {
OnValueChangedResult defaultValueResult = proxy->OnValueChanged(defaultValue);
if (defaultValueResult.success) {
ChangeCvarDescription(name, cvar, result.description);
} else {
Log::Notice("Default value '%s' is not correct for cvar '%s': %s",
defaultValue.c_str(), name.c_str(), defaultValueResult.description.c_str());
}
}
}
}
GetCCvar(name, *cvar);
return true;
}
void Unregister(const std::string& cvarName) {
CvarMap& cvars = GetCvarMap();
auto it = cvars.find(cvarName);
if (it != cvars.end()) {
cvarRecord_t* cvar = it->second;
cvar->proxy = nullptr;
cvar->flags |= CVAR_USER_CREATED;
} //TODO else what?
}
Cmd::CompletionResult Complete(Str::StringRef prefix) {
CvarMap& cvars = GetCvarMap();
Cmd::CompletionResult res;
for (const auto& entry : cvars) {
if (Str::IsIPrefix(prefix, entry.first)) {
res.emplace_back(entry.first, entry.second->description);
}
}
Cmd::NamespaceFold(res);
return res;
}
bool AddFlags(const std::string& cvarName, int flags) {
CvarMap& cvars = GetCvarMap();
auto it = cvars.find(cvarName);
if (it != cvars.end()) {
cvarRecord_t* cvar = it->second;
// User error. Possibly coder error too, but unlikely
if ((cvar->flags & TEMPORARY) && (flags & (ARCHIVE | USER_ARCHIVE)))
{
Log::Notice("Cvar '%s' is temporary and will not be archived", cvarName.c_str());
flags &= ~(ARCHIVE | USER_ARCHIVE);
}
cvar->flags |= flags;
//TODO: remove it, overkill ?
//Make sure to trigger the event as if this variable was changed
cvar_modifiedFlags |= flags;
return true; // success
} //TODO else what?
return false; // not found
}
bool GetFlags(const std::string& cvarName, int& flags) {
CvarMap& cvars = GetCvarMap();
auto it = cvars.find(cvarName);
if (it == cvars.end()) {
return false;
}
flags = it->second->flags;
return true;
}
bool ClearFlags(const std::string& cvarName, int flags) {
CvarMap& cvars = GetCvarMap();
auto it = cvars.find(cvarName);
if (it != cvars.end()) {
cvarRecord_t* cvar = it->second;
cvar->flags &= ~flags;
//TODO: remove it, overkill ?
//Make sure to trigger the event as if this variable was changed
cvar_modifiedFlags |= flags;
return true; // success
} //TODO else what?
return false; // not found
}
void SetCheatsAllowed(bool allowed) {
cheatsAllowed = allowed;
if (cheatsAllowed) {
return;
}
CvarMap& cvars = GetCvarMap();
// Reset all the CHEAT cvars to their default value
for (auto& entry : cvars) {
cvarRecord_t* cvar = entry.second;
if (cvar->flags & CHEAT && cvar->value != cvar->resetValue) {
cvar->value = cvar->resetValue;
SetCCvar(*cvar);
if (cvar->proxy) {
OnValueChangedResult result = cvar->proxy->OnValueChanged(cvar->resetValue);
if(result.success) {
ChangeCvarDescription(entry.first, cvar, result.description);
} else {
Log::Notice("Default value '%s' is not correct for cvar '%s': %s",
cvar->resetValue.c_str(), entry.first.c_str(), result.description.c_str());
}
}
}
}
}
// Activates the new value of a latched cvar, like Cvar_Get for an old-style cvar with
// CVAR_LATCH. By adding the latch flag here rather than in the cvar's constructor, we avoid
// 'change will take effect after restart' messages during initialization. Also we avoid
// the programming error of setting the latch flag but never reloading the value.
void Latch(CvarProxy& cvar)
{
CvarMap& map = GetCvarMap();
auto it = map.find(cvar.Name());
if (it == map.end() || !it->second->proxy) {
Sys::Error("cvar %s not registered", cvar.Name());
}
it->second->flags |= INTERNAL_LATCH;
if (it->second->latchedValue) {
cvar_modifiedFlags |= it->second->flags;
it->second->value = std::move(*it->second->latchedValue);
it->second->latchedValue = Util::nullopt;
SetCCvar(*it->second);
OnValueChangedResult result = it->second->proxy->OnValueChanged(it->second->value);
if (result.success) {
ChangeCvarDescription(it->first, it->second, result.description);
} else {
Log::Warn("BUG: failed setting cvar %s to latched value", cvar.Name());
}
}
}
// Used by the C API
cvar_t* FindCCvar(const std::string& cvarName) {
CvarMap& cvars = GetCvarMap();
auto iter = cvars.find(cvarName);
if (iter != cvars.end()) {
return &iter->second->ccvar;
}
return nullptr;
}
std::string GetCvarConfigText() {
CvarMap& cvars = GetCvarMap();
std::ostringstream result;
for (auto& entry : cvars) {
cvarRecord_t* cvar = entry.second;
if (cvar->IsArchived()) {
const char* value;
if (cvar->ccvar.latchedString) {
value = cvar->ccvar.latchedString;
} else {
value = cvar->value.c_str();
}
result << Str::Format("seta %s %s\n", entry.first.c_str(), Cmd::Escape(value).c_str());
}
}
return result.str();
}
char* InfoString(int flag, bool big) {
static char info[BIG_INFO_STRING];
info[0] = 0;
CvarMap& cvars = GetCvarMap();
for (auto& entry : cvars) {
cvarRecord_t* cvar = entry.second;
if (cvar->flags & flag) {
Info_SetValueForKey(info, entry.first.c_str(), cvar->value.c_str(), big);
}
}
return info;
}
void PopulateInfoMap(int flag, InfoMap& map) {
for (auto& entry : GetCvarMap()) {
cvarRecord_t* cvar = entry.second;
if (cvar->flags & flag) {
map[entry.first] = cvar->value;
}
}
}
void SetValueCProxy(const std::string& cvarName, const std::string& value) {
InternalSetValue(cvarName, value, 0, true, false);
}
/*
===============================================================================
Cvar:: Cvar-related commands
===============================================================================
*/
class SetCmd: public Cmd::StaticCmd {
public:
SetCmd(const std::string& name, const std::string& help, int flags): Cmd::StaticCmd(name, Cmd::BASE, help), flags(flags) {
}
void Run(const Cmd::Args& args) const override {
if (args.Argc() < 2) {
PrintUsage(args, "<variable> <value>","");
return;
}
const std::string& name = args.Argv(1);
const std::string& value = args.ConcatArgs(2);
::Cvar::SetValue(name, value);
::Cvar::AddFlags(name, flags);
}
Cmd::CompletionResult Complete(int argNum, const Cmd::Args&, Str::StringRef prefix) const override {
if (argNum == 1) {
return ::Cvar::Complete(prefix);
}
return {};
}
private:
int flags;
};
static SetCmd SetCmdRegistration("set", "sets the value of a cvar", 0);
static SetCmd SetuCmdRegistration("setu", "sets the value of a cvar", USERINFO);
static SetCmd SetsCmdRegistration("sets", "sets the value of a cvar", SERVERINFO);
static SetCmd SetaCmdRegistration("seta", "sets the value of a cvar and marks the cvar as archived", USER_ARCHIVE);
class ResetCmd: public Cmd::StaticCmd {
public:
ResetCmd(): Cmd::StaticCmd("reset", Cmd::BASE, "resets the named variables") {
}
void Run(const Cmd::Args& args) const override {
int argc = args.Argc();
bool clearArchive = true;
if (argc < 2) {
PrintUsage(args, "<variable>…", "");
return;
}
CvarMap& cvars = GetCvarMap();
for (int i = 1; i < argc; ++i)
{
const std::string& name = args[i];
auto iter = cvars.find(name);
if (iter != cvars.end()) {
cvarRecord_t* cvar = iter->second;
::Cvar::SetValue(name, cvar->resetValue);
if (clearArchive) {
::Cvar::ClearFlags(name, USER_ARCHIVE);
}
} else {
Print("Cvar '%s' doesn't exist", name.c_str());
}
}
}
Cmd::CompletionResult Complete(int argNum, const Cmd::Args&, Str::StringRef prefix) const override {
if (argNum) {
return ::Cvar::Complete(prefix);
}
return {};
}
};
static ResetCmd ResetCmdRegistration;
std::string Raw(const std::string& src) {
std::string out;
size_t length = src.length();
for (size_t i = 0; i < length; ++i)
{
if (src[i] == '^')
out += '^';
out += src[i];
}
return out;
}
class ListCvars: public Cmd::StaticCmd {
public:
ListCvars(): Cmd::StaticCmd("listCvars", Cmd::BASE, "lists variables") {
}
void Run(const Cmd::Args& args) const override {
CvarMap& cvars = GetCvarMap();
bool raw = false;
std::string pattern = "";
//Read parameters
if (args.Argc() > 1) {
pattern = args.Argv(1);
if (Cmd::IsSwitch(pattern, "-raw")) {
raw = true;
pattern = (args.Argc() > 2) ? args.Argv(2) : "";
}
}
struct CvarMatch_t {
std::string name;
cvarRecord_t* record;
};
std::vector<CvarMatch_t> matches;
size_t maxNameLength = 0;
//Find all the matching cvars
for (auto& entry : cvars) {
if (Com_Filter(pattern.c_str(), entry.first.c_str(), false)) {
CvarMatch_t match;
match.name = entry.first;
match.record = entry.second;
matches.push_back(match);
//TODO: the raw parameter is not handled, need a function to escape carets
maxNameLength = std::max(maxNameLength, match.name.length());
}
}
// TODO: case insensitive compare function?
std::sort(matches.begin(), matches.end(),
[](CvarMatch_t &a, CvarMatch_t &b) { return a.name < b.name; });
//Print the matches, keeping the flags and descriptions aligned
for (size_t i = 0; i < matches.size(); i++) {
const std::string& name = matches[i].name;
cvarRecord_t* var = matches[i].record;
std::string cvarFlags = "";
cvarFlags += (var->flags & SERVERINFO) ? "S" : "_";
cvarFlags += (var->flags & SYSTEMINFO) ? "s" : "_";
cvarFlags += (var->flags & USERINFO) ? "U" : "_";
cvarFlags += (var->flags & ROM) ? "R" : "_";
cvarFlags += (var->flags & INIT) ? "I" : "_";
cvarFlags += (var->flags & TEMPORARY) ? "T" : (var->flags & USER_ARCHIVE) ? "A" : "_";
cvarFlags += (var->flags & (CVAR_LATCH | INTERNAL_LATCH)) ? "L" : "_";
cvarFlags += (var->flags & CHEAT) ? "C" : "_";
cvarFlags += (var->flags & CVAR_USER_CREATED) ? "?" : "_";
// this is going to 'break' (wrong output) if the description contains any ^s other than in the variable value(s)
if (raw) {
Print(" %-*s %s %s", maxNameLength, name, cvarFlags, Raw(var->description));
}
else {
Print(" %-*s %s %s", maxNameLength, name, cvarFlags, var->description);
}
}
Print("%zu cvars", matches.size());
}
Cmd::CompletionResult Complete(int argNum, const Cmd::Args& args, Str::StringRef prefix) const override {
int nameIndex = 1;
int argc = args.Argc();
// FIXME: translation
static const std::initializer_list<Cmd::CompletionItem> cvarFlags = {
{"-raw", "display colour controls"},
};
// command only allows one switch
if (argc > 1 && args[1][0] == '-') {
nameIndex = 2;
}
if (argNum == 1) {
auto completion = ::Cvar::Complete(prefix);
Cmd::AddToCompletion(completion, prefix, cvarFlags);
return completion;
}
if (argNum == nameIndex) {
return ::Cvar::Complete(prefix);
}
return {};
}
};
static ListCvars ListCvarsRegistration;
}