diff --git a/src/Offsets.h b/src/Offsets.h index 864316e3..8a4f9d7b 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -1964,8 +1964,11 @@ enum Offsets { FUN_SCRIPT_PICKUP_INVENTORY_ITEM = 0x004C8DA0, // Bag-update fire sites — see TODO #67 for the full Ghidra - // investigation. Hook these two to detect BAG_UPDATE fires and + // investigation. Hook these three to detect BAG_UPDATE fires and // emit BAG_UPDATE_DELAYED once per frame in which any fired. + // Pattern-search confirms only three fire sites for `0x148` + // (the BAG_UPDATE event ID) exist in 1.12.1's `.text`, and we + // cover all three. // // FUN_BAG_SLOT_DIFF (`0x004F91A0`) — __cdecl `void()` (zero // args, ends with plain `RET`). Walks linear slots `0x13..0x16` @@ -1985,13 +1988,19 @@ enum Offsets { // field-change goes through here. 3 callers, all within the // bag subsystem at `0x004F9xxx`. // - // A third site (FUN_004F8DB0 at `0x004F8DB0`) handles keyring - // updates with a `BAG_UPDATE(-2)` direct fire — __thiscall - // calling convention plus ECX-context arg make the hook - // signature awkward; deferred for now. The two below cover - // the common cases. + // FUN_BAG_KEYRING_DESC_CHANGE (`0x004F8DB0`) — __thiscall + // `unsigned(void *bagDesc, uint guidLo, void *guidHi, uint *current)`. + // Bag descriptor update callback. Computes the slot index + // from `((int)this - 0x4a8) >> 3`; for slots `0x51..0x70` (the + // keyring range) fires `BAG_UPDATE` directly. For non-keyring + // slots it delegates to `FUN_BAG_ITEM_TO_BAG`, so we'd + // double-count if we naively hooked both — the keyring branch + // is the only path here that doesn't already go through our + // other hook. Hooking is safe because the `g_pending` drain + // is idempotent (the flag stays true until WorldTick clears it). FUN_BAG_SLOT_DIFF = 0x004F91A0, FUN_BAG_ITEM_TO_BAG = 0x004F9370, + FUN_BAG_KEYRING_DESC_CHANGE = 0x004F8DB0, // World-subsystem per-frame update — runs exactly once per frame // as part of the main world tick (`FUN_00482EA0`). Only one caller // in the entire binary; deep in the rendering/world pipeline, diff --git a/src/bag/UpdateDelayed.cpp b/src/bag/UpdateDelayed.cpp index 5c99700f..aae93417 100644 --- a/src/bag/UpdateDelayed.cpp +++ b/src/bag/UpdateDelayed.cpp @@ -18,18 +18,21 @@ // // See TODO #67 for the full investigation history. // -// Architecture: two MinHooks plus one tick subscription. +// Architecture: three MinHooks plus one tick subscription. // // 1. `FUN_004F91A0` — bag-slot diff loop. Hook just sets the // `g_pending` flag. // 2. `FUN_004F9370` — item→bag resolver (most common path). Hook // just sets the `g_pending` flag. -// 3. `Tick::WorldTick::AutoSubscribe` — shared per-frame +// 3. `FUN_004F8DB0` — bag-descriptor change callback. Covers the +// keyring `BAG_UPDATE(slot)` direct-fire branch that doesn't +// route through `FUN_004F9370`. +// 4. `Tick::WorldTick::AutoSubscribe` — shared per-frame // subscription (the engine's world-subsystem update at // `FUN_0066FD50`, single-hooked in `src/tick/WorldTick.cpp`). // Our callback drains `g_pending` and fires DELAYED if set. // -// All three targets are quiet (≤2 callers each, in code regions +// All hook targets are quiet (≤3 callers each, in code regions // other DLLs don't touch). The previous attempt at this feature // hooked `FUN_FIRE_EVENT` (100+ callers, in the chat path) and a // per-frame UI dispatcher — both prime collision targets for @@ -40,16 +43,16 @@ // touches it. It runs exactly once per frame and increments // the engine's own per-frame counter at `0x00C7B2C8`, so we // know it ticks 1:1 with frames. -// - The bag-subsystem hooks (`FUN_004F91A0`, `FUN_004F9370`) live -// in the `0x004F9xxx` region none of the popular DLLs care -// about. +// - The bag-subsystem hooks (`FUN_004F91A0`, `FUN_004F9370`, +// `FUN_004F8DB0`) live in the `0x004F8xxx`/`0x004F9xxx` region +// none of the popular DLLs care about. // -// Coverage limitation: the keyring `BAG_UPDATE(-2)` path goes -// through `FUN_004F8DB0` (a `__thiscall` callback registered with -// the engine's item-descriptor change system). Hooking it cleanly -// needs the right calling-convention plumbing, deferred. Real -// inventory changes (the 95% case) go through one of the two -// bag-subsystem hooks above. +// Coverage verified by pattern-searching the `.text` for every +// caller of `FUN_FIRE_EVENT(0x148, "%d", ...)` (the BAG_UPDATE +// event ID): exactly three sites, all covered by these three +// hooks. Double-fire concern with `FUN_004F8DB0` → `FUN_004F9370` +// path is moot: `g_pending` is a bool, idempotent until the +// per-frame drain clears it. #include "Game.h" #include "Offsets.h" @@ -89,6 +92,23 @@ void __stdcall BagItemToBag_h(int guidLo, int guidHi) { g_pending = true; } +// `FUN_004F8DB0` — `__thiscall unsigned(this, guidLo, guidHi, *current)`. +// Bag descriptor change callback. Wired with `__fastcall` + dummy +// EDX to match the thiscall ABI: ECX takes `this`, EDX unused, +// stack takes the rest, callee cleans 12 bytes. Same trick +// `Frame::RegisterEvent` uses in DllMain. +using KeyringDescChange_t = unsigned(__fastcall *)(void *bagDesc, void *edx_unused, + unsigned guidLo, void *guidHi, + unsigned *currentGuid); +KeyringDescChange_t KeyringDescChange_o = nullptr; +unsigned __fastcall KeyringDescChange_h(void *bagDesc, void *edx, + unsigned guidLo, void *guidHi, + unsigned *currentGuid) { + const unsigned result = KeyringDescChange_o(bagDesc, edx, guidLo, guidHi, currentGuid); + g_pending = true; + return result; +} + // Per-frame drain. Runs at the tail of each world tick (subscribed // through `Tick::WorldTick`, which owns the single hook on // `FUN_0066FD50`). All this frame's BAG_UPDATEs have already fired @@ -113,6 +133,11 @@ static const Game::HookAutoRegister _hookItemToBag{ reinterpret_cast(&BagItemToBag_h), reinterpret_cast(&BagItemToBag_o)}; +static const Game::HookAutoRegister _hookKeyringDesc{ + Offsets::FUN_BAG_KEYRING_DESC_CHANGE, + reinterpret_cast(&KeyringDescChange_h), + reinterpret_cast(&KeyringDescChange_o)}; + static const Tick::WorldTick::AutoSubscribe _tickSub{&OnWorldTick}; } // namespace Bag::UpdateDelayed