From 9683582d282b911a9515da81a306604f54fc5445 Mon Sep 17 00:00:00 2001 From: copygirl Date: Wed, 9 Nov 2022 20:09:37 +0100 Subject: [PATCH] Add RegisterAll and RegisterAllFrom methods They are helper methods that register multiple modules at once, the latter registering all modules from an Assembly. --- src/gaemstone/ECS/Universe+Modules.cs | 9 +++++++++ src/gaemstone/ECS/Universe.cs | 18 +++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/gaemstone/ECS/Universe+Modules.cs b/src/gaemstone/ECS/Universe+Modules.cs index 208a090..70b6f42 100644 --- a/src/gaemstone/ECS/Universe+Modules.cs +++ b/src/gaemstone/ECS/Universe+Modules.cs @@ -17,6 +17,15 @@ public class ModuleManager internal ModuleInfo? Lookup(Entity entity) => _modules.GetValueOrDefault(entity); + public void RegisterAllFrom(System.Reflection.Assembly assembly) + => RegisterAll(assembly.GetTypes()); + public void RegisterAll(IEnumerable types) + { + foreach (var type in types) + if (type.Has()) + Register(type); + } + public EntityRef Register() where T : class => Register(typeof(T)); public EntityRef Register(Type type) diff --git a/src/gaemstone/ECS/Universe.cs b/src/gaemstone/ECS/Universe.cs index 42f23d6..afdcaba 100644 --- a/src/gaemstone/ECS/Universe.cs +++ b/src/gaemstone/ECS/Universe.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using static flecs_hub.flecs; namespace gaemstone.ECS; @@ -15,17 +16,20 @@ public unsafe partial class Universe public Universe(params string[] args) { - Handle = ecs_init_w_args(args.Length, null); - + Handle = ecs_init_w_args(args.Length, null); Modules = new(this); - Modules.Register(typeof(Flecs.Core)); - Modules.Register(typeof(Flecs.Pipeline)); - Modules.Register(typeof(Flecs.ObserverEvent)); - Modules.Register(typeof(Flecs.SystemPhase)); + + // Register built-in (static) modules, which + // are defined in the "gaemstone.Flecs" namespace. + Modules.RegisterAll(GetType().Assembly.GetTypes() + .Where(t => t.IsAbstract && t.IsSealed)); ChildOf = LookupOrThrow(); - New("Game").Symbol("Game").Build().CreateLookup().Add(); + // Create "Game" singleton entity, which + // stores global state, configuration, ... + New("Game").Symbol("Game").Build() + .CreateLookup().Add(); } public EntityBuilder New(EntityPath? path = null)