From a75ea460b258d2918bc8b85a8f7b701cfe28705c Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 7 Aug 2022 10:32:08 +1000 Subject: [PATCH] Move OS detection code out of CmdServerInfo.cs and into Server.cs --- .../Commands/Information/CmdServerInfo.cs | 22 ++++++--------- MCGalaxy/Server/Server.cs | 27 +++++++++++++++++-- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/MCGalaxy/Commands/Information/CmdServerInfo.cs b/MCGalaxy/Commands/Information/CmdServerInfo.cs index a741f7eec..d8148e975 100644 --- a/MCGalaxy/Commands/Information/CmdServerInfo.cs +++ b/MCGalaxy/Commands/Information/CmdServerInfo.cs @@ -110,22 +110,16 @@ namespace MCGalaxy.Commands.Info public ulong IdleTime, KernelTime, UserTime; } - unsafe static CPUTime MeasureAllCPUTime() - { - if (Environment.OSVersion.Platform == PlatformID.Win32NT) - return MeasureAllWindows(); - - sbyte* ascii = stackalloc sbyte[8192]; - uname(ascii); - string kernel = new String(ascii); - if (kernel == "Darwin") return MeasureAllMac(); - - return MeasureAllLinux(); + unsafe static CPUTime MeasureAllCPUTime() { + switch (Server.DetectOS()) + { + case DetectedOS.Windows: return MeasureAllWindows(); + case DetectedOS.OSX: return MeasureAllMac(); + case DetectedOS.Linux: return MeasureAllLinux(); + } + return default(CPUTime); } - [DllImport("libc")] - unsafe static extern void uname(sbyte* uname_struct); - static CPUTime MeasureAllWindows() { CPUTime all = default(CPUTime); diff --git a/MCGalaxy/Server/Server.cs b/MCGalaxy/Server/Server.cs index 9d1380449..8953cc732 100644 --- a/MCGalaxy/Server/Server.cs +++ b/MCGalaxy/Server/Server.cs @@ -37,8 +37,12 @@ using MCGalaxy.Tasks; using MCGalaxy.Util; using MCGalaxy.Modules.Awards; -namespace MCGalaxy { - public sealed partial class Server { +namespace MCGalaxy +{ + public enum DetectedOS { Unknown, Windows, OSX, Linux } + + public sealed partial class Server + { public Server() { Server.s = this; } @@ -456,5 +460,24 @@ namespace MCGalaxy { if (!name.EndsWith("+")) name += "+"; return name; } + + + public unsafe static DetectedOS DetectOS() { + PlatformID platform = Environment.OSVersion.Platform; + if (platform == PlatformID.Win32NT || platform == PlatformID.Win32Windows) + return DetectedOS.Windows; + + sbyte* ascii = stackalloc sbyte[8192]; + uname(ascii); + string kernel = new String(ascii); + + if (kernel == "Darwin") return DetectedOS.OSX; + if (kernel == "Linux") return DetectedOS.Linux; + + return DetectedOS.Unknown; + } + + [DllImport("libc")] + unsafe static extern void uname(sbyte* uname_struct); } } \ No newline at end of file