You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.0 KiB

4 years ago
--- MSA-ProtRouter-1.0 - Router for Protected functions
5 months ago
--- Copyright (c) 2019-2024, Marouan Sabbagh <mar.sabbagh@gmail.com>
4 years ago
--- All Rights Reserved.
5 months ago
local name, version = "MSA-ProtRouter-1.0", 4
4 years ago
5 months ago
local lib = LibStub:NewLibrary(name, version)
4 years ago
if not lib then return end
-- Lua API
5 months ago
local next = next
4 years ago
local type = type
local unpack = unpack
local combatLockdown = InCombatLockdown()
lib.protectedActions = {}
local function protRunStoredActions()
5 months ago
local func, params = next(lib.protectedActions)
while func do
4 years ago
if combatLockdown then break end
func(unpack(params))
5 months ago
lib.protectedActions[func] = nil
func, params = next(lib.protectedActions)
4 years ago
end
end
function lib:prot(method, object, ...)
local func
if type(method) == "string" then
func = object[method]
else
func = method
end
local params = { object, ... }
if combatLockdown then
5 months ago
lib.protectedActions[func] = params
4 years ago
else
func(unpack(params))
end
end
-- Events
lib.eventFrame = lib.eventFrame or CreateFrame("Frame")
lib.eventFrame:SetScript("OnEvent", function(_, event)
if event == "PLAYER_REGEN_DISABLED" then
combatLockdown = true
elseif event == "PLAYER_REGEN_ENABLED" then
combatLockdown = false
protRunStoredActions()
end
end)
lib.eventFrame:RegisterEvent("PLAYER_REGEN_DISABLED")
lib.eventFrame:RegisterEvent("PLAYER_REGEN_ENABLED")
------------------------------------------------------------------------------------------------------------------------
-- Embed handling
------------------------------------------------------------------------------------------------------------------------
lib.embeds = lib.embeds or {}
local mixins = {
5 months ago
"prot"
4 years ago
}
function lib:Embed(target)
lib.embeds[target] = true
for _, v in next, mixins do
target[v] = lib[v]
end
return target
end
for addon in next, lib.embeds do
lib:Embed(addon)
end