Lookup throws on IsNone by default

wip/source-generators
copygirl 2 years ago
parent 8c5c5260c2
commit 36102b3536
  1. 1
      src/gaemstone.Bloxel/Client/ChunkMeshGenerator.cs
  2. 4
      src/gaemstone/ECS/Entity.cs
  3. 40
      src/gaemstone/ECS/Universe.cs

@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;
using gaemstone.Client;
using gaemstone.ECS;
using Silk.NET.Maths;

@ -32,8 +32,8 @@ public unsafe readonly struct Entity
public Entity(Universe universe, ecs_entity_t value)
{ Universe = universe; Value = value; }
public void ThrowIfNone() { if (IsNone) throw new InvalidOperationException("Entity isn't valid"); }
public void ThrowIfDead() { if (!IsAlive) throw new InvalidOperationException("Entity is dead"); }
public Entity ThrowIfNone() { if (IsNone) throw new InvalidOperationException("Entity is invalid"); return this; }
public Entity ThrowIfDead() { if (!IsAlive) throw new InvalidOperationException("Entity is dead"); return this; }
public void Delete() => ecs_delete(Universe, Value);

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
@ -70,16 +69,23 @@ public unsafe partial class Universe
}
public Entity Lookup<T>()
=> Lookup(typeof(T));
public Entity Lookup(Type type)
public Entity TryLookup<T>()
=> TryLookup(typeof(T));
public Entity TryLookup(Type type)
=> _byType.TryGetValue(type, out var e) ? new(this, e) : default;
public Entity TryLookup(ecs_entity_t value)
=> new(this, ecs_get_alive(this, value));
public Entity TryLookup(string path)
=> new(this, ecs_lookup_path_w_sep(this, default, path, ".", default, true));
public Entity Lookup(string path)
=> new(this, !path.Contains('.') ? ecs_lookup(this, path)
: ecs_lookup_path_w_sep(this, default, path, ".", default, true));
public Entity Lookup<T>()
=> TryLookup<T>().ThrowIfNone();
public Entity Lookup(Type type)
=> TryLookup(type).ThrowIfNone();
public Entity Lookup(ecs_entity_t value)
=> new(this, ecs_get_alive(this, value));
=> TryLookup(value).ThrowIfNone();
public Entity Lookup(string path)
=> TryLookup(path).ThrowIfNone();
public void RegisterAll(Assembly? from = null)
@ -87,16 +93,11 @@ public unsafe partial class Universe
from ??= Assembly.GetEntryAssembly()!;
foreach (var type in from.GetTypes()) {
var isPartOfModule = type.DeclaringType?.Has<ModuleAttribute>() == true;
if (type.Has<RelationAttribute>()) {
if (!isPartOfModule) RegisterRelation(type);
} else if (type.Has<ComponentAttribute>()) {
if (!isPartOfModule) RegisterComponent(type);
} else if (type.Has<TagAttribute>()) {
if (!isPartOfModule) RegisterTag(type);
} else if (type.Has<EntityAttribute>()) {
if (!isPartOfModule) RegisterEntity(type);
} else if (type.Has<ModuleAttribute>())
RegisterModule(type);
if (type.Has<RelationAttribute>()) { if (!isPartOfModule) RegisterRelation(type); }
else if (type.Has<ComponentAttribute>()) { if (!isPartOfModule) RegisterComponent(type); }
else if (type.Has<TagAttribute>()) { if (!isPartOfModule) RegisterTag(type); }
else if (type.Has<EntityAttribute>()) { if (!isPartOfModule) RegisterEntity(type); }
else if (type.Has<ModuleAttribute>()) RegisterModule(type);
}
}
@ -175,8 +176,7 @@ public unsafe partial class Universe
public Entity Create(ecs_entity_desc_t desc)
{
var entity = ecs_entity_init(Handle, &desc);
Debug.Assert(entity.Data != 0, "ECS_INVALID_PARAMETER");
return new(this, entity);
return new Entity(this, entity).ThrowIfNone();
}

Loading…
Cancel
Save