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
13 local EventRegistrar
= {}
15 local OnUpdate_Keyed
= {}
17 local qh_event_frame
= CreateFrame("Frame")
21 function QH_Hook_NotMyFault(func
, ...)
25 local function wraptime(ident
, func
, ...)
26 QuestHelper
: Assert(ident
)
29 if qh_loud_and_annoying
then
30 qhh_nmf
= QH_Hook_NotMyFault
31 QH_Hook_NotMyFault
= function(func
, ...)
33 --print("a", GetTime())
35 --print("c", GetTime(), GetTime() - zst, qhh_adj)
36 qhh_adj
= qhh_adj
+ (GetTime() - zst
)
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))
48 QH_Hook_NotMyFault
= qhh_nmf
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
, ...)
60 time_used
= time_used
+ (GetTime() - tstart
)
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
)
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
] = {}
80 table.insert(EventRegistrar
[event
], {func
= func
, id
= identifier
})
86 local last_frame
= GetTime()
87 local time_per_frame
= 0.05 -- Assume 20fps so we can get a little early loading
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
, ...)
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)
111 if tls < GetTime() - 1 then
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)))
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()
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
140 OnUpdate
[target
] = nil
142 OnUpdate
[target
] = {func
= function (...) func(target
, ...) end, id
= identifier
}
146 target
:SetScript(hookname
, nil)
148 target
:SetScript(hookname
, function (...) wraptime(identifier
, func
, ...) end)