Don't calculate macro when synth helper is collapsed
This commit is contained in:
@@ -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();
|
||||||
|
}
|
||||||
@@ -83,11 +83,10 @@ public sealed class MacroEditor : Window, IDisposable
|
|||||||
private ActionType[] DefaultActions { get; }
|
private ActionType[] DefaultActions { get; }
|
||||||
private Action<IEnumerable<ActionType>>? MacroSetter { get; set; }
|
private Action<IEnumerable<ActionType>>? MacroSetter { get; set; }
|
||||||
|
|
||||||
private CancellationTokenSource? SolverTokenSource { get; set; }
|
private BackgroundTask<int>? SolverTask { get; set; }
|
||||||
private Exception? SolverException { get; set; }
|
private bool SolverRunning => (!SolverTask?.Completed) ?? false;
|
||||||
private int? SolverStartStepCount { get; set; }
|
|
||||||
private Solver.Solver? SolverObject { get; set; }
|
private Solver.Solver? SolverObject { get; set; }
|
||||||
private bool SolverRunning => SolverTokenSource != null;
|
private int? SolverStartStepCount { get; set; }
|
||||||
|
|
||||||
private ILoadedTextureIcon ExpertBadge { get; }
|
private ILoadedTextureIcon ExpertBadge { get; }
|
||||||
private ILoadedTextureIcon CollectibleBadge { get; }
|
private ILoadedTextureIcon CollectibleBadge { get; }
|
||||||
@@ -197,7 +196,8 @@ public sealed class MacroEditor : Window, IDisposable
|
|||||||
|
|
||||||
public override void OnClose()
|
public override void OnClose()
|
||||||
{
|
{
|
||||||
SolverTokenSource?.Cancel();
|
base.OnClose();
|
||||||
|
SolverTask?.Cancel();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Update()
|
public override void Update()
|
||||||
@@ -1270,7 +1270,7 @@ public sealed class MacroEditor : Window, IDisposable
|
|||||||
ImGui.SameLine();
|
ImGui.SameLine();
|
||||||
if (SolverRunning)
|
if (SolverRunning)
|
||||||
{
|
{
|
||||||
if (SolverTokenSource?.IsCancellationRequested ?? false)
|
if (SolverTask?.Cancelling ?? false)
|
||||||
{
|
{
|
||||||
using var _disabled = ImRaii.Disabled();
|
using var _disabled = ImRaii.Disabled();
|
||||||
ImGui.Button("Stopping", new(halfWidth, height));
|
ImGui.Button("Stopping", new(halfWidth, height));
|
||||||
@@ -1278,7 +1278,7 @@ public sealed class MacroEditor : Window, IDisposable
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (ImGui.Button("Stop", new(halfWidth, height)))
|
if (ImGui.Button("Stop", new(halfWidth, height)))
|
||||||
SolverTokenSource?.Cancel();
|
SolverTask?.Cancel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -1524,9 +1524,7 @@ public sealed class MacroEditor : Window, IDisposable
|
|||||||
|
|
||||||
private void CalculateBestMacro()
|
private void CalculateBestMacro()
|
||||||
{
|
{
|
||||||
SolverTokenSource?.Cancel();
|
SolverTask?.Cancel();
|
||||||
SolverTokenSource = new();
|
|
||||||
SolverException = null;
|
|
||||||
Macro.ClearQueue();
|
Macro.ClearQueue();
|
||||||
|
|
||||||
RevertPreviousMacro();
|
RevertPreviousMacro();
|
||||||
@@ -1540,49 +1538,27 @@ public sealed class MacroEditor : Window, IDisposable
|
|||||||
|
|
||||||
SolverStartStepCount = Macro.Count;
|
SolverStartStepCount = Macro.Count;
|
||||||
|
|
||||||
var token = SolverTokenSource.Token;
|
|
||||||
var state = State;
|
var state = State;
|
||||||
var task = Task.Run(() => CalculateBestMacroTask(state, token), token);
|
SolverTask = new(token => CalculateBestMacroTask(state, token));
|
||||||
_ = task.ContinueWith(t =>
|
SolverTask.Start();
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CalculateBestMacroTask(SimulationState state, CancellationToken token)
|
private int CalculateBestMacroTask(SimulationState state, CancellationToken token)
|
||||||
{
|
{
|
||||||
var config = Service.Configuration.EditorSolverConfig;
|
var config = Service.Configuration.EditorSolverConfig;
|
||||||
|
|
||||||
token.ThrowIfCancellationRequested();
|
token.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
using (SolverObject = new Solver.Solver(config, state) { Token = token })
|
var solver = new Solver.Solver(config, state) { Token = token };
|
||||||
{
|
solver.OnLog += Log.Debug;
|
||||||
SolverObject.OnLog += Log.Debug;
|
solver.OnNewAction += a => Macro.Enqueue(a);
|
||||||
SolverObject.OnNewAction += a => Macro.Enqueue(a);
|
SolverObject = solver;
|
||||||
SolverObject.Start();
|
solver.Start();
|
||||||
_ = SolverObject.GetTask().GetAwaiter().GetResult();
|
_ = solver.GetTask().GetAwaiter().GetResult();
|
||||||
}
|
|
||||||
|
|
||||||
token.ThrowIfCancellationRequested();
|
token.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RevertPreviousMacro()
|
private void RevertPreviousMacro()
|
||||||
|
|||||||
@@ -60,44 +60,6 @@ public sealed unsafe class RecipeNote : Window, IDisposable
|
|||||||
public CharacterStats? CharacterStats { get; private set; }
|
public CharacterStats? CharacterStats { get; private set; }
|
||||||
public CraftableStatus CraftStatus { 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<(Macro?, SimulationState?)>? SavedMacroTask { get; set; }
|
||||||
private BackgroundTask<SolverSolution>? SuggestedMacroTask { get; set; }
|
private BackgroundTask<SolverSolution>? SuggestedMacroTask { get; set; }
|
||||||
private BackgroundTask<(CommunityMacros.CommunityMacro?, SimulationState?)>? CommunityMacroTask { get; set; }
|
private BackgroundTask<(CommunityMacros.CommunityMacro?, SimulationState?)>? CommunityMacroTask { get; set; }
|
||||||
|
|||||||
@@ -529,7 +529,7 @@ public sealed unsafe class SynthHelper : Window, IDisposable
|
|||||||
|
|
||||||
private void OnUseAction(ActionType action)
|
private void OnUseAction(ActionType action)
|
||||||
{
|
{
|
||||||
if (!IsCrafting)
|
if (!IsCrafting || !ShouldOpen || IsCollapsed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
(_, CurrentState) = new SimNoRandom().Execute(GetCurrentState(), action);
|
(_, CurrentState) = new SimNoRandom().Execute(GetCurrentState(), action);
|
||||||
@@ -539,7 +539,7 @@ public sealed unsafe class SynthHelper : Window, IDisposable
|
|||||||
|
|
||||||
private void OnFinishedUsingAction()
|
private void OnFinishedUsingAction()
|
||||||
{
|
{
|
||||||
if (!IsCrafting)
|
if (!IsCrafting || !ShouldOpen || IsCollapsed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
CurrentState = GetCurrentState();
|
CurrentState = GetCurrentState();
|
||||||
@@ -596,7 +596,7 @@ public sealed unsafe class SynthHelper : Window, IDisposable
|
|||||||
|
|
||||||
private void OnStateUpdated()
|
private void OnStateUpdated()
|
||||||
{
|
{
|
||||||
if (!IsCrafting)
|
if (!IsCrafting || !ShouldOpen || IsCollapsed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Macro.Clear();
|
Macro.Clear();
|
||||||
|
|||||||
Reference in New Issue
Block a user