Skip to content

Instantly share code, notes, and snippets.

@Adirelle
Created September 6, 2014 13:22
Show Gist options
  • Save Adirelle/423e5b1f9857a7412a42 to your computer and use it in GitHub Desktop.
Save Adirelle/423e5b1f9857a7412a42 to your computer and use it in GitHub Desktop.
Aura caching
local addonName, addon = ...
local _G = _G
local CreateFrame = _G.CreateFrame
local pairs = _G.pairs
local rawget = _G.rawget
local setmetatable = _G.setmetatable
local type = _G.type
local UnitAura = _G.UnitAura
local unpack = _G.unpack
local wipe = _G.wipe
local miss, clear, direct, iterator = 0, 0, 0, 0
local heap = setmetatable({}, { __mode = 'kv' })
local function FillTable(t, ...)
if select('#', ...) == 0 or not ... then
if t then
heap[t] = true
end
return false
end
if not t then
t = next(heap) or {}
heap[t] = nil
end
for i = 1, max(#t, select('#', ...)) do
t[i] = select(i, ...)
end
return t
end
local function BuildFilterTable(unit, filter)
return setmetatable({}, {
__index = function(t, key)
local aura
if type(key) == "string" then
aura = FillTable(false, UnitAura(unit, key, nil, filter))
else
aura = FillTable(false, UnitAura(unit, key, filter))
end
miss = miss + 1
t[key] = aura
return aura
end
})
end
local function BuildUnitTable(unit)
return setmetatable({}, {
__index = function(t, filter)
local sub = BuildFilterTable(unit, filter)
t[filter] = sub
return sub
end
})
end
local cache = setmetatable({}, {
__index = function(t, unit)
local sub = BuildUnitTable(unit)
t[unit] = sub
return sub
end
})
local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent('UNIT_AURA')
eventFrame:SetScript('OnEvent', function(_, _, unit)
local unitTable = rawget(cache, unit)
if not unitTable then return end
for filter, filterTable in pairs(unitTable) do
for key, aura in pairs(filterTable) do
heap[aura] = true
filterTable[key] = nil
end
end
clear = clear + 1
end)
function addon.UnitAura(unit, key, rank, filter)
if not unit then return end
if type(key) == "string" then
filter = rank
end
direct = direct + 1
local aura = cache[unit][filter or ""][key]
if aura then
return unpack(aura)
end
end
local function AuraIterator(auraTable, index)
iterator = iterator + 1
local aura = auraTable[index+1]
if aura then
return index+1, unpack(aura)
end
end
local function EmptyIterator() end
function addon.IterateAuras(unit, filter)
if not unit then return EmptyIterator end
return AuraIterator, cache[unit][filter or ""], 0
end
function addon.api.GetAuraCacheStats()
local heapSize = 0
for t in pairs(heap) do
heapSize = heapSize + 1
end
return direct, iterator, miss, clear, heapSize
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment