-- load pfUI environment setfenv(1, pfUI:GetEnvironment()) --[[ libdebuff ]]-- -- Aura readers over ClassicAPI's C_UnitAuras, which already resolves each -- aura's caster (sourceGUID) and expiration for non-player units: -- GetBestAuraCast - live HoT timers for libpredict -- UnitDebuff/OwnDebuff - libdebuff's legacy multi-return signature, still -- read by third-party addons (e.g. SuperCleveRoidMacros) -- return instantly when another libdebuff is already active if pfUI.api.libdebuff then return end local libdebuff = {} -- PERF (local change): the hot paths read auras as plain values -- (C_UnitAuras.UnitAuraBySlot / UnitAura) instead of GetAuraDataBySpellName / -- GetAuraDataByIndex, which build a new AuraData table on every call -- a steady -- garbage source in raids (HoT indicators on every raid frame), like Rinse had. -- The table-based calls stay as the fallback for ClassicAPI builds without them. -- Spell rank from an aura's spellId ("Rank 3" -> 3). GetSpellSubtext + gsub build -- strings, so each spellId is resolved once (a spell's rank never changes). -- One cache per pattern, matching the two original expressions. local rankCache = { [RANK or ""] = {}, ["Rank "] = {} } local function SpellRank(spellId, pattern) if not spellId then return nil end local cache = rankCache[pattern] local r = cache[spellId] if r == nil then local subtext = C_Spell.GetSpellSubtext(spellId) if subtext == nil then return nil end -- not cached: spell data may not be loaded yet r = subtext ~= "" and tonumber((string.gsub(subtext, pattern, ""))) or false cache[spellId] = r end return r or nil end -- ============================================================================ -- API: GetBestAuraCast (for libpredict HoT tracking) -- ============================================================================ local slotBuf = {} -- reused by ScanAuraSlots -- Report the active aura for spellName on `guid`, read straight from -- C_UnitAuras. expirationTime is the true, caster-modified remaining time, so -- no cast-event bookkeeping is needed -- and a unit only ever holds one instance -- of a given spell, so this is inherently the "best" one. -- unit (optional, PERF local change): the unit token for `guid`. When given, the -- unit's helpful auras are walked once with UnitAuraBySlot (no table); HoTs are -- helpful auras. Without it, the original table-based lookup by GUID is used. -- Returns (startTime, duration, timeleft, rank, casterGuid) or nil. function libdebuff:GetBestAuraCast(guid, spellName, unit) if not guid or not spellName then return nil end local duration, expirationTime, spellId, sourceGUID local scan = pfUI.api.ScanAuraSlots if unit and scan and C_UnitAuras.UnitAuraBySlot then local found local n = scan(unit, "HELPFUL", slotBuf) for i = 1, n do local name, _, _, _, dur, exp, caster, _, _, sid = C_UnitAuras.UnitAuraBySlot(unit, slotBuf[i]) if name == spellName then found, duration, expirationTime, spellId = true, dur, exp, sid sourceGUID = caster and UnitGUID(caster) or nil break end end if not found then return nil end else local aura = C_UnitAuras.GetAuraDataBySpellName(guid, spellName) if not aura then return nil end duration, expirationTime, spellId, sourceGUID = aura.duration, aura.expirationTime, aura.spellId, aura.sourceGUID end if not expirationTime or expirationTime == 0 then return nil end local timeleft = expirationTime - GetTime() if timeleft <= 0 then return nil end duration = (duration and duration > 0) and duration or timeleft local startTime = expirationTime - duration local rank = SpellRank(spellId, RANK or "") return startTime, duration, timeleft, rank, sourceGUID end -- ============================================================================ -- API: UnitDebuff / UnitOwnDebuff (C_UnitAuras adapters) -- ============================================================================ -- Remap C_UnitAuras' AuraData onto libdebuff's legacy tuple: -- effect, rank, texture, stacks, dtype, duration, timeleft, caster local function AuraToLegacy(aura) if not aura then return nil end local duration = aura.duration or 0 local timeleft = -1 -- Only report a timer for genuinely timed auras. ClassicAPI can leave a -- stale expirationTime on permanent (duration 0) auras, so gate on duration. if duration > 0 and aura.expirationTime and aura.expirationTime > 0 then timeleft = aura.expirationTime - GetTime() if timeleft < 0 then timeleft = 0 end end -- Aura spellId is the specific cast rank, so its subtext is the active rank. local rank = SpellRank(aura.spellId, "Rank ") local dtype = aura.dispelName if dtype == "" then dtype = nil end local caster = aura.isFromPlayerOrPlayerPet and "player" or "other" return aura.name, rank, aura.icon, aura.applications or 0, dtype, duration, timeleft, caster end -- PERF (local change): same tuple as AuraToLegacy, built from UnitAura's plain -- values (name, icon, count, dispelType, duration, expirationTime, caster, ..., -- spellId) instead of an AuraData table. "Cast by the player or the player's -- pet" (isFromPlayerOrPlayerPet) = caster unit "player" or "pet". -- own: the aura came through the PLAYER filter, so it is the player's whatever -- UnitAura reports as caster (it can be nil for self-applied auras like Forbearance) local function ValuesToLegacy(own, name, icon, count, dtype, duration, expirationTime, caster, _, _, spellId) if not name then return nil end duration = duration or 0 local timeleft = -1 if duration > 0 and expirationTime and expirationTime > 0 then timeleft = expirationTime - GetTime() if timeleft < 0 then timeleft = 0 end end if dtype == "" then dtype = nil end return name, SpellRank(spellId, "Rank "), icon, count or 0, dtype, duration, timeleft, (own or caster == "player" or caster == "pet") and "player" or "other" end -- id is a 1-based harmful-aura index, matching C_UnitAuras / Blizzard's -- compacted debuff slots. function libdebuff:UnitDebuff(unit, id) if C_UnitAuras.UnitAura then return ValuesToLegacy(false, C_UnitAuras.UnitAura(unit, id, "HARMFUL")) end return AuraToLegacy(C_UnitAuras.GetAuraDataByIndex(unit, id, "HARMFUL")) end -- Player-cast harmful auras only, via the PLAYER filter -- no manual -- caster-GUID bookkeeping needed. function libdebuff:UnitOwnDebuff(unit, id) if C_UnitAuras.UnitAura then return ValuesToLegacy(true, C_UnitAuras.UnitAura(unit, id, "HARMFUL|PLAYER")) end return AuraToLegacy(C_UnitAuras.GetAuraDataByIndex(unit, id, "HARMFUL|PLAYER")) end -- add libdebuff to pfUI API pfUI.api.libdebuff = libdebuff