Skip to content

Commit

Permalink
Add a hooks cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Ukendio committed Sep 9, 2024
1 parent e234bd8 commit 1503d7e
Show file tree
Hide file tree
Showing 5 changed files with 614 additions and 440 deletions.
76 changes: 76 additions & 0 deletions demo/default.project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"name": "demo",
"tree": {
"$className": "DataModel",
"ReplicatedStorage": {
"$className": "ReplicatedStorage",
"$path": "src/ReplicatedStorage",
"ecs": {
"$path": "../src"
},
"net": {
"$path": "net/client.luau"
},
"Packages": {
"$path": "Packages"
}
},
"ServerScriptService": {
"$className": "ServerScriptService",
"$path": "src/ServerScriptService",
"net": {
"$path": "net/server.luau"
}
},
"Workspace": {
"$properties": {
"FilteringEnabled": true
},
"Baseplate": {
"$className": "Part",
"$properties": {
"Anchored": true,
"Color": [
0.38823,
0.37254,
0.38823
],
"Locked": true,
"Position": [
0,
-10,
0
],
"Size": [
512,
20,
512
]
}
}
},
"Lighting": {
"$properties": {
"Ambient": [
0,
0,
0
],
"Brightness": 2,
"GlobalShadows": true,
"Outlines": false,
"Technology": "Voxel"
}
},
"SoundService": {
"$properties": {
"RespectFilteringEnabled": true
}
},
"StarterPlayer": {
"StarterPlayerScripts": {
"$path": "src/StarterPlayer/StarterPlayerScripts"
}
}
}
}
32 changes: 32 additions & 0 deletions demo/src/ReplicatedStorage/std/hooks.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--!native
--!optimize 2
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local jecs = require(ReplicatedStorage.ecs)

local function create_cache(hook)
local columns = setmetatable({}, {
__index = function(self, component)
local column = {}
self[component] = column
return column
end
})

return function(world, component, fn)
local column = columns[component]
table.insert(column, fn)
world:set(component, hook, function(entity, value)
for _, callback in column do
callback(entity, value)
end
end)
end
end

local hooks = {
OnSet = create_cache(jecs.OnSet),
OnAdd = create_cache(jecs.OnAdd),
OnRemove = create_cache(jecs.OnRemove)
}

return hooks
1 change: 1 addition & 0 deletions demo/src/ReplicatedStorage/std/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local std = {
world = world :: World,
pair = jecs.pair,
__ = jecs.w,
hooks = require(script.hooks)
}

return std
44 changes: 24 additions & 20 deletions demo/src/ReplicatedStorage/std/scheduler.luau
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage")
local jabby = require(ReplicatedStorage.Packages.jabby)
local jecs = require(ReplicatedStorage.ecs)
local pair = jecs.pair
local Name = jecs.Name

type World = jecs.World
type Entity<T=nil> = jecs.Entity<T>

Expand Down Expand Up @@ -43,20 +45,20 @@ export type Scheduler = {
Heartbeat: Entity,
},

phase: (after: Entity) -> Entity
phase: (after: Entity) -> Entity,

debugging: boolean,
}

local scheduler_new: (w: World, components: { [string]: Entity }) -> Scheduler


do
local world: World
local Disabled: Entity
local System: Entity<{}>
local DependsOn
local Phase
local Event
local Name
local System: Entity<System>
local DependsOn: Entity
local Phase: Entity
local Event: Entity<RBXScriptSignal>

local scheduler

Expand All @@ -65,18 +67,19 @@ do
local PreAnimation
local PreSimulation

local system: System
local sys: System
local dt

local function run()
local id = system.id
local id = sys.id
local system_data = scheduler.system_data[id]
if system_data.paused then return end

scheduler:mark_system_frame_start(id)
system.callback(dt)
scheduler:mark_system_frame_start(id)
sys.callback(dt)
scheduler:mark_system_frame_end(id)

end

local function panic(str)
-- We don't want to interrupt the loop when we error
task.spawn(error, str)
Expand All @@ -91,10 +94,10 @@ do
dt = event:Wait()

debug.profilebegin(event_name)
for _, sys in systems do
system = sys
for _, s in systems do
sys = s
local didNotYield, why = xpcall(function()
for _ in run do end
for _ in run do break end
end, debug.traceback)

if didNotYield then
Expand All @@ -105,7 +108,7 @@ do
panic("Not allowed to yield in the systems."
.. "\n"
.. "System: "
.. debug.info(system.callback, "n")
.. debug.info(s.callback, "n")
.. " has been ejected"
)
continue
Expand All @@ -121,13 +124,13 @@ do

local function scheduler_collect_systems_under_phase_recursive(systems, phase)
local phase_name = world:get(phase, Name)
for _, system in world:query(System):with(pair(DependsOn, phase)) do
for _, s in world:query(System):with(pair(DependsOn, phase)) do
table.insert(systems, {
id = scheduler:register_system({
name = system.name,
name = s.name,
phase = phase_name
}),
callback = system.callback
callback = s.callback
})
end
for after in world:query(Phase):with(pair(DependsOn, phase)) do
Expand Down Expand Up @@ -172,7 +175,6 @@ do
Phase = world:component()
DependsOn = world:component()
Event = world:component()
Name = world:component()

RenderStepped = world:component()
Heartbeat = world:component()
Expand All @@ -193,6 +195,7 @@ do

world:add(PreAnimation, Phase)
world:set(PreAnimation, Event, RunService.PreAnimation)

for name, component in components do
world:set(component, Name, name)
end
Expand All @@ -210,6 +213,7 @@ do
scheduler = jabby.scheduler.create("scheduler")

table.insert(jabby.public, scheduler)

return {
phase = scheduler_phase_new,

Expand Down
Loading

0 comments on commit 1503d7e

Please sign in to comment.