[Refactor] Invert if to reduce nesting.

This commit is contained in:
2025-11-24 19:19:14 +00:00
parent dea49e235d
commit 7b487033b5
16 changed files with 126 additions and 163 deletions

View File

@@ -270,15 +270,14 @@ public class PluginRegister
var foundFilter = (IFilter)filter.GetType().GetConstructor(Type.EmptyTypes)?.Invoke([]); var foundFilter = (IFilter)filter.GetType().GetConstructor(Type.EmptyTypes)?.Invoke([]);
if(foundFilter?.Open(path) == ErrorNumber.NoError) if(foundFilter?.Open(path) != ErrorNumber.NoError) continue;
{
transaction.Finish();
return foundFilter; transaction.Finish();
}
return foundFilter;
} }
else
noFilter = filter; noFilter = filter;
} }
catch(IOException ex) catch(IOException ex)
{ {

View File

@@ -130,13 +130,12 @@ public partial class LzdStream : Stream
break; break;
} }
if(status == LZDStatus.DONE) if(status != LZDStatus.DONE) continue;
{
_eof = true;
Debug.WriteLine(">>> SET _eof=true (no more data and already flushed)");
break; _eof = true;
} Debug.WriteLine(">>> SET _eof=true (no more data and already flushed)");
break;
// if OK but no bytes, loop again // if OK but no bytes, loop again
} }

View File

@@ -1042,29 +1042,27 @@ partial class Dump
sectorStatus[b] = SectorStatus.Dumped; sectorStatus[b] = SectorStatus.Dumped;
if(inData && _paranoia) if(!inData || !_paranoia) continue;
{
// Check valid sector
CdChecksums.CheckCdSector(sector,
out bool? correctEccP,
out bool? correctEccQ,
out bool? correctEdc);
if(correctEdc != true || correctEccP != true || correctEccQ != true) // Check valid sector
{ CdChecksums.CheckCdSector(sector,
sectorStatus[b] = SectorStatus.Errored; out bool? correctEccP,
_resume.BadBlocks.Add(i + (ulong)b); out bool? correctEccQ,
out bool? correctEdc);
if(correctEdc != true) if(correctEdc == true && correctEccP == true && correctEccQ == true) continue;
UpdateStatus?.Invoke(string.Format(UI.Incorrect_EDC_in_sector_0, i + (ulong)b));
if(correctEccP != true) sectorStatus[b] = SectorStatus.Errored;
UpdateStatus?.Invoke(string.Format(UI.Incorrect_ECC_P_in_sector_0, i + (ulong)b)); _resume.BadBlocks.Add(i + (ulong)b);
if(correctEccQ != true) if(correctEdc != true)
UpdateStatus?.Invoke(string.Format(UI.Incorrect_ECC_Q_in_sector_0, i + (ulong)b)); UpdateStatus?.Invoke(string.Format(UI.Incorrect_EDC_in_sector_0, i + (ulong)b));
}
} if(correctEccP != true)
UpdateStatus?.Invoke(string.Format(UI.Incorrect_ECC_P_in_sector_0, i + (ulong)b));
if(correctEccQ != true)
UpdateStatus?.Invoke(string.Format(UI.Incorrect_ECC_Q_in_sector_0, i + (ulong)b));
} }
if(supportsLongSectors) if(supportsLongSectors)

View File

@@ -206,13 +206,12 @@ public sealed partial class MediaScan
ibgLog.Write(i, 0); ibgLog.Write(i, 0);
} }
if(accumulatedSpeedMs >= 100) if(accumulatedSpeedMs < 100) continue;
{
currentSpeed = accumulatedSpeedSectors * blockSize / (1048576 * (accumulatedSpeedMs / 1000.0)); currentSpeed = accumulatedSpeedSectors * blockSize / (1048576 * (accumulatedSpeedMs / 1000.0));
ScanSpeed?.Invoke(i, currentSpeed * 1024); ScanSpeed?.Invoke(i, currentSpeed * 1024);
accumulatedSpeedMs = 0; accumulatedSpeedMs = 0;
accumulatedSpeedSectors = 0; accumulatedSpeedSectors = 0;
}
} }
_speedStopwatch.Stop(); _speedStopwatch.Stop();
@@ -341,16 +340,13 @@ public sealed partial class MediaScan
ibgLog.Write(currentBlock, 0); ibgLog.Write(currentBlock, 0);
} }
if(accumulatedSpeedMs >= 100) if(accumulatedSpeedMs < 100) continue;
{
currentSpeed = accumulatedSpeedSectors *
blockSize /
(1048576 * (accumulatedSpeedMs / 1000.0));
ScanSpeed?.Invoke(currentBlock, currentSpeed * 1024); currentSpeed = accumulatedSpeedSectors * blockSize / (1048576 * (accumulatedSpeedMs / 1000.0));
accumulatedSpeedMs = 0;
accumulatedSpeedSectors = 0; ScanSpeed?.Invoke(currentBlock, currentSpeed * 1024);
} accumulatedSpeedMs = 0;
accumulatedSpeedSectors = 0;
} }
} }
} }

