Skip to content

Commit

Permalink
State management bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
WorkingRobot committed Jul 31, 2024
1 parent 10bb95d commit 7c32ff7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
2 changes: 2 additions & 0 deletions Craftimizer/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Dalamud.IoC;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using Dalamud.Storage.Assets;

namespace Craftimizer.Plugin;

Expand All @@ -19,6 +20,7 @@ public sealed class Service
[PluginService] public static IClientState ClientState { get; private set; }
[PluginService] public static IDataManager DataManager { get; private set; }
[PluginService] public static ITextureProvider TextureProvider { get; private set; }
[PluginService] public static IDalamudAssetManager DalamudAssetManager { get; private set; }
[PluginService] public static ITargetManager TargetManager { get; private set; }
[PluginService] public static ICondition Condition { get; private set; }
[PluginService] public static IFramework Framework { get; private set; }
Expand Down
7 changes: 5 additions & 2 deletions Craftimizer/Utils/Hooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ private byte IsActionHighlightedDetour(ActionManager* manager, CSActionType acti
if (!Service.Configuration.SynthHelperAbilityAnts)
return ret;

if (!Service.Plugin.SynthHelperWindow.ShouldDrawAnts)
if (Service.Plugin.SynthHelperWindow is not { } window)
return ret;

if (!window.ShouldDrawAnts)
return ret;

if (actionType is not (CSActionType.CraftAction or CSActionType.Action))
Expand All @@ -84,7 +87,7 @@ private byte IsActionHighlightedDetour(ActionManager* manager, CSActionType acti
if (simActionType == null)
return ret;

if (Service.Plugin.SynthHelperWindow.NextAction != simActionType)
if (window.NextAction != simActionType)
return 0;
}
catch (Exception ex)
Expand Down
5 changes: 1 addition & 4 deletions Craftimizer/Utils/IconManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public sealed class IconManager : IDisposable
{
private sealed class LoadedIcon : ILoadedTextureIcon
{
// 10: DXGI_FORMAT_R16G16B16A16_FLOAT
public static IDalamudTextureWrap EmptyTexture { get; } = Service.TextureProvider.CreateEmpty(new(4, 4, 10), false, false);

public ISharedImmediateTexture Source { get; }

public Vector2? Dimensions => GetWrap()?.Size;
Expand All @@ -54,7 +51,7 @@ public LoadedIcon(ISharedImmediateTexture source)
return null;
}

public IDalamudTextureWrap GetWrapOrEmpty() => GetWrap() ?? EmptyTexture;
public IDalamudTextureWrap GetWrapOrEmpty() => GetWrap() ?? Service.DalamudAssetManager.Empty4X4;

public void Dispose()
{
Expand Down
40 changes: 22 additions & 18 deletions Craftimizer/Windows/SynthHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ public sealed unsafe class SynthHelper : Window, IDisposable
public CharacterStats? CharacterStats { get; private set; }
public SimulationInput? SimulationInput { get; private set; }
public ActionType? NextAction => (ShouldOpen && Macro.Count > 0) ? Macro[0].Action : null;
public bool ShouldDrawAnts => ShouldOpen;
public bool ShouldDrawAnts => ShouldOpen && !IsCollapsed;

public bool IsCrafting { get; private set; }
private int CurrentActionCount { get; set; }
private ActionStates CurrentActionStates { get; set; }
private SimulationState CurrentState
Expand Down Expand Up @@ -112,6 +111,8 @@ public SynthHelper() : base(WindowNamePinned)
private bool ShouldCalculate => !IsCollapsed && ShouldOpen;
private bool WasCalculatable { get; set; }

private bool IsRecalculateQueued { get; set; }

public override void Update()
{
base.Update();
Expand All @@ -121,14 +122,15 @@ public override void Update()
if (ShouldCalculate != WasCalculatable)
{
if (WasCalculatable)
{
IsCrafting = false;
SolverTask?.Cancel();
}
else if (Macro.Count == 0)
{
OnStateUpdated();
}
}

if (Macro.Count == 0)
{
if (ShouldOpen != WasOpen || IsCollapsed != WasCollapsed)
OnStateUpdated();
}

if (!ShouldOpen)
Expand Down Expand Up @@ -188,11 +190,11 @@ private bool CalculateShouldOpen()
if (agent->ActiveCraftRecipeId == 0)
return false;

if (!IsCrafting)
{
IsCrafting = true;
if (RecipeData?.RecipeId != agent->ActiveCraftRecipeId)
OnStartCrafting(recipeId);
}

if (IsRecalculateQueued)
OnStateUpdated();

Macro.FlushQueue();

Expand Down Expand Up @@ -474,6 +476,8 @@ public bool ExecuteNextAction()

private void OnStartCrafting(ushort recipeId)
{
Log.Debug("On Start craftgin begin!");

var shouldUpdateInput = false;
if (recipeId != RecipeData?.RecipeId)
{
Expand Down Expand Up @@ -504,23 +508,19 @@ private void OnStartCrafting(ushort recipeId)
CurrentActionCount = 0;
CurrentActionStates = new();
CurrentState = GetCurrentState();

Log.Debug("On Start craftgin end!");
}

private void OnUseAction(ActionType action)
{
if (!IsCrafting || !ShouldOpen || IsCollapsed)
return;

(_, CurrentState) = new SimNoRandom().Execute(GetCurrentState(), action);
CurrentActionCount = CurrentState.ActionCount;
CurrentActionStates = CurrentState.ActionStates;
}

private void OnFinishedUsingAction()
{
if (!IsCrafting || !ShouldOpen || IsCollapsed)
return;

CurrentState = GetCurrentState();
}

Expand Down Expand Up @@ -575,9 +575,13 @@ bool HasEffect(ushort id)

private void OnStateUpdated()
{
if (!IsCrafting || !ShouldOpen || IsCollapsed)
if (!ShouldOpen || IsCollapsed)
{
IsRecalculateQueued = true;
return;
}

IsRecalculateQueued = false;
Macro.Clear();
Macro.InitialState = CurrentState;
CalculateBestMacro();
Expand Down

1 comment on commit 7c32ff7

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 7c32ff7 Previous: 1622902 Ratio
Craftimizer.Benchmark.Bench.Solve(State: 15BDC93B, Config: B9F5D5BA) 93145475.55555557 ns (± 616339.8913435487)
Craftimizer.Benchmark.Bench.Solve(State: 9A232EB5, Config: B9F5D5BA) 66827255.833333336 ns (± 416356.75000926043)

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.