-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathDetector.cs
More file actions
331 lines (291 loc) · 11.7 KB
/
Copy pathPathDetector.cs
File metadata and controls
331 lines (291 loc) · 11.7 KB
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
#nullable enable
using System;
using System.IO;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using Serilog;
namespace GModContentWizard
{
/// <summary>
/// Detects and locates the Garry's Mod addons directory on Windows and Linux systems.
/// </summary>
internal static class PathDetector
{
private static readonly string[] SearchPaths =
[
@"SteamLibrary\steamapps\common\GarrysMod\garrysmod\addons",
@"Program Files (x86)\Steam\steamapps\common\GarrysMod\garrysmod\addons"
];
private static readonly string[] LinuxSearchRoots =
[
".steam/steam/steamapps/common/GarrysMod",
".local/share/Steam/steamapps/common/GarrysMod",
"Steam/steamapps/common/GarrysMod"
];
/// <summary>
/// Attempts to automatically detect the Garry's Mod addons directory.
/// </summary>
/// <returns>The path to the addons directory if found, otherwise null.</returns>
public static string? Select()
{
Log.Information("PathDetector.Select() called");
if (OperatingSystem.IsWindows())
{
return SelectWindows();
}
else
{
return SelectLinux();
}
}
/// <summary>
/// Searches for Garry's Mod addons directory on Windows systems.
/// </summary>
/// <returns>The addons path if found, otherwise null.</returns>
private static string? SelectWindows()
{
Log.Information("Searching for Garry's Mod on Windows");
return SearchWindowsKnownPaths();
}
private static string? SearchWindowsKnownPaths()
{
Log.Information("Searching known paths for GMod");
var drives = DriveInfo.GetDrives()
.Where(d => d.DriveType == DriveType.Fixed || d.DriveType == DriveType.Removable)
.Select(d => d.Name.TrimEnd('\\'))
.ToList();
foreach (var driveLetter in drives)
{
foreach (var subPath in SearchPaths)
{
var testPath = Path.Combine(driveLetter, subPath);
Log.Debug("Checking: {Path}", testPath);
if (Directory.Exists(testPath))
{
Log.Information("Found GMod addons at known path: {Path}", testPath);
return testPath;
}
}
}
Log.Warning("No addons directory found automatically on Windows");
return null;
}
/// <summary>
/// Searches for Garry's Mod addons directory on Linux systems using find.
/// </summary>
/// <returns>The addons path if found, otherwise null.</returns>
private static string? SelectLinux()
{
Log.Information("Searching for Garry's Mod on Linux");
var searchReturn = SearchLinux();
if (searchReturn != null)
{
Log.Information("Addons path found automatically: {Path}", searchReturn);
return searchReturn;
}
return null;
}
/// <summary>
/// Uses the find command to locate hl2_linux binary and derive the addons path.
/// </summary>
/// <returns>The addons path if found, otherwise null.</returns>
private static string? SearchLinux()
{
string? home = null;
try
{
home = Environment.GetEnvironmentVariable("HOME");
if (string.IsNullOrEmpty(home))
{
Log.Warning("HOME environment variable not set");
return null;
}
var locatePath = SearchLinuxWithLocate(home);
if (locatePath != null)
return locatePath;
return SearchLinuxKnownPaths(home);
}
catch (Exception ex)
{
Log.Error(ex, "Error searching for GMod on Linux");
return home != null ? SearchLinuxKnownPaths(home) : null;
}
}
private static string? SearchLinuxWithLocate(string home)
{
try
{
var process = new System.Diagnostics.Process
{
StartInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = "locate",
Arguments = "-l 10 hl2_linux",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
process.Start();
var output = process.StandardOutput.ReadToEnd();
var error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode == 0)
{
Log.Debug("locate output: {Output}", output);
var lines = output.Split('\n', StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var trimmedPath = line.Trim();
if (string.IsNullOrEmpty(trimmedPath)) continue;
if (trimmedPath.Contains("GarrysMod") && IsGarrysModHL2(trimmedPath))
{
var gmodRoot = System.IO.Path.GetDirectoryName(trimmedPath);
if (gmodRoot != null)
{
var addonsPath = System.IO.Path.Combine(gmodRoot, "garrysmod", "addons");
Log.Information("Found GMod addons via locate: {Path}", addonsPath);
return addonsPath;
}
}
}
}
else if (!string.IsNullOrEmpty(error))
{
Log.Debug("locate error: {Error}", error);
}
}
catch (Exception ex)
{
Log.Debug(ex, "locate not available or failed");
}
return null;
}
private static string? SearchLinuxKnownPaths(string home)
{
Log.Information("Searching known paths for GMod");
foreach (var root in LinuxSearchRoots)
{
var testPath = System.IO.Path.Combine(home, root);
var addonsPath = System.IO.Path.Combine(testPath, "garrysmod", "addons");
Log.Debug("Checking: {Path}", addonsPath);
if (System.IO.Directory.Exists(addonsPath) && IsValidGModPath(testPath))
{
Log.Information("Found GMod addons at known path: {Path}", addonsPath);
return addonsPath;
}
}
try
{
var process = new System.Diagnostics.Process
{
StartInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = "find",
Arguments = $"{home} -maxdepth 5 -type d -name \"GarrysMod\" 2>/dev/null",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
process.Start();
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
var lines = output.Split('\n', StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var trimmedPath = line.Trim();
if (string.IsNullOrEmpty(trimmedPath)) continue;
var addonsPath = System.IO.Path.Combine(trimmedPath, "garrysmod", "addons");
if (System.IO.Directory.Exists(addonsPath))
{
Log.Information("Found GMod addons via targeted find: {Path}", addonsPath);
return addonsPath;
}
}
}
catch (Exception ex)
{
Log.Debug(ex, "Fallback find failed");
}
return null;
}
/// <summary>
/// Verifies if the given path contains the Garry's Mod hl2 binary.
/// </summary>
/// <param name="hl2Path">Path to the hl2_linux binary.</param>
/// <returns>True if it's Garry's Mod, otherwise false.</returns>
private static bool IsGarrysModHL2(string hl2Path)
{
try
{
var gmodRoot = System.IO.Path.GetDirectoryName(hl2Path);
if (gmodRoot == null) return false;
return IsValidGModPath(gmodRoot);
}
catch
{
return false;
}
}
/// <summary>
/// Validates that a path is a valid Garry's Mod installation by checking for garrysmod folder and steam_appid.txt.
/// </summary>
/// <param name="rootPath">The potential GMod root directory.</param>
/// <returns>True if valid, otherwise false.</returns>
private static bool IsValidGModPath(string rootPath)
{
try
{
var garrysmodPath = System.IO.Path.Combine(rootPath, "garrysmod");
Log.Debug("Checking if {Path} exists", garrysmodPath);
if (!System.IO.Directory.Exists(garrysmodPath))
return false;
var appIdPath = System.IO.Path.Combine(rootPath, "steam_appid.txt");
if (System.IO.File.Exists(appIdPath))
{
var appId = System.IO.File.ReadAllText(appIdPath).Trim();
Log.Debug("steam_appid.txt contains: {AppId}", appId);
if (appId == "4000")
{
Log.Information("Verified GMod via steam_appid.txt (4000)");
return true;
}
}
return System.IO.Directory.Exists(System.IO.Path.Combine(garrysmodPath, "gamemodes"));
}
catch
{
return false;
}
}
/// <summary>
/// Opens a folder picker dialog to manually select the Garry's Mod addons directory.
/// </summary>
/// <param name="window">The parent window for the dialog.</param>
/// <returns>The selected addons path, or null if cancelled/invalid.</returns>
public static async Task<string?> SelectWithDialog(Window window)
{
var topLevel = TopLevel.GetTopLevel(window);
if (topLevel == null) return null;
var files = await topLevel.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{
Title = "Select Garry's Mod addons folder",
AllowMultiple = false
});
if (files.Count > 0)
{
var path = files[0].Path.LocalPath;
var addonsPath = System.IO.Path.Combine(path, "garrysmod", "addons");
if (System.IO.Directory.Exists(addonsPath))
return addonsPath;
if (System.IO.Directory.Exists(path) && System.IO.Path.GetFileName(path) == "addons")
return path;
}
return null;
}
}
}