re-introduce the qh hidden notice
[QuestHelper.git] / manager_event.lua
blob92cfd79fa12fa1ecf8337d19df759abe4999d654
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 tplf = math.min(time_per_frame + 0.1, tplf)
109 time_per_frame = (time_per_frame * (1 / (tplf + 0.0005)) + tplf) / (1 + 1 / (tplf + 0.0005))
111 QuestHelper: Assert(time_per_frame > 0 and time_per_frame < 10000) -- hmmm
113 if qh_hackery_event_timing and tls < GetTime() - 1 then
114 tls = GetTime()
115 print(string.format("Avg TPF %f, current TPLF %f, time_used %f, this adjustment %f", time_per_frame, tplf, time_used, time_per_frame - tplf - time_used))
117 if not qh_hackery_no_work then
118 QH_Timeslice_Work(time_used, time_per_frame, math.min(math.min(time_per_frame - tplf, (time_per_frame - tplf) * 0.8), 0.05))
120 last_frame = GetTime()
122 next_started = false
125 function QH_OnUpdate(func, identifier)
126 if not identifier then identifier = "(unknown onupdate)" end
127 table.insert(OnUpdate, {func = func, id = identifier})
130 function QH_OnUpdate_High(func, identifier)
131 if not identifier then identifier = "(unknown high-onupdate)" end
132 table.insert(OnUpdateHigh, {func = func, id = identifier})
135 qh_event_frame:SetScript("OnUpdate", OnUpdateTrigger)
138 function QH_Hook(target, hookname, func, identifier)
139 if not identifier then identifier = string.format("(unknown hook %s/%s)", hookname, tostring(target)) end
140 if hookname == "OnUpdate" then
141 if not func then
142 OnUpdate[target] = nil
143 else
144 OnUpdate[target] = {func = function (...) func(target, ...) end, id = identifier}
146 else
147 if not func then
148 target:SetScript(hookname, nil)
149 else
150 target:SetScript(hookname, function (...) wraptime(identifier, func, ...) end)