1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3
4#include "console.h"
5
6#include "config.h"
7#include "linereader.h"
8
9#include <base/color.h>
10#include <base/dbg.h>
11#include <base/io.h>
12#include <base/log.h>
13#include <base/log_color.h>
14#include <base/math.h>
15#include <base/mem.h>
16#include <base/str.h>
17
18#include <engine/client/checksum.h>
19#include <engine/console.h>
20#include <engine/shared/protocol.h>
21#include <engine/storage.h>
22
23#include <algorithm>
24#include <iterator> // std::size
25#include <new>
26
27// todo: rework this
28
29CConsole::CResult::CResult(int ClientId) :
30 IResult(ClientId)
31{
32 mem_zero(block: m_aStringStorage, size: sizeof(m_aStringStorage));
33 m_pArgsStart = nullptr;
34 m_pCommand = nullptr;
35 std::fill(first: std::begin(arr&: m_apArgs), last: std::end(arr&: m_apArgs), value: nullptr);
36}
37
38CConsole::CResult::CResult(const CResult &Other) :
39 IResult(Other),
40 m_vVictims(Other.m_vVictims)
41{
42 mem_copy(dest: m_aStringStorage, source: Other.m_aStringStorage, size: sizeof(m_aStringStorage));
43 m_pArgsStart = m_aStringStorage + (Other.m_pArgsStart - Other.m_aStringStorage);
44 m_pCommand = m_aStringStorage + (Other.m_pCommand - Other.m_aStringStorage);
45 for(unsigned i = 0; i < Other.m_NumArgs; ++i)
46 m_apArgs[i] = m_aStringStorage + (Other.m_apArgs[i] - Other.m_aStringStorage);
47}
48
49void CConsole::CResult::AddArgument(const char *pArg)
50{
51 m_apArgs[m_NumArgs++] = pArg;
52}
53
54void CConsole::CResult::RemoveArgument(unsigned Index)
55{
56 dbg_assert(Index < m_NumArgs, "invalid argument index");
57 for(unsigned i = Index; i < m_NumArgs - 1; i++)
58 m_apArgs[i] = m_apArgs[i + 1];
59
60 m_apArgs[m_NumArgs--] = nullptr;
61}
62
63const char *CConsole::CResult::GetString(unsigned Index) const
64{
65 if(Index >= m_NumArgs)
66 return "";
67 return m_apArgs[Index];
68}
69
70int CConsole::CResult::GetInteger(unsigned Index) const
71{
72 if(Index >= m_NumArgs)
73 return 0;
74 int Out;
75 return str_toint(str: m_apArgs[Index], out: &Out) ? Out : 0;
76}
77
78float CConsole::CResult::GetFloat(unsigned Index) const
79{
80 if(Index >= m_NumArgs)
81 return 0.0f;
82 float Out;
83 return str_tofloat(str: m_apArgs[Index], out: &Out) ? Out : 0.0f;
84}
85
86ColorHSLA CConsole::CResult::GetColor(unsigned Index, float DarkestLighting) const
87{
88 if(Index >= m_NumArgs)
89 return ColorHSLA(0, 0, 0);
90 return ColorParse(pStr: m_apArgs[Index], DarkestLighting).value_or(u: ColorHSLA(0, 0, 0));
91}
92
93bool CConsole::CCommand::TakesClientId() const
94{
95 const char *pFormat = m_pParams;
96 for(char Param = *pFormat; Param != '\0'; Param = NextParam(pFormat))
97 {
98 if(Param == 'v')
99 return true;
100 }
101 return false;
102}
103
104void CConsole::CCommand::SetAccessLevel(EAccessLevel AccessLevel)
105{
106 m_AccessLevel = AccessLevel;
107}
108
109const IConsole::ICommandInfo *CConsole::FirstCommandInfo(int ClientId, int FlagMask) const
110{
111 for(const CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->Next())
112 {
113 if(pCommand->m_Flags & FlagMask && CanUseCommand(ClientId, pCommand))
114 return pCommand;
115 }
116
117 return nullptr;
118}
119
120const IConsole::ICommandInfo *CConsole::NextCommandInfo(const IConsole::ICommandInfo *pInfo, int ClientId, int FlagMask) const
121{
122 const CCommand *pNext = ((CCommand *)pInfo)->Next();
123 while(pNext)
124 {
125 if(pNext->m_Flags & FlagMask && CanUseCommand(ClientId, pCommand: pNext))
126 break;
127 pNext = pNext->Next();
128 }
129 return pNext;
130}
131
132std::optional<CConsole::EAccessLevel> CConsole::AccessLevelToEnum(const char *pAccessLevel)
133{
134 // alias for legacy integer access levels
135 if(!str_comp(a: pAccessLevel, b: "0"))
136 return EAccessLevel::ADMIN;
137 if(!str_comp(a: pAccessLevel, b: "1"))
138 return EAccessLevel::MODERATOR;
139 if(!str_comp(a: pAccessLevel, b: "2"))
140 return EAccessLevel::HELPER;
141 if(!str_comp(a: pAccessLevel, b: "3"))
142 return EAccessLevel::USER;
143
144 // string access levels
145 if(!str_comp(a: pAccessLevel, b: "admin"))
146 return EAccessLevel::ADMIN;
147 if(!str_comp(a: pAccessLevel, b: "moderator"))
148 return EAccessLevel::MODERATOR;
149 if(!str_comp(a: pAccessLevel, b: "helper"))
150 return EAccessLevel::HELPER;
151 if(!str_comp(a: pAccessLevel, b: "all"))
152 return EAccessLevel::USER;
153 return std::nullopt;
154}
155
156const char *CConsole::AccessLevelToString(EAccessLevel AccessLevel)
157{
158 switch(AccessLevel)
159 {
160 case EAccessLevel::ADMIN:
161 return "admin";
162 case EAccessLevel::MODERATOR:
163 return "moderator";
164 case EAccessLevel::HELPER:
165 return "helper";
166 case EAccessLevel::USER:
167 return "all";
168 }
169 dbg_assert_failed("invalid access level: %d", (int)AccessLevel);
170}
171
172// the maximum number of tokens occurs in a string of length CONSOLE_MAX_STR_LENGTH with tokens size 1 separated by single spaces
173
174int CConsole::ParseStart(CResult *pResult, const char *pString, int Length)
175{
176 char *pStr;
177 int Len = sizeof(pResult->m_aStringStorage);
178 if(Length < Len)
179 Len = Length;
180
181 str_copy(dst: pResult->m_aStringStorage, src: pString, dst_size: Len);
182 pStr = pResult->m_aStringStorage;
183
184 // get command
185 pStr = str_skip_whitespaces(str: pStr);
186 pResult->m_pCommand = pStr;
187 pStr = str_skip_to_whitespace(str: pStr);
188
189 if(*pStr)
190 {
191 pStr[0] = 0;
192 pStr++;
193 }
194
195 pResult->m_pArgsStart = pStr;
196 return 0;
197}
198
199int CConsole::ParseArgs(CResult *pResult, const char *pFormat)
200{
201 char *pStr = pResult->m_pArgsStart;
202 bool Optional = false;
203
204 pResult->m_vVictims.clear();
205
206 for(char Command = *pFormat; Command != '\0'; Command = NextParam(pFormat))
207 {
208 if(Command == '?')
209 {
210 Optional = true;
211 continue;
212 }
213
214 pStr = str_skip_whitespaces(str: pStr);
215
216 if(*pStr == '\0') // error, non optional command needs value
217 {
218 if(!Optional)
219 {
220 return PARSEARGS_MISSING_VALUE;
221 }
222
223 while(Command)
224 {
225 if(Command == 'v')
226 {
227 // If the victim argument is optional we default to "me"
228 pResult->AddVictim(pVictim: "me");
229 break;
230 }
231 Command = NextParam(pFormat);
232 }
233 return PARSEARGS_OK;
234 }
235
236 // add token
237 if(*pStr == '"')
238 {
239 pStr++;
240 pResult->AddArgument(pArg: pStr);
241
242 char *pDst = pStr; // we might have to process escape data
243 while(pStr[0] != '"')
244 {
245 if(pStr[0] == '\\')
246 {
247 if(pStr[1] == '\\')
248 pStr++; // skip due to escape
249 else if(pStr[1] == '"')
250 pStr++; // skip due to escape
251 }
252 else if(pStr[0] == '\0')
253 {
254 return PARSEARGS_MISSING_VALUE; // return error
255 }
256
257 *pDst = *pStr;
258 pDst++;
259 pStr++;
260 }
261 *pDst = '\0';
262
263 pStr++;
264 }
265 else
266 {
267 pResult->AddArgument(pArg: pStr);
268
269 if(Command == 'r') // rest of the string
270 {
271 return PARSEARGS_OK;
272 }
273
274 pStr = str_skip_to_whitespace(str: pStr);
275 if(pStr[0] != '\0') // check for end of string
276 {
277 pStr[0] = '\0';
278 pStr++;
279 }
280 }
281
282 // validate arguments
283 if(Command == 'v')
284 {
285 const char *pVictim = pResult->GetString(Index: pResult->NumArguments() - 1);
286 if(pVictim[0] == '\0')
287 {
288 return PARSEARGS_MISSING_VALUE;
289 }
290 pResult->AddVictim(pVictim);
291 }
292 else if(Command == 'i')
293 {
294 int Value;
295 if(!str_toint(str: pResult->GetString(Index: pResult->NumArguments() - 1), out: &Value) ||
296 Value == std::numeric_limits<int>::max() ||
297 Value == std::numeric_limits<int>::min())
298 {
299 return PARSEARGS_INVALID_INTEGER;
300 }
301 }
302 else if(Command == 'c')
303 {
304 auto Color = ColorParse(pStr: pResult->GetString(Index: pResult->NumArguments() - 1), DarkestLighting: 0.0f);
305 if(!Color.has_value())
306 {
307 return PARSEARGS_INVALID_COLOR;
308 }
309 }
310 else if(Command == 'f')
311 {
312 float Value;
313 if(!str_tofloat(str: pResult->GetString(Index: pResult->NumArguments() - 1), out: &Value) ||
314 Value == std::numeric_limits<float>::max() ||
315 Value == std::numeric_limits<float>::min())
316 {
317 return PARSEARGS_INVALID_FLOAT;
318 }
319 }
320 // 's' and unknown commands are handled as strings
321 }
322
323 return PARSEARGS_OK;
324}
325
326char CConsole::NextParam(const char *&pFormat)
327{
328 if(*pFormat)
329 {
330 pFormat++;
331
332 if(*pFormat == '[')
333 {
334 // skip bracket contents
335 for(; *pFormat != ']'; pFormat++)
336 {
337 if(!*pFormat)
338 return *pFormat;
339 }
340
341 // skip ']'
342 pFormat++;
343
344 // skip space if there is one
345 if(*pFormat == ' ')
346 pFormat++;
347 }
348 }
349 return *pFormat;
350}
351
352LEVEL IConsole::ToLogLevel(int Level)
353{
354 switch(Level)
355 {
356 case IConsole::OUTPUT_LEVEL_STANDARD:
357 return LEVEL_INFO;
358 case IConsole::OUTPUT_LEVEL_ADDINFO:
359 return LEVEL_DEBUG;
360 case IConsole::OUTPUT_LEVEL_DEBUG:
361 return LEVEL_TRACE;
362 }
363 dbg_assert_failed("Invalid log level: %d", Level);
364}
365
366int IConsole::ToLogLevelFilter(int Level)
367{
368 dbg_assert(-3 <= Level && Level <= 2, "Invalid log level filter: %d", Level);
369 return Level + 2;
370}
371
372void CConsole::Print(int Level, const char *pFrom, const char *pStr, ColorRGBA PrintColor) const
373{
374 LEVEL LogLevel = IConsole::ToLogLevel(Level);
375 // if console colors are not enabled or if the color is pure white, use default terminal color
376 if(g_Config.m_ConsoleEnableColors && PrintColor != CONSOLE_DEFAULT_COLOR)
377 {
378 log_log_color(level: LogLevel, color: color_cast<LOG_COLOR>(rgb: PrintColor), sys: pFrom, fmt: "%s", pStr);
379 }
380 else
381 {
382 log_log(level: LogLevel, sys: pFrom, fmt: "%s", pStr);
383 }
384}
385
386void CConsole::SetGetVictimsCommandCallback(FGetVictimsCommandCallback pfnCallback, void *pUser)
387{
388 m_pfnGetVictimsCommandCallback = pfnCallback;
389 m_pGetVictimsCommandUserData = pUser;
390}
391
392void CConsole::SetTeeHistorianCommandCallback(FTeeHistorianCommandCallback pfnCallback, void *pUser)
393{
394 m_pfnTeeHistorianCommandCallback = pfnCallback;
395 m_pTeeHistorianCommandUserdata = pUser;
396}
397
398void CConsole::SetUnknownCommandCallback(FUnknownCommandCallback pfnCallback, void *pUser)
399{
400 m_pfnUnknownCommandCallback = pfnCallback;
401 m_pUnknownCommandUserdata = pUser;
402}
403
404void CConsole::SetCanUseCommandCallback(FCanUseCommandCallback pfnCallback, void *pUser)
405{
406 m_pfnCanUseCommandCallback = pfnCallback;
407 m_pCanUseCommandUserData = pUser;
408}
409
410void CConsole::InitChecksum(CChecksumData *pData) const
411{
412 pData->m_NumCommands = 0;
413 for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->Next())
414 {
415 if(pData->m_NumCommands < (int)(std::size(pData->m_aCommandsChecksum)))
416 {
417 FCommandCallback pfnCallback = pCommand->m_pfnCallback;
418 void *pUserData = pCommand->m_pUserData;
419 TraverseChain(ppfnCallback: &pfnCallback, ppUserData: &pUserData);
420 int CallbackBits = (uintptr_t)pfnCallback & 0xfff;
421 int *pTarget = &pData->m_aCommandsChecksum[pData->m_NumCommands];
422 *pTarget = ((uint8_t)pCommand->m_pName[0]) | ((uint8_t)pCommand->m_pName[1] << 8) | (CallbackBits << 16);
423 }
424 pData->m_NumCommands += 1;
425 }
426}
427
428bool CConsole::LineIsValid(const char *pStr)
429{
430 if(!pStr || *pStr == 0)
431 return false;
432
433 do
434 {
435 CResult Result(IConsole::CLIENT_ID_UNSPECIFIED);
436 const char *pEnd = pStr;
437 const char *pNextPart = nullptr;
438 bool InString = false;
439 bool IsEscaping = false;
440
441 while(*pEnd)
442 {
443 if(IsEscaping)
444 {
445 IsEscaping = false;
446 }
447 else if(*pEnd == '"')
448 {
449 InString = !InString;
450 }
451 else if(InString && *pEnd == '\\') // escape sequences
452 {
453 IsEscaping = true;
454 }
455
456 if(!InString)
457 {
458 if(*pEnd == ';') // command separator
459 {
460 pNextPart = pEnd + 1;
461 break;
462 }
463 else if(*pEnd == '#') // comment, no need to do anything more
464 {
465 break;
466 }
467 }
468
469 pEnd++;
470 }
471
472 if(ParseStart(pResult: &Result, pString: pStr, Length: (pEnd - pStr) + 1) != 0)
473 return false;
474
475 CCommand *pCommand = FindCommand(pName: Result.m_pCommand, FlagMask: m_FlagMask);
476 if(!pCommand || ParseArgs(pResult: &Result, pFormat: pCommand->m_pParams))
477 return false;
478
479 pStr = pNextPart;
480 } while(pStr && *pStr);
481
482 return true;
483}
484
485void CConsole::ExecuteLineStroked(int Stroke, const char *pStr, int ClientId, bool InterpretSemicolons)
486{
487 const char *pWithoutPrefix = str_startswith(str: pStr, prefix: "mc;");
488 if(pWithoutPrefix)
489 {
490 InterpretSemicolons = true;
491 pStr = pWithoutPrefix;
492 }
493 while(pStr && *pStr)
494 {
495 CResult Result(ClientId);
496 const char *pEnd = pStr;
497 const char *pNextPart = nullptr;
498 bool InString = false;
499 bool IsEscaping = false;
500
501 while(*pEnd)
502 {
503 if(IsEscaping)
504 {
505 IsEscaping = false;
506 }
507 else if(*pEnd == '"')
508 {
509 InString = !InString;
510 }
511 else if(InString && *pEnd == '\\') // escape sequences
512 {
513 IsEscaping = true;
514 }
515
516 if(!InString && InterpretSemicolons)
517 {
518 if(*pEnd == ';') // command separator
519 {
520 pNextPart = pEnd + 1;
521 break;
522 }
523 else if(*pEnd == '#') // comment, no need to do anything more
524 {
525 break;
526 }
527 }
528
529 pEnd++;
530 }
531
532 if(ParseStart(pResult: &Result, pString: pStr, Length: (pEnd - pStr) + 1) != 0)
533 return;
534
535 if(!*Result.m_pCommand)
536 {
537 if(pNextPart)
538 {
539 pStr = pNextPart;
540 continue;
541 }
542 return;
543 }
544
545 CCommand *pCommand;
546 if(ClientId == IConsole::CLIENT_ID_GAME)
547 pCommand = FindCommand(pName: Result.m_pCommand, FlagMask: m_FlagMask | CFGFLAG_GAME);
548 else
549 pCommand = FindCommand(pName: Result.m_pCommand, FlagMask: m_FlagMask);
550
551 if(pCommand)
552 {
553 if(ClientId == IConsole::CLIENT_ID_GAME && !(pCommand->m_Flags & CFGFLAG_GAME))
554 {
555 if(Stroke)
556 {
557 char aBuf[CMDLINE_LENGTH + 64];
558 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Command '%s' cannot be executed from a map.", Result.m_pCommand);
559 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
560 }
561 }
562 else if(ClientId == IConsole::CLIENT_ID_NO_GAME && pCommand->m_Flags & CFGFLAG_GAME)
563 {
564 if(Stroke)
565 {
566 char aBuf[CMDLINE_LENGTH + 64];
567 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Command '%s' cannot be executed from a non-map config file.", Result.m_pCommand);
568 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
569 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Hint: Put the command in '%s.cfg' instead of '%s.map.cfg' ", g_Config.m_SvMap, g_Config.m_SvMap);
570 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
571 }
572 }
573 else if(CanUseCommand(ClientId: Result.m_ClientId, pCommand))
574 {
575 int IsStrokeCommand = 0;
576 if(Result.m_pCommand[0] == '+')
577 {
578 // insert the stroke direction token
579 Result.AddArgument(pArg: m_apStrokeStr[Stroke]);
580 IsStrokeCommand = 1;
581 }
582
583 if(Stroke || IsStrokeCommand)
584 {
585 if(int Error = ParseArgs(pResult: &Result, pFormat: pCommand->m_pParams))
586 {
587 char aBuf[CMDLINE_LENGTH + 64];
588 if(Error == PARSEARGS_INVALID_INTEGER)
589 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s is not a valid integer.", Result.GetString(Index: Result.NumArguments() - 1));
590 else if(Error == PARSEARGS_INVALID_COLOR)
591 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s is not a valid color.", Result.GetString(Index: Result.NumArguments() - 1));
592 else if(Error == PARSEARGS_INVALID_FLOAT)
593 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s is not a valid decimal number.", Result.GetString(Index: Result.NumArguments() - 1));
594 else
595 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Invalid arguments. Usage: %s %s", pCommand->m_pName, pCommand->m_pParams);
596 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "chatresp", pStr: aBuf);
597 }
598 else if(m_StoreCommands && pCommand->m_Flags & CFGFLAG_STORE)
599 {
600 m_vExecutionQueue.emplace_back(args&: pCommand, args&: Result);
601 }
602 else
603 {
604 if(pCommand->m_Flags & CMDFLAG_TEST && !g_Config.m_SvTestingCommands)
605 {
606 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: "Test commands aren't allowed, enable them with 'sv_test_cmds 1' in your initial config.");
607 return;
608 }
609
610 if(m_pfnTeeHistorianCommandCallback && !(pCommand->m_Flags & CFGFLAG_NONTEEHISTORIC))
611 {
612 m_pfnTeeHistorianCommandCallback(ClientId, m_FlagMask, pCommand->m_pName, &Result, m_pTeeHistorianCommandUserdata);
613 }
614
615 int FanOutSlot = -1;
616 std::vector<int> vFanOutIds;
617 for(unsigned Slot = 0; Slot < Result.m_vVictims.size(); Slot++)
618 {
619 if(!Result.m_vVictims[Slot].m_aSpecialVictim[0])
620 continue;
621 std::optional<std::vector<int>> Victims;
622 if(m_pfnGetVictimsCommandCallback)
623 Victims = m_pfnGetVictimsCommandCallback(ClientId, Result.m_vVictims[Slot].m_aSpecialVictim, m_pGetVictimsCommandUserData);
624 if(!Victims.has_value())
625 {
626 log_error("console", "Invalid victim '%s'", Result.m_vVictims[Slot].m_aSpecialVictim);
627 return;
628 }
629 if(Victims->size() != 1 && FanOutSlot >= 0)
630 {
631 log_error("console", "Only one parameter may target multiple clients");
632 return;
633 }
634 if(Victims->size() == 1)
635 Result.SetVictim(Slot, Victim: Victims->front());
636 else
637 {
638 FanOutSlot = Slot;
639 vFanOutIds = std::move(Victims.value());
640 }
641 }
642
643 if(FanOutSlot < 0)
644 {
645 pCommand->m_pfnCallback(&Result, pCommand->m_pUserData);
646 }
647 else
648 {
649 for(const int VictimId : vFanOutIds)
650 {
651 Result.SetVictim(Slot: FanOutSlot, Victim: VictimId);
652 pCommand->m_pfnCallback(&Result, pCommand->m_pUserData);
653 }
654 }
655
656 if(pCommand->m_Flags & CMDFLAG_TEST)
657 m_Cheated = true;
658 }
659 }
660 }
661 else if(Stroke)
662 {
663 char aBuf[CMDLINE_LENGTH + 32];
664 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Access for command %s denied.", Result.m_pCommand);
665 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
666 }
667 }
668 else if(Stroke)
669 {
670 // Pass the original string to the unknown command callback instead of the parsed command, as the latter
671 // ends at the first whitespace, which breaks for unknown commands (filenames) containing spaces.
672 if(!m_pfnUnknownCommandCallback(pStr, m_pUnknownCommandUserdata))
673 {
674 char aBuf[CMDLINE_LENGTH + 32];
675 if(m_FlagMask & CFGFLAG_CHAT)
676 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "No such command: %s. Use /cmdlist for a list of all commands.", Result.m_pCommand);
677 else
678 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "No such command: %s.", Result.m_pCommand);
679 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "chatresp", pStr: aBuf);
680 }
681 }
682
683 pStr = pNextPart;
684 }
685}
686
687bool CConsole::CanUseCommand(int ClientId, const IConsole::ICommandInfo *pCommand) const
688{
689 // config files, econ, fifo and passed votes have full access
690 if(ClientId == IConsole::CLIENT_ID_UNSPECIFIED || ClientId == IConsole::CLIENT_ID_GAME || ClientId == IConsole::CLIENT_ID_NO_GAME)
691 return true;
692 dbg_assert(ClientId >= 0, "Invalid ClientId: %d", ClientId);
693 // the fallback is needed for the client and rust tests
694 if(!m_pfnCanUseCommandCallback)
695 return true;
696 return m_pfnCanUseCommandCallback(ClientId, pCommand, m_pCanUseCommandUserData);
697}
698
699int CConsole::PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser)
700{
701 int Index = 0;
702 for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->Next())
703 {
704 if(pCommand->m_Flags & FlagMask && pCommand->m_Temp == Temp)
705 {
706 if(str_find_nocase(haystack: pCommand->m_pName, needle: pStr))
707 {
708 pfnCallback(Index, pCommand->m_pName, pUser);
709 Index++;
710 }
711 }
712 }
713 return Index;
714}
715
716CConsole::CCommand *CConsole::FindCommand(const char *pName, int FlagMask)
717{
718 for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->Next())
719 {
720 if(pCommand->m_Flags & FlagMask)
721 {
722 if(str_comp_nocase(a: pCommand->m_pName, b: pName) == 0)
723 return pCommand;
724 }
725 }
726
727 return nullptr;
728}
729
730void CConsole::ExecuteLine(const char *pStr, int ClientId, bool InterpretSemicolons)
731{
732 CConsole::ExecuteLineStroked(Stroke: 1, pStr, ClientId, InterpretSemicolons); // press it
733 CConsole::ExecuteLineStroked(Stroke: 0, pStr, ClientId, InterpretSemicolons); // then release it
734}
735
736void CConsole::ExecuteLineFlag(const char *pStr, int FlagMask, int ClientId, bool InterpretSemicolons)
737{
738 int Temp = m_FlagMask;
739 m_FlagMask = FlagMask;
740 ExecuteLine(pStr, ClientId, InterpretSemicolons);
741 m_FlagMask = Temp;
742}
743
744bool CConsole::ExecuteFile(const char *pFilename, int ClientId, bool LogFailure, int StorageType)
745{
746 int Count = 0;
747 // make sure that this isn't being executed already and that recursion limit isn't met
748 for(CExecFile *pCur = m_pFirstExec; pCur; pCur = pCur->m_pPrev)
749 {
750 Count++;
751
752 if(str_comp(a: pFilename, b: pCur->m_pFilename) == 0 || Count > FILE_RECURSION_LIMIT)
753 return false;
754 }
755 if(!m_pStorage)
756 {
757 log_error("console", "Storage is not initialized");
758 return false;
759 }
760
761 // push this one to the stack
762 CExecFile ThisFile;
763 CExecFile *pPrev = m_pFirstExec;
764 ThisFile.m_pFilename = pFilename;
765 ThisFile.m_pPrev = m_pFirstExec;
766 m_pFirstExec = &ThisFile;
767
768 // exec the file
769 CLineReader LineReader;
770 bool Success = false;
771 if(LineReader.OpenFile(File: m_pStorage->OpenFile(pFilename, Flags: IOFLAG_READ, Type: StorageType)))
772 {
773 log_info("console", "executing '%s'", pFilename);
774
775 while(const char *pLine = LineReader.Get())
776 {
777 ExecuteLine(pStr: pLine, ClientId);
778 }
779
780 Success = true;
781 }
782 else if(LogFailure)
783 {
784 log_error("console", "failed to open '%s'", pFilename);
785 }
786
787 m_pFirstExec = pPrev;
788 return Success;
789}
790
791void CConsole::Con_Echo(IResult *pResult, void *pUserData)
792{
793 log_info("console", "%s", pResult->GetString(0));
794}
795
796void CConsole::Con_Exec(IResult *pResult, void *pUserData)
797{
798 ((CConsole *)pUserData)->ExecuteFile(pFilename: pResult->GetString(Index: 0), ClientId: pResult->m_ClientId, LogFailure: true, StorageType: IStorage::TYPE_ALL);
799}
800
801void CConsole::ConCommandAccess(IResult *pResult, void *pUser)
802{
803 CConsole *pConsole = static_cast<CConsole *>(pUser);
804 char aBuf[CMDLINE_LENGTH + 64];
805 CCommand *pCommand = pConsole->FindCommand(pName: pResult->GetString(Index: 0), FlagMask: CFGFLAG_SERVER);
806 if(pCommand)
807 {
808 if(pResult->NumArguments() == 2)
809 {
810 std::optional<EAccessLevel> AccessLevel = AccessLevelToEnum(pAccessLevel: pResult->GetString(Index: 1));
811 if(!AccessLevel.has_value())
812 {
813 log_error("console", "Invalid access level '%s'. Allowed values are admin, moderator, helper and all.", pResult->GetString(1));
814 return;
815 }
816 pCommand->SetAccessLevel(AccessLevel.value());
817 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "moderator access for '%s' is now %s", pResult->GetString(Index: 0), pCommand->GetAccessLevel() >= EAccessLevel::MODERATOR ? "enabled" : "disabled");
818 pConsole->Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
819 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "helper access for '%s' is now %s", pResult->GetString(Index: 0), pCommand->GetAccessLevel() >= EAccessLevel::HELPER ? "enabled" : "disabled");
820 pConsole->Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
821 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "user access for '%s' is now %s", pResult->GetString(Index: 0), pCommand->GetAccessLevel() >= EAccessLevel::USER ? "enabled" : "disabled");
822 }
823 else
824 {
825 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "moderator access for '%s' is %s", pResult->GetString(Index: 0), pCommand->GetAccessLevel() >= EAccessLevel::MODERATOR ? "enabled" : "disabled");
826 pConsole->Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
827 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "helper access for '%s' is %s", pResult->GetString(Index: 0), pCommand->GetAccessLevel() >= EAccessLevel::HELPER ? "enabled" : "disabled");
828 pConsole->Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
829 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "user access for '%s' is %s", pResult->GetString(Index: 0), pCommand->GetAccessLevel() >= EAccessLevel::USER ? "enabled" : "disabled");
830 }
831 }
832 else
833 {
834 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "No such command: '%s'.", pResult->GetString(Index: 0));
835 }
836
837 pConsole->Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
838}
839
840void CConsole::PrintCommandList(EAccessLevel MinAccessLevel, int ExcludeFlagMask)
841{
842 char aBuf[240] = "";
843 int Used = 0;
844
845 for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->Next())
846 {
847 if((pCommand->m_Flags & m_FlagMask) &&
848 !(pCommand->m_Flags & ExcludeFlagMask) &&
849 pCommand->GetAccessLevel() >= MinAccessLevel)
850 {
851 int Length = str_length(str: pCommand->m_pName);
852 if(Used + Length + 2 < (int)(sizeof(aBuf)))
853 {
854 if(Used > 0)
855 {
856 Used += 2;
857 str_append(dst&: aBuf, src: ", ");
858 }
859 str_append(dst&: aBuf, src: pCommand->m_pName);
860 Used += Length;
861 }
862 else
863 {
864 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "chatresp", pStr: aBuf);
865 str_copy(dst&: aBuf, src: pCommand->m_pName);
866 Used = Length;
867 }
868 }
869 }
870 if(Used > 0)
871 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "chatresp", pStr: aBuf);
872}
873
874void CConsole::ConCommandStatus(IResult *pResult, void *pUser)
875{
876 CConsole *pConsole = static_cast<CConsole *>(pUser);
877 std::optional<EAccessLevel> AccessLevel = AccessLevelToEnum(pAccessLevel: pResult->GetString(Index: 0));
878 if(!AccessLevel.has_value())
879 {
880 log_error("console", "Invalid access level '%s'. Allowed values are admin, moderator, helper and all.", pResult->GetString(0));
881 return;
882 }
883 pConsole->PrintCommandList(MinAccessLevel: AccessLevel.value(), ExcludeFlagMask: 0);
884}
885
886void CConsole::ConCmdlistChat(IResult *pResult, void *pUser)
887{
888 CConsole *pConsole = static_cast<CConsole *>(pUser);
889 pConsole->PrintCommandList(MinAccessLevel: EAccessLevel::USER, ExcludeFlagMask: CMDFLAG_PRACTICE);
890}
891
892void CConsole::TraverseChain(FCommandCallback *ppfnCallback, void **ppUserData)
893{
894 while(*ppfnCallback == Con_Chain)
895 {
896 CChain *pChainInfo = static_cast<CChain *>(*ppUserData);
897 *ppfnCallback = pChainInfo->m_pfnCallback;
898 *ppUserData = pChainInfo->m_pCallbackUserData;
899 }
900}
901
902CConsole::CConsole(int FlagMask)
903{
904 m_FlagMask = FlagMask;
905 m_pRecycleList = nullptr;
906 m_TempCommands.Reset();
907 m_StoreCommands = true;
908 m_apStrokeStr[0] = "0";
909 m_apStrokeStr[1] = "1";
910 m_pFirstCommand = nullptr;
911 m_pFirstExec = nullptr;
912 m_pfnTeeHistorianCommandCallback = nullptr;
913 m_pTeeHistorianCommandUserdata = nullptr;
914 m_pfnGetVictimsCommandCallback = nullptr;
915 m_pGetVictimsCommandUserData = nullptr;
916
917 m_pStorage = nullptr;
918
919 // register some basic commands
920 Register(pName: "echo", pParams: "r[text]", Flags: CFGFLAG_SERVER, pfnFunc: Con_Echo, pUser: this, pHelp: "Echo the text");
921 Register(pName: "exec", pParams: "r[file]", Flags: CFGFLAG_SERVER | CFGFLAG_CLIENT, pfnFunc: Con_Exec, pUser: this, pHelp: "Execute the specified file");
922
923 Register(pName: "access_level", pParams: "s[command] ?s['admin'|'moderator'|'helper'|'all']", Flags: CFGFLAG_SERVER, pfnFunc: ConCommandAccess, pUser: this, pHelp: "Specify command accessibility for given access level");
924 Register(pName: "access_status", pParams: "s['admin'|'moderator'|'helper'|'all']", Flags: CFGFLAG_SERVER, pfnFunc: ConCommandStatus, pUser: this, pHelp: "List all commands which are accessible for given access level");
925 Register(pName: "cmdlist", pParams: "", Flags: CFGFLAG_SERVER | CFGFLAG_CHAT, pfnFunc: ConCmdlistChat, pUser: this, pHelp: "List all commands which are accessible for users");
926
927 // DDRace
928
929 m_Cheated = false;
930}
931
932CConsole::~CConsole()
933{
934 CCommand *pCommand = m_pFirstCommand;
935 while(pCommand)
936 {
937 CCommand *pNext = pCommand->Next();
938 {
939 FCommandCallback pfnCallback = pCommand->m_pfnCallback;
940 void *pUserData = pCommand->m_pUserData;
941 CChain *pChain = nullptr;
942 while(pfnCallback == Con_Chain)
943 {
944 pChain = static_cast<CChain *>(pUserData);
945 pfnCallback = pChain->m_pfnCallback;
946 pUserData = pChain->m_pCallbackUserData;
947 delete pChain;
948 }
949 }
950 // Temp commands are on m_TempCommands heap, so don't delete them
951 if(!pCommand->m_Temp)
952 delete pCommand;
953 pCommand = pNext;
954 }
955}
956
957void CConsole::Init()
958{
959 m_pStorage = Kernel()->RequestInterface<IStorage>();
960}
961
962void CConsole::ParseArguments(int NumArgs, const char **ppArguments)
963{
964 for(int i = 0; i < NumArgs; i++)
965 {
966 // check for scripts to execute
967 if(ppArguments[i][0] == '-' && ppArguments[i][1] == 'f' && ppArguments[i][2] == 0)
968 {
969 if(NumArgs - i > 1)
970 ExecuteFile(pFilename: ppArguments[i + 1], ClientId: IConsole::CLIENT_ID_UNSPECIFIED, LogFailure: true, StorageType: IStorage::TYPE_ABSOLUTE);
971 i++;
972 }
973 else if(!str_comp(a: "-s", b: ppArguments[i]) || !str_comp(a: "--silent", b: ppArguments[i]))
974 {
975 // skip silent param
976 continue;
977 }
978 else
979 {
980 // search arguments for overrides
981 ExecuteLine(pStr: ppArguments[i]);
982 }
983 }
984}
985
986void CConsole::AddCommandSorted(CCommand *pCommand)
987{
988 if(!m_pFirstCommand || str_comp(a: pCommand->m_pName, b: m_pFirstCommand->m_pName) <= 0)
989 {
990 if(m_pFirstCommand && m_pFirstCommand->Next())
991 pCommand->SetNext(m_pFirstCommand);
992 else
993 pCommand->SetNext(nullptr);
994 m_pFirstCommand = pCommand;
995 }
996 else
997 {
998 for(CCommand *p = m_pFirstCommand; p; p = p->Next())
999 {
1000 if(!p->Next() || str_comp(a: pCommand->m_pName, b: p->Next()->m_pName) <= 0)
1001 {
1002 pCommand->SetNext(p->Next());
1003 p->SetNext(pCommand);
1004 break;
1005 }
1006 }
1007 }
1008}
1009
1010void CConsole::Register(const char *pName, const char *pParams,
1011 int Flags, FCommandCallback pfnFunc, void *pUser, const char *pHelp)
1012{
1013 CCommand *pCommand = FindCommand(pName, FlagMask: Flags);
1014 bool DoAdd = false;
1015 if(pCommand == nullptr)
1016 {
1017 pCommand = new CCommand();
1018 DoAdd = true;
1019 }
1020 pCommand->m_pfnCallback = pfnFunc;
1021 pCommand->m_pUserData = pUser;
1022
1023 pCommand->m_pName = pName;
1024 pCommand->m_pHelp = pHelp;
1025 pCommand->m_pParams = pParams;
1026
1027 pCommand->m_Flags = Flags;
1028 pCommand->m_Temp = false;
1029
1030 if(DoAdd)
1031 AddCommandSorted(pCommand);
1032
1033 if(pCommand->m_Flags & CFGFLAG_CHAT)
1034 pCommand->SetAccessLevel(EAccessLevel::USER);
1035}
1036
1037void CConsole::RegisterTemp(const char *pName, const char *pParams, int Flags, const char *pHelp)
1038{
1039 CCommand *pCommand;
1040 if(m_pRecycleList)
1041 {
1042 pCommand = m_pRecycleList;
1043 str_copy(dst: const_cast<char *>(pCommand->m_pName), src: pName, dst_size: TEMPCMD_NAME_LENGTH);
1044 str_copy(dst: const_cast<char *>(pCommand->m_pHelp), src: pHelp, dst_size: TEMPCMD_HELP_LENGTH);
1045 str_copy(dst: const_cast<char *>(pCommand->m_pParams), src: pParams, dst_size: TEMPCMD_PARAMS_LENGTH);
1046
1047 m_pRecycleList = m_pRecycleList->Next();
1048 }
1049 else
1050 {
1051 pCommand = new(m_TempCommands.Allocate(Size: sizeof(CCommand))) CCommand;
1052 char *pMem = static_cast<char *>(m_TempCommands.Allocate(Size: TEMPCMD_NAME_LENGTH));
1053 str_copy(dst: pMem, src: pName, dst_size: TEMPCMD_NAME_LENGTH);
1054 pCommand->m_pName = pMem;
1055 pMem = static_cast<char *>(m_TempCommands.Allocate(Size: TEMPCMD_HELP_LENGTH));
1056 str_copy(dst: pMem, src: pHelp, dst_size: TEMPCMD_HELP_LENGTH);
1057 pCommand->m_pHelp = pMem;
1058 pMem = static_cast<char *>(m_TempCommands.Allocate(Size: TEMPCMD_PARAMS_LENGTH));
1059 str_copy(dst: pMem, src: pParams, dst_size: TEMPCMD_PARAMS_LENGTH);
1060 pCommand->m_pParams = pMem;
1061 }
1062
1063 pCommand->m_pfnCallback = nullptr;
1064 pCommand->m_pUserData = nullptr;
1065 pCommand->m_Flags = Flags;
1066 pCommand->m_Temp = true;
1067
1068 AddCommandSorted(pCommand);
1069}
1070
1071void CConsole::DeregisterTemp(const char *pName)
1072{
1073 if(!m_pFirstCommand)
1074 return;
1075
1076 CCommand *pRemoved = nullptr;
1077
1078 // remove temp entry from command list
1079 if(m_pFirstCommand->m_Temp && str_comp(a: m_pFirstCommand->m_pName, b: pName) == 0)
1080 {
1081 pRemoved = m_pFirstCommand;
1082 m_pFirstCommand = m_pFirstCommand->Next();
1083 }
1084 else
1085 {
1086 for(CCommand *pCommand = m_pFirstCommand; pCommand->Next(); pCommand = pCommand->Next())
1087 if(pCommand->Next()->m_Temp && str_comp(a: pCommand->Next()->m_pName, b: pName) == 0)
1088 {
1089 pRemoved = pCommand->Next();
1090 pCommand->SetNext(pCommand->Next()->Next());
1091 break;
1092 }
1093 }
1094
1095 // add to recycle list
1096 if(pRemoved)
1097 {
1098 pRemoved->SetNext(m_pRecycleList);
1099 m_pRecycleList = pRemoved;
1100 }
1101}
1102
1103void CConsole::DeregisterTempAll()
1104{
1105 // set non temp as first one
1106 for(; m_pFirstCommand && m_pFirstCommand->m_Temp; m_pFirstCommand = m_pFirstCommand->Next())
1107 ;
1108
1109 // remove temp entries from command list
1110 for(CCommand *pCommand = m_pFirstCommand; pCommand && pCommand->Next(); pCommand = pCommand->Next())
1111 {
1112 CCommand *pNext = pCommand->Next();
1113 if(pNext->m_Temp)
1114 {
1115 for(; pNext && pNext->m_Temp; pNext = pNext->Next())
1116 ;
1117 pCommand->SetNext(pNext);
1118 }
1119 }
1120
1121 m_TempCommands.Reset();
1122 m_pRecycleList = nullptr;
1123}
1124
1125void CConsole::Con_Chain(IResult *pResult, void *pUserData)
1126{
1127 CChain *pInfo = (CChain *)pUserData;
1128 pInfo->m_pfnChainCallback(pResult, pInfo->m_pUserData, pInfo->m_pfnCallback, pInfo->m_pCallbackUserData);
1129}
1130
1131void CConsole::Chain(const char *pName, FChainCommandCallback pfnChainFunc, void *pUser)
1132{
1133 CCommand *pCommand = FindCommand(pName, FlagMask: m_FlagMask);
1134 dbg_assert(pCommand != nullptr, "Invalid command to chain: '%s'", pName);
1135
1136 // store info
1137 CChain *pChainInfo = new CChain();
1138 pChainInfo->m_pfnChainCallback = pfnChainFunc;
1139 pChainInfo->m_pUserData = pUser;
1140 pChainInfo->m_pfnCallback = pCommand->m_pfnCallback;
1141 pChainInfo->m_pCallbackUserData = pCommand->m_pUserData;
1142
1143 // chain
1144 pCommand->m_pfnCallback = Con_Chain;
1145 pCommand->m_pUserData = pChainInfo;
1146}
1147
1148void CConsole::StoreCommands(bool Store)
1149{
1150 if(!Store)
1151 {
1152 for(CExecutionQueueEntry &Entry : m_vExecutionQueue)
1153 {
1154 Entry.m_pCommand->m_pfnCallback(&Entry.m_Result, Entry.m_pCommand->m_pUserData);
1155 }
1156 m_vExecutionQueue.clear();
1157 }
1158 m_StoreCommands = Store;
1159}
1160
1161const IConsole::ICommandInfo *CConsole::GetCommandInfo(const char *pName, int FlagMask, bool Temp)
1162{
1163 for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->Next())
1164 {
1165 if(pCommand->m_Flags & FlagMask && pCommand->m_Temp == Temp)
1166 {
1167 if(str_comp_nocase(a: pCommand->Name(), b: pName) == 0)
1168 return pCommand;
1169 }
1170 }
1171
1172 return nullptr;
1173}
1174
1175std::unique_ptr<IConsole> CreateConsole(int FlagMask) { return std::make_unique<CConsole>(args&: FlagMask); }
1176
1177int CConsole::CResult::GetVictim(unsigned Slot) const
1178{
1179 dbg_assert(Slot < m_vVictims.size(), "victim slot %u out of range", Slot);
1180 dbg_assert(m_vVictims[Slot].m_Id.has_value(), "victim %u has no value", Slot);
1181 return m_vVictims[Slot].m_Id.value();
1182}
1183
1184void CConsole::CResult::SetVictim(unsigned Slot, int Victim)
1185{
1186 dbg_assert(Slot < m_vVictims.size(), "victim slot %u out of range", Slot);
1187 dbg_assert(in_range(Victim, 0, MAX_CLIENTS - 1), "Victim ID %d out of range [0, %d]", Victim, MAX_CLIENTS - 1);
1188 m_vVictims[Slot].m_Id = Victim;
1189}
1190
1191void CConsole::CResult::AddVictim(const char *pVictim)
1192{
1193 CVictim Victim;
1194 int Value;
1195 if(str_toint(str: pVictim, out: &Value) && in_range(a: Value, lower: 0, upper: MAX_CLIENTS - 1))
1196 Victim.m_Id = Value;
1197 else
1198 str_copy(dst&: Victim.m_aSpecialVictim, src: pVictim);
1199 m_vVictims.push_back(x: Victim);
1200}
1201
1202std::optional<ColorHSLA> CConsole::ColorParse(const char *pStr, float DarkestLighting)
1203{
1204 if(str_isallnum(str: pStr) || ((pStr[0] == '-' || pStr[0] == '+') && str_isallnum(str: pStr + 1))) // Teeworlds Color (Packed HSL)
1205 {
1206 unsigned long Value = str_toulong_base(str: pStr, base: 10);
1207 if(Value == std::numeric_limits<unsigned long>::max())
1208 return std::nullopt;
1209 return ColorHSLA(Value, true).UnclampLighting(Darkest: DarkestLighting);
1210 }
1211 else if(*pStr == '$') // Hex RGB/RGBA
1212 {
1213 auto ParsedColor = color_parse<ColorRGBA>(pStr: pStr + 1);
1214 if(ParsedColor)
1215 return color_cast<ColorHSLA>(rgb: ParsedColor.value());
1216 else
1217 return std::nullopt;
1218 }
1219 else if(!str_comp_nocase(a: pStr, b: "red"))
1220 return ColorHSLA(0.0f / 6.0f, 1.0f, 0.5f);
1221 else if(!str_comp_nocase(a: pStr, b: "yellow"))
1222 return ColorHSLA(1.0f / 6.0f, 1.0f, 0.5f);
1223 else if(!str_comp_nocase(a: pStr, b: "green"))
1224 return ColorHSLA(2.0f / 6.0f, 1.0f, 0.5f);
1225 else if(!str_comp_nocase(a: pStr, b: "cyan"))
1226 return ColorHSLA(3.0f / 6.0f, 1.0f, 0.5f);
1227 else if(!str_comp_nocase(a: pStr, b: "blue"))
1228 return ColorHSLA(4.0f / 6.0f, 1.0f, 0.5f);
1229 else if(!str_comp_nocase(a: pStr, b: "magenta"))
1230 return ColorHSLA(5.0f / 6.0f, 1.0f, 0.5f);
1231 else if(!str_comp_nocase(a: pStr, b: "white"))
1232 return ColorHSLA(0.0f, 0.0f, 1.0f);
1233 else if(!str_comp_nocase(a: pStr, b: "gray"))
1234 return ColorHSLA(0.0f, 0.0f, 0.5f);
1235 else if(!str_comp_nocase(a: pStr, b: "black"))
1236 return ColorHSLA(0.0f, 0.0f, 0.0f);
1237
1238 return std::nullopt;
1239}
1240