119 lines
3.3 KiB
Lua
119 lines
3.3 KiB
Lua
local lib = LibStub:NewLibrary("LibCombatLog", 20)
|
|
if not lib then return end
|
|
if not lib['cb'] then
|
|
lib['cb'] = LibStub("CallbackHandler-1.0"):New(lib)
|
|
end
|
|
local CombatLogGetCurrentEventInfo, pairs, unpack, type = CombatLogGetCurrentEventInfo, pairs, unpack, type
|
|
lib.mixinTargets = lib.mixinTargets or {}
|
|
local mixins = {"RegisterCLEvent", "UnRegisterCLEvent", "RegisterCLSuffix", "UnRegisterCLSuffix", "RegisterCLStatus", "UnRegisterCLStatus","SplitCLEvent"}
|
|
|
|
local allPrefix = {
|
|
'SWING', 'RANGE', 'SPELL_PERIODIC', 'SPELL_BUILDING', 'SPELL', 'ENVIRONMENTAL', 'UNIT', 'PARTY', 'ENCHANT'
|
|
}
|
|
local knownsplits = {
|
|
['DAMAGE_SHIELD'] = {'SPELL', 'DAMAGE'},
|
|
['DAMAGE_SHIELD_MISSED'] = {'SPELL', 'MISSED'},
|
|
['DAMAGE_SPLIT'] = {'SPELL', 'DAMAGE'},
|
|
}
|
|
local function splitEvent(event)
|
|
if(event) then
|
|
if(knownsplits[event]) then
|
|
return knownsplits[event]
|
|
end
|
|
local prefix, suffix
|
|
for i = 1, #allPrefix do
|
|
if(strsub(event, 1, strlen(allPrefix[i])) == allPrefix[i]) then
|
|
prefix = allPrefix[i]
|
|
break
|
|
end
|
|
end
|
|
if(prefix) then
|
|
suffix = strsub(event, strlen(prefix) + 2)
|
|
if(suffix) then
|
|
knownsplits[event] = {prefix, suffix}
|
|
return knownsplits[event]
|
|
end
|
|
end
|
|
end
|
|
return {'NONE','NONE'}
|
|
end
|
|
local function OnEvent2(self, event)
|
|
if(event == 'COMBAT_LOG_EVENT_UNFILTERED') then
|
|
local data = { event, CombatLogGetCurrentEventInfo()}
|
|
local prefix, suffix = unpack(splitEvent(data[3]))
|
|
lib['cb']:Fire(data[3], data, prefix, suffix)
|
|
lib['cb']:Fire(suffix, data, prefix, suffix)
|
|
elseif(event == 'PLAYER_REGEN_DISABLED' or event == 'PLAYER_REGEN_ENABLED') then
|
|
lib['cb']:Fire('PLAYER_REGEN', event)
|
|
else
|
|
print('unknown',event)
|
|
end
|
|
end
|
|
local function OnEvent(self, event)
|
|
self:RegisterEvent('COMBAT_LOG_EVENT_UNFILTERED')
|
|
self:RegisterEvent("PLAYER_REGEN_DISABLED")
|
|
self:RegisterEvent("PLAYER_REGEN_ENABLED")
|
|
self:UnregisterEvent(event)
|
|
self:SetScript("OnEvent", OnEvent2)
|
|
end
|
|
lib['f'] = lib['f'] or CreateFrame("Frame")
|
|
if(not lib['f']:IsEventRegistered("PLAYER_ENTERING_WORLD")) then
|
|
lib['f']:RegisterEvent("PLAYER_ENTERING_WORLD")
|
|
end
|
|
lib['f']:SetScript("OnEvent", OnEvent)
|
|
function lib:SplitCLEvent(event)
|
|
return splitEvent(event)
|
|
end
|
|
function lib:RegisterCLSuffix(suffix, funcName)
|
|
if(type(suffix) == "table") then
|
|
for _,v in pairs(suffix) do
|
|
self:RegisterCLSuffix(v, funcName)
|
|
end
|
|
else
|
|
lib['RegisterCallback'](self, suffix, funcName)
|
|
end
|
|
end
|
|
function lib:UnregisterCLSuffix(suffix)
|
|
if(type(suffix) == "table") then
|
|
for _,v in pairs(suffix) do
|
|
self:UnregisterCLSuffix(v)
|
|
end
|
|
return
|
|
else
|
|
lib['UnregisterCallback'](self, suffix)
|
|
end
|
|
end
|
|
function lib:RegisterCLEvent(event, funcName)
|
|
if(type(event) == "table") then
|
|
for _,v in pairs(event) do
|
|
self:RegisterCLEvent(v, funcName)
|
|
end
|
|
else
|
|
lib['RegisterCallback'](self, event, funcName)
|
|
end
|
|
end
|
|
function lib:RegisterCLStatus(funcName)
|
|
lib['RegisterCallback'](self, 'PLAYER_REGEN', funcName)
|
|
end
|
|
function lib:UnregisterCLStatus()
|
|
lib['UnregisterCallback'](self, 'PLAYER_REGEN')
|
|
end
|
|
function lib:UnregisterCLEvent(event)
|
|
if(type(event) == "table") then
|
|
for _,v in pairs(event) do
|
|
self:UnregisterCLEvent(v)
|
|
end
|
|
return
|
|
else
|
|
lib['UnregisterCallback'](self, event)
|
|
end
|
|
end
|
|
function lib:Embed(target)
|
|
for _,name in pairs(mixins) do
|
|
target[name] = lib[name]
|
|
end
|
|
lib.mixinTargets[target] = true
|
|
end
|
|
for target, _ in pairs(lib.mixinTargets) do
|
|
lib:Embed(target)
|
|
end |