Merge branch 'master' into translations
[QuestHelper.git] / manager_event.lua
blobd92b512b4fd626b96b09f63e9172f44eb8916dd8
1 QuestHelper_File["manager_event.lua"] = "Development Version"
2 QuestHelper_Loadtime["manager_event.lua"] = GetTime()
4 -- zorba why does this file exist, are you a terrible person? you are a terrible person aren't you
5 -- yep, I'm a terrible person
7 -- File exists to centralize all the event hooks in one place. QH should never (rarely) eat CPU without going through this file. As a nice side effect, I can use this to measure when QH is using CPU. yaaaaay
9 local next_started = false
11 local time_used = 0
13 local EventRegistrar = {}
15 local OnUpdate_Keyed = {}
17 local qh_event_frame = CreateFrame("Frame")
21 function QH_Hook_NotMyFault(func, ...)
22 return func(...)
23 end
25 local function wraptime(ident, func, ...)
26 QuestHelper: Assert(ident)
27 local st, qhh_nmf
28 local qhh_adj = 0
29 if qh_loud_and_annoying then
30 qhh_nmf = QH_Hook_NotMyFault
31 QH_Hook_NotMyFault = function(func, ...)
32 local zst = GetTime()
33 --print("a", GetTime())
34 return (function(...)
35 --print("c", GetTime(), GetTime() - zst, qhh_adj)
36 qhh_adj = qhh_adj + (GetTime() - zst)
37 --print(qhh_adj)
38 return ...
39 end)(func(...))
40 end
41 st = GetTime()
42 end
43 func(...)
44 if qh_loud_and_annoying then
45 if GetTime() - st - qhh_adj > 0.0025 then
46 QuestHelper: TextOut(string.format("Took way too long, %4f, at %s (adjusted by %4f)", (GetTime() - st - qhh_adj) * 1000, ident, qhh_adj * 1000))
47 end
48 QH_Hook_NotMyFault = qhh_nmf
49 end
50 end
52 local function OnEvent(_, event, ...)
53 if not next_started then next_started, time_used = true, 0 end
55 if EventRegistrar[event] then
56 local tstart = GetTime()
57 for _, v in pairs(EventRegistrar[event]) do
58 wraptime(v.id, v.func, ...)
59 end
60 time_used = time_used + (GetTime() - tstart)
61 end
62 end
64 qh_event_frame:UnregisterAllEvents()
65 qh_event_frame:RegisterAllEvents() -- I wonder what the performance penalty of this actually is
66 qh_event_frame:SetScript("OnEvent", OnEvent)
68 function QH_Event(event, func, identifier)
69 QuestHelper: Assert(func)
70 if type(event) == "table" then
71 for _, v in ipairs(event) do
72 QH_Event(v, func, identifier)
73 end
74 else
75 if not identifier then identifier = "(unknown event " .. event .. ")" end
76 if not EventRegistrar[event] then
77 --qh_event_frame:RegisterEvent(event)
78 EventRegistrar[event] = {}
79 end
80 table.insert(EventRegistrar[event], {func = func, id = identifier})
81 end
82 end
84 local tls = GetTime()
86 local last_frame = GetTime()
87 local time_per_frame = 0.05 -- Assume 20fps so we can get a little early loading
89 local OnUpdate = {}
90 local OnUpdateHigh = {}
91 local function OnUpdateTrigger(_, ...)
92 if not next_started then next_started, time_used = true, 0 end
95 local tstart = GetTime()
96 for _, v in pairs(OnUpdateHigh) do
97 wraptime(v.id, v.func, ...)
98 end
100 for _, v in pairs(OnUpdate) do
101 if v.func then wraptime(v.id, v.func, ...) end
103 time_used = time_used + (GetTime() - tstart)
106 local tframe = GetTime()
107 local tplf = tframe - last_frame
108 time_per_frame = math.exp(math.log(time_per_frame) * 0.99 + math.log(tplf + 0.0000000001) * 0.01)
110 --[[
111 if tls < GetTime() - 1 then
112 tls = GetTime()
113 print(string.format("Avg TPF %f, current TPLF %f, time_used %f, this adjustment %f", time_per_frame, tplf, time_used, time_used - (time_per_frame - tplf)))
114 end]]
115 if not qh_hackery_no_work then
116 QH_Timeslice_Work(time_used, time_per_frame, math.min(math.min(time_per_frame - tplf, (time_per_frame - tplf) * 0.8), 0.05))
118 last_frame = GetTime()
120 next_started = false
123 function QH_OnUpdate(func, identifier)
124 if not identifier then identifier = "(unknown onupdate)" end
125 table.insert(OnUpdate, {func = func, id = identifier})
128 function QH_OnUpdate_High(func, identifier)
129 if not identifier then identifier = "(unknown high-onupdate)" end
130 table.insert(OnUpdateHigh, {func = func, id = identifier})
133 qh_event_frame:SetScript("OnUpdate", OnUpdateTrigger)
136 function QH_Hook(target, hookname, func, identifier)
137 if not identifier then identifier = string.format("(unknown hook %s/%s)", hookname, tostring(target)) end
138 if hookname == "OnUpdate" then
139 if not func then
140 OnUpdate[target] = nil
141 else
142 OnUpdate[target] = {func = function (...) func(target, ...) end, id = identifier}
144 else
145 if not func then
146 target:SetScript(hookname, nil)
147 else
148 target:SetScript(hookname, function (...) wraptime(identifier, func, ...) end)