-
Notifications
You must be signed in to change notification settings - Fork 2
/
GmodInterop.cs
393 lines (340 loc) · 15.8 KB
/
GmodInterop.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
using GTerm.Extensions;
using Microsoft.Win32;
using Spectre.Console;
using System.Diagnostics;
using System.Runtime.InteropServices;
using VdfParser;
namespace GTerm
{
internal class GmodInterop
{
private const string GMOD_ID = "4000";
private static Process? GetGmodProcess()
=> Process.GetProcessesByName("gmod").FirstOrDefault();
private static bool TryGetSteamVDFPath(out string vdfPath)
{
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
string? steamInstallPath = steamInstallPath = Registry.GetValue(@"HKEY_CLASSES_ROOT\steamlink\Shell\Open\Command", null, null) as string;
if (string.IsNullOrWhiteSpace(steamInstallPath))
{
steamInstallPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
vdfPath = Path.Combine(steamInstallPath, "Steam/steamapps/libraryfolders.vdf");
if (!File.Exists(vdfPath))
{
steamInstallPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
vdfPath = Path.Combine(steamInstallPath, "Steam/steamapps/libraryfolders.vdf");
return File.Exists(vdfPath);
}
return true;
}
else
{
steamInstallPath = Path.GetDirectoryName(steamInstallPath.Split('-').First().Replace("\"", string.Empty));
if (steamInstallPath == null)
{
vdfPath = "?";
return false;
}
vdfPath = Path.Combine(steamInstallPath, "steamapps/libraryfolders.vdf");
return File.Exists(vdfPath);
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
string? homeDir = Environment.GetEnvironmentVariable("HOME");
if (homeDir != null)
{
vdfPath = Path.Join(homeDir, "/Library/Application Support/Steam/steamapps/libraryfolders.vdf");
return File.Exists(vdfPath);
}
}
else
{
string? homeDir = Environment.GetEnvironmentVariable("HOME");
if (homeDir != null)
{
vdfPath = Path.Join(homeDir, "/.steam/steam/steamapps/libraryfolders.vdf");
if (!File.Exists(vdfPath))
{
vdfPath = Path.Join(homeDir, "/.local/share/Steam/steamapps/libraryfolders.vdf");
if (!File.Exists(vdfPath))
{
// flatpak
vdfPath = Path.Join(homeDir, "/.var/app/com.valvesoftware.Steam/.local/share/Steam/steamapps/libraryfolders.vdf");
}
}
return File.Exists(vdfPath);
}
}
}
catch {}
vdfPath = "?";
return false;
}
private static bool TryGetMountedBeta(out bool isX64)
{
if (TryGetSteamVDFPath(out string vdfPath))
{
try
{
string? vdfDirPath = Path.GetDirectoryName(vdfPath);
if (vdfDirPath != null)
{
string gmodManifiestPath = Path.Join(vdfDirPath, "appmanifest_4000.acf");
if (File.Exists(gmodManifiestPath))
{
FileStream gmodManifestFile = File.OpenRead(gmodManifiestPath);
VdfDeserializer deserializer = new();
dynamic result = deserializer.Deserialize(gmodManifestFile);
isX64 = result?.AppState?.UserConfig?.BetaKey == "x86-64";
return true;
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Could not find Garry's Mod manifest, assuming branch\n" + ex.Message);
}
}
isX64 = false;
return false;
}
internal static bool TryGetGmodPath(out string gmodPath, bool toBin = true)
{
try
{
// push this in priority
// fallback for installations from steamcmd and other weird things
// this will require gmod restart
Process? gmodProcess = GetGmodProcess();
if (gmodProcess != null)
{
string? path = gmodProcess.MainModule?.FileName.Replace('\\', '/');
if (path != null)
{
if (toBin)
{
gmodPath = path;
return true;
}
int index = path.IndexOf("GarrysMod/bin");
gmodPath = Path.Combine(path[..index], "GarrysMod");
return true;
}
}
if (TryGetSteamVDFPath(out string steamLibsDescFilePath))
{
FileStream libDescFile = File.OpenRead(steamLibsDescFilePath);
VdfDeserializer deserializer = new();
dynamic result = deserializer.Deserialize(libDescFile);
foreach (dynamic kv in result.libraryfolders)
{
if (!int.TryParse(kv.Key, out int _)) continue; // dont take things that arent steam libs
LocalLogger.WriteLine("Found Steam game library at :", kv.Value.path);
foreach (dynamic appKv in kv.Value.apps)
{
if (appKv.Key != GMOD_ID) continue;
gmodPath = Path.Combine(kv.Value.path, "steamapps/common/GarrysMod");
if (toBin)
{
string gmodPathDir = gmodPath;
bool gotBeta = TryGetMountedBeta(out bool isX64);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (gotBeta)
{
gmodPath = isX64
? Path.Combine(gmodPathDir, "bin/win64/gmod.exe")
: Path.Combine(gmodPathDir, "bin/gmod.exe");
}
else
{
// get x64 bin path in priority
string gmodPathX64 = Path.Combine(gmodPathDir, "bin/win64/gmod.exe");
string gmodPathX86 = Path.Combine(gmodPathDir, "bin/gmod.exe");
if (File.Exists(gmodPathX64))
gmodPath = gmodPathX64;
else if (File.Exists(gmodPathX86))
gmodPath = gmodPathX86;
}
if (!File.Exists(gmodPath))
gmodPath = Path.Combine(gmodPathDir, "hl2.exe");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
gmodPath = Path.Combine(gmodPathDir, "hl2_osx");
}
else
{
if (gotBeta) {
gmodPath = isX64 ? Path.Combine(gmodPathDir, "linux64/gmod") : Path.Combine(gmodPathDir, "hl2_linux");
if (!File.Exists(gmodPath))
gmodPath = isX64 ? Path.Combine(gmodPathDir, "linux64/srcds") : Path.Combine(gmodPathDir, "srcds");
}
else
{
gmodPath = Path.Combine(gmodPathDir, "hl2_linux");
if (!File.Exists(gmodPath))
gmodPath = Path.Combine(gmodPathDir, "srcds");
}
}
}
return true;
}
}
}
LocalLogger.WriteLine("Could not find Gmod directory: Maybe it's not installed or installed via steamcmd?");
gmodPath = string.Empty;
return false;
}
catch (Exception ex)
{
LocalLogger.WriteLine("Could not find Gmod directory: ", ex.Message);
gmodPath = string.Empty;
return false;
}
}
private static string GetBinaryFileName(bool isX64)
{
string moduleName = "gmsv_xconsole_";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
moduleName += (isX64 ? "win64" : "win32");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
// that's always x64
moduleName += "osx64";
}
else
{
moduleName += isX64 ? "linux64" : "linux";
}
moduleName += ".dll";
return moduleName;
}
private static async Task DownloadFileAsync(string url, string outputPath)
{
using HttpClient client = new();
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
byte[] content = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync(outputPath, content);
}
private static async Task<bool> InstallBinary(bool isX64, string luaBinPath)
{
string moduleName = GetBinaryFileName(isX64);
string targetXConsoleFilePath = Path.Combine(luaBinPath, moduleName);
if (!File.Exists(targetXConsoleFilePath))
{
string XConsoleUrl = $"https://raw.githubusercontent.com/Earu/GTerm/master/Modules/{moduleName}";
await DownloadFileAsync(XConsoleUrl, targetXConsoleFilePath);
return true;
}
return false;
}
private static bool IsGmodX64(string gmodBinPath)
{
// Base assumption in case it fails later (windows can do x86 and x64, linux/mac only x64)
bool isX64 = RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
|| gmodBinPath.Contains("win64", StringComparison.CurrentCulture)
|| gmodBinPath.Contains("linux64", StringComparison.CurrentCulture);
// Fetch the gmod manifest to make a safer assumption of the current branch
if (TryGetMountedBeta(out bool isBetaX64))
isX64 = isBetaX64;
return isX64;
}
internal static async Task<(bool, bool)> InstallXConsole()
{
bool modifiedGameFiles = false;
bool success = false;
if (!TryGetGmodPath(out string baseGmodPath, false) || !TryGetGmodPath(out string gmodBinPath)) return (success, modifiedGameFiles);
try
{
LocalLogger.WriteLine("Installing xconsole");
string luaPath = Path.Combine(baseGmodPath, "garrysmod/lua");
string luaBinPath = Path.Combine(luaPath, "bin");
if (!Directory.Exists(luaBinPath))
{
Directory.CreateDirectory(luaBinPath);
modifiedGameFiles = true;
}
bool isX64 = IsGmodX64(gmodBinPath);
bool justInstalledBin = await InstallBinary(isX64, luaBinPath);
if (justInstalledBin) {
modifiedGameFiles = true;
}
string menuInitFilePath = Path.Combine(luaPath, "menu/menu.lua");
string menuInitLuaCode = File.ReadAllText(menuInitFilePath);
if (!menuInitLuaCode.Contains("xconsole", StringComparison.CurrentCulture))
{
File.AppendAllText(menuInitFilePath, "\nrequire(\"xconsole\")\n");
modifiedGameFiles = true;
}
if (modifiedGameFiles)
{
LocalLogger.WriteLine("Installation complete!");
if (GetGmodProcess() != null)
{
AnsiConsole.MarkupLine("[white on red]Garry's Mod needs to be restarted for GTerm to work properly.[/]");
}
}
success = true;
}
catch (Exception ex)
{
LocalLogger.WriteLine("Could not install xconsole: ", ex.Message);
AnsiConsole.MarkupLine("[white on red]Could not install xconsole![/]");
}
return (success, modifiedGameFiles);
}
private static bool ConsoleEventCallback(int eventType)
{
if (eventType == 2)
GmodProcess?.Kill();
return false;
}
// Keeps it from getting garbage collected
private static Win32Extensions.ConsoleEventDelegate? handler;
private static Process? GmodProcess;
/// <summary>
/// Launches gmod in textmode
/// </summary>
/// <returns></returns>
internal static bool StartGmod()
{
if (!TryGetGmodPath(out string gmodBinPath)) return false;
GmodProcess = Process.Start(new ProcessStartInfo
{
Arguments = "-textmode",
FileName = gmodBinPath,
CreateNoWindow = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
});
if (GmodProcess == null) return false;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
// this makes sure gmod gets killed when GTerm is closed
handler = new Win32Extensions.ConsoleEventDelegate(ConsoleEventCallback);
Win32Extensions.SetConsoleCtrlHandler(handler, true);
Win32Extensions.ShowWindow(GmodProcess.MainWindowHandle, Win32Extensions.SW_HIDE);
Timer visibityTimer = new(timer =>
{
if (GmodProcess.HasExited)
{
(timer as Timer)?.Dispose();
Environment.Exit(GmodProcess.ExitCode);
return;
}
if (Win32Extensions.IsWindowVisible(GmodProcess.MainWindowHandle))
Win32Extensions.ShowWindow(GmodProcess.MainWindowHandle, Win32Extensions.SW_HIDE);
});
visibityTimer.Change(0, 100);
}
return true;
}
}
}