Fix PluginConfig serialization with custom properties

This commit is contained in:
Him188 2020-09-02 20:21:21 +08:00
parent decf1920b4
commit 4c1bf2702c
2 changed files with 12 additions and 11 deletions

View File

@ -85,13 +85,15 @@ internal abstract class PluginDataImpl {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun serialize(encoder: Encoder, value: Unit) { override fun serialize(encoder: Encoder, value: Unit) {
val descriptor = descriptor val descriptor = descriptor
with(encoder.beginCollection(descriptor, valueNodes.size)) { with(encoder.beginStructure(descriptor)) {
var index = 0 repeat(descriptor.elementsCount) { index ->
encodeSerializableElement(
// val vSerializer = dataUpdaterSerializerTypeArguments[1] as KSerializer<Any?> descriptor,
valueNodes.forEach { (_, _, _, valueSerializer) -> index,
//encodeStringElement(descriptor, index, valueName) valueNodes.find { it.valueName == descriptor.getElementName(index) }?.updaterSerializer
encodeSerializableElement(descriptor, index++, valueSerializer, Unit) ?: error("Cannot find a serializer for ${descriptor.getElementName(index)}"),
Unit
)
} }
endStructure(descriptor) endStructure(descriptor)
} }
@ -106,7 +108,7 @@ internal abstract class PluginDataImpl {
private val dataUpdaterSerializerDescriptor by lazy { private val dataUpdaterSerializerDescriptor by lazy {
kotlinx.serialization.descriptors.buildClassSerialDescriptor((this as PluginData).saveName) { kotlinx.serialization.descriptors.buildClassSerialDescriptor((this as PluginData).saveName) {
for (valueNode in valueNodes) valueNode.run { 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 <T : Any?> Constructor<T>.invoke(vararg args: Any?)
internal inline fun <reified T : Any> findAnnotationImplementationClassConstructor(): Constructor<out T>? { internal inline fun <reified T : Any> findAnnotationImplementationClassConstructor(): Constructor<out T>? {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
return T::class.nestedClasses return T::class.nestedClasses
.also { println(it.joinToString()) }
.find { it.simpleName?.endsWith("Impl") == true }?.java?.run { .find { it.simpleName?.endsWith("Impl") == true }?.java?.run {
constructors.singleOrNull() constructors.singleOrNull()
} as Constructor<out T>? } as Constructor<out T>?

View File

@ -16,13 +16,13 @@ import net.mamoe.mirai.console.data.PluginConfig
import net.mamoe.mirai.console.data.PluginData import net.mamoe.mirai.console.data.PluginData
import net.mamoe.mirai.console.data.PluginDataStorage import net.mamoe.mirai.console.data.PluginDataStorage
import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge 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 import net.mamoe.mirai.utils.minutesToMillis
internal object ConsoleDataScope : CoroutineScope by MiraiConsole.childScope("ConsoleDataScope") { internal object ConsoleDataScope : CoroutineScope by MiraiConsole.childScope("ConsoleDataScope") {
private val data: Array<out PluginData> = arrayOf() private val data: Array<out PluginData> = arrayOf()
private val configs: Array<out PluginConfig> = arrayOf(ManagersConfig) private val configs: Array<out PluginConfig> = arrayOf(ManagersConfig, AutoLoginConfig)
fun reloadAll() { fun reloadAll() {
data.forEach { dt -> data.forEach { dt ->