Skip to content

Commit

Permalink
refactor ComponentsHolder toString() (#110)
Browse files Browse the repository at this point in the history
* refactor ComponentsHolder toString()
* 2.5-SNAPSHOT
  • Loading branch information
Quillraven authored Aug 2, 2023
1 parent 1bbdffb commit d363c57
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Fleks

[![LTS](https://img.shields.io/badge/LTS-2.4-orange.svg)](https://search.maven.org/artifact/io.github.quillraven.fleks/Fleks/2.4/jar)
[![Snapshot](https://img.shields.io/badge/Snapshot-2.4--SNAPSHOT-orange.svg)](https://s01.oss.sonatype.org/#nexus-search;gav~io.github.quillraven.fleks~~2.4-SNAPSHOT~~)
[![Snapshot](https://img.shields.io/badge/Snapshot-2.5--SNAPSHOT-orange.svg)](https://s01.oss.sonatype.org/#nexus-search;gav~io.github.quillraven.fleks~~2.5-SNAPSHOT~~)

[![Build Master](https://img.shields.io/github/actions/workflow/status/quillraven/fleks/build.yml?branch=master)](https://github.com/Quillraven/fleks/actions)
[![Kotlin](https://img.shields.io/badge/Kotlin-1.9.0-red.svg)](http://kotlinlang.org/)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
}

group = "io.github.quillraven.fleks"
version = "2.4"
version = "2.5-SNAPSHOT"

kotlin {
jvm {
Expand Down
29 changes: 13 additions & 16 deletions src/commonMain/kotlin/com/github/quillraven/fleks/component.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.github.quillraven.fleks.collection.Bag
import com.github.quillraven.fleks.collection.bag
import kotlin.math.max
import kotlin.native.concurrent.ThreadLocal
import kotlin.reflect.KClass

/**
* A class that assigns a unique [id] per type of [Component] starting from 0.
Expand Down Expand Up @@ -68,7 +67,7 @@ interface Component<T> {
*/
class ComponentsHolder<T : Component<*>>(
private val world: World,
private val name: String,
private val type: ComponentType<*>,
private var components: Array<T?>,
) {
/**
Expand Down Expand Up @@ -128,7 +127,7 @@ class ComponentsHolder<T : Component<*>>(
* @throws [FleksNoSuchEntityComponentException] if the [entity] does not have such a component.
*/
operator fun get(entity: Entity): T {
return components[entity.id] ?: throw FleksNoSuchEntityComponentException(entity, name)
return components[entity.id] ?: throw FleksNoSuchEntityComponentException(entity, componentName())
}

/**
Expand All @@ -143,8 +142,15 @@ class ComponentsHolder<T : Component<*>>(
operator fun contains(entity: Entity): Boolean =
components.size > entity.id && components[entity.id] != null

/**
* Returns the simplified component name of a [ComponentType].
* The default toString() format is 'package.Component$Companion'.
* This method returns 'Component' without package and companion.
*/
private fun componentName(): String = type::class.toString().substringAfterLast(".").substringBefore("$")

override fun toString(): String {
return "ComponentsHolder($name)"
return "ComponentsHolder(type=${componentName()}, id=${type.id})"
}
}

Expand All @@ -162,23 +168,15 @@ class ComponentService {
@PublishedApi
internal val holdersBag = bag<ComponentsHolder<*>>()

// Format of toString() is package.Component$Companion
// This method returns 'Component' which is the short name of the component class.
fun KClass<*>.toComponentName(): String =
this.toString().substringAfterLast(".").substringBefore("$")

/**
* Returns a [ComponentsHolder] for the given [componentType]. This function is only
* used by [World.loadSnapshot] where we don't have the correct type information
* during runtime, and therefore we can only provide '*' as a type and need to cast it internally.
*/
fun wildcardHolder(componentType: ComponentType<*>): ComponentsHolder<*> {
if (holdersBag.hasNoValueAtIndex(componentType.id)) {
// We cannot use simpleName here because it returns "Companion".
// Therefore, we do some string manipulation to get the name of the component correctly.
// Format of toString() is package.Component$Companion
val name = componentType::class.toComponentName()
holdersBag[componentType.id] = ComponentsHolder(world, name, Array<Component<*>?>(world.capacity) { null })
holdersBag[componentType.id] =
ComponentsHolder(world, componentType, Array<Component<*>?>(world.capacity) { null })
}
return holdersBag[componentType.id]
}
Expand All @@ -190,8 +188,7 @@ class ComponentService {
@Suppress("UNCHECKED_CAST")
inline fun <reified T : Component<*>> holder(componentType: ComponentType<T>): ComponentsHolder<T> {
if (holdersBag.hasNoValueAtIndex(componentType.id)) {
val name = T::class.simpleName ?: T::class.toComponentName()
holdersBag[componentType.id] = ComponentsHolder(world, name, Array<T?>(world.capacity) { null })
holdersBag[componentType.id] = ComponentsHolder(world, componentType, Array<T?>(world.capacity) { null })
}
return holdersBag[componentType.id] as ComponentsHolder<T>
}
Expand Down

0 comments on commit d363c57

Please sign in to comment.