diff --git a/Directory.Packages.props b/Directory.Packages.props index 4b6bb1917..84dd68eba 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -21,6 +21,7 @@ + @@ -46,7 +47,6 @@ - diff --git a/src/Ryujinx.Common/Ryujinx.Common.csproj b/src/Ryujinx.Common/Ryujinx.Common.csproj index c02b11e0c..3e0c16a85 100644 --- a/src/Ryujinx.Common/Ryujinx.Common.csproj +++ b/src/Ryujinx.Common/Ryujinx.Common.csproj @@ -8,8 +8,8 @@ + - diff --git a/src/Ryujinx.Common/SystemInfo/SystemInfo.cs b/src/Ryujinx.Common/SystemInfo/SystemInfo.cs index 44977589b..45301de6a 100644 --- a/src/Ryujinx.Common/SystemInfo/SystemInfo.cs +++ b/src/Ryujinx.Common/SystemInfo/SystemInfo.cs @@ -1,4 +1,5 @@ -using Ryujinx.Common.Logging; +using Microsoft.Management.Infrastructure; +using Ryujinx.Common.Logging; using System; using System.Diagnostics; using System.IO; @@ -92,9 +93,11 @@ namespace Ryujinx.Common.SystemInfo { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - foreach (var item in new System.Management.ManagementObjectSearcher("Select NumberOfCores from Win32_Processor").Get()) + var session = CimSession.Create(null); + var instances = session.QueryInstances(@"root\cimv2", "WQL", "SELECT NumberOfCores FROM Win32_Processor"); + foreach (CimInstance instance in instances) { - coreCount = int.Parse(item["NumberOfCores"].ToString()); + coreCount = int.Parse(instance.CimInstanceProperties["NumberOfCores"].Value.ToString()); } } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) diff --git a/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs b/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs index fce9daffd..740722ebc 100644 --- a/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs +++ b/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs @@ -1,7 +1,8 @@ +using Microsoft.Management.Infrastructure; using Ryujinx.Common.Logging; using System; -using System.Management; -using System.Runtime.InteropServices; +using System.Collections.Generic; +using System.Linq; using System.Runtime.Versioning; namespace Ryujinx.Common.SystemInfo @@ -11,77 +12,67 @@ namespace Ryujinx.Common.SystemInfo { internal WindowsSystemInfo() { - CpuName = $"{GetCpuidCpuName() ?? GetCpuNameWMI()} ; {GetPhysicalCoreCount()} physical ; {LogicalCoreCount} logical"; // WMI is very slow - (RamTotal, RamAvailable) = GetMemoryStats(); + CpuName = $"{GetCpuidCpuName() ?? GetCpuNameMMI()} ; {GetPhysicalCoreCount()} physical ; {LogicalCoreCount} logical"; // WMI is very slow + (RamTotal, RamAvailable) = GetMemoryStatsMMI(); } - private static (ulong Total, ulong Available) GetMemoryStats() + private static string GetCpuNameMMI() { - MemoryStatusEx memStatus = new(); - if (GlobalMemoryStatusEx(ref memStatus)) - { - return (memStatus.TotalPhys, memStatus.AvailPhys); // Bytes - } - - Logger.Error?.Print(LogClass.Application, $"GlobalMemoryStatusEx failed. Error {Marshal.GetLastWin32Error():X}"); - - return (0, 0); - } - - private static string GetCpuNameWMI() - { - ManagementObjectCollection cpuObjs = GetWMIObjects("root\\CIMV2", "SELECT * FROM Win32_Processor"); + var cpuObjs = GetMMIObjects(@"root\cimv2", "SELECT * FROM Win32_Processor"); if (cpuObjs != null) { foreach (var cpuObj in cpuObjs) { - return cpuObj["Name"].ToString().Trim(); + return cpuObj.CimInstanceProperties["Name"].Value.ToString().Trim(); + } + } + + return Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER")?.Trim(); + } + + private static (ulong TotalPhys, ulong AvailPhys) GetMemoryStatsMMI() + { + var memObjs = GetMMIObjects(@"root\cimv2", "SELECT * FROM Win32_ComputerSystem"); + var memObjs2 = GetMMIObjects(@"root\cimv2", "SELECT * FROM Win32_OperatingSystem"); + + ulong TotalPhys = 0; + ulong AvailPhys = 0; + + if (memObjs != null) + { + foreach (var memObj in memObjs) + { + TotalPhys = (ulong)memObj.CimInstanceProperties["TotalPhysicalMemory"].Value; } } - return Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER").Trim(); - } - - [StructLayout(LayoutKind.Sequential)] - private struct MemoryStatusEx - { - public uint Length; - public uint MemoryLoad; - public ulong TotalPhys; - public ulong AvailPhys; - public ulong TotalPageFile; - public ulong AvailPageFile; - public ulong TotalVirtual; - public ulong AvailVirtual; - public ulong AvailExtendedVirtual; - - public MemoryStatusEx() + if (memObjs2 != null) { - Length = (uint)Marshal.SizeOf(); + foreach (var memObj2 in memObjs2) + { + AvailPhys = (ulong)memObj2.CimInstanceProperties["FreePhysicalMemory"].Value*1000; + } } + + return (TotalPhys, AvailPhys); } - [LibraryImport("kernel32.dll", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - private static partial bool GlobalMemoryStatusEx(ref MemoryStatusEx lpBuffer); - - private static ManagementObjectCollection GetWMIObjects(string scope, string query) + private static IEnumerable GetMMIObjects(string namespaceName, string query) { try { - return new ManagementObjectSearcher(scope, query).Get(); + using (CimSession session = CimSession.Create(null)) + { + return session.QueryInstances(namespaceName, "WQL", query).ToList(); + } } - catch (PlatformNotSupportedException ex) + catch (CimException ex) { - Logger.Error?.Print(LogClass.Application, $"WMI isn't available : {ex.Message}"); - } - catch (COMException ex) - { - Logger.Error?.Print(LogClass.Application, $"WMI isn't available : {ex.Message}"); + Logger.Error?.Print(LogClass.Application, $"MMI isn't available : {ex.Message}"); } - return null; + return Enumerable.Empty(); } } }