From 56187602990537939dd8afdb4ce7df8707863377 Mon Sep 17 00:00:00 2001 From: sunshineinabox Date: Sat, 22 Jul 2023 12:09:23 -0700 Subject: [PATCH] Switch to WmiLight & enable BuiltInComInteropSupport --- Directory.Packages.props | 1 + src/Ryujinx.Ava/Ryujinx.Ava.csproj | 1 + src/Ryujinx.Common/Ryujinx.Common.csproj | 2 +- src/Ryujinx.Common/SystemInfo/SystemInfo.cs | 14 +-- .../SystemInfo/WindowsSystemInfo.cs | 88 +++++++++---------- .../Ryujinx.Headless.SDL2.csproj | 1 + src/Ryujinx/Ryujinx.csproj | 1 + 7 files changed, 51 insertions(+), 57 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 84dd68eba..1fd347d39 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -48,6 +48,7 @@ + \ No newline at end of file diff --git a/src/Ryujinx.Ava/Ryujinx.Ava.csproj b/src/Ryujinx.Ava/Ryujinx.Ava.csproj index 1fac5400e..3996621b2 100644 --- a/src/Ryujinx.Ava/Ryujinx.Ava.csproj +++ b/src/Ryujinx.Ava/Ryujinx.Ava.csproj @@ -20,6 +20,7 @@ true false true + true partial diff --git a/src/Ryujinx.Common/Ryujinx.Common.csproj b/src/Ryujinx.Common/Ryujinx.Common.csproj index 3e0c16a85..434e1d153 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 e51747b28..760eaf21f 100644 --- a/src/Ryujinx.Common/SystemInfo/SystemInfo.cs +++ b/src/Ryujinx.Common/SystemInfo/SystemInfo.cs @@ -1,5 +1,4 @@ -using Microsoft.Management.Infrastructure; -using Ryujinx.Common.Logging; +using Ryujinx.Common.Logging; using System; using System.Diagnostics; using System.IO; @@ -7,6 +6,7 @@ using System.Linq; using System.Runtime.InteropServices; using System.Runtime.Intrinsics.X86; using System.Text; +using WmiLight; namespace Ryujinx.Common.SystemInfo { @@ -14,6 +14,7 @@ namespace Ryujinx.Common.SystemInfo { public string OsDescription { get; protected set; } public string CpuName { get; protected set; } + public static int PhysicalCores { get; protected set; } public ulong RamTotal { get; protected set; } public ulong RamAvailable { get; protected set; } protected static int LogicalCoreCount => Environment.ProcessorCount; @@ -93,14 +94,7 @@ namespace Ryujinx.Common.SystemInfo { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - using (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(instance.CimInstanceProperties["NumberOfCores"].Value.ToString()); - } - } + coreCount = PhysicalCores; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { diff --git a/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs b/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs index 463c04c85..4bb1d0936 100644 --- a/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs +++ b/src/Ryujinx.Common/SystemInfo/WindowsSystemInfo.cs @@ -1,86 +1,82 @@ -using Microsoft.Management.Infrastructure; -using Ryujinx.Common.Logging; using System; +using System.Runtime.Versioning; +using WmiLight; +using Ryujinx.Common.Logging; using System.Collections.Generic; using System.Linq; -using System.Runtime.Versioning; namespace Ryujinx.Common.SystemInfo { [SupportedOSPlatform("windows")] partial class WindowsSystemInfo : SystemInfo { - private CimSession _cimSession; - internal WindowsSystemInfo() { try { - //Inefficient to create a session for each query do it all at once - _cimSession = CimSession.Create(null); - CpuName = $"{GetCpuNameMMI() ?? GetCpuidCpuName()} ; {GetPhysicalCoreCount()} physical ; {LogicalCoreCount} logical"; - (RamTotal, RamAvailable) = GetMemoryStatsMMI(); - } - finally - { - _cimSession?.Dispose(); - } - } - - private string GetCpuNameMMI() - { - var cpuObjs = GetMMIObjects(@"root\cimv2", "SELECT Name FROM Win32_Processor"); - - if (cpuObjs != null) - { - foreach (var cpuObj in cpuObjs) + using (WmiConnection connection = new WmiConnection()) { - return cpuObj.CimInstanceProperties["Name"].Value.ToString().Trim(); + (string cpuName, int physicalCores) = GetCpuStatsLight(connection); + CpuName = $"{cpuName} ; {physicalCores} physical ; {LogicalCoreCount} logical"; + (RamTotal, RamAvailable) = GetMemoryStatsWmiLight(connection); } } - - return Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER")?.Trim(); + catch (Exception ex) + { + Logger.Error?.Print(LogClass.Application, $"WmiLight isn't available : {ex.Message}"); + } } - - private (ulong TotalPhys, ulong AvailPhys) GetMemoryStatsMMI() - { - var memObjs = GetMMIObjects(@"root\cimv2", "SELECT TotalPhysicalMemory FROM Win32_ComputerSystem"); - var memObjs2 = GetMMIObjects(@"root\cimv2", "SELECT FreePhysicalMemory FROM Win32_OperatingSystem"); + + private (string cpuName, int physicalCores) GetCpuStatsLight(WmiConnection connection) + { + string cpuName = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER")?.Trim(); + int physicalCores = LogicalCoreCount; + foreach (WmiObject cpuObj in GetWmiObjects(connection, "SELECT Name FROM Win32_Processor")) + { + cpuName = cpuObj["Name"].ToString().Trim(); + } + + foreach (WmiObject cpuObj in GetWmiObjects(connection, "SELECT NumberOfCores FROM Win32_Processor")) + { + physicalCores = Convert.ToInt32(cpuObj["NumberOfCores"]); + } + + return (cpuName, physicalCores); + } + + private (ulong TotalPhys, ulong AvailPhys) GetMemoryStatsWmiLight(WmiConnection connection) + { ulong TotalPhys = 0; ulong AvailPhys = 0; - if (memObjs != null) + foreach (WmiObject memObj in GetWmiObjects(connection, "SELECT TotalPhysicalMemory FROM Win32_ComputerSystem")) { - foreach (var memObj in memObjs) - { - TotalPhys = (ulong)memObj.CimInstanceProperties["TotalPhysicalMemory"].Value; - } + TotalPhys = Convert.ToUInt64(memObj["TotalPhysicalMemory"]); } - if (memObjs2 != null) + foreach (WmiObject memObj2 in GetWmiObjects(connection, "SELECT FreePhysicalMemory FROM Win32_OperatingSystem")) { - foreach (var memObj2 in memObjs2) - { - AvailPhys = (ulong)memObj2.CimInstanceProperties["FreePhysicalMemory"].Value*1000; - } + AvailPhys = Convert.ToUInt64(memObj2["FreePhysicalMemory"]) * 1000; } return (TotalPhys, AvailPhys); } - private IEnumerable GetMMIObjects(string namespaceName, string query) + + private IEnumerable GetWmiObjects(WmiConnection connection, string query) { try { - return _cimSession.QueryInstances(namespaceName, "WQL", query).ToList(); + return connection.CreateQuery(query).ToList(); } - catch (CimException ex) + catch (Exception ex) { - Logger.Error?.Print(LogClass.Application, $"MMI isn't available : {ex.Message}"); + Logger.Error?.Print(LogClass.Application, $"WmiLight isn't available : {ex.Message}"); } - return Enumerable.Empty(); + return Enumerable.Empty(); } + } } diff --git a/src/Ryujinx.Headless.SDL2/Ryujinx.Headless.SDL2.csproj b/src/Ryujinx.Headless.SDL2/Ryujinx.Headless.SDL2.csproj index d2585c563..6d4b88a47 100644 --- a/src/Ryujinx.Headless.SDL2/Ryujinx.Headless.SDL2.csproj +++ b/src/Ryujinx.Headless.SDL2/Ryujinx.Headless.SDL2.csproj @@ -67,6 +67,7 @@ true true + true partial \ No newline at end of file diff --git a/src/Ryujinx/Ryujinx.csproj b/src/Ryujinx/Ryujinx.csproj index cf4435e57..8fa3e13cf 100644 --- a/src/Ryujinx/Ryujinx.csproj +++ b/src/Ryujinx/Ryujinx.csproj @@ -16,6 +16,7 @@ true false true + true partial