/
SystemInfo.cs
230 lines (210 loc) · 9.66 KB
/
SystemInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// COPYRIGHT 2009 - 2023 by the Open Rails project.
//
// This file is part of Open Rails.
//
// Open Rails is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Open Rails is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.IO;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Globalization;
using System.Diagnostics;
using SharpDX.DXGI;
namespace ORTS.Common
{
public static class SystemInfo
{
static readonly Regex NameAndVersionRegex = new Regex("^(.*?) +([0-9.]+)$");
static readonly NativeStructs.MemoryStatusExtended MemoryStatusExtended = new NativeStructs.MemoryStatusExtended()
{
Size = 64
};
static SystemInfo()
{
Application = new Platform
{
Name = System.Windows.Forms.Application.ProductName,
Version = VersionInfo.VersionOrBuild,
Architecture = RuntimeInformation.ProcessArchitecture.ToString(),
};
var runtime = NameAndVersionRegex.Match(RuntimeInformation.FrameworkDescription.Trim());
Runtime = new Platform
{
Name = runtime.Groups[1].Value,
Version = runtime.Groups[2].Value,
};
try
{
// Almost nothing will correctly identify Windows 11 at this point, so we have to use WMI.
var operatingSystem = new ManagementClass("Win32_OperatingSystem").GetInstances().Cast<ManagementObject>().First();
OperatingSystem = new Platform
{
Name = (string)operatingSystem["Caption"],
Version = (string)operatingSystem["Version"],
Architecture = RuntimeInformation.OSArchitecture.ToString(),
Language = CultureInfo.CurrentUICulture.IetfLanguageTag,
Languages = (string[])operatingSystem["MUILanguages"],
};
}
catch (ManagementException error)
{
Trace.WriteLine(error);
}
NativeMethods.GlobalMemoryStatusEx(MemoryStatusExtended);
InstalledMemoryMB = (int)(MemoryStatusExtended.TotalPhysical / 1024 / 1024);
try
{
CPUs = new ManagementClass("Win32_Processor").GetInstances().Cast<ManagementObject>().Select(processor => new CPU
{
Name = (string)processor["Name"],
Manufacturer = (string)processor["Manufacturer"],
ThreadCount = (uint)processor["ThreadCount"],
MaxClockMHz = (uint)processor["MaxClockSpeed"],
}).ToList();
}
catch (ManagementException error)
{
Trace.WriteLine(error);
}
// The WMI data for AdapterRAM is unreliable, so we have to use DXGI to get the real numbers.
// Alas, DXGI doesn't give us the manufacturer name for the adapter, so we combine it with WMI.
var descriptions = new Factory1().Adapters.Select(adapter => adapter.Description).ToArray();
try
{
GPUs = new ManagementClass("Win32_VideoController").GetInstances().Cast<ManagementObject>().Select(adapter => new GPU
{
Name = (string)adapter["Name"],
Manufacturer = (string)adapter["AdapterCompatibility"],
MemoryMB = (uint)((long)descriptions.FirstOrDefault(desc => desc.Description == (string)adapter["Name"]).DedicatedVideoMemory / 1024 / 1024),
}).ToList();
}
catch (ManagementException error)
{
Trace.WriteLine(error);
}
var featureLevels = new uint[] {
NativeMethods.D3D_FEATURE_LEVEL_12_2,
NativeMethods.D3D_FEATURE_LEVEL_12_1,
NativeMethods.D3D_FEATURE_LEVEL_12_0,
NativeMethods.D3D_FEATURE_LEVEL_11_1,
NativeMethods.D3D_FEATURE_LEVEL_11_0,
NativeMethods.D3D_FEATURE_LEVEL_10_1,
NativeMethods.D3D_FEATURE_LEVEL_10_0,
NativeMethods.D3D_FEATURE_LEVEL_9_3,
NativeMethods.D3D_FEATURE_LEVEL_9_2,
NativeMethods.D3D_FEATURE_LEVEL_9_1,
};
foreach (var featureLevel in featureLevels)
{
var levels = new uint[] { featureLevel };
try
{
var rv = NativeMethods.D3D11CreateDevice(IntPtr.Zero, NativeMethods.D3D_DRIVER_TYPE_HARDWARE, IntPtr.Zero, 0, levels, levels.Length, NativeMethods.D3D11_SDK_VERSION, IntPtr.Zero, out uint level, IntPtr.Zero);
if (level == featureLevel) Direct3DFeatureLevels.Add(string.Format("{0}_{1}", level >> 12 & 0xF, level >> 8 & 0xF));
}
catch (EntryPointNotFoundException) { }
catch (DllNotFoundException) { }
}
}
public static readonly Platform Application;
public static readonly Platform Runtime;
public static readonly Platform OperatingSystem;
public static readonly int InstalledMemoryMB;
public static readonly List<CPU> CPUs = new List<CPU>();
public static readonly List<GPU> GPUs = new List<GPU>();
public static readonly List<string> Direct3DFeatureLevels = new List<string>();
public static void WriteSystemDetails(TextWriter output)
{
output.WriteLine("Date/time = {0} ({1:u})",
DateTime.Now, DateTime.UtcNow);
output.WriteLine("Application = {0} {1} ({2})", Application.Name, Application.Version, Application.Architecture);
output.WriteLine("Runtime = {0} {1}", Runtime.Name, Runtime.Version);
output.WriteLine("System = {0} {1} ({2}; {3}; {4})", OperatingSystem.Name, OperatingSystem.Version, OperatingSystem.Architecture, OperatingSystem.Language, string.Join(",", OperatingSystem.Languages ?? new string[0]));
output.WriteLine("Memory = {0:N0} MB", InstalledMemoryMB);
foreach (var cpu in CPUs) output.WriteLine("CPU = {0} ({1}; {2} threads; {3:N0} MHz)", cpu.Name, cpu.Manufacturer, cpu.ThreadCount, cpu.MaxClockMHz);
foreach (var gpu in GPUs) output.WriteLine("GPU = {0} ({1}; {2:N0} MB)", gpu.Name, gpu.Manufacturer, gpu.MemoryMB);
output.WriteLine("Direct3D = {0}", string.Join(",", Direct3DFeatureLevels));
}
public struct Platform
{
public string Name;
public string Version;
public string Architecture;
public string Language;
public string[] Languages;
}
public struct CPU
{
public string Name;
public string Manufacturer;
public uint ThreadCount;
public uint MaxClockMHz;
}
public struct GPU
{
public string Name;
public string Manufacturer;
public uint MemoryMB;
}
static class NativeStructs
{
[StructLayout(LayoutKind.Sequential, Size = 64)]
public class MemoryStatusExtended
{
public uint Size;
public uint MemoryLoad;
public ulong TotalPhysical;
public ulong AvailablePhysical;
public ulong TotalPageFile;
public ulong AvailablePageFile;
public ulong TotalVirtual;
public ulong AvailableVirtual;
public ulong AvailableExtendedVirtual;
}
}
static class NativeMethods
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GlobalMemoryStatusEx([In, Out] NativeStructs.MemoryStatusExtended buffer);
public const uint D3D11_SDK_VERSION = 7;
public const uint D3D_DRIVER_TYPE_HARDWARE = 1;
public const uint D3D_FEATURE_LEVEL_9_1 = 0x9100;
public const uint D3D_FEATURE_LEVEL_9_2 = 0x9200;
public const uint D3D_FEATURE_LEVEL_9_3 = 0x9300;
public const uint D3D_FEATURE_LEVEL_10_0 = 0xA000;
public const uint D3D_FEATURE_LEVEL_10_1 = 0xA100;
public const uint D3D_FEATURE_LEVEL_11_0 = 0xB000;
public const uint D3D_FEATURE_LEVEL_11_1 = 0xB100;
public const uint D3D_FEATURE_LEVEL_12_0 = 0xC000;
public const uint D3D_FEATURE_LEVEL_12_1 = 0xC100;
public const uint D3D_FEATURE_LEVEL_12_2 = 0xC200;
[DllImport("d3d11.dll", ExactSpelling = true)]
public static extern uint D3D11CreateDevice(
IntPtr adapter,
uint driverType,
IntPtr software,
uint flags,
[In] uint[] featureLevels,
int featureLevelCount,
uint sdkVersion,
IntPtr device,
out uint featureLevel,
IntPtr immediateContext
);
}
}
}