More code cleanup and framework update

This commit is contained in:
Tercio Jose
2023-06-25 11:29:45 -03:00
parent fae511884c
commit 7b34b88ca3
10 changed files with 383 additions and 338 deletions
+83 -100
View File
@@ -1,13 +1,11 @@
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
local _detalhes = _G.Details
local Details = _G.Details
local Loc = LibStub("AceLocale-3.0"):GetLocale ( "Details" )
local _
local addonName, Details222 = ...
--Event types:
_detalhes.RegistredEvents = {
Details.RegistredEvents = {
--instances
["DETAILS_INSTANCE_OPEN"] = {},
["DETAILS_INSTANCE_CLOSE"] = {},
@@ -69,10 +67,10 @@
["COMM_EVENT_SENT"] = {}, --added on core 129
}
local function AlreadyRegistred (_tables, _object)
local function isAlreadyRegistred(_tables, _object)
for index, _this_object in ipairs(_tables) do
if (_this_object.__eventtable) then
if (_this_object [1] == _object) then
if (_this_object[1] == _object) then
return index
end
elseif (_this_object == _object) then
@@ -131,23 +129,22 @@ local common_events = {
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--register a event
function _detalhes:RegisterEvent(object, event, func)
if (not _detalhes.RegistredEvents [event]) then
function Details:RegisterEvent(object, event, func)
if (not Details.RegistredEvents[event]) then
if (object.Msg) then
object:DelayMsg ("[debug] unknown event:", event, object.__name)
object:DelayMsg("[debug] unknown event:", event, object.__name)
else
_detalhes:DelayMsg ("[debug] unknown event:", event, object.__name)
Details:DelayMsg("[debug] unknown event:", event, object.__name)
end
return
end
if (common_events [event]) then
if (not AlreadyRegistred (_detalhes.RegistredEvents [event], object)) then
if (common_events[event]) then
if (not isAlreadyRegistred(Details.RegistredEvents[event], object)) then
if (func) then
tinsert(_detalhes.RegistredEvents [event], {object, func, __eventtable = true})
table.insert(Details.RegistredEvents[event], {object, func, __eventtable = true})
else
tinsert(_detalhes.RegistredEvents [event], object)
table.insert(Details.RegistredEvents[event], object)
end
return true
else
@@ -155,29 +152,29 @@ local common_events = {
end
else
if (event == "BUFF_UPDATE") then
if (not AlreadyRegistred (_detalhes.RegistredEvents ["BUFF_UPDATE"], object)) then
if (not isAlreadyRegistred(Details.RegistredEvents["BUFF_UPDATE"], object)) then
if (func) then
tinsert(_detalhes.RegistredEvents ["BUFF_UPDATE"], {object, func, __eventtable = true})
table.insert(Details.RegistredEvents["BUFF_UPDATE"], {object, func, __eventtable = true})
else
tinsert(_detalhes.RegistredEvents ["BUFF_UPDATE"], object)
table.insert(Details.RegistredEvents["BUFF_UPDATE"], object)
end
_detalhes.Buffs:CatchBuffs()
_detalhes.RecordPlayerSelfBuffs = true
_detalhes:UpdateParserGears()
Details.Buffs:CatchBuffs()
Details.RecordPlayerSelfBuffs = true
Details:UpdateParserGears()
return true
else
return false
end
elseif (event == "BUFF_UPDATE_DEBUFFPOWER") then
if (not AlreadyRegistred (_detalhes.RegistredEvents ["BUFF_UPDATE_DEBUFFPOWER"], object)) then
if (not isAlreadyRegistred(Details.RegistredEvents["BUFF_UPDATE_DEBUFFPOWER"], object)) then
if (func) then
tinsert(_detalhes.RegistredEvents ["BUFF_UPDATE_DEBUFFPOWER"], {object, func, __eventtable = true})
table.insert(Details.RegistredEvents["BUFF_UPDATE_DEBUFFPOWER"], {object, func, __eventtable = true})
else
tinsert(_detalhes.RegistredEvents ["BUFF_UPDATE_DEBUFFPOWER"], object)
table.insert(Details.RegistredEvents["BUFF_UPDATE_DEBUFFPOWER"], object)
end
_detalhes.RecordPlayerAbilityWithBuffs = true
_detalhes:UpdateParserGears()
Details.RecordPlayerAbilityWithBuffs = true
Details:UpdateParserGears()
return true
else
return false
@@ -189,33 +186,32 @@ local common_events = {
------------------------------------------------------------------------------------------------------------------------------------------------------------------
--Unregister a Event
function _detalhes:UnregisterEvent (object, event)
if (not _detalhes.RegistredEvents [event]) then
function Details:UnregisterEvent(object, event)
if (not Details.RegistredEvents[event]) then
if (object.Msg) then
object:Msg("(debug) unknown event", event)
else
_detalhes:Msg("(debug) unknown event", event)
Details:Msg("(debug) unknown event", event)
end
return
end
if (common_events [event]) then
local index = AlreadyRegistred (_detalhes.RegistredEvents [event], object)
if (common_events[event]) then
local index = isAlreadyRegistred(Details.RegistredEvents[event], object)
if (index) then
table.remove (_detalhes.RegistredEvents [event], index)
table.remove(Details.RegistredEvents[event], index)
return true
else
return false
end
else
if (event == "BUFF_UPDATE") then
local index = AlreadyRegistred (_detalhes.RegistredEvents ["BUFF_UPDATE"], object)
local index = isAlreadyRegistred(Details.RegistredEvents["BUFF_UPDATE"], object)
if (index) then
table.remove (_detalhes.RegistredEvents ["BUFF_UPDATE"], index)
if (#_detalhes.RegistredEvents ["BUFF_UPDATE"] < 1) then
_detalhes.RecordPlayerSelfBuffs = true
_detalhes:UpdateParserGears()
table.remove(Details.RegistredEvents["BUFF_UPDATE"], index)
if (#Details.RegistredEvents["BUFF_UPDATE"] < 1) then
Details.RecordPlayerSelfBuffs = true
Details:UpdateParserGears()
end
return true
else
@@ -223,12 +219,12 @@ local common_events = {
end
elseif (event == "BUFF_UPDATE_DEBUFFPOWER") then
local index = AlreadyRegistred (_detalhes.RegistredEvents ["BUFF_UPDATE_DEBUFFPOWER"], object)
local index = isAlreadyRegistred(Details.RegistredEvents["BUFF_UPDATE_DEBUFFPOWER"], object)
if (index) then
table.remove (_detalhes.RegistredEvents ["BUFF_UPDATE_DEBUFFPOWER"], index)
if (#_detalhes.RegistredEvents ["BUFF_UPDATE_DEBUFFPOWER"] < 1) then
_detalhes.RecordPlayerAbilityWithBuffs = false
_detalhes:UpdateParserGears()
table.remove(Details.RegistredEvents["BUFF_UPDATE_DEBUFFPOWER"], index)
if (#Details.RegistredEvents["BUFF_UPDATE_DEBUFFPOWER"] < 1) then
Details.RecordPlayerAbilityWithBuffs = false
Details:UpdateParserGears()
end
return true
else
@@ -242,11 +238,11 @@ local common_events = {
--internal functions
local dispatch_error = function(name, errortext)
_detalhes:Msg((name or "<no context>"), " |cFFFF9900error|r: ", errortext)
Details:Msg((name or "<no context>"), " |cFFFF9900error|r: ", errortext)
end
--safe call an external func with payload and without telling who is calling
function _detalhes:QuickDispatchEvent (func, event, ...)
function Details:QuickDispatchEvent(func, event, ...)
if (type(func) ~= "function") then
return
elseif (type(event) ~= "string") then
@@ -257,7 +253,7 @@ local common_events = {
if (not okay) then
--trigger an error msg
dispatch_error (_, errortext)
dispatch_error(_, errortext)
return
end
@@ -266,11 +262,13 @@ local common_events = {
end
--quick dispatch with context, send the caller object within the payload
function _detalhes:QuickDispatchEventWithContext (context, func, event, ...)
function Details:QuickDispatchEventWithContext(context, func, event, ...)
if (type(context) ~= "table") then
return
elseif (type(func) ~= "function") then
return
elseif (type(event) ~= "string") then
return
end
@@ -281,8 +279,7 @@ local common_events = {
--attempt to get the context name
local objectName = context.__name or context._name or context.name or context.Name
--trigger an error msg
dispatch_error (objectName, errortext)
dispatch_error(objectName, errortext)
return
end
@@ -290,119 +287,105 @@ local common_events = {
end
--Send Event
function _detalhes:SendEvent(event, object, ...)
function Details:SendEvent(event, object, ...)
--send event to all registred plugins
if (event == "PLUGIN_DISABLED" or event == "PLUGIN_ENABLED") then
return object:OnDetailsEvent (event, ...)
return object:OnDetailsEvent(event, ...)
elseif (not object) then
--iterate among all plugins which registered a function for this event
for _, PluginObject in ipairs(_detalhes.RegistredEvents[event]) do
for _, PluginObject in ipairs(Details.RegistredEvents[event]) do
--when __eventtable is true, the plugin registered a function or method name to callback
--if is false, we call OnDetailsEvent method on the plugin
if (PluginObject.__eventtable) then
local pluginTable = PluginObject [1]
local pluginTable = PluginObject[1]
--check if the plugin is enabled
if (pluginTable.Enabled and pluginTable.__enabled) then
--check if fegistered a function
if (type(PluginObject [2]) == "function") then
local func = PluginObject [2]
_detalhes:QuickDispatchEvent (func, event, ...)
--PluginObject [2] (event, ...)
if (type(PluginObject[2]) == "function") then
local func = PluginObject[2]
Details:QuickDispatchEvent(func, event, ...)
--if not it must be a method name
else
local methodName = PluginObject [2]
local func = pluginTable [methodName]
_detalhes:QuickDispatchEventWithContext (pluginTable, func, event, ...)
--PluginObject [1] [PluginObject [2]] (PluginObject, event, ...)
local methodName = PluginObject[2]
local func = pluginTable[methodName]
Details:QuickDispatchEventWithContext(pluginTable, func, event, ...)
end
end
--if no function(only registred the event) sent the event to OnDetailsEvent
else
if (PluginObject.Enabled and PluginObject.__enabled) then
_detalhes:QuickDispatchEventWithContext (PluginObject, PluginObject.OnDetailsEvent, event, ...)
--PluginObject:OnDetailsEvent (event, ...)
Details:QuickDispatchEventWithContext (PluginObject, PluginObject.OnDetailsEvent, event, ...)
end
end
end
--plugin notifications (does not send to listeners)
elseif (type(object) == "string" and object == "SEND_TO_ALL") then
for _, PluginObject in ipairs(_detalhes.RaidTables.Plugins) do
for _, PluginObject in ipairs(Details.RaidTables.Plugins) do
if (PluginObject.__enabled) then
_detalhes:QuickDispatchEventWithContext (PluginObject, PluginObject.OnDetailsEvent, event)
--PluginObject:OnDetailsEvent (event)
Details:QuickDispatchEventWithContext(PluginObject, PluginObject.OnDetailsEvent, event)
end
end
for _, PluginObject in ipairs(_detalhes.SoloTables.Plugins) do
for _, PluginObject in ipairs(Details.SoloTables.Plugins) do
if (PluginObject.__enabled) then
_detalhes:QuickDispatchEventWithContext (PluginObject, PluginObject.OnDetailsEvent, event)
--PluginObject:OnDetailsEvent (event)
Details:QuickDispatchEventWithContext(PluginObject, PluginObject.OnDetailsEvent, event)
end
end
for _, PluginObject in ipairs(_detalhes.ToolBar.Plugins) do
for _, PluginObject in ipairs(Details.ToolBar.Plugins) do
if (PluginObject.__enabled) then
_detalhes:QuickDispatchEventWithContext (PluginObject, PluginObject.OnDetailsEvent, event)
--PluginObject:OnDetailsEvent (event)
Details:QuickDispatchEventWithContext(PluginObject, PluginObject.OnDetailsEvent, event)
end
end
else
--send the event only for requested plugin
if (object.Enabled and object.__enabled) then
return _detalhes:QuickDispatchEventWithContext (object, object.OnDetailsEvent, event, ...)
--return object:OnDetailsEvent (event, ...)
return Details:QuickDispatchEventWithContext(object, object.OnDetailsEvent, event, ...)
end
end
end
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--special cases
function _detalhes:SendOptionsModifiedEvent (instance)
_detalhes.last_options_modified = _detalhes.last_options_modified or (GetTime() - 5)
if (_detalhes.last_options_modified + 0.3 < GetTime()) then
_detalhes:SendEvent("DETAILS_OPTIONS_MODIFIED", nil, instance)
_detalhes.last_options_modified = GetTime()
if (_detalhes.last_options_modified_schedule) then
_detalhes:CancelTimer(_detalhes.last_options_modified_schedule)
_detalhes.last_options_modified_schedule = nil
function Details:SendOptionsModifiedEvent(instance)
Details.last_options_modified = Details.last_options_modified or (GetTime() - 5)
if (Details.last_options_modified + 0.3 < GetTime()) then
Details:SendEvent("DETAILS_OPTIONS_MODIFIED", nil, instance)
Details.last_options_modified = GetTime()
if (Details.last_options_modified_schedule) then
Details:CancelTimer(Details.last_options_modified_schedule)
Details.last_options_modified_schedule = nil
end
else
if (_detalhes.last_options_modified_schedule) then
_detalhes:CancelTimer(_detalhes.last_options_modified_schedule)
if (Details.last_options_modified_schedule) then
Details:CancelTimer(Details.last_options_modified_schedule)
end
_detalhes.last_options_modified_schedule = _detalhes:ScheduleTimer("SendOptionsModifiedEvent", 0.31, instance)
Details.last_options_modified_schedule = Details:ScheduleTimer("SendOptionsModifiedEvent", 0.31, instance)
end
end
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--listeners
local listener_meta = setmetatable({}, _detalhes)
local listener_meta = setmetatable({}, Details)
listener_meta.__index = listener_meta
function listener_meta:RegisterEvent(event, func)
return _detalhes:RegisterEvent(self, event, func)
end
function listener_meta:UnregisterEvent (event)
return _detalhes:UnregisterEvent (self, event)
return Details:RegisterEvent(self, event, func)
end
function _detalhes:CreateEventListener()
function listener_meta:UnregisterEvent(event)
return Details:UnregisterEvent(self, event)
end
function Details:CreateEventListener()
local new = {Enabled = true, __enabled = true}
setmetatable(new, listener_meta)
return new
end
end