Skip to content

Commit

Permalink
Update autoscanner to work on new system function syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Mar 13, 2024
1 parent 018b0b7 commit f56659d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,27 @@ import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.systems.System
import com.mineinabyss.idofront.di.DI
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.full.createInstance

val autoScanner by DI.observe<AutoScanner>()

interface AutoScanner {
val scannedComponents: MutableSet<KClass<*>>
val scannedSystems: MutableSet<KClass<*>>
val scannedSystems: MutableSet<KFunction<*>>

fun installSystems()

companion object Addon : GearyAddonWithDefault<AutoScanner> {
override fun default() = object : AutoScanner {
private val logger get() = geary.logger
override val scannedComponents = mutableSetOf<KClass<*>>()
override val scannedSystems = mutableSetOf<KClass<*>>()
override val scannedSystems = mutableSetOf<KFunction<*>>()

override fun installSystems() {
scannedSystems.asSequence()
.mapNotNull { it.objectInstance ?: runCatching { it.createInstance() }.getOrNull() }
.filterIsInstance<System<*>>()
.onEach { geary.pipeline.addSystem(it) }
.map { it::class.simpleName }
.onEach { it.call(geary) }
.map { it.name }
.let {
if (logger.config.minSeverity <= Severity.Verbose)
logger.i("Autoscan loaded singleton systems: ${it.joinToString()}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import co.touchlab.kermit.Severity
import com.mineinabyss.geary.addons.dsl.GearyDSL
import com.mineinabyss.geary.datatypes.Component
import com.mineinabyss.geary.modules.GearyConfiguration
import com.mineinabyss.geary.modules.GearyModule
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.serialization.dsl.serialization
import com.mineinabyss.geary.systems.System
Expand All @@ -13,9 +14,13 @@ import org.reflections.Reflections
import org.reflections.scanners.Scanners
import org.reflections.util.ConfigurationBuilder
import org.reflections.util.FilterBuilder
import java.lang.reflect.Method
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.full.hasAnnotation
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.jvm.kotlinFunction
import kotlin.reflect.typeOf

@GearyDSL
fun GearyConfiguration.autoscan(
Expand Down Expand Up @@ -96,10 +101,10 @@ class AutoScannerDSL(
*/
fun systems() {
val scanned = reflections
.get(Scanners.TypesAnnotated.with(AutoScan::class.java).asClass<Class<*>>(classLoader))
.get(Scanners.MethodsAnnotated.with(AutoScan::class.java).`as`(Method::class.java))
.asSequence()
.map { it.kotlin }
.filter { !it.hasAnnotation<ExcludeAutoScan>() && it.isSubclassOf(System::class) }
.mapNotNull { it.kotlinFunction }
.filter { it.parameters.singleOrNull()?.type == typeOf<GearyModule>() }

autoScanner.scannedSystems += scanned
}
Expand Down

0 comments on commit f56659d

Please sign in to comment.