Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 103 additions & 17 deletions fluXis/Screens/Edit/Tabs/VerifyTab.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using fluXis.Graphics.Containers;
using fluXis.Graphics.Sprites;
using fluXis.Graphics.Sprites.Text;
using fluXis.Graphics.UserInterface.Buttons;
using fluXis.Graphics.UserInterface.Color;
using fluXis.Screens.Edit.Tabs.Verify;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
Expand Down Expand Up @@ -34,11 +38,14 @@ public partial class VerifyTab : EditorTab

private ForcedHeightText totalIssuesText;
private ForcedHeightText problematicIssuesText;
private FillFlowContainer<VerifyIssueEntry> issuesContainer;

private FillFlowContainer issuesContainer;
private LoadingIcon loadingIcon;
private readonly List<IVerifyCheck> checks = new();

private readonly List<VerifyIssue> issues = new();
private CancellationTokenSource cancellationTokenSource;

public BindableBool IsRunning { get; private set; } = new();
public IReadOnlyList<VerifyIssue> Issues => issues;

public int TotalIssues => issues.Count;
Expand Down Expand Up @@ -89,11 +96,22 @@ private void load()
Origin = Anchor.CentreLeft,
Children = new Drawable[]
{
totalIssuesText = new ForcedHeightText
new FillFlowContainer
{
Text = "{} total issues",
WebFontSize = 20,
Height = 15
Direction = FillDirection.Horizontal,
AutoSizeAxes = Axes.X,
Spacing = new Vector2(8),
Height = 15,
Children = new Drawable[]
{
totalIssuesText = new ForcedHeightText
{
Text = "{} total issues",
WebFontSize = 20,
Height = 15
},
loadingIcon = new LoadingIcon { Size = new Vector2(20) }
}
},
problematicIssuesText = new ForcedHeightText
{
Expand Down Expand Up @@ -156,7 +174,7 @@ private void load()
{
RelativeSizeAxes = Axes.Both,
ScrollbarVisible = false,
Child = issuesContainer = new FillFlowContainer<VerifyIssueEntry>
Child = issuesContainer = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Expand All @@ -168,6 +186,16 @@ private void load()
}
}
};

IsRunning.BindValueChanged(e => {
if (e.NewValue)
{
loadingIcon.Show();
totalIssuesText.Text = $"Running Checks..";
}
else
loadingIcon.Hide();
}, false);
}

protected override void LoadComplete()
Expand Down Expand Up @@ -198,24 +226,73 @@ private void loadChecks()
}
}

public void RefreshIssues()
public async void RefreshIssues()
{
issues.Clear();
cancellationTokenSource?.Cancel();
cancellationTokenSource?.Dispose();
cancellationTokenSource = new CancellationTokenSource();

var results = RunVerify(map);
IsRunning.Value = true;

issues.AddRange(results.Issues);
try
{
issues.Clear();

var results = await Task.Run(() => RunVerifyAsync(map, cancellationTokenSource.Token), cancellationTokenSource.Token);

if (!cancellationTokenSource.Token.IsCancellationRequested)
{
Schedule(() =>
{
issues.AddRange(results.Issues);

issuesContainer.Clear();
issuesContainer.AddRange(issues.Select(x => new VerifyIssueEntry(x)));
issuesContainer.Clear();
issuesContainer.AddRange(issues.Select(x => new VerifyIssueEntry(x)));

totalIssuesText.Text = $"{TotalIssues} total issues";
problematicIssuesText.Text = $"{ProblematicIssues} problematic";
totalIssuesText.Text = $"{TotalIssues} total issues";
problematicIssuesText.Text = $"{ProblematicIssues} problematic";
});
}
}
catch (OperationCanceledException)
{
Logger.Log("Verification cancelled", LoggingTarget.Runtime, LogLevel.Debug);
}
catch (Exception ex)
{
Logger.Log($"Error during verification: {ex.Message}", LoggingTarget.Runtime, LogLevel.Error);
}
finally
{
IsRunning.Value = false;
}
}

public VerifyResults RunVerify(IVerifyContext ctx)
public async Task<VerifyResults> RunVerifyAsync(IVerifyContext ctx, CancellationToken cancellationToken = default)
{
var list = checks.SelectMany(x => x.Check(ctx)).ToList();
var checkTasks = checks.Select(check => Task.Run(() =>
{
cancellationToken.ThrowIfCancellationRequested();

try
{
return check.Check(ctx);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
Logger.Log($"Error running check {check.GetType().Name}: {ex.Message}", LoggingTarget.Runtime, LogLevel.Error);
return Enumerable.Empty<VerifyIssue>();
}
}, cancellationToken)).ToArray();

var results = await Task.WhenAll(checkTasks);

var list = results.SelectMany(x => x).ToList();

list.Sort((a, b) =>
{
if (a.Time != b.Time)
Expand All @@ -229,4 +306,13 @@ public VerifyResults RunVerify(IVerifyContext ctx)

return new VerifyResults(list);
}

public VerifyResults RunVerify(IVerifyContext ctx) => RunVerifyAsync(ctx).GetAwaiter().GetResult();

protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
cancellationTokenSource?.Cancel();
cancellationTokenSource?.Dispose();
}
}