diff --git a/src/Immersion/Program.cs b/src/Immersion/Program.cs index 18caa82..e28acfa 100644 --- a/src/Immersion/Program.cs +++ b/src/Immersion/Program.cs @@ -20,9 +20,10 @@ FlecsAbortException.SetupHook(); Resources.ResourceAssembly = typeof(Program).Assembly; var universe = new Universe(); -var game = universe.LookupOrThrow(); -universe.EnableRest(); -universe.EnableMonitor(); +var game = universe.LookupOrThrow(); + +universe.Modules.Register(); +universe.Modules.Register(); var window = Window.Create(WindowOptions.Default with { Title = "gæmstone", diff --git a/src/gaemstone/ECS/Universe+Modules.cs b/src/gaemstone/ECS/Universe+Modules.cs index 39acdf7..01edca4 100644 --- a/src/gaemstone/ECS/Universe+Modules.cs +++ b/src/gaemstone/ECS/Universe+Modules.cs @@ -45,12 +45,9 @@ public class ModuleManager foreach (var nested in type.GetNestedTypes()) { if (nested.Get() is not EntityAttribute nestedAttr) continue; - - var name = nestedAttr.Name ?? nested.Name; - if (name.Contains('.')) throw new Exception( + if (nestedAttr.Name?.Contains('.') == true) throw new Exception( $"EntityAttribute.Name for {type} must not contain a dot (path separator)"); - - Universe.LookupOrThrow($"{moduleName}.{name}") + Universe.LookupOrThrow($"{moduleName}.{nestedAttr.Name ?? nested.Name}") .CreateLookup(nested); } @@ -58,14 +55,10 @@ public class ModuleManager } else { - var name = GetModuleName(type); - if (Universe.Lookup(name) != null) throw new Exception( - $"Can't register module {type}: '{type.FullName}' is already in use"); - + var name = GetModuleName(type); var module = new ModuleInfo(Universe, type, name); _modules.Add(module.Entity, module); TryEnableModule(module); - return module.Entity; } diff --git a/src/gaemstone/ECS/Universe.cs b/src/gaemstone/ECS/Universe.cs index c211e73..663655b 100644 --- a/src/gaemstone/ECS/Universe.cs +++ b/src/gaemstone/ECS/Universe.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; using static flecs_hub.flecs; using static flecs_hub.flecs.Runtime; @@ -29,29 +28,6 @@ public unsafe partial class Universe public EntityBuilder New(string? name = null, string? symbol = null) => new(this, name, symbol); - // TODO: Move to module. - public void EnableRest(ushort port = 27750) - { - [UnmanagedCallersOnly] - static void RestImport(ecs_world_t* world) - => FlecsRestImport(world); - - ecs_import_c(this, new() { Data = new() { Pointer = &RestImport } }, "FlecsRest"); - - var rest = LookupOrThrow("flecs.rest.Rest"); - _lookupByType.Add(typeof(EcsRest), rest); - rest.Set(new EcsRest { port = port }); - } - - public void EnableMonitor() - { - [UnmanagedCallersOnly] - static void MonitorImport(ecs_world_t* world) - => FlecsMonitorImport(world); - - ecs_import_c(this, new() { Data = new() { Pointer = &MonitorImport } }, "FlecsMonitor"); - } - public bool Progress(TimeSpan delta) => ecs_progress(this, (float)delta.TotalSeconds); diff --git a/src/gaemstone/Systems/Monitor.cs b/src/gaemstone/Systems/Monitor.cs new file mode 100644 index 0000000..52fa273 --- /dev/null +++ b/src/gaemstone/Systems/Monitor.cs @@ -0,0 +1,20 @@ +using System.Runtime.InteropServices; +using gaemstone.ECS; +using static flecs_hub.flecs; + +namespace gaemstone.Systems; + +[Module(Name = "flecs.monitor")] +public unsafe class Monitor + : IModuleInitializer +{ + public void Initialize(EntityRef entity) + { + ecs_import_c(entity.Universe, new() { Data = new() { + Pointer = &MonitorImport } }, "FlecsMonitor"); + } + + [UnmanagedCallersOnly] + private static void MonitorImport(ecs_world_t* world) + => FlecsMonitorImport(world); +} diff --git a/src/gaemstone/Systems/Rest.cs b/src/gaemstone/Systems/Rest.cs new file mode 100644 index 0000000..8d156d3 --- /dev/null +++ b/src/gaemstone/Systems/Rest.cs @@ -0,0 +1,22 @@ +using System.Runtime.InteropServices; +using gaemstone.ECS; +using static flecs_hub.flecs; + +namespace gaemstone.Systems; + +[Module(Name = "flecs.rest")] +public unsafe class Rest + : IModuleInitializer +{ + public void Initialize(EntityRef entity) + { + ecs_import_c(entity.Universe, new() { Data = new() { + Pointer = &RestImport } }, "FlecsRest"); + entity.NewChild("Rest").Build() + .CreateLookup().Set(new EcsRest { port = 27750 }); + } + + [UnmanagedCallersOnly] + static void RestImport(ecs_world_t* world) + => FlecsRestImport(world); +}