Move Rest and Monitor to separate module classes

wip/source-generators
copygirl 2 years ago
parent 0e25265366
commit fc2cfdc8b4
  1. 7
      src/Immersion/Program.cs
  2. 13
      src/gaemstone/ECS/Universe+Modules.cs
  3. 24
      src/gaemstone/ECS/Universe.cs
  4. 20
      src/gaemstone/Systems/Monitor.cs
  5. 22
      src/gaemstone/Systems/Rest.cs

@ -20,9 +20,10 @@ FlecsAbortException.SetupHook();
Resources.ResourceAssembly = typeof(Program).Assembly;
var universe = new Universe();
var game = universe.LookupOrThrow<Game>();
universe.EnableRest();
universe.EnableMonitor();
var game = universe.LookupOrThrow<Game>();
universe.Modules.Register<gaemstone.Systems.Rest>();
universe.Modules.Register<gaemstone.Systems.Monitor>();
var window = Window.Create(WindowOptions.Default with {
Title = "gæmstone",

@ -45,12 +45,9 @@ public class ModuleManager
foreach (var nested in type.GetNestedTypes()) {
if (nested.Get<EntityAttribute>() 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;
}

@ -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);

@ -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);
}

@ -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<EcsRest>().Set(new EcsRest { port = 27750 });
}
[UnmanagedCallersOnly]
static void RestImport(ecs_world_t* world)
=> FlecsRestImport(world);
}
Loading…
Cancel
Save