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 <base/lock.h>
7#include <base/logger.h>
8#include <base/math.h>
9#include <base/str.h>
10#include <base/system.h>
11
12#include <engine/console.h>
13#include <engine/engine.h>
14#include <engine/graphics.h>
15#include <engine/keys.h>
16#include <engine/shared/config.h>
17#include <engine/shared/ringbuffer.h>
18#include <engine/storage.h>
19#include <engine/textrender.h>
20
21#include <generated/client_data.h>
22
23#include <game/client/gameclient.h>
24#include <game/client/ui.h>
25#include <game/localization.h>
26#include <game/version.h>
27
28#include <iterator>
29
30static constexpr float FONT_SIZE = 10.0f;
31static constexpr float LINE_SPACING = 1.0f;
32
33class CConsoleLogger : public ILogger
34{
35 CGameConsole *m_pConsole;
36 CLock m_ConsoleMutex;
37
38public:
39 CConsoleLogger(CGameConsole *pConsole) :
40 m_pConsole(pConsole)
41 {
42 dbg_assert(pConsole != nullptr, "console pointer must not be null");
43 }
44
45 void Log(const CLogMessage *pMessage) override REQUIRES(!m_ConsoleMutex);
46 void OnConsoleDeletion() REQUIRES(!m_ConsoleMutex);
47};
48
49void CConsoleLogger::Log(const CLogMessage *pMessage)
50{
51 if(m_Filter.Filters(pMessage))
52 {
53 return;
54 }
55 ColorRGBA Color = gs_ConsoleDefaultColor;
56 if(pMessage->m_HaveColor)
57 {
58 Color.r = pMessage->m_Color.r / 255.0;
59 Color.g = pMessage->m_Color.g / 255.0;
60 Color.b = pMessage->m_Color.b / 255.0;
61 }
62 const CLockScope LockScope(m_ConsoleMutex);
63 if(m_pConsole)
64 {
65 m_pConsole->m_LocalConsole.PrintLine(pLine: pMessage->m_aLine, Len: pMessage->m_LineLength, PrintColor: Color);
66 }
67}
68
69void CConsoleLogger::OnConsoleDeletion()
70{
71 const CLockScope LockScope(m_ConsoleMutex);
72 m_pConsole = nullptr;
73}
74
75enum class EArgumentCompletionType
76{
77 NONE,
78 MAP,
79 TUNE,
80 SETTING,
81 KEY,
82};
83
84class CArgumentCompletionEntry
85{
86public:
87 EArgumentCompletionType m_Type;
88 const char *m_pCommandName;
89 int m_ArgumentIndex;
90};
91
92static const CArgumentCompletionEntry gs_aArgumentCompletionEntries[] = {
93 {.m_Type: EArgumentCompletionType::MAP, .m_pCommandName: "sv_map", .m_ArgumentIndex: 0},
94 {.m_Type: EArgumentCompletionType::MAP, .m_pCommandName: "change_map", .m_ArgumentIndex: 0},
95 {.m_Type: EArgumentCompletionType::TUNE, .m_pCommandName: "tune", .m_ArgumentIndex: 0},
96 {.m_Type: EArgumentCompletionType::TUNE, .m_pCommandName: "tune_reset", .m_ArgumentIndex: 0},
97 {.m_Type: EArgumentCompletionType::TUNE, .m_pCommandName: "toggle_tune", .m_ArgumentIndex: 0},
98 {.m_Type: EArgumentCompletionType::TUNE, .m_pCommandName: "tune_zone", .m_ArgumentIndex: 1},
99 {.m_Type: EArgumentCompletionType::SETTING, .m_pCommandName: "reset", .m_ArgumentIndex: 0},
100 {.m_Type: EArgumentCompletionType::SETTING, .m_pCommandName: "toggle", .m_ArgumentIndex: 0},
101 {.m_Type: EArgumentCompletionType::SETTING, .m_pCommandName: "access_level", .m_ArgumentIndex: 0},
102 {.m_Type: EArgumentCompletionType::SETTING, .m_pCommandName: "+toggle", .m_ArgumentIndex: 0},
103 {.m_Type: EArgumentCompletionType::KEY, .m_pCommandName: "bind", .m_ArgumentIndex: 0},
104 {.m_Type: EArgumentCompletionType::KEY, .m_pCommandName: "binds", .m_ArgumentIndex: 0},
105 {.m_Type: EArgumentCompletionType::KEY, .m_pCommandName: "unbind", .m_ArgumentIndex: 0},
106};
107
108static std::pair<EArgumentCompletionType, int> ArgumentCompletion(const char *pStr)
109{
110 const char *pCommandStart = pStr;
111 const char *pIt = pStr;
112 pIt = str_skip_to_whitespace_const(str: pIt);
113 int CommandLength = pIt - pCommandStart;
114 const char *pCommandEnd = pIt;
115
116 if(!CommandLength)
117 return {EArgumentCompletionType::NONE, -1};
118
119 pIt = str_skip_whitespaces_const(str: pIt);
120 if(pIt == pCommandEnd)
121 return {EArgumentCompletionType::NONE, -1};
122
123 for(const auto &Entry : gs_aArgumentCompletionEntries)
124 {
125 int Length = maximum(a: str_length(str: Entry.m_pCommandName), b: CommandLength);
126 if(str_comp_nocase_num(a: Entry.m_pCommandName, b: pCommandStart, num: Length) == 0)
127 {
128 int CurrentArg = 0;
129 const char *pArgStart = nullptr, *pArgEnd = nullptr;
130 while(CurrentArg < Entry.m_ArgumentIndex)
131 {
132 pArgStart = pIt;
133 pIt = str_skip_to_whitespace_const(str: pIt); // Skip argument value
134 pArgEnd = pIt;
135
136 if(!pIt[0] || pArgStart == pIt) // Check that argument is not empty
137 return {EArgumentCompletionType::NONE, -1};
138
139 pIt = str_skip_whitespaces_const(str: pIt); // Go to next argument position
140 CurrentArg++;
141 }
142 if(pIt == pArgEnd)
143 return {EArgumentCompletionType::NONE, -1}; // Check that there is at least one space after
144 return {Entry.m_Type, pIt - pStr};
145 }
146 }
147 return {EArgumentCompletionType::NONE, -1};
148}
149
150static int PossibleTunings(const char *pStr, IConsole::FPossibleCallback pfnCallback = IConsole::EmptyPossibleCommandCallback, void *pUser = nullptr)
151{
152 int Index = 0;
153 for(int i = 0; i < CTuningParams::Num(); i++)
154 {
155 if(str_find_nocase(haystack: CTuningParams::Name(Index: i), needle: pStr))
156 {
157 pfnCallback(Index, CTuningParams::Name(Index: i), pUser);
158 Index++;
159 }
160 }
161 return Index;
162}
163
164static int PossibleKeys(const char *pStr, IInput *pInput, IConsole::FPossibleCallback pfnCallback = IConsole::EmptyPossibleCommandCallback, void *pUser = nullptr)
165{
166 int Index = 0;
167 for(int Key = KEY_A; Key < KEY_JOY_AXIS_11_RIGHT; Key++)
168 {
169 // Ignore unnamed keys starting with '&'
170 const char *pKeyName = pInput->KeyName(Key);
171 if(pKeyName[0] != '&' && str_find_nocase(haystack: pKeyName, needle: pStr))
172 {
173 pfnCallback(Index, pKeyName, pUser);
174 Index++;
175 }
176 }
177 return Index;
178}
179
180static void CollectPossibleCommandsCallback(int Index, const char *pStr, void *pUser)
181{
182 ((std::vector<const char *> *)pUser)->push_back(x: pStr);
183}
184
185static void SortCompletions(std::vector<const char *> &vCompletions, const char *pSearch)
186{
187 if(pSearch[0] == '\0')
188 return;
189
190 std::sort(first: vCompletions.begin(), last: vCompletions.end(), comp: [pSearch](const char *pA, const char *pB) {
191 const char *pMatchA = str_find_nocase(haystack: pA, needle: pSearch);
192 const char *pMatchB = str_find_nocase(haystack: pB, needle: pSearch);
193 int MatchPosA = pMatchA ? (pMatchA - pA) : -1;
194 int MatchPosB = pMatchB ? (pMatchB - pB) : -1;
195
196 if(MatchPosA != MatchPosB)
197 return MatchPosA < MatchPosB;
198
199 int LenA = str_length(str: pA);
200 int LenB = str_length(str: pB);
201 if(LenA != LenB)
202 return LenA < LenB;
203
204 return str_comp_nocase(a: pA, b: pB) < 0;
205 });
206}
207
208CGameConsole::CInstance::CInstance(int Type)
209{
210 m_pHistoryEntry = nullptr;
211
212 m_Type = Type;
213
214 if(Type == CGameConsole::CONSOLETYPE_LOCAL)
215 {
216 m_pName = "local_console";
217 m_CompletionFlagmask = CFGFLAG_CLIENT;
218 }
219 else
220 {
221 m_pName = "remote_console";
222 m_CompletionFlagmask = CFGFLAG_SERVER;
223 }
224
225 m_aCompletionBuffer[0] = 0;
226 m_CompletionChosen = -1;
227 m_aCompletionBufferArgument[0] = 0;
228 m_CompletionChosenArgument = -1;
229 m_CompletionArgumentPosition = 0;
230 m_CompletionDirty = true;
231 m_QueueResetAnimation = false;
232 Reset();
233
234 m_aUser[0] = '\0';
235 m_UserGot = false;
236 m_UsernameReq = false;
237
238 m_IsCommand = false;
239
240 m_Backlog.SetPopCallback([this](CBacklogEntry *pEntry) {
241 if(pEntry->m_LineCount != -1)
242 {
243 m_NewLineCounter -= pEntry->m_LineCount;
244 for(auto &SearchMatch : m_vSearchMatches)
245 {
246 SearchMatch.m_StartLine += pEntry->m_LineCount;
247 SearchMatch.m_EndLine += pEntry->m_LineCount;
248 SearchMatch.m_EntryLine += pEntry->m_LineCount;
249 }
250 }
251 });
252
253 m_Input.SetClipboardLineCallback([this](const char *pStr) { ExecuteLine(pLine: pStr); });
254
255 m_CurrentMatchIndex = -1;
256 m_aCurrentSearchString[0] = '\0';
257}
258
259void CGameConsole::CInstance::Init(CGameConsole *pGameConsole)
260{
261 m_pGameConsole = pGameConsole;
262}
263
264void CGameConsole::CInstance::ClearBacklog()
265{
266 {
267 // We must ensure that no log messages are printed while owning
268 // m_BacklogPendingLock or this will result in a dead lock.
269 const CLockScope LockScope(m_BacklogPendingLock);
270 m_BacklogPending.Init();
271 }
272
273 m_Backlog.Init();
274 m_BacklogCurLine = 0;
275 ClearSearch();
276}
277
278void CGameConsole::CInstance::UpdateBacklogTextAttributes()
279{
280 // Pending backlog entries are not handled because they don't have text attributes yet.
281 for(CBacklogEntry *pEntry = m_Backlog.First(); pEntry; pEntry = m_Backlog.Next(pCurrent: pEntry))
282 {
283 UpdateEntryTextAttributes(pEntry);
284 }
285}
286
287void CGameConsole::CInstance::PumpBacklogPending()
288{
289 {
290 // We must ensure that no log messages are printed while owning
291 // m_BacklogPendingLock or this will result in a dead lock.
292 const CLockScope LockScopePending(m_BacklogPendingLock);
293 for(CBacklogEntry *pPendingEntry = m_BacklogPending.First(); pPendingEntry; pPendingEntry = m_BacklogPending.Next(pCurrent: pPendingEntry))
294 {
295 const size_t EntrySize = sizeof(CBacklogEntry) + pPendingEntry->m_Length;
296 CBacklogEntry *pEntry = m_Backlog.Allocate(Size: EntrySize);
297 mem_copy(dest: pEntry, source: pPendingEntry, size: EntrySize);
298 }
299
300 m_BacklogPending.Init();
301 }
302
303 // Update text attributes and count number of added lines
304 m_pGameConsole->Ui()->MapScreen();
305 for(CBacklogEntry *pEntry = m_Backlog.First(); pEntry; pEntry = m_Backlog.Next(pCurrent: pEntry))
306 {
307 if(pEntry->m_LineCount == -1)
308 {
309 UpdateEntryTextAttributes(pEntry);
310 m_NewLineCounter += pEntry->m_LineCount;
311 }
312 }
313}
314
315void CGameConsole::CInstance::ClearHistory()
316{
317 m_History.Init();
318 m_pHistoryEntry = nullptr;
319}
320
321void CGameConsole::CInstance::Reset()
322{
323 m_CompletionRenderOffset = 0.0f;
324 m_CompletionRenderOffsetChange = 0.0f;
325 m_pCommandName = "";
326 m_pCommandHelp = "";
327 m_pCommandParams = "";
328 m_CompletionArgumentPosition = 0;
329 m_CompletionDirty = true;
330}
331
332void CGameConsole::ForceUpdateRemoteCompletionSuggestions()
333{
334 m_RemoteConsole.m_CompletionDirty = true;
335 m_RemoteConsole.UpdateCompletionSuggestions();
336}
337
338void CGameConsole::CInstance::UpdateCompletionSuggestions()
339{
340 if(!m_CompletionDirty)
341 return;
342
343 // Store old selection
344 char aOldCommand[IConsole::CMDLINE_LENGTH];
345 aOldCommand[0] = '\0';
346 if(m_CompletionChosen != -1 && (size_t)m_CompletionChosen < m_vpCommandSuggestions.size())
347 str_copy(dst: aOldCommand, src: m_vpCommandSuggestions[m_CompletionChosen], dst_size: sizeof(aOldCommand));
348
349 char aOldArgument[IConsole::CMDLINE_LENGTH];
350 aOldArgument[0] = '\0';
351 if(m_CompletionChosenArgument != -1 && (size_t)m_CompletionChosenArgument < m_vpArgumentSuggestions.size())
352 str_copy(dst: aOldArgument, src: m_vpArgumentSuggestions[m_CompletionChosenArgument], dst_size: sizeof(aOldArgument));
353
354 m_vpCommandSuggestions.clear();
355 m_vpArgumentSuggestions.clear();
356
357 // Command completion
358 char aSearch[IConsole::CMDLINE_LENGTH];
359 GetCommand(pInput: m_aCompletionBuffer, aCmd&: aSearch);
360 const bool RemoteConsoleCompletion = m_Type == CGameConsole::CONSOLETYPE_REMOTE && m_pGameConsole->Client()->RconAuthed();
361 const bool UseTempCommands = RemoteConsoleCompletion && m_pGameConsole->Client()->UseTempRconCommands();
362 m_pGameConsole->m_pConsole->PossibleCommands(pStr: aSearch, FlagMask: m_CompletionFlagmask, Temp: UseTempCommands, pfnCallback: CollectPossibleCommandsCallback, pUser: &m_vpCommandSuggestions);
363 SortCompletions(vCompletions&: m_vpCommandSuggestions, pSearch: aSearch);
364
365 // Argument completion
366 const auto [CompletionType, CompletionPos] = ArgumentCompletion(pStr: GetString());
367 if(CompletionType != EArgumentCompletionType::NONE)
368 {
369 if(CompletionType == EArgumentCompletionType::MAP)
370 m_pGameConsole->PossibleMaps(pStr: m_aCompletionBufferArgument, pfnCallback: CollectPossibleCommandsCallback, pUser: &m_vpArgumentSuggestions);
371 else if(CompletionType == EArgumentCompletionType::TUNE)
372 PossibleTunings(pStr: m_aCompletionBufferArgument, pfnCallback: CollectPossibleCommandsCallback, pUser: &m_vpArgumentSuggestions);
373 else if(CompletionType == EArgumentCompletionType::SETTING)
374 m_pGameConsole->m_pConsole->PossibleCommands(pStr: m_aCompletionBufferArgument, FlagMask: m_CompletionFlagmask, Temp: UseTempCommands, pfnCallback: CollectPossibleCommandsCallback, pUser: &m_vpArgumentSuggestions);
375 else if(CompletionType == EArgumentCompletionType::KEY)
376 PossibleKeys(pStr: m_aCompletionBufferArgument, pInput: m_pGameConsole->Input(), pfnCallback: CollectPossibleCommandsCallback, pUser: &m_vpArgumentSuggestions);
377 SortCompletions(vCompletions&: m_vpArgumentSuggestions, pSearch: m_aCompletionBufferArgument);
378 }
379
380 // Restore old selection if it changed
381 if(m_CompletionChosen != -1 && (size_t)m_CompletionChosen < m_vpCommandSuggestions.size() &&
382 aOldCommand[0] != '\0' && str_comp(a: m_vpCommandSuggestions[m_CompletionChosen], b: aOldCommand) != 0)
383 {
384 for(size_t SuggestedId = 0; SuggestedId < m_vpCommandSuggestions.size(); SuggestedId++)
385 {
386 if(str_comp(a: m_vpCommandSuggestions[SuggestedId], b: aOldCommand) == 0)
387 {
388 m_CompletionChosen = SuggestedId;
389 m_QueueResetAnimation = true;
390 break;
391 }
392 }
393 }
394 if(m_CompletionChosenArgument != -1 && (size_t)m_CompletionChosenArgument < m_vpArgumentSuggestions.size() &&
395 aOldArgument[0] != '\0' && str_comp(a: m_vpArgumentSuggestions[m_CompletionChosenArgument], b: aOldArgument) != 0)
396 {
397 for(size_t SuggestedId = 0; SuggestedId < m_vpArgumentSuggestions.size(); SuggestedId++)
398 {
399 if(str_comp(a: m_vpArgumentSuggestions[SuggestedId], b: aOldArgument) == 0)
400 {
401 m_CompletionChosenArgument = SuggestedId;
402 m_QueueResetAnimation = true;
403 break;
404 }
405 }
406 }
407
408 m_CompletionDirty = false;
409}
410
411void CGameConsole::CInstance::ExecuteLine(const char *pLine)
412{
413 if(m_Type == CONSOLETYPE_LOCAL || m_pGameConsole->Client()->RconAuthed())
414 {
415 const char *pPrevEntry = m_History.Last();
416 if(pPrevEntry == nullptr || str_comp(a: pPrevEntry, b: pLine) != 0)
417 {
418 const size_t Size = str_length(str: pLine) + 1;
419 char *pEntry = m_History.Allocate(Size);
420 str_copy(dst: pEntry, src: pLine, dst_size: Size);
421 }
422 // print out the user's commands before they get run
423 char aBuf[IConsole::CMDLINE_LENGTH + 3];
424 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "> %s", pLine);
425 m_pGameConsole->PrintLine(Type: m_Type, pLine: aBuf);
426 }
427
428 if(m_Type == CGameConsole::CONSOLETYPE_LOCAL)
429 {
430 m_pGameConsole->m_pConsole->ExecuteLine(pStr: pLine);
431 }
432 else
433 {
434 if(m_pGameConsole->Client()->RconAuthed())
435 {
436 m_pGameConsole->Client()->Rcon(pLine);
437 }
438 else
439 {
440 if(!m_UserGot && m_UsernameReq)
441 {
442 m_UserGot = true;
443 str_copy(dst&: m_aUser, src: pLine);
444 }
445 else
446 {
447 m_pGameConsole->Client()->RconAuth(pUsername: m_aUser, pPassword: pLine, Dummy: g_Config.m_ClDummy);
448 m_UserGot = false;
449 }
450 }
451 }
452}
453
454void CGameConsole::CInstance::PossibleCommandsCompleteCallback(int Index, const char *pStr, void *pUser)
455{
456 CGameConsole::CInstance *pInstance = (CGameConsole::CInstance *)pUser;
457 if(pInstance->m_CompletionChosen == Index)
458 {
459 char aBefore[IConsole::CMDLINE_LENGTH];
460 str_truncate(dst: aBefore, dst_size: sizeof(aBefore), src: pInstance->m_aCompletionBuffer, truncation_len: pInstance->m_CompletionCommandStart);
461 char aBuf[IConsole::CMDLINE_LENGTH];
462 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s%s%s", aBefore, pStr, pInstance->m_aCompletionBuffer + pInstance->m_CompletionCommandEnd);
463 pInstance->m_Input.Set(aBuf);
464 pInstance->m_Input.SetCursorOffset(str_length(str: pStr) + pInstance->m_CompletionCommandStart);
465 }
466}
467
468void CGameConsole::CInstance::GetCommand(const char *pInput, char (&aCmd)[IConsole::CMDLINE_LENGTH])
469{
470 char aInput[IConsole::CMDLINE_LENGTH];
471 str_copy(dst&: aInput, src: pInput);
472 m_CompletionCommandStart = 0;
473 m_CompletionCommandEnd = 0;
474
475 char aaSeparators[][2] = {";", "\""};
476 for(auto *pSeparator : aaSeparators)
477 {
478 int Start, End;
479 str_delimiters_around_offset(haystack: aInput + m_CompletionCommandStart, delim: pSeparator, offset: m_Input.GetCursorOffset() - m_CompletionCommandStart, start: &Start, end: &End);
480 m_CompletionCommandStart += Start;
481 m_CompletionCommandEnd = m_CompletionCommandStart + (End - Start);
482 aInput[m_CompletionCommandEnd] = '\0';
483 }
484
485 str_copy(dst: aCmd, src: aInput + m_CompletionCommandStart, dst_size: sizeof(aCmd));
486}
487
488static void StrCopyUntilSpace(char *pDest, size_t DestSize, const char *pSrc)
489{
490 const char *pSpace = str_find(haystack: pSrc, needle: " ");
491 str_copy(dst: pDest, src: pSrc, dst_size: minimum<size_t>(a: pSpace ? pSpace - pSrc + 1 : 1, b: DestSize));
492}
493
494void CGameConsole::CInstance::PossibleArgumentsCompleteCallback(int Index, const char *pStr, void *pUser)
495{
496 CGameConsole::CInstance *pInstance = (CGameConsole::CInstance *)pUser;
497 if(pInstance->m_CompletionChosenArgument == Index)
498 {
499 // get command
500 char aBuf[IConsole::CMDLINE_LENGTH];
501 str_copy(dst: aBuf, src: pInstance->GetString(), dst_size: pInstance->m_CompletionArgumentPosition);
502 str_append(dst&: aBuf, src: " ");
503
504 // append argument
505 str_append(dst&: aBuf, src: pStr);
506 pInstance->m_Input.Set(aBuf);
507 }
508}
509
510bool CGameConsole::CInstance::OnInput(const IInput::CEvent &Event)
511{
512 bool Handled = false;
513
514 // Don't allow input while the console is opening/closing
515 if(m_pGameConsole->m_ConsoleState == CONSOLE_OPENING || m_pGameConsole->m_ConsoleState == CONSOLE_CLOSING)
516 return Handled;
517
518 auto &&SelectNextSearchMatch = [&](int Direction) {
519 if(!m_vSearchMatches.empty())
520 {
521 m_CurrentMatchIndex += Direction;
522 if(m_CurrentMatchIndex >= (int)m_vSearchMatches.size())
523 m_CurrentMatchIndex = 0;
524 if(m_CurrentMatchIndex < 0)
525 m_CurrentMatchIndex = (int)m_vSearchMatches.size() - 1;
526 m_HasSelection = false;
527 // Also scroll to the correct line
528 ScrollToCenter(StartLine: m_vSearchMatches[m_CurrentMatchIndex].m_StartLine, EndLine: m_vSearchMatches[m_CurrentMatchIndex].m_EndLine);
529 }
530 };
531
532 const int BacklogPrevLine = m_BacklogCurLine;
533 if(Event.m_Flags & IInput::FLAG_PRESS)
534 {
535 if(Event.m_Key == KEY_RETURN || Event.m_Key == KEY_KP_ENTER)
536 {
537 if(!m_Searching)
538 {
539 if(!m_Input.IsEmpty() || (m_UsernameReq && !m_pGameConsole->Client()->RconAuthed() && !m_UserGot))
540 {
541 ExecuteLine(pLine: m_Input.GetString());
542 m_Input.Clear();
543 m_pHistoryEntry = nullptr;
544 }
545 }
546 else
547 {
548 SelectNextSearchMatch(m_pGameConsole->GameClient()->Input()->ShiftIsPressed() ? -1 : 1);
549 }
550
551 Handled = true;
552 }
553 else if(Event.m_Key == KEY_UP)
554 {
555 if(m_Searching)
556 {
557 SelectNextSearchMatch(-1);
558 }
559 else if(m_Type == CONSOLETYPE_LOCAL || m_pGameConsole->Client()->RconAuthed())
560 {
561 if(m_pHistoryEntry)
562 {
563 char *pTest = m_History.Prev(pCurrent: m_pHistoryEntry);
564
565 if(pTest)
566 m_pHistoryEntry = pTest;
567 }
568 else
569 m_pHistoryEntry = m_History.Last();
570
571 if(m_pHistoryEntry)
572 m_Input.Set(m_pHistoryEntry);
573 }
574 Handled = true;
575 }
576 else if(Event.m_Key == KEY_DOWN)
577 {
578 if(m_Searching)
579 {
580 SelectNextSearchMatch(1);
581 }
582 else if(m_Type == CONSOLETYPE_LOCAL || m_pGameConsole->Client()->RconAuthed())
583 {
584 if(m_pHistoryEntry)
585 m_pHistoryEntry = m_History.Next(pCurrent: m_pHistoryEntry);
586
587 if(m_pHistoryEntry)
588 m_Input.Set(m_pHistoryEntry);
589 else
590 m_Input.Clear();
591 }
592 Handled = true;
593 }
594 else if(Event.m_Key == KEY_TAB)
595 {
596 const int Direction = m_pGameConsole->GameClient()->Input()->ShiftIsPressed() ? -1 : 1;
597
598 if(!m_Searching)
599 {
600 UpdateCompletionSuggestions();
601
602 // Command completion
603 int CompletionEnumerationCount = m_vpCommandSuggestions.size();
604
605 if(m_Type == CGameConsole::CONSOLETYPE_LOCAL || m_pGameConsole->Client()->RconAuthed())
606 {
607 if(CompletionEnumerationCount)
608 {
609 if(m_CompletionChosen == -1 && Direction < 0)
610 m_CompletionChosen = 0;
611 m_CompletionChosen = (m_CompletionChosen + Direction + CompletionEnumerationCount) % CompletionEnumerationCount;
612 m_CompletionArgumentPosition = 0;
613
614 PossibleCommandsCompleteCallback(Index: m_CompletionChosen, pStr: m_vpCommandSuggestions[m_CompletionChosen], pUser: this);
615 }
616 else if(m_CompletionChosen != -1)
617 {
618 m_CompletionChosen = -1;
619 Reset();
620 }
621 }
622
623 // Argument completion
624 const auto [CompletionType, CompletionPos] = ArgumentCompletion(pStr: GetString());
625 int CompletionEnumerationCountArgs = m_vpArgumentSuggestions.size();
626 if(CompletionEnumerationCountArgs)
627 {
628 if(m_CompletionChosenArgument == -1 && Direction < 0)
629 m_CompletionChosenArgument = 0;
630 m_CompletionChosenArgument = (m_CompletionChosenArgument + Direction + CompletionEnumerationCountArgs) % CompletionEnumerationCountArgs;
631 m_CompletionArgumentPosition = CompletionPos;
632
633 PossibleArgumentsCompleteCallback(Index: m_CompletionChosenArgument, pStr: m_vpArgumentSuggestions[m_CompletionChosenArgument], pUser: this);
634 }
635 else if(m_CompletionChosenArgument != -1)
636 {
637 m_CompletionChosenArgument = -1;
638 Reset();
639 }
640 }
641 else
642 {
643 // Use Tab / Shift-Tab to cycle through search matches
644 SelectNextSearchMatch(Direction);
645 }
646 Handled = true;
647 }
648 else if(Event.m_Key == KEY_PAGEUP)
649 {
650 m_BacklogCurLine += GetLinesToScroll(Direction: -1, LinesToScroll: m_LinesRendered);
651 Handled = true;
652 }
653 else if(Event.m_Key == KEY_PAGEDOWN)
654 {
655 m_BacklogCurLine -= GetLinesToScroll(Direction: 1, LinesToScroll: m_LinesRendered);
656 if(m_BacklogCurLine < 0)
657 {
658 m_BacklogCurLine = 0;
659 }
660 Handled = true;
661 }
662 else if(Event.m_Key == KEY_MOUSE_WHEEL_UP)
663 {
664 m_BacklogCurLine += GetLinesToScroll(Direction: -1, LinesToScroll: 1);
665 Handled = true;
666 }
667 else if(Event.m_Key == KEY_MOUSE_WHEEL_DOWN)
668 {
669 --m_BacklogCurLine;
670 if(m_BacklogCurLine < 0)
671 {
672 m_BacklogCurLine = 0;
673 }
674 Handled = true;
675 }
676 // in order not to conflict with CLineInput's handling of Home/End only
677 // react to it when the input is empty
678 else if(Event.m_Key == KEY_HOME && m_Input.IsEmpty())
679 {
680 m_BacklogCurLine += GetLinesToScroll(Direction: -1, LinesToScroll: -1);
681 m_BacklogLastActiveLine = m_BacklogCurLine;
682 Handled = true;
683 }
684 else if(Event.m_Key == KEY_END && m_Input.IsEmpty())
685 {
686 m_BacklogCurLine = 0;
687 Handled = true;
688 }
689 else if(Event.m_Key == KEY_ESCAPE && m_Searching)
690 {
691 SetSearching(false);
692 Handled = true;
693 }
694 else if(Event.m_Key == KEY_F && m_pGameConsole->Input()->ModifierIsPressed())
695 {
696 SetSearching(true);
697 Handled = true;
698 }
699 }
700
701 if(m_BacklogCurLine != BacklogPrevLine)
702 {
703 m_HasSelection = false;
704 }
705
706 if(!Handled)
707 {
708 Handled = m_Input.ProcessInput(Event);
709 if(Handled)
710 UpdateSearch();
711 }
712
713 if(Event.m_Flags & (IInput::FLAG_PRESS | IInput::FLAG_TEXT))
714 {
715 if(Event.m_Key != KEY_TAB && Event.m_Key != KEY_LSHIFT && Event.m_Key != KEY_RSHIFT)
716 {
717 const char *pInputStr = m_Input.GetString();
718
719 m_CompletionChosen = -1;
720 str_copy(dst&: m_aCompletionBuffer, src: pInputStr);
721
722 const auto [CompletionType, CompletionPos] = ArgumentCompletion(pStr: GetString());
723 if(CompletionType != EArgumentCompletionType::NONE)
724 {
725 for(const auto &Entry : gs_aArgumentCompletionEntries)
726 {
727 if(Entry.m_Type != CompletionType)
728 continue;
729 const int Len = str_length(str: Entry.m_pCommandName);
730 if(str_comp_nocase_num(a: pInputStr, b: Entry.m_pCommandName, num: Len) == 0 && str_isspace(c: pInputStr[Len]))
731 {
732 m_CompletionChosenArgument = -1;
733 str_copy(dst&: m_aCompletionBufferArgument, src: &pInputStr[CompletionPos]);
734 }
735 }
736 }
737
738 Reset();
739 }
740
741 // find the current command
742 {
743 char aCmd[IConsole::CMDLINE_LENGTH];
744 GetCommand(pInput: GetString(), aCmd);
745 char aBuf[IConsole::CMDLINE_LENGTH];
746 StrCopyUntilSpace(pDest: aBuf, DestSize: sizeof(aBuf), pSrc: aCmd);
747
748 const IConsole::ICommandInfo *pCommand = m_pGameConsole->m_pConsole->GetCommandInfo(pName: aBuf, FlagMask: m_CompletionFlagmask,
749 Temp: m_Type != CGameConsole::CONSOLETYPE_LOCAL && m_pGameConsole->Client()->RconAuthed() && m_pGameConsole->Client()->UseTempRconCommands());
750 if(pCommand)
751 {
752 m_IsCommand = true;
753 m_pCommandName = pCommand->Name();
754 m_pCommandHelp = pCommand->Help();
755 m_pCommandParams = pCommand->Params();
756 }
757 else
758 m_IsCommand = false;
759 }
760 }
761
762 return Handled;
763}
764
765void CGameConsole::CInstance::PrintLine(const char *pLine, int Len, ColorRGBA PrintColor)
766{
767 // We must ensure that no log messages are printed while owning
768 // m_BacklogPendingLock or this will result in a dead lock.
769 const CLockScope LockScope(m_BacklogPendingLock);
770 CBacklogEntry *pEntry = m_BacklogPending.Allocate(Size: sizeof(CBacklogEntry) + Len);
771 pEntry->m_YOffset = -1.0f;
772 pEntry->m_PrintColor = PrintColor;
773 pEntry->m_Length = Len;
774 pEntry->m_LineCount = -1;
775 str_copy(dst: pEntry->m_aText, src: pLine, dst_size: Len + 1);
776}
777
778int CGameConsole::CInstance::GetLinesToScroll(int Direction, int LinesToScroll)
779{
780 auto *pEntry = m_Backlog.Last();
781 int Line = 0;
782 int LinesToSkip = (Direction == -1 ? m_BacklogCurLine + m_LinesRendered : m_BacklogCurLine - 1);
783 while(Line < LinesToSkip && pEntry)
784 {
785 if(pEntry->m_LineCount == -1)
786 UpdateEntryTextAttributes(pEntry);
787 Line += pEntry->m_LineCount;
788 pEntry = m_Backlog.Prev(pCurrent: pEntry);
789 }
790
791 int Amount = maximum(a: 0, b: Line - LinesToSkip);
792 while(pEntry && (LinesToScroll > 0 ? Amount < LinesToScroll : true))
793 {
794 if(pEntry->m_LineCount == -1)
795 UpdateEntryTextAttributes(pEntry);
796 Amount += pEntry->m_LineCount;
797 pEntry = Direction == -1 ? m_Backlog.Prev(pCurrent: pEntry) : m_Backlog.Next(pCurrent: pEntry);
798 }
799
800 return LinesToScroll > 0 ? minimum(a: Amount, b: LinesToScroll) : Amount;
801}
802
803void CGameConsole::CInstance::ScrollToCenter(int StartLine, int EndLine)
804{
805 // This method is used to scroll lines from `StartLine` to `EndLine` to the center of the screen, if possible.
806
807 // Find target line
808 int Target = maximum(a: 0, b: (int)ceil(x: StartLine - minimum(a: StartLine - EndLine, b: m_LinesRendered) / 2) - m_LinesRendered / 2);
809 if(m_BacklogCurLine == Target)
810 return;
811
812 // Compute actual amount of lines to scroll to make sure lines fit in viewport and we don't have empty space
813 int Direction = m_BacklogCurLine - Target < 0 ? -1 : 1;
814 int LinesToScroll = absolute(a: Target - m_BacklogCurLine);
815 int ComputedLines = GetLinesToScroll(Direction, LinesToScroll);
816
817 if(Direction == -1)
818 m_BacklogCurLine += ComputedLines;
819 else
820 m_BacklogCurLine -= ComputedLines;
821}
822
823void CGameConsole::CInstance::UpdateEntryTextAttributes(CBacklogEntry *pEntry) const
824{
825 CTextCursor Cursor;
826 Cursor.m_FontSize = FONT_SIZE;
827 Cursor.m_Flags = 0;
828 Cursor.m_LineWidth = m_pGameConsole->Ui()->Screen()->w - 10;
829 Cursor.m_MaxLines = 10;
830 Cursor.m_LineSpacing = LINE_SPACING;
831 m_pGameConsole->TextRender()->TextEx(pCursor: &Cursor, pText: pEntry->m_aText, Length: -1);
832 pEntry->m_YOffset = Cursor.Height();
833 pEntry->m_LineCount = Cursor.m_LineCount;
834}
835
836bool CGameConsole::CInstance::IsInputHidden() const
837{
838 if(m_Type != CONSOLETYPE_REMOTE)
839 return false;
840 if(m_pGameConsole->Client()->State() != IClient::STATE_ONLINE || m_Searching)
841 return false;
842 if(m_pGameConsole->Client()->RconAuthed())
843 return false;
844 return m_UserGot || !m_UsernameReq;
845}
846
847void CGameConsole::CInstance::SetSearching(bool Searching)
848{
849 m_Searching = Searching;
850 if(Searching)
851 {
852 m_Input.SetClipboardLineCallback(nullptr); // restore default behavior (replace newlines with spaces)
853 m_Input.Set(m_aCurrentSearchString);
854 m_Input.SelectAll();
855 UpdateSearch();
856 }
857 else
858 {
859 m_Input.SetClipboardLineCallback([this](const char *pLine) { ExecuteLine(pLine); });
860 m_Input.Clear();
861 }
862}
863
864void CGameConsole::CInstance::ClearSearch()
865{
866 m_vSearchMatches.clear();
867 m_CurrentMatchIndex = -1;
868 m_Input.Clear();
869 m_aCurrentSearchString[0] = '\0';
870}
871
872void CGameConsole::CInstance::UpdateSearch()
873{
874 if(!m_Searching)
875 return;
876
877 const char *pSearchText = m_Input.GetString();
878 bool SearchChanged = str_utf8_comp_nocase(a: pSearchText, b: m_aCurrentSearchString) != 0;
879
880 int SearchLength = m_Input.GetLength();
881 str_copy(dst&: m_aCurrentSearchString, src: pSearchText);
882
883 m_vSearchMatches.clear();
884 if(pSearchText[0] == '\0')
885 {
886 m_CurrentMatchIndex = -1;
887 return;
888 }
889
890 if(SearchChanged)
891 {
892 m_CurrentMatchIndex = -1;
893 m_HasSelection = false;
894 }
895
896 ITextRender *pTextRender = m_pGameConsole->Ui()->TextRender();
897 const int LineWidth = m_pGameConsole->Ui()->Screen()->w - 10.0f;
898
899 CBacklogEntry *pEntry = m_Backlog.Last();
900 int EntryLine = 0, LineToScrollStart = 0, LineToScrollEnd = 0;
901
902 for(; pEntry; EntryLine += pEntry->m_LineCount, pEntry = m_Backlog.Prev(pCurrent: pEntry))
903 {
904 const char *pSearchPos = str_utf8_find_nocase(haystack: pEntry->m_aText, needle: pSearchText);
905 if(!pSearchPos)
906 continue;
907
908 int EntryLineCount = pEntry->m_LineCount;
909
910 // Find all occurrences of the search string and save their positions
911 while(pSearchPos)
912 {
913 int Pos = pSearchPos - pEntry->m_aText;
914
915 if(EntryLineCount == 1)
916 {
917 m_vSearchMatches.emplace_back(args&: Pos, args&: EntryLine, args&: EntryLine, args&: EntryLine);
918 if(EntryLine > LineToScrollStart)
919 {
920 LineToScrollStart = EntryLine;
921 LineToScrollEnd = EntryLine;
922 }
923 }
924 else
925 {
926 // A match can span multiple lines in case of a multiline entry, so we need to know which line the match starts at
927 // and which line it ends at in order to put it in viewport properly
928 STextSizeProperties Props;
929 int LineCount;
930 Props.m_pLineCount = &LineCount;
931
932 // Compute line of end match
933 pTextRender->TextWidth(Size: FONT_SIZE, pText: pEntry->m_aText, StrLength: Pos + SearchLength, LineWidth, Flags: 0, TextSizeProps: Props);
934 int EndLine = (EntryLineCount - LineCount);
935 int MatchEndLine = EntryLine + EndLine;
936
937 // Compute line of start of match
938 int MatchStartLine = MatchEndLine;
939 if(LineCount > 1)
940 {
941 pTextRender->TextWidth(Size: FONT_SIZE, pText: pEntry->m_aText, StrLength: Pos, LineWidth, Flags: 0, TextSizeProps: Props);
942 int StartLine = (EntryLineCount - LineCount);
943 MatchStartLine = EntryLine + StartLine;
944 }
945
946 if(MatchStartLine > LineToScrollStart)
947 {
948 LineToScrollStart = MatchStartLine;
949 LineToScrollEnd = MatchEndLine;
950 }
951
952 m_vSearchMatches.emplace_back(args&: Pos, args&: MatchStartLine, args&: MatchEndLine, args&: EntryLine);
953 }
954
955 pSearchPos = str_utf8_find_nocase(haystack: pEntry->m_aText + Pos + SearchLength, needle: pSearchText);
956 }
957 }
958
959 if(!m_vSearchMatches.empty() && SearchChanged)
960 m_CurrentMatchIndex = 0;
961 else
962 m_CurrentMatchIndex = std::clamp(val: m_CurrentMatchIndex, lo: -1, hi: (int)m_vSearchMatches.size() - 1);
963
964 // Reverse order of lines by sorting so we have matches from top to bottom instead of bottom to top
965 std::sort(first: m_vSearchMatches.begin(), last: m_vSearchMatches.end(), comp: [](const SSearchMatch &MatchA, const SSearchMatch &MatchB) {
966 if(MatchA.m_StartLine == MatchB.m_StartLine)
967 return MatchA.m_Pos < MatchB.m_Pos; // Make sure to keep position order
968 return MatchA.m_StartLine > MatchB.m_StartLine;
969 });
970
971 if(!m_vSearchMatches.empty() && SearchChanged)
972 {
973 ScrollToCenter(StartLine: LineToScrollStart, EndLine: LineToScrollEnd);
974 }
975}
976
977void CGameConsole::CInstance::Dump()
978{
979 char aTimestamp[20];
980 str_timestamp(buffer: aTimestamp, buffer_size: sizeof(aTimestamp));
981 char aFilename[IO_MAX_PATH_LENGTH];
982 str_format(buffer: aFilename, buffer_size: sizeof(aFilename), format: "dumps/%s_dump_%s.txt", m_pName, aTimestamp);
983 IOHANDLE File = m_pGameConsole->Storage()->OpenFile(pFilename: aFilename, Flags: IOFLAG_WRITE, Type: IStorage::TYPE_SAVE);
984 if(File)
985 {
986 PumpBacklogPending();
987 for(CInstance::CBacklogEntry *pEntry = m_Backlog.First(); pEntry; pEntry = m_Backlog.Next(pCurrent: pEntry))
988 {
989 io_write(io: File, buffer: pEntry->m_aText, size: pEntry->m_Length);
990 io_write_newline(io: File);
991 }
992 io_close(io: File);
993 log_info("console", "%s contents were written to '%s'", m_pName, aFilename);
994 }
995 else
996 {
997 log_error("console", "Failed to open '%s'", aFilename);
998 }
999}
1000
1001CGameConsole::CGameConsole() :
1002 m_LocalConsole(CONSOLETYPE_LOCAL), m_RemoteConsole(CONSOLETYPE_REMOTE)
1003{
1004 m_ConsoleType = CONSOLETYPE_LOCAL;
1005 m_ConsoleState = CONSOLE_CLOSED;
1006 m_StateChangeEnd = 0.0f;
1007 m_StateChangeDuration = 0.1f;
1008
1009 m_pConsoleLogger = new CConsoleLogger(this);
1010}
1011
1012CGameConsole::~CGameConsole()
1013{
1014 if(m_pConsoleLogger)
1015 m_pConsoleLogger->OnConsoleDeletion();
1016}
1017
1018CGameConsole::CInstance *CGameConsole::ConsoleForType(int ConsoleType)
1019{
1020 if(ConsoleType == CONSOLETYPE_REMOTE)
1021 return &m_RemoteConsole;
1022 return &m_LocalConsole;
1023}
1024
1025CGameConsole::CInstance *CGameConsole::CurrentConsole()
1026{
1027 return ConsoleForType(ConsoleType: m_ConsoleType);
1028}
1029
1030void CGameConsole::OnReset()
1031{
1032 m_RemoteConsole.Reset();
1033}
1034
1035int CGameConsole::PossibleMaps(const char *pStr, IConsole::FPossibleCallback pfnCallback, void *pUser)
1036{
1037 int Index = 0;
1038 for(const std::string &Entry : Client()->MaplistEntries())
1039 {
1040 if(str_find_nocase(haystack: Entry.c_str(), needle: pStr))
1041 {
1042 pfnCallback(Index, Entry.c_str(), pUser);
1043 Index++;
1044 }
1045 }
1046 return Index;
1047}
1048
1049// only defined for 0<=t<=1
1050static float ConsoleScaleFunc(float t)
1051{
1052 return std::sin(x: std::acos(x: 1.0f - t));
1053}
1054
1055struct CCompletionOptionRenderInfo
1056{
1057 CGameConsole *m_pSelf;
1058 CTextCursor m_Cursor;
1059 const char *m_pCurrentCmd;
1060 int m_WantedCompletion;
1061 float m_Offset;
1062 float *m_pOffsetChange;
1063 float m_Width;
1064 float m_TotalWidth;
1065};
1066
1067void CGameConsole::PossibleCommandsRenderCallback(int Index, const char *pStr, void *pUser)
1068{
1069 CCompletionOptionRenderInfo *pInfo = static_cast<CCompletionOptionRenderInfo *>(pUser);
1070
1071 ColorRGBA TextColor;
1072 if(Index == pInfo->m_WantedCompletion)
1073 {
1074 TextColor = ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f);
1075 const float TextWidth = pInfo->m_pSelf->TextRender()->TextWidth(Size: pInfo->m_Cursor.m_FontSize, pText: pStr);
1076 const CUIRect Rect = {.x: pInfo->m_Cursor.m_X - 2.0f, .y: pInfo->m_Cursor.m_Y - 2.0f, .w: TextWidth + 4.0f, .h: pInfo->m_Cursor.m_FontSize + 4.0f};
1077 Rect.Draw(Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.85f), Corners: IGraphics::CORNER_ALL, Rounding: 2.0f);
1078
1079 // scroll when out of sight
1080 const bool MoveLeft = Rect.x - *pInfo->m_pOffsetChange < 0.0f;
1081 const bool MoveRight = Rect.x + Rect.w - *pInfo->m_pOffsetChange > pInfo->m_Width;
1082 if(MoveLeft && !MoveRight)
1083 {
1084 *pInfo->m_pOffsetChange -= -Rect.x + pInfo->m_Width / 4.0f;
1085 }
1086 else if(!MoveLeft && MoveRight)
1087 {
1088 *pInfo->m_pOffsetChange += Rect.x + Rect.w - pInfo->m_Width + pInfo->m_Width / 4.0f;
1089 }
1090 }
1091 else
1092 {
1093 TextColor = ColorRGBA(0.75f, 0.75f, 0.75f, 1.0f);
1094 }
1095
1096 const char *pMatchStart = str_find_nocase(haystack: pStr, needle: pInfo->m_pCurrentCmd);
1097 if(pMatchStart)
1098 {
1099 pInfo->m_pSelf->TextRender()->TextColor(Color: TextColor);
1100 pInfo->m_pSelf->TextRender()->TextEx(pCursor: &pInfo->m_Cursor, pText: pStr, Length: pMatchStart - pStr);
1101 pInfo->m_pSelf->TextRender()->TextColor(r: 1.0f, g: 0.75f, b: 0.0f, a: 1.0f);
1102 pInfo->m_pSelf->TextRender()->TextEx(pCursor: &pInfo->m_Cursor, pText: pMatchStart, Length: str_length(str: pInfo->m_pCurrentCmd));
1103 pInfo->m_pSelf->TextRender()->TextColor(Color: TextColor);
1104 pInfo->m_pSelf->TextRender()->TextEx(pCursor: &pInfo->m_Cursor, pText: pMatchStart + str_length(str: pInfo->m_pCurrentCmd));
1105 }
1106 else
1107 {
1108 pInfo->m_pSelf->TextRender()->TextColor(Color: TextColor);
1109 pInfo->m_pSelf->TextRender()->TextEx(pCursor: &pInfo->m_Cursor, pText: pStr);
1110 }
1111
1112 pInfo->m_Cursor.m_X += 7.0f;
1113 pInfo->m_TotalWidth = pInfo->m_Cursor.m_X + pInfo->m_Offset;
1114}
1115
1116void CGameConsole::Prompt(char (&aPrompt)[32])
1117{
1118 CInstance *pConsole = CurrentConsole();
1119 if(pConsole->m_Searching)
1120 {
1121 str_format(buffer: aPrompt, buffer_size: sizeof(aPrompt), format: "%s: ", Localize(pStr: "Searching"));
1122 }
1123 else if(m_ConsoleType == CONSOLETYPE_REMOTE)
1124 {
1125 if(Client()->State() == IClient::STATE_LOADING || Client()->State() == IClient::STATE_ONLINE)
1126 {
1127 if(Client()->RconAuthed())
1128 str_copy(dst&: aPrompt, src: "rcon> ");
1129 else if(pConsole->m_UsernameReq && !pConsole->m_UserGot)
1130 str_format(buffer: aPrompt, buffer_size: sizeof(aPrompt), format: "%s> ", Localize(pStr: "Enter Username"));
1131 else
1132 str_format(buffer: aPrompt, buffer_size: sizeof(aPrompt), format: "%s> ", Localize(pStr: "Enter Password"));
1133 }
1134 else
1135 str_format(buffer: aPrompt, buffer_size: sizeof(aPrompt), format: "%s> ", Localize(pStr: "NOT CONNECTED"));
1136 }
1137 else
1138 {
1139 str_copy(dst&: aPrompt, src: "> ");
1140 }
1141}
1142
1143void CGameConsole::OnRender()
1144{
1145 CUIRect Screen = *Ui()->Screen();
1146 CInstance *pConsole = CurrentConsole();
1147
1148 const float MaxConsoleHeight = Screen.h * 3 / 5.0f;
1149 float Progress = (Client()->GlobalTime() - (m_StateChangeEnd - m_StateChangeDuration)) / m_StateChangeDuration;
1150
1151 if(Progress >= 1.0f)
1152 {
1153 if(m_ConsoleState == CONSOLE_CLOSING)
1154 {
1155 m_ConsoleState = CONSOLE_CLOSED;
1156 pConsole->m_BacklogLastActiveLine = -1;
1157 }
1158 else if(m_ConsoleState == CONSOLE_OPENING)
1159 {
1160 m_ConsoleState = CONSOLE_OPEN;
1161 pConsole->m_Input.Activate(Priority: EInputPriority::CONSOLE);
1162 }
1163
1164 Progress = 1.0f;
1165 }
1166
1167 if(m_ConsoleState == CONSOLE_OPEN && g_Config.m_ClEditor)
1168 Toggle(Type: CONSOLETYPE_LOCAL);
1169
1170 if(m_ConsoleState == CONSOLE_CLOSED)
1171 return;
1172
1173 if(m_ConsoleState == CONSOLE_OPEN)
1174 Input()->MouseModeAbsolute();
1175
1176 float ConsoleHeightScale;
1177 if(m_ConsoleState == CONSOLE_OPENING)
1178 ConsoleHeightScale = ConsoleScaleFunc(t: Progress);
1179 else if(m_ConsoleState == CONSOLE_CLOSING)
1180 ConsoleHeightScale = ConsoleScaleFunc(t: 1.0f - Progress);
1181 else // CONSOLE_OPEN
1182 ConsoleHeightScale = ConsoleScaleFunc(t: 1.0f);
1183
1184 const float ConsoleHeight = ConsoleHeightScale * MaxConsoleHeight;
1185
1186 const ColorRGBA ShadowColor = ColorRGBA(0.0f, 0.0f, 0.0f, 0.4f);
1187 const ColorRGBA TransparentColor = ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f);
1188 const ColorRGBA aBackgroundColors[NUM_CONSOLETYPES] = {ColorRGBA(0.2f, 0.2f, 0.2f, 0.9f), ColorRGBA(0.4f, 0.2f, 0.2f, 0.9f)};
1189 const ColorRGBA aBorderColors[NUM_CONSOLETYPES] = {ColorRGBA(0.1f, 0.1f, 0.1f, 0.9f), ColorRGBA(0.2f, 0.1f, 0.1f, 0.9f)};
1190
1191 Ui()->MapScreen();
1192
1193 // background
1194 Graphics()->TextureSet(Texture: g_pData->m_aImages[IMAGE_BACKGROUND_NOISE].m_Id);
1195 Graphics()->QuadsBegin();
1196 Graphics()->SetColor(aBackgroundColors[m_ConsoleType]);
1197 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: Screen.w / 80.0f, BottomRightV: ConsoleHeight / 80.0f);
1198 IGraphics::CQuadItem QuadItemBackground(0.0f, 0.0f, Screen.w, ConsoleHeight);
1199 Graphics()->QuadsDrawTL(pArray: &QuadItemBackground, Num: 1);
1200 Graphics()->QuadsEnd();
1201
1202 // bottom border
1203 Graphics()->TextureClear();
1204 Graphics()->QuadsBegin();
1205 Graphics()->SetColor(aBorderColors[m_ConsoleType]);
1206 IGraphics::CQuadItem QuadItemBorder(0.0f, ConsoleHeight, Screen.w, 1.0f);
1207 Graphics()->QuadsDrawTL(pArray: &QuadItemBorder, Num: 1);
1208 Graphics()->QuadsEnd();
1209
1210 // bottom shadow
1211 Graphics()->TextureClear();
1212 Graphics()->QuadsBegin();
1213 Graphics()->SetColor4(TopLeft: ShadowColor, TopRight: ShadowColor, BottomLeft: TransparentColor, BottomRight: TransparentColor);
1214 IGraphics::CQuadItem QuadItemShadow(0.0f, ConsoleHeight + 1.0f, Screen.w, 10.0f);
1215 Graphics()->QuadsDrawTL(pArray: &QuadItemShadow, Num: 1);
1216 Graphics()->QuadsEnd();
1217
1218 {
1219 // Get height of 1 line
1220 const float LineHeight = TextRender()->TextBoundingBox(Size: FONT_SIZE, pText: " ", StrLength: -1, LineWidth: -1.0f, LineSpacing: LINE_SPACING).m_H;
1221
1222 const float RowHeight = FONT_SIZE * 2.0f;
1223
1224 float x = 3;
1225 float y = ConsoleHeight - RowHeight - 18.0f;
1226
1227 const float InitialX = x;
1228 const float InitialY = y;
1229
1230 // render prompt
1231 CTextCursor PromptCursor;
1232 PromptCursor.SetPosition(vec2(x, y + FONT_SIZE / 2.0f));
1233 PromptCursor.m_FontSize = FONT_SIZE;
1234
1235 char aPrompt[32];
1236 Prompt(aPrompt);
1237 TextRender()->TextEx(pCursor: &PromptCursor, pText: aPrompt);
1238
1239 // check if mouse is pressed
1240 const vec2 WindowSize = vec2(Graphics()->WindowWidth(), Graphics()->WindowHeight());
1241 const vec2 ScreenSize = vec2(Screen.w, Screen.h);
1242 Ui()->UpdateTouchState(State&: m_TouchState);
1243 const auto &&GetMousePosition = [&]() -> vec2 {
1244 if(m_TouchState.m_PrimaryPressed)
1245 {
1246 return m_TouchState.m_PrimaryPosition * ScreenSize;
1247 }
1248 else
1249 {
1250 return Input()->NativeMousePos() / WindowSize * ScreenSize;
1251 }
1252 };
1253 if(!pConsole->m_MouseIsPress && (m_TouchState.m_PrimaryPressed || Input()->NativeMousePressed(Index: 1)))
1254 {
1255 pConsole->m_MouseIsPress = true;
1256 pConsole->m_MousePress = GetMousePosition();
1257 }
1258 if(pConsole->m_MouseIsPress && !m_TouchState.m_PrimaryPressed && !Input()->NativeMousePressed(Index: 1))
1259 {
1260 pConsole->m_MouseIsPress = false;
1261 }
1262 if(pConsole->m_MouseIsPress)
1263 {
1264 pConsole->m_MouseRelease = GetMousePosition();
1265 }
1266 const float ScaledLineHeight = LineHeight / ScreenSize.y;
1267 if(absolute(a: m_TouchState.m_ScrollAmount.y) >= ScaledLineHeight)
1268 {
1269 if(m_TouchState.m_ScrollAmount.y > 0.0f)
1270 {
1271 pConsole->m_BacklogCurLine += pConsole->GetLinesToScroll(Direction: -1, LinesToScroll: 1);
1272 m_TouchState.m_ScrollAmount.y -= ScaledLineHeight;
1273 }
1274 else
1275 {
1276 --pConsole->m_BacklogCurLine;
1277 if(pConsole->m_BacklogCurLine < 0)
1278 pConsole->m_BacklogCurLine = 0;
1279 m_TouchState.m_ScrollAmount.y += ScaledLineHeight;
1280 }
1281 pConsole->m_HasSelection = false;
1282 }
1283
1284 x = PromptCursor.m_X;
1285
1286 if(m_ConsoleState == CONSOLE_OPEN)
1287 {
1288 if(pConsole->m_MousePress.y >= pConsole->m_BoundingBox.m_Y && pConsole->m_MousePress.y < pConsole->m_BoundingBox.m_Y + pConsole->m_BoundingBox.m_H)
1289 {
1290 CLineInput::SMouseSelection *pMouseSelection = pConsole->m_Input.GetMouseSelection();
1291 if(pMouseSelection->m_Selecting && !pConsole->m_MouseIsPress && pConsole->m_Input.IsActive())
1292 {
1293 Input()->EnsureScreenKeyboardShown();
1294 }
1295 pMouseSelection->m_Selecting = pConsole->m_MouseIsPress;
1296 pMouseSelection->m_PressMouse = pConsole->m_MousePress;
1297 pMouseSelection->m_ReleaseMouse = pConsole->m_MouseRelease;
1298 }
1299 else if(pConsole->m_MouseIsPress)
1300 {
1301 pConsole->m_Input.SelectNothing();
1302 }
1303 }
1304
1305 // render console input (wrap line)
1306 pConsole->m_Input.SetHidden(pConsole->IsInputHidden());
1307 if(m_ConsoleState == CONSOLE_OPEN)
1308 {
1309 pConsole->m_Input.Activate(Priority: EInputPriority::CONSOLE); // Ensure that the input is active
1310 }
1311 const CUIRect InputCursorRect = {.x: x, .y: y + FONT_SIZE * 1.5f, .w: 0.0f, .h: 0.0f};
1312 const bool WasChanged = pConsole->m_Input.WasChanged();
1313 const bool WasCursorChanged = pConsole->m_Input.WasCursorChanged();
1314 const bool Changed = WasChanged || WasCursorChanged;
1315 pConsole->m_BoundingBox = pConsole->m_Input.Render(pRect: &InputCursorRect, FontSize: FONT_SIZE, Align: TEXTALIGN_BL, Changed, LineWidth: Screen.w - 10.0f - x, LineSpacing: LINE_SPACING);
1316 if(pConsole->m_LastInputHeight == 0.0f && pConsole->m_BoundingBox.m_H != 0.0f)
1317 pConsole->m_LastInputHeight = pConsole->m_BoundingBox.m_H;
1318 if(pConsole->m_Input.HasSelection())
1319 pConsole->m_HasSelection = false; // Clear console selection if we have a line input selection
1320
1321 y -= pConsole->m_BoundingBox.m_H - FONT_SIZE;
1322
1323 if(pConsole->m_LastInputHeight != pConsole->m_BoundingBox.m_H)
1324 {
1325 pConsole->m_HasSelection = false;
1326 pConsole->m_MouseIsPress = false;
1327 pConsole->m_LastInputHeight = pConsole->m_BoundingBox.m_H;
1328 }
1329
1330 // render possible commands
1331 if(!pConsole->m_Searching && (m_ConsoleType == CONSOLETYPE_LOCAL || Client()->RconAuthed()) && !pConsole->m_Input.IsEmpty())
1332 {
1333 pConsole->UpdateCompletionSuggestions();
1334
1335 CCompletionOptionRenderInfo Info;
1336 Info.m_pSelf = this;
1337 Info.m_WantedCompletion = pConsole->m_CompletionChosen;
1338 Info.m_Offset = pConsole->m_CompletionRenderOffset;
1339 Info.m_pOffsetChange = &pConsole->m_CompletionRenderOffsetChange;
1340 Info.m_Width = Screen.w;
1341 Info.m_TotalWidth = 0.0f;
1342 char aCmd[IConsole::CMDLINE_LENGTH];
1343 pConsole->GetCommand(pInput: pConsole->m_aCompletionBuffer, aCmd);
1344 Info.m_pCurrentCmd = aCmd;
1345
1346 Info.m_Cursor.SetPosition(vec2(InitialX - Info.m_Offset, InitialY + RowHeight + 2.0f));
1347 Info.m_Cursor.m_FontSize = FONT_SIZE;
1348
1349 for(size_t SuggestionId = 0; SuggestionId < pConsole->m_vpCommandSuggestions.size(); ++SuggestionId)
1350 {
1351 PossibleCommandsRenderCallback(Index: SuggestionId, pStr: pConsole->m_vpCommandSuggestions[SuggestionId], pUser: &Info);
1352 }
1353 const int NumCommands = pConsole->m_vpCommandSuggestions.size();
1354 Info.m_TotalWidth = Info.m_Cursor.m_X + Info.m_Offset;
1355 pConsole->m_CompletionRenderOffset = Info.m_Offset;
1356
1357 if(NumCommands <= 0 && pConsole->m_IsCommand)
1358 {
1359 int NumArguments = 0;
1360 if(!pConsole->m_vpArgumentSuggestions.empty())
1361 {
1362 Info.m_WantedCompletion = pConsole->m_CompletionChosenArgument;
1363 Info.m_TotalWidth = 0.0f;
1364 Info.m_pCurrentCmd = pConsole->m_aCompletionBufferArgument;
1365
1366 for(size_t SuggestionId = 0; SuggestionId < pConsole->m_vpArgumentSuggestions.size(); ++SuggestionId)
1367 {
1368 PossibleCommandsRenderCallback(Index: SuggestionId, pStr: pConsole->m_vpArgumentSuggestions[SuggestionId], pUser: &Info);
1369 }
1370 NumArguments = pConsole->m_vpArgumentSuggestions.size();
1371 Info.m_TotalWidth = Info.m_Cursor.m_X + Info.m_Offset;
1372 pConsole->m_CompletionRenderOffset = Info.m_Offset;
1373 }
1374
1375 if(NumArguments <= 0 && pConsole->m_IsCommand)
1376 {
1377 char aBuf[1024];
1378 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Help: %s ", pConsole->m_pCommandHelp);
1379 TextRender()->TextEx(pCursor: &Info.m_Cursor, pText: aBuf, Length: -1);
1380 TextRender()->TextColor(r: 0.75f, g: 0.75f, b: 0.75f, a: 1);
1381 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Usage: %s %s", pConsole->m_pCommandName, pConsole->m_pCommandParams);
1382 TextRender()->TextEx(pCursor: &Info.m_Cursor, pText: aBuf, Length: -1);
1383 }
1384 }
1385
1386 // Reset animation offset in case our chosen completion index changed due to new commands being added/removed
1387 if(pConsole->m_QueueResetAnimation)
1388 {
1389 pConsole->m_CompletionRenderOffset += pConsole->m_CompletionRenderOffsetChange;
1390 pConsole->m_CompletionRenderOffsetChange = 0.0f;
1391 pConsole->m_QueueResetAnimation = false;
1392 }
1393 Ui()->DoSmoothScrollLogic(pScrollOffset: &pConsole->m_CompletionRenderOffset, pScrollOffsetChange: &pConsole->m_CompletionRenderOffsetChange, ViewPortSize: Info.m_Width, TotalSize: Info.m_TotalWidth);
1394 }
1395 else if(pConsole->m_Searching && !pConsole->m_Input.IsEmpty())
1396 { // Render current match and match count
1397 CTextCursor MatchInfoCursor;
1398 MatchInfoCursor.SetPosition(vec2(InitialX, InitialY + RowHeight + 2.0f));
1399 MatchInfoCursor.m_FontSize = FONT_SIZE;
1400 TextRender()->TextColor(r: 0.8f, g: 0.8f, b: 0.8f, a: 1.0f);
1401 if(!pConsole->m_vSearchMatches.empty())
1402 {
1403 char aBuf[64];
1404 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: Localize(pStr: "Match %d of %d"), pConsole->m_CurrentMatchIndex + 1, (int)pConsole->m_vSearchMatches.size());
1405 TextRender()->TextEx(pCursor: &MatchInfoCursor, pText: aBuf, Length: -1);
1406 }
1407 else
1408 {
1409 TextRender()->TextEx(pCursor: &MatchInfoCursor, pText: Localize(pStr: "No results"), Length: -1);
1410 }
1411 }
1412
1413 pConsole->PumpBacklogPending();
1414 if(pConsole->m_NewLineCounter != 0)
1415 {
1416 pConsole->UpdateSearch();
1417
1418 // keep scroll position when new entries are printed.
1419 if(pConsole->m_BacklogCurLine != 0 || pConsole->m_HasSelection)
1420 {
1421 pConsole->m_BacklogCurLine += pConsole->m_NewLineCounter;
1422 pConsole->m_BacklogLastActiveLine += pConsole->m_NewLineCounter;
1423 }
1424 if(pConsole->m_NewLineCounter < 0)
1425 pConsole->m_NewLineCounter = 0;
1426 }
1427
1428 // render console log (current entry, status, wrap lines)
1429 CInstance::CBacklogEntry *pEntry = pConsole->m_Backlog.Last();
1430 float OffsetY = 0.0f;
1431
1432 std::string SelectionString;
1433
1434 if(pConsole->m_BacklogLastActiveLine < 0)
1435 pConsole->m_BacklogLastActiveLine = pConsole->m_BacklogCurLine;
1436
1437 int LineNum = -1;
1438 pConsole->m_LinesRendered = 0;
1439
1440 int SkippedLines = 0;
1441 bool First = true;
1442
1443 const float XScale = Graphics()->ScreenWidth() / Screen.w;
1444 const float YScale = Graphics()->ScreenHeight() / Screen.h;
1445 const float CalcOffsetY = LineHeight * std::floor(x: (y - RowHeight) / LineHeight);
1446 const float ClipStartY = (y - CalcOffsetY) * YScale;
1447 Graphics()->ClipEnable(x: 0, y: ClipStartY, w: Screen.w * XScale, h: (y + 2.0f) * YScale - ClipStartY);
1448
1449 while(pEntry)
1450 {
1451 if(pEntry->m_LineCount == -1)
1452 pConsole->UpdateEntryTextAttributes(pEntry);
1453
1454 LineNum += pEntry->m_LineCount;
1455 if(LineNum < pConsole->m_BacklogLastActiveLine)
1456 {
1457 SkippedLines += pEntry->m_LineCount;
1458 pEntry = pConsole->m_Backlog.Prev(pCurrent: pEntry);
1459 continue;
1460 }
1461 TextRender()->TextColor(Color: pEntry->m_PrintColor);
1462
1463 if(First)
1464 {
1465 OffsetY -= (pConsole->m_BacklogLastActiveLine - SkippedLines) * LineHeight;
1466 }
1467
1468 const float LocalOffsetY = OffsetY + pEntry->m_YOffset / (float)pEntry->m_LineCount;
1469 OffsetY += pEntry->m_YOffset;
1470
1471 // Only apply offset if we do not keep scroll position (m_BacklogCurLine == 0)
1472 if((pConsole->m_HasSelection || pConsole->m_MouseIsPress) && pConsole->m_NewLineCounter > 0 && pConsole->m_BacklogCurLine == 0)
1473 {
1474 pConsole->m_MousePress.y -= pEntry->m_YOffset;
1475 if(!pConsole->m_MouseIsPress)
1476 pConsole->m_MouseRelease.y -= pEntry->m_YOffset;
1477 }
1478
1479 // stop rendering when lines reach the top
1480 const bool Outside = y - OffsetY <= RowHeight;
1481 const bool CanRenderOneLine = y - LocalOffsetY > RowHeight;
1482 if(Outside && !CanRenderOneLine)
1483 break;
1484
1485 const int LinesNotRendered = pEntry->m_LineCount - minimum(a: (int)std::floor(x: (y - LocalOffsetY) / RowHeight), b: pEntry->m_LineCount);
1486 pConsole->m_LinesRendered -= LinesNotRendered;
1487
1488 CTextCursor EntryCursor;
1489 EntryCursor.SetPosition(vec2(0.0f, y - OffsetY));
1490 EntryCursor.m_FontSize = FONT_SIZE;
1491 EntryCursor.m_LineWidth = Screen.w - 10.0f;
1492 EntryCursor.m_MaxLines = pEntry->m_LineCount;
1493 EntryCursor.m_LineSpacing = LINE_SPACING;
1494 EntryCursor.m_CalculateSelectionMode = (m_ConsoleState == CONSOLE_OPEN && pConsole->m_MousePress.y < pConsole->m_BoundingBox.m_Y && (pConsole->m_MouseIsPress || (pConsole->m_CurSelStart != pConsole->m_CurSelEnd) || pConsole->m_HasSelection)) ? TEXT_CURSOR_SELECTION_MODE_CALCULATE : TEXT_CURSOR_SELECTION_MODE_NONE;
1495 EntryCursor.m_PressMouse = pConsole->m_MousePress;
1496 EntryCursor.m_ReleaseMouse = pConsole->m_MouseRelease;
1497
1498 if(pConsole->m_Searching && pConsole->m_CurrentMatchIndex != -1)
1499 {
1500 std::vector<CInstance::SSearchMatch> vMatches;
1501 std::copy_if(first: pConsole->m_vSearchMatches.begin(), last: pConsole->m_vSearchMatches.end(), result: std::back_inserter(x&: vMatches), pred: [&](const CInstance::SSearchMatch &Match) { return Match.m_EntryLine == LineNum + 1 - pEntry->m_LineCount; });
1502
1503 auto CurrentSelectedOccurrence = pConsole->m_vSearchMatches[pConsole->m_CurrentMatchIndex];
1504
1505 EntryCursor.m_vColorSplits.reserve(n: vMatches.size());
1506 for(const auto &Match : vMatches)
1507 {
1508 bool IsSelected = CurrentSelectedOccurrence.m_EntryLine == Match.m_EntryLine && CurrentSelectedOccurrence.m_Pos == Match.m_Pos;
1509 EntryCursor.m_vColorSplits.emplace_back(
1510 args: Match.m_Pos,
1511 args: pConsole->m_Input.GetLength(),
1512 args: IsSelected ? ms_SearchSelectedColor : ms_SearchHighlightColor);
1513 }
1514 }
1515
1516 TextRender()->TextEx(pCursor: &EntryCursor, pText: pEntry->m_aText, Length: -1);
1517 EntryCursor.m_vColorSplits = {};
1518
1519 if(EntryCursor.m_CalculateSelectionMode == TEXT_CURSOR_SELECTION_MODE_CALCULATE)
1520 {
1521 pConsole->m_CurSelStart = minimum(a: EntryCursor.m_SelectionStart, b: EntryCursor.m_SelectionEnd);
1522 pConsole->m_CurSelEnd = maximum(a: EntryCursor.m_SelectionStart, b: EntryCursor.m_SelectionEnd);
1523 }
1524 pConsole->m_LinesRendered += First ? pEntry->m_LineCount - (pConsole->m_BacklogLastActiveLine - SkippedLines) : pEntry->m_LineCount;
1525
1526 if(pConsole->m_CurSelStart != pConsole->m_CurSelEnd)
1527 {
1528 if(m_WantsSelectionCopy)
1529 {
1530 const bool HasNewLine = !SelectionString.empty();
1531 const size_t OffUTF8Start = str_utf8_offset_chars_to_bytes(str: pEntry->m_aText, char_offset: pConsole->m_CurSelStart);
1532 const size_t OffUTF8End = str_utf8_offset_chars_to_bytes(str: pEntry->m_aText, char_offset: pConsole->m_CurSelEnd);
1533 SelectionString.insert(pos1: 0, str: (std::string(&pEntry->m_aText[OffUTF8Start], OffUTF8End - OffUTF8Start) + (HasNewLine ? "\n" : "")));
1534 }
1535 pConsole->m_HasSelection = true;
1536 }
1537
1538 if(pConsole->m_NewLineCounter > 0) // Decrease by the entry line count since we can have multiline entries
1539 pConsole->m_NewLineCounter -= pEntry->m_LineCount;
1540
1541 pEntry = pConsole->m_Backlog.Prev(pCurrent: pEntry);
1542
1543 // reset color
1544 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
1545 First = false;
1546
1547 if(!pEntry)
1548 break;
1549 }
1550
1551 // Make sure to reset m_NewLineCounter when we are done drawing
1552 // This is because otherwise, if many entries are printed at once while console is
1553 // hidden, m_NewLineCounter will always be > 0 since the console won't be able to render
1554 // them all, thus wont be able to decrease m_NewLineCounter to 0.
1555 // This leads to an infinite increase of m_BacklogCurLine and m_BacklogLastActiveLine
1556 // when we want to keep scroll position.
1557 pConsole->m_NewLineCounter = 0;
1558
1559 Graphics()->ClipDisable();
1560
1561 pConsole->m_BacklogLastActiveLine = pConsole->m_BacklogCurLine;
1562
1563 if(m_WantsSelectionCopy && !SelectionString.empty())
1564 {
1565 pConsole->m_HasSelection = false;
1566 pConsole->m_CurSelStart = -1;
1567 pConsole->m_CurSelEnd = -1;
1568 Input()->SetClipboardText(SelectionString.c_str());
1569 m_WantsSelectionCopy = false;
1570 }
1571
1572 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
1573
1574 // render current lines and status (locked, following)
1575 char aBuf[128];
1576 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: Localize(pStr: "Lines %d - %d (%s)"), pConsole->m_BacklogCurLine + 1, pConsole->m_BacklogCurLine + pConsole->m_LinesRendered, pConsole->m_BacklogCurLine != 0 ? Localize(pStr: "Locked") : Localize(pStr: "Following"));
1577 TextRender()->Text(x: 10.0f, y: FONT_SIZE / 2.f, Size: FONT_SIZE, pText: aBuf);
1578
1579 if(m_ConsoleType == CONSOLETYPE_REMOTE && (Client()->ReceivingRconCommands() || Client()->ReceivingMaplist()))
1580 {
1581 const float Percentage = Client()->ReceivingRconCommands() ? Client()->GotRconCommandsPercentage() : Client()->GotMaplistPercentage();
1582 SProgressSpinnerProperties ProgressProps;
1583 ProgressProps.m_Progress = Percentage;
1584 Ui()->RenderProgressSpinner(Center: vec2(Screen.w / 4.0f + FONT_SIZE / 2.f, FONT_SIZE), OuterRadius: FONT_SIZE / 2.f, Props: ProgressProps);
1585
1586 char aLoading[128];
1587 str_copy(dst&: aLoading, src: Client()->ReceivingRconCommands() ? Localize(pStr: "Loading commands…") : Localize(pStr: "Loading maps…"));
1588 if(Percentage > 0)
1589 {
1590 char aPercentage[8];
1591 str_format(buffer: aPercentage, buffer_size: sizeof(aPercentage), format: " %d%%", (int)(Percentage * 100));
1592 str_append(dst&: aLoading, src: aPercentage);
1593 }
1594 TextRender()->Text(x: Screen.w / 4.0f + FONT_SIZE + 2.0f, y: FONT_SIZE / 2.f, Size: FONT_SIZE, pText: aLoading);
1595 }
1596
1597 // render version
1598 str_copy(dst&: aBuf, src: "v" GAME_VERSION " on " CONF_PLATFORM_STRING " " CONF_ARCH_STRING);
1599 TextRender()->Text(x: Screen.w - TextRender()->TextWidth(Size: FONT_SIZE, pText: aBuf) - 10.0f, y: FONT_SIZE / 2.f, Size: FONT_SIZE, pText: aBuf);
1600 }
1601}
1602
1603void CGameConsole::OnMessage(int MsgType, void *pRawMsg)
1604{
1605}
1606
1607bool CGameConsole::OnInput(const IInput::CEvent &Event)
1608{
1609 // accept input when opening, but not at first frame to discard the input that caused the console to open
1610 if(m_ConsoleState != CONSOLE_OPEN && (m_ConsoleState != CONSOLE_OPENING || m_StateChangeEnd == Client()->GlobalTime() + m_StateChangeDuration))
1611 return false;
1612 if((Event.m_Key >= KEY_F1 && Event.m_Key <= KEY_F12) || (Event.m_Key >= KEY_F13 && Event.m_Key <= KEY_F24))
1613 return false;
1614
1615 if(Event.m_Key == KEY_ESCAPE && (Event.m_Flags & IInput::FLAG_PRESS) && !CurrentConsole()->m_Searching)
1616 Toggle(Type: m_ConsoleType);
1617 else if(!CurrentConsole()->OnInput(Event))
1618 {
1619 if(GameClient()->Input()->ModifierIsPressed() && Event.m_Flags & IInput::FLAG_PRESS && Event.m_Key == KEY_C)
1620 m_WantsSelectionCopy = true;
1621 }
1622
1623 return true;
1624}
1625
1626void CGameConsole::Toggle(int Type)
1627{
1628 if(m_ConsoleType != Type && (m_ConsoleState == CONSOLE_OPEN || m_ConsoleState == CONSOLE_OPENING))
1629 {
1630 // don't toggle console, just switch what console to use
1631 }
1632 else
1633 {
1634 if(m_ConsoleState == CONSOLE_CLOSED || m_ConsoleState == CONSOLE_OPEN)
1635 {
1636 m_StateChangeEnd = Client()->GlobalTime() + m_StateChangeDuration;
1637 }
1638 else
1639 {
1640 float Progress = m_StateChangeEnd - Client()->GlobalTime();
1641 float ReversedProgress = m_StateChangeDuration - Progress;
1642
1643 m_StateChangeEnd = Client()->GlobalTime() + ReversedProgress;
1644 }
1645
1646 if(m_ConsoleState == CONSOLE_CLOSED || m_ConsoleState == CONSOLE_CLOSING)
1647 {
1648 Ui()->SetEnabled(false);
1649 m_ConsoleState = CONSOLE_OPENING;
1650 }
1651 else
1652 {
1653 ConsoleForType(ConsoleType: Type)->m_Input.Deactivate();
1654 Input()->MouseModeRelative();
1655 Ui()->SetEnabled(true);
1656 GameClient()->OnRelease();
1657 m_ConsoleState = CONSOLE_CLOSING;
1658 }
1659 }
1660 m_ConsoleType = Type;
1661}
1662
1663void CGameConsole::ConToggleLocalConsole(IConsole::IResult *pResult, void *pUserData)
1664{
1665 ((CGameConsole *)pUserData)->Toggle(Type: CONSOLETYPE_LOCAL);
1666}
1667
1668void CGameConsole::ConToggleRemoteConsole(IConsole::IResult *pResult, void *pUserData)
1669{
1670 ((CGameConsole *)pUserData)->Toggle(Type: CONSOLETYPE_REMOTE);
1671}
1672
1673void CGameConsole::ConClearLocalConsole(IConsole::IResult *pResult, void *pUserData)
1674{
1675 ((CGameConsole *)pUserData)->m_LocalConsole.ClearBacklog();
1676}
1677
1678void CGameConsole::ConClearRemoteConsole(IConsole::IResult *pResult, void *pUserData)
1679{
1680 ((CGameConsole *)pUserData)->m_RemoteConsole.ClearBacklog();
1681}
1682
1683void CGameConsole::ConDumpLocalConsole(IConsole::IResult *pResult, void *pUserData)
1684{
1685 ((CGameConsole *)pUserData)->m_LocalConsole.Dump();
1686}
1687
1688void CGameConsole::ConDumpRemoteConsole(IConsole::IResult *pResult, void *pUserData)
1689{
1690 ((CGameConsole *)pUserData)->m_RemoteConsole.Dump();
1691}
1692
1693void CGameConsole::ConConsolePageUp(IConsole::IResult *pResult, void *pUserData)
1694{
1695 CInstance *pConsole = ((CGameConsole *)pUserData)->CurrentConsole();
1696 pConsole->m_BacklogCurLine += pConsole->GetLinesToScroll(Direction: -1, LinesToScroll: pConsole->m_LinesRendered);
1697 pConsole->m_HasSelection = false;
1698}
1699
1700void CGameConsole::ConConsolePageDown(IConsole::IResult *pResult, void *pUserData)
1701{
1702 CInstance *pConsole = ((CGameConsole *)pUserData)->CurrentConsole();
1703 pConsole->m_BacklogCurLine -= pConsole->GetLinesToScroll(Direction: 1, LinesToScroll: pConsole->m_LinesRendered);
1704 pConsole->m_HasSelection = false;
1705 if(pConsole->m_BacklogCurLine < 0)
1706 pConsole->m_BacklogCurLine = 0;
1707}
1708
1709void CGameConsole::ConConsolePageTop(IConsole::IResult *pResult, void *pUserData)
1710{
1711 CInstance *pConsole = ((CGameConsole *)pUserData)->CurrentConsole();
1712 pConsole->m_BacklogCurLine += pConsole->GetLinesToScroll(Direction: -1, LinesToScroll: pConsole->m_LinesRendered);
1713 pConsole->m_HasSelection = false;
1714}
1715
1716void CGameConsole::ConConsolePageBottom(IConsole::IResult *pResult, void *pUserData)
1717{
1718 CInstance *pConsole = ((CGameConsole *)pUserData)->CurrentConsole();
1719 pConsole->m_BacklogCurLine = 0;
1720 pConsole->m_HasSelection = false;
1721}
1722
1723void CGameConsole::ConchainConsoleOutputLevel(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
1724{
1725 CGameConsole *pSelf = (CGameConsole *)pUserData;
1726 pfnCallback(pResult, pCallbackUserData);
1727 if(pResult->NumArguments())
1728 {
1729 pSelf->m_pConsoleLogger->SetFilter(CLogFilter{.m_MaxLevel: IConsole::ToLogLevelFilter(ConsoleLevel: g_Config.m_ConsoleOutputLevel)});
1730 }
1731}
1732
1733void CGameConsole::RequireUsername(bool UsernameReq)
1734{
1735 if((m_RemoteConsole.m_UsernameReq = UsernameReq))
1736 {
1737 m_RemoteConsole.m_aUser[0] = '\0';
1738 m_RemoteConsole.m_UserGot = false;
1739 }
1740}
1741
1742void CGameConsole::PrintLine(int Type, const char *pLine)
1743{
1744 if(Type == CONSOLETYPE_LOCAL)
1745 m_LocalConsole.PrintLine(pLine, Len: str_length(str: pLine), PrintColor: TextRender()->DefaultTextColor());
1746 else if(Type == CONSOLETYPE_REMOTE)
1747 m_RemoteConsole.PrintLine(pLine, Len: str_length(str: pLine), PrintColor: TextRender()->DefaultTextColor());
1748}
1749
1750void CGameConsole::OnConsoleInit()
1751{
1752 // init console instances
1753 m_LocalConsole.Init(pGameConsole: this);
1754 m_RemoteConsole.Init(pGameConsole: this);
1755
1756 m_pConsole = Kernel()->RequestInterface<IConsole>();
1757
1758 Console()->Register(pName: "toggle_local_console", pParams: "", Flags: CFGFLAG_CLIENT, pfnFunc: ConToggleLocalConsole, pUser: this, pHelp: "Toggle local console");
1759 Console()->Register(pName: "toggle_remote_console", pParams: "", Flags: CFGFLAG_CLIENT, pfnFunc: ConToggleRemoteConsole, pUser: this, pHelp: "Toggle remote console");
1760 Console()->Register(pName: "clear_local_console", pParams: "", Flags: CFGFLAG_CLIENT, pfnFunc: ConClearLocalConsole, pUser: this, pHelp: "Clear local console");
1761 Console()->Register(pName: "clear_remote_console", pParams: "", Flags: CFGFLAG_CLIENT, pfnFunc: ConClearRemoteConsole, pUser: this, pHelp: "Clear remote console");
1762 Console()->Register(pName: "dump_local_console", pParams: "", Flags: CFGFLAG_CLIENT, pfnFunc: ConDumpLocalConsole, pUser: this, pHelp: "Write local console contents to a text file");
1763 Console()->Register(pName: "dump_remote_console", pParams: "", Flags: CFGFLAG_CLIENT, pfnFunc: ConDumpRemoteConsole, pUser: this, pHelp: "Write remote console contents to a text file");
1764
1765 Console()->Register(pName: "console_page_up", pParams: "", Flags: CFGFLAG_CLIENT, pfnFunc: ConConsolePageUp, pUser: this, pHelp: "Previous page in console");
1766 Console()->Register(pName: "console_page_down", pParams: "", Flags: CFGFLAG_CLIENT, pfnFunc: ConConsolePageDown, pUser: this, pHelp: "Next page in console");
1767 Console()->Register(pName: "console_page_top", pParams: "", Flags: CFGFLAG_CLIENT, pfnFunc: ConConsolePageTop, pUser: this, pHelp: "Last page in console");
1768 Console()->Register(pName: "console_page_bottom", pParams: "", Flags: CFGFLAG_CLIENT, pfnFunc: ConConsolePageBottom, pUser: this, pHelp: "First page in console");
1769 Console()->Chain(pName: "console_output_level", pfnChainFunc: ConchainConsoleOutputLevel, pUser: this);
1770}
1771
1772void CGameConsole::OnInit()
1773{
1774 Engine()->SetAdditionalLogger(std::unique_ptr<ILogger>(m_pConsoleLogger));
1775 // add resize event
1776 Graphics()->AddWindowResizeListener(pFunc: [this]() {
1777 m_LocalConsole.UpdateBacklogTextAttributes();
1778 m_LocalConsole.m_HasSelection = false;
1779 m_RemoteConsole.UpdateBacklogTextAttributes();
1780 m_RemoteConsole.m_HasSelection = false;
1781 });
1782}
1783
1784void CGameConsole::OnStateChange(int NewState, int OldState)
1785{
1786 if(OldState <= IClient::STATE_ONLINE && NewState == IClient::STATE_OFFLINE)
1787 {
1788 m_RemoteConsole.m_UserGot = false;
1789 m_RemoteConsole.m_aUser[0] = '\0';
1790 m_RemoteConsole.m_Input.Clear();
1791 m_RemoteConsole.m_UsernameReq = false;
1792 }
1793}
1794