Switch to WmiLight & enable BuiltInComInteropSupport

This commit is contained in:
sunshineinabox 2023-07-22 12:09:23 -07:00
parent 9345d284d9
commit 5618760299
7 changed files with 51 additions and 57 deletions

View file

@ -48,6 +48,7 @@
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="6.31.0" />
<PackageVersion Include="System.IO.Hashing" Version="7.0.0" />
<PackageVersion Include="UnicornEngine.Unicorn" Version="2.0.2-rc1-fb78016" />
<PackageVersion Include="WmiLight" Version="4.0.0" />
<PackageVersion Include="XamlNameReferenceGenerator" Version="1.6.1" />
</ItemGroup>
</Project>

View file

@ -20,6 +20,7 @@
<PublishSingleFile>true</PublishSingleFile>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
<PublishTrimmed>true</PublishTrimmed>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<TrimMode>partial</TrimMode>
</PropertyGroup>

View file

@ -8,8 +8,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" />
<PackageReference Include="Microsoft.Management.Infrastructure" />
<PackageReference Include="MsgPack.Cli" />
<PackageReference Include="WmiLight" />
</ItemGroup>
</Project>

View file

@ -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))
{

View file

@ -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<CimInstance> GetMMIObjects(string namespaceName, string query)
private IEnumerable<WmiObject> 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<CimInstance>();
return Enumerable.Empty<WmiObject>();
}
}
}

View file

@ -67,6 +67,7 @@
<PropertyGroup Condition="'$(RuntimeIdentifier)' != ''">
<PublishSingleFile>true</PublishSingleFile>
<PublishTrimmed>true</PublishTrimmed>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<TrimMode>partial</TrimMode>
</PropertyGroup>
</Project>

View file

@ -16,6 +16,7 @@
<PublishSingleFile>true</PublishSingleFile>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
<PublishTrimmed>true</PublishTrimmed>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<TrimMode>partial</TrimMode>
</PropertyGroup>