From 06f899c81f37fbefdd677ae7ec6abc89c5dcfc12 Mon Sep 17 00:00:00 2001 From: Aptivi Date: Wed, 4 Sep 2024 12:14:36 +0300 Subject: [PATCH] imp - SpecProbe is now nullable ready! --- We've added full nullable support for SpecProbe! --- Type: imp Breaking: False Doc Required: False Backport Required: False Part: 1/1 --- SpecProbe.ConsoleTest/Program.cs | 59 +++++++++++-------- SpecProbe.Loader/LibraryFile.cs | 2 +- SpecProbe.Loader/LibraryManager.cs | 4 +- SpecProbe.Software/Platform/RidGraphReader.cs | 25 ++++++-- SpecProbe/HardwareProber.cs | 8 +-- SpecProbe/Parts/Types/HardDiskPart.cs | 2 +- SpecProbe/Parts/Types/ProcessorPart.cs | 4 +- SpecProbe/Parts/Types/VideoPart.cs | 2 +- .../Probers/Platform/PlatformMacInterop.cs | 2 +- .../Platform/PlatformWindowsInterop.cs | 4 +- 10 files changed, 68 insertions(+), 44 deletions(-) diff --git a/SpecProbe.ConsoleTest/Program.cs b/SpecProbe.ConsoleTest/Program.cs index d065907..c7529cd 100644 --- a/SpecProbe.ConsoleTest/Program.cs +++ b/SpecProbe.ConsoleTest/Program.cs @@ -22,6 +22,7 @@ using SpecProbe.Pci; using SpecProbe.Software.Platform; using System.Diagnostics; +using Terminaux.Colors.Data; using Terminaux.Writer.ConsoleWriters; using Terminaux.Writer.FancyWriters; @@ -43,22 +44,27 @@ public static void Main() var processor = HardwareProber.GetProcessor(); var processorErrors = HardwareProber.GetParseExceptions(HardwarePartType.Processor); stopwatch.Stop(); - TextWriterColor.WriteColor("- Processor cores: ", false, 3); - TextWriterColor.WriteColor($"{processor.ProcessorCores}", true, 8); - TextWriterColor.WriteColor("- Cores for each core: ", false, 3); - TextWriterColor.WriteColor($"{processor.Cores}", true, 8); - TextWriterColor.WriteColor("- Logical cores: ", false, 3); - TextWriterColor.WriteColor($"{processor.LogicalCores}", true, 8); - TextWriterColor.WriteColor("- L1, L2, L3 cache sizes in bytes: ", false, 3); - TextWriterColor.WriteColor($"{processor.L1CacheSize}, {processor.L2CacheSize}, {processor.L3CacheSize}", true, 8); - TextWriterColor.WriteColor("- Name: ", false, 3); - TextWriterColor.WriteColor($"{(processor.Hypervisor ? "[virt'd] " : "")}{processor.Name}", true, 8); - TextWriterColor.WriteColor("- Vendor (CPUID): ", false, 3); - TextWriterColor.WriteColor($"{processor.CpuidVendor}", true, 8); - TextWriterColor.WriteColor("- Vendor (Real): ", false, 3); - TextWriterColor.WriteColor($"{processor.Vendor}", true, 8); - TextWriterColor.WriteColor("- Clock speed: ", false, 3); - TextWriterColor.WriteColor($"{processor.Speed}", true, 8); + if (processor is not null) + { + TextWriterColor.WriteColor("- Processor cores: ", false, 3); + TextWriterColor.WriteColor($"{processor.ProcessorCores}", true, 8); + TextWriterColor.WriteColor("- Cores for each core: ", false, 3); + TextWriterColor.WriteColor($"{processor.Cores}", true, 8); + TextWriterColor.WriteColor("- Logical cores: ", false, 3); + TextWriterColor.WriteColor($"{processor.LogicalCores}", true, 8); + TextWriterColor.WriteColor("- L1, L2, L3 cache sizes in bytes: ", false, 3); + TextWriterColor.WriteColor($"{processor.L1CacheSize}, {processor.L2CacheSize}, {processor.L3CacheSize}", true, 8); + TextWriterColor.WriteColor("- Name: ", false, 3); + TextWriterColor.WriteColor($"{(processor.Hypervisor ? "[virt'd] " : "")}{processor.Name}", true, 8); + TextWriterColor.WriteColor("- Vendor (CPUID): ", false, 3); + TextWriterColor.WriteColor($"{processor.CpuidVendor}", true, 8); + TextWriterColor.WriteColor("- Vendor (Real): ", false, 3); + TextWriterColor.WriteColor($"{processor.Vendor}", true, 8); + TextWriterColor.WriteColor("- Clock speed: ", false, 3); + TextWriterColor.WriteColor($"{processor.Speed}", true, 8); + } + else + TextWriterColor.WriteColor("- Unable to fetch processors.", ConsoleColors.Red); TextWriterRaw.Write(); foreach (var exc in processorErrors) { @@ -77,12 +83,17 @@ public static void Main() var memory = HardwareProber.GetMemory(); var memoryErrors = HardwareProber.GetParseExceptions(HardwarePartType.Memory); stopwatch.Stop(); - TextWriterColor.WriteColor("- Total memory (system): ", false, 3); - TextWriterColor.WriteColor($"{memory.TotalMemory}", true, 8); - TextWriterColor.WriteColor("- Total memory (real): ", false, 3); - TextWriterColor.WriteColor($"{memory.TotalPhysicalMemory}", true, 8); - TextWriterColor.WriteColor("- Reserved memory: ", false, 3); - TextWriterColor.WriteColor($"{memory.SystemReservedMemory}", true, 8); + if (memory is not null) + { + TextWriterColor.WriteColor("- Total memory (system): ", false, 3); + TextWriterColor.WriteColor($"{memory.TotalMemory}", true, 8); + TextWriterColor.WriteColor("- Total memory (real): ", false, 3); + TextWriterColor.WriteColor($"{memory.TotalPhysicalMemory}", true, 8); + TextWriterColor.WriteColor("- Reserved memory: ", false, 3); + TextWriterColor.WriteColor($"{memory.SystemReservedMemory}", true, 8); + } + else + TextWriterColor.WriteColor("- Unable to fetch processors.", ConsoleColors.Red); TextWriterRaw.Write(); foreach (var exc in memoryErrors) { @@ -98,7 +109,7 @@ public static void Main() // Video SeparatorWriterColor.WriteSeparator("Video information", true, 15); stopwatch.Start(); - var videoParts = HardwareProber.GetVideos(); + var videoParts = HardwareProber.GetVideos() ?? []; var videoErrors = HardwareProber.GetParseExceptions(HardwarePartType.Video); stopwatch.Stop(); foreach (var video in videoParts) @@ -129,7 +140,7 @@ public static void Main() // Hard drive SeparatorWriterColor.WriteSeparator("Hard drive information", true, 15); stopwatch.Start(); - var hardDiskParts = HardwareProber.GetHardDisks(); + var hardDiskParts = HardwareProber.GetHardDisks() ?? []; var hardDiskErrors = HardwareProber.GetParseExceptions(HardwarePartType.HardDisk); stopwatch.Stop(); foreach (var hardDisk in hardDiskParts) diff --git a/SpecProbe.Loader/LibraryFile.cs b/SpecProbe.Loader/LibraryFile.cs index e62b961..2f12822 100644 --- a/SpecProbe.Loader/LibraryFile.cs +++ b/SpecProbe.Loader/LibraryFile.cs @@ -123,7 +123,7 @@ internal bool NativeMethodExists(string methodName, out IntPtr ptr) return ptr != IntPtr.Zero; } - internal T GetNativeMethodDelegate(IntPtr ptr) + internal T? GetNativeMethodDelegate(IntPtr ptr) where T : class => Marshal.GetDelegateForFunctionPointer(ptr, typeof(T)) as T; diff --git a/SpecProbe.Loader/LibraryManager.cs b/SpecProbe.Loader/LibraryManager.cs index 99abcdd..4b35f74 100644 --- a/SpecProbe.Loader/LibraryManager.cs +++ b/SpecProbe.Loader/LibraryManager.cs @@ -60,10 +60,10 @@ public void LoadNativeLibrary() /// Target type /// Native method name /// - public T GetNativeMethodDelegate(string methodName) + public T? GetNativeMethodDelegate(string methodName) where T : class { - T nativeDelegate = null; + T? nativeDelegate = null; foreach (var libraryFile in _files) { if (libraryFile.NativeMethodExists(methodName, out IntPtr ptr)) diff --git a/SpecProbe.Software/Platform/RidGraphReader.cs b/SpecProbe.Software/Platform/RidGraphReader.cs index 88b3127..d9e9947 100644 --- a/SpecProbe.Software/Platform/RidGraphReader.cs +++ b/SpecProbe.Software/Platform/RidGraphReader.cs @@ -17,6 +17,7 @@ // along with this program. If not, see . // +using System; using System.Collections.Generic; using System.IO; using System.Text.Json; @@ -44,19 +45,31 @@ public static string[] GetGraphFromRid(string rid) { // Sync with this source: https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.NETCore.Platforms/src/runtime.json string graphJson = GetRidGraphJson(); - var graphInstance = JsonNode.Parse(graphJson)["runtimes"]; + var fullGraph = JsonNode.Parse(graphJson) ?? + throw new Exception("Unable to fetch the graph"); + var graphInstance = fullGraph["runtimes"] ?? + throw new Exception("Unable to fetch the runtimes list"); List finalGraph = []; foreach (var ridGraph in graphInstance.AsObject()) { if (ridGraph.Key == rid) { finalGraph.Add(ridGraph.Key); - var currentGraph = ridGraph.Value; - while (((JsonArray)currentGraph["#import"]).Count > 0) + var currentGraph = ridGraph.Value ?? + throw new Exception($"Unable to fetch the current graph for {ridGraph.Key}"); + var graphImport = (JsonArray?)currentGraph["#import"] ?? + throw new Exception($"Unable to fetch the current graph imports for {ridGraph.Key}"); + while (graphImport.Count > 0) { - foreach (var element in (JsonArray)currentGraph["#import"]) - finalGraph.Add(element.GetValue()); - currentGraph = graphInstance[finalGraph[finalGraph.Count - 1]]; + foreach (var element in graphImport) + { + if (element is not null) + finalGraph.Add(element.GetValue()); + } + currentGraph = graphInstance[finalGraph[finalGraph.Count - 1]] ?? + throw new Exception($"Unable to fetch the current graph for {ridGraph.Key}"); + graphImport = (JsonArray?)currentGraph["#import"] ?? + throw new Exception($"Unable to fetch the current graph imports for {ridGraph.Key}"); } } } diff --git a/SpecProbe/HardwareProber.cs b/SpecProbe/HardwareProber.cs index 066cdd0..357f23d 100644 --- a/SpecProbe/HardwareProber.cs +++ b/SpecProbe/HardwareProber.cs @@ -37,7 +37,7 @@ public static class HardwareProber /// /// Gets processor information /// - public static ProcessorPart GetProcessor() + public static ProcessorPart? GetProcessor() { if (cachedParts.Keys.Contains(HardwarePartType.Processor) && cachedParts[HardwarePartType.Processor].parts.Length > 0) return cachedParts[HardwarePartType.Processor].parts[0] as ProcessorPart; @@ -49,7 +49,7 @@ public static ProcessorPart GetProcessor() /// /// Gets memory information /// - public static MemoryPart GetMemory() + public static MemoryPart? GetMemory() { if (cachedParts.Keys.Contains(HardwarePartType.Memory) && cachedParts[HardwarePartType.Memory].parts.Length > 0) return cachedParts[HardwarePartType.Memory].parts[0] as MemoryPart; @@ -61,7 +61,7 @@ public static MemoryPart GetMemory() /// /// Gets the list of video cards /// - public static VideoPart[] GetVideos() + public static VideoPart[]? GetVideos() { if (cachedParts.Keys.Contains(HardwarePartType.Video) && cachedParts[HardwarePartType.Video].parts.Length > 0) return cachedParts[HardwarePartType.Video].parts as VideoPart[]; @@ -73,7 +73,7 @@ public static VideoPart[] GetVideos() /// /// Gets the list of hard disks /// - public static HardDiskPart[] GetHardDisks() + public static HardDiskPart[]? GetHardDisks() { if (cachedParts.Keys.Contains(HardwarePartType.HardDisk) && cachedParts[HardwarePartType.HardDisk].parts.Length > 0) return cachedParts[HardwarePartType.HardDisk].parts as HardDiskPart[]; diff --git a/SpecProbe/Parts/Types/HardDiskPart.cs b/SpecProbe/Parts/Types/HardDiskPart.cs index c0215c3..9d3dbb8 100644 --- a/SpecProbe/Parts/Types/HardDiskPart.cs +++ b/SpecProbe/Parts/Types/HardDiskPart.cs @@ -87,7 +87,7 @@ public long PartitionOffset private ulong hardDiskSize; private int hardDiskNum; private PartitionTableType partTableType = PartitionTableType.Unknown; - private PartitionPart[] parts; + private PartitionPart[] parts = []; /// public override HardwarePartType Type => diff --git a/SpecProbe/Parts/Types/ProcessorPart.cs b/SpecProbe/Parts/Types/ProcessorPart.cs index 4e09835..551bf9b 100644 --- a/SpecProbe/Parts/Types/ProcessorPart.cs +++ b/SpecProbe/Parts/Types/ProcessorPart.cs @@ -30,8 +30,8 @@ public class ProcessorPart : BaseHardwarePartInfo, IHardwarePartInfo private uint cacheL1; private uint cacheL2; private uint cacheL3; - private string name; - private string cpuidVendor; + private string name = "Unknown"; + private string cpuidVendor = "Unknown"; private double speed; private bool hypervisor; diff --git a/SpecProbe/Parts/Types/VideoPart.cs b/SpecProbe/Parts/Types/VideoPart.cs index 1091f85..65bd52d 100644 --- a/SpecProbe/Parts/Types/VideoPart.cs +++ b/SpecProbe/Parts/Types/VideoPart.cs @@ -26,7 +26,7 @@ namespace SpecProbe.Parts.Types /// public class VideoPart : BaseHardwarePartInfo, IHardwarePartInfo { - private string videoCardName; + private string videoCardName = "Unknown"; private uint vendorId; private uint modelId; diff --git a/SpecProbe/Probers/Platform/PlatformMacInterop.cs b/SpecProbe/Probers/Platform/PlatformMacInterop.cs index b664934..de0e264 100644 --- a/SpecProbe/Probers/Platform/PlatformMacInterop.cs +++ b/SpecProbe/Probers/Platform/PlatformMacInterop.cs @@ -33,7 +33,7 @@ internal static unsafe class PlatformMacInterop /// CGError CGGetOnlineDisplayList(uint32_t maxDisplays, CGDirectDisplayID *onlineDisplays, uint32_t *displayCount); /// [DllImport(cgFrameworkPath)] - public static extern CGError CGGetOnlineDisplayList(uint maxDisplays, uint[] onlineDisplays, out uint displayCount); + public static extern CGError CGGetOnlineDisplayList(uint maxDisplays, uint[]? onlineDisplays, out uint displayCount); /// /// CGError CGGetOnlineDisplayList(uint32_t maxDisplays, CGDirectDisplayID *onlineDisplays, uint32_t *displayCount); diff --git a/SpecProbe/Probers/Platform/PlatformWindowsInterop.cs b/SpecProbe/Probers/Platform/PlatformWindowsInterop.cs index b772f73..60c249a 100644 --- a/SpecProbe/Probers/Platform/PlatformWindowsInterop.cs +++ b/SpecProbe/Probers/Platform/PlatformWindowsInterop.cs @@ -622,7 +622,7 @@ IntPtr lpOverlapped public static extern bool DeviceIoControl( IntPtr hDevice, EIOControlCode IoControlCode, - byte[] InBuffer, + byte[]? InBuffer, int nInBufferSize, out DISK_GEOMETRY OutBuffer, int nOutBufferSize, @@ -634,7 +634,7 @@ IntPtr Overlapped public static extern bool DeviceIoControl( IntPtr hDevice, EIOControlCode IoControlCode, - byte[] InBuffer, + byte[]? InBuffer, int nInBufferSize, out STORAGE_DEVICE_NUMBER OutBuffer, int nOutBufferSize,