From 87ab75870ecf674ed55a5069287d1b4c1c01d74a Mon Sep 17 00:00:00 2001 From: Danielle Voznyy Date: Thu, 1 Feb 2024 13:19:32 -0500 Subject: [PATCH] Add exists check, fix contains function in TypeMap --- .../kotlin/com/mineinabyss/geary/autoscan/AutoScannerDSL.kt | 5 ++++- .../kotlin/com/mineinabyss/geary/datatypes/Entity.kt | 3 +++ .../com/mineinabyss/geary/datatypes/maps/ArrayTypeMap.kt | 2 +- .../com/mineinabyss/geary/engine/EntityReadOperations.kt | 3 +++ .../engine/archetypes/operations/ArchetypeReadOperations.kt | 4 ++++ 5 files changed, 15 insertions(+), 2 deletions(-) diff --git a/addons/geary-autoscan/src/main/kotlin/com/mineinabyss/geary/autoscan/AutoScannerDSL.kt b/addons/geary-autoscan/src/main/kotlin/com/mineinabyss/geary/autoscan/AutoScannerDSL.kt index 035b385d7..5aca91d45 100644 --- a/addons/geary-autoscan/src/main/kotlin/com/mineinabyss/geary/autoscan/AutoScannerDSL.kt +++ b/addons/geary-autoscan/src/main/kotlin/com/mineinabyss/geary/autoscan/AutoScannerDSL.kt @@ -68,7 +68,10 @@ class AutoScannerDSL( scanned.forEach { scannedComponent -> runCatching { component(scannedComponent) } .onFailure { - this@AutoScannerDSL.logger.w("Failed to register component ${scannedComponent.simpleName}\n${it.stackTraceToString()}") + if (it is ClassNotFoundException) + this@AutoScannerDSL.logger.w("Failed to register component ${scannedComponent.simpleName}, class not found") + else + this@AutoScannerDSL.logger.w("Failed to register component ${scannedComponent.simpleName}\n${it.stackTraceToString()}") } } } diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/Entity.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/Entity.kt index db612faaa..52ba4f12e 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/Entity.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/Entity.kt @@ -55,6 +55,9 @@ value class Entity(val id: EntityId) { entityProvider.remove(this) } + /** Checks whether this entity has not been removed. */ + fun exists(): Boolean = read.exists(this) + /** * Sets a component that holds data for this entity * diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/maps/ArrayTypeMap.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/maps/ArrayTypeMap.kt index 5182321f9..5590494de 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/maps/ArrayTypeMap.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/datatypes/maps/ArrayTypeMap.kt @@ -28,6 +28,6 @@ class ArrayTypeMap : TypeMap { override operator fun contains(entity: Entity): Boolean { val id = entity.id.toInt() - return map.size < id && map[id] != null + return map.size > id && map[id] != null } } diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/EntityReadOperations.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/EntityReadOperations.kt index 19a8b6f06..30c9a2a1f 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/EntityReadOperations.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/EntityReadOperations.kt @@ -10,6 +10,9 @@ interface EntityReadOperations { /** Gets a list of all the components [entity] has, as well as relations in the form of [RelationComponent]. */ fun getComponentsFor(entity: Entity): Array + /** Checks whether an [entity] is still active in the engine. */ + fun exists(entity: Entity): Boolean + /** * Gets relations in the same format as [Archetype.getRelations], but when kind/target [HOLDS_DATA], the appropriate * data is written to a [RelationWithData] object. diff --git a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/operations/ArchetypeReadOperations.kt b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/operations/ArchetypeReadOperations.kt index 0dd5af32f..690cb9154 100644 --- a/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/operations/ArchetypeReadOperations.kt +++ b/geary-core/src/commonMain/kotlin/com/mineinabyss/geary/engine/archetypes/operations/ArchetypeReadOperations.kt @@ -24,6 +24,10 @@ class ArchetypeReadOperations : EntityReadOperations { } } + override fun exists(entity: Entity): Boolean { + return records.contains(entity) + } + override fun getRelationsWithDataFor( entity: Entity, kind: ComponentId,