From 36102b3536ab9e2a90c40ebf8b531be2a3aa0209 Mon Sep 17 00:00:00 2001 From: copygirl Date: Tue, 20 Sep 2022 08:59:08 +0200 Subject: [PATCH] Lookup throws on IsNone by default --- .../Client/ChunkMeshGenerator.cs | 1 - src/gaemstone/ECS/Entity.cs | 4 +- src/gaemstone/ECS/Universe.cs | 40 +++++++++---------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/gaemstone.Bloxel/Client/ChunkMeshGenerator.cs b/src/gaemstone.Bloxel/Client/ChunkMeshGenerator.cs index d7bfb4c..f5b1f99 100644 --- a/src/gaemstone.Bloxel/Client/ChunkMeshGenerator.cs +++ b/src/gaemstone.Bloxel/Client/ChunkMeshGenerator.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; using gaemstone.Client; using gaemstone.ECS; using Silk.NET.Maths; diff --git a/src/gaemstone/ECS/Entity.cs b/src/gaemstone/ECS/Entity.cs index d82b667..87732f6 100644 --- a/src/gaemstone/ECS/Entity.cs +++ b/src/gaemstone/ECS/Entity.cs @@ -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); diff --git a/src/gaemstone/ECS/Universe.cs b/src/gaemstone/ECS/Universe.cs index 56fe78a..a223636 100644 --- a/src/gaemstone/ECS/Universe.cs +++ b/src/gaemstone/ECS/Universe.cs @@ -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() - => Lookup(typeof(T)); - public Entity Lookup(Type type) + public Entity TryLookup() + => 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() + => TryLookup().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() == true; - if (type.Has()) { - if (!isPartOfModule) RegisterRelation(type); - } else if (type.Has()) { - if (!isPartOfModule) RegisterComponent(type); - } else if (type.Has()) { - if (!isPartOfModule) RegisterTag(type); - } else if (type.Has()) { - if (!isPartOfModule) RegisterEntity(type); - } else if (type.Has()) - RegisterModule(type); + if (type.Has()) { if (!isPartOfModule) RegisterRelation(type); } + else if (type.Has()) { if (!isPartOfModule) RegisterComponent(type); } + else if (type.Has()) { if (!isPartOfModule) RegisterTag(type); } + else if (type.Has()) { if (!isPartOfModule) RegisterEntity(type); } + else if (type.Has()) 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(); }