Skip to content

Commit

Permalink
fix: Actually load prefabs defined by prefab addon
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Oct 25, 2024
1 parent 2e5408e commit 0a71387
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class PrefabsDSL(
}

fun fromRecursive(folder: Path) {
PrefabPath(namespaced.namespace) {
prefabsBuilder.paths.add(PrefabPath(namespaced.namespace) {
fileSystem
.listRecursively(folder, true)
.filter { it.name.contains('.') }
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class PrefabsBuilder {
}

val Prefabs
get() = createAddon<PrefabsBuilder, PrefabsModule>("Prefabs", {
PrefabsBuilder()
}) {
get() = createAddon<PrefabsBuilder, PrefabsModule>("Prefabs", { PrefabsBuilder() }) {
val formats = geary.getAddon(SerializableComponents).formats
val module = object : PrefabsModule {
override val manager = PrefabManager()
override val loader: PrefabLoader = PrefabLoader(geary, formats, logger)
}

configuration.paths.forEach { module.loader.addSource(it) }

systems {
createInheritPrefabsOnLoadListener()
createParseChildOnPrefabListener()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import com.mineinabyss.geary.datatypes.Entity
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.prefabs.PrefabKey
import com.mineinabyss.geary.prefabs.configuration.components.InheritPrefabs
import com.mineinabyss.geary.prefabs.entityOfOrNull

/**
* Adds prefabs to this entity from an [InheritPrefabs] component. Will make sure parents have their prefabs
* added from this component before trying to add it
*/
fun Entity.inheritPrefabsIfNeeded(instances: Set<Entity> = setOf()) {
if (this in instances)
fun Entity.inheritPrefabsIfNeeded(instances: Set<Entity> = setOf()): Unit = with(world) {
if (this@inheritPrefabsIfNeeded in instances)
error("Circular dependency found while loading prefabs for ${get<PrefabKey>()}, chain was: $instances")
val add = get<InheritPrefabs>() ?: return
remove<InheritPrefabs>()
add.from.mapNotNull { key ->
TODO()
// key.toEntityOrNull().also {
// if (it == null) geary.logger.w("Prefab ${get<PrefabKey>()} could not inherit prefab $key, it does not exist")
// }
entityOfOrNull(key).also {
if (it == null) logger.w("Prefab ${get<PrefabKey>()} could not inherit prefab $key, it does not exist")
}
}.forEach { parent ->
parent.inheritPrefabsIfNeeded(instances + this)
parent.inheritPrefabsIfNeeded(instances + this@inheritPrefabsIfNeeded)
extend(parent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ data class SerializableComponentsBuilder(

fun build(): SerializableComponentsModule {
module {
println("Adding Geary to serializers")
contextual<Geary>(GearyWorldProvider(world))
contextual<ComponentId>(ComponentIdSerializer(serializers.build(), world))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ class SerializersByMap(

//TODO allow this to work for all registered classes, not just components
override fun getClassFor(serialName: String, namespaces: List<String>): KClass<out Component> {
val defaultNamespaces = (namespaces + "geary").toSet()
val parsedKey = serialName.fromCamelCaseToSnakeCase()
return (if (parsedKey.hasNamespace())
serialName2Component[parsedKey]
else namespaces.firstNotNullOfOrNull { namespace ->
else defaultNamespaces.firstNotNullOfOrNull { namespace ->
serialName2Component["$namespace:$parsedKey"]
})
?: error("$parsedKey is not a component registered in any of the namespaces: $namespaces")
?: error("$parsedKey is not a component registered in any of the namespaces: $defaultNamespaces")
}

override fun <T : Component> getSerializerFor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ open class PolymorphicListAsMapSerializer<T : Any>(
if (key.startsWith("kotlin.")) {
return@runCatching serializersModule.getPolymorphic(polymorphicSerializer.baseClass, key) as KSerializer<T>
}
val defaultNamespaces = namespaces.plus("geary").toSet()
val parsedKey = "${config.prefix}$key".fromCamelCaseToSnakeCase()
return@runCatching (if (parsedKey.hasNamespace())
serializersModule.getPolymorphic(polymorphicSerializer.baseClass, parsedKey)
else namespaces.firstNotNullOfOrNull { namespace ->
else defaultNamespaces.firstNotNullOfOrNull { namespace ->
serializersModule.getPolymorphic(polymorphicSerializer.baseClass, "$namespace:$parsedKey")
} ?: error("No serializer found for $parsedKey in any of the namespaces $namespaces"))
} ?: error("No serializer found for $parsedKey in any of the namespaces $defaultNamespaces"))
as? KSerializer<T> ?: error("Serializer for $parsedKey is not a component serializer")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ data class UninitializedGearyModule(
val world = Geary(setup.application)
world.addons.initAll(setup)
initializer.start()
world.pipeline.runStartupTasks() // TODO keep pipeline separate, it shouldnt be used after init
return world
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.mineinabyss.geary.modules
import co.touchlab.kermit.Logger
import com.mineinabyss.geary.addons.Namespaced
import com.mineinabyss.geary.addons.dsl.Addon
import com.mineinabyss.geary.addons.dsl.AddonSetup
import com.mineinabyss.geary.addons.dsl.createAddon
import org.koin.core.KoinApplication

/**
Expand All @@ -20,6 +22,12 @@ class GearySetup(
return addon
}

inline fun install(name: String, crossinline init: AddonSetup<Unit>.() -> Unit) {
install(createAddon(name) {
init()
})
}

fun namespace(namespace: String, configure: Namespaced.() -> Unit) {
Namespaced(namespace, this).configure()
}
Expand Down

0 comments on commit 0a71387

Please sign in to comment.