aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/System/OperatingSystem.cs
diff options
context:
space:
mode:
authorLogicalPhallacy <44458166+LogicalPhallacy@users.noreply.github.com>2019-03-16 00:25:16 -0700
committerGitHub <noreply@github.com>2019-03-16 00:25:16 -0700
commit2d0844b5dbf08869896c5479c10675c4fe7fa988 (patch)
treedea28b14d2b1733edf1a90a46f4163f6123616fe /MediaBrowser.Common/System/OperatingSystem.cs
parent221389089cc9ca4b69907d6bf3e9d38bf94393ea (diff)
parent59031ee3b8b2ac8298c41f9171b658e3f15e17bc (diff)
Merge pull request #1 from jellyfin/master
merging myself to latest
Diffstat (limited to 'MediaBrowser.Common/System/OperatingSystem.cs')
-rw-r--r--MediaBrowser.Common/System/OperatingSystem.cs72
1 files changed, 72 insertions, 0 deletions
diff --git a/MediaBrowser.Common/System/OperatingSystem.cs b/MediaBrowser.Common/System/OperatingSystem.cs
new file mode 100644
index 000000000..640821d4d
--- /dev/null
+++ b/MediaBrowser.Common/System/OperatingSystem.cs
@@ -0,0 +1,72 @@
+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";
+ default: throw new Exception($"Unknown OS {Id}");
+ }
+ }
+ }
+
+ 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;
+ if (osDescription.IndexOf("linux", StringComparison.OrdinalIgnoreCase) != -1)
+ {
+ return OperatingSystemId.Linux;
+ }
+ else if (osDescription.IndexOf("darwin", StringComparison.OrdinalIgnoreCase) != -1)
+ {
+ return OperatingSystemId.Darwin;
+ }
+ else if (osDescription.IndexOf("bsd", StringComparison.OrdinalIgnoreCase) != -1)
+ {
+ return OperatingSystemId.BSD;
+ }
+
+ throw new Exception($"Can't resolve OS with description: '{osDescription}'");
+ }
+ }
+ }
+ }
+}