From 9b0f6490519b8dbc15a6e708faad5bd2eafcea6a Mon Sep 17 00:00:00 2001 From: Pdawg11239 <83825746+Pdawg-bytes@users.noreply.github.com> Date: Mon, 17 Mar 2025 14:26:41 -0400 Subject: [PATCH] Clean up formatter --- BadBuilder.Formatter/DiskFormatter.cs | 32 +++++++++++-------- BadBuilder.Formatter/Utilities.cs | 7 ++-- .../ConsoleExperiences/DiskExperience.cs | 3 +- .../ConsoleExperiences/ExtractExperience.cs | 3 +- BadBuilder/Helpers/DiskHelper.cs | 10 +++++- BadBuilder/Program.cs | 2 -- 6 files changed, 32 insertions(+), 25 deletions(-) diff --git a/BadBuilder.Formatter/DiskFormatter.cs b/BadBuilder.Formatter/DiskFormatter.cs index 1cf5965..23ecddc 100644 --- a/BadBuilder.Formatter/DiskFormatter.cs +++ b/BadBuilder.Formatter/DiskFormatter.cs @@ -22,16 +22,16 @@ namespace BadBuilder.Formatter if (driveHandle.IsInvalid) return Error("Unable to open device. GetLastError: " + Marshal.GetLastWin32Error()); if (!EnableExtendedDASDIO(driveHandle) || !LockDevice(driveHandle)) - return Error("Failed to initialize device access."); + return Error($"Failed to initialize device access. GetLastError: {Marshal.GetLastWin32Error()}"); DISK_GEOMETRY diskGeometry; if (!TryGetDiskGeometry(driveHandle, out diskGeometry)) - return Error("Failed to get drive geometry."); + return Error($"Failed to get disk geometry. GetLastError: {Marshal.GetLastWin32Error()}"); - PARTITION_INFORMATION partitionInfo = new(); + PARTITION_INFORMATION partitionInfo; bool isGPT = false; if (!TryGetPartitionInfo(driveHandle, ref diskGeometry, out partitionInfo, out isGPT)) - return Error("Failed to get partition information."); + return Error($"Failed to get partition information. GetLastError: {Marshal.GetLastWin32Error()}"); uint totalSectors = (uint)(partitionInfo.PartitionLength / diskGeometry.BytesPerSector); if (!IsValidFAT32Size(totalSectors)) @@ -175,18 +175,25 @@ namespace BadBuilder.Formatter return sector; } - private static unsafe string FormatVolumeData(SafeFileHandle driveHandle, DISK_GEOMETRY diskGeometry, FAT32BootSector bootSector, FAT32FsInfoSector fsInfo, uint[] firstFATSector, bool isGPT, PARTITION_INFORMATION partitionInfo) + private static unsafe string FormatVolumeData( + SafeFileHandle driveHandle, + DISK_GEOMETRY diskGeometry, + FAT32BootSector bootSector, + FAT32FsInfoSector fsInfo, + uint[] firstFATSector, + bool isGPT, + PARTITION_INFORMATION partitionInfo) { uint bytesPerSector = diskGeometry.BytesPerSector; - uint totalSectors = (uint)(partitionInfo.PartitionLength / diskGeometry.BytesPerSector); - uint userAreaSize = totalSectors - bootSector.ReservedSectorCount - (bootSector.NumberOfFATs * bootSector.SectorsPerFAT); + uint totalSectors = (uint)(partitionInfo.PartitionLength / bytesPerSector); uint systemAreaSize = bootSector.ReservedSectorCount + (bootSector.NumberOfFATs * bootSector.SectorsPerFAT) + bootSector.SectorsPerCluster; + uint userAreaSize = totalSectors - systemAreaSize; uint clusterCount = userAreaSize / bootSector.SectorsPerCluster; if (clusterCount < 65536 || clusterCount > 0x0FFFFFFF) - return Error("The drives cluster count is out of range (65536 < clusterCount < 0x0FFFFFFF)"); + return Error("The drive's cluster count is out of range (65536 < clusterCount < 0x0FFFFFFF)"); - fsInfo.FreeClusterCount = (userAreaSize / bootSector.SectorsPerCluster) - 1; + fsInfo.FreeClusterCount = clusterCount - 1; ZeroOutSectors(driveHandle, 0, systemAreaSize, bytesPerSector); @@ -199,16 +206,13 @@ namespace BadBuilder.Formatter for (int i = 0; i < bootSector.NumberOfFATs; i++) { - uint sectorStart = bootSector.ReservedSectorCount + ((uint)i * bootSector.SectorsPerFAT); + uint sectorStart = (uint)(bootSector.ReservedSectorCount + (i * bootSector.SectorsPerFAT)); WriteSector(driveHandle, sectorStart, 1, bytesPerSector, UintArrayToBytes(firstFATSector)); } if (!isGPT) { - SET_PARTITION_INFORMATION setPartInfo = new SET_PARTITION_INFORMATION - { - PartitionType = 0x0C - }; + SET_PARTITION_INFORMATION setPartInfo = new() { PartitionType = 0x0C }; if (!DeviceIoControl(driveHandle, IOCTL_DISK_SET_PARTITION_INFO, &setPartInfo, (uint)sizeof(SET_PARTITION_INFORMATION), null, 0, null, null)) return Error($"Failed to set the drive partition information. GetLastError: {Marshal.GetLastWin32Error()}"); diff --git a/BadBuilder.Formatter/Utilities.cs b/BadBuilder.Formatter/Utilities.cs index 23fe006..dea4ba4 100644 --- a/BadBuilder.Formatter/Utilities.cs +++ b/BadBuilder.Formatter/Utilities.cs @@ -71,13 +71,11 @@ namespace BadBuilder.Formatter internal static unsafe void WriteSector(SafeHandle hDevice, uint sector, uint numberOfSectors, uint bytesPerSector, byte[] data) { - uint bytesWritten; - SeekTo(hDevice, sector, bytesPerSector); fixed (byte* pData = &data[0]) { - if (!WriteFile(new HANDLE(hDevice.DangerousGetHandle()), pData, numberOfSectors * bytesPerSector, &bytesWritten, null)) + if (!WriteFile(new HANDLE(hDevice.DangerousGetHandle()), pData, numberOfSectors * bytesPerSector, null, null)) ExitWithError($"Unable to write sectors to FAT32 device, exiting. GetLastError: {Marshal.GetLastWin32Error()}"); } } @@ -86,7 +84,6 @@ namespace BadBuilder.Formatter { const uint burstSize = 128; uint writeSize; - uint bytesWritten; byte* pZeroSector = (byte*)VirtualAlloc(null, bytesPerSector * burstSize, VIRTUAL_ALLOCATION_TYPE.MEM_COMMIT | VIRTUAL_ALLOCATION_TYPE.MEM_RESERVE, PAGE_PROTECTION_FLAGS.PAGE_READWRITE); @@ -98,7 +95,7 @@ namespace BadBuilder.Formatter { writeSize = (numberOfSectors > burstSize) ? burstSize : numberOfSectors; - if (!WriteFile(new HANDLE(hDevice.DangerousGetHandle()), pZeroSector, writeSize * bytesPerSector, &bytesWritten, null)) + if (!WriteFile(new HANDLE(hDevice.DangerousGetHandle()), pZeroSector, writeSize * bytesPerSector, null, null)) ExitWithError($"Unable to write sectors to FAT32 device, exiting. GetLastError: {Marshal.GetLastWin32Error()}"); numberOfSectors -= writeSize; diff --git a/BadBuilder/ConsoleExperiences/DiskExperience.cs b/BadBuilder/ConsoleExperiences/DiskExperience.cs index 66b0ee4..3d5c98e 100644 --- a/BadBuilder/ConsoleExperiences/DiskExperience.cs +++ b/BadBuilder/ConsoleExperiences/DiskExperience.cs @@ -39,10 +39,11 @@ namespace BadBuilder bool ret = true; string output = string.Empty; - AnsiConsole.Status().SpinnerStyle(LightOrangeStyle).Start($"[#76B900]Formatting disk[/] {selectedDisk}", ctx => + AnsiConsole.Status().SpinnerStyle(LightOrangeStyle).Start($"[#76B900]Formatting disk[/] {selectedDisk}", async ctx => { if (diskIndex == -1) return; + ClearConsole(); output = DiskHelper.FormatDisk(disks[diskIndex]); if (output != string.Empty) ret = false; }); diff --git a/BadBuilder/ConsoleExperiences/ExtractExperience.cs b/BadBuilder/ConsoleExperiences/ExtractExperience.cs index 2671e7e..b06d441 100644 --- a/BadBuilder/ConsoleExperiences/ExtractExperience.cs +++ b/BadBuilder/ConsoleExperiences/ExtractExperience.cs @@ -13,8 +13,7 @@ namespace BadBuilder .Columns( new TaskDescriptionColumn(), new ProgressBarColumn().FinishedStyle(GreenStyle).CompletedStyle(LightOrangeStyle), - new PercentageColumn().CompletedStyle(GreenStyle), - new RemainingTimeColumn().Style(GrayStyle) + new PercentageColumn().CompletedStyle(GreenStyle) ) .StartAsync(async ctx => { diff --git a/BadBuilder/Helpers/DiskHelper.cs b/BadBuilder/Helpers/DiskHelper.cs index 9403dfa..9631654 100644 --- a/BadBuilder/Helpers/DiskHelper.cs +++ b/BadBuilder/Helpers/DiskHelper.cs @@ -1,5 +1,7 @@ using BadBuilder.Models; using BadBuilder.Formatter; +using System.Runtime.InteropServices; +using Spectre.Console; namespace BadBuilder.Helpers { @@ -27,6 +29,12 @@ namespace BadBuilder.Helpers return disks; } - internal static string FormatDisk(DiskInfo disk) => DiskFormatter.FormatVolume(disk.DriveLetter.ToCharArray()[0]); + internal static string FormatDisk(DiskInfo disk) + { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + return "\u001b[38;2;255;114;0m[-]\u001b[0m Formatting is currently only supported on Windows. Please format your drive manually and try again."; + + return DiskFormatter.FormatVolume(disk.DriveLetter[0]); + } } } \ No newline at end of file diff --git a/BadBuilder/Program.cs b/BadBuilder/Program.cs index 6c2cd7c..7ae030d 100644 --- a/BadBuilder/Program.cs +++ b/BadBuilder/Program.cs @@ -48,8 +48,6 @@ namespace BadBuilder } } - ClearConsole(); - List downloadedFiles = DownloadRequiredFiles().Result; ExtractFiles(downloadedFiles).Wait();