[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([]);
if(foundFilter?.Open(path) == ErrorNumber.NoError)
{
transaction.Finish();
if(foundFilter?.Open(path) != ErrorNumber.NoError) continue;
return foundFilter;
}
transaction.Finish();
return foundFilter;
}
else
noFilter = filter;
noFilter = filter;
}
catch(IOException ex)
{

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -488,17 +488,14 @@ public partial class SpectreTextBlock : TextBlock
{
i++;
if(i < text.Length && text[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));
}
if(i >= text.Length || text[i] != ']') continue;
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
@@ -516,12 +513,10 @@ public partial class SpectreTextBlock : TextBlock
int openTagEnd = i + 1; // After the ']'
tagStack.Push((tagStart, openTagEnd, tagName));
}
i++;
}
}
else
i++;
i++;
}
return result;

View File

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

View File

@@ -485,17 +485,14 @@ public partial class SpectreTextBlock : TextBlock
{
i++;
if(i < text.Length && text[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));
}
if(i >= text.Length || text[i] != ']') continue;
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
@@ -513,12 +510,10 @@ public partial class SpectreTextBlock : TextBlock
int openTagEnd = i + 1; // After the ']'
tagStack.Push((tagStart, openTagEnd, tagName));
}
i++;
}
}
else
i++;
i++;
}
return result;

View File

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

View File

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

View File

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

View File

@@ -1397,14 +1397,11 @@ sealed class ConvertImageCommand : Command<ConvertImageCommand.Settings>
return (false, 0, 0, 0);
}
if(!uint.TryParse(geometryPieces[2], out uint sectors) || sectors == 0)
{
AaruLogging.Error(UI.Invalid_sectors_per_track_specified);
if(uint.TryParse(geometryPieces[2], out uint sectors) && sectors != 0) return (true, cylinders, heads, sectors);
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(