Skip to content

Commit

Permalink
Move OS detection code out of CmdServerInfo.cs and into Server.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Aug 7, 2022
1 parent 5a5eb44 commit a75ea46
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
22 changes: 8 additions & 14 deletions MCGalaxy/Commands/Information/CmdServerInfo.cs
Expand Up @@ -110,22 +110,16 @@ struct CPUTime
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);
Expand Down
27 changes: 25 additions & 2 deletions MCGalaxy/Server/Server.cs
Expand Up @@ -37,8 +37,12 @@
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; }

Expand Down Expand Up @@ -456,5 +460,24 @@ public sealed partial class Server {
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);
}
}

0 comments on commit a75ea46

Please sign in to comment.