-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathConsoleHostGui.cpp
894 lines (717 loc) · 22.8 KB
/
ConsoleHostGui.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
/*
* This file is part of the CitizenFX project - http://citizen.re/
*
* See LICENSE and MENTIONS in the root of the source tree for information
* regarding licensing.
*/
#include "StdInc.h"
#include <imgui.h>
#include <imgui_internal.h>
#include <ConsoleHost.h>
#include <CoreConsole.h>
#include <ICoreGameInit.h>
#include <CfxRGBA.h>
#include <regex>
#if __has_include("nutsnbolts.h")
#include <nutsnbolts.h>
#endif
#include <se/Security.h>
#include <boost/circular_buffer.hpp>
#include <concurrent_queue.h>
bool IsNonProduction();
DLL_EXPORT console::Context* g_customConsoleContext;
console::Context* GetConsoleContext()
{
if (g_customConsoleContext)
{
return g_customConsoleContext;
}
return console::GetDefaultContext();
}
extern ImFont* consoleFontSmall;
extern ImFont* consoleFontTiny;
// following 2 functions based on https://www.programmingalgorithms.com/algorithm/hsl-to-rgb/cpp/
static float HueToRGB(float v1, float v2, float vH) {
if (vH < 0)
vH += 1;
if (vH > 1)
vH -= 1;
if ((6 * vH) < 1)
return (v1 + (v2 - v1) * 6 * vH);
if ((2 * vH) < 1)
return v2;
if ((3 * vH) < 2)
return (v1 + (v2 - v1) * ((2.0f / 3) - vH) * 6);
return v1;
}
struct HSL
{
int H;
float S;
float L;
};
static CRGBA HSLToRGB(HSL hsl) {
unsigned char r = 0;
unsigned char g = 0;
unsigned char b = 0;
if (hsl.S == 0)
{
r = g = b = (unsigned char)(hsl.L * 255);
}
else
{
float v1, v2;
float hue = (float)hsl.H / 360;
v2 = (hsl.L < 0.5) ? (hsl.L * (1 + hsl.S)) : ((hsl.L + hsl.S) - (hsl.L * hsl.S));
v1 = 2 * hsl.L - v2;
r = (unsigned char)(255 * HueToRGB(v1, v2, hue + (1.0f / 3)));
g = (unsigned char)(255 * HueToRGB(v1, v2, hue));
b = (unsigned char)(255 * HueToRGB(v1, v2, hue - (1.0f / 3)));
}
return CRGBA(r, g, b);
}
struct FiveMConsoleBase
{
boost::circular_buffer<std::string> Items{ 2500 };
boost::circular_buffer<std::string> ItemKeys{ 2500 };
std::recursive_mutex ItemsMutex;
// Portable helpers
static int Stricmp(const char* str1, const char* str2) { int d; while ((d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; } return d; }
static int Strnicmp(const char* str1, const char* str2, int n) { int d = 0; while (n > 0 && (d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; n--; } return d; }
static char* Strdup(const char* str) { size_t len = strlen(str) + 1; void* buff = malloc(len); return (char*)memcpy(buff, (const void*)str, len); }
virtual void AddLog(const char* key, const char* fmt, ...)
{
char buf[1024];
va_list args;
va_start(args, fmt);
vsnprintf(buf, _countof(buf), fmt, args);
buf[_countof(buf) - 1] = 0;
va_end(args);
{
std::unique_lock<std::recursive_mutex> lock(ItemsMutex);
ItemKeys.push_back(key);
Items.push_back(buf);
OnAddLog(key, buf);
}
}
virtual bool FilterLog(const std::string& channel)
{
return true;
}
virtual void OnAddLog(std::string_view key, std::string_view message)
{
}
virtual void UpdateLog(int idx)
{
}
virtual void Draw(const char* title, bool* p_open) = 0;
virtual void DrawItem(int i, float alpha = 1.0f, bool fullBg = false)
{
const auto& item = Items[i];
auto key = ItemKeys[i];
if (strlen(key.c_str()) > 0 && strlen(item.c_str()) > 0)
{
ImGui::PushFont(consoleFontSmall);
auto hue = int{ HashRageString(key.c_str()) % 360 };
auto color = HSLToRGB(HSL{ hue, 0.8f, 0.4f });
color.alpha = alpha * 255.0f;
std::string refStr;
if (fullBg)
{
key = fmt::sprintf("[%s]:", key);
}
auto textSize = ImGui::CalcTextSize(key.c_str());
auto dl = ImGui::GetCurrentContext()->CurrentWindow->DrawList;
auto startPos = ImGui::GetCursorScreenPos();
ImVec2 itemSize(0.0, 0.0);
if (fullBg)
{
itemSize = ImGui::CalcTextSize(item.c_str());
itemSize.x += 8.0f;
color.alpha *= 0.8f;
}
dl->AddRectFilled(ImVec2(startPos.x, startPos.y + 1.0f), ImVec2(startPos.x + textSize.x + itemSize.x + 8.0f, startPos.y + 22.f - 1.0f), color.AsARGB(), fullBg ? 10.0f : 6.0f);
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 4.0f);
//ImGui::PushStyleColor(ImGuiCol_Text, col);
ImGui::TextUnformatted(key.c_str());
//ImGui::PopStyleColor();
ImGui::PopFont();
ImGui::SameLine(textSize.x + 16.0f);
}
ImVec4 col = ImVec4(1.0f, 1.0f, 1.0f, alpha);
if (strstr(item.c_str(), "[error]"))
col = ImColor(1.0f, 0.4f, 0.4f, alpha);
else if (strncmp(item.c_str(), "# ", 2) == 0)
col = ImColor(1.0f, 0.78f, 0.58f, alpha);
ImGui::PushStyleColor(ImGuiCol_Text, col);
ImGui::TextUnformatted(item.c_str());
ImGui::PopStyleColor();
}
virtual void ClearLog()
{
std::unique_lock<std::recursive_mutex> lock(ItemsMutex);
Items.clear();
ItemKeys.clear();
}
};
extern float g_menuHeight;
struct CfxBigConsole : FiveMConsoleBase
{
char InputBuf[1024];
bool ScrollToBottom;
ImVector<char*> History;
int HistoryPos; // -1: new line, 0..History.Size-1 browsing history.
ImVector<const char*> Commands;
concurrency::concurrent_queue<std::string> CommandQueue;
CfxBigConsole()
{
ClearLog();
memset(InputBuf, 0, sizeof(InputBuf));
HistoryPos = -1;
Commands.push_back("HELP");
Commands.push_back("HISTORY");
Commands.push_back("CLEAR");
Commands.push_back("LOADLEVEL");
Commands.push_back("CONNECT");
Commands.push_back("QUIT");
Commands.push_back("NETGRAPH");
Commands.push_back("STRDBG");
}
virtual ~CfxBigConsole()
{
ClearLog();
for (int i = 0; i < History.Size; i++)
free(History[i]);
}
virtual void ClearLog() override
{
FiveMConsoleBase::ClearLog();
ScrollToBottom = true;
}
virtual void OnAddLog(std::string_view key, std::string_view msg) override
{
// TODO: figure out if scrolled up somehow?
ScrollToBottom = true;
}
void Draw(const char* title, bool* p_open) override
{
#ifndef IS_FXSERVER
if (GetKeyState(VK_CONTROL) & 0x8000 && GetKeyState(VK_MENU) & 0x8000)
{
return;
}
#endif
ImGui::SetNextWindowPos(ImVec2(ImGui::GetMainViewport()->Pos.x + 0, ImGui::GetMainViewport()->Pos.y + g_menuHeight));
ImGui::SetNextWindowSize(ImVec2(ImGui::GetIO().DisplaySize.x,
#ifndef IS_FXSERVER
ImGui::GetFrameHeightWithSpacing() * 12.0f
#else
ImGui::GetIO().DisplaySize.y - g_menuHeight
#endif
), ImGuiCond_Always);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoSavedSettings |
ImGuiWindowFlags_NoBringToFrontOnFocus;
if (!ImGui::Begin(title, nullptr, flags))
{
ImGui::End();
ImGui::PopStyleVar();
return;
}
std::unique_lock<std::recursive_mutex> lock(ItemsMutex);
/*ImGui::TextWrapped("This example implements a console with basic coloring, completion and history. A more elaborate implementation may want to store entries along with extra data such as timestamp, emitter, etc.");
ImGui::TextWrapped("Enter 'HELP' for help, press TAB to use text completion.");
// TODO: display items starting from the bottom
if (ImGui::SmallButton("Add Dummy Text")) { AddLog("%d some text", Items.Size); AddLog("some more text"); AddLog("display very important message here!"); } ImGui::SameLine();
if (ImGui::SmallButton("Add Dummy Error")) AddLog("[error] something went wrong"); ImGui::SameLine();
if (ImGui::SmallButton("Clear")) ClearLog(); ImGui::SameLine();
if (ImGui::SmallButton("Scroll to bottom")) ScrollToBottom = true;
//static float t = 0.0f; if (ImGui::GetTime() - t > 0.02f) { t = ImGui::GetTime(); AddLog("Spam %f", t); }
ImGui::Separator();
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
static ImGuiTextFilter filter;
filter.Draw("Filter (\"incl,-excl\") (\"error\")", 180);
ImGui::PopStyleVar();
ImGui::Separator();*/
ImGui::BeginChild("ScrollingRegion", ImVec2(0, -ImGui::GetFrameHeightWithSpacing()), false, 0);
// Display every line as a separate entry so we can change their color or add custom widgets. If you only want raw text you can use ImGui::TextUnformatted(log.begin(), log.end());
// NB- if you have thousands of entries this approach may be too inefficient and may require user-side clipping to only process visible items.
// You can seek and display only the lines that are visible using the ImGuiListClipper helper, if your elements are evenly spaced and you have cheap random access to the elements.
// To use the clipper we could replace the 'for (int i = 0; i < Items.Size; i++)' loop with:
// ImGuiListClipper clipper(Items.Size);
// while (clipper.Step())
// for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)
// However take note that you can not use this code as is if a filter is active because it breaks the 'cheap random-access' property. We would need random-access on the post-filtered list.
// A typical application wanting coarse clipping and filtering may want to pre-compute an array of indices that passed the filtering test, recomputing this array when user changes the filter,
// and appending newly elements as they are inserted. This is left as a task to the user until we can manage to improve this example code!
// If your items are of variable size you may want to implement code similar to what ImGuiListClipper does. Or split your data into fixed height items to allow random-seeking into your list.
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1)); // Tighten spacing
ImGuiListClipper clipper(Items.size());
while (clipper.Step())
{
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)
{
DrawItem(i);
}
}
if (ScrollToBottom)
ImGui::SetScrollHere();
ScrollToBottom = false;
ImGui::PopStyleVar();
ImGui::EndChild();
ImGui::Separator();
// Command-line
ImGui::PushItemWidth(-1.0f);
if (ImGui::InputText("_", InputBuf, _countof(InputBuf), ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory, &TextEditCallbackStub, (void*)this))
{
char* input_end = InputBuf + strlen(InputBuf);
while (input_end > InputBuf && input_end[-1] == ' ') input_end--; *input_end = 0;
if (InputBuf[0])
ExecCommand(InputBuf);
strcpy(InputBuf, "");
}
ImGui::PopItemWidth();
// Demonstrate keeping auto focus on the input box
if (ImGui::IsItemHovered() || (ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows) && !ImGui::IsAnyItemActive() && !ImGui::IsMouseClicked(0)))
ImGui::SetKeyboardFocusHere(-1); // Auto focus previous widget
ImGui::End();
ImGui::PopStyleVar();
}
void ExecCommand(const char* command_line)
{
AddLog("", "> %s\n", command_line);
// Insert into history. First find match and delete it so it can be pushed to the back. This isn't trying to be smart or optimal.
HistoryPos = -1;
for (int i = History.Size - 1; i >= 0; i--)
if (Stricmp(History[i], command_line) == 0)
{
free(History[i]);
History.erase(History.begin() + i);
break;
}
History.push_back(Strdup(command_line));
// Process command
if (Stricmp(command_line, "clear") == 0)
{
ClearLog();
}
else if (Stricmp(command_line, "history") == 0)
{
for (int i = History.Size >= 10 ? History.Size - 10 : 0; i < History.Size; i++)
AddLog("history", "%3d: %s\n", i, History[i]);
}
else
{
CommandQueue.push(command_line);
}
}
static int TextEditCallbackStub(ImGuiTextEditCallbackData* data) // In C++11 you are better off using lambdas for this sort of forwarding callbacks
{
CfxBigConsole* console = (CfxBigConsole*)data->UserData;
return console->TextEditCallback(data);
}
int TextEditCallback(ImGuiTextEditCallbackData* data)
{
//AddLog("cursor: %d, selection: %d-%d", data->CursorPos, data->SelectionStart, data->SelectionEnd);
switch (data->EventFlag)
{
case ImGuiInputTextFlags_CallbackCompletion:
{
// Example of TEXT COMPLETION
// Locate beginning of current word
const char* word_end = data->Buf + data->CursorPos;
const char* word_start = word_end;
while (word_start > data->Buf)
{
const char c = word_start[-1];
if (c == ' ' || c == '\t' || c == ',' || c == ';')
break;
word_start--;
}
// Build a list of candidates
std::set<std::string> candidates;
auto hasAccess = [](const std::string& command)
{
if (IsNonProduction())
{
return true;
}
se::ScopedPrincipalReset reset;
se::ScopedPrincipal principal{
se::Principal{
"system.extConsole" }
};
return seCheckPrivilege(fmt::sprintf("command.%s", command));
};
GetConsoleContext()->GetCommandManager()->ForAllCommands([&](const std::string& command)
{
if (Strnicmp(command.c_str(), word_start, (int)(word_end - word_start)) == 0 && hasAccess(command))
{
candidates.insert(command);
}
});
console::GetDefaultContext()->GetCommandManager()->ForAllCommands([&](const std::string& command)
{
if (Strnicmp(command.c_str(), word_start, (int)(word_end - word_start)) == 0 && hasAccess(command))
{
candidates.insert(command);
}
});
if (candidates.empty())
{
// No match
AddLog("", "No match for \"%.*s\"!\n", (int)(word_end - word_start), word_start);
}
else if (candidates.size() == 1)
{
// Single match. Delete the beginning of the word and replace it entirely so we've got nice casing
data->DeleteChars((int)(word_start - data->Buf), (int)(word_end - word_start));
data->InsertChars(data->CursorPos, candidates.begin()->c_str());
data->InsertChars(data->CursorPos, " ");
}
else
{
// Multiple matches. Complete as much as we can, so inputing "C" will complete to "CL" and display "CLEAR" and "CLASSIFY"
int match_len = (int)(word_end - word_start);
for (;;)
{
int c = 0;
bool all_candidates_matches = true;
int i = 0;
for (const auto& candidate : candidates)
{
if (i == 0)
c = toupper(candidate[match_len]);
else if (c == 0 || c != toupper(candidate[match_len]))
all_candidates_matches = false;
i++;
if (!all_candidates_matches)
{
break;
}
}
if (!all_candidates_matches)
break;
match_len++;
}
if (match_len > 0)
{
data->DeleteChars((int)(word_start - data->Buf), (int)(word_end - word_start));
data->InsertChars(data->CursorPos, candidates.begin()->c_str(), candidates.begin()->c_str() + match_len);
}
// List matches
AddLog("", "Possible matches:\n");
for (const auto& candidate : candidates)
AddLog("", "- %s\n", candidate.c_str());
}
break;
}
case ImGuiInputTextFlags_CallbackHistory:
{
// Example of HISTORY
const int prev_history_pos = HistoryPos;
if (data->EventKey == ImGuiKey_UpArrow)
{
if (HistoryPos == -1)
HistoryPos = History.Size - 1;
else if (HistoryPos > 0)
HistoryPos--;
}
else if (data->EventKey == ImGuiKey_DownArrow)
{
if (HistoryPos != -1)
if (++HistoryPos >= History.Size)
HistoryPos = -1;
}
// A better implementation would preserve the data on the current input line along with cursor position.
if (prev_history_pos != HistoryPos)
{
data->CursorPos = data->SelectionStart = data->SelectionEnd = data->BufTextLen = (int)_snprintf(data->Buf, (size_t)data->BufSize, "%s", (HistoryPos >= 0) ? History[HistoryPos] : "");
data->BufDirty = true;
}
}
}
return 0;
}
void RunCommandQueue()
{
std::string command_line;
while (CommandQueue.try_pop(command_line))
{
se::ScopedPrincipal scope(se::Principal{ (IsNonProduction()) ? "system.console" : "system.extConsole" });
GetConsoleContext()->AddToBuffer(command_line);
GetConsoleContext()->AddToBuffer("\n");
GetConsoleContext()->ExecuteBuffer();
}
}
virtual bool FilterLog(const std::string& channel) override
{
if (IsNonProduction())
{
return true;
}
if (channel == "script:game:nui")
{
return false;
}
if (channel == "cmd")
{
return true;
}
return channel.find("script:") == 0;
}
};
auto msec()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch()
);
}
#include <boost/algorithm/string.hpp>
struct MiniConsole : CfxBigConsole
{
ImVector<std::chrono::milliseconds> ItemTimes;
ConVar<std::string>* m_miniconChannels;
std::string m_miniconLastValue;
std::regex m_miniconRegex;
MiniConsole()
{
m_miniconChannels = new ConVar<std::string>("con_miniconChannels", ConVar_Archive, "minicon:*");
m_miniconLastValue = m_miniconChannels->GetValue();
try
{
m_miniconRegex = MakeRegex(m_miniconLastValue);
}
catch (std::exception & e)
{
m_miniconLastValue = "minicon:*";
m_miniconChannels->GetHelper()->SetValue("minicon:*");
}
}
virtual void Draw(const char* title, bool* p_open) override
{
auto& io = ImGui::GetIO();
ImGui::PushFont(consoleFontSmall);
ImGui::SetNextWindowBgAlpha(0.0f);
ImGui::SetNextWindowPos(ImVec2(ImGui::GetMainViewport()->Pos.x + 20, ImGui::GetMainViewport()->Pos.y + io.DisplaySize.y - 20), ImGuiCond_Always, ImVec2(0.0f, 1.0f));
ImGui::SetNextWindowSize(ImVec2(io.DisplaySize.x * 0.4f, ImGui::GetFrameHeightWithSpacing() * 12.0f));
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 4)); // Tighten spacing
if (ImGui::Begin("MiniCon", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar))
{
auto t = msec();
std::unique_lock<std::recursive_mutex> lock(ItemsMutex);
// clip old entries
{
while ((ItemTimes.size() > 1 && (t - ItemTimes[0]) > std::chrono::seconds(10)) || Items.size() > 14)
{
Items.erase(Items.begin());
ItemKeys.erase(ItemKeys.begin());
ItemTimes.erase(ItemTimes.begin());
}
if (ItemTimes.size() == 1 && ((t - ItemTimes[0]) > std::chrono::seconds(10)))
{
Items[0] = "";
ItemKeys[0] = "";
ItemTimes[0] = t;
}
}
ImGuiListClipper clipper(Items.size());
while (clipper.Step())
{
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)
{
auto deltaTime = t - ItemTimes[i];
float alpha = 1.0f;
if (deltaTime < std::chrono::milliseconds(100))
{
alpha = deltaTime.count() / 100.0f;
}
else if (deltaTime > std::chrono::seconds(10) - std::chrono::milliseconds(100))
{
alpha = (std::chrono::seconds(10) - deltaTime).count() / 100.0f;
}
DrawItem(i, alpha, true);
}
}
ImGui::SetScrollHere();
}
ImGui::PopStyleVar();
ImGui::PopStyleVar();
ImGui::End();
ImGui::PopFont();
}
virtual void ClearLog() override
{
FiveMConsoleBase::ClearLog();
std::unique_lock<std::recursive_mutex> lock(ItemsMutex);
ItemTimes.clear();
}
virtual bool FilterLog(const std::string& channel) override
{
std::unique_lock<std::recursive_mutex> lock(ItemsMutex);
if (m_miniconLastValue != m_miniconChannels->GetValue())
{
try
{
m_miniconRegex = MakeRegex(m_miniconChannels->GetValue());
m_miniconLastValue = m_miniconChannels->GetValue();
}
catch (std::exception& e)
{
m_miniconChannels->GetHelper()->SetValue(m_miniconLastValue);
}
}
return std::regex_match(channel, m_miniconRegex);
}
std::regex MakeRegex(const std::string& pattern)
{
std::string re = pattern;
re = std::regex_replace(re, std::regex{"[.^$|()\\[\\]{}?\\\\]"}, "\\$&");
boost::algorithm::replace_all(re, " ", "|");
boost::algorithm::replace_all(re, "+", "|");
boost::algorithm::replace_all(re, "*", ".*");
return std::regex{ "(?:" + re + ")", std::regex::icase };
}
virtual void UpdateLog(int idx) override
{
ItemTimes[idx] = msec();
}
virtual void OnAddLog(std::string_view key, std::string_view msg) override
{
ItemTimes.push_back(
msec()
);
ScrollToBottom = true;
}
};
static std::unique_ptr<FiveMConsoleBase> g_consoles[2];
static std::recursive_mutex g_consolesMutex;
static void EnsureConsoles()
{
std::unique_lock _(g_consolesMutex);
if (!g_consoles[0])
{
g_consoles[0] = std::make_unique<CfxBigConsole>();
g_consoles[1] = std::make_unique<MiniConsole>();
}
}
bool IsNonProduction()
{
#if !defined(GTA_FIVE) || defined(_DEBUG)
return true;
#else
static ConVar<int> moo("moo", ConVar_None, 0);
static auto isProd = ([]()
{
wchar_t resultPath[1024];
static std::wstring fpath = MakeRelativeCitPath(L"CitizenFX.ini");
GetPrivateProfileString(L"Game", L"UpdateChannel", L"production", resultPath, std::size(resultPath), fpath.c_str());
return _wcsicmp(resultPath, L"production") == 0 || _wcsicmp(resultPath, L"prod") == 0;
})();
return !isProd || moo.GetValue() == 31337;
#endif
}
void DrawConsole()
{
EnsureConsoles();
static bool pOpen = true;
g_consoles[0]->Draw("", &pOpen);
}
void DrawMiniConsole()
{
EnsureConsoles();
static bool pOpen = true;
g_consoles[1]->Draw("", &pOpen);
}
#include <sstream>
void SendPrintMessage(const std::string& channel, const std::string& message)
{
if (channel == "no_console")
{
return;
}
{
std::unique_lock _(g_consolesMutex);
EnsureConsoles();
for (auto& console : g_consoles)
{
if (console->Items.size() == 0)
{
console->AddLog("", "");
}
}
}
static bool wasNewLine;
std::stringstream msgStream;
auto flushStream = [&]()
{
auto str = msgStream.str();
for (auto& console : g_consoles)
{
if (console->FilterLog(channel))
{
std::unique_lock<std::recursive_mutex> lock(console->ItemsMutex);
auto strRef = (console->Items[console->Items.size() - 1] + str);
std::swap(console->Items[console->Items.size() - 1], strRef);
console->UpdateLog(console->Items.size() - 1);
}
}
msgStream.str("");
};
for (auto c = 0; c < message.size(); c++)
{
char b[2] = { message[c], 0 };
if (wasNewLine)
{
for (auto& console : g_consoles)
{
if (console->FilterLog(channel))
{
console->AddLog(channel.c_str(), "");
}
}
wasNewLine = false;
}
if (b[0] == '\n')
{
flushStream();
wasNewLine = true;
continue;
}
msgStream << std::string_view(b);
}
flushStream();
}
DLL_EXPORT void RunConsoleGameFrame()
{
if (g_consoles[0])
{
((CfxBigConsole*)g_consoles[0].get())->RunCommandQueue();
}
}
#if __has_include("nutsnbolts.h")
static InitFunction initFunction([]()
{
ConHost::OnShouldDrawGui.Connect([](bool* should)
{
if (g_consoles[1] && !*should)
{
std::unique_lock<std::recursive_mutex> lock(g_consoles[1]->ItemsMutex);
*should = *should || ((g_consoles[1]->Items.size() > 1) || (!g_consoles[1]->Items[0].empty()));
}
}, 999);
OnCriticalGameFrame.Connect([] { RunConsoleGameFrame(); });
});
#endif
static InitFunction initFunctionCon([]()
{
for (auto& command : { "connect", "quit", "cl_drawFPS", "bind", "rbind", "unbind", "disconnect", "storymode", "loadlevel", "cl_drawPerf", "profile_reticuleSize", "profile_musicVolumeInMp", "profile_musicVolume", "profile_sfxVolume" })
{
seGetCurrentContext()->AddAccessControlEntry(se::Principal{ "system.extConsole" }, se::Object{ fmt::sprintf("command.%s", command) }, se::AccessType::Allow);
}
});