405 lines
13 KiB
Lua
405 lines
13 KiB
Lua
local ____lualib = require("lualib_bundle")
|
|
local __TS__New = ____lualib.__TS__New
|
|
local __TS__InstanceOf = ____lualib.__TS__InstanceOf
|
|
local __TS__ArraySetLength = ____lualib.__TS__ArraySetLength
|
|
local ____exports = __module.env
|
|
local M__share_base_scoped_object = __MODULE__.Import("share.base.scoped_object", __module)
|
|
local ScopedObject = M__share_base_scoped_object.ScopedObject
|
|
local M__share_base_traceback = __MODULE__.Import("share.base.traceback", __module)
|
|
local tbpcall = M__share_base_traceback.tbpcall
|
|
____exports.TimerHandle = __TS__DefineClass("TimerHandle", __module)
|
|
local TimerHandle = ____exports.TimerHandle
|
|
TimerHandle.name = "TimerHandle"
|
|
function TimerHandle.prototype.____constructor(self, scheduler, entryId)
|
|
self.disposed = false
|
|
self.scheduler = scheduler
|
|
self.entryId = entryId
|
|
end
|
|
function TimerHandle.prototype.GetId(self)
|
|
return self.entryId
|
|
end
|
|
function TimerHandle.prototype.IsDisposed(self)
|
|
return self.disposed
|
|
end
|
|
function TimerHandle.prototype.IsActive(self)
|
|
return not self.disposed and self.scheduler ~= nil and self.scheduler:IsEntryActive(self.entryId)
|
|
end
|
|
function TimerHandle.prototype.Cancel(self)
|
|
self:Dispose()
|
|
end
|
|
function TimerHandle.prototype.Dispose(self)
|
|
if self.disposed then
|
|
return
|
|
end
|
|
self.disposed = true
|
|
if self.scheduler ~= nil then
|
|
local s = self.scheduler
|
|
self.scheduler = nil
|
|
s:CancelById(self.entryId)
|
|
end
|
|
end
|
|
function TimerHandle.prototype.MarkCompletedInternal(self)
|
|
self.disposed = true
|
|
self.scheduler = nil
|
|
end
|
|
____exports.FrameScheduler = __TS__DefineClass("FrameScheduler", __module)
|
|
local FrameScheduler = ____exports.FrameScheduler
|
|
FrameScheduler.name = "FrameScheduler"
|
|
function FrameScheduler.prototype.____constructor(self)
|
|
self.nextId = 0
|
|
self.monotonicTime = 0
|
|
self.worldTime = 0
|
|
self.currentWorldGen = 1
|
|
self.frameCounter = 0
|
|
self.disposed = false
|
|
self.entriesById = {}
|
|
self.frameIds = {}
|
|
self.worldIds = {}
|
|
self.monotonicIds = {}
|
|
end
|
|
function FrameScheduler.prototype.IsDisposed(self)
|
|
return self.disposed
|
|
end
|
|
function FrameScheduler.prototype.NowSeconds(self)
|
|
return self.monotonicTime
|
|
end
|
|
function FrameScheduler.prototype.GetWorldTimeSeconds(self)
|
|
return self.worldTime
|
|
end
|
|
function FrameScheduler.prototype.GetFrameCount(self)
|
|
return self.frameCounter
|
|
end
|
|
function FrameScheduler.prototype.GetCurrentWorldGen(self)
|
|
return self.currentWorldGen
|
|
end
|
|
function FrameScheduler.prototype.IsEntryActive(self, id)
|
|
local entry = self.entriesById[id]
|
|
return entry ~= nil and not entry.cancelled
|
|
end
|
|
function FrameScheduler.prototype.AddFrameTimer(self, frames, callback, owner)
|
|
local count = math.max(
|
|
1,
|
|
math.floor(frames)
|
|
)
|
|
return self:CreateEntry(
|
|
"frame",
|
|
count,
|
|
0,
|
|
false,
|
|
callback,
|
|
owner
|
|
)
|
|
end
|
|
function FrameScheduler.prototype.RunNextFrame(self, callback, owner)
|
|
return self:AddFrameTimer(1, callback, owner)
|
|
end
|
|
function FrameScheduler.prototype.AddWorldTimer(self, delaySeconds, callback, owner)
|
|
local delay = math.max(0, delaySeconds)
|
|
return self:CreateEntry(
|
|
"world",
|
|
0,
|
|
delay,
|
|
false,
|
|
callback,
|
|
owner
|
|
)
|
|
end
|
|
function FrameScheduler.prototype.AddWorldRepeatTimer(self, intervalSeconds, callback, owner)
|
|
if intervalSeconds <= 0 then
|
|
return self:CreateEntry(
|
|
"frame",
|
|
1,
|
|
0,
|
|
true,
|
|
callback,
|
|
owner
|
|
)
|
|
end
|
|
return self:CreateEntry(
|
|
"world",
|
|
0,
|
|
intervalSeconds,
|
|
true,
|
|
callback,
|
|
owner
|
|
)
|
|
end
|
|
function FrameScheduler.prototype.AddTimer(self, delaySeconds, callback, owner)
|
|
return self:AddMonotonicTimer(delaySeconds, callback, owner)
|
|
end
|
|
function FrameScheduler.prototype.AddRepeatTimer(self, intervalSeconds, callback, owner)
|
|
return self:AddMonotonicRepeatTimer(intervalSeconds, callback, owner)
|
|
end
|
|
function FrameScheduler.prototype.AddMonotonicTimer(self, delaySeconds, callback, owner)
|
|
local delay = math.max(0, delaySeconds)
|
|
return self:CreateEntry(
|
|
"monotonic",
|
|
0,
|
|
delay,
|
|
false,
|
|
callback,
|
|
owner
|
|
)
|
|
end
|
|
function FrameScheduler.prototype.AddMonotonicRepeatTimer(self, intervalSeconds, callback, owner)
|
|
local interval = math.max(0.001, intervalSeconds)
|
|
return self:CreateEntry(
|
|
"monotonic",
|
|
0,
|
|
interval,
|
|
true,
|
|
callback,
|
|
owner
|
|
)
|
|
end
|
|
function FrameScheduler.prototype.CreateEntry(self, domain, remainingFrames, intervalSec, ____repeat, callback, owner)
|
|
self.nextId = self.nextId + 1
|
|
local id = self.nextId
|
|
local handle = __TS__New(____exports.TimerHandle, self, id)
|
|
if self.disposed or owner ~= nil and owner:IsDisposed() then
|
|
handle:MarkCompletedInternal()
|
|
return handle
|
|
end
|
|
local baseTime = domain == "world" and self.worldTime or self.monotonicTime
|
|
local entry = {
|
|
id = id,
|
|
domain = domain,
|
|
callback = callback,
|
|
owner = owner,
|
|
["repeat"] = ____repeat,
|
|
cancelled = false,
|
|
remainingFrames = remainingFrames,
|
|
intervalSec = intervalSec,
|
|
nextTriggerTime = baseTime + intervalSec,
|
|
worldGen = self.currentWorldGen,
|
|
handle = handle
|
|
}
|
|
self.entriesById[id] = entry
|
|
if domain == "frame" then
|
|
self.frameIds[#self.frameIds + 1] = id
|
|
elseif domain == "world" then
|
|
self.worldIds[#self.worldIds + 1] = id
|
|
else
|
|
self.monotonicIds[#self.monotonicIds + 1] = id
|
|
end
|
|
if owner ~= nil and __TS__InstanceOf(owner, ScopedObject) then
|
|
owner:AddScopedObject(handle)
|
|
end
|
|
return handle
|
|
end
|
|
function FrameScheduler.prototype.CancelById(self, id)
|
|
local entry = self.entriesById[id]
|
|
if entry == nil then
|
|
return
|
|
end
|
|
entry.cancelled = true
|
|
self.entriesById[id] = nil
|
|
end
|
|
function FrameScheduler.prototype.InvalidateWorld(self)
|
|
local prevGen = self.currentWorldGen
|
|
self.currentWorldGen = self.currentWorldGen + 1
|
|
self.worldTime = 0
|
|
self:CancelEntriesForWorldGen(self.worldIds, prevGen)
|
|
self:CancelEntriesForWorldGen(self.frameIds, prevGen)
|
|
return self.currentWorldGen
|
|
end
|
|
function FrameScheduler.prototype.CancelEntriesForWorldGen(self, idList, worldGen)
|
|
local kept = {}
|
|
do
|
|
local i = 0
|
|
while i < #idList do
|
|
do
|
|
local id = idList[i + 1]
|
|
local entry = self.entriesById[id]
|
|
if entry == nil or entry.cancelled then
|
|
goto __continue38
|
|
end
|
|
if entry.worldGen == worldGen then
|
|
entry.cancelled = true
|
|
self.entriesById[id] = nil
|
|
entry.handle:MarkCompletedInternal()
|
|
else
|
|
kept[#kept + 1] = id
|
|
end
|
|
end
|
|
::__continue38::
|
|
i = i + 1
|
|
end
|
|
end
|
|
__TS__ArraySetLength(idList, 0)
|
|
do
|
|
local i = 0
|
|
while i < #kept do
|
|
idList[i + 1] = kept[i + 1]
|
|
i = i + 1
|
|
end
|
|
end
|
|
end
|
|
function FrameScheduler.prototype.Tick(self, unscaledDeltaSeconds, worldDeltaSeconds, isWorldPaused)
|
|
if isWorldPaused == nil then
|
|
isWorldPaused = false
|
|
end
|
|
if self.disposed then
|
|
return
|
|
end
|
|
local safeUnscaled = math.max(0, unscaledDeltaSeconds)
|
|
self.monotonicTime = self.monotonicTime + safeUnscaled
|
|
self.frameCounter = self.frameCounter + 1
|
|
self:ProcessTimedQueue(self.monotonicIds, self.monotonicTime)
|
|
if not isWorldPaused then
|
|
local safeWorldDt = math.max(0, worldDeltaSeconds or safeUnscaled)
|
|
self.worldTime = self.worldTime + safeWorldDt
|
|
self:ProcessTimedQueue(self.worldIds, self.worldTime)
|
|
end
|
|
self:ProcessFrameQueue()
|
|
end
|
|
function FrameScheduler.prototype.ProcessFrameQueue(self)
|
|
local snapshotLen = #self.frameIds
|
|
local nextFrameIds = {}
|
|
do
|
|
local i = 0
|
|
while i < snapshotLen do
|
|
do
|
|
local id = self.frameIds[i + 1]
|
|
local entry = self.entriesById[id]
|
|
if entry == nil or entry.cancelled then
|
|
goto __continue49
|
|
end
|
|
if entry.owner ~= nil and entry.owner:IsDisposed() then
|
|
entry.cancelled = true
|
|
self.entriesById[id] = nil
|
|
entry.handle:MarkCompletedInternal()
|
|
goto __continue49
|
|
end
|
|
entry.remainingFrames = entry.remainingFrames - 1
|
|
if entry.remainingFrames <= 0 then
|
|
if not entry["repeat"] then
|
|
entry.cancelled = true
|
|
self.entriesById[id] = nil
|
|
entry.handle:MarkCompletedInternal()
|
|
else
|
|
entry.remainingFrames = 1
|
|
nextFrameIds[#nextFrameIds + 1] = id
|
|
end
|
|
tbpcall(entry.callback)
|
|
else
|
|
nextFrameIds[#nextFrameIds + 1] = id
|
|
end
|
|
end
|
|
::__continue49::
|
|
i = i + 1
|
|
end
|
|
end
|
|
do
|
|
local i = snapshotLen
|
|
while i < #self.frameIds do
|
|
local id = self.frameIds[i + 1]
|
|
local entry = self.entriesById[id]
|
|
if entry ~= nil and not entry.cancelled then
|
|
nextFrameIds[#nextFrameIds + 1] = id
|
|
end
|
|
i = i + 1
|
|
end
|
|
end
|
|
self.frameIds = nextFrameIds
|
|
end
|
|
function FrameScheduler.prototype.ProcessTimedQueue(self, idList, currentTime)
|
|
local snapshotLen = #idList
|
|
local kept = {}
|
|
do
|
|
local i = 0
|
|
while i < snapshotLen do
|
|
do
|
|
local id = idList[i + 1]
|
|
local entry = self.entriesById[id]
|
|
if entry == nil or entry.cancelled then
|
|
goto __continue61
|
|
end
|
|
if entry.owner ~= nil and entry.owner:IsDisposed() then
|
|
entry.cancelled = true
|
|
self.entriesById[id] = nil
|
|
entry.handle:MarkCompletedInternal()
|
|
goto __continue61
|
|
end
|
|
if currentTime + 1e-9 >= entry.nextTriggerTime then
|
|
if not entry["repeat"] then
|
|
entry.cancelled = true
|
|
self.entriesById[id] = nil
|
|
entry.handle:MarkCompletedInternal()
|
|
tbpcall(entry.callback)
|
|
else
|
|
entry.nextTriggerTime = currentTime + entry.intervalSec
|
|
tbpcall(entry.callback)
|
|
if not entry.cancelled and self.entriesById[id] ~= nil then
|
|
kept[#kept + 1] = id
|
|
end
|
|
end
|
|
else
|
|
kept[#kept + 1] = id
|
|
end
|
|
end
|
|
::__continue61::
|
|
i = i + 1
|
|
end
|
|
end
|
|
do
|
|
local i = snapshotLen
|
|
while i < #idList do
|
|
local id = idList[i + 1]
|
|
local entry = self.entriesById[id]
|
|
if entry ~= nil and not entry.cancelled then
|
|
kept[#kept + 1] = id
|
|
end
|
|
i = i + 1
|
|
end
|
|
end
|
|
__TS__ArraySetLength(idList, 0)
|
|
do
|
|
local i = 0
|
|
while i < #kept do
|
|
idList[i + 1] = kept[i + 1]
|
|
i = i + 1
|
|
end
|
|
end
|
|
end
|
|
function FrameScheduler.prototype.GetStats(self)
|
|
local frameTasks = self:CountActive(self.frameIds)
|
|
local worldTasks = self:CountActive(self.worldIds)
|
|
local monotonicTasks = self:CountActive(self.monotonicIds)
|
|
return {frameTasks = frameTasks, worldTasks = worldTasks, monotonicTasks = monotonicTasks, totalTasks = frameTasks + worldTasks + monotonicTasks}
|
|
end
|
|
function FrameScheduler.prototype.CountActive(self, idList)
|
|
local count = 0
|
|
do
|
|
local i = 0
|
|
while i < #idList do
|
|
local entry = self.entriesById[idList[i + 1]]
|
|
if entry ~= nil and not entry.cancelled then
|
|
count = count + 1
|
|
end
|
|
i = i + 1
|
|
end
|
|
end
|
|
return count
|
|
end
|
|
function FrameScheduler.prototype.Dispose(self)
|
|
if self.disposed then
|
|
return
|
|
end
|
|
self.disposed = true
|
|
for id, entry in pairs(self.entriesById) do
|
|
if entry ~= nil then
|
|
entry.cancelled = true
|
|
entry.handle:MarkCompletedInternal()
|
|
self.entriesById[id] = nil
|
|
end
|
|
end
|
|
self.frameIds = {}
|
|
self.worldIds = {}
|
|
self.monotonicIds = {}
|
|
end
|
|
function __module.UpdateImports()
|
|
ScopedObject = M__share_base_scoped_object.ScopedObject
|
|
tbpcall = M__share_base_traceback.tbpcall
|
|
end
|