/
con_data.c
1834 lines (1583 loc) · 50.1 KB
/
con_data.c
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**\file con_data.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2012 Daniel Swanson <danij@dengine.net>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* Console databases for cvars, ccmds, aliases and known words.
*/
// HEADER FILES ------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include "de_base.h"
#include "de_console.h"
#include "cbuffer.h"
#include "m_misc.h"
#include "blockset.h"
#include "pathtree.h"
// MACROS ------------------------------------------------------------------
// Substrings in CVar names are delimited by this character.
#define CVARDIRECTORY_DELIMITER '-'
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
D_CMD(HelpWhat);
D_CMD(HelpApropos);
D_CMD(ListAliases);
D_CMD(ListCmds);
D_CMD(ListVars);
#if _DEBUG
D_CMD(PrintVarStats);
#endif
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static boolean inited = false;
/// Console variable directory.
static PathTree* cvarDirectory;
static ccmd_t* ccmdListHead;
/// \todo Replace with a data structure that allows for deletion of elements.
static blockset_t* ccmdBlockSet;
/// Running total of the number of uniquely-named commands.
static uint numUniqueNamedCCmds;
/// \todo Replace with a BST.
static uint numCAliases;
static calias_t** caliases;
/// The list of known words (for completion).
/// \todo Replace with a persistent self-balancing BST (Treap?)?
static knownword_t* knownWords;
static uint numKnownWords;
static boolean knownWordsNeedUpdate;
static char* emptyString = "";
static Uri* emptyUri;
// CODE --------------------------------------------------------------------
void Con_DataRegister(void)
{
C_CMD("help", "s", HelpWhat);
C_CMD("apropos", "s", HelpApropos);
C_CMD("listaliases", NULL, ListAliases);
C_CMD("listcmds", NULL, ListCmds);
C_CMD("listvars", NULL, ListVars);
#if _DEBUG
C_CMD("varstats", NULL, PrintVarStats);
#endif
}
static int markVariableUserDataFreed(PathTreeNode* node, void* parameters)
{
assert(node && parameters);
{
cvar_t* var = PathTreeNode_UserData(node);
void** ptr = (void**) parameters;
if(var)
switch(CVar_Type(var))
{
case CVT_CHARPTR:
if(*ptr == CV_CHARPTR(var)) var->flags &= ~CVF_CAN_FREE;
break;
case CVT_URIPTR:
if(*ptr == CV_URIPTR(var)) var->flags &= ~CVF_CAN_FREE;
break;
default: break;
}
return 0; // Continue iteration.
}
}
static int clearVariable(PathTreeNode* node, void* parameters)
{
cvar_t* var = (cvar_t*)PathTreeNode_UserData(node);
DENG_UNUSED(parameters);
if(var)
{
// Detach our user data from this node.
PathTreeNode_SetUserData(node, 0);
if(CVar_Flags(var) & CVF_CAN_FREE)
{
void** ptr = NULL;
switch(CVar_Type(var))
{
case CVT_CHARPTR:
if(!CV_CHARPTR(var)) break;
ptr = (void**)var->ptr;
/// @note Multiple vars could be using the same pointer (so only free once).
PathTree_Iterate2(cvarDirectory, PCF_NO_BRANCH, NULL, PATHTREE_NOHASH, markVariableUserDataFreed, ptr);
free(*ptr); *ptr = emptyString;
break;
case CVT_URIPTR:
if(!CV_URIPTR(var)) break;
ptr = (void**)var->ptr;
/// @note Multiple vars could be using the same pointer (so only free once).
PathTree_Iterate2(cvarDirectory, PCF_NO_BRANCH, NULL, PATHTREE_NOHASH, markVariableUserDataFreed, ptr);
Uri_Delete((Uri*)*ptr); *ptr = emptyUri;
break;
default: {
#if _DEBUG
AutoStr* path = CVar_ComposePath(var);
Con_Message("Warning:clearVariable: Attempt to free user data for non-pointer type variable %s [%p], ignoring.\n",
Str_Text(path), (void*)var);
#endif
break; }
}
}
free(var);
}
return 0; // Continue iteration.
}
static void clearVariables(void)
{
/// If _DEBUG we'll traverse all nodes and verify our clear logic.
#if _DEBUG
int flags = 0;
#else
int flags = PCF_NO_BRANCH;
#endif
if(!cvarDirectory) return;
PathTree_Iterate(cvarDirectory, flags, NULL, PATHTREE_NOHASH, clearVariable);
PathTree_Clear(cvarDirectory);
}
/// Construct a new variable from the specified template and add it to the database.
static cvar_t* addVariable(const cvartemplate_t* tpl)
{
PathTreeNode* node = PathTree_Insert2(cvarDirectory, tpl->path, CVARDIRECTORY_DELIMITER);
cvar_t* newVar;
if(PathTreeNode_UserData(node))
{
Con_Error("Con_AddVariable: A variable with path '%s' is already known!", tpl->path);
return NULL; // Unreachable.
}
newVar = (cvar_t*) malloc(sizeof *newVar);
if(!newVar)
Con_Error("Con_AddVariable: Failed on allocation of %lu bytes for new CVar.", (unsigned long) sizeof *newVar);
newVar->flags = tpl->flags;
newVar->type = tpl->type;
newVar->ptr = tpl->ptr;
newVar->min = tpl->min;
newVar->max = tpl->max;
newVar->notifyChanged = tpl->notifyChanged;
newVar->directoryNode = node;
PathTreeNode_SetUserData(node, newVar);
knownWordsNeedUpdate = true;
return newVar;
}
static void clearAliases(void)
{
if(caliases)
{
// Free the alias data.
calias_t** cal;
uint i;
for(i = 0, cal = caliases; i < numCAliases; ++i, ++cal)
{
free((*cal)->name);
free((*cal)->command);
free(*cal);
}
free(caliases);
}
caliases = 0;
numCAliases = 0;
}
static void clearCommands(void)
{
if(ccmdBlockSet)
BlockSet_Delete(ccmdBlockSet);
ccmdBlockSet = 0;
ccmdListHead = 0;
numUniqueNamedCCmds = 0;
}
static void clearKnownWords(void)
{
if(knownWords)
free(knownWords);
knownWords = 0;
numKnownWords = 0;
knownWordsNeedUpdate = false;
}
static int C_DECL compareKnownWordByName(const void* a, const void* b)
{
const knownword_t* wA = (const knownword_t*)a;
const knownword_t* wB = (const knownword_t*)b;
AutoStr* textAString = NULL, *textBString = NULL;
const char* textA, *textB;
switch(wA->type)
{
case WT_CALIAS: textA = ((calias_t*)wA->data)->name; break;
case WT_CCMD: textA = ((ccmd_t*)wA->data)->name; break;
case WT_CVAR:
textAString = CVar_ComposePath((cvar_t*)wA->data);
textA = Str_Text(textAString);
break;
case WT_GAME: textA = Str_Text(Game_IdentityKey((Game*)wA->data)); break;
default:
Con_Error("compareKnownWordByName: Invalid type %i for word A.", wA->type);
exit(1); // Unreachable
}
switch(wB->type)
{
case WT_CALIAS: textB = ((calias_t*)wB->data)->name; break;
case WT_CCMD: textB = ((ccmd_t*)wB->data)->name; break;
case WT_CVAR:
textBString = CVar_ComposePath((cvar_t*)wB->data);
textB = Str_Text(textBString);
break;
case WT_GAME: textB = Str_Text(Game_IdentityKey((Game*)wB->data)); break;
default:
Con_Error("compareKnownWordByName: Invalid type %i for word B.", wB->type);
exit(1); // Unreachable
}
return stricmp(textA, textB);
}
static boolean removeFromKnownWords(knownwordtype_t type, void* data)
{
assert(VALID_KNOWNWORDTYPE(type) && data != 0);
{ uint i;
for(i = 0; i < numKnownWords; ++i)
{
knownword_t* word = &knownWords[i];
if(word->type != type || word->data != data)
continue;
if(i != numKnownWords-1)
memmove(knownWords + i, knownWords + i + 1, sizeof(knownword_t) * (numKnownWords - 1 - i));
--numKnownWords;
if(numKnownWords != 0)
{
knownWords = realloc(knownWords, sizeof(knownword_t) * numKnownWords);
}
else
{
free(knownWords); knownWords = 0;
}
return true;
}}
return false;
}
typedef struct {
uint count;
cvartype_t type;
boolean hidden;
boolean ignoreHidden;
} countvariableparams_t;
static int countVariable(const PathTreeNode* node, void* parameters)
{
assert(NULL != node && NULL != parameters);
{
countvariableparams_t* p = (countvariableparams_t*) parameters;
cvar_t* var = PathTreeNode_UserData(node);
if(!(p->ignoreHidden && (var->flags & CVF_HIDE)))
{
if(!VALID_CVARTYPE(p->type) && !p->hidden)
{
if(!p->ignoreHidden || !(var->flags & CVF_HIDE))
++(p->count);
}
else if((p->hidden && (var->flags & CVF_HIDE)) ||
(VALID_CVARTYPE(p->type) && p->type == CVar_Type(var)))
{
++(p->count);
}
}
return 0; // Continue iteration.
}
}
static int addVariableToKnownWords(const PathTreeNode* node, void* parameters)
{
assert(NULL != node && NULL != parameters);
{
cvar_t* var = PathTreeNode_UserData(node);
uint* index = (uint*) parameters;
if(NULL != var && !(var->flags & CVF_HIDE))
{
knownWords[*index].type = WT_CVAR;
knownWords[*index].data = var;
++(*index);
}
return 0; // Continue iteration.
}
}
/**
* Collate all known words and sort them alphabetically.
* Commands, variables (except those hidden) and aliases are known words.
*/
static void updateKnownWords(void)
{
countvariableparams_t countCVarParams;
int i, gameCount;
ccmd_t* ccmd;
size_t len;
uint c;
if(!knownWordsNeedUpdate) return;
// Count the number of visible console variables.
countCVarParams.count = 0;
countCVarParams.type = -1;
countCVarParams.hidden = false;
countCVarParams.ignoreHidden = true;
if(cvarDirectory)
{
PathTree_Iterate2_Const(cvarDirectory, PCF_NO_BRANCH, NULL, PATHTREE_NOHASH, countVariable, &countCVarParams);
}
// Build the known words table.
numKnownWords = numUniqueNamedCCmds + countCVarParams.count + numCAliases + GameCollection_Count(App_GameCollection());
len = sizeof(knownword_t) * numKnownWords;
knownWords = realloc(knownWords, len);
memset(knownWords, 0, len);
c = 0;
// Add commands?
/// \note ccmd list is NOT yet sorted.
for(ccmd = ccmdListHead; ccmd; ccmd = ccmd->next)
{
// Skip overloaded variants.
if(ccmd->prevOverload) continue;
knownWords[c].type = WT_CCMD;
knownWords[c].data = ccmd;
++c;
}
// Add variables?
if(0 != countCVarParams.count)
{
/// \note cvars are NOT sorted.
PathTree_Iterate2_Const(cvarDirectory, PCF_NO_BRANCH, NULL, PATHTREE_NOHASH, addVariableToKnownWords, &c);
}
// Add aliases?
if(0 != numCAliases)
{
/// \note caliases array is already sorted.
calias_t** cal;
uint i;
for(i = 0, cal = caliases; i < numCAliases; ++i, ++cal)
{
knownWords[c].type = WT_CALIAS;
knownWords[c].data = *cal;
++c;
}
}
// Add games?
gameCount = GameCollection_Count(App_GameCollection());
for(i = 0; i < gameCount; ++i)
{
Game* game = GameCollection_ByIndex(App_GameCollection(), i);
knownWords[c].type = WT_GAME;
knownWords[c].data = game;
++c;
}
// Sort it so we get nice alphabetical word completions.
qsort(knownWords, numKnownWords, sizeof(knownword_t), compareKnownWordByName);
knownWordsNeedUpdate = false;
}
const ddstring_t* CVar_TypeName(cvartype_t type)
{
static const ddstring_t names[CVARTYPE_COUNT] = {
{ "invalid" },
{ "CVT_BYTE" },
{ "CVT_INT" },
{ "CVT_FLOAT" },
{ "CVT_CHARPTR"},
{ "CVT_URIPTR" }
};
return &names[(VALID_CVARTYPE(type)? type : 0)];
}
cvartype_t CVar_Type(const cvar_t* var)
{
assert(var);
return var->type;
}
int CVar_Flags(const cvar_t* var)
{
assert(var);
return var->flags;
}
AutoStr* CVar_ComposePath(const cvar_t* var)
{
assert(var);
return PathTreeNode_ComposePath2(var->directoryNode, AutoStr_NewStd(), NULL, CVARDIRECTORY_DELIMITER);
}
void CVar_SetUri2(cvar_t* var, const Uri* uri, int svFlags)
{
assert(var);
{
Uri* newUri = NULL;
boolean changed = false;
if((var->flags & CVF_READ_ONLY) && !(svFlags & SVF_WRITE_OVERRIDE))
{
AutoStr* path = CVar_ComposePath(var);
Con_Printf("%s (var) is read-only. It can't be changed (not even with force)\n", Str_Text(path));
return;
}
if(var->type != CVT_URIPTR)
{
Con_Error("CVar::SetUri: Not of type %s.", Str_Text(CVar_TypeName(CVT_URIPTR)));
return; // Unreachable.
}
if(!CV_URIPTR(var) && !uri)
return;
// Compose the new uri.
newUri = Uri_NewCopy(uri);
if(!CV_URIPTR(var) || !Uri_Equality(CV_URIPTR(var), newUri))
changed = true;
// Free the old uri, if one exists.
if((var->flags & CVF_CAN_FREE) && CV_URIPTR(var))
Uri_Delete(CV_URIPTR(var));
var->flags |= CVF_CAN_FREE;
CV_URIPTR(var) = newUri;
// Make the change notification callback
if(var->notifyChanged != NULL && changed)
var->notifyChanged();
}
}
void CVar_SetUri(cvar_t* var, const Uri* uri)
{
CVar_SetUri2(var, uri, 0);
}
void CVar_SetString2(cvar_t* var, const char* text, int svFlags)
{
assert(NULL != var);
{
boolean changed = false;
size_t oldLen, newLen;
if((var->flags & CVF_READ_ONLY) && !(svFlags & SVF_WRITE_OVERRIDE))
{
AutoStr* path = CVar_ComposePath(var);
Con_Printf("%s (var) is read-only. It can't be changed (not even with force)\n", Str_Text(path));
return;
}
if(var->type != CVT_CHARPTR)
{
Con_Error("CVar::SetString: Not of type %s.", Str_Text(CVar_TypeName(CVT_CHARPTR)));
return; // Unreachable.
}
oldLen = (!CV_CHARPTR(var)? 0 : strlen(CV_CHARPTR(var)));
newLen = (!text ? 0 : strlen(text));
if(oldLen == 0 && newLen == 0)
return;
if(oldLen != newLen || stricmp(text, CV_CHARPTR(var)))
changed = true;
// Free the old string, if one exists.
if((var->flags & CVF_CAN_FREE) && CV_CHARPTR(var))
free(CV_CHARPTR(var));
// Allocate a new string.
var->flags |= CVF_CAN_FREE;
CV_CHARPTR(var) = (char*)malloc(newLen + 1);
if(!CV_CHARPTR(var))
Con_Error("CVar::SetString: Failed on allocation of %lu bytes for new string value.", (unsigned long) (newLen + 1));
strcpy(CV_CHARPTR(var), text);
// Make the change notification callback
if(var->notifyChanged != NULL && changed)
var->notifyChanged();
}
}
void CVar_SetString(cvar_t* var, const char* text)
{
CVar_SetString2(var, text, 0);
}
void CVar_SetInteger2(cvar_t* var, int value, int svFlags)
{
assert(NULL != var);
{
boolean changed = false;
if((var->flags & CVF_READ_ONLY) && !(svFlags & SVF_WRITE_OVERRIDE))
{
AutoStr* path = CVar_ComposePath(var);
Con_Printf("%s (var) is read-only. It can't be changed (not even with force).\n", Str_Text(path));
return;
}
switch(var->type)
{
case CVT_INT:
if(CV_INT(var) != value)
changed = true;
CV_INT(var) = value;
break;
case CVT_BYTE:
if(CV_BYTE(var) != (byte) value)
changed = true;
CV_BYTE(var) = (byte) value;
break;
case CVT_FLOAT:
if(CV_FLOAT(var) != (float) value)
changed = true;
CV_FLOAT(var) = (float) value;
break;
default: {
AutoStr* path = CVar_ComposePath(var);
Con_Message("Warning:CVar::SetInteger: Attempt to set incompatible var %s to %i, ignoring.\n", Str_Text(path), value);
return; }
}
// Make a change notification callback?
if(var->notifyChanged != 0 && changed)
var->notifyChanged();
}
}
void CVar_SetInteger(cvar_t* var, int value)
{
CVar_SetInteger2(var, value, 0);
}
void CVar_SetFloat2(cvar_t* var, float value, int svFlags)
{
assert(NULL != var);
{
boolean changed = false;
if((var->flags & CVF_READ_ONLY) && !(svFlags & SVF_WRITE_OVERRIDE))
{
AutoStr* path = CVar_ComposePath(var);
Con_Printf("%s (cvar) is read-only. It can't be changed (not even with force).\n", Str_Text(path));
return;
}
switch(var->type)
{
case CVT_INT:
if(CV_INT(var) != (int) value)
changed = true;
CV_INT(var) = (int) value;
break;
case CVT_BYTE:
if(CV_BYTE(var) != (byte) value)
changed = true;
CV_BYTE(var) = (byte) value;
break;
case CVT_FLOAT:
if(CV_FLOAT(var) != value)
changed = true;
CV_FLOAT(var) = value;
break;
default: {
AutoStr* path = CVar_ComposePath(var);
Con_Message("Warning: CVar::SetFloat: Attempt to set incompatible cvar %s to %g, ignoring.\n", Str_Text(path), value);
return; }
}
// Make a change notification callback?
if(var->notifyChanged != 0 && changed)
var->notifyChanged();
}
}
void CVar_SetFloat(cvar_t* var, float value)
{
CVar_SetFloat2(var, value, 0);
}
int CVar_Integer(const cvar_t* var)
{
assert(var);
switch(var->type)
{
case CVT_BYTE: return CV_BYTE(var);
case CVT_INT: return CV_INT(var);
case CVT_FLOAT: return CV_FLOAT(var);
case CVT_CHARPTR: return strtol(CV_CHARPTR(var), 0, 0);
default: {
#if _DEBUG
AutoStr* path = CVar_ComposePath(var);
Con_Message("Warning: CVar::Integer: Attempted on incompatible variable %s [%p type:%s], returning 0\n",
Str_Text(path), (void*)var, Str_Text(CVar_TypeName(CVar_Type(var))));
#endif
return 0; }
}
}
float CVar_Float(const cvar_t* var)
{
assert(var);
switch(var->type)
{
case CVT_BYTE: return CV_BYTE(var);
case CVT_INT: return CV_INT(var);
case CVT_FLOAT: return CV_FLOAT(var);
case CVT_CHARPTR: return strtod(CV_CHARPTR(var), 0);
default: {
#if _DEBUG
AutoStr* path = CVar_ComposePath(var);
Con_Message("Warning: CVar::Float: Attempted on incompatible variable %s [%p type:%s], returning 0\n",
Str_Text(path), (void*)var, Str_Text(CVar_TypeName(CVar_Type(var))));
#endif
return 0;
}
}
}
byte CVar_Byte(const cvar_t* var)
{
assert(var);
switch(var->type)
{
case CVT_BYTE: return CV_BYTE(var);
case CVT_INT: return CV_INT(var);
case CVT_FLOAT: return CV_FLOAT(var);
case CVT_CHARPTR: return strtol(CV_CHARPTR(var), 0, 0);
default: {
#if _DEBUG
AutoStr* path = CVar_ComposePath(var);
Con_Message("Warning: CVar::Byte: Attempted on incompatible variable %s [%p type:%s], returning 0\n",
Str_Text(path), (void*)var, Str_Text(CVar_TypeName(CVar_Type(var))));
#endif
return 0;
}
}
}
char* CVar_String(const cvar_t* var)
{
assert(var);
/// @todo Why not implement in-place value to string conversion?
switch(var->type)
{
case CVT_CHARPTR: return CV_CHARPTR(var);
default: {
#if _DEBUG
AutoStr* path = CVar_ComposePath(var);
Con_Message("Warning: CVar::String: Attempted on incompatible variable %s [%p type:%s], returning emptyString\n",
Str_Text(path), (void*)var, Str_Text(CVar_TypeName(CVar_Type(var))));
#endif
return emptyString;
}
}
}
Uri* CVar_Uri(const cvar_t* var)
{
assert(var);
/// @todo Why not implement in-place string to uri conversion?
switch(var->type)
{
case CVT_URIPTR: return CV_URIPTR(var);
default: {
#if _DEBUG
AutoStr* path = CVar_ComposePath(var);
Con_Message("Warning: CVar::String: Attempted on incompatible variable %s [%p type:%s], returning emptyUri\n",
Str_Text(path), (void*)var, Str_Text(CVar_TypeName(CVar_Type(var))));
#endif
return emptyUri;
}
}
}
void Con_AddVariable(const cvartemplate_t* tpl)
{
assert(inited);
if(!tpl)
{
Con_Message("Warning:Con_AddVariable: Passed invalid value for argument 'tpl', ignoring.\n");
return;
}
if(CVT_NULL == tpl->type)
{
Con_Message("Warning:Con_AddVariable: Attempt to register variable '%s' as type "
"%s, ignoring.\n", Str_Text(CVar_TypeName(CVT_NULL)), tpl->path);
return;
}
if(Con_FindVariable(tpl->path))
Con_Error("Error: A CVAR with the name '%s' is already registered.", tpl->path);
addVariable(tpl);
}
void Con_AddVariableList(const cvartemplate_t* tplList)
{
assert(inited);
if(!tplList)
{
Con_Message("Warning:Con_AddVariableList: Passed invalid value for "
"argument 'tplList', ignoring.\n");
return;
}
for(; tplList->path; ++tplList)
{
if(Con_FindVariable(tplList->path))
Con_Error("Error: A CVAR with the name '%s' is already registered.", tplList->path);
addVariable(tplList);
}
}
cvar_t* Con_FindVariable(const char* path)
{
PathTreeNode* node;
assert(inited);
node = PathTree_Find2(cvarDirectory, PCF_NO_BRANCH|PCF_MATCH_FULL, path, CVARDIRECTORY_DELIMITER);
if(!node) return NULL;
return (cvar_t*) PathTreeNode_UserData(node);
}
/// \note Part of the Doomsday public API
cvartype_t Con_GetVariableType(const char* path)
{
cvar_t* var = Con_FindVariable(path);
if(!var) return CVT_NULL;
return var->type;
}
void Con_PrintCVar(cvar_t* var, char* prefix)
{
assert(inited);
{
char equals = '=';
AutoStr* path;
if(!var) return;
if((var->flags & CVF_PROTECTED) || (var->flags & CVF_READ_ONLY))
equals = ':';
if(prefix)
Con_Printf("%s", prefix);
path = CVar_ComposePath(var);
switch(var->type)
{
case CVT_BYTE: Con_Printf("%s %c %d", Str_Text(path), equals, CV_BYTE(var)); break;
case CVT_INT: Con_Printf("%s %c %d", Str_Text(path), equals, CV_INT(var)); break;
case CVT_FLOAT: Con_Printf("%s %c %g", Str_Text(path), equals, CV_FLOAT(var)); break;
case CVT_CHARPTR: Con_Printf("%s %c \"%s\"", Str_Text(path), equals, CV_CHARPTR(var)); break;
case CVT_URIPTR: {
AutoStr* valPath = (CV_URIPTR(var)? Uri_ToString(CV_URIPTR(var)) : NULL);
Con_Printf("%s %c \"%s\"", Str_Text(path), equals, (CV_URIPTR(var)? Str_Text(valPath) : ""));
break; }
default: Con_Printf("%s (bad type!)", Str_Text(path)); break;
}
Con_Printf("\n");
}
}
void Con_AddCommand(const ccmdtemplate_t* ccmd)
{
assert(inited);
{
int minArgs, maxArgs;
cvartype_t args[MAX_ARGS];
ccmd_t* newCCmd, *overloaded = 0;
if(!ccmd) return;
if(!ccmd->name)
Con_Error("Con_AddCommand: CCmd missing a name.");
/*#if _DEBUG
Con_Message("Con_AddCommand: '%s' \"%s\" (%i).\n", ccmd->name,
ccmd->argTemplate, ccmd->flags);
#endif*/
// Decode the usage string if present.
if(ccmd->argTemplate != 0)
{
size_t l, len;
cvartype_t type = CVT_NULL;
boolean unlimitedArgs;
char c;
len = strlen(ccmd->argTemplate);
minArgs = 0;
unlimitedArgs = false;
for(l = 0; l < len; ++l)
{
c = ccmd->argTemplate[l];
switch(c)
{
// Supported type symbols:
case 'b': type = CVT_BYTE; break;
case 'i': type = CVT_INT; break;
case 'f': type = CVT_FLOAT; break;
case 's': type = CVT_CHARPTR; break;
// Special symbols:
case '*':
// Variable arg list.
if(l != len-1)
Con_Error("Con_AddCommand: CCmd '%s': '*' character "
"not last in argument template: \"%s\".",
ccmd->name, ccmd->argTemplate);
unlimitedArgs = true;
type = CVT_NULL; // Not a real argument.
break;
// Erroneous symbol:
default:
Con_Error("Con_AddCommand: CCmd '%s': Invalid character "
"'%c' in argument template: \"%s\".", ccmd->name, c,
ccmd->argTemplate);
}
if(type != CVT_NULL)
{
if(minArgs >= MAX_ARGS)
Con_Error("Con_AddCommand: CCmd '%s': Too many arguments. "
"Limit is %i.", ccmd->name, MAX_ARGS);
args[minArgs++] = type;
}
}
// Set the min/max parameter counts for this ccmd.
if(unlimitedArgs)
{
maxArgs = -1;
if(minArgs == 0)
minArgs = -1;
}
else
maxArgs = minArgs;
/*#if _DEBUG
{
int i;
Con_Message("Con_AddCommand: CCmd '%s': minArgs %i, maxArgs %i: \"",
ccmd->name, minArgs, maxArgs);
for(i = 0; i < minArgs; ++i)
{
switch(args[i])
{
case CVT_BYTE: c = 'b'; break;
case CVT_INT: c = 'i'; break;
case CVT_FLOAT: c = 'f'; break;
case CVT_CHARPTR: c = 's'; break;
}
Con_Printf("%c", c);
}
Con_Printf("\".\n");
}
#endif*/
}
else // It's usage is NOT validated by Doomsday.
{
minArgs = maxArgs = -1;
/*#if _DEBUG
if(ccmd->args == NULL)
Con_Message("Con_AddCommand: CCmd '%s' will not have it's usage validated.\n", ccmd->name);
#endif*/
}
// Now check that the ccmd to be registered is unique.
// We allow multiple ccmds with the same name if we can determine by
// their paramater lists that they are unique (overloading).
{ ccmd_t* other;
if((other = Con_FindCommand(ccmd->name)) != 0)
{
boolean unique = true;
// The ccmd being registered is NOT a deng validated ccmd
// and there is already an existing ccmd by this name?
if(minArgs == -1 && maxArgs == -1)
unique = false;
if(unique)
{
// Check each variant.
ccmd_t* variant = other;
do
{
// An existing ccmd with no validation?
if(variant->minArgs == -1 && variant->maxArgs == -1)
unique = false;
// An existing ccmd with a lower minimum and no maximum?
else if(variant->minArgs < minArgs && variant->maxArgs == -1)
unique = false;
// An existing ccmd with a larger min and this ccmd has no max?
else if(variant->minArgs > minArgs && maxArgs == -1)
unique = false;
// An existing ccmd with the same minimum number of args?
else if(variant->minArgs == minArgs)
{
// \todo Implement support for paramater type checking.