From e0a19bb5e554867d96548b16db0c9443e6f1e976 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 20 Jun 2022 20:52:42 +0600 Subject: [PATCH] start 1.1.2 + add opportunity to load object plugins --- CHANGELOG.md | 5 +++++ bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt | 2 +- bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt | 10 +++++----- bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt | 2 ++ gradle.properties | 2 +- .../main/kotlin/dev/inmo/plagubot/PluginSerializer.kt | 3 ++- 6 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb04dbf..a9b2987 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt b/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt index 77dba0d..15f45bc 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt @@ -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 diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt b/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt index 59b1cdb..513f561 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt @@ -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() diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt b/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt index 5135e5b..88c1769 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt @@ -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, diff --git a/gradle.properties b/gradle.properties index 5158144..39a275e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,4 +5,4 @@ kotlin.js.generate.externals=true kotlin.incremental=true group=dev.inmo -version=1.1.1 +version=1.1.2 diff --git a/plugin/src/main/kotlin/dev/inmo/plagubot/PluginSerializer.kt b/plugin/src/main/kotlin/dev/inmo/plagubot/PluginSerializer.kt index ad2252d..d3d89a4 100644 --- a/plugin/src/main/kotlin/dev/inmo/plagubot/PluginSerializer.kt +++ b/plugin/src/main/kotlin/dev/inmo/plagubot/PluginSerializer.kt @@ -13,7 +13,8 @@ class PluginSerializer : KSerializer { 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) {