From 153e76f50f54feede3b454843adb0853ce481787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89=E9=B8=AD=E8=9B=8B?= Date: Wed, 15 Jan 2025 00:32:09 +0800 Subject: [PATCH] DxDiag --- .../BetterGenshinImpact.csproj | 3 +- .../Helpers/Device/AllPcInfo.cs | 154 ++++++++++++++++++ BetterGenshinImpact/Helpers/Device/PCInfo.cs | 4 +- .../Service/Singletons/StartEndSingleton.cs | 2 +- 4 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 BetterGenshinImpact/Helpers/Device/AllPcInfo.cs diff --git a/BetterGenshinImpact/BetterGenshinImpact.csproj b/BetterGenshinImpact/BetterGenshinImpact.csproj index 72d2cd4c..7afcef52 100644 --- a/BetterGenshinImpact/BetterGenshinImpact.csproj +++ b/BetterGenshinImpact/BetterGenshinImpact.csproj @@ -10,7 +10,7 @@ true Assets\Images\logo.ico BetterGI - 10.39.2 + 10.39.3 x64 embedded @@ -72,6 +72,7 @@ + diff --git a/BetterGenshinImpact/Helpers/Device/AllPcInfo.cs b/BetterGenshinImpact/Helpers/Device/AllPcInfo.cs new file mode 100644 index 00000000..be9fb8a2 --- /dev/null +++ b/BetterGenshinImpact/Helpers/Device/AllPcInfo.cs @@ -0,0 +1,154 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; +using BetterGenshinImpact.Core.Config; +using BetterGenshinImpact.GameTask.Common; +using Microsoft.Extensions.Logging; + +namespace BetterGenshinImpact.Helpers.Device; + +[Serializable] +public class AllPcInfo +{ + public PCInfo? WMI { get; set; } + + public object? DxDiag { get; set; } + + public static string GetJson() + { + return Newtonsoft.Json.JsonConvert.SerializeObject(GetPcInfo()); + } + + public static AllPcInfo GetPcInfo() + { + var pcInfo = new AllPcInfo(); + pcInfo.WMI = GetPCInfo.GetClass(); + pcInfo.DxDiag = GetDxDiagInfo(); + return pcInfo; + } + + + static object? GetDxDiagInfo() + { + try + { + var path = Global.Absolute("User\\dxdiag.txt"); + + if (!File.Exists(path)) + { + ProcessStartInfo psi = new ProcessStartInfo + { + FileName = "dxdiag", + Arguments = $"/t \"{path}\"", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + Process? process = Process.Start(psi); + process?.WaitForExit(); + + if (process?.ExitCode != 0) + { + Debug.WriteLine("DxDiag命令执行失败。"); + TaskControl.Logger.LogDebug("DxDiag命令执行失败。" + process?.ExitCode); + return null; + } + } + + + // Read the input file + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); + string input = File.ReadAllText(path, Encoding.GetEncoding("GB2312")); + + // Parse the content + var parsed = Parse(input); + Debug.WriteLine("DxDiag命令执行成功。"); + return parsed; + } + catch (Exception e) + { + Debug.WriteLine("DxDiag命令执行失败:" + e.Message); + TaskControl.Logger.LogDebug("DxDiag命令执行失败:" + e.Source + "\r\n--" + Environment.NewLine + e.StackTrace + "\r\n---" + Environment.NewLine + e.Message); + } + return null; + } + + public static Dictionary Parse(string input) + { + var result = new Dictionary(); + var currentSection = ""; + var currentData = new Dictionary(); + var keyCount = new Dictionary(); + + + string[] lines = input.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None); + + foreach (var line in lines) + { + // Skip empty lines + if (string.IsNullOrWhiteSpace(line)) + continue; + + // Check if it's a section header + if (line.StartsWith("----")) + { + // Save previous section if exists + if (!string.IsNullOrEmpty(currentSection) && currentData.Count > 0) + { + result[currentSection] = currentData; + currentData = new Dictionary(); + } + + // Get next line as section name + continue; + } + + // If line contains only letters and spaces, it's a section name + if (Regex.IsMatch(line.Trim(), @"^[A-Za-z ]+$")) + { + currentSection = line.Trim(); + continue; + } + + // Parse key-value pairs + if (line.Contains(":")) + { + var colonIndex = line.IndexOf(':'); + var key = line.Substring(0, colonIndex).Trim(); + var value = line.Substring(colonIndex + 1).Trim(); + + // Add to current section data + if (!string.IsNullOrEmpty(key)) + { + if (currentData.ContainsKey(key)) + { + if (!keyCount.ContainsKey(key)) + { + keyCount[key] = 1; + } + keyCount[key]++; + key = $"{key}_{keyCount[key]}"; + } + else + { + keyCount[key] = 1; + } + + currentData[key] = value; + } + } + } + + // Add last section + if (!string.IsNullOrEmpty(currentSection) && currentData.Count > 0) + { + result[currentSection] = currentData; + } + + return result; + } +} \ No newline at end of file diff --git a/BetterGenshinImpact/Helpers/Device/PCInfo.cs b/BetterGenshinImpact/Helpers/Device/PCInfo.cs index 8035b6b7..49e7f10b 100644 --- a/BetterGenshinImpact/Helpers/Device/PCInfo.cs +++ b/BetterGenshinImpact/Helpers/Device/PCInfo.cs @@ -1,7 +1,9 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace BetterGenshinImpact.Helpers.Device; +[Serializable] public class PCInfo { public CPU[] CPU { get; set; } diff --git a/BetterGenshinImpact/Service/Singletons/StartEndSingleton.cs b/BetterGenshinImpact/Service/Singletons/StartEndSingleton.cs index 1c9ff955..3541409d 100644 --- a/BetterGenshinImpact/Service/Singletons/StartEndSingleton.cs +++ b/BetterGenshinImpact/Service/Singletons/StartEndSingleton.cs @@ -50,7 +50,7 @@ public class StartEndSingleton : Singleton { try { - var json = GetPCInfo.GetJson(); + var json = AllPcInfo.GetJson(); // 保存 File.WriteAllText(Global.Absolute(@$"User/pc.json"), json); }