From 4c1bf2702c89b61cb4440631ee5e8236bcf11fa0 Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 2 Sep 2020 20:21:21 +0800 Subject: [PATCH] Fix PluginConfig serialization with custom properties --- .../console/internal/data/PluginDataImpl.kt | 19 ++++++++++--------- .../internal/data/builtin/ConsoleDataScope.kt | 4 ++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/PluginDataImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/PluginDataImpl.kt index 75d21c61e..4dc70fa25 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/PluginDataImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/PluginDataImpl.kt @@ -85,13 +85,15 @@ internal abstract class PluginDataImpl { @Suppress("UNCHECKED_CAST") override fun serialize(encoder: Encoder, value: Unit) { val descriptor = descriptor - with(encoder.beginCollection(descriptor, valueNodes.size)) { - var index = 0 - - // val vSerializer = dataUpdaterSerializerTypeArguments[1] as KSerializer - valueNodes.forEach { (_, _, _, valueSerializer) -> - //encodeStringElement(descriptor, index, valueName) - encodeSerializableElement(descriptor, index++, valueSerializer, Unit) + with(encoder.beginStructure(descriptor)) { + repeat(descriptor.elementsCount) { index -> + encodeSerializableElement( + descriptor, + index, + valueNodes.find { it.valueName == descriptor.getElementName(index) }?.updaterSerializer + ?: error("Cannot find a serializer for ${descriptor.getElementName(index)}"), + Unit + ) } endStructure(descriptor) } @@ -106,7 +108,7 @@ internal abstract class PluginDataImpl { private val dataUpdaterSerializerDescriptor by lazy { kotlinx.serialization.descriptors.buildClassSerialDescriptor((this as PluginData).saveName) { for (valueNode in valueNodes) valueNode.run { - element(valueName, updaterSerializer.descriptor, isOptional = true) + element(valueName, updaterSerializer.descriptor, annotations = annotations, isOptional = true) } } } @@ -132,7 +134,6 @@ internal inline operator fun Constructor.invoke(vararg args: Any?) internal inline fun findAnnotationImplementationClassConstructor(): Constructor? { @Suppress("UNCHECKED_CAST") return T::class.nestedClasses - .also { println(it.joinToString()) } .find { it.simpleName?.endsWith("Impl") == true }?.java?.run { constructors.singleOrNull() } as Constructor? diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/builtin/ConsoleDataScope.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/builtin/ConsoleDataScope.kt index 998765917..f0c9527f2 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/builtin/ConsoleDataScope.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/builtin/ConsoleDataScope.kt @@ -16,13 +16,13 @@ import net.mamoe.mirai.console.data.PluginConfig import net.mamoe.mirai.console.data.PluginData import net.mamoe.mirai.console.data.PluginDataStorage import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge -import net.mamoe.mirai.console.util.childScope +import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope import net.mamoe.mirai.utils.minutesToMillis internal object ConsoleDataScope : CoroutineScope by MiraiConsole.childScope("ConsoleDataScope") { private val data: Array = arrayOf() - private val configs: Array = arrayOf(ManagersConfig) + private val configs: Array = arrayOf(ManagersConfig, AutoLoginConfig) fun reloadAll() { data.forEach { dt ->