Skip to content

Commit

Permalink
support creating task with custom factory function
Browse files Browse the repository at this point in the history
  • Loading branch information
sicusa committed Nov 22, 2023
1 parent 948909b commit 8afbe1f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Sia/Systems/SystemChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ public SystemChain Add<TSystem>()
}
return new(Sequence.Add(typeof(TSystem)), newEntries);
}

public SystemChain Add<TSystem>(Func<TSystem> creator)
where TSystem : ISystem
{
var newEntries = Entries.Add(
typeof(TSystem), new(
(sysLib, scheduler, taskGraphNodes) => sysLib.Register(scheduler, creator, taskGraphNodes),
GetAttributedSystemTypes<TSystem>(typeof(AfterSystemAttribute<>)),
GetAttributedSystemTypes<TSystem>(typeof(BeforeSystemAttribute<>))
));
if (newEntries == Entries) {
return this;
}
return new(Sequence.Add(typeof(TSystem)), newEntries);
}

public SystemChain Remove<TSystem>()
where TSystem : ISystem, new()
Expand Down
6 changes: 5 additions & 1 deletion Sia/Systems/SystemLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,12 @@ internal Entry Acquire(Type systemType)

public SystemHandle Register<TSystem>(Scheduler scheduler, IEnumerable<Scheduler.TaskGraphNode>? dependedTasks = null)
where TSystem : ISystem, new()
=> Register<TSystem>(scheduler, () => new(), dependedTasks);

public SystemHandle Register<TSystem>(Scheduler scheduler, Func<TSystem> creator, IEnumerable<Scheduler.TaskGraphNode>? dependedTasks = null)
where TSystem : ISystem
{
var system = new TSystem();
var system = creator();
var sysEntry = Acquire(system.GetType());

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down

0 comments on commit 8afbe1f

Please sign in to comment.