forked from stride3d/stride
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppHelper.cs
More file actions
93 lines (82 loc) · 3.12 KB
/
Copy pathAppHelper.cs
File metadata and controls
93 lines (82 loc) · 3.12 KB
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
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System.Management;
using System.Text;
using Stride.Core.Extensions;
using System.Runtime.InteropServices;
namespace Stride.Core.Windows;
public static class AppHelper
{
public static string[] GetCommandLineArgs()
{
return Environment.GetCommandLineArgs().Skip(1).ToArray();
}
public static string BuildErrorMessage(Exception exception, string? header = null)
{
var body = new StringBuilder();
if (header != null)
{
body.Append(header);
}
body.AppendLine($"Current Directory: {Environment.CurrentDirectory}");
body.AppendLine($"Command Line Args: {string.Join(" ", GetCommandLineArgs())}");
body.AppendLine($"OS Version: {RuntimeInformation.OSDescription} ({(Environment.Is64BitOperatingSystem ? "x64" : "x86")})");
body.AppendLine($"Processor Count: {Environment.ProcessorCount}");
body.AppendLine("Video configuration:");
WriteVideoConfig(body);
body.AppendLine($"Exception: {exception.FormatFull()}");
return body.ToString();
}
public static void WriteVideoConfig(StringBuilder writer)
{
try
{
var i = 0;
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
foreach (var managementObject in searcher.Get().OfType<ManagementObject>())
{
writer.AppendLine($"GPU {++i}");
foreach (var property in managementObject.Properties)
{
writer.AppendLine($" {property.Name}: {property.Value}");
}
}
}
catch (Exception)
{
writer.AppendLine("An error occurred while trying to retrieve video configuration.");
}
}
public static Dictionary<string, string> GetVideoConfig()
{
return OperatingSystem.IsWindows() ? GetVideoConfigWindows() : [];
}
private static Dictionary<string, string> GetVideoConfigWindows()
{
var result = new Dictionary<string, string>();
if (OperatingSystem.IsWindows())
GetVideoConfigWindows(result);
return result;
}
private static void GetVideoConfigWindows(Dictionary<string, string> result)
{
try
{
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController");
int deviceId = 0;
foreach (var managementObject in searcher.Get().OfType<ManagementObject>())
{
foreach (var property in managementObject.Properties)
{
if (property.Value == null) continue;
result.Add($"GPU{deviceId}.{property.Name}", property.Value.ToString());
}
deviceId++;
}
}
catch (Exception)
{
// ignored
}
}
}