Rename ModuleInfo properties

wip/source-generators
copygirl 2 years ago
parent 3777801c40
commit 8177ea24ef
  1. 50
      src/gaemstone/ECS/Universe+Modules.cs

@ -52,9 +52,9 @@ public class ModuleManager
var path = GetModulePath(type);
var module = new ModuleInfo(Universe, type, path);
_modules.Add(module.ModuleEntity, module);
_modules.Add(module.Entity, module);
TryEnableModule(module);
return module.ModuleEntity;
return module.Entity;
}
}
@ -67,14 +67,16 @@ public class ModuleManager
// Find other modules that might be missing this module as a dependency.
foreach (var other in _modules.Values) {
if (other.IsActive) continue;
if (!other.UnmetDependencies.Contains(module.ModuleEntity)) continue;
if (!other.UnmetDependencies.Contains(module.Entity)) continue;
// Move the just enabled module from unmet to met depedencies.
other.UnmetDependencies.Remove(module.ModuleEntity);
other.UnmetDependencies.Remove(module.Entity);
other.MetDependencies.Add(module);
TryEnableModule(other);
}
Console.WriteLine("Enabled module " + module.Path);
}
public static EntityPath GetModulePath(Type type)
@ -103,10 +105,10 @@ public class ModuleManager
internal class ModuleInfo
{
public Universe Universe { get; }
public Type ModuleType { get; }
public EntityPath ModulePath { get; }
public Type Type { get; }
public EntityPath Path { get; }
public EntityRef ModuleEntity { get; }
public EntityRef Entity { get; }
public object? Instance { get; internal set; }
public bool IsActive => Instance != null;
@ -115,19 +117,19 @@ internal class ModuleInfo
public ModuleInfo(Universe universe, Type type, EntityPath path)
{
Universe = universe;
ModuleType = type;
ModulePath = path;
Universe = universe;
Type = type;
Path = path;
if (ModuleType.IsAbstract || ModuleType.IsSealed) throw new Exception(
$"Module {ModuleType} must not be abstract or sealed");
if (ModuleType.GetConstructor(Type.EmptyTypes) == null) throw new Exception(
$"Module {ModuleType} must define public parameterless constructor");
if (Type.IsAbstract || Type.IsSealed) throw new Exception(
$"Module {Type} must not be abstract or sealed");
if (Type.GetConstructor(Type.EmptyTypes) == null) throw new Exception(
$"Module {Type} must define public parameterless constructor");
var module = Universe.New(path).Add<Module>();
// Add module dependencies from [DependsOn] attributes.
foreach (var dependsAttr in ModuleType.GetMultiple<DependsOnAttribute>()) {
foreach (var dependsAttr in Type.GetMultiple<DependsOnAttribute>()) {
var dependsPath = ModuleManager.GetModulePath(dependsAttr.Type);
var dependency = Universe.Lookup(dependsPath) ??
Universe.New(dependsPath).Add<Module>().Disable().Build();
@ -139,26 +141,26 @@ internal class ModuleInfo
module.Add<DependsOn>(dependency);
}
ModuleEntity = module.Build().CreateLookup(type);
Entity = module.Build().CreateLookup(type);
}
public void Enable()
{
ModuleEntity.Enable();
Instance = Activator.CreateInstance(ModuleType)!;
Entity.Enable();
Instance = Activator.CreateInstance(Type)!;
RegisterNestedTypes();
(Instance as IModuleInitializer)?.Initialize(ModuleEntity);
(Instance as IModuleInitializer)?.Initialize(Entity);
RegisterMethods(Instance);
}
private void RegisterNestedTypes()
{
foreach (var type in ModuleType.GetNestedTypes()) {
foreach (var type in Type.GetNestedTypes()) {
if (type.Get<EntityAttribute>() is not EntityAttribute attr) continue;
if (attr.Name?.Contains(EntityPath.Separator) == true) throw new Exception(
$"{type} must not contain '{EntityPath.Separator}'");
var name = attr.Name ?? type.Name;
var entity = ModuleEntity.NewChild(name).Symbol(name);
var entity = Entity.NewChild(name).Symbol(name);
switch (attr) {
case TagAttribute:
@ -183,11 +185,11 @@ internal class ModuleInfo
private void RegisterMethods(object? instance)
{
foreach (var method in ModuleType.GetMethods()) {
foreach (var method in Type.GetMethods()) {
if (method.Has<SystemAttribute>())
Universe.RegisterSystem(instance, method).ChildOf(ModuleEntity);
Universe.RegisterSystem(instance, method).ChildOf(Entity);
if (method.Has<ObserverAttribute>())
Universe.RegisterObserver(instance, method).ChildOf(ModuleEntity);
Universe.RegisterObserver(instance, method).ChildOf(Entity);
}
}
}

Loading…
Cancel
Save