fffff
[QuestHelper.git] / manager_event.lua
blobb3278d1f2f7c3f0b7892547c1bdf591380b6d188
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 if QuestHelper then QuestHelper: Assert(ident) end
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 not qh_hackery_eventless and 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 -- I'm just putting this here so I can stop rewriting it
88 --[[
89 Try "/script qh_hackery_no_work = true". See if that fixes things.
91 Whether it does or not, log out, log back in, then do "/qh hackery_event_timing = true". Wait a few seconds, then take a screenshot. Post that here.
94 local last_frame = GetTime()
95 local time_per_frame = 0.01 -- Assume 100fps so we don't fuck with people's framerate
97 local OnUpdate = {}
98 local OnUpdateHigh = {}
99 local function OnUpdateTrigger(_, ...)
100 if not QuestHelper then return end
102 if not next_started then next_started, time_used = true, 0 end
105 local tstart = GetTime()
106 for _, v in pairs(OnUpdateHigh) do
107 wraptime(v.id, v.func, ...)
110 for _, v in pairs(OnUpdate) do
111 if v.func then wraptime(v.id, v.func, ...) end
113 time_used = time_used + (GetTime() - tstart)
116 local tframe = GetTime()
117 local tplf = tframe - last_frame
118 tplf = math.min(time_per_frame + 0.1, tplf)
119 local tplf_weight = tplf * 20
120 time_per_frame = (time_per_frame * (1 / (tplf_weight + 0.0005)) + tplf) / (1 + 1 / (tplf_weight + 0.0005))
122 QuestHelper: Assert(time_per_frame > 0 and time_per_frame < 10000) -- hmmm
124 local verbose = false
125 if qh_hackery_event_timing and tls < GetTime() - 1 then
126 tls = GetTime()
127 print(string.format("Avg TPF %f, current TPLF %f, time_used %f, this adjustment %f, bonus time %f", time_per_frame, tplf, time_used, time_per_frame - tplf - time_used, math.min(time_per_frame - tplf, (time_per_frame - tplf) * 0.8, 0.05)))
128 verbose = true
130 if not qh_hackery_no_work then
131 QH_Timeslice_Work(time_used, time_per_frame, math.min(time_per_frame - tplf, (time_per_frame - tplf) * 0.8, 0.05), verbose)
133 last_frame = GetTime()
135 next_started = false
138 function QH_OnUpdate(func, identifier)
139 if not identifier then identifier = "(unknown onupdate)" end
140 table.insert(OnUpdate, {func = func, id = identifier})
143 function QH_OnUpdate_High(func, identifier)
144 if not identifier then identifier = "(unknown high-onupdate)" end
145 table.insert(OnUpdateHigh, {func = func, id = identifier})
148 qh_event_frame:SetScript("OnUpdate", OnUpdateTrigger)
151 function QH_Hook(target, hookname, func, identifier)
152 if not identifier then identifier = string.format("(unknown hook %s/%s)", hookname, tostring(target)) end
153 if hookname == "OnUpdate" then
154 if not func then
155 OnUpdate[target] = nil
156 else
157 OnUpdate[target] = {func = function (...) func(target, ...) end, id = identifier}
159 else
160 if not func then
161 target:SetScript(hookname, nil)
162 else
163 target:SetScript(hookname, function (...) wraptime(identifier, func, ...) end)