View File

@@ -508,13 +508,12 @@ public sealed partial class MediaScan
} }
} }
if(accumulatedSpeedMs >= 100) if(accumulatedSpeedMs < 100) continue;
{
currentSpeed = accumulatedSpeedSectors * blockSize / (1048576 * (accumulatedSpeedMs / 1000.0)); currentSpeed = accumulatedSpeedSectors * blockSize / (1048576 * (accumulatedSpeedMs / 1000.0));
ScanSpeed?.Invoke(i, currentSpeed * 1024); ScanSpeed?.Invoke(i, currentSpeed * 1024);
accumulatedSpeedMs = 0; accumulatedSpeedMs = 0;
accumulatedSpeedSectors = 0; accumulatedSpeedSectors = 0;
}
} }
_scanStopwatch.Stop(); _scanStopwatch.Stop();
@@ -624,13 +623,11 @@ public sealed partial class MediaScan
ibgLog.Write(i, 0); ibgLog.Write(i, 0);
} }
if(accumulatedSpeedMs >= 100) if(accumulatedSpeedMs < 100) continue;
{ currentSpeed = accumulatedSpeedSectors * blockSize / (1048576 * (accumulatedSpeedMs / 1000.0));
currentSpeed = accumulatedSpeedSectors * blockSize / (1048576 * (accumulatedSpeedMs / 1000.0)); ScanSpeed?.Invoke(i, currentSpeed * 1024);
ScanSpeed?.Invoke(i, currentSpeed * 1024); accumulatedSpeedMs = 0;
accumulatedSpeedMs = 0; accumulatedSpeedSectors = 0;
accumulatedSpeedSectors = 0;
}
} }
_scanStopwatch.Stop(); _scanStopwatch.Stop();

View File

@@ -343,13 +343,12 @@ public sealed partial class MediaScan
ibgLog.Write(i, 0); ibgLog.Write(i, 0);
} }
if(accumulatedSpeedMs >= 100) if(accumulatedSpeedMs < 100) continue;
{
currentSpeed = accumulatedSpeedSectors * blockSize / (1048576 * (accumulatedSpeedMs / 1000.0)); currentSpeed = accumulatedSpeedSectors * blockSize / (1048576 * (accumulatedSpeedMs / 1000.0));
ScanSpeed?.Invoke(i, currentSpeed * 1024); ScanSpeed?.Invoke(i, currentSpeed * 1024);
accumulatedSpeedMs = 0; accumulatedSpeedMs = 0;
accumulatedSpeedSectors = 0; accumulatedSpeedSectors = 0;
}
} }
_scanStopwatch.Stop(); _scanStopwatch.Stop();

View File

@@ -134,7 +134,7 @@ public static class DI
Reserved2 = DIResponse[3] Reserved2 = DIResponse[3]
}; };
int offset = 4; var offset = 4;
List<DiscInformationUnits> units = []; List<DiscInformationUnits> units = [];
while(true) while(true)
@@ -232,7 +232,7 @@ public static class DI
decoded.Units = new DiscInformationUnits[units.Count]; decoded.Units = new DiscInformationUnits[units.Count];
for(int i = 0; i < units.Count; i++) decoded.Units[i] = units[i]; for(var i = 0; i < units.Count; i++) decoded.Units[i] = units[i];
return decoded; return decoded;
} }
@@ -425,13 +425,11 @@ public static class DI
sb.AppendFormat(Localization.Disc_product_revision_number_0, unit.ProductRevisionNumber).AppendLine(); sb.AppendFormat(Localization.Disc_product_revision_number_0, unit.ProductRevisionNumber).AppendLine();
} }
if(unit.FormatDependentContents is not null) if(unit.FormatDependentContents is null) continue;
{
sb.AppendLine(Localization.Blu_ray_DI_Unit_format_dependent_contents_as_hex_follows);
sb.AppendLine(Markup.Escape(PrintHex.ByteArrayToHexArrayString(unit.FormatDependentContents, sb.AppendLine(Localization.Blu_ray_DI_Unit_format_dependent_contents_as_hex_follows);
color: true)));
} sb.AppendLine(Markup.Escape(PrintHex.ByteArrayToHexArrayString(unit.FormatDependentContents, color: true)));
} }
return sb.ToString(); return sb.ToString();

View File

