-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
253 lines (204 loc) · 5.23 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
local ReplicatedStorage = game:GetService("ReplicatedStorage")
type SystemInfo = {
name: string,
phase: any,
system: (...any) -> ...any,
}
type HookArgs = {
scheduler: any,
system: SystemInfo,
nextFn: (...any) -> ...any,
}
type PhaseBeganArgs = {
scheduler: any,
phase: any,
}
type SystemsAddRemove = {
scheduler: any,
system: SystemInfo,
}
type SystemsReplace = {
scheduler: any,
new: SystemInfo,
old: SystemInfo,
}
type Plugin = {
build: (self: Plugin, scheduler: any) -> (),
new: (module: ModuleScript?) -> Plugin,
}
type ConnectionObject = {
Disconnect: (() -> ())?,
Destroy: (() -> ())?,
disconnect: (() -> ())?,
destroy: (() -> ())?,
} | () -> ()
type CustomEvent = {
Connect: (...any) -> ConnectionObject,
[any]: any,
} | {
on: (...any) -> ConnectionObject,
[any]: any,
} | {
connect: (...any) -> ConnectionObject,
[any]: any,
}
type Library = {
useDeltaTime: () -> number,
useEvent: (
instance: Instance | { [string]: CustomEvent } | CustomEvent,
event: string | RBXScriptSignal | CustomEvent
) -> () -> (number, ...any),
useThrottle: (seconds: number, discriminator: any?) -> boolean,
Plugin: Plugin,
}
local topoRuntime
local export = {} :: any
local hooks = {
"useDeltaTime",
"useEvent",
"useThrottle",
"log",
}
local function setHooks(package)
topoRuntime = require(package:FindFirstChild("topoRuntime", true)) :: any
for _, hook in hooks do
local module = package:FindFirstChild(hook, true)
export[hook] = require(module) :: any
end
end
-- When using Matter, we must use the topoRuntime in the library
if ReplicatedStorage:FindFirstChild("Packages") then
for _, package in ReplicatedStorage.Packages._Index:GetChildren() do
if
string.find(package.Name, "matter%-ecs_matter")
or string.find(package.Name, "evaera_matter")
then
setHooks(package)
break
end
end
end
if not topoRuntime then
setHooks(script)
end
local Plugin = {}
Plugin.__index = Plugin
function Plugin:build(scheduler: any)
if not scheduler._systemState then
scheduler._systemState = {}
end
if not scheduler._systemLogs then
scheduler._systemLogs = {}
end
local phaseDetails = {}
for _, phase in scheduler._orderedPhases do
if not phaseDetails[phase] then
phaseDetails[phase] = {
lastTime = os.clock(),
generation = false,
}
end
local details = phaseDetails[phase]
details.currentTime = os.clock()
details.deltaTime = details.currentTime - details.lastTime
details.lastTime = details.currentTime
details.generation = not details.generation
end
scheduler:_addHook(scheduler.Hooks.PhaseAdd, function(args: PhaseBeganArgs)
local phase = args.phase
if not phaseDetails[phase] then
phaseDetails[phase] = {
currentTime = os.clock(),
deltaTime = 0,
lastTime = os.clock(),
generation = false,
}
end
local details = phaseDetails[phase]
details.currentTime = os.clock()
details.deltaTime = details.currentTime - details.lastTime
details.lastTime = details.currentTime
details.generation = not details.generation
end)
scheduler:_addHook(
scheduler.Hooks.PhaseBegan,
function(args: PhaseBeganArgs)
local phase = args.phase
if not phaseDetails[phase] then
phaseDetails[phase] = {
currentTime = os.clock(),
deltaTime = 0,
lastTime = os.clock(),
generation = false,
}
end
local details = phaseDetails[phase]
details.currentTime = os.clock()
details.deltaTime = details.currentTime - details.lastTime
details.lastTime = details.currentTime
details.generation = not details.generation
end
)
scheduler:_addHook(
scheduler.Hooks.SystemAdd,
function(info: SystemsAddRemove)
local systemFn = info.system.system
if not scheduler._systemState[systemFn] then
scheduler._systemState[systemFn] = {}
end
if not scheduler._systemLogs[systemFn] then
scheduler._systemLogs[systemFn] = {}
end
end
)
scheduler:_addHook(
scheduler.Hooks.SystemRemove,
function(info: SystemsAddRemove)
local systemFn = info.system.system
scheduler._systemState[systemFn] = nil
scheduler._systemLogs[systemFn] = nil
end
)
scheduler:_addHook(
scheduler.Hooks.SystemReplace,
function(info: SystemsReplace)
local newSystem = info.new.system
local oldSystem = info.old.system
scheduler._systemState[newSystem] =
scheduler._systemState[oldSystem]
scheduler._systemLogs[newSystem] = scheduler._systemLogs[oldSystem]
scheduler._systemState[oldSystem] = nil
scheduler._systemLogs[oldSystem] = nil
end
)
scheduler:_addHook(scheduler.Hooks.OuterSystemCall, function(args: HookArgs)
local systemFn = args.system.system
local phase = args.system.phase
local details = phaseDetails[phase]
return function()
topoRuntime.start({
system = scheduler._systemState[systemFn],
frame = {
generation = details.generation,
deltaTime = details.deltaTime,
logs = scheduler._systemLogs[systemFn],
},
currentSystem = systemFn,
}, function()
local thread = coroutine.create(function()
args.nextFn()
end)
local _startTime = os.clock()
local _success, _err = coroutine.resume(thread)
end)
end
end)
end
function Plugin.new(module)
if module then
setHooks(module)
end
return setmetatable({}, Plugin)
end
export.Plugin = Plugin
return export :: Library