-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatformManager.cs
More file actions
297 lines (253 loc) · 9.79 KB
/
PlatformManager.cs
File metadata and controls
297 lines (253 loc) · 9.79 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
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace JustLauncher;
public static class PlatformManager
{
public static string GetCurrentOsName()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return "windows";
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) return "osx";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) return "linux";
return "windows";
}
public static bool IsWindows()
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
}
public static bool IsLinux()
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
}
public static bool IsMac()
{
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
}
public static bool IsArchitectureMatch(string classifier, string currentOs)
{
var arch = RuntimeInformation.OSArchitecture;
bool is64 = arch == Architecture.X64;
bool isX86 = arch == Architecture.X86;
bool isArm64 = arch == Architecture.Arm64;
string baseNatives = $"natives-{currentOs}";
if (classifier == baseNatives)
{
if (currentOs == "osx") return true;
return is64;
}
if (classifier.EndsWith("-x86") || classifier.EndsWith("-i386")) return isX86;
if (classifier.EndsWith("-x64") || classifier.EndsWith("-x86_64") || classifier.EndsWith("-amd64")) return is64;
if (classifier.EndsWith("-arm64") || classifier.EndsWith("-aarch64")) return isArm64;
if (currentOs == "osx" && (classifier.Contains("arm64") || classifier.Contains("aarch64"))) return isArm64;
return true;
}
public static string GetMinecraftDirectory()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ".minecraft");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".minecraft");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library", "Application Support", "minecraft");
}
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".minecraft");
}
public static string GetLauncherDirectory()
{
string minecraftDir = GetMinecraftDirectory();
return Path.Combine(minecraftDir, ".justlauncher");
}
public static string GetJavaExecutableName()
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "java.exe" : "java";
}
public static string[] GetCommonJavaSearchPaths()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return new[]
{
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Java"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Java"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Eclipse Adoptium")
};
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return new[]
{
"/usr/lib/jvm",
"/usr/java",
"/usr/lib/jvm/default",
"/usr/bin"
};
}
return Array.Empty<string>();
}
public static void OpenBrowser(string url)
{
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
var psi = new ProcessStartInfo("xdg-open");
psi.ArgumentList.Add(url);
Process.Start(psi);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
var psi = new ProcessStartInfo("open");
psi.ArgumentList.Add(url);
Process.Start(psi);
}
}
catch { }
}
public static void OpenFolder(string folderPath)
{
try
{
if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Process.Start("explorer.exe", folderPath);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
var psi = new ProcessStartInfo("xdg-open");
psi.ArgumentList.Add(folderPath);
Process.Start(psi);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
var psi = new ProcessStartInfo("open");
psi.ArgumentList.Add(folderPath);
Process.Start(psi);
}
}
catch { }
}
public static string GetJavaExecutable()
{
return GetJavaExecutableName();
}
public static async System.Threading.Tasks.Task<(string? version, string? path)> FindJavaInstallationAsync(int requiredMajorVersion)
{
var candidates = new System.Collections.Generic.List<(string version, string path, int major)>();
async Task CheckCandidate(string javaPath)
{
if (!File.Exists(javaPath)) return;
if (candidates.Any(c => c.path == javaPath)) return;
var version = await GetJavaVersionFromPathAsync(javaPath);
if (version != null)
{
int major = ExtractMajorVersion(version);
candidates.Add((version, javaPath, major));
}
}
var pathVersion = await GetJavaVersionFromPathAsync(GetJavaExecutableName());
if (pathVersion != null)
{
candidates.Add((pathVersion, GetJavaExecutableName(), ExtractMajorVersion(pathVersion)));
}
var searchPaths = new System.Collections.Generic.List<string>(GetCommonJavaSearchPaths());
string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
searchPaths.Add(Path.Combine(home, ".sdkman", "candidates", "java"));
searchPaths.Add(Path.Combine(home, ".jdks"));
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
searchPaths.Add(Path.Combine(home, ".jdks"));
}
foreach (var basePath in searchPaths)
{
if (!Directory.Exists(basePath)) continue;
try
{
var dirs = Directory.GetDirectories(basePath);
foreach (var dir in dirs)
{
await CheckCandidate(Path.Combine(dir, "bin", GetJavaExecutableName()));
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
await CheckCandidate(Path.Combine(dir, "Contents", "Home", "bin", "java"));
}
}
}
catch { /* Ignore access errors */ }
}
var compatible = candidates.Where(c => c.major >= requiredMajorVersion).ToList();
if (compatible.Any())
{
var best = compatible.OrderByDescending(c => c.major).First();
return (best.version, best.path);
}
if (candidates.Any())
{
var bestStart = candidates.OrderByDescending(c => c.major).First();
return (bestStart.version, bestStart.path);
}
return (null, null);
}
public static int ExtractMajorVersion(string versionString)
{
var parts = versionString.Split('.');
if (parts.Length > 0 && int.TryParse(parts[0], out int major))
{
if (major == 1 && parts.Length > 1 && int.TryParse(parts[1], out int second))
{
return second;
}
return major;
}
return 0;
}
private static async System.Threading.Tasks.Task<string?> GetJavaVersionFromPathAsync(string javaPath)
{
try
{
var startInfo = new ProcessStartInfo
{
FileName = javaPath,
Arguments = "-version",
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using var process = Process.Start(startInfo);
if (process == null) return null;
string output = await process.StandardError.ReadToEndAsync();
await process.WaitForExitAsync();
var match = System.Text.RegularExpressions.Regex.Match(output, @"version ""([^""]+)""");
if (match.Success) return match.Groups[1].Value;
match = System.Text.RegularExpressions.Regex.Match(output, @"openjdk (\d+\.\d+\.\d+)");
if (match.Success) return match.Groups[1].Value;
return null;
}
catch (Exception ex)
{
Debug.WriteLine($"Java check error: {ex.Message}");
return null;
}
}
public static async System.Threading.Tasks.Task<string?> GetJavaVersionAsync()
{
var result = await GetJavaVersionFromPathAsync(GetJavaExecutableName());
return result;
}
}