forked from necropotame/teeworlds-infclass
-
Notifications
You must be signed in to change notification settings - Fork 4
/
console.cpp
1057 lines (899 loc) · 24.6 KB
/
console.cpp
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
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#include <new>
#include <base/math.h>
#include <base/system.h>
#include <engine/storage.h>
#include <engine/shared/protocol.h>
#include "config.h"
#include "console.h"
#include "linereader.h"
// todo: rework this
const char *CConsole::CResult::GetString(unsigned Index)
{
if (Index >= m_NumArgs)
return "";
return m_apArgs[Index];
}
int CConsole::CResult::GetInteger(unsigned Index)
{
if (Index >= m_NumArgs)
return 0;
return str_toint(m_apArgs[Index]);
}
float CConsole::CResult::GetFloat(unsigned Index)
{
if (Index >= m_NumArgs)
return 0.0f;
return str_tofloat(m_apArgs[Index]);
}
const IConsole::CCommandInfo *CConsole::CCommand::NextCommandInfo(int AccessLevel, int FlagMask) const
{
const CCommand *pInfo = m_pNext;
while(pInfo)
{
if(pInfo->m_Flags&FlagMask && pInfo->m_AccessLevel >= AccessLevel)
break;
pInfo = pInfo->m_pNext;
}
return pInfo;
}
const IConsole::CCommandInfo *CConsole::FirstCommandInfo(int AccessLevel, int FlagMask) const
{
for(const CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->m_pNext)
{
if(pCommand->m_Flags&FlagMask && pCommand->GetAccessLevel() >= AccessLevel)
return pCommand;
}
return 0;
}
// the maximum number of tokens occurs in a string of length CONSOLE_MAX_STR_LENGTH with tokens size 1 separated by single spaces
int CConsole::ParseStart(CResult *pResult, const char *pString, int Length)
{
char *pStr;
int Len = sizeof(pResult->m_aStringStorage);
if(Length < Len)
Len = Length;
str_copy(pResult->m_aStringStorage, pString, Len);
pStr = pResult->m_aStringStorage;
// get command
pStr = str_skip_whitespaces(pStr);
pResult->m_pCommand = pStr;
pStr = str_skip_to_whitespace(pStr);
if(*pStr)
{
pStr[0] = 0;
pStr++;
}
pResult->m_pArgsStart = pStr;
return 0;
}
int CConsole::ParseArgs(CResult *pResult, const char *pFormat)
{
char Command = *pFormat;
char *pStr;
int Optional = 0;
int Error = 0;
pStr = pResult->m_pArgsStart;
while(1)
{
if(!Command)
break;
if(Command == '?')
Optional = 1;
else
{
pStr = str_skip_whitespaces(pStr);
if(!(*pStr)) // error, non optional command needs value
{
if(!Optional)
{
Error = 1;
break;
}
while(Command)
{
Command = NextParam(pFormat);
}
break;
}
// add token
if(*pStr == '"')
{
char *pDst;
pStr++;
pResult->AddArgument(pStr);
pDst = pStr; // we might have to process escape data
while(1)
{
if(pStr[0] == '"')
break;
else if(pStr[0] == '\\')
{
if(pStr[1] == '\\')
pStr++; // skip due to escape
else if(pStr[1] == '"')
pStr++; // skip due to escape
}
else if(pStr[0] == 0)
return 1; // return error
*pDst = *pStr;
pDst++;
pStr++;
}
// write null termination
*pDst = 0;
pStr++;
}
else
{
pResult->AddArgument(pStr);
if(Command == 'r') // rest of the string
break;
else if(Command == 'i') // validate int
pStr = str_skip_to_whitespace(pStr);
else if(Command == 'f') // validate float
pStr = str_skip_to_whitespace(pStr);
else if(Command == 's') // validate string
pStr = str_skip_to_whitespace(pStr);
if(pStr[0] != 0) // check for end of string
{
pStr[0] = 0;
pStr++;
}
}
}
Command = NextParam(pFormat);
}
return Error;
}
/* DDNET MODIFICATION START *******************************************/
char CConsole::NextParam(const char *&pFormat)
{
if (*pFormat)
{
pFormat++;
if (*pFormat == '<')
{
// skip bracket contents
for (; *pFormat != '>'; pFormat++)
{
if (!*pFormat)
return *pFormat;
}
// skip ']'
pFormat++;
// skip space if there is one
if (*pFormat == ' ')
pFormat++;
}
}
return *pFormat;
}
/* DDNET MODIFICATION END *********************************************/
int CConsole::RegisterPrintCallback(int OutputLevel, FPrintCallback pfnPrintCallback, void *pUserData)
{
if(m_NumPrintCB == MAX_PRINT_CB)
return -1;
m_aPrintCB[m_NumPrintCB].m_OutputLevel = clamp(OutputLevel, (int)(OUTPUT_LEVEL_STANDARD), (int)(OUTPUT_LEVEL_DEBUG));
m_aPrintCB[m_NumPrintCB].m_pfnPrintCallback = pfnPrintCallback;
m_aPrintCB[m_NumPrintCB].m_pPrintCallbackUserdata = pUserData;
return m_NumPrintCB++;
}
void CConsole::SetPrintOutputLevel(int Index, int OutputLevel)
{
if(Index >= 0 && Index < MAX_PRINT_CB)
m_aPrintCB[Index].m_OutputLevel = clamp(OutputLevel, (int)(OUTPUT_LEVEL_STANDARD), (int)(OUTPUT_LEVEL_DEBUG));
}
void CConsole::Print(int Level, const char *pFrom, const char *pStr)
{
dbg_msg(pFrom ,"%s", pStr);
for(int i = 0; i < m_NumPrintCB; ++i)
{
if(Level <= m_aPrintCB[i].m_OutputLevel && m_aPrintCB[i].m_pfnPrintCallback)
{
char aBuf[1024];
str_format(aBuf, sizeof(aBuf), "[%s]: %s", pFrom, pStr);
m_aPrintCB[i].m_pfnPrintCallback(aBuf, m_aPrintCB[i].m_pPrintCallbackUserdata);
}
}
}
bool CConsole::LineIsValid(const char *pStr)
{
if(!pStr || *pStr == 0)
return false;
do
{
CResult Result;
const char *pEnd = pStr;
const char *pNextPart = 0;
int InString = 0;
while(*pEnd)
{
if(*pEnd == '"')
InString ^= 1;
else if(*pEnd == '\\') // escape sequences
{
if(pEnd[1] == '"')
pEnd++;
}
else if(!InString)
{
if(*pEnd == ';') // command separator
{
pNextPart = pEnd+1;
break;
}
else if(*pEnd == '#') // comment, no need to do anything more
break;
}
pEnd++;
}
if(ParseStart(&Result, pStr, (pEnd-pStr) + 1) != 0)
return false;
CCommand *pCommand = FindCommand(Result.m_pCommand, m_FlagMask);
if(!pCommand || ParseArgs(&Result, pCommand->m_pParams))
return false;
pStr = pNextPart;
}
while(pStr && *pStr);
return true;
}
void CConsole::ExecuteLineStroked(int Stroke, const char *pStr, int ClientID, bool TeamChat)
{
while(pStr && *pStr)
{
CResult Result;
Result.SetClientID(ClientID);
Result.SetTeamChat(TeamChat);
const char *pEnd = pStr;
const char *pNextPart = 0;
int InString = 0;
while(*pEnd)
{
if(*pEnd == '"')
InString ^= 1;
else if(*pEnd == '\\') // escape sequences
{
if(pEnd[1] == '"')
pEnd++;
}
else if(!InString)
{
if(*pEnd == ';') // command separator
{
pNextPart = pEnd+1;
break;
}
else if(*pEnd == '#') // comment, no need to do anything more
break;
}
pEnd++;
}
if(ParseStart(&Result, pStr, (pEnd-pStr) + 1) != 0)
return;
if(!*Result.m_pCommand)
return;
CCommand *pCommand = FindCommand(Result.m_pCommand, m_FlagMask);
if(pCommand)
{
if(pCommand->GetAccessLevel() >= m_AccessLevel)
{
int IsStrokeCommand = 0;
if(Result.m_pCommand[0] == '+')
{
// insert the stroke direction token
Result.AddArgument(m_paStrokeStr[Stroke]);
IsStrokeCommand = 1;
}
if(Stroke || IsStrokeCommand)
{
if(ParseArgs(&Result, pCommand->m_pParams))
{
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "Usage: %s %s", pCommand->m_pName, pCommand->m_pUsage);
Print(OUTPUT_LEVEL_STANDARD, "Console", "Invalid arguments.");
Print(OUTPUT_LEVEL_STANDARD, "Console", aBuf);
}
else if(m_StoreCommands && pCommand->m_Flags&CFGFLAG_STORE)
{
m_ExecutionQueue.AddEntry();
m_ExecutionQueue.m_pLast->m_pfnCommandCallback = pCommand->m_pfnCallback;
m_ExecutionQueue.m_pLast->m_pCommandUserData = pCommand->m_pUserData;
m_ExecutionQueue.m_pLast->m_Result = Result;
}
else
{
bool ValideArguments = pCommand->m_pfnCallback(&Result, pCommand->m_pUserData);
if(!ValideArguments)
{
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "Usage: %s %s", pCommand->m_pName, pCommand->m_pUsage);
Print(OUTPUT_LEVEL_STANDARD, "Console", "Invalid arguments.");
Print(OUTPUT_LEVEL_STANDARD, "Console", aBuf);
}
}
}
}
else if(Stroke)
{
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "Access for command %s denied.", Result.m_pCommand);
Print(OUTPUT_LEVEL_STANDARD, "Console", aBuf);
}
}
else if(Stroke)
{
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "No such command: %s.", Result.m_pCommand);
Print(OUTPUT_LEVEL_STANDARD, "Console", aBuf);
}
pStr = pNextPart;
}
}
void CConsole::PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser)
{
for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->m_pNext)
{
if(pCommand->m_Flags&FlagMask && pCommand->m_Temp == Temp)
{
if(str_find_nocase(pCommand->m_pName, pStr))
pfnCallback(pCommand->m_pName, pUser);
}
}
}
CConsole::CCommand *CConsole::FindCommand(const char *pName, int FlagMask)
{
for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->m_pNext)
{
if(pCommand->m_Flags&FlagMask)
{
if(str_comp_nocase(pCommand->m_pName, pName) == 0)
return pCommand;
}
}
return 0x0;
}
void CConsole::ExecuteLine(const char *pStr, int ClientID, bool TeamChat)
{
CConsole::ExecuteLineStroked(1, pStr, ClientID, TeamChat); // press it
CConsole::ExecuteLineStroked(0, pStr, ClientID, TeamChat); // then release it
}
void CConsole::ExecuteLineFlag(const char *pStr, int ClientID, bool TeamChat, int FlagMask)
{
int Temp = m_FlagMask;
m_FlagMask = FlagMask;
ExecuteLine(pStr, ClientID, TeamChat);
m_FlagMask = Temp;
}
void CConsole::ExecuteFile(const char *pFilename)
{
// make sure that this isn't being executed already
for(CExecFile *pCur = m_pFirstExec; pCur; pCur = pCur->m_pPrev)
if(str_comp(pFilename, pCur->m_pFilename) == 0)
return;
if(!m_pStorage)
m_pStorage = Kernel()->RequestInterface<IStorage>();
if(!m_pStorage)
return;
// push this one to the stack
CExecFile ThisFile;
CExecFile *pPrev = m_pFirstExec;
ThisFile.m_pFilename = pFilename;
ThisFile.m_pPrev = m_pFirstExec;
m_pFirstExec = &ThisFile;
// exec the file
IOHANDLE File = m_pStorage->OpenFile(pFilename, IOFLAG_READ, IStorage::TYPE_ALL);
char aBuf[256];
if(File)
{
char *pLine;
CLineReader lr;
str_format(aBuf, sizeof(aBuf), "executing '%s'", pFilename);
Print(IConsole::OUTPUT_LEVEL_STANDARD, "console", aBuf);
lr.Init(File);
while((pLine = lr.Get()))
ExecuteLine(pLine, -1, false);
io_close(File);
}
else
{
str_format(aBuf, sizeof(aBuf), "failed to open '%s'", pFilename);
Print(IConsole::OUTPUT_LEVEL_STANDARD, "console", aBuf);
}
m_pFirstExec = pPrev;
}
bool CConsole::Con_Echo(IResult *pResult, void *pUserData)
{
((CConsole*)pUserData)->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Console", pResult->GetString(0));
return true;
}
bool CConsole::Con_Exec(IResult *pResult, void *pUserData)
{
((CConsole*)pUserData)->ExecuteFile(pResult->GetString(0));
return true;
}
bool CConsole::ConModCommandAccess(IResult *pResult, void *pUser)
{
CConsole* pConsole = static_cast<CConsole *>(pUser);
char aBuf[128];
CCommand *pCommand = pConsole->FindCommand(pResult->GetString(0), CFGFLAG_SERVER);
if(pCommand)
{
if(pResult->NumArguments() == 2)
{
pCommand->SetAccessLevel(pResult->GetInteger(1));
str_format(aBuf, sizeof(aBuf), "moderator access for '%s' is now %s", pResult->GetString(0), pCommand->GetAccessLevel() ? "enabled" : "disabled");
}
else
str_format(aBuf, sizeof(aBuf), "moderator access for '%s' is %s", pResult->GetString(0), pCommand->GetAccessLevel() ? "enabled" : "disabled");
}
else
str_format(aBuf, sizeof(aBuf), "No such command: '%s'.", pResult->GetString(0));
pConsole->Print(OUTPUT_LEVEL_STANDARD, "Console", aBuf);
return true;
}
bool CConsole::ConModCommandStatus(IResult *pResult, void *pUser)
{
CConsole* pConsole = static_cast<CConsole *>(pUser);
char aBuf[240];
mem_zero(aBuf, sizeof(aBuf));
int Used = 0;
for(CCommand *pCommand = pConsole->m_pFirstCommand; pCommand; pCommand = pCommand->m_pNext)
{
if(pCommand->m_Flags&pConsole->m_FlagMask && pCommand->GetAccessLevel() == ACCESS_LEVEL_MOD)
{
int Length = str_length(pCommand->m_pName);
if(Used + Length + 2 < (int)(sizeof(aBuf)))
{
if(Used > 0)
{
Used += 2;
str_append(aBuf, ", ", sizeof(aBuf));
}
str_append(aBuf, pCommand->m_pName, sizeof(aBuf));
Used += Length;
}
else
{
pConsole->Print(OUTPUT_LEVEL_STANDARD, "Console", aBuf);
mem_zero(aBuf, sizeof(aBuf));
str_copy(aBuf, pCommand->m_pName, sizeof(aBuf));
Used = Length;
}
}
}
if(Used > 0)
pConsole->Print(OUTPUT_LEVEL_STANDARD, "Console", aBuf);
return true;
}
struct CIntVariableData
{
IConsole *m_pConsole;
int *m_pVariable;
int m_Min;
int m_Max;
};
struct CStrVariableData
{
IConsole *m_pConsole;
char *m_pStr;
int m_MaxSize;
};
static bool IntVariableCommand(IConsole::IResult *pResult, void *pUserData)
{
CIntVariableData *pData = (CIntVariableData *)pUserData;
if(pResult->NumArguments())
{
int Val = pResult->GetInteger(0);
// do clamping
if(pData->m_Min != pData->m_Max)
{
if (Val < pData->m_Min)
Val = pData->m_Min;
if (pData->m_Max != 0 && Val > pData->m_Max)
Val = pData->m_Max;
}
*(pData->m_pVariable) = Val;
}
return true;
}
static bool StrVariableCommand(IConsole::IResult *pResult, void *pUserData)
{
CStrVariableData *pData = (CStrVariableData *)pUserData;
if(pResult->NumArguments())
{
const char *pString = pResult->GetString(0);
if(!str_utf8_check(pString))
{
char Temp[4];
int Length = 0;
while(*pString)
{
int Size = str_utf8_encode(Temp, static_cast<const unsigned char>(*pString++));
if(Length+Size < pData->m_MaxSize)
{
mem_copy(pData->m_pStr+Length, &Temp, Size);
Length += Size;
}
else
break;
}
pData->m_pStr[Length] = 0;
}
else
str_copy(pData->m_pStr, pString, pData->m_MaxSize);
}
return true;
}
bool CConsole::ConToggle(IConsole::IResult *pResult, void *pUser)
{
CConsole* pConsole = static_cast<CConsole *>(pUser);
char aBuf[128] = {0};
CCommand *pCommand = pConsole->FindCommand(pResult->GetString(0), pConsole->m_FlagMask);
if(pCommand)
{
FCommandCallback pfnCallback = pCommand->m_pfnCallback;
void *pUserData = pCommand->m_pUserData;
// check for chain
if(pCommand->m_pfnCallback == Con_Chain)
{
CChain *pChainInfo = static_cast<CChain *>(pCommand->m_pUserData);
pfnCallback = pChainInfo->m_pfnCallback;
pUserData = pChainInfo->m_pCallbackUserData;
}
if(pfnCallback == IntVariableCommand)
{
CIntVariableData *pData = static_cast<CIntVariableData *>(pUserData);
int Val = *(pData->m_pVariable)==pResult->GetInteger(1) ? pResult->GetInteger(2) : pResult->GetInteger(1);
str_format(aBuf, sizeof(aBuf), "%s %i", pResult->GetString(0), Val);
pConsole->ExecuteLine(aBuf, -1, false);
aBuf[0] = 0;
}
else
str_format(aBuf, sizeof(aBuf), "Invalid command: '%s'.", pResult->GetString(0));
}
else
str_format(aBuf, sizeof(aBuf), "No such command: '%s'.", pResult->GetString(0));
if(aBuf[0] != 0)
pConsole->Print(OUTPUT_LEVEL_STANDARD, "Console", aBuf);
return true;
}
bool CConsole::ConToggleStroke(IConsole::IResult *pResult, void *pUser)
{
CConsole* pConsole = static_cast<CConsole *>(pUser);
char aBuf[128] = {0};
CCommand *pCommand = pConsole->FindCommand(pResult->GetString(1), pConsole->m_FlagMask);
if(pCommand)
{
FCommandCallback pfnCallback = pCommand->m_pfnCallback;
// check for chain
if(pCommand->m_pfnCallback == Con_Chain)
{
CChain *pChainInfo = static_cast<CChain *>(pCommand->m_pUserData);
pfnCallback = pChainInfo->m_pfnCallback;
}
if(pfnCallback == IntVariableCommand)
{
int Val = pResult->GetInteger(0)==0 ? pResult->GetInteger(3) : pResult->GetInteger(2);
str_format(aBuf, sizeof(aBuf), "%s %i", pResult->GetString(1), Val);
pConsole->ExecuteLine(aBuf, -1, false);
aBuf[0] = 0;
}
else
str_format(aBuf, sizeof(aBuf), "Invalid command: '%s'.", pResult->GetString(1));
}
else
str_format(aBuf, sizeof(aBuf), "No such command: '%s'.", pResult->GetString(1));
if(aBuf[0] != 0)
pConsole->Print(OUTPUT_LEVEL_STANDARD, "Console", aBuf);
return true;
}
CConsole::CConsole(int FlagMask)
{
m_FlagMask = FlagMask;
m_AccessLevel = ACCESS_LEVEL_ADMIN;
m_pRecycleList = 0;
m_TempCommands.Reset();
m_StoreCommands = true;
m_paStrokeStr[0] = "0";
m_paStrokeStr[1] = "1";
m_ExecutionQueue.Reset();
m_pFirstCommand = 0;
m_pFirstExec = 0;
mem_zero(m_aPrintCB, sizeof(m_aPrintCB));
m_NumPrintCB = 0;
m_pStorage = 0;
// register some basic commands
Register("echo", "r", CFGFLAG_SERVER|CFGFLAG_CLIENT, Con_Echo, this, "Echo the text");
Register("exec", "r", CFGFLAG_SERVER|CFGFLAG_CLIENT, Con_Exec, this, "Execute the specified file");
Register("toggle", "sii", CFGFLAG_SERVER|CFGFLAG_CLIENT, ConToggle, this, "Toggle config value");
Register("+toggle", "sii", CFGFLAG_CLIENT, ConToggleStroke, this, "Toggle config value via keypress");
Register("mod_command", "s?i", CFGFLAG_SERVER, ConModCommandAccess, this, "Specify command accessibility for moderators");
Register("mod_status", "", CFGFLAG_SERVER, ConModCommandStatus, this, "List all commands which are accessible for moderators");
// TODO: this should disappear
#define MACRO_CONFIG_INT(Name,ScriptName,Def,Min,Max,Flags,Desc) \
{ \
static CIntVariableData Data = { this, &g_Config.m_##Name, Min, Max }; \
Register(#ScriptName, "?i", Flags, IntVariableCommand, &Data, Desc); \
}
#define MACRO_CONFIG_STR(Name,ScriptName,Len,Def,Flags,Desc) \
{ \
static CStrVariableData Data = { this, g_Config.m_##Name, Len }; \
Register(#ScriptName, "?r", Flags, StrVariableCommand, &Data, Desc); \
}
#include "config_variables.h"
#undef MACRO_CONFIG_INT
#undef MACRO_CONFIG_STR
}
void CConsole::ParseArguments(int NumArgs, const char **ppArguments)
{
for(int i = 0; i < NumArgs; i++)
{
// check for scripts to execute
if(ppArguments[i][0] == '-' && ppArguments[i][1] == 'f' && ppArguments[i][2] == 0)
{
if(NumArgs - i > 1)
ExecuteFile(ppArguments[i+1]);
i++;
}
else if(!str_comp("-s", ppArguments[i]) || !str_comp("--silent", ppArguments[i]))
{
// skip silent param
continue;
}
else
{
// search arguments for overrides
ExecuteLine(ppArguments[i], -1, false);
}
}
}
void CConsole::AddCommandSorted(CCommand *pCommand)
{
if(!m_pFirstCommand || str_comp(pCommand->m_pName, m_pFirstCommand->m_pName) <= 0)
{
if(m_pFirstCommand && m_pFirstCommand->m_pNext)
pCommand->m_pNext = m_pFirstCommand;
else
pCommand->m_pNext = 0;
m_pFirstCommand = pCommand;
}
else
{
for(CCommand *p = m_pFirstCommand; p; p = p->m_pNext)
{
if(!p->m_pNext || str_comp(pCommand->m_pName, p->m_pNext->m_pName) <= 0)
{
pCommand->m_pNext = p->m_pNext;
p->m_pNext = pCommand;
break;
}
}
}
}
void CConsole::GenerateUsage(const char* pParam, char* pUsage)
{
while(*pParam)
{
char ParamType = *pParam;
pParam++;
if (*pParam == '<')
{
if(ParamType == 'i')
{
pParam++;
for (; *pParam != '>'; pParam++)
{
if (*pParam)
{
*pUsage = *pParam;
pUsage++;
}
else
return;
}
pParam++;
*pUsage = ' ';
pUsage++;
}
else
{
*pUsage = '"';
pUsage++;
pParam++;
for (; *pParam != '>'; pParam++)
{
if (*pParam)
{
*pUsage = *pParam;
pUsage++;
}
else
{
*pUsage = '"';
return;
}
}
pParam++;
*pUsage = '"';
pUsage++;
*pUsage = ' ';
pUsage++;
}
// skip space if there is one
if (*pParam == ' ')
pParam++;
}
else if(ParamType == 's')
{
str_copy(pUsage, "\"text\" ", sizeof("\"text\" "));
pUsage += sizeof("\"text\" ");
}
else if(ParamType == 'r')
{
str_copy(pUsage, "\"text...\" ", sizeof("\"text...\" "));
pUsage += sizeof("\"text...\" ");
}
else if(ParamType == 'i')
{
str_copy(pUsage, "number ", sizeof("number "));
pUsage += sizeof("number ");
}
else if(ParamType == '?')
{
*pUsage = '?';
pUsage++;
}
}
*pUsage = 0;
}
void CConsole::Register(const char *pName, const char *pParams,
int Flags, FCommandCallback pfnFunc, void *pUser, const char *pHelp)
{
CCommand *pCommand = FindCommand(pName, Flags);
bool DoAdd = false;
if(pCommand == 0)
{
pCommand = new(mem_alloc(sizeof(CCommand), sizeof(void*))) CCommand;
DoAdd = true;
}
pCommand->m_pfnCallback = pfnFunc;
pCommand->m_pUserData = pUser;
pCommand->m_pName = pName;
GenerateUsage(pParams, pCommand->m_pUsage);
pCommand->m_pHelp = pHelp;
pCommand->m_pParams = pParams;
pCommand->m_Flags = Flags;
pCommand->m_Temp = false;
if(pCommand->m_Flags&CFGFLAG_USER)
pCommand->SetAccessLevel(ACCESS_LEVEL_USER);
if(DoAdd)
AddCommandSorted(pCommand);
}
void CConsole::RegisterTemp(const char *pName, const char *pParams, int Flags, const char *pHelp)
{
CCommand *pCommand;
if(m_pRecycleList)
{
pCommand = m_pRecycleList;
str_copy(const_cast<char *>(pCommand->m_pName), pName, TEMPCMD_NAME_LENGTH);
GenerateUsage(pParams, pCommand->m_pUsage);
str_copy(const_cast<char *>(pCommand->m_pHelp), pHelp, TEMPCMD_HELP_LENGTH);
str_copy(const_cast<char *>(pCommand->m_pParams), pParams, TEMPCMD_PARAMS_LENGTH);
m_pRecycleList = m_pRecycleList->m_pNext;
}
else
{
pCommand = new(m_TempCommands.Allocate(sizeof(CCommand))) CCommand;
char *pMem = static_cast<char *>(m_TempCommands.Allocate(TEMPCMD_NAME_LENGTH));
str_copy(pMem, pName, TEMPCMD_NAME_LENGTH);
pCommand->m_pName = pMem;
GenerateUsage(pParams, pCommand->m_pUsage);
pMem = static_cast<char *>(m_TempCommands.Allocate(TEMPCMD_HELP_LENGTH));
str_copy(pMem, pHelp, TEMPCMD_HELP_LENGTH);
pCommand->m_pHelp = pMem;
pMem = static_cast<char *>(m_TempCommands.Allocate(TEMPCMD_PARAMS_LENGTH));
str_copy(pMem, pParams, TEMPCMD_PARAMS_LENGTH);
pCommand->m_pParams = pMem;
}
pCommand->m_pfnCallback = 0;
pCommand->m_pUserData = 0;
pCommand->m_Flags = Flags;
pCommand->m_Temp = true;
AddCommandSorted(pCommand);
}
void CConsole::DeregisterTemp(const char *pName)
{
if(!m_pFirstCommand)
return;
CCommand *pRemoved = 0;
// remove temp entry from command list
if(m_pFirstCommand->m_Temp && str_comp(m_pFirstCommand->m_pName, pName) == 0)
{
pRemoved = m_pFirstCommand;
m_pFirstCommand = m_pFirstCommand->m_pNext;
}
else
{
for(CCommand *pCommand = m_pFirstCommand; pCommand->m_pNext; pCommand = pCommand->m_pNext)
if(pCommand->m_pNext->m_Temp && str_comp(pCommand->m_pNext->m_pName, pName) == 0)
{
pRemoved = pCommand->m_pNext;
pCommand->m_pNext = pCommand->m_pNext->m_pNext;
break;
}
}
// add to recycle list
if(pRemoved)
{
pRemoved->m_pNext = m_pRecycleList;
m_pRecycleList = pRemoved;
}
}
void CConsole::DeregisterTempAll()
{
// set non temp as first one
for(; m_pFirstCommand && m_pFirstCommand->m_Temp; m_pFirstCommand = m_pFirstCommand->m_pNext);
// remove temp entries from command list
for(CCommand *pCommand = m_pFirstCommand; pCommand && pCommand->m_pNext; pCommand = pCommand->m_pNext)
{
CCommand *pNext = pCommand->m_pNext;
if(pNext->m_Temp)
{
for(; pNext && pNext->m_Temp; pNext = pNext->m_pNext);
pCommand->m_pNext = pNext;
}
}
m_TempCommands.Reset();
m_pRecycleList = 0;
}
bool CConsole::Con_Chain(IResult *pResult, void *pUserData)
{
CChain *pInfo = (CChain *)pUserData;
pInfo->m_pfnChainCallback(pResult, pInfo->m_pUserData, pInfo->m_pfnCallback, pInfo->m_pCallbackUserData);