mirror of
https://github.com/InsanusMokrassar/PlaguBot.git
synced 2025-09-14 21:09:27 +00:00
start migration
This commit is contained in:
@@ -6,9 +6,13 @@ import dev.inmo.tgbotapi.types.BotCommand
|
||||
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.koin.core.KoinApplication
|
||||
|
||||
/**
|
||||
* **ANY REALIZATION OF [Plugin] MUST HAVE CONSTRUCTOR WITH ABSENCE OF INCOMING PARAMETERS**
|
||||
*
|
||||
* Use this interface for your bot. It is possible to use [kotlinx.serialization.SerialName] annotations on your plugins
|
||||
* to set up short name for your plugin. Besides, simple name of your class will be used as key for deserialization
|
||||
* too.
|
||||
@@ -22,26 +26,20 @@ interface Plugin {
|
||||
*/
|
||||
suspend fun getCommands(): List<BotCommand> = emptyList()
|
||||
|
||||
@Deprecated("Override other method with receiver BehaviourContext")
|
||||
suspend operator fun invoke(
|
||||
bot: TelegramBot,
|
||||
/**
|
||||
* This method (usually) will be invoked just one time in the whole application.
|
||||
*/
|
||||
suspend operator fun BehaviourContext.invoke(
|
||||
database: Database,
|
||||
updatesFilter: FlowsUpdatesFilter,
|
||||
scope: CoroutineScope
|
||||
koinApplication: KoinApplication,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* This method (usually) will be invoked just one time in the whole application.
|
||||
*/
|
||||
suspend operator fun BehaviourContext.invoke(
|
||||
database: Database
|
||||
) = invoke(bot, database, flowsUpdatesFilter, scope)
|
||||
|
||||
/**
|
||||
* This method (usually) will be invoked just one time in the whole application.
|
||||
*/
|
||||
suspend operator fun BehaviourContext.invoke(
|
||||
database: Database,
|
||||
params: Map<String, Any>
|
||||
) = invoke(database)
|
||||
koinApplication: KoinApplication,
|
||||
params: JsonObject
|
||||
) = invoke(database, koinApplication)
|
||||
}
|
||||
|
@@ -1,53 +1,24 @@
|
||||
package dev.inmo.plagubot
|
||||
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.Serializer
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||
import kotlinx.serialization.encoding.Decoder
|
||||
import kotlinx.serialization.encoding.Encoder
|
||||
import kotlinx.serialization.json.*
|
||||
|
||||
private val defaultJson = Json {
|
||||
ignoreUnknownKeys = true
|
||||
}
|
||||
|
||||
@Serializer(Plugin::class)
|
||||
object PluginSerializer : KSerializer<Plugin> {
|
||||
private val polymorphic = PolymorphicSerializer(Plugin::class)
|
||||
override val descriptor: SerialDescriptor = JsonObject.serializer().descriptor
|
||||
class PluginSerializer : KSerializer<Plugin> {
|
||||
override val descriptor: SerialDescriptor
|
||||
get() = String.serializer().descriptor
|
||||
|
||||
@OptIn(InternalSerializationApi::class)
|
||||
override fun deserialize(decoder: Decoder): Plugin {
|
||||
val format = (decoder as? JsonDecoder) ?.json ?: defaultJson
|
||||
val asJson = JsonElement.serializer().deserialize(decoder)
|
||||
val jsonObject = (asJson as? JsonObject)
|
||||
|
||||
val type = (jsonObject ?.get("type") as? JsonPrimitive) ?.contentOrNull
|
||||
val external = if (type != null) {
|
||||
try {
|
||||
Class.forName(type) ?.kotlin ?.serializerOrNull()
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
return if (jsonObject != null && external != null) {
|
||||
format.decodeFromJsonElement(
|
||||
external as KSerializer<Plugin>,
|
||||
JsonObject(jsonObject.toMutableMap().also { it.remove("type") })
|
||||
)
|
||||
} else {
|
||||
format.decodeFromJsonElement(
|
||||
polymorphic,
|
||||
asJson
|
||||
)
|
||||
}
|
||||
return Class.forName(decoder.decodeString()).getDeclaredConstructor().newInstance() as Plugin
|
||||
}
|
||||
|
||||
@OptIn(InternalSerializationApi::class)
|
||||
override fun serialize(encoder: Encoder, value: Plugin) {
|
||||
val serializer = (value::class.serializerOrNull() ?: polymorphic) as KSerializer<Plugin>
|
||||
serializer.serialize(encoder, value)
|
||||
encoder.encodeString(
|
||||
value::class.java.canonicalName
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user