141 lines
3.9 KiB
Lua
141 lines
3.9 KiB
Lua
local lib = LibStub:NewLibrary("LibCombatLog", 14)
|
|
if not lib then return end
|
|
local CombatLogGetCurrentEventInfo = CombatLogGetCurrentEventInfo
|
|
lib.mixinTargets = lib.mixinTargets or {}
|
|
local mixins = {"RegisterCLEvent", "UnRegisterCLEvent", "RegisterCLSuffix", "UnRegisterCLSuffix", "RegisterCLStatus", "UnRegisterCLStatus","SplitCLEvent"}
|
|
lib['regs'] = lib['regs'] or {}
|
|
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]))
|
|
if(lib['regs'][data[3]]) then
|
|
for k, v in pairs(lib['regs'][data[3]]) do
|
|
k[v](self, data, prefix, suffix)
|
|
end
|
|
end
|
|
if(suffix) then
|
|
lib['regs'][suffix] = lib['regs'][suffix] or {}
|
|
for k, v in pairs(lib['regs'][suffix]) do
|
|
k[v](self, data, prefix, suffix)
|
|
end
|
|
end
|
|
elseif(event == 'PLAYER_REGEN_DISABLED' or event == 'PLAYER_REGEN_ENABLED') then
|
|
for k, v in pairs(lib['regs']['PLAYER_REGEN']) do
|
|
k[v](self, event)
|
|
end
|
|
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) ~= "string" and funcName) then
|
|
funcName = funcName or suffix[1]
|
|
for _,v in pairs(suffix) do
|
|
self:RegisterCLSuffix(v, funcName)
|
|
end
|
|
else
|
|
funcName = funcName or suffix
|
|
lib['regs'][suffix] = lib['regs'][suffix] or {}
|
|
lib['regs'][suffix][self] = funcName
|
|
end
|
|
end
|
|
function lib:UnRegisterCLSuffix(suffix)
|
|
if(lib['regs'][suffix]) then
|
|
if(lib['regs'][suffix][self]) then
|
|
lib['regs'][suffix][self] = nil
|
|
end
|
|
end
|
|
end
|
|
function lib:RegisterCLEvent(event,funcName)
|
|
if(type(event) ~= "string" and funcName) then
|
|
funcName = funcName or event[1]
|
|
for _,v in pairs(event) do
|
|
self:RegisterCLEvent(v, funcName)
|
|
end
|
|
else
|
|
funcName = funcName or event
|
|
lib['regs'][event] = lib['regs'][event] or {}
|
|
lib['regs'][event][self] = funcName
|
|
end
|
|
end
|
|
function lib:RegisterCLStatus(funcName)
|
|
lib['regs']['PLAYER_REGEN'] = lib['regs']['PLAYER_REGEN'] or {}
|
|
lib['regs']['PLAYER_REGEN'][self] = funcName
|
|
end
|
|
function lib:UnRegisterCLStatus()
|
|
if(lib['regs']['PLAYER_REGEN']) then
|
|
if(lib['regs']['PLAYER_REGEN'][self]) then
|
|
lib['regs']['PLAYER_REGEN'][self] = nil
|
|
end
|
|
end
|
|
end
|
|
function lib:UnRegisterCLEvent(event)
|
|
if(lib['regs'][event]) then
|
|
if(lib['regs'][event][self]) then
|
|
lib['regs'][event][self] = nil
|
|
end
|
|
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
|
|
--[[ Debug demo
|
|
local f = CreateFrame("Frame")
|
|
f['dmg'] = function(self, ...)
|
|
print(...)
|
|
end
|
|
LibStub("LibCombatLog"):Embed(f)
|
|
f:RegisterCLSuffix('DAMAGE', 'dmg')
|
|
]] |