@@ -106,11 +106,10 @@ public partial class BlockMap : UserControl
_sectorData = change.GetNewValue<ObservableCollection<(ulong startingSector, double duration)>>(); _sectorData = change.GetNewValue<ObservableCollection<(ulong startingSector, double duration)>>();
if(_sectorData != null) if(_sectorData == null) return;
{
_sectorData.CollectionChanged += OnSectorDataChanged; _sectorData.CollectionChanged += OnSectorDataChanged;
RedrawAll(); RedrawAll();
}
} }
else if(change.Property == ScanBlockSizeProperty) else if(change.Property == ScanBlockSizeProperty)
{ {
@@ -136,26 +135,25 @@ public partial class BlockMap : UserControl
void EnsureBitmap() void EnsureBitmap()
{ {
if(_blocksPerRow <= 0 || _rows <= 0) return; if(_blocksPerRow <= 0 || _rows <= 0) return;
int blockWithSpacing = BlockSize + BlockSpacing; int blockWithSpacing = BlockSize + BlockSpacing;
int width = _blocksPerRow * blockWithSpacing; int width = _blocksPerRow * blockWithSpacing;
int height = _rows * blockWithSpacing; int height = _rows * blockWithSpacing;
if(_bitmap == null || _bitmap.PixelSize.Width != width || _bitmap.PixelSize.Height != height) if(_bitmap != null && _bitmap.PixelSize.Width == width && _bitmap.PixelSize.Height == height) return;
{
_bitmap?.Dispose();
_bitmap = new WriteableBitmap(new PixelSize(width, height), _bitmap?.Dispose();
new Vector(96, 96),
PixelFormat.Bgra8888,
AlphaFormat.Premul);
if(_image != null) _bitmap = new WriteableBitmap(new PixelSize(width, height),
{ new Vector(96, 96),
_image.Source = _bitmap; PixelFormat.Bgra8888,
_image.Width = width; AlphaFormat.Premul);
_image.Height = height;
} if(_image == null) return;
}
_image.Source = _bitmap;
_image.Width = width;
_image.Height = height;
} }
private void RedrawAll() private void RedrawAll()

View File

@@ -183,22 +183,20 @@ public partial class DiscSpeedGraph : UserControl
{ {
double newZoom = Math.Min(_yZoomLevel + ZOOM_STEP, MAX_ZOOM); double newZoom = Math.Min(_yZoomLevel + ZOOM_STEP, MAX_ZOOM);
if(Math.Abs(newZoom - _yZoomLevel) > 0.001) if(!(Math.Abs(newZoom - _yZoomLevel) > 0.001)) return;
{
_yZoomLevel = newZoom; _yZoomLevel = newZoom;
RedrawAll(); RedrawAll();
}
} }
void ZoomOut() void ZoomOut()
{ {
double newZoom = Math.Max(_yZoomLevel - ZOOM_STEP, MIN_ZOOM); double newZoom = Math.Max(_yZoomLevel - ZOOM_STEP, MIN_ZOOM);
if(Math.Abs(newZoom - _yZoomLevel) > 0.001) if(!(Math.Abs(newZoom - _yZoomLevel) > 0.001)) return;
{
_yZoomLevel = newZoom; _yZoomLevel = newZoom;
RedrawAll(); RedrawAll();
}
} }
void OnPointerWheelChanged(object sender, PointerWheelEventArgs e) void OnPointerWheelChanged(object sender, PointerWheelEventArgs e)

View File

@@ -488,17 +488,14 @@ public partial class SpectreTextBlock : TextBlock
{ {
i++; i++;
if(i < text.Length && text[i] == ']') if(i >= text.Length || text[i] != ']') continue;
{
// Found [/], close the most recent tag
if(tagStack.Count > 0)
{
(int openStart, int openTagEnd, string tag) = tagStack.Pop();
int closeTagEnd = i + 1; // After the ']' of [/]
result.Add(new MarkupTag(openStart, closeTagEnd, tag, openTagEnd, tagStart));
}
i++; // Found [/], close the most recent tag
if(tagStack.Count > 0)
{
(int openStart, int openTagEnd, string tag) = tagStack.Pop();
int closeTagEnd = i + 1; // After the ']' of [/]
result.Add(new MarkupTag(openStart, closeTagEnd, tag, openTagEnd, tagStart));
} }
} }
else else
@@ -516,12 +513,10 @@ public partial class SpectreTextBlock : TextBlock
int openTagEnd = i + 1; // After the ']' int openTagEnd = i + 1; // After the ']'
tagStack.Push((tagStart, openTagEnd, tagName)); tagStack.Push((tagStart, openTagEnd, tagName));
} }
i++;
} }
} }
else
i++; i++;
} }
return result; return result;

View File

