start 1.1.2 + add opportunity to load object plugins

This commit is contained in:
InsanusMokrassar 2022-06-20 20:52:42 +06:00
parent 5ec1c8c55f
commit e0a19bb5e5
6 changed files with 16 additions and 8 deletions

View File

@ -1,5 +1,10 @@
# Changelog
## 1.1.2
* `Plugin`:
* Now it is possible to use `object`s of plugins instead of classes
## 1.1.1
* `Versions`

View File

@ -18,7 +18,7 @@ import org.koin.dsl.module
@Serializable
@SerialName("Hello")
class HelloPlugin : Plugin {
object HelloPlugin : Plugin {
@Serializable
data class HelloPluginConfig(
val print: String

View File

@ -56,17 +56,17 @@ data class PlaguBot(
}
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
config.plugins.map {
config.plugins.map { plugin ->
launch {
runCatchingSafely {
logger.i("Start loading of $it")
with(it) {
logger.i("Start loading of $plugin")
with(plugin) {
setupBotPlugin(koin)
}
}.onFailure { e ->
logger.w("Unable to load bot part of $it", e)
logger.w("Unable to load bot part of $plugin", e)
}.onSuccess {
logger.i("Complete loading of $it")
logger.i("Complete loading of $plugin")
}
}
}.joinAll()

View File

@ -1,9 +1,11 @@
package dev.inmo.plagubot.config
import dev.inmo.micro_utils.common.Warning
import dev.inmo.plagubot.Plugin
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Warning("This API is internal and can be changed without notifications of mentions of changes")
@Serializable
data class Config(
val botToken: String,

View File

@ -5,4 +5,4 @@ kotlin.js.generate.externals=true
kotlin.incremental=true
group=dev.inmo
version=1.1.1
version=1.1.2

View File

@ -13,7 +13,8 @@ class PluginSerializer : KSerializer<Plugin> {
get() = String.serializer().descriptor
override fun deserialize(decoder: Decoder): Plugin {
return Class.forName(decoder.decodeString()).getDeclaredConstructor().newInstance() as Plugin
val kclass = Class.forName(decoder.decodeString()).kotlin
return (kclass.objectInstance ?: kclass.constructors.first { it.parameters.isEmpty() }.call()) as Plugin
}
override fun serialize(encoder: Encoder, value: Plugin) {