[Refactor] General reformat and clean-up.

This commit is contained in:
2025-11-24 20:12:10 +00:00
parent 8331fba1e4
commit a58f2e60e5
415 changed files with 4920 additions and 4829 deletions

View File

@@ -32,7 +32,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection"/>
<PackageReference Include="Sentry" />
<PackageReference Include="Sentry"/>
<PackageReference Include="System.Management"/>
</ItemGroup>
<ItemGroup>

View File

@@ -215,9 +215,8 @@ public partial class Device : IDisposable
if(string.IsNullOrEmpty(dev.Serial))
dev.Serial = dev.UsbSerialString;
else
{
foreach(char c in dev.Serial.Where(static c => !char.IsControl(c))) dev.Serial = $"{dev.Serial}{c:X2}";
}
foreach(char c in dev.Serial.Where(static c => !char.IsControl(c)))
dev.Serial = $"{dev.Serial}{c:X2}";
}
if(dev.IsFireWire)
@@ -229,9 +228,8 @@ public partial class Device : IDisposable
if(string.IsNullOrEmpty(dev.Serial))
dev.Serial = $"{dev.FirewireGuid:X16}";
else
{
foreach(char c in dev.Serial.Where(static c => !char.IsControl(c))) dev.Serial = $"{dev.Serial}{c:X2}";
}
foreach(char c in dev.Serial.Where(static c => !char.IsControl(c)))
dev.Serial = $"{dev.Serial}{c:X2}";
}
// Some optical drives are not getting the correct serial, and IDENTIFY PACKET DEVICE is blocked without

View File

