Added small system overview

This commit is contained in:
2025-12-25 21:55:46 +01:00
parent a2d4edc0e5
commit ca69d410f2
9 changed files with 422 additions and 0 deletions

View 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}";
}
}