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