Don't calculate macro when synth helper is collapsed

This commit is contained in:
Asriel Camora
2024-07-27 11:05:46 -07:00
parent 6dd45456a5
commit 339f26f119
4 changed files with 66 additions and 84 deletions
+44
View File
@@ -0,0 +1,44 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Craftimizer.Utils;
public sealed class BackgroundTask<T>(Func<CancellationToken, T> func) : IDisposable where T : struct
{
public T? Result { get; private set; }
public Exception? Exception { get; private set; }
public bool Completed { get; private set; }
public bool Cancelling => !Completed && TokenSource.IsCancellationRequested;
private CancellationTokenSource TokenSource { get; } = new();
private Func<CancellationToken, T> Func { get; } = func;
public void Start()
{
var token = TokenSource.Token;
var task = Task.Run(() => Result = Func(token), token);
_ = task.ContinueWith(t => Completed = true);
_ = task.ContinueWith(t =>
{
if (token.IsCancellationRequested)
return;
try
{
t.Exception!.Flatten().Handle(ex => ex is TaskCanceledException or OperationCanceledException);
}
catch (AggregateException e)
{
Exception = e;
Log.Error(e, "Background task failed");
}
}, TaskContinuationOptions.OnlyOnFaulted);
}
public void Cancel() =>
TokenSource.Cancel();
public void Dispose() =>
Cancel();
}
+19 -43
View File
@@ -83,11 +83,10 @@ public sealed class MacroEditor : Window, IDisposable
private ActionType[] DefaultActions { get; }
private Action<IEnumerable<ActionType>>? MacroSetter { get; set; }
private CancellationTokenSource? SolverTokenSource { get; set; }
private Exception? SolverException { get; set; }
private int? SolverStartStepCount { get; set; }
private BackgroundTask<int>? SolverTask { get; set; }
private bool SolverRunning => (!SolverTask?.Completed) ?? false;
private Solver.Solver? SolverObject { get; set; }
private bool SolverRunning => SolverTokenSource != null;
private int? SolverStartStepCount { get; set; }
private ILoadedTextureIcon ExpertBadge { get; }
private ILoadedTextureIcon CollectibleBadge { get; }
@@ -197,7 +196,8 @@ public sealed class MacroEditor : Window, IDisposable
public override void OnClose()
{
SolverTokenSource?.Cancel();
base.OnClose();
SolverTask?.Cancel();
}
public override void Update()
@@ -1270,7 +1270,7 @@ public sealed class MacroEditor : Window, IDisposable
ImGui.SameLine();
if (SolverRunning)
{
if (SolverTokenSource?.IsCancellationRequested ?? false)
if (SolverTask?.Cancelling ?? false)
{
using var _disabled = ImRaii.Disabled();
ImGui.Button("Stopping", new(halfWidth, height));
@@ -1278,7 +1278,7 @@ public sealed class MacroEditor : Window, IDisposable
else
{
if (ImGui.Button("Stop", new(halfWidth, height)))
SolverTokenSource?.Cancel();
SolverTask?.Cancel();
}
}
else
@@ -1524,9 +1524,7 @@ public sealed class MacroEditor : Window, IDisposable
private void CalculateBestMacro()
{
SolverTokenSource?.Cancel();
SolverTokenSource = new();
SolverException = null;
SolverTask?.Cancel();
Macro.ClearQueue();
RevertPreviousMacro();
@@ -1540,49 +1538,27 @@ public sealed class MacroEditor : Window, IDisposable
SolverStartStepCount = Macro.Count;
var token = SolverTokenSource.Token;
var state = State;
var task = Task.Run(() => CalculateBestMacroTask(state, token), token);
_ = task.ContinueWith(t =>
{
if (token == SolverTokenSource.Token)
{
SolverTokenSource = null;
SolverObject = null;
}
});
_ = task.ContinueWith(t =>
{
if (token.IsCancellationRequested)
return;
try
{
t.Exception!.Flatten().Handle(ex => ex is TaskCanceledException or OperationCanceledException);
}
catch (AggregateException e)
{
SolverException = e;
Log.Error(e, "Calculating macro failed");
}
}, TaskContinuationOptions.OnlyOnFaulted);
SolverTask = new(token => CalculateBestMacroTask(state, token));
SolverTask.Start();
}
private void CalculateBestMacroTask(SimulationState state, CancellationToken token)
private int CalculateBestMacroTask(SimulationState state, CancellationToken token)
{
var config = Service.Configuration.EditorSolverConfig;
token.ThrowIfCancellationRequested();
using (SolverObject = new Solver.Solver(config, state) { Token = token })
{
SolverObject.OnLog += Log.Debug;
SolverObject.OnNewAction += a => Macro.Enqueue(a);
SolverObject.Start();
_ = SolverObject.GetTask().GetAwaiter().GetResult();
}
var solver = new Solver.Solver(config, state) { Token = token };
solver.OnLog += Log.Debug;
solver.OnNewAction += a => Macro.Enqueue(a);
SolverObject = solver;
solver.Start();
_ = solver.GetTask().GetAwaiter().GetResult();
token.ThrowIfCancellationRequested();
return 0;
}
private void RevertPreviousMacro()
-38
View File
@@ -60,44 +60,6 @@ public sealed unsafe class RecipeNote : Window, IDisposable
public CharacterStats? CharacterStats { get; private set; }
public CraftableStatus CraftStatus { get; private set; }
public sealed class BackgroundTask<T>(Func<CancellationToken, T> func) : IDisposable where T : struct
{
public T? Result { get; private set; }
public Exception? Exception { get; private set; }
public bool Completed { get; private set; }
private CancellationTokenSource TokenSource { get; } = new();
private Func<CancellationToken, T> Func { get; } = func;
public void Start()
{
var token = TokenSource.Token;
var task = Task.Run(() => Result = Func(token), token);
_ = task.ContinueWith(t => Completed = true);
_ = task.ContinueWith(t =>
{
if (token.IsCancellationRequested)
return;
try
{
t.Exception!.Flatten().Handle(ex => ex is TaskCanceledException or OperationCanceledException);
}
catch (AggregateException e)
{
Exception = e;
Log.Error(e, "Calculating macros failed");
}
}, TaskContinuationOptions.OnlyOnFaulted);
}
public void Cancel() =>
TokenSource.Cancel();
public void Dispose() =>
Cancel();
}
private BackgroundTask<(Macro?, SimulationState?)>? SavedMacroTask { get; set; }
private BackgroundTask<SolverSolution>? SuggestedMacroTask { get; set; }
private BackgroundTask<(CommunityMacros.CommunityMacro?, SimulationState?)>? CommunityMacroTask { get; set; }
+3 -3
View File
@@ -529,7 +529,7 @@ public sealed unsafe class SynthHelper : Window, IDisposable
private void OnUseAction(ActionType action)
{
if (!IsCrafting)
if (!IsCrafting || !ShouldOpen || IsCollapsed)
return;
(_, CurrentState) = new SimNoRandom().Execute(GetCurrentState(), action);
@@ -539,7 +539,7 @@ public sealed unsafe class SynthHelper : Window, IDisposable
private void OnFinishedUsingAction()
{
if (!IsCrafting)
if (!IsCrafting || !ShouldOpen || IsCollapsed)
return;
CurrentState = GetCurrentState();
@@ -596,7 +596,7 @@ public sealed unsafe class SynthHelper : Window, IDisposable
private void OnStateUpdated()
{
if (!IsCrafting)
if (!IsCrafting || !ShouldOpen || IsCollapsed)
return;
Macro.Clear();