diff --git a/Craftimizer/Plugin.cs b/Craftimizer/Plugin.cs index 0eaebac..ec2d7a1 100644 --- a/Craftimizer/Plugin.cs +++ b/Craftimizer/Plugin.cs @@ -1,8 +1,12 @@ 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; @@ -18,6 +22,8 @@ public sealed class Plugin : IDalamudPlugin public Craft SynthesisWindow { get; } public Windows.Simulator? SimulatorWindow { get; set; } + public Hooks Hook { get; } + public Plugin([RequiredVersion("1.0")] DalamudPluginInterface pluginInterface) { Service.Plugin = this; @@ -32,6 +38,8 @@ 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) @@ -53,5 +61,6 @@ public sealed class Plugin : IDalamudPlugin public void Dispose() { SimulatorWindow?.Dispose(); + Hook.Dispose(); } } diff --git a/Craftimizer/Utils/Hooks.cs b/Craftimizer/Utils/Hooks.cs new file mode 100644 index 0000000..1807544 --- /dev/null +++ b/Craftimizer/Utils/Hooks.cs @@ -0,0 +1,72 @@ +using Craftimizer.Simulator; +using Dalamud.Hooking; +using Dalamud.Logging; +using Dalamud.Utility.Signatures; +using System; + +namespace Craftimizer.Utils; + +public sealed class Hooks : IDisposable +{ + public class ConditionUpdatedEventArgs : EventArgs + { + public Condition Condition { get; } + + public ConditionUpdatedEventArgs(Condition condition) + { + Condition = condition; + } + } + + public event EventHandler? OnConditionUpdated; + + public delegate void ActorControlSelfPrototype(uint entityId, uint type, uint a3, uint a4, uint a5, uint source, uint a7, uint a8, ulong a9, byte flag); + + // https://github.com/Kouzukii/ffxiv-deathrecap/blob/1298e75c5e15a6596e8678e85b8f1bde926051bf/Events/CombatEventCapture.cs#L82 + [Signature("E8 ?? ?? ?? ?? 0F B7 0B 83 E9 64", DetourName = nameof(ActorControlSelfDetour))] + public readonly Hook ActorControlSelfHook = null!; + + public Hooks() + { + SignatureHelper.Initialise(this); + ActorControlSelfHook.Enable(); + } + + private bool HandleCondition(uint type, uint a3, uint a4) + { + // Crafting related or something? + if (type != 300) + return false; + + // Condition update + if (a3 != 9) + return false; + + // Invalid condition + if (a4 < 2) + { + PluginLog.LogError($"Invalid condition {a4}"); + return false; + } + + var condition = (Condition)(1 << ((int)a4 - 2)); + + OnConditionUpdated?.Invoke(this, new(condition)); + return true; + } + + private void ActorControlSelfDetour(uint entityId, uint type, uint a3, uint a4, uint a5, uint source, uint a7, uint a8, ulong a9, byte flag) + { + ActorControlSelfHook.Original(entityId, type, a3, a4, a5, source, a7, a8, a9, flag); + + if (HandleCondition(type, a3, a4)) + return; + + //PluginLog.LogDebug($"{entityId} {type} {a3} {a4} {a5} {a7} {a8} {a9} {flag}"); + } + + public void Dispose() + { + ActorControlSelfHook.Dispose(); + } +} diff --git a/Craftimizer/Utils/RecipeNote.cs b/Craftimizer/Utils/RecipeNote.cs index 018fe39..02503e5 100644 --- a/Craftimizer/Utils/RecipeNote.cs +++ b/Craftimizer/Utils/RecipeNote.cs @@ -31,7 +31,7 @@ public unsafe class RecipeNote public RecipeNote() { - + } public bool Update(out bool isNewRecipe) @@ -44,11 +44,6 @@ public unsafe class RecipeNote AddonRecipe = (AddonRecipeNote*)Service.GameGui.GetAddonByName("RecipeNote"); AddonSynthesis = (AddonSynthesis*)Service.GameGui.GetAddonByName("Synthesis"); - if (AddonRecipe == null) - return false; - if (AddonSynthesis == null) - return false; - State = CSRecipeNote.Instance(); var list = State->RecipeList; diff --git a/Craftimizer/Windows/Craft.cs b/Craftimizer/Windows/Craft.cs index a55d896..55b8b56 100644 --- a/Craftimizer/Windows/Craft.cs +++ b/Craftimizer/Windows/Craft.cs @@ -58,6 +58,9 @@ public unsafe class Craft : Window { if (!RecipeUtils.Update(out _)) return false; + return false; + if (RecipeUtils.AddonSynthesis == null) + return false; // Check if Synthesis addon is visible if (RecipeUtils.AddonSynthesis->AtkUnitBase.WindowNode == null) diff --git a/Craftimizer/Windows/CraftingLog.cs b/Craftimizer/Windows/CraftingLog.cs index 3181937..833dc6e 100644 --- a/Craftimizer/Windows/CraftingLog.cs +++ b/Craftimizer/Windows/CraftingLog.cs @@ -394,6 +394,9 @@ public unsafe class CraftingLog : Window if (!RecipeUtils.Update(out var isNew)) return false; + if (RecipeUtils.AddonRecipe == null) + return false; + // Check if RecipeNote addon is visible if (RecipeUtils.AddonRecipe->AtkUnitBase.WindowNode == null) return false;