diff options
| author | Mary <57835969+h1k421@users.noreply.github.com> | 2020-05-04 04:15:27 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-04 12:15:27 +1000 |
| commit | 651a07c6c2a3e89c059d56e916c45e1881d56abc (patch) | |
| tree | c53cd1f08b93e363fa838ad5e5643ba47f7bd734 /Ryujinx.Common/SystemInfo/MacOSSysteminfo.cs | |
| parent | 4c54f36c380683ef4575becf516953b7b0dfd6fc (diff) | |
Refactor SystemInfo and implement macOS system info backend (#1177)
Diffstat (limited to 'Ryujinx.Common/SystemInfo/MacOSSysteminfo.cs')
| -rw-r--r-- | Ryujinx.Common/SystemInfo/MacOSSysteminfo.cs | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/Ryujinx.Common/SystemInfo/MacOSSysteminfo.cs b/Ryujinx.Common/SystemInfo/MacOSSysteminfo.cs new file mode 100644 index 00000000..167c96db --- /dev/null +++ b/Ryujinx.Common/SystemInfo/MacOSSysteminfo.cs @@ -0,0 +1,97 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text; +using Ryujinx.Common.Logging; + +namespace Ryujinx.Common.SystemInfo +{ + internal class MacOSSysteminfo : SystemInfo + { + public override string CpuName { get; } + public override ulong RamSize { get; } + + [DllImport("libSystem.dylib", CharSet = CharSet.Ansi, SetLastError = true)] + private static extern int sysctlbyname(string name, IntPtr oldValue, ref ulong oldSize, IntPtr newValue, ulong newValueSize); + + private static int sysctlbyname(string name, IntPtr oldValue, ref ulong oldSize) + { + if (sysctlbyname(name, oldValue, ref oldSize, IntPtr.Zero, 0) == -1) + { + return Marshal.GetLastWin32Error(); + } + + return 0; + } + + private static int sysctlbyname<T>(string name, ref T oldValue) + { + unsafe + { + ulong oldValueSize = (ulong)Unsafe.SizeOf<T>(); + + return sysctlbyname(name, (IntPtr)Unsafe.AsPointer(ref oldValue), ref oldValueSize); + } + } + + private static int sysctlbyname(string name, out string oldValue) + { + oldValue = default; + + ulong strSize = 0; + + int res = sysctlbyname(name, IntPtr.Zero, ref strSize); + + if (res == 0) + { + byte[] rawData = new byte[strSize]; + + unsafe + { + fixed (byte* rawDataPtr = rawData) + { + res = sysctlbyname(name, (IntPtr)rawDataPtr, ref strSize); + } + + if (res == 0) + { + oldValue = Encoding.ASCII.GetString(rawData); + } + } + } + + return res; + } + + public MacOSSysteminfo() + { + ulong ramSize = 0; + + int res = sysctlbyname("hw.memsize", ref ramSize); + + if (res == 0) + { + RamSize = ramSize; + } + else + { + Logger.PrintError(LogClass.Application, $"Cannot get memory size, sysctlbyname error: {res}"); + + RamSize = 0; + } + + res = sysctlbyname("machdep.cpu.brand_string", out string cpuName); + + if (res == 0) + { + CpuName = cpuName; + } + else + { + Logger.PrintError(LogClass.Application, $"Cannot get CPU name, sysctlbyname error: {res}"); + + CpuName = "Unknown"; + } + } + } +}
\ No newline at end of file |