@@ -52,10 +52,9 @@ public partial class DeviceView : Window
{ {
base.OnDataContextChanged(e); base.OnDataContextChanged(e);
if(DataContext is DeviceViewModel vm) if(DataContext is not DeviceViewModel vm) return;
{
Closed += (_, _) => vm?.Closed(); Closed += (_, _) => vm?.Closed();
vm?.LoadData(); vm?.LoadData();
}
} }
} }

View File

@@ -485,17 +485,14 @@ public partial class SpectreTextBlock : TextBlock
{ {
i++; i++;
if(i < text.Length && text[i] == ']') if(i >= text.Length || text[i] != ']') continue;
{
// Found [/], close the most recent tag
if(tagStack.Count > 0)
{
(int openStart, int openTagEnd, string tag) = tagStack.Pop();
int closeTagEnd = i + 1; // After the ']' of [/]
result.Add(new MarkupTag(openStart, closeTagEnd, tag, openTagEnd, tagStart));
}
i++; // Found [/], close the most recent tag
if(tagStack.Count > 0)
{
(int openStart, int openTagEnd, string tag) = tagStack.Pop();
int closeTagEnd = i + 1; // After the ']' of [/]
result.Add(new MarkupTag(openStart, closeTagEnd, tag, openTagEnd, tagStart));
} }
} }
else else
@@ -513,12 +510,10 @@ public partial class SpectreTextBlock : TextBlock
int openTagEnd = i + 1; // After the ']' int openTagEnd = i + 1; // After the ']'
tagStack.Push((tagStart, openTagEnd, tagName)); tagStack.Push((tagStart, openTagEnd, tagName));
} }
i++;
} }
} }
else
i++; i++;
} }
return result; return result;

View File

@@ -40,14 +40,11 @@ public partial class FileView : UserControl
private void ListBox_OnKeyDown(object sender, KeyEventArgs e) private void ListBox_OnKeyDown(object sender, KeyEventArgs e)
{ {
if(e.Key == Key.Enter) if(e.Key != Key.Enter) return;
{ if(DataContext is not FileViewViewModel vm || !vm.OpenSelectedFileCommand.CanExecute(null)) return;
if(DataContext is FileViewViewModel vm && vm.OpenSelectedFileCommand.CanExecute(null))
{ vm.OpenSelectedFileCommand.Execute(null);
vm.OpenSelectedFileCommand.Execute(null); e.Handled = true;
e.Handled = true;
}
}
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@@ -390,11 +390,10 @@ sealed class StatisticsCommand : AsyncCommand<StatisticsCommand.Settings>
AaruLogging.WriteLine(); AaruLogging.WriteLine();
} }
if(!thereAreStats) if(thereAreStats) return (int)ErrorNumber.NoError;
{
AaruLogging.WriteLine(UI.There_are_no_statistics); AaruLogging.WriteLine(UI.There_are_no_statistics);
AaruLogging.Information(UI.There_are_no_statistics); AaruLogging.Information(UI.There_are_no_statistics);
}
return (int)ErrorNumber.NoError; return (int)ErrorNumber.NoError;
} }

View File

@@ -414,12 +414,11 @@ sealed class LsCommand : Command<LsCommand.Settings>
byte[] xattrBuf = []; byte[] xattrBuf = [];
error = fs.GetXattr(path + "/" + entry.Key, xattr, ref xattrBuf); error = fs.GetXattr(path + "/" + entry.Key, xattr, ref xattrBuf);
if(error == ErrorNumber.NoError) if(error != ErrorNumber.NoError) continue;
{
table.AddRow("", $"[lime]{xattrBuf.Length}[/]", "", $"[fuchsia]{Markup.Escape(xattr)}[/]");
AaruLogging.Information($"{xattrBuf.Length} {xattr}"); table.AddRow("", $"[lime]{xattrBuf.Length}[/]", "", $"[fuchsia]{Markup.Escape(xattr)}[/]");
}
AaruLogging.Information($"{xattrBuf.Length} {xattr}");
} }
} }
else else

View File

@@ -1397,14 +1397,11 @@ sealed class ConvertImageCommand : Command<ConvertImageCommand.Settings>
return (false, 0, 0, 0); return (false, 0, 0, 0);
} }
if(!uint.TryParse(geometryPieces[2], out uint sectors) || sectors == 0) if(uint.TryParse(geometryPieces[2], out uint sectors) && sectors != 0) return (true, cylinders, heads, sectors);
{
AaruLogging.Error(UI.Invalid_sectors_per_track_specified);
return (false, 0, 0, 0); AaruLogging.Error(UI.Invalid_sectors_per_track_specified);
}
return (true, cylinders, heads, sectors); return (false, 0, 0, 0);
} }
private (bool success, Metadata sidecar, Resume resume) LoadMetadata( private (bool success, Metadata sidecar, Resume resume) LoadMetadata(