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.

97 lines
3.2 KiB

4 years ago
local Details = _G.Details
local detailsFramework = _G.DetailsFramework
4 years ago
local C_Timer = _G.C_Timer
local addonName, Details222 = ...
local load = loadstring
4 years ago
--auto run scripts
local functionCache = {}
4 years ago
--compile and store code
function Details222.AutoRunCode.RecompileAutoRunCode()
for codeKey, code in pairs(Details222.AutoRunCode.CodeTable) do
local func, errorText = load(code)
4 years ago
if (func) then
detailsFramework:SetEnvironment(func)
functionCache[codeKey] = func
4 years ago
else
--if the code didn't pass, create a dummy function for it without triggering errors
functionCache[codeKey] = function() end
4 years ago
end
end
end
--function to dispatch events
function Details222.AutoRunCode.DispatchAutoRunCode(codeKey)
local func = functionCache[codeKey]
3 years ago
if (type(func) ~= "function") then
Details:Msg("error running function for auto run script", codeKey)
return
end
local okay, errortext = pcall(func)
if (not okay) then
Details:Msg("error running auto run script: ", codeKey, errortext)
return
end
4 years ago
end
--auto run frame to dispatch scrtips for some events that details! doesn't handle
local autoRunCodeEventFrame = CreateFrame("frame")
4 years ago
if (not detailsFramework.IsTimewalkWoW()) then
4 years ago
autoRunCodeEventFrame:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED")
end
autoRunCodeEventFrame.OnEventFunc = function(self, event)
3 years ago
--ignore events triggered more than once in a small time window
if (autoRunCodeEventFrame[event] and not autoRunCodeEventFrame[event]:IsCancelled()) then
4 years ago
return
end
if (event == "PLAYER_SPECIALIZATION_CHANGED") then
3 years ago
--create a trigger for the event, many times it is triggered more than once
--so if the event is triggered a second time, it will be ignored
4 years ago
local newTimer = C_Timer.NewTimer(1, function()
Details222.AutoRunCode.DispatchAutoRunCode("on_specchanged")
3 years ago
--clear and invalidate the timer
4 years ago
autoRunCodeEventFrame[event]:Cancel()
autoRunCodeEventFrame[event] = nil
end)
3 years ago
--store the trigger
4 years ago
autoRunCodeEventFrame[event] = newTimer
end
end
autoRunCodeEventFrame:SetScript("OnEvent", autoRunCodeEventFrame.OnEventFunc)
--dispatch scripts at startup
C_Timer.After(2, function()
Details222.AutoRunCode.DispatchAutoRunCode("on_init")
Details222.AutoRunCode.DispatchAutoRunCode("on_specchanged")
Details222.AutoRunCode.DispatchAutoRunCode("on_zonechanged")
4 years ago
if (_G.InCombatLockdown()) then
Details222.AutoRunCode.DispatchAutoRunCode("on_entercombat")
4 years ago
else
Details222.AutoRunCode.DispatchAutoRunCode("on_leavecombat")
4 years ago
end
Details222.AutoRunCode.DispatchAutoRunCode("on_groupchange")
4 years ago
end)
function Details222.AutoRunCode.StartAutoRun()
local newData = detailsFramework.table.copy({}, Details.run_code)
Details.run_code = nil
Details222.AutoRunCode.CodeTable = newData
Details222.AutoRunCode.RecompileAutoRunCode()
4 years ago
end
function Details222.AutoRunCode.OnLogout()
_detalhes_global.run_code = Details222.AutoRunCode.CodeTable
end