Added small system overview
This commit is contained in:
98
Moonlight.Api/Helpers/OsHelper.cs
Normal file
98
Moonlight.Api/Helpers/OsHelper.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Moonlight.Api.Helpers;
|
||||
|
||||
public class OsHelper
|
||||
{
|
||||
public static string GetName()
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
return GetWindowsVersion();
|
||||
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
return GetLinuxVersion();
|
||||
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
return "Goofy OS";
|
||||
|
||||
return "Unknown OS";
|
||||
}
|
||||
|
||||
private static string GetWindowsVersion()
|
||||
{
|
||||
var version = Environment.OSVersion.Version;
|
||||
|
||||
// Windows 11 is version 10.0 build 22000+
|
||||
if (version.Major == 10 && version.Build >= 22000)
|
||||
return $"Windows 11 ({version.Build})";
|
||||
|
||||
if (version.Major == 10)
|
||||
return $"Windows 10 ({version.Build})";
|
||||
|
||||
if (version.Major == 6 && version.Minor == 3)
|
||||
return "Windows 8.1";
|
||||
|
||||
if (version.Major == 6 && version.Minor == 2)
|
||||
return "Windows 8";
|
||||
|
||||
if (version.Major == 6 && version.Minor == 1)
|
||||
return "Windows 7";
|
||||
|
||||
return $"Windows {version.Major}.{version.Minor}";
|
||||
}
|
||||
|
||||
private static string GetLinuxVersion()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Read /etc/os-release, should work everywhere
|
||||
if (File.Exists("/etc/os-release"))
|
||||
{
|
||||
var lines = File.ReadAllLines("/etc/os-release");
|
||||
string? name = null;
|
||||
string? version = null;
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
if (line.StartsWith("NAME="))
|
||||
name = line.Substring(5).Trim('"');
|
||||
else if (line.StartsWith("VERSION_ID="))
|
||||
version = line.Substring(11).Trim('"');
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(name))
|
||||
{
|
||||
return string.IsNullOrEmpty(version) ? name : $"{name} {version}";
|
||||
}
|
||||
}
|
||||
|
||||
//If for some weird reason it still uses lsb release
|
||||
if (File.Exists("/etc/lsb-release"))
|
||||
{
|
||||
var lines = File.ReadAllLines("/etc/lsb-release");
|
||||
string? name = null;
|
||||
string? version = null;
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
if (line.StartsWith("DISTRIB_ID="))
|
||||
name = line.Substring(11);
|
||||
else if (line.StartsWith("DISTRIB_RELEASE="))
|
||||
version = line.Substring(16);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(name))
|
||||
{
|
||||
return string.IsNullOrEmpty(version) ? name : $"{name} {version}";
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore
|
||||
}
|
||||
|
||||
// Fallback
|
||||
return $"Linux {Environment.OSVersion.Version}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user