MacOSSystemInfo.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. using System.Runtime.Versioning;
  5. using System.Text;
  6. using Ryujinx.Common.Logging;
  7. namespace Ryujinx.Common.SystemInfo
  8. {
  9. [SupportedOSPlatform("macos")]
  10. internal class MacOSSystemInfo : SystemInfo
  11. {
  12. public override string CpuName { get; }
  13. public override ulong RamSize { get; }
  14. [DllImport("libSystem.dylib", CharSet = CharSet.Ansi, SetLastError = true)]
  15. private static extern int sysctlbyname(string name, IntPtr oldValue, ref ulong oldSize, IntPtr newValue, ulong newValueSize);
  16. private static int sysctlbyname(string name, IntPtr oldValue, ref ulong oldSize)
  17. {
  18. if (sysctlbyname(name, oldValue, ref oldSize, IntPtr.Zero, 0) == -1)
  19. {
  20. return Marshal.GetLastWin32Error();
  21. }
  22. return 0;
  23. }
  24. private static int sysctlbyname<T>(string name, ref T oldValue)
  25. {
  26. unsafe
  27. {
  28. ulong oldValueSize = (ulong)Unsafe.SizeOf<T>();
  29. return sysctlbyname(name, (IntPtr)Unsafe.AsPointer(ref oldValue), ref oldValueSize);
  30. }
  31. }
  32. private static int sysctlbyname(string name, out string oldValue)
  33. {
  34. oldValue = default;
  35. ulong strSize = 0;
  36. int res = sysctlbyname(name, IntPtr.Zero, ref strSize);
  37. if (res == 0)
  38. {
  39. byte[] rawData = new byte[strSize];
  40. unsafe
  41. {
  42. fixed (byte* rawDataPtr = rawData)
  43. {
  44. res = sysctlbyname(name, (IntPtr)rawDataPtr, ref strSize);
  45. }
  46. if (res == 0)
  47. {
  48. oldValue = Encoding.ASCII.GetString(rawData);
  49. }
  50. }
  51. }
  52. return res;
  53. }
  54. public MacOSSystemInfo()
  55. {
  56. ulong ramSize = 0;
  57. int res = sysctlbyname("hw.memsize", ref ramSize);
  58. if (res == 0)
  59. {
  60. RamSize = ramSize;
  61. }
  62. else
  63. {
  64. Logger.Error?.Print(LogClass.Application, $"Cannot get memory size, sysctlbyname error: {res}");
  65. RamSize = 0;
  66. }
  67. res = sysctlbyname("machdep.cpu.brand_string", out string cpuName);
  68. if (res == 0)
  69. {
  70. CpuName = cpuName;
  71. }
  72. else
  73. {
  74. Logger.Error?.Print(LogClass.Application, $"Cannot get CPU name, sysctlbyname error: {res}");
  75. CpuName = "Unknown";
  76. }
  77. }
  78. }
  79. }