Skip to content

Commit 0a9d939

Browse files
committed
fix: Allow for malfunctioning WMI in system data
1 parent 64a29c8 commit 0a9d939

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

Source/ORTS.Common/SystemInfo.cs

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using System.Text.RegularExpressions;
2424
using System.Collections.Generic;
2525
using System.Globalization;
26+
using System.Diagnostics;
2627

2728
namespace ORTS.Common
2829
{
@@ -50,34 +51,55 @@ static SystemInfo()
5051
Version = runtime.Groups[2].Value,
5152
};
5253

53-
// Almost nothing will correctly identify Windows 11 at this point, so we have to use WMI.
54-
var operatingSystem = new ManagementClass("Win32_OperatingSystem").GetInstances().Cast<ManagementObject>().First();
55-
OperatingSystem = new Platform
54+
try
5655
{
57-
Name = (string)operatingSystem["Caption"],
58-
Version = (string)operatingSystem["Version"],
59-
Architecture = RuntimeInformation.OSArchitecture.ToString(),
60-
Language = CultureInfo.CurrentUICulture.IetfLanguageTag,
61-
Languages = (string[])operatingSystem["MUILanguages"],
62-
};
56+
// Almost nothing will correctly identify Windows 11 at this point, so we have to use WMI.
57+
var operatingSystem = new ManagementClass("Win32_OperatingSystem").GetInstances().Cast<ManagementObject>().First();
58+
OperatingSystem = new Platform
59+
{
60+
Name = (string)operatingSystem["Caption"],
61+
Version = (string)operatingSystem["Version"],
62+
Architecture = RuntimeInformation.OSArchitecture.ToString(),
63+
Language = CultureInfo.CurrentUICulture.IetfLanguageTag,
64+
Languages = (string[])operatingSystem["MUILanguages"],
65+
};
66+
}
67+
catch (ManagementException error)
68+
{
69+
Trace.WriteLine(error);
70+
}
6371

6472
NativeMethods.GlobalMemoryStatusEx(MemoryStatusExtended);
6573
InstalledMemoryMB = (int)(MemoryStatusExtended.TotalPhysical / 1024 / 1024);
6674

67-
CPUs = new ManagementClass("Win32_Processor").GetInstances().Cast<ManagementObject>().Select(processor => new CPU
75+
try
6876
{
69-
Name = (string)processor["Name"],
70-
Manufacturer = (string)processor["Manufacturer"],
71-
ThreadCount = (uint)processor["ThreadCount"],
72-
MaxClockMHz = (uint)processor["MaxClockSpeed"],
73-
}).ToList();
77+
CPUs = new ManagementClass("Win32_Processor").GetInstances().Cast<ManagementObject>().Select(processor => new CPU
78+
{
79+
Name = (string)processor["Name"],
80+
Manufacturer = (string)processor["Manufacturer"],
81+
ThreadCount = (uint)processor["ThreadCount"],
82+
MaxClockMHz = (uint)processor["MaxClockSpeed"],
83+
}).ToList();
84+
}
85+
catch (ManagementException error)
86+
{
87+
Trace.WriteLine(error);
88+
}
7489

75-
GPUs = new ManagementClass("Win32_VideoController").GetInstances().Cast<ManagementObject>().Select(adapter => new GPU
90+
try
91+
{
92+
GPUs = new ManagementClass("Win32_VideoController").GetInstances().Cast<ManagementObject>().Select(adapter => new GPU
93+
{
94+
Name = (string)adapter["Name"],
95+
Manufacturer = (string)adapter["AdapterCompatibility"],
96+
MemoryMB = (uint?)adapter["AdapterRAM"] / 1024 / 1024 ?? 0,
97+
}).ToList();
98+
}
99+
catch (ManagementException error)
76100
{
77-
Name = (string)adapter["Name"],
78-
Manufacturer = (string)adapter["AdapterCompatibility"],
79-
MemoryMB = (uint)adapter["AdapterRAM"] / 1024 / 1024,
80-
}).ToList();
101+
Trace.WriteLine(error);
102+
}
81103

82104
var featureLevels = new uint[] {
83105
NativeMethods.D3D_FEATURE_LEVEL_12_2,
@@ -108,8 +130,8 @@ static SystemInfo()
108130
public static readonly Platform Runtime;
109131
public static readonly Platform OperatingSystem;
110132
public static readonly int InstalledMemoryMB;
111-
public static readonly List<CPU> CPUs;
112-
public static readonly List<GPU> GPUs;
133+
public static readonly List<CPU> CPUs = new List<CPU>();
134+
public static readonly List<GPU> GPUs = new List<GPU>();
113135
public static readonly List<string> Direct3DFeatureLevels = new List<string>();
114136

115137
public static void WriteSystemDetails(TextWriter output)
@@ -118,7 +140,7 @@ public static void WriteSystemDetails(TextWriter output)
118140
DateTime.Now, DateTime.UtcNow);
119141
output.WriteLine("Application = {0} {1} ({2})", Application.Name, Application.Version, Application.Architecture);
120142
output.WriteLine("Runtime = {0} {1}", Runtime.Name, Runtime.Version);
121-
output.WriteLine("System = {0} {1} ({2}; {3}; {4})", OperatingSystem.Name, OperatingSystem.Version, OperatingSystem.Architecture, OperatingSystem.Language, string.Join(",", OperatingSystem.Languages));
143+
output.WriteLine("System = {0} {1} ({2}; {3}; {4})", OperatingSystem.Name, OperatingSystem.Version, OperatingSystem.Architecture, OperatingSystem.Language, string.Join(",", OperatingSystem.Languages ?? new string[0]));
122144
output.WriteLine("Memory = {0:N0} MB", InstalledMemoryMB);
123145
foreach (var cpu in CPUs) output.WriteLine("CPU = {0} ({1}; {2} threads; {3:N0} MHz)", cpu.Name, cpu.Manufacturer, cpu.ThreadCount, cpu.MaxClockMHz);
124146
foreach (var gpu in GPUs) output.WriteLine("GPU = {0} ({1}; {2:N0} MB)", gpu.Name, gpu.Manufacturer, gpu.MemoryMB);

0 commit comments

Comments
 (0)