-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
372 lines (299 loc) · 14.4 KB
/
Program.cs
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
using GTerm.Extensions;
using GTerm.Listeners;
using Spectre.Console;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
namespace GTerm
{
class Program
{
private const string HEADER = @"
██████ ████████ ███████ ██████ ███ ███
██ ██ ██ ██ ██ ████ ████
██ ███ ██ █████ ██████ ██ ████ ██
██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ███████ ██ ██ ██ ██
";
private static readonly object Locker = new();
private static readonly StringBuilder LogBuffer = new();
private static readonly StringBuilder MarkupBuffer = new();
private static readonly StringBuilder InputBuffer = new();
private static readonly Thread UserInputThread = new(ProcessUserInput);
private static readonly ILogListener Listener = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? new WindowsLogListener()
: new UnixLogListener();
private static string ArchivePath = string.Empty;
private static Config Config = new();
private static WebSocketAPI? API;
static void Main(string[] args)
{
LocalLogger.WriteLine("Starting up...");
// prevent running it multiple times
Process curProc = Process.GetCurrentProcess();
string processName = curProc.ProcessName;
if (Process.GetProcesses().Count(p => p.ProcessName == processName) > 1)
{
LocalLogger.WriteLine("Another instance of GTerm is running, aborting");
return;
}
SetMetadata();
Config = new Config(args);
Process? parent = curProc.GetParent();
if (parent?.MainModule?.ModuleName == "gmod.exe")
{
LocalLogger.WriteLine("Started from Gmod!");
Config.StartAsGmod = false; // this cannot be true if gmod already runs GTerm as a child process
if (Config.MonitorGmod)
{
LocalLogger.WriteLine("Gmod monitoring starting");
parent.EnableRaisingEvents = true;
parent.Exited += (_, __) =>
{
if (parent.ExitCode == 0) return;
LocalLogger.WriteLine("Gmod crashed, attempting to restart!");
ProcessStartInfo oldStartInfo = parent.StartInfo;
if (GmodInterop.TryGetGmodPath(out string gmodBinpath)) {
oldStartInfo.FileName = gmodBinpath;
LocalLogger.WriteLine("Restarting Gmod!");
Process.Start(parent.StartInfo); // reboot gmod because crash and it was the owning process
curProc.Kill(); // we also kill the current process, because its likely it will be rebooted from gmod
}
};
}
}
string? processPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule?.FileName);
if (processPath != null)
{
ArchivePath = Path.Combine(processPath, "Archives");
if (Config.ArchiveLogs)
{
if (!Directory.Exists(ArchivePath))
{
LocalLogger.WriteLine("Archives directory was not found, creating it at: ", ArchivePath);
Directory.CreateDirectory(ArchivePath);
}
}
}
Task.Run(GmodInterop.InstallXConsole); // try to install xconsole
if (Config.StartAsGmod)
{
if (!GmodInterop.StartGmod()) return;
}
ShowWaitingConnection();
Listener.OnConnected += (_, __) =>
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(new string('=', Console.BufferWidth - 1));
Console.WriteLine(HEADER);
Console.WriteLine(new string('=', Console.BufferWidth - 1));
Console.WriteLine("Welcome to GTerm! You can read your Garry's Mod logs from here in real-time.");
Console.WriteLine("You can also execute any command you normally would by typing here!");
Console.WriteLine();
Console.WriteLine();
Console.Write("If you have any issue with GTerm please open an issue at: ");
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("https://github.com/Earu/GTerm/issues");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Enjoy your stay!");
Console.Write("\n\n\n");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
};
Listener.OnDisconnected += (_, __) =>
{
AnsiConsole.Markup("[bold red]Disconnected[/]");
ShowWaitingConnection();
};
Listener.OnLog += OnLog;
Listener.OnError += OnError;
Listener.Start();
UserInputThread.Start();
if (Config.API)
{
API = new WebSocketAPI(Listener, Config.APIPort, Config.APISecret);
Task.Run(API.Start);
}
}
private static void SetMetadata()
{
Console.Clear();
LocalLogger.WriteLine("Setting metadata");
Console.Title = "GTerm";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && GmodInterop.TryGetGmodPath(out string gmodPath))
{
// copy icon from the gmod exe
Win32Extensions.SetConsoleIcon(gmodPath);
LocalLogger.WriteLine("Set icon metadata");
}
else
{
LocalLogger.WriteLine("Failed to set icon metadata, gmod path not found");
}
}
private static void ProcessUserInput()
{
Console.WriteLine(); // on UNIX this prevents a terminal deadlock
while (true)
{
if (!Console.KeyAvailable) {
Thread.Sleep(100);
continue;
}
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
switch (keyInfo.Key)
{
case ConsoleKey.Enter:
{
string input = InputBuffer.ToString();
InputBuffer.Clear();
if (string.IsNullOrWhiteSpace(input.Trim())) continue;
if (input.Trim().Equals("clear", StringComparison.CurrentCultureIgnoreCase))
Console.Clear();
_ = Listener.WriteMessage(input);
Console.CursorLeft = 0;
Console.Write(new string(' ', Console.BufferWidth - 1));
}
break;
case ConsoleKey.Backspace:
if (InputBuffer.Length > 0)
InputBuffer.Remove(InputBuffer.Length - 1, 1);
int oldCursorLeft = Console.CursorLeft;
int oldCursorTop = Console.CursorTop;
Console.CursorLeft = 0;
Console.Write(new string(' ', Console.BufferWidth - 1));
Console.CursorLeft = oldCursorLeft;
Console.CursorTop = oldCursorTop;
break;
case ConsoleKey.Escape:
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
IntPtr hWndConsole = Win32Extensions.GetConsoleWindow();
Win32Extensions.ShowWindow(hWndConsole, Win32Extensions.SW_MINIMIZE);
}
break;
default:
InputBuffer.Append(keyInfo.KeyChar);
if (InputBuffer.Length > 255) // source console is limited to 255 characters
InputBuffer.Remove(254, InputBuffer.Length - 255);
break;
}
Console.Write("\r" + InputBuffer.ToString());
}
}
private static void ShowWaitingConnection() => AnsiConsole.Status()
.AutoRefresh(false)
.Spinner(Spinner.Known.Aesthetic)
.SpinnerStyle(Style.Parse("red bold"))
.StartAsync("Waiting for Garry's Mod connection...", async ctx =>
{
while (true)
{
if (Listener.IsConnected)
{
ctx.Status = "Connected";
ctx.SpinnerStyle(Style.Parse("green cold"));
ctx.Spinner(Spinner.Known.Star);
}
ctx.Refresh();
await Task.Delay(500);
}
});
private static void OnError(object sender, ErrorEventArgs e)
{
string stackTrace = SanitizeLogMessage(e.GetException().ToString());
AnsiConsole.Markup($"[italic red]{stackTrace}[/]");
}
private static bool IsBlack(System.Drawing.Color col)
=> col.R == 0 && col.G == 0 && col.B == 0;
private static string SanitizeLogMessage(string msg)
{
msg = Markup.Escape(msg);
return msg;
}
private static bool ShouldExcludeLog(string log)
{
if (Config == null) return false;
foreach (Regex pattern in Config.ExclusionPatterns)
{
if (pattern.IsMatch(log)) return true;
}
return false;
}
private static void OnLog(object sender, LogEventArgs args)
{
lock (Locker)
{
DateTime now = DateTime.Now;
string timeStamp = now.ToString("hh:mm:ss");
System.Drawing.Color col = IsBlack(args.Color) ? System.Drawing.Color.White : args.Color;
string msg = args.Message;
int newLineIndex = msg.IndexOf('\n');
while (newLineIndex != -1)
{
if (MarkupBuffer.Length == 0 && LogBuffer.Length == 0)
{
MarkupBuffer.Append($"[#ffaf00]{timeStamp}[/] | ");
LogBuffer.Append($"{timeStamp} | ");
API?.StartData(now);
}
string chunk = string.Concat(msg.AsSpan(0, newLineIndex), "\n");
string nextChunk = msg.Length > newLineIndex + 1 ? msg[chunk.Length..] : string.Empty;
LogBuffer.Append(chunk);
MarkupBuffer.Append($"[rgb({col.R},{col.G},{col.B})]{SanitizeLogMessage(chunk)}[/]");
API?.AppendData(col, chunk);
string mk = MarkupBuffer.ToString();
string log = LogBuffer.ToString();
MarkupBuffer.Clear();
LogBuffer.Clear();
API?.FinishDataAsync();
string logChunk = log.Contains('|', StringComparison.CurrentCulture) ? string.Join("|", log.Split('|').Skip(1).ToArray()) : log; // there should always be 1
if (!string.IsNullOrWhiteSpace(logChunk))
{
if (ShouldExcludeLog(logChunk)) return;
int currentTopCursor = Console.CursorTop;
int currentLeftCursor = Console.CursorLeft;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
Console.MoveBufferArea(0, currentTopCursor, Console.WindowWidth, 1, 0, Math.Min(Console.BufferHeight - 1, currentTopCursor + 1));
}
Console.CursorTop = Math.Min(Console.BufferHeight - 1, currentTopCursor);
Console.CursorLeft = 0;
AnsiConsole.Markup(mk);
Console.Out.Flush();
// this makes typing when the console is filled more stable
if (currentTopCursor + 1 >= Console.BufferHeight)
{
Console.Write(InputBuffer.ToString());
}
Console.CursorTop = Math.Min(Console.BufferHeight - 1, currentTopCursor + 1);
Console.CursorLeft = currentLeftCursor;
if (Config.ArchiveLogs)
{
string fileName = $"{DateTime.Now.ToString("d").Replace("/", "_")}.log";
File.AppendAllText(Path.Combine(ArchivePath, fileName), log);
}
}
msg = nextChunk;
newLineIndex = msg.IndexOf('\n');
if (msg.Length == 0) break;
}
if (MarkupBuffer.Length == 0 && LogBuffer.Length == 0)
{
MarkupBuffer.Append($"[#ffaf00]{timeStamp}[/] | ");
LogBuffer.Append($"{timeStamp} | ");
API?.StartData(now);
}
if (msg.Length > 0)
{
LogBuffer.Append(msg);
MarkupBuffer.Append($"[rgb({col.R},{col.G},{col.B})]{SanitizeLogMessage(msg)}[/]");
API?.AppendData(col, msg);
}
}
}
}
}