From 0c2be52f7f47330a5d99cedd4caee9714925a35e Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Tue, 12 May 2026 03:10:08 -0500 Subject: [PATCH] C_Reputation.GetWatchedFactionData() --- README.md | 2 +- docs/API.md | 53 ++++++++++++ src/Offsets.h | 41 ++++++++++ src/faction/Info.cpp | 140 ++++++++++++++++++++++++++++++++ src/faction/StandingChanged.cpp | 59 ++++++++++---- src/faction/StandingChanged.h | 15 ++++ 6 files changed, 292 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 7ad78daf..50fbcf7f 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Full per-function reference: **[docs/API.md](docs/API.md)**. | Container | `C_Container.GetContainerItemDurability`, `C_Container.GetContainerItemID`, `C_Container.GetContainerNumFreeSlots`, `C_Container.PlayerHasHearthstone`, `C_Container.UseHearthstone` | | EquipmentSet | `C_EquipmentSet.CanUseEquipmentSets`, `C_EquipmentSet.ClearIgnoredSlotsForSave`, `C_EquipmentSet.CreateEquipmentSet`, `C_EquipmentSet.DeleteEquipmentSet`, `C_EquipmentSet.EquipmentSetContainsLockedItems`, `C_EquipmentSet.GetEquipmentSetID`, `C_EquipmentSet.GetEquipmentSetIDs`, `C_EquipmentSet.GetEquipmentSetInfo`, `C_EquipmentSet.GetIgnoredSlots`, `C_EquipmentSet.GetItemIDs`, `C_EquipmentSet.GetItemLocations`, `C_EquipmentSet.GetNumEquipmentSets`, `C_EquipmentSet.IgnoreSlotForSave`, `C_EquipmentSet.IsSlotIgnoredForSave`, `C_EquipmentSet.ModifyEquipmentSet`, `C_EquipmentSet.SaveEquipmentSet`, `C_EquipmentSet.UnignoreSlotForSave`, `C_EquipmentSet.UseEquipmentSet` (sets persist to `WTF\Account\...\ClassicAPI_EquipmentSets.txt`) | | Events | `C_EventUtils.IsEventValid` | -| Faction | `C_Reputation.SetWatchedFactionByID`, `GetFactionIDByIndex`, `GetFactionInfoByID`, `GetFactionParentID` | +| Faction | `C_Reputation.GetLastStandingChange`, `C_Reputation.GetWatchedFactionData`, `C_Reputation.SetWatchedFactionByID`, `GetFactionIDByIndex`, `GetFactionInfoByID`, `GetFactionParentID` | | GameTooltip | `GameTooltip:SetInventoryItemByID`, `GameTooltip:SetItemByID`, `GameTooltip:SetSpellByID`, `GameTooltip:SetTalentByID`, `GameTooltip:SetUnitAura` | | Hooks | `hooksecurefunc` | | Input | `IsLeftAltKeyDown`, `IsLeftControlKeyDown`, `IsLeftShiftKeyDown`, `IsModifierKeyDown`, `IsRightAltKeyDown`, `IsRightControlKeyDown`, `IsRightShiftKeyDown` | diff --git a/docs/API.md b/docs/API.md index d40af8f6..16099458 100644 --- a/docs/API.md +++ b/docs/API.md @@ -34,7 +34,9 @@ build instructions. - [`GetFactionIDByIndex(factionIndex)`](#getfactionidbyindexfactionindex) - [`GetFactionInfoByID(factionID)`](#getfactioninfobyidfactionid) - [`GetFactionParentID(factionID)`](#getfactionparentidfactionid) + - [`C_Reputation.GetWatchedFactionData()`](#c_reputationgetwatchedfactiondata) - [`C_Reputation.SetWatchedFactionByID(factionID)`](#c_reputationsetwatchedfactionbyidfactionid) + - [`C_Reputation.GetLastStandingChange()`](#c_reputationgetlaststandingchange) - [`FACTION_STANDING_CHANGED` event](#faction_standing_changed-event) - [Item](#item) - [`C_Item.IsBound(itemLocation)`](#c_itemisbounditemlocation) @@ -908,6 +910,57 @@ regardless of whether the player has rep with it. Equivalent to the function of the same name introduced in 3.0 (as the 13th return of `GetFactionInfoByID`). +### `C_Reputation.GetWatchedFactionData()` + +Returns a table describing the faction shown above the XP bar, or +`nil` if no faction is being watched. Backports the modern +struct-style accessor — vanilla's `GetWatchedFactionInfo()` returns +the same data as a 5-tuple without the factionID, which is the field +modern callers rely on most. + +Fields populated (a subset of the modern schema — only what 1.12's +engine actually has): + +| Field | Type | Notes | +|----------------------------|---------|-------------------------------------------| +| `factionID` | number | Faction.dbc record ID | +| `name` | string | Locale-applied | +| `description` | string | Locale-applied (may be `""`) | +| `reaction` | number | `1`=Hated .. `8`=Exalted | +| `currentReactionThreshold` | number | Band min standing value | +| `nextReactionThreshold` | number | Band max standing value | +| `currentStanding` | number | Current standing (`base + delta`) | +| `atWarWith` | boolean | At-war flag (rep slot flags bit 1) | +| `canToggleAtWar` | boolean | Can-toggle-at-war flag (bit 4) | +| `isWatched` | boolean | Always `true` (it IS the watched faction) | +| `isHeader` | boolean | Always `false` (factions only) | + +Fields the modern API exposes but 1.12 has no source for — +`isChild`, `isHeaderWithRep`, `isCollapsed`, `hasBonusRepGain`, +`canSetInactive`, `isAccountWide` — are omitted rather than +fabricated. Addons that need to feature-detect can check +`data.isAccountWide ~= nil`. + +```lua +local data = C_Reputation.GetWatchedFactionData() +if data then + print(string.format("%s (%d): %d / %d", + data.name, data.factionID, + data.currentStanding - data.currentReactionThreshold, + data.nextReactionThreshold - data.currentReactionThreshold)) +end +``` + +Equivalent to the function of the same name introduced in 10.x; +matches the Classic Era 1.15.x return shape for the fields it +populates. + +Implementation reads the watched `RepListID` from the player's +`+0xE68` sub-struct, indexes the per-faction rep slot array at +`0x00B73290` to recover the factionID, then derives the remaining +fields from `Faction.dbc` plus the static reaction-threshold tables +at `0x0080928C` / `0x00809290`. + ### `C_Reputation.SetWatchedFactionByID(factionID)` Sets the faction shown above the XP bar by ID. The vanilla diff --git a/src/Offsets.h b/src/Offsets.h index ef507f4d..c214cf90 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -778,6 +778,47 @@ enum Offsets { // after the engine's setter has written the new delta. FUN_REPUTATION_GET_STANDING = 0x004D6370, + // Reaction-band classifier — `__fastcall(ecx = factionID) → int`. + // Internally calls `FUN_REPUTATION_GET_STANDING` and maps the total + // through the 7-band threshold ladder (Hated..Exalted). Returns + // 0..7 (0=Hated, 7=Exalted); `Script_GetWatchedFactionInfo` pushes + // this `+ 1` to produce the 1..8 standingID Lua addons see. + FUN_REPUTATION_GET_REACTION_BAND = 0x004D63A0, + + // Reputation-list slots — the player's 64-entry per-faction rep + // store. Stride `0x10`; layout per entry: + // +0x00 i32 factionID (0 if slot unused) + // +0x04 u8 flags (bit 1 = atWar, bit 4 = canToggleAtWar) + // +0x08 i32 base standing + // +0x0C i32 delta standing + // Slot index = RepListID = Faction.dbc record `+0x04`. Indexed by + // `FUN_004D5600(factionID) → repListID`. Reverse direction is + // `FUN_004D5620(repListID) → factionID`. + VAR_PLAYER_REP_SLOTS = 0x00B73290, + REP_SLOT_STRIDE = 0x10, + OFF_REP_SLOT_FACTION_ID = 0x00, + OFF_REP_SLOT_FLAGS = 0x04, + REP_SLOT_FLAG_AT_WAR = 0x02, + REP_SLOT_FLAG_CAN_TOGGLE_AT_WAR = 0x10, + MAX_REP_SLOTS = 64, + + // Static reaction-band threshold tables, indexed by reaction band + // 0..7 (returned by `FUN_REPUTATION_GET_REACTION_BAND`). The pair + // gives the `[min, max)` standing range that defines each band — + // currentReactionThreshold / nextReactionThreshold for modern + // `C_Reputation.GetWatchedFactionData`-style table fields. + VAR_REACTION_MIN_TABLE = 0x0080928C, + VAR_REACTION_MAX_TABLE = 0x00809290, + + // `[player +0xE68] + 0x10C4` — the RepListID (rep-slot index) of + // the faction currently being watched (the one shown above the XP + // bar). Written by `FUN_PLAYER_SET_WATCHED_FACTION` after + // translating the user-supplied factionID via + // `FUN_004D5600 → RepListID`. Negative or out-of-range means "no + // watched faction", which `Script_GetWatchedFactionInfo` checks + // against `FUN_004D5620(slot)`'s return. + OFF_CGPLAYER_INFO_WATCHED_REP_LIST_ID = 0x10C4, + // `__fastcall(ecx = factionID, edx = signedDelta)`. This is the // engine's "reputation changed, fire the chat event" notify // helper — it formats the localized chat message and dispatches diff --git a/src/faction/Info.cpp b/src/faction/Info.cpp index 94ba5a9b..0914674a 100644 --- a/src/faction/Info.cpp +++ b/src/faction/Info.cpp @@ -194,6 +194,144 @@ static int __fastcall Script_GetFactionParentID(void *L) { return 1; } +// Pushes a `key = value` pair into the table at stack[-3]. Matches +// the helper shape used by `charlist/Cache.cpp`'s table builders. +void SetField(void *L, const char *key, double value) { + Game::Lua::PushString(L, key); + Game::Lua::PushNumber(L, value); + Game::Lua::SetTable(L, -3); +} + +void SetField(void *L, const char *key, const char *value) { + Game::Lua::PushString(L, key); + Game::Lua::PushString(L, value != nullptr ? value : ""); + Game::Lua::SetTable(L, -3); +} + +void SetField(void *L, const char *key, bool value) { + Game::Lua::PushString(L, key); + Game::Lua::PushBoolean(L, value ? 1 : 0); + Game::Lua::SetTable(L, -3); +} + +// Resolves the local player's CGPlayer-side info sub-struct at +// `[player + 0xE68]`. Returns nullptr if "player" isn't resolvable +// or the sub-struct is uninitialized (pre-login / glue). +const uint8_t *PlayerInfo() { + using ResolveUnitToken_t = void *(__fastcall *)(const char *token); + auto resolve = reinterpret_cast( + Offsets::FUN_RESOLVE_UNIT_TOKEN); + auto *player = static_cast(resolve("player")); + if (player == nullptr) + return nullptr; + return *reinterpret_cast( + player + Offsets::OFF_CGPLAYER_INFO); +} + +// Returns the player's RepListID for the currently-watched faction, +// or -1 if no faction is watched. RepListID is the rep-slot index in +// `VAR_PLAYER_REP_SLOTS`, NOT a factionID — the resolver below +// translates it. +int WatchedRepListID(const uint8_t *playerInfo) { + return *reinterpret_cast( + playerInfo + Offsets::OFF_CGPLAYER_INFO_WATCHED_REP_LIST_ID); +} + +// Returns the rep-slot pointer for `repListID`, or nullptr if out of +// range. Slot layout is documented at `VAR_PLAYER_REP_SLOTS` in +// `Offsets.h`. +const uint8_t *RepSlot(int repListID) { + if (repListID < 0 || repListID >= Offsets::MAX_REP_SLOTS) + return nullptr; + return reinterpret_cast( + static_cast(Offsets::VAR_PLAYER_REP_SLOTS) + + static_cast(repListID) * Offsets::REP_SLOT_STRIDE); +} + +// `C_Reputation.GetWatchedFactionData()` — returns the modern-style +// table describing the faction shown above the XP bar (or nil when no +// faction is watched). Vanilla's `GetWatchedFactionInfo` returns the +// same data as a 5-tuple without the factionID, which is the field +// modern callers rely on most. +// +// Fields populated (a subset of the modern schema — only what 1.12's +// engine actually has): +// factionID number — Faction.dbc record ID +// name string — locale-applied +// description string — locale-applied (may be "") +// reaction number — 1=Hated .. 8=Exalted +// currentReactionThreshold number — band min (rep value) +// nextReactionThreshold number — band max (rep value) +// currentStanding number — base + delta +// atWarWith boolean — rep slot flags bit 1 +// canToggleAtWar boolean — rep slot flags bit 4 +// isWatched boolean — always true (it's THE watched) +// isHeader boolean — always false (factions only) +// +// Fields the modern API also exposes but 1.12 has no source for +// (isChild, isHeaderWithRep, isCollapsed, hasBonusRepGain, +// canSetInactive, isAccountWide) are omitted rather than synthesized +// — addons can check `data.isAccountWide ~= nil` if they ever need +// to feature-detect. +static int __fastcall Script_C_Reputation_GetWatchedFactionData(void *L) { + const uint8_t *playerInfo = PlayerInfo(); + if (playerInfo == nullptr) + return 0; + const int repListID = WatchedRepListID(playerInfo); + const uint8_t *slot = RepSlot(repListID); + if (slot == nullptr) + return 0; + const int factionID = *reinterpret_cast( + slot + Offsets::OFF_REP_SLOT_FACTION_ID); + if (factionID <= 0) + return 0; + const uint8_t *record = FactionRecord(factionID); + if (record == nullptr) + return 0; + + // Reaction band — engine returns 0..7, Lua-side convention is 1..8. + using ReactionBand_t = int(__fastcall *)(int factionID); + auto reactionFn = reinterpret_cast( + Offsets::FUN_REPUTATION_GET_REACTION_BAND); + const int band = reactionFn(factionID); + const int reaction = band + 1; + + using GetStanding_t = int(__fastcall *)(int factionID); + auto standingFn = reinterpret_cast( + Offsets::FUN_REPUTATION_GET_STANDING); + const int currentStanding = standingFn(factionID); + + const int barMin = *reinterpret_cast( + static_cast(Offsets::VAR_REACTION_MIN_TABLE) + + static_cast(band) * 4); + const int barMax = *reinterpret_cast( + static_cast(Offsets::VAR_REACTION_MAX_TABLE) + + static_cast(band) * 4); + + const uint8_t flags = *(slot + Offsets::OFF_REP_SLOT_FLAGS); + const bool atWar = (flags & Offsets::REP_SLOT_FLAG_AT_WAR) != 0; + const bool canToggleAtWar = + (flags & Offsets::REP_SLOT_FLAG_CAN_TOGGLE_AT_WAR) != 0; + + const char *name = LocalizedString(record, Offsets::OFF_FACTION_NAMES); + const char *description = LocalizedString(record, Offsets::OFF_FACTION_DESCRIPTIONS); + + Game::Lua::SetTop(L, 0); + Game::Lua::NewTable(L); + SetField(L, "factionID", static_cast(factionID)); + SetField(L, "name", name); + SetField(L, "description", description); + SetField(L, "reaction", static_cast(reaction)); + SetField(L, "currentReactionThreshold", static_cast(barMin)); + SetField(L, "nextReactionThreshold", static_cast(barMax)); + SetField(L, "currentStanding", static_cast(currentStanding)); + SetField(L, "atWarWith", atWar); + SetField(L, "canToggleAtWar", canToggleAtWar); + SetField(L, "isWatched", true); + SetField(L, "isHeader", false); + return 1; +} + // `C_Reputation.SetWatchedFactionByID(factionID)` — sets the faction // shown above the XP bar by ID rather than by displayed-list index. // Modern API; vanilla 1.12 only exposes `SetWatchedFactionIndex(idx)`, @@ -229,6 +367,8 @@ static void RegisterLuaFunctions() { Game::Lua::RegisterGlobalFunction("GetFactionParentID", &Script_GetFactionParentID); Game::Lua::RegisterTableFunction("C_Reputation", "SetWatchedFactionByID", &Script_C_Reputation_SetWatchedFactionByID); + Game::Lua::RegisterTableFunction("C_Reputation", "GetWatchedFactionData", + &Script_C_Reputation_GetWatchedFactionData); } static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; diff --git a/src/faction/StandingChanged.cpp b/src/faction/StandingChanged.cpp index 8292e463..3384a431 100644 --- a/src/faction/StandingChanged.cpp +++ b/src/faction/StandingChanged.cpp @@ -52,32 +52,57 @@ constexpr const char *kEventName = "FACTION_STANDING_CHANGED"; const Event::Custom::AutoReserve _reserve{kEventName}; +// In-flight snapshot of the current rep change. Set on hook entry, +// cleared on hook exit. WoW's main thread is the only one that runs +// the rep-update SMSG handler and Lua callbacks, so a plain file +// static is sufficient — no TLS needed. +LastChange g_last{}; + +int __fastcall Script_C_Reputation_GetLastStandingChange(void *L) { + if (!g_last.valid) + return 0; + Game::Lua::PushNumber(L, static_cast(g_last.factionID)); + Game::Lua::PushNumber(L, static_cast(g_last.newStanding)); + Game::Lua::PushNumber(L, static_cast(g_last.delta)); + return 3; +} + +void RegisterLuaFunctions() { + Game::Lua::RegisterTableFunction("C_Reputation", "GetLastStandingChange", + &Script_C_Reputation_GetLastStandingChange); +} + +const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; + } // namespace FireNotify_t FireNotify_o = nullptr; +const LastChange &Last() { return g_last; } + void __fastcall FireNotify_h(int factionID, int delta) { - // Forward to the engine first so the chat event fires before our - // custom event observers can react — same order as if we weren't - // here. Also ensures the standing value is fully settled (the - // setter at 0x004D6330 wrote it before reaching the call site, but - // following original-first keeps observer ordering predictable - // even if a future engine change introduces post-write fixup). - FireNotify_o(factionID, delta); - - const int slot = Event::Custom::Lookup(kEventName); - if (slot < 0) - return; - - // Live total = base + currentDelta from the per-slot rep storage. - // The helper returns 0 for factionIDs not in the player's rep list - // — but we shouldn't see those here, since the engine only calls - // this notify after a successful per-slot update. + // The engine's setter at 0x004D6330 has already written the new + // standing into the per-slot storage by the time we get here, so + // `FUN_REPUTATION_GET_STANDING` returns the post-change total. We + // capture it before forwarding so anything reading + // `g_last` from inside the engine's chat dispatch (and from our + // own `FACTION_STANDING_CHANGED` observers below) sees a + // consistent snapshot. auto getStanding = reinterpret_cast( Offsets::FUN_REPUTATION_GET_STANDING); const int newStanding = getStanding(factionID); + g_last = {true, factionID, newStanding, delta}; - Event::Custom::Fire_DDD(slot, factionID, newStanding, delta); + // Forward to the engine first so its CHAT_MSG_COMBAT_FACTION_CHANGE + // fires before our FACTION_STANDING_CHANGED — preserves ordering + // any addon relying on the chat event firing first would expect. + FireNotify_o(factionID, delta); + + const int slot = Event::Custom::Lookup(kEventName); + if (slot >= 0) + Event::Custom::Fire_DDD(slot, factionID, newStanding, delta); + + g_last = {}; } static const Game::HookAutoRegister _hookreg{ diff --git a/src/faction/StandingChanged.h b/src/faction/StandingChanged.h index 1680fa2f..d45d22a6 100644 --- a/src/faction/StandingChanged.h +++ b/src/faction/StandingChanged.h @@ -27,4 +27,19 @@ using FireNotify_t = void(__fastcall *)(int factionID, int delta); extern FireNotify_t FireNotify_o; void __fastcall FireNotify_h(int factionID, int delta); +// Snapshot of the currently-firing rep change, valid only during the +// call stack rooted at `FireNotify_h`. `C_Reputation.GetLastStandingChange` +// reads this — observers of the engine's CHAT_MSG_COMBAT_FACTION_CHANGE +// (fired by `FireNotify_o`) and of our own `FACTION_STANDING_CHANGED` +// (fired immediately after) see `valid == true`; everything outside +// that window sees `valid == false` and the Lua getter returns nil. +struct LastChange { + bool valid; + int factionID; + int newStanding; + int delta; +}; + +const LastChange &Last(); + } // namespace Faction::StandingChanged