-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finalizing addon system, figuring out I/O on multiplatform
- Loading branch information
Showing
30 changed files
with
345 additions
and
367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 0 additions & 139 deletions
139
geary-autoscan/src/main/kotlin/com/mineinabyss/geary/autoscan/AutoScanAddon.kt
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
geary-autoscan/src/main/kotlin/com/mineinabyss/geary/autoscan/AutoScanner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.mineinabyss.geary.autoscan | ||
|
||
import com.mineinabyss.ding.DI | ||
import com.mineinabyss.geary.addons.GearyPhase | ||
import com.mineinabyss.geary.addons.Namespaced | ||
import com.mineinabyss.geary.addons.dsl.GearyAddon | ||
import com.mineinabyss.geary.addons.dsl.GearyAddonWithDefault | ||
import com.mineinabyss.geary.addons.dsl.GearyDSL | ||
import com.mineinabyss.geary.modules.geary | ||
import com.mineinabyss.geary.systems.System | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.full.createInstance | ||
|
||
val autoScanner by DI.observe<AutoScanner>() | ||
|
||
interface AutoScanner { | ||
val scannedComponents: MutableSet<KClass<*>> | ||
val scannedSystems: MutableSet<KClass<*>> | ||
|
||
fun installSystems() | ||
|
||
companion object Addon : GearyAddonWithDefault<AutoScanner> { | ||
override fun default() = object : AutoScanner { | ||
private val logger = geary.logger | ||
override val scannedComponents = mutableSetOf<KClass<*>>() | ||
override val scannedSystems = mutableSetOf<KClass<*>>() | ||
|
||
override fun installSystems() { | ||
scannedSystems.asSequence() | ||
.mapNotNull { it.objectInstance ?: runCatching { it.createInstance() }.getOrNull() } | ||
.filterIsInstance<System>() | ||
.onEach { geary.systems.add(it) } | ||
.map { it::class.simpleName } | ||
.joinToString() | ||
.let { logger.i("Autoscan loaded singleton systems: $it") } | ||
} | ||
} | ||
|
||
override fun AutoScanner.install() { | ||
geary.pipeline.intercept(GearyPhase.INIT_SYSTEMS) { | ||
installSystems() | ||
} | ||
} | ||
} | ||
} | ||
|
||
@GearyDSL | ||
fun Namespaced.autoscan(vararg limitToPackages: String, configure: AutoScannerDSL.() -> Unit) = | ||
gearyConf.install(AutoScanner).also { AutoScannerDSL(this, limitToPackages.toList()).configure() } |
85 changes: 85 additions & 0 deletions
85
geary-autoscan/src/main/kotlin/com/mineinabyss/geary/autoscan/AutoScannerDSL.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.mineinabyss.geary.autoscan | ||
|
||
import com.mineinabyss.geary.addons.Namespaced | ||
import com.mineinabyss.geary.datatypes.Component | ||
import com.mineinabyss.geary.modules.geary | ||
import com.mineinabyss.geary.serialization.serialization | ||
import com.mineinabyss.geary.systems.System | ||
import kotlinx.serialization.* | ||
import kotlinx.serialization.modules.PolymorphicModuleBuilder | ||
import org.reflections.Reflections | ||
import org.reflections.util.ClasspathHelper | ||
import org.reflections.util.ConfigurationBuilder | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.full.hasAnnotation | ||
import kotlin.reflect.full.isSubclassOf | ||
|
||
class AutoScannerDSL( | ||
val namespaced: Namespaced, | ||
val limitTo: List<String> | ||
) { | ||
val logger = geary.logger | ||
|
||
private val reflections: Reflections by lazy { | ||
Reflections( | ||
ConfigurationBuilder() | ||
.addClassLoader(namespaced.currentClass.java.classLoader) | ||
.apply { | ||
limitTo.forEach { pkg -> | ||
addUrls(ClasspathHelper.forPackage(pkg, namespaced.currentClass.java.classLoader)) | ||
} | ||
} | ||
) | ||
} | ||
|
||
/** | ||
* Automatically scans for all annotated components | ||
* | ||
* @see autoScanComponents | ||
* @see autoScanSystems | ||
*/ | ||
fun all() { | ||
components() | ||
systems() | ||
} | ||
|
||
/** | ||
* Registers serializers for [Component]s. | ||
* | ||
* @see AutoScanner | ||
*/ | ||
fun components() { | ||
val scanned = reflections | ||
.getTypesAnnotatedWith(Serializable::class.java) | ||
.asSequence() | ||
.map { it.kotlin } | ||
.filter { !it.hasAnnotation<ExcludeAutoScan>() } | ||
|
||
geary { | ||
namespaced.serialization { | ||
components { | ||
scanned.forEach { component(it) } | ||
} | ||
} | ||
} | ||
logger.i("Autoscan found components: ${scanned.joinToString { it.simpleName!! }}") | ||
|
||
autoScanner.scannedComponents += scanned | ||
} | ||
|
||
/** | ||
* Registers any systems (including event listeners) that are annotated with [AutoScanner]. | ||
* | ||
* Supports singletons or classes with no constructor parameters. | ||
* | ||
* @see AutoScanner | ||
*/ | ||
fun systems() { | ||
val scanned = reflections.getTypesAnnotatedWith(AutoScan::class.java) | ||
.asSequence() | ||
.map { it.kotlin } | ||
.filter { !it.hasAnnotation<ExcludeAutoScan>() && it.isSubclassOf(System::class) } | ||
|
||
autoScanner.scannedSystems += scanned | ||
} | ||
} |
6 changes: 5 additions & 1 deletion
6
geary-core/src/commonMain/kotlin/com/mineinabyss/geary/addons/Namespaced.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
package com.mineinabyss.geary.addons | ||
|
||
import com.mineinabyss.geary.addons.dsl.GearyDSL | ||
import com.mineinabyss.geary.modules.GearyConfiguration | ||
import com.mineinabyss.geary.modules.GearyModule | ||
import kotlin.reflect.KClass | ||
|
||
class Namespaced(val namespace: String) | ||
@GearyDSL | ||
class Namespaced(val namespace: String, val currentClass: KClass<*>, val gearyConf: GearyConfiguration) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 5 additions & 45 deletions
50
geary-core/src/commonMain/kotlin/com/mineinabyss/geary/addons/dsl/GearyAddon.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,9 @@ | ||
package com.mineinabyss.geary.addons.dsl | ||
|
||
import com.mineinabyss.geary.modules.GearyModule | ||
|
||
/** | ||
* The entry point for other plugins to hook into Geary. Allows registering serializable components, systems, actions, | ||
* and more. | ||
*/ | ||
//@GearyDSLMarker | ||
//class GearyAddon( | ||
// val namespace: String, | ||
// val classLoader: ClassLoader | ||
//) : GearyDSL { | ||
// | ||
// override fun system(system: GearySystem) { | ||
// geary.systems.add(system) | ||
// } | ||
// | ||
// override fun systems(vararg systems: GearySystem) { | ||
// systems.forEach { system(it) } | ||
// } | ||
// | ||
// override fun serialization(init: SerializationAddon.() -> Unit) = on(GearyLoadPhase.REGISTER_SERIALIZERS) { | ||
// SerializationAddon(this@GearyAddon).init() | ||
// } | ||
// | ||
// override fun formats(init: Formats.(SerializersModule) -> Unit) = on(GearyLoadPhase.REGISTER_FORMATS) { | ||
// geary.formats.init(geary.serializers.module) | ||
// } | ||
// | ||
// /** | ||
// * Allows defining actions that should run at a specific phase during startup | ||
// * | ||
// * Within its context, invoke a [GearyLoadPhase] to run something during it, ex: | ||
// * | ||
// * ``` | ||
// * GearyLoadPhase.ENABLE { | ||
// * // run code here | ||
// * } | ||
// * ``` | ||
// */ | ||
// override fun on(phase: GearyLoadPhase, run: () -> Unit) { | ||
// addons.manager.add(phase, run) | ||
// } | ||
//} | ||
interface GearyAddon<Module, Conf> { | ||
interface GearyAddonWithDefault<Module>: GearyAddon<Module> { | ||
fun default(): Module | ||
fun Module.install(geary: GearyModule) | ||
} | ||
|
||
interface GearyAddon<Module> { | ||
fun Module.install() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.