diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Setting.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Setting.kt index 2e857e110..13ae9f943 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Setting.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Setting.kt @@ -7,7 +7,7 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ -@file:Suppress("NOTHING_TO_INLINE") +@file:Suppress("NOTHING_TO_INLINE", "unused") package net.mamoe.mirai.console.setting @@ -17,6 +17,7 @@ import net.mamoe.mirai.console.setting.internal.serialNameOrPropertyName import net.mamoe.mirai.utils.MiraiExperimentalAPI import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty +import kotlin.reflect.KProperty0 import kotlin.reflect.full.findAnnotation /** @@ -37,7 +38,8 @@ abstract class Setting : SettingImpl() { data class PropertyInfo( val serialName: String, - val annotations: List + val annotations: List, + val propertyOriginalName: String? ) /** @@ -60,6 +62,23 @@ abstract class Setting : SettingImpl() { return value } + /** + * 获取这个属性的真实 [Value] 委托 + */ + @get:JvmSynthetic + val KProperty0.correspondingValue: Value + @Suppress("UNCHECKED_CAST") + get() = findCorrespondingValue(this) + ?: throw NoSuchElementException("No corresponding Value found for property $this") + + /** + * 获取这个属性的真实 [Value] 委托 + */ + fun findCorrespondingValue(property: KProperty0): Value? { + @Suppress("UNCHECKED_CAST") + return this@Setting.valueList.firstOrNull { it.second.propertyOriginalName == property.name }?.first as Value? + } + /** * 提供属性委托, 并添加这个对象的自动保存跟踪. */ @@ -69,7 +88,7 @@ abstract class Setting : SettingImpl() { property: KProperty<*> ): ReadWriteProperty { if (built) error("The Setting is already serialized so it's structure is immutable.") - valueList.add(this to PropertyInfo(property.serialNameOrPropertyName, property.annotations)) + valueList.add(this to PropertyInfo(property.serialNameOrPropertyName, property.annotations, property.name)) return this }