Add SingletonAttribute.AutoAdd

Singletons no longer auto-add themselves,
unless AutoAdd is set to true.
wip/source-generators
copygirl 1 year ago
parent 3084e2ccfb
commit c559c40243
  1. 12
      src/gaemstone/ECS/Attributes.cs
  2. 12
      src/gaemstone/ECS/Entity.cs
  3. 2
      src/gaemstone/ECS/Universe+Modules.cs
  4. 4
      src/gaemstone/Utility/IL/IterActionGenerator.cs

@ -19,6 +19,18 @@ public interface ICreateEntityAttribute { }
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum)]
public class PrivateAttribute : Attribute { }
/// <summary>
/// A singleton is a single instance of a tag or component that can be retrieved
/// without explicitly specifying an entity in a query, where it is equivalent
/// to <see cref="SourceAttribute{}"/> with itself as the generic type parameter.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum)]
public class SingletonAttribute : Attribute
{
/// <summary> Whether to add the entity to itself on registration. </summary>
public bool AutoAdd { get; init; } = false;
}
/// <summary>
/// Register the proxied type instead of the one marked with this attribute.
/// This can be used to make types not registered in a module available,

@ -21,18 +21,6 @@ public class EntityAttribute : Attribute, ICreateEntityAttribute
}
}
/// <summary>
/// A singleton is a single instance of a tag or component that can be retrieved
/// without explicitly specifying an entity in a query, where it is equivalent
/// to <see cref="SourceAttribute{}"/> with itself as the generic type parameter.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum)]
public class SingletonAttribute : EntityAttribute
{
public SingletonAttribute() { }
public SingletonAttribute(params string[] path) : base(path) { }
}
public readonly struct Entity
: IEquatable<Entity>
{

@ -198,7 +198,7 @@ internal class ModuleInfo
foreach (var attr in type.GetMultiple<AddRelationAttribute>())
entity.Add(Lookup(attr.Relation), Lookup(attr.Target));
if (type.Has<SingletonAttribute>()) entity.Add(entity);
if (type.Get<SingletonAttribute>()?.AutoAdd == true) entity.Add(entity);
if (type.Has<ComponentAttribute>()) entity.CreateComponent(proxyType);
else entity.CreateLookup(proxyType);
}

@ -271,8 +271,8 @@ public unsafe class IterActionGenerator
// Reference types have a backing type of nint - they're pointers.
FieldType = underlyingType.IsValueType ? underlyingType : typeof(nint);
Source = Info.Get<SourceAttribute>()?.Type
?? (UnderlyingType.Has<SingletonAttribute>() ? UnderlyingType : null);
if (UnderlyingType.Has<SingletonAttribute>()) Source = UnderlyingType;
if (Info.Get<SourceAttribute>()?.Type is Type type) Source = type;
}
public static ParamInfo Build(ParameterInfo info)

Loading…
Cancel
Save