diff --git a/Changelog/2026-10-05-native-drag-real-item.md b/Changelog/2026-10-05-native-drag-real-item.md new file mode 100644 index 0000000..19b40f0 --- /dev/null +++ b/Changelog/2026-10-05-native-drag-real-item.md @@ -0,0 +1,49 @@ +# Mini Remote: dragging from the game inventory uses the item you dragged + +**Date:** 2026-10-05 +**Components:** RynthCore.Engine (next installer 2026.10.5.11), RynthAi 0.5.12-legacy-ui (previous 0.5.11-legacy-ui) + +## Problem + +Dragging an item from AC's own inventory onto a Mini Remote slot assigned whatever item was +selected before the drag, not the item being dragged. AC does not change the selected object +when a drag starts, and the engine's drag bridge read the dragged item from the selection. + +Log evidence (session pid 33152): three drags of different items all published +`0x803AAE1B` (Frozen Valley Everlasting Portal Gem, the last clicked item), which landed in slot 17. + +## Fix + +- **New `Compatibility/DragDropHooks.cs`** hooks `UIElementManager::StartDragandDrop` + (`0x0045E120`, thiscall). It resolves the dragged UI element to an object id with AC's own + `UIElement_ItemList::InqDropIconInfo` (`0x004E3380`, cdecl). Same approach as Chorizite's UIHooks. + - Both addresses are resolved by byte patterns that are unique in the shipped client. If either + pattern misses, the hook is skipped (no hard-coded fallback) and the old behavior remains. +- **`ItemDragBridge`** + - `OnNativeDragStart(itemId)` takes the hooked item while a game-owned left press is held, for + carried items only (pack, side pack or worn). Spell-bar drags are ignored. + - Hooked drags are published as `RYNTH_INV_ITEM`, so they can go on **any** slot (and replace an + assigned one), exactly like drags from the RynthCore Inventory. + - The selection guess (`RYNTH_GAME_ITEM`, empty slots only) now runs only when the hook is not installed. + - Released over an ImGui window, the drop is still handed back to AC at the drag's start, so the + item never lands on the ground. +- **RynthAi** — `MiniRemoteHud` comment updated to describe both payloads; version 0.5.12-legacy-ui. + +## Verifying + +The engine log shows, at startup: + +- `HookResolver[DragDrop.InqDropIconInfo]: RESOLVED via pattern-unique @ 0x004E3380` +- `HookResolver[DragDrop.StartDragandDrop]: RESOLVED via pattern-unique @ 0x0045E120` +- `Compat: drag-start hook ready` + +Then, on each AC inventory drag: + +- `DragDropHooks: AC drag start item=0x........` (first 20 drags) +- `ItemDragBridge: native inventory drag 0x........ published to ImGui.` + +## Unchanged + +- Click an item (game inventory or RynthCore Inventory), then click an empty Mini Remote slot. +- RynthCore Inventory right-click **Add to Mini Remote**. +- Drag from the RynthCore Inventory or a popped-out Inventory. diff --git a/src/RynthCore.Engine/Compatibility/DragDropHooks.cs b/src/RynthCore.Engine/Compatibility/DragDropHooks.cs new file mode 100644 index 0000000..6b221bc --- /dev/null +++ b/src/RynthCore.Engine/Compatibility/DragDropHooks.cs @@ -0,0 +1,110 @@ +// ============================================================================ +// RynthCore.Engine - Compatibility/DragDropHooks.cs +// Hooks UIElementManager::StartDragandDrop so the engine knows which item AC +// is actually dragging. AC does not change the selected object when a drag +// starts, so the selection is the previously clicked item, not the dragged one. +// +// The dragged UI element is resolved to an object id with AC's own +// UIElement_ItemList::InqDropIconInfo (the same call AC makes when the drop +// lands), then handed to ItemDragBridge, which republishes it to ImGui. +// +// Threads: AC's main thread (AC's input / UI processing). +// ============================================================================ + +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using RynthCore.Engine.Hooking; +using RynthCore.Engine.ImGuiBackend; + +namespace RynthCore.Engine.Compatibility; + +internal static unsafe class DragDropHooks +{ + // bool __thiscall UIElementManager::StartDragandDrop(UIElementManager*, UIElement* elem, int clickX, int clickY) + private const int StartDragandDropVa = 0x0045E120; + // void __cdecl UIElement_ItemList::InqDropIconInfo(const UIElement* icon, uint& itemId, uint& spellId, DropItemFlags& flags) + private const int InqDropIconInfoVa = 0x004E3380; + + // Both verified unique in the shipped client (sub esp,34h / mov edi,[esp+48h] / mov esi,ecx prologue; + // InqDropIconInfo zeroes its out-params up front). + private static readonly byte?[] StartDragandDropPattern = + [ + 0x83, 0xEC, 0x34, 0x53, 0x55, 0x56, 0x57, 0x8B, 0x7C, 0x24, 0x48, 0x85, 0xFF, 0x8B, 0xF1, 0x0F, 0x84, + null, null, null, null, 0xA1, null, null, null, null, 0x85, 0xC0 + ]; + private static readonly byte?[] InqDropIconInfoPattern = + [ + 0x8B, 0x44, 0x24, 0x10, 0x83, 0xEC, 0x3C, 0x53, 0x55, 0x8B, 0x6C, 0x24, 0x50, 0x56, 0x8B, 0x74, 0x24, 0x4C, + 0x33, 0xDB, 0x3B, 0xF3, 0x57, 0x8B, 0x7C, 0x24, 0x54, 0x89, 0x1F + ]; + + private static IntPtr _originalStartDrag; + private static delegate* unmanaged[Cdecl] _inqDropIconInfo; + private static int _logCount; + + /// The drag-start hook is live, so ItemDragBridge gets the real dragged item. + public static bool IsInstalled { get; private set; } + public static string StatusMessage { get; private set; } = "Not probed yet."; + + public static void Initialize() + { + if (IsInstalled) return; + if (!AcClientModule.TryReadTextSection(out AcClientTextSection text)) + { + StatusMessage = "acclient.exe not available."; + return; + } + + var inq = HookResolver.Resolve(text, "DragDrop.InqDropIconInfo", InqDropIconInfoPattern, InqDropIconInfoVa); + var start = HookResolver.Resolve(text, "DragDrop.StartDragandDrop", StartDragandDropPattern, StartDragandDropVa); + // A guessed address for either would mean calling or patching the wrong code: pattern hits only. + if (inq.Source != HookResolver.ResolveSource.PatternScan || start.Source != HookResolver.ResolveSource.PatternScan) + { + StatusMessage = $"drag-start hook skipped (InqDropIconInfo {inq.Detail}, StartDragandDrop {start.Detail})."; + RynthLog.Compat($"Compat: {StatusMessage}"); + return; + } + + try + { + _inqDropIconInfo = (delegate* unmanaged[Cdecl])inq.Address; + delegate* unmanaged[Thiscall] detour = &StartDragandDropDetour; + MinHook.Hook(start.Address, (IntPtr)detour, out _originalStartDrag); + IsInstalled = true; + StatusMessage = $"Hooked UIElementManager::StartDragandDrop @ 0x{start.Address.ToInt32():X8}."; + RynthLog.Info($"Compat: drag-start hook ready - StartDragandDrop=0x{start.Address.ToInt32():X8}, InqDropIconInfo=0x{inq.Address.ToInt32():X8}"); + } + catch (Exception ex) + { + StatusMessage = ex.Message; + RynthLog.Compat($"Compat: drag-start hook failed - {ex.Message}"); + } + } + + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvThiscall)])] + private static byte StartDragandDropDetour(IntPtr manager, IntPtr element, int clickX, int clickY) + { + try + { + if (element != IntPtr.Zero && _inqDropIconInfo != null) + { + uint itemId = 0, spellId = 0, flags = 0; + _inqDropIconInfo(element, &itemId, &spellId, &flags); + // Spell-bar drags carry a spell id and no item. + if (itemId != 0) + { + if (_logCount < 20) { _logCount++; RynthLog.UI($"DragDropHooks: AC drag start item=0x{itemId:X8} flags=0x{flags:X}"); } + ItemDragBridge.OnNativeDragStart(itemId); + } + } + } + catch (Exception ex) + { + try { RynthLog.Compat($"Compat: drag-start detour error - {ex.GetType().Name}: {ex.Message}"); } catch { } + } + + var original = (delegate* unmanaged[Thiscall])_originalStartDrag; + return original(manager, element, clickX, clickY); + } +} diff --git a/src/RynthCore.Engine/EntryPoint.cs b/src/RynthCore.Engine/EntryPoint.cs index 56f59df..1939e93 100644 --- a/src/RynthCore.Engine/EntryPoint.cs +++ b/src/RynthCore.Engine/EntryPoint.cs @@ -667,6 +667,8 @@ public static class EntryPoint Step("account hooks", AccountHooks.Initialize); Step("client combat hooks", () => ClientCombatHooks.Probe()); Step("selected-target hooks", SelectedTargetHooks.Initialize); + // Which item AC is dragging (AC keeps the old selection during a drag). + Step("drag-start hook", DragDropHooks.Initialize); Step("smartbox hooks", SmartBoxHooks.Initialize); Step("player vitals hooks", PlayerVitalsHooks.Initialize); Step("enchantment hooks", () => EnchantmentHooks.Initialize()); diff --git a/src/RynthCore.Engine/ImGui/ItemDragBridge.cs b/src/RynthCore.Engine/ImGui/ItemDragBridge.cs index c439429..6cbd4e8 100644 --- a/src/RynthCore.Engine/ImGui/ItemDragBridge.cs +++ b/src/RynthCore.Engine/ImGui/ItemDragBridge.cs @@ -10,10 +10,12 @@ // button press, so the payload uses PayloadAutoExpire: it is delivered to // the hovered target on the first frame it is no longer submitted. // • AC's own inventory: a press that began on the game belongs to the game, -// and ImGui ignores hovering for it. A left drag that starts with a carried -// item selected (AC selects the item under the press) is republished the -// same way, under NativePayloadType, and delivered when ImGui sees the -// button go up. +// and ImGui ignores hovering for it. DragDropHooks reports the item AC +// starts dragging (OnNativeDragStart); a carried one is republished the +// same way, under PayloadType, and delivered when ImGui sees the button go +// up. AC does not select the dragged item, so without that hook the bridge +// can only guess from the selection: the guess goes out under +// NativePayloadType, which targets treat as unreliable. // // A native drag released over an ImGui window would otherwise complete in AC // as a drop on the 3D view (the item lands on the ground). TryRedirectNativeDrop @@ -36,9 +38,10 @@ internal static unsafe class ItemDragBridge public const string PayloadType = "RYNTH_INV_ITEM"; /// - /// Payload type for a drag that began in AC's own UI (uint object id). Any game left-drag with - /// a carried item selected looks like this (moving an AC panel too), so targets should take it - /// only where a wrong item does no harm (RynthAi: empty slots). Must match MiniRemoteHud.GameItemPayloadType. + /// Payload type for a game drag whose item was guessed from the selection (DragDropHooks not + /// installed). Any game left-drag with a carried item selected looks like this (moving an AC + /// panel too), so targets should take it only where a wrong item does no harm (RynthAi: empty + /// slots). Must match MiniRemoteHud.GameItemPayloadType. /// public const string NativePayloadType = "RYNTH_GAME_ITEM"; @@ -61,6 +64,10 @@ internal static unsafe class ItemDragBridge private static int _pressX, _pressY; private static long _pressAt; private static uint _nativeItem; + /// _nativeItem came from AC's drag start (DragDropHooks), not from the selection. + private static bool _nativeFromHook; + /// The left button is down on a press that belongs to the game. + private static bool _gamePressHeld; private static bool _nativeReleased; /// The cursor was over an ImGui window in the last main frame (only tracked during a native drag). private static bool _overImGui; @@ -98,7 +105,8 @@ internal static unsafe class ItemDragBridge _pressX = LoWord(lParam); _pressY = HiWord(lParam); _pressAt = Stopwatch.GetTimestamp(); - if (_pressPending) { _nativeItem = 0; _nativeReleased = false; } + _gamePressHeld = _pressPending; + if (_pressPending) { _nativeItem = 0; _nativeFromHook = false; _nativeReleased = false; } break; case WM_MOUSEMOVE: @@ -106,6 +114,7 @@ internal static unsafe class ItemDragBridge { // A release we never saw (outside the window): finish the drag. _pressPending = false; + _gamePressHeld = false; if (_nativeItem != 0) _nativeReleased = true; break; } @@ -115,12 +124,16 @@ internal static unsafe class ItemDragBridge if (dx * dx + dy * dy >= DragThresholdSq) { _pressPending = false; - _nativeItem = CarriedSelection(); - if (_nativeItem != 0) - RynthLog.UI($"ItemDragBridge: native inventory drag 0x{_nativeItem:X8} published to ImGui."); + // With the drag-start hook the real item arrives through OnNativeDragStart. + if (_nativeItem == 0 && !DragDropHooks.IsInstalled) + { + _nativeItem = CarriedSelection(); + if (_nativeItem != 0) + RynthLog.UI($"ItemDragBridge: native inventory drag 0x{_nativeItem:X8} (from the selection) published to ImGui."); + } } } - else if (_nativeItem != 0 && !_nativeReleased && Stopwatch.GetTimestamp() - _pressAt < SelectionSettleTicks) + else if (_nativeItem != 0 && !_nativeFromHook && !_nativeReleased && Stopwatch.GetTimestamp() - _pressAt < SelectionSettleTicks) { // The selection can trail the press by a few messages: follow it while it settles. uint sel = CarriedSelection(); @@ -130,11 +143,26 @@ internal static unsafe class ItemDragBridge case WM_LBUTTONUP: _pressPending = false; + _gamePressHeld = false; if (_nativeItem != 0) _nativeReleased = true; break; } } + /// + /// AC started dragging (DragDropHooks, AC's main thread). Taken while + /// a game-owned left press is held and only for items the player carries. + /// + public static void OnNativeDragStart(uint itemId) + { + if (!_gamePressHeld || itemId == 0 || !IsCarried(itemId)) return; + _pressPending = false; + _nativeItem = itemId; + _nativeFromHook = true; + _nativeReleased = false; + RynthLog.UI($"ItemDragBridge: native inventory drag 0x{itemId:X8} published to ImGui."); + } + /// /// A native item drag is being released over an ImGui window: returns the press position /// to send AC instead, so AC puts the item back rather than dropping it on the ground. @@ -162,12 +190,13 @@ internal static unsafe class ItemDragBridge if (id == 0 && _nativeItem != 0) { - type = NativePayloadType; + type = _nativeFromHook ? PayloadType : NativePayloadType; // After the button-up, stop once ImGui itself sees the button up: that frame // delivers the drop (the payload outlives its last submission by one frame). if (_nativeReleased && !io.MouseDown[0]) { _nativeItem = 0; + _nativeFromHook = false; _nativeReleased = false; _overImGui = false; return; @@ -191,24 +220,30 @@ internal static unsafe class ItemDragBridge { _popOutItem = 0; _pressPending = false; + _gamePressHeld = false; _nativeItem = 0; + _nativeFromHook = false; _nativeReleased = false; _overImGui = false; } - /// The game's selected object when the player carries it (pack, side pack or worn), else 0. + /// The game's selected object when the player carries it, else 0. private static uint CarriedSelection() { uint id = ClientHelperHooks.GetSelectedItemId(); - if (id == 0 || !ClientHelperHooks.HasGetPlayerId) return 0; + return IsCarried(id) ? id : 0; + } + + /// is in the player's pack, a side pack, or worn. + private static bool IsCarried(uint id) + { + if (id == 0 || !ClientHelperHooks.HasGetPlayerId) return false; uint player = ClientHelperHooks.GetPlayerId(); - if (player == 0 || id == player) return 0; - if (!ClientObjectHooks.TryGetObjectOwnershipInfo(id, out uint container, out uint wielder, out _)) return 0; - if (wielder == player || container == player) return id; + if (player == 0 || id == player) return false; + if (!ClientObjectHooks.TryGetObjectOwnershipInfo(id, out uint container, out uint wielder, out _)) return false; + if (wielder == player || container == player) return true; // Side pack in the main pack. - if (container != 0 && ClientObjectHooks.TryGetObjectOwnershipInfo(container, out uint outer, out _, out _) && outer == player) - return id; - return 0; + return container != 0 && ClientObjectHooks.TryGetObjectOwnershipInfo(container, out uint outer, out _, out _) && outer == player; } private static int LoWord(IntPtr l) => unchecked((short)((long)l & 0xFFFF));