mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 08:00:42 +00:00
C_UnitAuras.* returned nothing for a party/raid member with no live
CGUnit (different map / far out of range): every path bailed on the
null unit, and the only fallback was the Aura::Source SMSG_SPELL_GO
cache, which needs the object to exist.
The server keeps transmitting each group member's current aura spell
IDs via SMSG_PARTY_MEMBER_STATS (GROUP_UPDATE_FLAG_AURAS / _NEGATIVE)
and the client stashes them in the group-member roster structs -- the
same source the built-in UnitBuff/UnitDebuff read out of range. Surface
them.
- Group::MemberStats::AuraArray(guid) mirrors the engine's own
out-of-range fallback in Script_UnitBuff/Script_UnitDebuff exactly
(party stats block +0x1a gated on the online flag, else raid slot
+0x64; both 48x u16).
- Factor aura-table enrichment (caster/expiration/duration) into a
single descriptor-independent PushEnriched core; Push and
PushFromCache now delegate to it.
- New group-array enumerate branch (PushNthGroupAura /
PushGroupAuraBySpellID / ...BySpellName / AppendGroupAuras), applying
the engine's per-slot visibility predicate and merging any
caster/timing the SpellGo cache observed.
- Api.cpp routes unit==null to the group array for every C_UnitAuras
entry point. Safe: the token resolver never raises (returns 0), and
the GUID lookup only runs after ResolveUnit already accepted the
token.
Spell IDs only (no stacks; IDs are u16-truncated) -- same limitations
as UnitBuff. Verified in-game: GetUnitAuras("raid1") cross-map returns
Power Word: Fortitude (10938), matching UnitBuff.
780 lines
32 KiB
C++
780 lines
32 KiB
C++
// This file is part of ClassicAPI.
|
|
//
|
|
// ClassicAPI is free software: you can redistribute it and/or modify it under the terms
|
|
// of the GNU General Public License as published by the Free Software Foundation, either
|
|
// version 3 of the License, or (at your option) any later version.
|
|
//
|
|
// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
// PURPOSE. See the GNU General Public License for more details.
|
|
|
|
// Aura-table primitives. See `Data.h` for the contract; see `Api.cpp`
|
|
// for the Lua-binding surface that consumes these.
|
|
|
|
#include "Data.h"
|
|
|
|
#include "Game.h"
|
|
#include "Offsets.h"
|
|
#include "aura/Source.h"
|
|
#include "dbc/Lookup.h"
|
|
#include "group/MemberStats.h"
|
|
#include "guid/Guid.h"
|
|
#include "unit/Identity.h"
|
|
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
|
|
namespace Aura::Data {
|
|
|
|
namespace {
|
|
|
|
// Spell.dbc record field offsets used locally. `spell/Info.cpp` has
|
|
// the same set of locals for `Script_GetSpellInfo`; promoting them
|
|
// to `Offsets.h` is a future refactor — for now we mirror its
|
|
// convention so both files stay in sync.
|
|
constexpr int OFF_SPELL_ICON_ID = 0x1D4;
|
|
constexpr int OFF_SPELLICON_PATH = 0x04;
|
|
|
|
const uint8_t *Descriptor(const uint8_t *unit) {
|
|
if (unit == nullptr)
|
|
return nullptr;
|
|
return *reinterpret_cast<const uint8_t *const *>(
|
|
unit + Offsets::OFF_CGUNIT_OBJECT_FIELDS);
|
|
}
|
|
|
|
// Keys into the `Aura::Source` caster/expiration cache. Thin alias for the
|
|
// shared `Unit::Identity::GuidForObject` so the lookup here and the
|
|
// OnAuraAdded hook in `Aura::Source` read the GUID the same way.
|
|
uint64_t UnitGuid(const uint8_t *unit) {
|
|
return Unit::Identity::GuidForObject(unit);
|
|
}
|
|
|
|
const uint8_t *SpellRecord(uint32_t spellID) {
|
|
return DBC::Record(Offsets::VAR_SPELL_RECORDS,
|
|
Offsets::VAR_SPELL_RECORD_COUNT, spellID);
|
|
}
|
|
|
|
int LocaleIndex() {
|
|
return *reinterpret_cast<const int *>(
|
|
static_cast<uintptr_t>(Offsets::VAR_LOCALE_INDEX));
|
|
}
|
|
|
|
const char *LocalizedSpellName(const uint8_t *spellRecord) {
|
|
if (spellRecord == nullptr)
|
|
return nullptr;
|
|
const auto *names = reinterpret_cast<const char *const *>(
|
|
spellRecord + Offsets::OFF_SPELL_NAMES);
|
|
const char *s = names[LocaleIndex()];
|
|
return (s == nullptr || *s == '\0') ? nullptr : s;
|
|
}
|
|
|
|
const char *SpellIconPath(const uint8_t *spellRecord) {
|
|
if (spellRecord == nullptr)
|
|
return nullptr;
|
|
const int iconID = *reinterpret_cast<const int *>(
|
|
spellRecord + OFF_SPELL_ICON_ID);
|
|
if (iconID <= 0)
|
|
return nullptr;
|
|
return DBC::StringField(Offsets::VAR_SPELL_ICON_RECORDS,
|
|
Offsets::VAR_SPELL_ICON_COUNT,
|
|
static_cast<uint32_t>(iconID),
|
|
OFF_SPELLICON_PATH);
|
|
}
|
|
|
|
using ResolveUnitToken_t = void *(__fastcall *)(const char *);
|
|
|
|
// Returns the local player's CGUnit pointer, or nullptr pre-login.
|
|
const uint8_t *LocalPlayer() {
|
|
auto fn = reinterpret_cast<ResolveUnitToken_t>(
|
|
static_cast<uintptr_t>(Offsets::FUN_RESOLVE_UNIT_TOKEN));
|
|
return static_cast<const uint8_t *>(fn("player"));
|
|
}
|
|
|
|
// Looks up the player-buff-table entry for a given spellID. The table
|
|
// at `VAR_PLAYER_BUFF_TABLE` mirrors the local player's auras with
|
|
// timing data (which UNIT_FIELD_AURA lacks for all units). Returns
|
|
// nullptr when the spell isn't in any populated entry.
|
|
//
|
|
// Only meaningful when the caller is building data for the local
|
|
// player — every entry here belongs to "player". Caller is expected
|
|
// to gate on that.
|
|
const uint8_t *FindPlayerBuffEntry(uint32_t spellID) {
|
|
auto *base = reinterpret_cast<const uint8_t *>(
|
|
static_cast<uintptr_t>(Offsets::VAR_PLAYER_BUFF_TABLE));
|
|
for (int i = 0; i < Offsets::PLAYER_BUFF_TABLE_COUNT; ++i) {
|
|
const uint8_t *entry = base + i * Offsets::PLAYER_BUFF_ENTRY_STRIDE;
|
|
const int slotCode = *reinterpret_cast<const int *>(
|
|
entry + Offsets::OFF_PLAYER_BUFF_SLOT_CODE);
|
|
if (slotCode < 0)
|
|
continue;
|
|
const uint32_t entrySpell = *reinterpret_cast<const uint32_t *>(
|
|
entry + Offsets::OFF_PLAYER_BUFF_SPELL_ID);
|
|
if (entrySpell == spellID)
|
|
return entry;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
// Reads the absolute expiration timestamp (in seconds, GetTime-epoch)
|
|
// for a player-buff entry, or 0 if the entry has expired / has no
|
|
// timing. The engine stores ms-since-tickcount-epoch which matches
|
|
// what Lua's `GetTime()` returns (engineMs * 0.001), so converting
|
|
// `expirationMs * 0.001` lands directly on the same timeline addons
|
|
// compare against on the Lua side. No epoch reconciliation needed.
|
|
//
|
|
// **Read as `uint32_t`**: GetTickCount overflows `INT_MAX` at ~24.86
|
|
// days of system uptime. A signed read flips negative past that
|
|
// point, which broke a `<= 0` early-bail that was supposed to catch
|
|
// only the "no timing" (0) sentinel. The engine's own arithmetic
|
|
// works on the unsigned tick because both sides of `expiration -
|
|
// currentMs` wrap together.
|
|
double PlayerBuffExpirationSeconds(const uint8_t *entry) {
|
|
if (entry == nullptr)
|
|
return 0.0;
|
|
const int slotCode = *reinterpret_cast<const int *>(
|
|
entry + Offsets::OFF_PLAYER_BUFF_SLOT_CODE);
|
|
if (slotCode < 0)
|
|
return 0.0;
|
|
auto *expirationTable = reinterpret_cast<const uint32_t *>(
|
|
static_cast<uintptr_t>(Offsets::VAR_PLAYER_BUFF_EXPIRATION_TABLE));
|
|
const uint32_t expirationMs = expirationTable[slotCode];
|
|
if (expirationMs == 0)
|
|
return 0.0;
|
|
return static_cast<double>(expirationMs) * 0.001;
|
|
}
|
|
|
|
// Computes the spell's base duration in seconds via Spell.dbc →
|
|
// SpellDuration.dbc lookup with level scaling. Returns 0 for spells
|
|
// without a real duration (the sentinel `base < 0 && perLevel == 0`
|
|
// path the engine uses for infinite auras like passives, paladin
|
|
// auras, racials).
|
|
//
|
|
// Doesn't include talent / glyph / aura-extension modifiers — those
|
|
// are caster-side and the engine applies them when computing the
|
|
// expiration timestamp. So `expirationTime - GetTime()` reflects the
|
|
// *true* remaining time; this `duration` is the base value modern
|
|
// addons use for "X / Y" timer rendering.
|
|
double SpellBaseDurationSeconds(uint32_t spellID, int unitLevel) {
|
|
const uint8_t *spell = SpellRecord(spellID);
|
|
if (spell == nullptr)
|
|
return 0.0;
|
|
const int durIdx = *reinterpret_cast<const int *>(
|
|
spell + Offsets::OFF_SPELL_DURATION_INDEX);
|
|
if (durIdx <= 0)
|
|
return 0.0;
|
|
const uint8_t *durRec = DBC::Record(Offsets::VAR_SPELLDURATION_RECORDS,
|
|
Offsets::VAR_SPELLDURATION_COUNT,
|
|
static_cast<uint32_t>(durIdx));
|
|
if (durRec == nullptr)
|
|
return 0.0;
|
|
const int base = *reinterpret_cast<const int *>(
|
|
durRec + Offsets::OFF_SPELLDURATION_BASE_MS);
|
|
const int perLevel = *reinterpret_cast<const int *>(
|
|
durRec + Offsets::OFF_SPELLDURATION_PER_LEVEL_MS);
|
|
const int maxMs = *reinterpret_cast<const int *>(
|
|
durRec + Offsets::OFF_SPELLDURATION_MAX_MS);
|
|
// Engine's "infinite duration" sentinel — matches the check in
|
|
// `FUN_004E44B0`'s "no real duration" branch at `0x004e455c`.
|
|
if (base < 0 && perLevel < 1)
|
|
return 0.0;
|
|
const int spellBaseLevel = *reinterpret_cast<const int *>(
|
|
spell + Offsets::OFF_SPELL_BASE_LEVEL);
|
|
int effLevel = unitLevel;
|
|
if (effLevel < spellBaseLevel)
|
|
effLevel = spellBaseLevel;
|
|
int durationMs = (effLevel - spellBaseLevel) * perLevel + base;
|
|
if (maxMs > 0 && durationMs > maxMs)
|
|
durationMs = maxMs;
|
|
if (durationMs <= 0)
|
|
return 0.0;
|
|
return static_cast<double>(durationMs) * 0.001;
|
|
}
|
|
|
|
// Reads the player's level from UNIT_FIELD_LEVEL. Returns 0 when
|
|
// unresolved (pre-login, no descriptor) — caller should treat that
|
|
// as "skip level scaling".
|
|
int PlayerLevel(const uint8_t *player) {
|
|
auto *desc = Descriptor(player);
|
|
if (desc == nullptr)
|
|
return 0;
|
|
return *reinterpret_cast<const int *>(
|
|
desc + Offsets::OFF_UNIT_FIELD_LEVEL);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
uint32_t ReadSpellID(const uint8_t *unit, int slot) {
|
|
if (slot < 0 || slot >= Offsets::UNIT_AURA_TOTAL)
|
|
return 0;
|
|
auto *desc = Descriptor(unit);
|
|
if (desc == nullptr)
|
|
return 0;
|
|
return *reinterpret_cast<const uint32_t *>(
|
|
desc + Offsets::OFF_UNIT_FIELD_AURA + slot * 4);
|
|
}
|
|
|
|
bool IsSlotPopulated(const uint8_t *unit, int slot) {
|
|
if (slot < 0 || slot >= Offsets::UNIT_AURA_TOTAL)
|
|
return false;
|
|
auto *desc = Descriptor(unit);
|
|
if (desc == nullptr)
|
|
return false;
|
|
const uint32_t spellID = *reinterpret_cast<const uint32_t *>(
|
|
desc + Offsets::OFF_UNIT_FIELD_AURA + slot * 4);
|
|
if (spellID == 0)
|
|
return false;
|
|
const uint8_t byte = *(desc + Offsets::OFF_UNIT_FIELD_AURAFLAGS + slot / 2);
|
|
const uint8_t nibble = (byte >> ((slot & 1) * 4)) & 0xF;
|
|
if ((nibble & Offsets::UNIT_AURA_VISIBLE_MASK) == 0)
|
|
return false;
|
|
return IsVisible(SpellRecord(spellID));
|
|
}
|
|
|
|
bool IsPlayerCast(const uint8_t *unit, int slot) {
|
|
const uint32_t spellID = ReadSpellID(unit, slot);
|
|
if (spellID == 0)
|
|
return false;
|
|
uint64_t casterGuid = 0;
|
|
uint32_t expMs = 0;
|
|
uint32_t durMs = 0;
|
|
if (!Aura::Source::Get(UnitGuid(unit), spellID, &casterGuid, &expMs, &durMs))
|
|
return false;
|
|
return casterGuid != 0 && casterGuid == Unit::Identity::PlayerGuid();
|
|
}
|
|
|
|
int FindNthSlot(const uint8_t *unit, int oneBasedIndex, Filter filter,
|
|
bool playerOnly) {
|
|
if (unit == nullptr || oneBasedIndex < 1)
|
|
return -1;
|
|
const int start = (filter == Filter::Harmful)
|
|
? Offsets::UNIT_AURA_BUFF_COUNT
|
|
: 0;
|
|
const int end = (filter == Filter::Harmful)
|
|
? Offsets::UNIT_AURA_TOTAL
|
|
: Offsets::UNIT_AURA_BUFF_COUNT;
|
|
int matches = 0;
|
|
for (int slot = start; slot < end; ++slot) {
|
|
if (!IsSlotPopulated(unit, slot))
|
|
continue;
|
|
if (playerOnly && !IsPlayerCast(unit, slot))
|
|
continue;
|
|
if (++matches == oneBasedIndex)
|
|
return slot;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int FindSlotBySpellID(const uint8_t *unit, uint32_t spellID,
|
|
const Filter *filter, bool playerOnly) {
|
|
if (unit == nullptr || spellID == 0)
|
|
return -1;
|
|
const int start = (filter != nullptr && *filter == Filter::Harmful)
|
|
? Offsets::UNIT_AURA_BUFF_COUNT
|
|
: 0;
|
|
const int end = (filter != nullptr && *filter == Filter::Helpful)
|
|
? Offsets::UNIT_AURA_BUFF_COUNT
|
|
: Offsets::UNIT_AURA_TOTAL;
|
|
for (int slot = start; slot < end; ++slot) {
|
|
if (!IsSlotPopulated(unit, slot))
|
|
continue;
|
|
if (ReadSpellID(unit, slot) != spellID)
|
|
continue;
|
|
if (playerOnly && !IsPlayerCast(unit, slot))
|
|
continue;
|
|
return slot;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int FindSlotBySpellName(const uint8_t *unit, const char *spellName,
|
|
const Filter *filter, bool playerOnly) {
|
|
if (unit == nullptr || spellName == nullptr || *spellName == '\0')
|
|
return -1;
|
|
const int start = (filter != nullptr && *filter == Filter::Harmful)
|
|
? Offsets::UNIT_AURA_BUFF_COUNT
|
|
: 0;
|
|
const int end = (filter != nullptr && *filter == Filter::Helpful)
|
|
? Offsets::UNIT_AURA_BUFF_COUNT
|
|
: Offsets::UNIT_AURA_TOTAL;
|
|
for (int slot = start; slot < end; ++slot) {
|
|
if (!IsSlotPopulated(unit, slot))
|
|
continue;
|
|
const char *name = LocalizedSpellName(SpellRecord(ReadSpellID(unit, slot)));
|
|
if (name == nullptr || std::strcmp(name, spellName) != 0)
|
|
continue;
|
|
if (playerOnly && !IsPlayerCast(unit, slot))
|
|
continue;
|
|
return slot;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int ReadStacks(const uint8_t *unit, int slot) {
|
|
if (slot < 0 || slot >= Offsets::UNIT_AURA_TOTAL)
|
|
return 0;
|
|
auto *desc = Descriptor(unit);
|
|
if (desc == nullptr)
|
|
return 0;
|
|
const uint8_t stored = *reinterpret_cast<const uint8_t *>(
|
|
desc + Offsets::OFF_UNIT_FIELD_AURAAPPLICATIONS + slot);
|
|
return static_cast<int>(stored) + 1;
|
|
}
|
|
|
|
bool IsVisible(const uint8_t *spellRecord) {
|
|
if (spellRecord == nullptr)
|
|
return false;
|
|
using Fn = char(__fastcall *)(const uint8_t *);
|
|
auto fn = reinterpret_cast<Fn>(
|
|
static_cast<uintptr_t>(Offsets::FUN_SPELL_IS_VISIBLE_AURA));
|
|
return fn(spellRecord) != 0;
|
|
}
|
|
|
|
const char *DispelName(uint32_t dispelTypeID) {
|
|
if (dispelTypeID == 0)
|
|
return nullptr;
|
|
const int count = *reinterpret_cast<const int *>(
|
|
static_cast<uintptr_t>(Offsets::VAR_SPELLDISPEL_COUNT));
|
|
if (static_cast<int>(dispelTypeID) > count)
|
|
return nullptr;
|
|
auto *records = *reinterpret_cast<const uint8_t *const *const *>(
|
|
static_cast<uintptr_t>(Offsets::VAR_SPELLDISPEL_RECORDS));
|
|
if (records == nullptr)
|
|
return nullptr;
|
|
const uint8_t *record = records[dispelTypeID];
|
|
if (record == nullptr)
|
|
return nullptr;
|
|
if (*reinterpret_cast<const int *>(
|
|
record + Offsets::OFF_SPELLDISPEL_HAS_NAME) == 0)
|
|
return nullptr;
|
|
const char *name = *reinterpret_cast<const char *const *>(
|
|
record + Offsets::OFF_SPELLDISPEL_NAME);
|
|
return (name == nullptr || *name == '\0') ? nullptr : name;
|
|
}
|
|
|
|
// Writes the full modern AuraData table on top of the Lua stack from
|
|
// already-resolved values. Shared by the descriptor path (`Push`) and the
|
|
// cache-fallback path (`PushFromCache`). Net stack effect: +1 (the table).
|
|
static void BuildTable(void *L, uint32_t spellID, int applications,
|
|
bool isHelpful, double duration, double expirationTime,
|
|
uint64_t casterGuid) {
|
|
const uint8_t *spellRecord = SpellRecord(spellID);
|
|
|
|
uint32_t dispelTypeID = 0;
|
|
if (spellRecord != nullptr) {
|
|
dispelTypeID = *reinterpret_cast<const uint32_t *>(
|
|
spellRecord + Offsets::OFF_SPELL_DISPEL_TYPE);
|
|
}
|
|
|
|
const char *name = LocalizedSpellName(spellRecord);
|
|
const char *icon = SpellIconPath(spellRecord);
|
|
const char *dispel = DispelName(dispelTypeID);
|
|
|
|
Game::Lua::NewTable(L);
|
|
|
|
Game::Lua::SetFieldString(L, "name", name);
|
|
Game::Lua::SetFieldString(L, "icon", icon);
|
|
Game::Lua::SetFieldNumber(L, "applications",
|
|
static_cast<double>(applications));
|
|
Game::Lua::SetFieldNumber(L, "spellId", static_cast<double>(spellID));
|
|
Game::Lua::SetFieldString(L, "dispelName", dispel);
|
|
Game::Lua::SetFieldBool(L, "isHelpful", isHelpful);
|
|
Game::Lua::SetFieldBool(L, "isHarmful", !isHelpful);
|
|
Game::Lua::SetFieldNumber(L, "duration", duration);
|
|
Game::Lua::SetFieldNumber(L, "expirationTime", expirationTime);
|
|
|
|
if (casterGuid != 0) {
|
|
// `sourceUnit` is a token (nil when the caster maps to no current
|
|
// token); `sourceGUID` is the raw "0x..." GUID and is set whenever we
|
|
// have a caster — a ClassicAPI extension (not in retail AuraData) that
|
|
// survives the caster leaving token range and doubles as a unit token
|
|
// under SuperWoW.
|
|
char tokenBuf[32];
|
|
char guidBuf[Guid::STRING_SIZE];
|
|
const char *sourceUnit = Unit::Identity::TokenFromGUID(
|
|
casterGuid, tokenBuf, sizeof tokenBuf);
|
|
const char *sourceGUID =
|
|
Guid::FormatAsString(casterGuid, guidBuf, sizeof guidBuf);
|
|
if (sourceUnit != nullptr)
|
|
Game::Lua::SetFieldString(L, "sourceUnit", sourceUnit);
|
|
if (sourceGUID != nullptr)
|
|
Game::Lua::SetFieldString(L, "sourceGUID", sourceGUID);
|
|
}
|
|
|
|
Game::Lua::SetFieldNumber(L, "charges", 0);
|
|
Game::Lua::SetFieldNumber(L, "maxCharges", 0);
|
|
Game::Lua::SetFieldNumber(L, "timeMod", 1);
|
|
|
|
// Boolean fields whose modern semantics don't apply in vanilla.
|
|
Game::Lua::SetFieldBool(L, "isStealable", false);
|
|
Game::Lua::SetFieldBool(L, "isBossAura", false);
|
|
Game::Lua::SetFieldBool(L, "isFromPlayerOrPlayerPet", false);
|
|
Game::Lua::SetFieldBool(L, "isNameplateOnly", false);
|
|
Game::Lua::SetFieldBool(L, "nameplateShowAll", false);
|
|
Game::Lua::SetFieldBool(L, "nameplateShowPersonal", false);
|
|
Game::Lua::SetFieldBool(L, "canApplyAura", false);
|
|
Game::Lua::SetFieldBool(L, "shouldConsolidate", false);
|
|
Game::Lua::SetFieldBool(L, "isRaid", false);
|
|
|
|
// `auraInstanceID` and `points` are deliberately omitted — modern returns
|
|
// nil for those when they don't apply, and Lua reading a missing key
|
|
// yields nil, so the table doesn't need an explicit entry.
|
|
}
|
|
|
|
// Enriches a bare `(guid, spellID)` into a full AuraData table — the shared
|
|
// core behind every push path (descriptor, cache fallback, group-array). No
|
|
// dependence on a live descriptor: the descriptor path passes what it read
|
|
// (stacks, level, is-this-the-player), the others pass their known-or-default
|
|
// values.
|
|
//
|
|
// Timing rules (identical across paths): `duration` is the Spell.dbc →
|
|
// SpellDuration.dbc base scaled by `unitLevel` (0 skips scaling); talent/glyph
|
|
// duration modifiers are baked into the caster-side expiration, so
|
|
// `expirationTime - GetTime()` is the true remaining time even when `duration`
|
|
// omits the boost. `expirationTime` for the local player comes from the engine
|
|
// buff table (gated on `isPlayer`); for everyone else it, the caster, and the
|
|
// applied (caster-modified) duration come from the `Aura::Source` SMSG_SPELL_GO
|
|
// cache when it observed the cast — a miss leaves the modern-truthful defaults
|
|
// (expiration 0, no sourceUnit/GUID).
|
|
static void PushEnriched(void *L, uint64_t guid, uint32_t spellID,
|
|
bool isHelpful, int applications, int unitLevel,
|
|
bool isPlayer) {
|
|
double duration = 0.0;
|
|
double expirationTime = 0.0;
|
|
uint64_t casterGuid = 0;
|
|
if (spellID != 0)
|
|
duration = SpellBaseDurationSeconds(spellID, unitLevel);
|
|
if (isPlayer && spellID != 0) {
|
|
const uint8_t *entry = FindPlayerBuffEntry(spellID);
|
|
if (entry != nullptr)
|
|
expirationTime = PlayerBuffExpirationSeconds(entry);
|
|
}
|
|
if (spellID != 0 && guid != 0) {
|
|
uint64_t c = 0;
|
|
uint32_t expMs = 0;
|
|
uint32_t durMs = 0;
|
|
if (Aura::Source::Get(guid, spellID, &c, &expMs, &durMs)) {
|
|
if (expirationTime == 0.0 && expMs != 0)
|
|
expirationTime = static_cast<double>(expMs) * 0.001;
|
|
if (durMs != 0)
|
|
duration = static_cast<double>(durMs) * 0.001;
|
|
casterGuid = c;
|
|
}
|
|
}
|
|
BuildTable(L, spellID, applications, isHelpful, duration, expirationTime,
|
|
casterGuid);
|
|
}
|
|
|
|
void Push(void *L, const uint8_t *unit, int slot) {
|
|
const uint32_t spellID = ReadSpellID(unit, slot);
|
|
const bool isHelpful = slot < Offsets::UNIT_AURA_BUFF_COUNT;
|
|
const int unitLevel = (unit != nullptr) ? PlayerLevel(unit) : 0;
|
|
PushEnriched(L, UnitGuid(unit), spellID, isHelpful, ReadStacks(unit, slot),
|
|
unitLevel, unit != nullptr && unit == LocalPlayer());
|
|
}
|
|
|
|
namespace {
|
|
|
|
// Builds an AuraData table from an `Aura::Source` cache entry — the fallback
|
|
// when the descriptor has dropped the aura's slot. Stack count isn't broadcast
|
|
// in SMSG_SPELL_GO, so `applications` defaults to 1. `duration` uses the
|
|
// applied (caster-modified) ms when known, else the Spell.dbc base.
|
|
void PushFromCache(void *L, const uint8_t *unit,
|
|
const Aura::Source::CachedAura &c, bool isHelpful) {
|
|
// `PushEnriched` re-reads the same cache entry `c` came from (by
|
|
// guid+spellID) to fill caster / expiration / applied duration, so the
|
|
// result is identical to reading `c`'s fields directly. Stacks aren't in
|
|
// SMSG_SPELL_GO, so `applications` is 1.
|
|
const int unitLevel = (unit != nullptr) ? PlayerLevel(unit) : 0;
|
|
PushEnriched(L, UnitGuid(unit), c.spellId, isHelpful, 1, unitLevel, false);
|
|
}
|
|
|
|
// Reconciles the `Aura::Source` cache against `unit`'s descriptor: when the
|
|
// descriptor holds at least one raw aura the unit is in view and fully synced,
|
|
// so it's authoritative — any cached entry for the unit whose spell isn't in
|
|
// the array was removed while we couldn't see it (e.g. cancelled out of range,
|
|
// so no OnAuraRemoved reached us) and must be dropped before the fallback
|
|
// resurfaces it as a phantom (an enumerated aura whose SetUnitAura tooltip is
|
|
// empty). Reads the RAW slots (not IsSlotPopulated): a spell that's present
|
|
// but flag-hidden is still legitimately on the unit. Skips when the array is
|
|
// empty — that can't be told apart from an out-of-range descriptor drop, and
|
|
// reconciling then would wipe still-valid out-of-range entries.
|
|
void ReconcileCache(const uint8_t *unit) {
|
|
if (unit == nullptr)
|
|
return;
|
|
uint32_t present[Offsets::UNIT_AURA_TOTAL];
|
|
int n = 0;
|
|
for (int slot = 0; slot < Offsets::UNIT_AURA_TOTAL; ++slot) {
|
|
const uint32_t id = ReadSpellID(unit, slot);
|
|
if (id != 0)
|
|
present[n++] = id;
|
|
}
|
|
if (n == 0)
|
|
return;
|
|
Aura::Source::EvictAbsent(UnitGuid(unit), present, n);
|
|
}
|
|
|
|
// True if `unit`'s descriptor currently exposes any visible aura in either
|
|
// range. The cache fallback is only trustworthy when the descriptor has been
|
|
// wholesale-cleared: the two states it exists for — rogue stealth and party
|
|
// range fluctuation — clear the *entire* aura array at once. If the unit shows
|
|
// even one live aura, the descriptor is authoritative, so a cache entry the
|
|
// descriptor lacks is a genuinely-gone aura we never saw removed (e.g. a
|
|
// no-duration debuff — Ghost/Spirit — on a unit that died and resurrected out
|
|
// of our visibility, so no OnAuraRemoved fired and its infinite entry never
|
|
// expired). Surfacing it then produces a stuck phantom.
|
|
bool DescriptorHasVisibleAura(const uint8_t *unit) {
|
|
if (unit == nullptr)
|
|
return false;
|
|
for (int slot = 0; slot < Offsets::UNIT_AURA_TOTAL; ++slot) {
|
|
if (IsSlotPopulated(unit, slot))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Counts populated descriptor slots matching `filter` (and `playerOnly`).
|
|
int CountSlots(const uint8_t *unit, Filter filter, bool playerOnly) {
|
|
if (unit == nullptr)
|
|
return 0;
|
|
const int start = (filter == Filter::Harmful)
|
|
? Offsets::UNIT_AURA_BUFF_COUNT
|
|
: 0;
|
|
const int end = (filter == Filter::Harmful) ? Offsets::UNIT_AURA_TOTAL
|
|
: Offsets::UNIT_AURA_BUFF_COUNT;
|
|
int n = 0;
|
|
for (int slot = start; slot < end; ++slot) {
|
|
if (!IsSlotPopulated(unit, slot))
|
|
continue;
|
|
if (playerOnly && !IsPlayerCast(unit, slot))
|
|
continue;
|
|
++n;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
constexpr int kFallbackMax = Offsets::UNIT_AURA_TOTAL;
|
|
|
|
// Whether cache entry `c` should be surfaced as a fallback for `unit` under
|
|
// `filter`/`playerOnly`: the spell must be a user-visible aura, it must not
|
|
// already be in a populated descriptor slot (no double-listing the same live
|
|
// aura) and, when player-filtered, must have been cast by us. Superseded /
|
|
// dispelled auras are kept out of the cache by `Aura::Source`'s removal
|
|
// eviction, not filtered here.
|
|
bool FallbackEligible(const uint8_t *unit, const Aura::Source::CachedAura &c,
|
|
Filter filter, bool playerOnly) {
|
|
if (!IsVisible(SpellRecord(c.spellId)))
|
|
return false; // hidden/internal aura (e.g. SPELL_ATTR_HIDDEN_CLIENTSIDE):
|
|
// the descriptor path drops these via IsSlotPopulated, so
|
|
// the cache path must apply the same engine predicate or
|
|
// they leak through as tooltip-less phantom buffs.
|
|
if (FindSlotBySpellID(unit, c.spellId, &filter, false) >= 0)
|
|
return false; // still in the descriptor — returned via the slot path
|
|
if (playerOnly && c.casterGuid != Unit::Identity::PlayerGuid())
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool PushNthCacheFallback(void *L, const uint8_t *unit, int oneBasedIndex,
|
|
Filter filter, bool playerOnly) {
|
|
if (unit == nullptr)
|
|
return false;
|
|
ReconcileCache(unit); // drop entries the (synced) descriptor contradicts
|
|
if (DescriptorHasVisibleAura(unit))
|
|
return false; // descriptor authoritative — see DescriptorHasVisibleAura
|
|
// The descriptor held `descCount` matches; the caller already found fewer
|
|
// than `oneBasedIndex` there, so the cache supplies index
|
|
// `oneBasedIndex - descCount` (1-based) of the entries it didn't.
|
|
const int target = oneBasedIndex - CountSlots(unit, filter, playerOnly);
|
|
if (target < 1)
|
|
return false;
|
|
const uint64_t guid = UnitGuid(unit);
|
|
if (guid == 0)
|
|
return false;
|
|
Aura::Source::CachedAura buf[kFallbackMax];
|
|
const int n = Aura::Source::Enumerate(
|
|
guid, filter == Filter::Harmful, buf, kFallbackMax);
|
|
int seen = 0;
|
|
for (int i = 0; i < n; ++i) {
|
|
if (!FallbackEligible(unit, buf[i], filter, playerOnly))
|
|
continue;
|
|
if (++seen == target) {
|
|
PushFromCache(L, unit, buf[i], filter == Filter::Helpful);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void AppendCacheFallbacks(void *L, const uint8_t *unit, Filter filter,
|
|
bool playerOnly, int outerIdx, int &nextKey) {
|
|
if (unit == nullptr)
|
|
return;
|
|
ReconcileCache(unit); // drop entries the (synced) descriptor contradicts
|
|
if (DescriptorHasVisibleAura(unit))
|
|
return; // descriptor authoritative — see DescriptorHasVisibleAura
|
|
const uint64_t guid = UnitGuid(unit);
|
|
if (guid == 0)
|
|
return;
|
|
Aura::Source::CachedAura buf[kFallbackMax];
|
|
const int n = Aura::Source::Enumerate(
|
|
guid, filter == Filter::Harmful, buf, kFallbackMax);
|
|
for (int i = 0; i < n; ++i) {
|
|
if (!FallbackEligible(unit, buf[i], filter, playerOnly))
|
|
continue;
|
|
Game::Lua::PushNumber(L, static_cast<double>(nextKey++));
|
|
PushFromCache(L, unit, buf[i], filter == Filter::Helpful);
|
|
Game::Lua::SetTable(L, outerIdx);
|
|
}
|
|
}
|
|
|
|
// ── Out-of-range groupmate path ────────────────────────────────────────────
|
|
|
|
namespace {
|
|
|
|
// Absolute slot range [start, end) for a filter, matching the descriptor
|
|
// convention (helpful 0..31, harmful 32..47).
|
|
void GroupRange(Filter filter, int &start, int &end) {
|
|
start = (filter == Filter::Harmful) ? Offsets::UNIT_AURA_BUFF_COUNT : 0;
|
|
end = (filter == Filter::Harmful) ? Offsets::UNIT_AURA_TOTAL
|
|
: Offsets::UNIT_AURA_BUFF_COUNT;
|
|
}
|
|
|
|
// Spell ID at group-array slot (0..47), 0 if `arr` null / OOB / empty.
|
|
uint32_t GroupSpellID(const uint16_t *arr, int slot) {
|
|
if (arr == nullptr || slot < 0 || slot >= Offsets::UNIT_AURA_TOTAL)
|
|
return 0;
|
|
return arr[slot];
|
|
}
|
|
|
|
// Whether the group-array slot holds a surfacable aura for this query. Mirrors
|
|
// the engine's per-slot test in Script_UnitBuff/UnitDebuff — a non-zero spell
|
|
// ID with a real Spell.dbc record that passes the visibility predicate (the
|
|
// server only transmits *applied* auras, so there is no flag nibble to check).
|
|
// `playerOnly` additionally requires the `Aura::Source` cache to attribute the
|
|
// cast to the local player (a miss excludes it — we can't confirm the caster
|
|
// from the group array alone).
|
|
bool GroupSlotEligible(uint64_t guid, const uint16_t *arr, int slot,
|
|
bool playerOnly) {
|
|
const uint32_t id = GroupSpellID(arr, slot);
|
|
if (id == 0 || !IsVisible(SpellRecord(id)))
|
|
return false;
|
|
if (playerOnly) {
|
|
uint64_t c = 0;
|
|
uint32_t e = 0;
|
|
uint32_t d = 0;
|
|
if (!Aura::Source::Get(guid, id, &c, &e, &d) ||
|
|
c != Unit::Identity::PlayerGuid())
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// The groupmate's level, for Spell.dbc duration scaling (0 = skip). Cheap
|
|
// roster lookup; only hit on the out-of-range path.
|
|
int GroupMemberLevel(uint64_t guid) {
|
|
return Group::MemberStats::Lookup(guid).level;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool PushNthGroupAura(void *L, uint64_t guid, int oneBasedIndex, Filter filter,
|
|
bool playerOnly) {
|
|
if (guid == 0 || oneBasedIndex < 1)
|
|
return false;
|
|
const uint16_t *arr = Group::MemberStats::AuraArray(guid);
|
|
if (arr == nullptr)
|
|
return false;
|
|
int start, end;
|
|
GroupRange(filter, start, end);
|
|
int matches = 0;
|
|
for (int slot = start; slot < end; ++slot) {
|
|
if (!GroupSlotEligible(guid, arr, slot, playerOnly))
|
|
continue;
|
|
if (++matches == oneBasedIndex) {
|
|
PushEnriched(L, guid, GroupSpellID(arr, slot),
|
|
filter == Filter::Helpful, 1, GroupMemberLevel(guid),
|
|
false);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool PushGroupAuraBySpellID(void *L, uint64_t guid, uint32_t spellID,
|
|
const Filter *filter, bool playerOnly) {
|
|
if (guid == 0 || spellID == 0)
|
|
return false;
|
|
const uint16_t *arr = Group::MemberStats::AuraArray(guid);
|
|
if (arr == nullptr)
|
|
return false;
|
|
const int start = (filter != nullptr && *filter == Filter::Harmful)
|
|
? Offsets::UNIT_AURA_BUFF_COUNT
|
|
: 0;
|
|
const int end = (filter != nullptr && *filter == Filter::Helpful)
|
|
? Offsets::UNIT_AURA_BUFF_COUNT
|
|
: Offsets::UNIT_AURA_TOTAL;
|
|
for (int slot = start; slot < end; ++slot) {
|
|
if (GroupSpellID(arr, slot) != spellID)
|
|
continue;
|
|
if (!GroupSlotEligible(guid, arr, slot, playerOnly))
|
|
continue;
|
|
PushEnriched(L, guid, spellID, slot < Offsets::UNIT_AURA_BUFF_COUNT, 1,
|
|
GroupMemberLevel(guid), false);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool PushGroupAuraBySpellName(void *L, uint64_t guid, const char *spellName,
|
|
const Filter *filter, bool playerOnly) {
|
|
if (guid == 0 || spellName == nullptr || *spellName == '\0')
|
|
return false;
|
|
const uint16_t *arr = Group::MemberStats::AuraArray(guid);
|
|
if (arr == nullptr)
|
|
return false;
|
|
const int start = (filter != nullptr && *filter == Filter::Harmful)
|
|
? Offsets::UNIT_AURA_BUFF_COUNT
|
|
: 0;
|
|
const int end = (filter != nullptr && *filter == Filter::Helpful)
|
|
? Offsets::UNIT_AURA_BUFF_COUNT
|
|
: Offsets::UNIT_AURA_TOTAL;
|
|
for (int slot = start; slot < end; ++slot) {
|
|
const uint32_t id = GroupSpellID(arr, slot);
|
|
if (id == 0)
|
|
continue;
|
|
const char *name = LocalizedSpellName(SpellRecord(id));
|
|
if (name == nullptr || std::strcmp(name, spellName) != 0)
|
|
continue;
|
|
if (!GroupSlotEligible(guid, arr, slot, playerOnly))
|
|
continue;
|
|
PushEnriched(L, guid, id, slot < Offsets::UNIT_AURA_BUFF_COUNT, 1,
|
|
GroupMemberLevel(guid), false);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void AppendGroupAuras(void *L, uint64_t guid, Filter filter, bool playerOnly,
|
|
int outerIdx, int &nextKey) {
|
|
if (guid == 0)
|
|
return;
|
|
const uint16_t *arr = Group::MemberStats::AuraArray(guid);
|
|
if (arr == nullptr)
|
|
return;
|
|
int start, end;
|
|
GroupRange(filter, start, end);
|
|
const int level = GroupMemberLevel(guid);
|
|
for (int slot = start; slot < end; ++slot) {
|
|
if (!GroupSlotEligible(guid, arr, slot, playerOnly))
|
|
continue;
|
|
Game::Lua::PushNumber(L, static_cast<double>(nextKey++));
|
|
PushEnriched(L, guid, GroupSpellID(arr, slot),
|
|
filter == Filter::Helpful, 1, level, false);
|
|
Game::Lua::SetTable(L, outerIdx);
|
|
}
|
|
}
|
|
|
|
} // namespace Aura::Data
|