From 9bfb7dc12e4d277f6e8423424fdf68ae1e60ebf5 Mon Sep 17 00:00:00 2001 From: Anna Clemens Date: Wed, 28 Apr 2021 20:37:49 -0400 Subject: [PATCH] fix: handle disabled menu items --- XivCommon/Functions/ContextMenu.cs | 38 +++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/XivCommon/Functions/ContextMenu.cs b/XivCommon/Functions/ContextMenu.cs index dae6eb8..88ed3fc 100755 --- a/XivCommon/Functions/ContextMenu.cs +++ b/XivCommon/Functions/ContextMenu.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Runtime.InteropServices; using System.Text; using Dalamud; @@ -165,6 +166,8 @@ namespace XivCommon.Functions { private unsafe byte OpenMenuDetour(IntPtr addon, int menuSize, AtkValue* atkValueArgs) { this.NormalSize = (int) (&atkValueArgs[0])->UInt; + var hasGameDisabled = menuSize - 7 - this.NormalSize > 0; + var addonName = this.GetParentAddonName(addon); var agent = this.GetContextMenuAgent(); @@ -189,9 +192,18 @@ namespace XivCommon.Functions { this.Items.AddRange(args.AdditionalItems); + var hasCustomDisabled = this.Items.Any(item => !item.Enabled); + var hasAnyDisabled = hasGameDisabled || hasCustomDisabled; + for (var i = 0; i < this.Items.Count; i++) { var item = this.Items[i]; + if (hasAnyDisabled) { + var disabledArg = &atkValueArgs[7 + this.NormalSize * 2 + i + 1]; + this._atkValueChangeType(disabledArg, ValueType.Int); + disabledArg->Int = item.Enabled ? 0 : 1; + } + // set up the agent to ignore this item var menuActions = (byte*) (Marshal.ReadIntPtr(agent + MenuActionsPointerOffset) + MenuActionsOffset); *(menuActions + 7 + this.NormalSize + i) = NoopContextId; @@ -215,7 +227,21 @@ namespace XivCommon.Functions { (&atkValueArgs[0])->UInt += 1; } - menuSize = 7 + (int) (&atkValueArgs[0])->UInt; + // need to enable all the game items manually + if (!hasGameDisabled && hasCustomDisabled) { + for (var i = 0; i < this.NormalSize; i++) { + var disabledArg = &atkValueArgs[7 + this.NormalSize + i + 1]; + this._atkValueChangeType(disabledArg, ValueType.Int); + disabledArg->Int = 0; + } + } + + menuSize = (int) (&atkValueArgs[0])->UInt; + if (hasAnyDisabled) { + menuSize *= 2; + } + + menuSize += 7; Original: return this.ContextMenuOpenHook!.Original(addon, menuSize, atkValueArgs); @@ -259,11 +285,6 @@ namespace XivCommon.Functions { /// A custom context menu item /// public class ContextMenuItem { - /// - /// A unique ID to identify this item. - /// - public Guid Id { get; } = Guid.NewGuid(); - /// /// The name of the context item to be shown for English clients. /// @@ -289,6 +310,11 @@ namespace XivCommon.Functions { /// public ContextMenu.ContextMenuItemSelectedDelegate Action { get; set; } + /// + /// If this item should be enabled in the menu. + /// + public bool Enabled { get; set; } = true; + /// /// Create a new context menu item. ///