@@ -101,7 +101,7 @@ public partial class Device
public bool AdaptecSetErrorThreshold(byte threshold, out ReadOnlySpan<byte> senseBuffer, bool drive1, uint timeout,
out double duration)
{
byte[] buffer = new byte[1];
var buffer = new byte[1];
buffer[0] = threshold;
Span<byte> cdb = CdbBuffer[..6];
cdb.Clear();
@@ -167,7 +167,7 @@ public partial class Device
/// <param name="duration">Duration.</param>
public bool AdaptecWriteBuffer(byte[] buffer, out ReadOnlySpan<byte> senseBuffer, uint timeout, out double duration)
{
byte[] oneKBuffer = new byte[1024];
var oneKBuffer = new byte[1024];
Array.Copy(buffer, 0, oneKBuffer, 0, buffer.Length < 1024 ? buffer.Length : 1024);
Span<byte> cdb = CdbBuffer[..6];

View File

@@ -52,11 +52,11 @@ public partial class Device
string firstHalf, string secondHalf, uint timeout, out double duration)
{
byte[] tmp;
byte[] firstHalfBytes = new byte[8];
byte[] secondHalfBytes = new byte[8];
byte[] buffer = new byte[17];
bool displayLen = false;
bool halfMsg = false;
var firstHalfBytes = new byte[8];
var secondHalfBytes = new byte[8];
var buffer = new byte[17];
var displayLen = false;
var halfMsg = false;
Span<byte> cdb = CdbBuffer[..10];
senseBuffer = SenseBuffer;
cdb.Clear();

View File

@@ -106,12 +106,12 @@ public partial class Device
static bool CheckSectorNumber(IReadOnlyList<byte> buffer, uint firstLba, uint transferLength, uint layerbreak,
bool otp)
{
for(int i = 0; i < transferLength; i++)
for(var i = 0; i < transferLength; i++)
{
byte layer = (byte)(buffer[0 + 2064 * i] & 0x1);
var layer = (byte)(buffer[0 + 2064 * i] & 0x1);
byte[] sectorBuffer = [0x0, buffer[1 + 2064 * i], buffer[2 + 2064 * i], buffer[3 + 2064 * i]];
uint sectorNumber = BigEndianBitConverter.ToUInt32(sectorBuffer, 0);
var sectorNumber = BigEndianBitConverter.ToUInt32(sectorBuffer, 0);
if(otp)

View File

@@ -128,7 +128,7 @@ public partial class Device
senseBuffer = SenseBuffer;
Span<byte> cdb = CdbBuffer[..10];
cdb.Clear();
byte[] buffer = new byte[26];
var buffer = new byte[26];
features = 0;
cdb[0] = (byte)ScsiCommands.KreonCommand;
@@ -146,9 +146,9 @@ public partial class Device
if(buffer[0] != 0xA5 || buffer[1] != 0x5A || buffer[2] != 0x5A || buffer[3] != 0xA5) return true;
for(int i = 4; i < 26; i += 2)
for(var i = 4; i < 26; i += 2)
{
ushort feature = BitConverter.ToUInt16(buffer, i);
var feature = BitConverter.ToUInt16(buffer, i);
if(feature == 0x0000) break;

View File

@@ -281,11 +281,11 @@ public partial class Device
{
// TODO: Save ECC instead of just throwing it away
byte[] deinterleaved = new byte[2064 * transferLength];
var deinterleaved = new byte[2064 * transferLength];
for(int j = 0; j < transferLength; j++)
for(var j = 0; j < transferLength; j++)
{
for(int i = 0; i < 12; i++) Array.Copy(buffer, j * 2384 + i * 182, deinterleaved, j * 2064 + i * 172, 172);
for(var i = 0; i < 12; i++) Array.Copy(buffer, j * 2384 + i * 182, deinterleaved, j * 2064 + i * 172, 172);
}
return deinterleaved;

View File

@@ -98,7 +98,7 @@ public partial class Device
if(sense) return true;
ushort confLength = (ushort)((buffer[2] << 8) + buffer[3] + 4);
var confLength = (ushort)((buffer[2] << 8) + buffer[3] + 4);
buffer = new byte[confLength];
cdb[7] = (byte)((buffer.Length & 0xFF00) >> 8);
cdb[8] = (byte)(buffer.Length & 0xFF);
@@ -158,7 +158,7 @@ public partial class Device
if(sense) return true;
ushort strctLength = (ushort)((buffer[0] << 8) + buffer[1] + 2);
var strctLength = (ushort)((buffer[0] << 8) + buffer[1] + 2);
// WORKAROUND: Some drives return incorrect length information. As these structures are fixed length just apply known length.
if(mediaType == MmcDiscStructureMediaType.Bd)
@@ -312,7 +312,7 @@ public partial class Device
Error = LastError != 0;
uint strctLength = (uint)((tmpBuffer[0] << 8) + tmpBuffer[1] + 2);
var strctLength = (uint)((tmpBuffer[0] << 8) + tmpBuffer[1] + 2);
buffer = new byte[strctLength];
if(buffer.Length <= tmpBuffer.Length)
@@ -380,7 +380,7 @@ public partial class Device
senseBuffer = SenseBuffer;
Span<byte> cdb = CdbBuffer[..10];
cdb.Clear();
byte[] tmpBuffer = new byte[804];
var tmpBuffer = new byte[804];
cdb[0] = (byte)ScsiCommands.ReadDiscInformation;
cdb[1] = (byte)dataType;
@@ -391,7 +391,7 @@ public partial class Device
Error = LastError != 0;
uint strctLength = (uint)((tmpBuffer[0] << 8) + tmpBuffer[1] + 2);
var strctLength = (uint)((tmpBuffer[0] << 8) + tmpBuffer[1] + 2);
if(strctLength > tmpBuffer.Length) strctLength = (uint)tmpBuffer.Length;
@@ -536,7 +536,7 @@ public partial class Device
cdb[10] = (byte)subchannel;
uint transferLength = (uint)((cdb[6] - cdb[3]) * 60 * 75 + (cdb[7] - cdb[4]) * 75 + (cdb[8] - cdb[5]));
var transferLength = (uint)((cdb[6] - cdb[3]) * 60 * 75 + (cdb[7] - cdb[4]) * 75 + (cdb[8] - cdb[5]));
buffer = new byte[blockSize * transferLength];

View File

@@ -101,7 +101,7 @@ public partial class Device
cdb[9] = (byte)(endMsf & 0xFF);
cdb[10] = (byte)subchannel;
uint transferLength = (uint)((cdb[7] - cdb[3]) * 60 * 75 + (cdb[8] - cdb[4]) * 75 + (cdb[9] - cdb[5]));
var transferLength = (uint)((cdb[7] - cdb[3]) * 60 * 75 + (cdb[8] - cdb[4]) * 75 + (cdb[9] - cdb[5]));
buffer = new byte[blockSize * transferLength];
LastError = SendScsiCommand(cdb, ref buffer, timeout, ScsiDirection.In, out duration, out bool sense);

View File

@@ -214,7 +214,7 @@ public partial class Device
public bool PlextorGetSpeeds(out ReadOnlySpan<byte> senseBuffer, out ushort selected, out ushort max,
out ushort last, uint timeout, out double duration)
{
byte[] buf = new byte[10];
var buf = new byte[10];
senseBuffer = SenseBuffer;
Span<byte> cdb = CdbBuffer[..12];
cdb.Clear();
@@ -251,7 +251,7 @@ public partial class Device
public bool PlextorGetPoweRec(out ReadOnlySpan<byte> senseBuffer, out bool enabled, out ushort speed, uint timeout,
out double duration)
{
byte[] buf = new byte[8];
var buf = new byte[8];
senseBuffer = SenseBuffer;
Span<byte> cdb = CdbBuffer[..12];
cdb.Clear();

View File

@@ -80,7 +80,7 @@ public partial class Device
if(sense) return true;
uint attrLen = (uint)((buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3] + 4);
var attrLen = (uint)((buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3] + 4);
buffer = new byte[attrLen];
cdb[10] = (byte)((buffer.Length & 0xFF000000) >> 24);
cdb[11] = (byte)((buffer.Length & 0xFF0000) >> 16);

View File

@@ -93,7 +93,7 @@ public partial class Device
if(sense) return true;
byte pagesLength = (byte)(buffer[4] + 5);
var pagesLength = (byte)(buffer[4] + 5);
cdb[0] = (byte)ScsiCommands.Inquiry;
cdb[1] = 0;
@@ -177,7 +177,7 @@ public partial class Device
// This is because INQ was returned instead of EVPD
if(buffer[1] != page) return true;
byte pagesLength = (byte)(buffer[3] + 4);
var pagesLength = (byte)(buffer[3] + 4);
cdb[0] = (byte)ScsiCommands.Inquiry;
cdb[1] = 1;
@@ -281,7 +281,7 @@ public partial class Device
if(sense) return true;
byte modeLength = (byte)(buffer[0] + 1);
var modeLength = (byte)(buffer[0] + 1);
if(modeLength % 2 != 0) modeLength++;
buffer = new byte[modeLength];
@@ -363,7 +363,7 @@ public partial class Device
if(sense) return true;
ushort modeLength = (ushort)((buffer[0] << 8) + buffer[1] + 2);
var modeLength = (ushort)((buffer[0] << 8) + buffer[1] + 2);
if(modeLength % 2 != 0) modeLength++;
buffer = new byte[modeLength];
@@ -568,7 +568,7 @@ public partial class Device
if(sense) return true;
uint strctLength = (uint)((buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3] + 4);
var strctLength = (uint)((buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3] + 4);
buffer = new byte[strctLength];
cdb[6] = (byte)((buffer.Length & 0xFF000000) >> 24);
cdb[7] = (byte)((buffer.Length & 0xFF0000) >> 16);

View File

@@ -908,7 +908,7 @@ public partial class Device
if(sense) return true;
ushort availableLength = (ushort)((buffer[0] << 8) + buffer[1] + 2);
var availableLength = (ushort)((buffer[0] << 8) + buffer[1] + 2);
buffer = new byte[availableLength];
cdb[7] = (byte)((buffer.Length & 0xFF00) >> 8);
cdb[8] = (byte)(buffer.Length & 0xFF);

View File

@@ -193,7 +193,7 @@ partial class Device
if(buffer == null) return -1;
byte[] cdb = new byte[16];
var cdb = new byte[16];
cdb[0] = (byte)ScsiCommands.AtaPassThrough16;
cdb[1] = (byte)((byte)protocol << 1 & 0x1E);
@@ -257,7 +257,7 @@ partial class Device
if(buffer == null) return -1;
byte[] cdb = new byte[16];
var cdb = new byte[16];
cdb[0] = (byte)ScsiCommands.AtaPassThrough16;
cdb[1] = (byte)((byte)protocol << 1 & 0x1E);
@@ -321,7 +321,7 @@ partial class Device
if(buffer == null) return -1;
byte[] cdb = new byte[16];
var cdb = new byte[16];
cdb[0] = (byte)ScsiCommands.AtaPassThrough16;
cdb[1] = (byte)((byte)protocol << 1 & 0x1E);
cdb[1] |= 0x01;
@@ -503,17 +503,17 @@ partial class Device
sense = false;
// Create array for buffers
IntPtr[] bufferPointers = new nint[commands.Length];
var bufferPointers = new nint[commands.Length];
// Allocate memory for the array for commands
byte[] ioMultiCmd = new byte[sizeof(ulong) + Marshal.SizeOf<MmcIocCmd>() * commands.Length];
var ioMultiCmd = new byte[sizeof(ulong) + Marshal.SizeOf<MmcIocCmd>() * commands.Length];
// First value of array is uint64 with count of commands
Array.Copy(BitConverter.GetBytes((ulong)commands.Length), 0, ioMultiCmd, 0, sizeof(ulong));
int off = sizeof(ulong);
for(int i = 0; i < commands.Length; i++)
for(var i = 0; i < commands.Length; i++)
{
// Create command
var ioCmd = new MmcIocCmd();
@@ -573,9 +573,9 @@ partial class Device
Marshal.Copy(ioMultiCmdPtr, ioMultiCmd, 0, ioMultiCmd.Length);
// TODO: Use real pointers this is too slow
for(int i = 0; i < commands.Length; i++)
for(var i = 0; i < commands.Length; i++)
{
byte[] tmp = new byte[Marshal.SizeOf<MmcIocCmd>()];
var tmp = new byte[Marshal.SizeOf<MmcIocCmd>()];
// Copy command to managed space
Array.Copy(ioMultiCmd, off, tmp, 0, tmp.Length);
@@ -675,7 +675,7 @@ partial class Device
resultSize = result;
}
byte[] resultString = new byte[resultSize];
var resultString = new byte[resultSize];
Marshal.Copy(buf, resultString, 0, resultSize);
Marshal.FreeHGlobal(buf);

View File

@@ -248,8 +248,8 @@ partial class Device : Devices.Device, IDisposable
var usbFs = new FileStream(resolvedLink + "/descriptors", FileMode.Open, FileAccess.Read);
byte[] usbBuf = new byte[65536];
int usbCount = usbFs.EnsureRead(usbBuf, 0, 65536);
var usbBuf = new byte[65536];
int usbCount = usbFs.EnsureRead(usbBuf, 0, 65536);
dev.UsbDescriptors = new byte[usbCount];
Array.Copy(usbBuf, 0, dev.UsbDescriptors, 0, usbCount);
usbFs.Close();
@@ -417,8 +417,8 @@ partial class Device : Devices.Device, IDisposable
var cisFs = new FileStream(possibleDir + "/cis", FileMode.Open, FileAccess.Read);
byte[] cisBuf = new byte[65536];
int cisCount = cisFs.EnsureRead(cisBuf, 0, 65536);
var cisBuf = new byte[65536];
int cisCount = cisFs.EnsureRead(cisBuf, 0, 65536);
dev.Cis = new byte[cisCount];
Array.Copy(cisBuf, 0, dev.Cis, 0, cisCount);
cisFs.Close();

View File

@@ -194,7 +194,7 @@ public partial class Device
if(_remote.ServerProtocolVersion >= 2)
return _remote.SendMultipleMmcCommands(commands, out duration, out sense, timeout);
int error = 0;
var error = 0;
duration = 0;
sense = false;

View File

@@ -87,7 +87,7 @@ partial class Device
if(direction != ScsiDirection.In) buffer.AsSpan().CopyTo(new Span<byte>((void*)_nativeBuffer, buffer.Length));
uint k = 0;
int error = 0;
var error = 0;
var cmdStopwatch = new Stopwatch();
cmdStopwatch.Start();
@@ -172,7 +172,7 @@ partial class Device
aptd.AtaFlags |= AtaFlags.DrdyRequired;
uint k = 0;
int error = 0;
var error = 0;
Marshal.Copy(buffer, 0, aptd.DataBuffer, buffer.Length);
@@ -267,7 +267,7 @@ partial class Device
aptd.AtaFlags |= AtaFlags.DrdyRequired;
uint k = 0;
int error = 0;
var error = 0;
Marshal.Copy(buffer, 0, aptd.DataBuffer, buffer.Length);
@@ -371,7 +371,7 @@ partial class Device
aptd.AtaFlags |= AtaFlags.DrdyRequired;
uint k = 0;
int error = 0;
var error = 0;
Marshal.Copy(buffer, 0, aptd.DataBuffer, buffer.Length);
@@ -534,18 +534,17 @@ partial class Device
if(flags.HasFlag(MmcFlags.ResponseR6)) commandDescriptor.responseType = SdResponseType.R6;
byte[] commandB =
new byte[commandData.size + commandData.protocolArgumentSize + commandData.deviceDataBufferSize];
var commandB = new byte[commandData.size + commandData.protocolArgumentSize + commandData.deviceDataBufferSize];
Array.Copy(buffer, 0, commandB, commandData.size + commandData.protocolArgumentSize, buffer.Length);
IntPtr hBuf = Marshal.AllocHGlobal(commandB.Length);
Marshal.StructureToPtr(commandData, hBuf, true);
IntPtr descriptorOffset = IntPtr.Add(hBuf, commandData.size);
var descriptorOffset = IntPtr.Add(hBuf, commandData.size);
Marshal.StructureToPtr(commandDescriptor, descriptorOffset, true);
Marshal.Copy(hBuf, commandB, 0, commandB.Length);
Marshal.FreeHGlobal(hBuf);
int error = 0;
var error = 0;
cmdStopwatch.Restart();
sense = !Extern.DeviceIoControl(_fileHandle,
@@ -577,7 +576,7 @@ partial class Device
// We need a timeout
if(timeout == 0) timeout = Timeout > 0 ? Timeout : 15;
int error = 0;
var error = 0;
duration = 0;
sense = false;

View File

@@ -128,10 +128,10 @@ partial class Device : Devices.Device, IDisposable
};
IntPtr descriptorPtr = Marshal.AllocHGlobal(1000);
byte[] descriptorB = new byte[1000];
var descriptorB = new byte[1000];
uint returned = 0;
int error = 0;
var error = 0;
bool hasError = !Extern.DeviceIoControlStorageQuery(dev._fileHandle,
WindowsIoctl.IoctlStorageQueryProperty,
@@ -229,7 +229,7 @@ partial class Device : Devices.Device, IDisposable
if(IsSdhci(dev._fileHandle))
{
byte[] sdBuffer = new byte[16];
var sdBuffer = new byte[16];
dev.LastError = dev.SendMmcCommand(MmcCommands.SendCsd,
false,