2019-10-09 15:10:16 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-03-07 16:39:40 +00:00
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Threading;
|
|
|
|
using MediaBrowser.Model.System;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.System
|
|
|
|
{
|
|
|
|
public static class OperatingSystem
|
|
|
|
{
|
|
|
|
// We can't use Interlocked.CompareExchange for enums
|
|
|
|
private static int _id = int.MaxValue;
|
|
|
|
|
|
|
|
public static OperatingSystemId Id
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (_id == int.MaxValue)
|
|
|
|
{
|
|
|
|
Interlocked.CompareExchange(ref _id, (int)GetId(), int.MaxValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (OperatingSystemId)_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string Name
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
switch (Id)
|
|
|
|
{
|
|
|
|
case OperatingSystemId.BSD: return "BSD";
|
|
|
|
case OperatingSystemId.Linux: return "Linux";
|
|
|
|
case OperatingSystemId.Darwin: return "macOS";
|
|
|
|
case OperatingSystemId.Windows: return "Windows";
|
2020-04-01 13:30:53 +00:00
|
|
|
default: throw new PlatformNotSupportedException($"Unknown OS {Id}");
|
2019-03-07 16:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static OperatingSystemId GetId()
|
|
|
|
{
|
|
|
|
switch (Environment.OSVersion.Platform)
|
|
|
|
{
|
|
|
|
// On .NET Core `MacOSX` got replaced by `Unix`, this case should never be hit.
|
|
|
|
case PlatformID.MacOSX:
|
|
|
|
return OperatingSystemId.Darwin;
|
|
|
|
case PlatformID.Win32NT:
|
|
|
|
return OperatingSystemId.Windows;
|
|
|
|
case PlatformID.Unix:
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
string osDescription = RuntimeInformation.OSDescription;
|
2020-04-01 13:30:53 +00:00
|
|
|
if (osDescription.Contains("linux", StringComparison.OrdinalIgnoreCase))
|
2019-03-07 16:39:40 +00:00
|
|
|
{
|
|
|
|
return OperatingSystemId.Linux;
|
|
|
|
}
|
2020-04-01 13:30:53 +00:00
|
|
|
else if (osDescription.Contains("darwin", StringComparison.OrdinalIgnoreCase))
|
2019-03-07 16:39:40 +00:00
|
|
|
{
|
|
|
|
return OperatingSystemId.Darwin;
|
|
|
|
}
|
2020-04-01 13:30:53 +00:00
|
|
|
else if (osDescription.Contains("bsd", StringComparison.OrdinalIgnoreCase))
|
2019-03-07 16:39:40 +00:00
|
|
|
{
|
|
|
|
return OperatingSystemId.BSD;
|
|
|
|
}
|
|
|
|
|
2020-04-01 13:30:53 +00:00
|
|
|
throw new PlatformNotSupportedException($"Can't resolve OS with description: '{osDescription}'");
|
2019-03-07 16:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|