-
Notifications
You must be signed in to change notification settings - Fork 5
/
OS.cs
89 lines (75 loc) · 2.72 KB
/
OS.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
using System.Runtime.InteropServices;
namespace AtomicLib;
/// <summary>
/// Class that has information about the current running operating system.
/// </summary>
public static class OS
{
/// <summary>
/// Determines if the current OS is Windows.
/// </summary>
public static readonly bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
/// <summary>
/// Determines if the current OS is Linux.
/// </summary>
public static readonly bool IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
/// <summary>
/// Determines if the current OS is Mac.
/// </summary>
public static readonly bool IsMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
/// <summary>
/// Determines if the current OS is a unix based system (Mac or Linux).
/// </summary>
public static readonly bool IsUnix = IsLinux || IsMac;
/// <summary>
/// Gets a string representation of the current OS.
/// </summary>
public static readonly string Name = DetermineOsName();
/// <summary>
/// Generates a string representation of the current OS
/// </summary>
private static string DetermineOsName()
{
if (IsWindows)
return "Windows";
if (IsLinux)
return "Linux";
if (IsMac)
return "Mac";
return "Unknown OS";
}
/// <summary>
/// Checks if this is run via WINE.
/// </summary>
public static readonly bool IsThisRunningFromWINE = CheckIfRunFromWINE();
/// <summary>
/// Checks if this is run via Flatpak.
/// </summary>
public static readonly bool IsThisRunningFromFlatpak = CheckIfRunFromFlatpak();
/// <summary>
/// Checks if the Launcher is ran from WINE.
/// </summary>
/// <returns><see langword="true"/> if run from WINE, <see langword="false"/> if not.</returns>
private static bool CheckIfRunFromWINE()
{
// We check for wine by seeing if a reg entry exists.
// Not the best way, and could be removed from the future, but good enough for our purposes.
#pragma warning disable CA1416
if (IsWindows && (Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Wine") != null))
return true;
#pragma warning restore CA1416
return false;
}
/// <summary>
/// Checks if the Launcher is ran from a Flatpak.
/// </summary>
/// <returns><see langword="true"/> if run from a Flatpak, <see langword="false"/> if not.</returns>
private static bool CheckIfRunFromFlatpak()
{
if (!IsLinux) return false;
// This file is present in all flatpaks
if (File.Exists("/.flatpak-info"))
return true;
return false;
}
}