From 8aee6e54afd8b62d7fd93cd3b6924e3aa807e18f Mon Sep 17 00:00:00 2001 From: Asriel Camora Date: Fri, 21 Jul 2023 15:57:46 +0400 Subject: [PATCH] Change RecipeNote code into utility --- Craftimizer/Plugin.cs | 14 ++--- Craftimizer/Service.cs | 1 + Craftimizer/Utils/RecipeNote.cs | 93 +++++++++++++++++++++++++-------- 3 files changed, 79 insertions(+), 29 deletions(-) diff --git a/Craftimizer/Plugin.cs b/Craftimizer/Plugin.cs index ec2d7a1..ddfdda5 100644 --- a/Craftimizer/Plugin.cs +++ b/Craftimizer/Plugin.cs @@ -1,12 +1,9 @@ using Craftimizer.Plugin.Windows; using Craftimizer.Simulator; using Craftimizer.Utils; -using Dalamud.Hooking; using Dalamud.Interface.Windowing; using Dalamud.IoC; -using Dalamud.Logging; using Dalamud.Plugin; -using Dalamud.Utility.Signatures; using Lumina.Excel.GeneratedSheets; using ClassJob = Craftimizer.Simulator.ClassJob; @@ -22,7 +19,8 @@ public sealed class Plugin : IDalamudPlugin public Craft SynthesisWindow { get; } public Windows.Simulator? SimulatorWindow { get; set; } - public Hooks Hook { get; } + public Hooks Hooks { get; } + public RecipeNote RecipeNote { get; } public Plugin([RequiredVersion("1.0")] DalamudPluginInterface pluginInterface) { @@ -30,6 +28,8 @@ public sealed class Plugin : IDalamudPlugin pluginInterface.Create(); Service.Configuration = pluginInterface.GetPluginConfig() as Configuration ?? new Configuration(); + Hooks = new(); + RecipeNote = new(); WindowSystem = new(Name); SettingsWindow = new(); @@ -38,8 +38,6 @@ public sealed class Plugin : IDalamudPlugin Service.PluginInterface.UiBuilder.Draw += WindowSystem.Draw; Service.PluginInterface.UiBuilder.OpenConfigUi += OpenSettingsWindow; - - Hook = new(); } public void OpenSimulatorWindow(Item item, bool isExpert, SimulationInput input, ClassJob classJob, Macro? macro) @@ -61,6 +59,8 @@ public sealed class Plugin : IDalamudPlugin public void Dispose() { SimulatorWindow?.Dispose(); - Hook.Dispose(); + SynthesisWindow.Dispose(); + RecipeNote.Dispose(); + Hooks.Dispose(); } } diff --git a/Craftimizer/Service.cs b/Craftimizer/Service.cs index c2f3b56..7485821 100644 --- a/Craftimizer/Service.cs +++ b/Craftimizer/Service.cs @@ -23,6 +23,7 @@ public sealed class Service [PluginService] public static DataManager DataManager { get; private set; } [PluginService] public static TargetManager TargetManager { get; private set; } [PluginService] public static Condition Condition { get; private set; } + [PluginService] public static Framework Framework { get; private set; } public static Plugin Plugin { get; internal set; } public static Configuration Configuration { get; internal set; } diff --git a/Craftimizer/Utils/RecipeNote.cs b/Craftimizer/Utils/RecipeNote.cs index 02503e5..315bbcd 100644 --- a/Craftimizer/Utils/RecipeNote.cs +++ b/Craftimizer/Utils/RecipeNote.cs @@ -1,25 +1,29 @@ using Craftimizer.Plugin; using Craftimizer.Simulator; -using FFXIVClientStructs.FFXIV.Client.Game.UI; +using Dalamud.Game; +using Dalamud.Logging; using FFXIVClientStructs.FFXIV.Client.Game; +using FFXIVClientStructs.FFXIV.Client.Game.Object; +using FFXIVClientStructs.FFXIV.Client.Game.UI; using FFXIVClientStructs.FFXIV.Client.UI; +using FFXIVClientStructs.FFXIV.Client.UI.Agent; using Lumina.Excel.GeneratedSheets; -using System.Linq; using System; +using System.Linq; +using ActionType = Craftimizer.Simulator.Actions.ActionType; using ClassJob = Craftimizer.Simulator.ClassJob; using CSRecipeNote = FFXIVClientStructs.FFXIV.Client.Game.UI.RecipeNote; -using ActionType = Craftimizer.Simulator.Actions.ActionType; -using FFXIVClientStructs.FFXIV.Client.Game.Object; namespace Craftimizer.Utils; -public unsafe class RecipeNote +public sealed unsafe class RecipeNote : IDisposable { public AddonRecipeNote* AddonRecipe { get; private set; } public AddonSynthesis* AddonSynthesis { get; private set; } - public CSRecipeNote* State { get; private set; } + public bool IsCrafting { get; private set; } public ushort RecipeId { get; private set; } public Recipe Recipe { get; private set; } = null!; + public bool HasValidRecipe { get; private set; } public RecipeLevelTable Table { get; private set; } = null!; public RecipeInfo Info { get; private set; } = null!; @@ -31,34 +35,45 @@ public unsafe class RecipeNote public RecipeNote() { - + Service.Framework.Update += FrameworkUpdate; } - public bool Update(out bool isNewRecipe) + private void FrameworkUpdate(Framework f) { - isNewRecipe = false; + HasValidRecipe = false; + try + { + HasValidRecipe = Update(); + } + catch (Exception e) + { + PluginLog.LogError(e, "RecipeNote framework update failed"); + } + } + public bool Update() + { if (Service.ClientState.LocalPlayer == null) return false; AddonRecipe = (AddonRecipeNote*)Service.GameGui.GetAddonByName("RecipeNote"); AddonSynthesis = (AddonSynthesis*)Service.GameGui.GetAddonByName("Synthesis"); - State = CSRecipeNote.Instance(); + var recipeId = GetRecipeIdFromList(); + if (recipeId == null) + { + recipeId = GetRecipeIdFromAgent(); + if (recipeId == null) + return false; + else + IsCrafting = true; + } + else + IsCrafting = false; - var list = State->RecipeList; + var isNewRecipe = RecipeId != recipeId.Value; - if (list == null) - return false; - - var recipeEntry = list->SelectedRecipe; - - if (recipeEntry == null) - return false; - - isNewRecipe = RecipeId != recipeEntry->RecipeId; - - RecipeId = recipeEntry->RecipeId; + RecipeId = recipeId.Value; var recipe = LuminaSheets.RecipeSheet.GetRow(RecipeId); @@ -73,6 +88,35 @@ public unsafe class RecipeNote return true; } + private static ushort? GetRecipeIdFromList() + { + var instance = CSRecipeNote.Instance(); + + var list = instance->RecipeList; + + if (list == null) + return null; + + var recipeEntry = list->SelectedRecipe; + + if (recipeEntry == null) + return null; + + return recipeEntry->RecipeId; + } + + private static ushort? GetRecipeIdFromAgent() + { + var instance = AgentRecipeNote.Instance(); + + var recipeId = instance->ActiveCraftRecipeId; + + if (recipeId == 0) + return null; + + return (ushort)recipeId; + } + private void CalculateStats() { Table = Recipe.RecipeLevelTable.Value!; @@ -104,4 +148,9 @@ public unsafe class RecipeNote ProgressModifier = Table.ProgressModifier, ProgressDivider = Table.ProgressDivider, }; + + public void Dispose() + { + Service.Framework.Update -= FrameworkUpdate; + } }