1 QuestHelper_File
["timeslice.lua"] = "Development Version"
2 QuestHelper_Loadtime
["timeslice.lua"] = GetTime()
4 local debug_output
= (QuestHelper_File
["timeslice.lua"] == "Development Version")
6 -- Any non-local item here is part of an available public interface.
8 local coroutine_running
= false
9 local coroutine_stop_time
= 0
10 local coroutine_panic_time
= 0
11 local coroutine_list
= {}
12 local coroutine_route_pass
= 1
14 local coroutine_verbose
= false
16 local coroutine_time_used
= {}
17 local coroutine_power_up
= GetTime()
19 local coroutine_time_exceeded
= 0
21 function QH_Timeslice_DumpPerf()
23 for k
, v
in pairs(coroutine_time_used
) do
24 table.insert(sortable
, {name
= k
, amount
= v
})
26 table.sort(sortable
, function(a
, b
) return a
.name
< b
.name
end)
27 for _
, v
in pairs(sortable
) do
28 QuestHelper
:TextOut(string.format("%s: %f", QuestHelper
:HighlightText(v
.name
), v
.amount
))
30 QuestHelper
:TextOut(string.format("%s: %f", QuestHelper
:HighlightText("poweron"), GetTime() - coroutine_power_up
))
33 local last_stack
= nil
35 function QH_Timeslice_Yield()
36 if coroutine_running
then
37 -- Check if we've run our alotted time
38 yield_ct
= yield_ct
+ 1
39 if GetTime() > coroutine_stop_time
then
40 -- As a safety, reset stop time to 0. If somehow we fail to set it next time,
41 -- we'll be sure to yield promptly.
42 coroutine_stop_time
= 0
44 local sti
= debugstack(2, 5, 5) -- string.gsub(debugstack(2, 1, 1), "\n.*", "")
45 if qh_loud_and_annoying
and GetTime() > coroutine_panic_time
then
46 print(yield_ct
, "took too long", last_stack
, "------", sti
, "------", GetTime() - coroutine_panic_time
)
55 function QH_Timeslice_Bonus(quantity
)
56 if coroutine_verbose
then QuestHelper
:TextOut(string.format("timeslice: %d bonus", quantity
)) end
57 coroutine_route_pass
= coroutine_route_pass
+ quantity
68 function QH_Timeslice_Add(workfunc
, name
)
69 QuestHelper
: Assert(workfunc
)
70 QuestHelper
: Assert(name
)
71 local priority
= prioritize
[name
] and prioritize
[name
][1] or 0
72 local sharding
= prioritize
[name
] and prioritize
[name
][2] or 1
73 if coroutine_verbose
then QuestHelper
:TextOut(string.format("timeslice: %s added (%s, %d)", name
, tostring(workfunc
), priority
)) end
74 local ncoro
= coroutine
.create(workfunc
)
75 QuestHelper
: Assert(ncoro
)
76 table.insert(coroutine_list
, {priority
= priority
, sharding
= sharding
, name
= name
, coro
= ncoro
, active
= true})
79 function QH_Timeslice_Toggle(name
, flag
)
80 --if coroutine_verbose then QuestHelper:TextOut(string.format("timeslice: %s toggled to %s", name, tostring(not not flag))) end
81 for _
, v
in pairs(coroutine_list
) do
82 if v
.name
== name
then v
.active
= flag
end
88 function QH_Timeslice_Doneinit()
91 QuestHelper
:TextOut("Done with initialization step")
94 QuestHelper
.loading_main
= nil
95 QuestHelper
.loading_flightpath
= nil
96 QuestHelper
.loading_preroll
= nil
98 collectgarbage("collect") -- fuuuuuck youuuuuuuu
104 function QH_Timeslice_Work()
105 -- There's probably a better way to do this, but. Eh. Lua.
108 for k
, v
in pairs(coroutine_list
) do
110 --if v.sharding then QuestHelper:TextOut(string.format("%d mod %d is %d, %s", time(), v.sharding, bit.mod(time(), v.sharding), tostring(bit.mod(time(), v.sharding) == 0))) end
111 if (not v
.sharding
or bit
.mod(time(), v
.sharding
) == 0) and (not coro
or (v
.priority
> coro
.priority
)) then
119 --if coroutine_verbose then QuestHelper:TextOut(string.format("timeslice: %s running", coro.name)) end
121 if coroutine
.status(coro
.coro
) == "dead" then -- Someone was claiming to get an infinite loop with this. I don't see how it's possible, but this should at least fix the infinite loop.
122 coroutine_list
[key
] = nil
123 QuestHelper
: Assert(coroutine
.status(coro
.coro
) ~= "dead")
126 local slicefactor
= (QuestHelper_Pref
.hide
and 0.01 or (QuestHelper_Pref
.perf_scale
* math
.min(coroutine_route_pass
, 5)))
127 if not started
then slicefactor
= 5 * QuestHelper_Pref
.perfload_scale
* math
.min(coroutine_route_pass
, 5) end -- the init process gets much higher priority so we get done with it faster
128 local coroutine_intended_stop_time
= GetTime() + 2e-3 * slicefactor
129 coroutine_stop_time
= coroutine_intended_stop_time
- coroutine_time_exceeded
130 coroutine_panic_time
= coroutine_stop_time
+ 2e-3 * slicefactor
131 coroutine_route_pass
= coroutine_route_pass
- 5
132 if coroutine_route_pass
<= 0 then coroutine_route_pass
= 1 end
134 local start
= GetTime()
135 local state
, err
= true, nil -- default values for "we're fine"
136 if start
< coroutine_stop_time
then -- We don't want to just return on failure because we want to credit the exceeded time properly.
137 coroutine_running
= true
138 state
, err
= coroutine
.resume(coro
.coro
)
139 coroutine_running
= false
141 local total
= GetTime() - start
143 local coroutine_this_cycle_exceeded
= GetTime() - coroutine_intended_stop_time
-- may be either positive or negative
144 coroutine_time_exceeded
= coroutine_time_exceeded
+ coroutine_this_cycle_exceeded
146 coroutine_time_used
[coro
.name
] = (coroutine_time_used
[coro
.name
] or 0) + total
149 if coroutine_verbose
then QuestHelper
:TextOut(string.format("timeslice: %s errored", coro
.name
)) end
150 QuestHelper_ErrorCatcher_ExplicitError(true, err
, "", string.format("(Coroutine error in %s)\n", coro
.name
))
153 QuestHelper
: Assert(coro
.coro
)
154 if coroutine
.status(coro
.coro
) == "dead" then
155 if coroutine_verbose
then QuestHelper
:TextOut(string.format("timeslice: %s complete", coro
.name
)) end
156 coroutine_list
[key
] = nil
159 if coroutine_verbose
then QuestHelper
:TextOut(string.format("timeslice: no available tasks")) end
163 function QH_Timeslice_Increment(quantity
, name
)
164 local an
= "(nc) " .. name
165 coroutine_time_used
[an
] = (coroutine_time_used
[an
] or 0) + quantity