Free, open-source Windows optimization tool for performance, privacy, and simplicity.
C# csharpdebloatdotnetgamingopen-sourceoptimizationoptimizeroptimizerduckperformanceprivacytweakertweakswindowswinui
feat(systeminfo): add disk info and SSD detection
itsfatduck44e72dddaa20
diff --git a/optimizerDuck.Test/SystemInfoServiceTests.cs b/optimizerDuck.Test/SystemInfoServiceTests.csindex ca20c9e..7638d26 100644--- a/optimizerDuck.Test/SystemInfoServiceTests.cs+++ b/optimizerDuck.Test/SystemInfoServiceTests.cs@@ -150,7 +150,11 @@ public class SystemInfoServiceTests"PCI\\VEN_10DE","PCI_VEN_10DE");++ var disk = new DiskInfo([+ new DiskVolume("C:", "NTFS", "Fixed", "System", 500.0, 250.0, 250.0, 50.0, "SSD")+ ]);- return new SystemSnapshot(cpu, ram, os, bios, [gpu], gpu);+ return new SystemSnapshot(cpu, ram, os, bios, [gpu], gpu, disk);}}\ No newline at end of filediff --git a/optimizerDuck/src/Core/Managers/OptimizationManager.cs b/optimizerDuck/src/Core/Managers/OptimizationManager.csindex 4bb67a9..a10ac53 100644--- a/optimizerDuck/src/Core/Managers/OptimizationManager.cs+++ b/optimizerDuck/src/Core/Managers/OptimizationManager.cs@@ -176,14 +176,14 @@ public class OptimizationManager(SystemSnapshot systemSnapshot)SystemHelper.Title("Setting up");- return Task.FromResult(PromptDialog.Warning("Restore Point Before Continuing",+ return Task.FromResult(PromptDialog.Warning("Create Restore Point",$"""- We [{Theme.Success}]RECOMMEND[/] you to create a Restore Point before continuing.- This way, you will be [{Theme.Success}]protected[/] against system-level changes and can always revert them if needed.+ For your safety, we [{Theme.Success}]highly recommend[/] creating a system Restore Point now.+ It acts as a safeguard, allowing you to [{Theme.Success}]roll back[/] any unwanted system changes later.""",new PromptOption("Yes", Theme.Success, () =>{- if (SystemProtectionHelper.Enable()) // if we can enable, create+ if (SystemProtectionHelper.Enable())SystemProtectionHelper.Create();}),new PromptOption("Skip", Theme.Error, () => true),diff --git a/optimizerDuck/src/Core/Services/SystemInfoService.cs b/optimizerDuck/src/Core/Services/SystemInfoService.csindex 856c500..d0d613d 100644--- a/optimizerDuck/src/Core/Services/SystemInfoService.cs+++ b/optimizerDuck/src/Core/Services/SystemInfoService.cs@@ -3,6 +3,8 @@ using optimizerDuck.UI;using Spectre.Console;using Spectre.Console.Rendering;using System.Management;+using System.Runtime.InteropServices;+using Microsoft.Win32.SafeHandles;namespace optimizerDuck.Core.Services;@@ -49,6 +51,24 @@ public sealed record CpuInfo(public static readonly CpuInfo Unknown = new("Unknown", "Unknown", "Unknown", "Unknown", 0, 0, 0, 0, 0, 0);}+public sealed record DiskVolume(+ string Name,+ string FileSystem,+ string DriveType,+ string Label,+ double TotalSizeGB,+ double FreeSpaceGB,+ double UsedSpaceGB,+ double UsedPercent,+ string DriveTypeDescription+);++public sealed record DiskInfo(+ IReadOnlyList<DiskVolume> Volumes+)+{+ public static readonly DiskInfo Unknown = new([]);+}public sealed record RamModule(double CapacityGB,string SpeedMHz,@@ -101,7 +121,8 @@ public sealed record SystemSnapshot(OsInfo Os,BiosInfo Bios,IReadOnlyList<GpuInfo> Gpus,- GpuInfo? PrimaryGpu+ GpuInfo? PrimaryGpu,+ DiskInfo Disk){public static readonly SystemSnapshot Unknown = new(@@ -110,7 +131,8 @@ public sealed record SystemSnapshot(OsInfo.Unknown,BiosInfo.Unknown,[],- null+ null,+ DiskInfo.Unknown);}@@ -306,6 +328,142 @@ internal static class RamProvider}}+public static class DiskHelper+{+ [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]+ static extern SafeFileHandle CreateFile(+ string lpFileName, uint dwDesiredAccess, uint dwShareMode,+ IntPtr lpSecurityAttributes, uint dwCreationDisposition,+ uint dwFlagsAndAttributes, IntPtr hTemplateFile);++ [DllImport("kernel32.dll", SetLastError = true)]+ private static extern bool DeviceIoControl(+ SafeFileHandle hDevice, uint dwIoControlCode,+ IntPtr lpInBuffer, uint nInBufferSize,+ IntPtr lpOutBuffer, uint nOutBufferSize,+ out uint lpBytesReturned, IntPtr lpOverlapped);++ const uint FILE_SHARE_READ = 0x00000001;+ const uint FILE_SHARE_WRITE = 0x00000002;+ const uint OPEN_EXISTING = 3;+ const uint FILE_ATTRIBUTE_NORMAL = 0x80;+ const uint GENERIC_READ = 0x80000000;+ const uint IOCTL_STORAGE_QUERY_PROPERTY = 0x2D1400;++ enum STORAGE_PROPERTY_ID+ {+ StorageDeviceSeekPenaltyProperty = 7+ }++ enum STORAGE_QUERY_TYPE+ {+ PropertyStandardQuery = 0+ }++ [StructLayout(LayoutKind.Sequential)]+ struct STORAGE_PROPERTY_QUERY+ {+ public STORAGE_PROPERTY_ID PropertyId;+ public STORAGE_QUERY_TYPE QueryType;+ [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]+ public byte[] AdditionalParameters;+ }++ [StructLayout(LayoutKind.Sequential)]+ struct DEVICE_SEEK_PENALTY_DESCRIPTOR+ {+ public uint Version;+ public uint Size;+ [MarshalAs(UnmanagedType.U1)]+ public bool IncursSeekPenalty;+ }++ public static bool IsSSD(string driveLetter)+ {+ var path = @"\\.\" + driveLetter.TrimEnd('\\');+ var handle = CreateFile(path, GENERIC_READ,+ FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero,+ OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);++ if (handle.IsInvalid)+ return false;++ var query = new STORAGE_PROPERTY_QUERY+ {+ PropertyId = STORAGE_PROPERTY_ID.StorageDeviceSeekPenaltyProperty,+ QueryType = STORAGE_QUERY_TYPE.PropertyStandardQuery,+ AdditionalParameters = new byte[1]+ };++ var querySize = Marshal.SizeOf(query);+ var queryPtr = Marshal.AllocHGlobal(querySize);+ Marshal.StructureToPtr(query, queryPtr, false);++ var result = new DEVICE_SEEK_PENALTY_DESCRIPTOR();+ var resultSize = Marshal.SizeOf(result);+ var resultPtr = Marshal.AllocHGlobal(resultSize);++ var success = DeviceIoControl(handle, IOCTL_STORAGE_QUERY_PROPERTY,+ queryPtr, (uint)querySize,+ resultPtr, (uint)resultSize,+ out _, IntPtr.Zero);++ Marshal.FreeHGlobal(queryPtr);++ if (!success)+ {+ Marshal.FreeHGlobal(resultPtr);+ return false;+ }++ result = Marshal.PtrToStructure<DEVICE_SEEK_PENALTY_DESCRIPTOR>(resultPtr);+ Marshal.FreeHGlobal(resultPtr);++ return !result.IncursSeekPenalty; // false = HDD, true = SSD+ }+}++internal static class DiskProvider+{+++ public static DiskInfo Get()+ {+ try+ {+ var volumes = new List<DiskVolume>();+ var drives = DriveInfo.GetDrives().Where(d => d.IsReady);++ foreach (var drive in drives)+ {+ var totalBytes = drive.TotalSize;+ var freeBytes = drive.AvailableFreeSpace;+ var usedBytes = totalBytes - freeBytes;++ double toGb(long bytes) => Math.Round(bytes / Math.Pow(1024, 3), 2);++ volumes.Add(new DiskVolume(+ Name: drive.Name.TrimEnd('\\'),+ FileSystem: drive.DriveFormat,+ DriveType: drive.DriveType.ToString(),+ Label: drive.VolumeLabel,+ TotalSizeGB: toGb(totalBytes),+ FreeSpaceGB: toGb(freeBytes),+ UsedSpaceGB: toGb(usedBytes),+ UsedPercent: totalBytes > 0 ? Math.Round(usedBytes * 100.0 / totalBytes, 1) : 0,+ DriveTypeDescription: DiskHelper.IsSSD(drive.Name) ? "SSD" : "HDD"+ ));+ }++ return new DiskInfo(volumes);+ }+ catch+ {+ return DiskInfo.Unknown;+ }+ }+}+internal static class GpuProvider{public static IReadOnlyList<GpuInfo> GetAll()@@ -517,13 +675,13 @@ internal static class BiosProviderpublic static class SystemInfoService{- private static readonly SemaphoreSlim _semaphore = new(1, 1);+ private static readonly SemaphoreSlim Semaphore = new(1, 1);public static SystemSnapshot Snapshot { get; private set; } = SystemSnapshot.Unknown;public static async Task<SystemSnapshot> RefreshAsync(CancellationToken ct = default){- await _semaphore.WaitAsync(ct).ConfigureAwait(false);+ await Semaphore.WaitAsync(ct).ConfigureAwait(false);try{var tasks = new Task[]@@ -543,15 +701,16 @@ public static class SystemInfoServicevar bios = ((Task<BiosInfo>)tasks[3]).Result;var gpus = ((Task<IReadOnlyList<GpuInfo>>)tasks[4]).Result;var primaryGpu = GpuProvider.GetPrimary(gpus);+ var disk = DiskProvider.Get();- var snapshot = new SystemSnapshot(cpu, ram, os, bios, gpus, primaryGpu);+ var snapshot = new SystemSnapshot(cpu, ram, os, bios, gpus, primaryGpu, disk);Snapshot = snapshot;return snapshot;}finally{- _semaphore.Release();+ Semaphore.Release();}}@@ -567,6 +726,12 @@ public static class SystemInfoServicelog.LogDebug("RAM: {RamTotalGb:F1} GB (Used: {RamUsedPercent:F1}%)",Snapshot.Ram.TotalGB, Snapshot.Ram.UsedPercent);++ foreach (var volume in Snapshot.Disk.Volumes)+ {+ log.LogDebug("Disk: {VolumeName} ({VolumeDriveType}) [{VolumeTotalSizeGb:F1} GB] (Free: {VolumeFreeSpaceGb:F1} GB)",+ volume.Name, volume.DriveTypeDescription, volume.TotalSizeGB, volume.FreeSpaceGB);+ }}private static void LogGpuSummary(ILogger log)