Move OS detection code out of CmdServerInfo.cs and into Server.cs

This commit is contained in:
UnknownShadow200 2022-08-07 10:32:08 +10:00
parent 5a5eb4492b
commit a75ea460b2
2 changed files with 33 additions and 16 deletions

View File

@ -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);

View File

@ -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);
}
}