-
Notifications
You must be signed in to change notification settings - Fork 0
/
SezzAuras.lua
215 lines (169 loc) · 5.34 KB
/
SezzAuras.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
--[[
s:UI Auras Library
Contains all buffs and debuffs of a specified unit.
Callbacks are used to react on buff gain/change/lose.
Martin Karer / Sezz, 2014
http://www.sezz.at
--]]
local MAJOR, MINOR = "Sezz:Auras-0.1", 1;
local APkg = Apollo.GetPackage(MAJOR);
if (APkg and (APkg.nVersion or 0) >= MINOR) then return; end
local Auras = APkg and APkg.tPackage or {};
local log;
-- Lua API
local tinsert = table.insert;
-- WildStar API
local Apollo, XmlDoc = Apollo, XmlDoc;
-----------------------------------------------------------------------------
function Auras:Enable()
if (self.bEnabled) then return; end
-- Update
if (self.unit and self.unit:IsValid()) then
-- log:debug("Enable");
self.bEnabled = true;
Apollo.RegisterEventHandler("VarChange_FrameCount", "Update", self);
self:Update();
else
self:Disable();
end
end
function Auras:Disable()
if (not self.bEnabled) then return; end
-- log:debug("Disable");
self.bEnabled = false;
Apollo.RemoveEventHandler("VarChange_FrameCount", self);
self.unit = nil;
self:Reset();
end
function Auras:RegisterCallback(strEvent, strFunction, tEventHandler)
if (not self.tCallbacks[strEvent]) then
self.tCallbacks[strEvent] = {};
end
tinsert(self.tCallbacks[strEvent], { strFunction, tEventHandler });
end
function Auras:SetUnit(unit, bNoAutoEnable)
local bClearBuffs = (not self.unit or self.unit ~= unit);
self.unit = unit;
--[[
if (self.wndBuffContainer) then
self.wndBuffContainer:SetUnit(self.unit);
end
--]]
if (bClearBuffs) then
self:Reset();
end
if (not bNoAutoEnable and not self.bEnabled) then
self:Enable()
end
return self;
end
function Auras:Reset()
-- log:debug("Reset");
for _, tAura in pairs(self.tDebuffs) do
self.tDebuffs[tAura.idBuff] = nil;
self:Call("OnAuraRemoved", tAura);
end
for _, tAura in pairs(self.tBuffs) do
self.tBuffs[tAura.idBuff] = nil;
self:Call("OnAuraRemoved", tAura);
end
end
function Auras:Call(strEvent, tAura, ...)
local bFiltered = (tAura.bIsDebuff and self.tFilter["Debuff"]) or (not tAura.bIsDebuff and self.tFilter["Buff"]) or (self.tFilter[tAura.splEffect:GetId()]);
if (not bFiltered and self.tCallbacks[strEvent]) then
for _, tCallback in ipairs(self.tCallbacks[strEvent]) do
local strFunction = tCallback[1];
local tEventHandler = tCallback[2];
tEventHandler[strFunction](tEventHandler, tAura, ...);
end
end
end
function Auras:ScanAuras(arAuras, tCache, bIsDebuff)
-- Mark all auras as expired
for _, tAura in pairs(tCache) do
tAura.bExpired = true;
end
-- Add/Update Auras
for i, tAura in ipairs(arAuras) do
tAura.bIsDebuff = bIsDebuff;
if (not tCache[tAura.idBuff]) then
-- New Aura
-- log:debug("Added Aura: %s", tAura.splEffect:GetName());
tCache[tAura.idBuff] = tAura;
tAura.unit = self.unit;
tAura.nIndex = i;
self:Call("OnAuraAdded", tAura);
else
-- Update Existing Aura
local bAuraUpdated = (tAura.fTimeRemaining > tCache[tAura.idBuff].fTimeRemaining);
tCache[tAura.idBuff].fTimeRemaining = tAura.fTimeRemaining; -- fTimeRemaining doesn't get updated when a buff is refreshed!
tCache[tAura.idBuff].nIndex = i;
if (tCache[tAura.idBuff].nCount ~= tAura.nCount) then
tCache[tAura.idBuff].nCount = tAura.nCount;
bAuraUpdated = true;
end
if (bAuraUpdated) then
-- log:debug("Updated Aura: %s", tAura.splEffect:GetName());
self:Call("OnAuraUpdated", tCache[tAura.idBuff]);
end
end
-- Remove IsExpired
tCache[tAura.idBuff].bExpired = false;
end
-- Remove expired
for _, tAura in pairs(tCache) do
if (tAura.bExpired) then
-- Remove Aura
tCache[tAura.idBuff] = nil;
-- log:debug("Removed Aura: %s", tAura.splEffect:GetName());
self:Call("OnAuraRemoved", tAura);
end
end
end
function Auras:Update()
-- log:debug("Update");
if (self.unit and self.unit:IsValid()) then
local tAuras = self.unit:GetBuffs();
self:ScanAuras(tAuras.arBeneficial, self.tBuffs, false);
self:ScanAuras(tAuras.arHarmful, self.tDebuffs, true);
else
-- Invalid Unit
log:debug("Unit Invalid!")
self:Disable();
end
end
function Auras:SetFilter(tFilter)
self.tFilter = tFilter;
end
-----------------------------------------------------------------------------
-- Constructor
-----------------------------------------------------------------------------
function Auras:New()
local self = setmetatable({}, { __index = Auras });
self.bEnabled = false;
self.tBuffs = {};
self.tDebuffs = {};
self.tCallbacks = {};
self.tFilter = {};
return self;
end
-----------------------------------------------------------------------------
-- Apollo Registration
-----------------------------------------------------------------------------
function Auras:OnLoad()
local GeminiLogging = Apollo.GetPackage("Gemini:Logging-1.2") and Apollo.GetAddon("GeminiConsole") and Apollo.GetPackage("Gemini:Logging-1.2").tPackage;
if (GeminiLogging) then
log = GeminiLogging:GetLogger({
level = GeminiLogging.DEBUG,
pattern = "%d %n %c %l - %m",
appender ="GeminiConsole"
});
else
log = setmetatable({}, { __index = function() return function(self, ...) local args = #{...}; if (args > 1) then Print(string.format(...)); elseif (args == 1) then Print(tostring(...)); end; end; end });
end
end
function Auras:OnDependencyError(strDep, strError)
return false;
end
-----------------------------------------------------------------------------
Apollo.RegisterPackage(Auras, MAJOR, MINOR, {});