Skip to content

Commit

Permalink
imp - SpecProbe is now nullable ready!
Browse files Browse the repository at this point in the history
---

We've added full nullable support for SpecProbe!

---

Type: imp
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Sep 4, 2024
1 parent a9365a6 commit 06f899c
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 44 deletions.
59 changes: 35 additions & 24 deletions SpecProbe.ConsoleTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion SpecProbe.Loader/LibraryFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ internal bool NativeMethodExists(string methodName, out IntPtr ptr)
return ptr != IntPtr.Zero;
}

internal T GetNativeMethodDelegate<T>(IntPtr ptr)
internal T? GetNativeMethodDelegate<T>(IntPtr ptr)
where T : class =>
Marshal.GetDelegateForFunctionPointer(ptr, typeof(T)) as T;

Expand Down
4 changes: 2 additions & 2 deletions SpecProbe.Loader/LibraryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public void LoadNativeLibrary()
/// <typeparam name="T">Target type</typeparam>
/// <param name="methodName">Native method name</param>
/// <returns></returns>
public T GetNativeMethodDelegate<T>(string methodName)
public T? GetNativeMethodDelegate<T>(string methodName)
where T : class
{
T nativeDelegate = null;
T? nativeDelegate = null;
foreach (var libraryFile in _files)
{
if (libraryFile.NativeMethodExists(methodName, out IntPtr ptr))
Expand Down
25 changes: 19 additions & 6 deletions SpecProbe.Software/Platform/RidGraphReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
Expand Down Expand Up @@ -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<string> 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<string>());
currentGraph = graphInstance[finalGraph[finalGraph.Count - 1]];
foreach (var element in graphImport)
{
if (element is not null)
finalGraph.Add(element.GetValue<string>());
}
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}");
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions SpecProbe/HardwareProber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static class HardwareProber
/// <summary>
/// Gets processor information
/// </summary>
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;
Expand All @@ -49,7 +49,7 @@ public static ProcessorPart GetProcessor()
/// <summary>
/// Gets memory information
/// </summary>
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;
Expand All @@ -61,7 +61,7 @@ public static MemoryPart GetMemory()
/// <summary>
/// Gets the list of video cards
/// </summary>
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[];
Expand All @@ -73,7 +73,7 @@ public static VideoPart[] GetVideos()
/// <summary>
/// Gets the list of hard disks
/// </summary>
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[];
Expand Down
2 changes: 1 addition & 1 deletion SpecProbe/Parts/Types/HardDiskPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public long PartitionOffset
private ulong hardDiskSize;
private int hardDiskNum;
private PartitionTableType partTableType = PartitionTableType.Unknown;
private PartitionPart[] parts;
private PartitionPart[] parts = [];

/// <inheritdoc/>
public override HardwarePartType Type =>
Expand Down
4 changes: 2 additions & 2 deletions SpecProbe/Parts/Types/ProcessorPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion SpecProbe/Parts/Types/VideoPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace SpecProbe.Parts.Types
/// </summary>
public class VideoPart : BaseHardwarePartInfo, IHardwarePartInfo
{
private string videoCardName;
private string videoCardName = "Unknown";
private uint vendorId;
private uint modelId;

Expand Down
2 changes: 1 addition & 1 deletion SpecProbe/Probers/Platform/PlatformMacInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal static unsafe class PlatformMacInterop
/// CGError CGGetOnlineDisplayList(uint32_t maxDisplays, CGDirectDisplayID *onlineDisplays, uint32_t *displayCount);
/// </summary>
[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);

/// <summary>
/// CGError CGGetOnlineDisplayList(uint32_t maxDisplays, CGDirectDisplayID *onlineDisplays, uint32_t *displayCount);
Expand Down
4 changes: 2 additions & 2 deletions SpecProbe/Probers/Platform/PlatformWindowsInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 06f899c

Please sign in to comment.