diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt index 8cab73f5b..3669f780f 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt @@ -16,8 +16,6 @@ import net.mamoe.mirai.Bot import net.mamoe.mirai.console.plugin.PluginLoader import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader import net.mamoe.mirai.console.plugin.jvm.JvmPlugin -import net.mamoe.mirai.console.setting.SettingStorage -import net.mamoe.mirai.console.setting.internal.ConsoleBuiltInSetting import net.mamoe.mirai.utils.DefaultLogger import net.mamoe.mirai.utils.MiraiExperimentalAPI import net.mamoe.mirai.utils.MiraiLogger @@ -72,14 +70,6 @@ object MiraiConsole : CoroutineScope, IMiraiConsole { override val builtInPluginLoaders: List> get() = instance.builtInPluginLoaders - @Suppress("CANNOT_WEAKEN_ACCESS_PRIVILEGE") - internal override val jvmSettingStorage: SettingStorage - get() = instance.jvmSettingStorage - - @Suppress("CANNOT_WEAKEN_ACCESS_PRIVILEGE") - override val consoleBuiltIntSettingStorage: SettingStorage - get() = instance.consoleBuiltIntSettingStorage - init { DefaultLogger = { identity -> this.newLogger(identity) } this.coroutineContext[Job]!!.invokeOnCompletion { @@ -116,16 +106,6 @@ internal interface IMiraiConsole : CoroutineScope { * 内建加载器列表, 一般需要包含 [JarPluginLoader] */ val builtInPluginLoaders: List> - - /** - * 内建的供 [JvmPlugin] 使用的 [SettingStorage] - */ - val jvmSettingStorage: SettingStorage - - /** - * 内建的供 [ConsoleBuiltInSetting] 使用的 [SettingStorage] - */ - val consoleBuiltIntSettingStorage: SettingStorage } /** diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JarPluginLoader.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JarPluginLoader.kt index 6491cfd7f..9c7eca155 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JarPluginLoader.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JarPluginLoader.kt @@ -15,7 +15,6 @@ import net.mamoe.mirai.console.plugin.AbstractFilePluginLoader import net.mamoe.mirai.console.plugin.PluginLoadException import net.mamoe.mirai.console.plugin.internal.JvmPluginImpl import net.mamoe.mirai.console.plugin.internal.PluginsLoader -import net.mamoe.mirai.console.setting.SettingStorage import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.yamlkt.Yaml import java.io.File @@ -49,8 +48,6 @@ object JarPluginLoader : AbstractFilePluginLoader.mapToDescription(): List { diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/KotlinPlugin.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/KotlinPlugin.kt index 891163d74..e3a9dc28f 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/KotlinPlugin.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/KotlinPlugin.kt @@ -12,9 +12,6 @@ package net.mamoe.mirai.console.plugin.jvm import net.mamoe.mirai.console.plugin.internal.JvmPluginImpl -import net.mamoe.mirai.console.plugin.internal.job -import net.mamoe.mirai.console.setting.Setting -import net.mamoe.mirai.console.setting.Value import kotlin.coroutines.CoroutineContext import kotlin.coroutines.EmptyCoroutineContext @@ -23,20 +20,4 @@ import kotlin.coroutines.EmptyCoroutineContext */ abstract class KotlinPlugin @JvmOverloads constructor( parentCoroutineContext: CoroutineContext = EmptyCoroutineContext -) : JvmPlugin, JvmPluginImpl(parentCoroutineContext) { - abstract inner class PluginSetting : Setting() { - private val track = - @Suppress("LeakingThis") - JarPluginLoader.settingStorage.trackOn(this) - - init { - this@KotlinPlugin.job.invokeOnCompletion { - track.close() - } - } - - override fun onElementChanged(value: Value<*>) { - TODO() - } - } -} \ No newline at end of file +) : JvmPlugin, JvmPluginImpl(parentCoroutineContext) \ No newline at end of file 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 deleted file mode 100644 index 4e32f1a1a..000000000 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Setting.kt +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -@file:Suppress("NOTHING_TO_INLINE", "unused") - -package net.mamoe.mirai.console.setting - -import kotlinx.serialization.KSerializer -import net.mamoe.mirai.console.setting.internal.SettingImpl -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 - -/** - * 在配置文件和图像界面中保存的名称. - */ -typealias SerialName = kotlinx.serialization.SerialName - -/** - * 在配置文件和图像界面中显示的说明. - */ -typealias Comment = net.mamoe.yamlkt.Comment - -/** - * 配置的基类. 所有配置必须拥有一个无参构造器, 以用于在 [MutableList] 与 [MutableMap] 中动态识别类型 - */ -@Suppress("EXPOSED_SUPER_CLASS") -abstract class Setting : SettingImpl() { - - /** - * 表示这个配置的嵌套对象, 自动绑定数据更新. - */ - abstract inner class Inner : Setting() { - internal lateinit var attachedValue: Value<*> - override fun onElementChanged(value: Value<*>) { - this@Setting.onElementChanged(attachedValue) - } - } - - data class PropertyInfo( - val serialName: String, - val annotations: List, - val propertyOriginalName: String? - ) - - /** - * 这个配置的名称, 仅对于顶层配置有效. - */ - @MiraiExperimentalAPI - open val serialName: String - get() = this::class.findAnnotation()?.value - ?: this::class.qualifiedName - ?: error("Names should be assigned to anonymous classes manually by overriding serialName") - - - // for Java only - fun addProperty( - propertyInfo: PropertyInfo, - value: Value<*> - ): Value<*> { - if (built) error("The Setting is already serialized so it's structure is immutable.") - valueList.add(value to propertyInfo) - 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? - } - - /** - * 提供属性委托, 并添加这个对象的自动保存跟踪. - */ - @JvmSynthetic - operator fun Value.provideDelegate( - thisRef: Setting, - 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, property.name)) - return this - } - - abstract override fun onElementChanged(value: Value<*>) - - override fun toString(): String = yamlForToString.stringify(this.serializer, this) -} - -/** - * 用于更新或保存这个 [Value] 的序列化器. - */ -@Suppress("UNCHECKED_CAST") -val T.serializer: KSerializer - get() = kotlinSerializer as KSerializer diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/SettingStorage.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/SettingStorage.kt deleted file mode 100644 index 9d14cfcc2..000000000 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/SettingStorage.kt +++ /dev/null @@ -1,65 +0,0 @@ -@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") - -package net.mamoe.mirai.console.setting - -import kotlinx.atomicfu.locks.withLock -import kotlinx.serialization.* -import kotlinx.serialization.internal.getChecked -import net.mamoe.mirai.console.setting.internal.SettingSerializerMark -import java.io.Closeable -import java.io.File -import java.util.concurrent.locks.ReentrantLock - -/** - * [Setting] 存储方式 - */ -interface SettingStorage { - interface TrackedSetting : Closeable { - fun save() - fun update() - - override fun close() - } - - fun trackOn(setting: Setting): TrackedSetting - - fun saveAll() - fun updateAll() -} - -class SingleFileSettingStorage( - val file: File -) : SettingStorage { - private val descriptor: MutableList = ArrayList() - private val updaterSerializer: KSerializer = object : KSerializer { - override val descriptor: SerialDescriptor = SerialDescriptor("SingleFileSettingStorage") { - TODO() - } - - override fun deserialize(decoder: Decoder): SettingSerializerMark { - TODO("Not yet implemented") - } - - override fun serialize(encoder: Encoder, value: SettingSerializerMark) { - TODO("Not yet implemented") - } - - } - - init { - require(file.isFile) { "file $file is not a file" } - require(file.canRead()) { "file $file is not readable" } - } - - override fun trackOn(setting: Setting): SettingStorage.TrackedSetting { - TODO("Not yet implemented") - } - - override fun saveAll() { - TODO("Not yet implemented") - } - - override fun updateAll() { - TODO("Not yet implemented") - } -} \ No newline at end of file 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 deleted file mode 100644 index 4706684f9..000000000 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/_Setting.kt +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright 2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "unused") - -package net.mamoe.mirai.console.setting - -import net.mamoe.mirai.console.setting.internal.valueImpl -import kotlin.internal.LowPriorityInOverloadResolution - - -/** - * !!! This file is auto-generated by backend/codegen/src/kotlin/net.mamoe.mirai.console.codegen.SettingValueUseSiteCodegen.kt - * !!! DO NOT MODIFY THIS FILE MANUALLY - */ - - -fun Setting.value(default: Int): IntValue = valueImpl(default) -fun Setting.value(default: Short): ShortValue = valueImpl(default) -fun Setting.value(default: Byte): ByteValue = valueImpl(default) -fun Setting.value(default: Long): LongValue = valueImpl(default) -fun Setting.value(default: Float): FloatValue = valueImpl(default) -fun Setting.value(default: Double): DoubleValue = valueImpl(default) -fun Setting.value(default: Boolean): BooleanValue = valueImpl(default) -fun Setting.value(default: Char): CharValue = valueImpl(default) -fun Setting.value(default: String): StringValue = valueImpl(default) -fun Setting.value(default: IntArray): IntArrayValue = valueImpl(default) -fun Setting.value(default: ShortArray): ShortArrayValue = valueImpl(default) -fun Setting.value(default: ByteArray): ByteArrayValue = valueImpl(default) -fun Setting.value(default: LongArray): LongArrayValue = valueImpl(default) -fun Setting.value(default: FloatArray): FloatArrayValue = valueImpl(default) -fun Setting.value(default: DoubleArray): DoubleArrayValue = valueImpl(default) -fun Setting.value(default: BooleanArray): BooleanArrayValue = valueImpl(default) -fun Setting.value(default: CharArray): CharArrayValue = valueImpl(default) -fun Setting.value(default: Array): TypedIntArrayValue = valueImpl(default) -fun Setting.value(default: Array): TypedShortArrayValue = valueImpl(default) -fun Setting.value(default: Array): TypedByteArrayValue = valueImpl(default) -fun Setting.value(default: Array): TypedLongArrayValue = valueImpl(default) -fun Setting.value(default: Array): TypedFloatArrayValue = valueImpl(default) -fun Setting.value(default: Array): TypedDoubleArrayValue = valueImpl(default) -fun Setting.value(default: Array): TypedBooleanArrayValue = valueImpl(default) -fun Setting.value(default: Array): TypedCharArrayValue = valueImpl(default) -fun Setting.value(default: Array): TypedStringArrayValue = valueImpl(default) -fun Setting.value(default: List): IntListValue = valueImpl(default) -fun Setting.value(default: List): ShortListValue = valueImpl(default) -fun Setting.value(default: List): ByteListValue = valueImpl(default) -fun Setting.value(default: List): LongListValue = valueImpl(default) -fun Setting.value(default: List): FloatListValue = valueImpl(default) -fun Setting.value(default: List): DoubleListValue = valueImpl(default) -fun Setting.value(default: List): BooleanListValue = valueImpl(default) -fun Setting.value(default: List): CharListValue = valueImpl(default) -fun Setting.value(default: List): StringListValue = valueImpl(default) -fun Setting.value(default: Set): IntSetValue = valueImpl(default) -fun Setting.value(default: Set): ShortSetValue = valueImpl(default) -fun Setting.value(default: Set): ByteSetValue = valueImpl(default) -fun Setting.value(default: Set): LongSetValue = valueImpl(default) -fun Setting.value(default: Set): FloatSetValue = valueImpl(default) -fun Setting.value(default: Set): DoubleSetValue = valueImpl(default) -fun Setting.value(default: Set): BooleanSetValue = valueImpl(default) -fun Setting.value(default: Set): CharSetValue = valueImpl(default) -fun Setting.value(default: Set): StringSetValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableList): MutableIntListValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableList): MutableShortListValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableList): MutableByteListValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableList): MutableLongListValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableList): MutableFloatListValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableList): MutableDoubleListValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableList): MutableBooleanListValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableList): MutableCharListValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableList): MutableStringListValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableSet): MutableIntSetValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableSet): MutableShortSetValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableSet): MutableByteSetValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableSet): MutableLongSetValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableSet): MutableFloatSetValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableSet): MutableDoubleSetValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableSet): MutableBooleanSetValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableSet): MutableCharSetValue = valueImpl(default) - -@JvmName("valueMutable") -fun Setting.value(default: MutableSet): MutableStringSetValue = valueImpl(default) - -fun Setting.value(default: T): Value { - require(this::class != default::class) { - "Recursive nesting is prohibited" - } - return valueImpl(default).also { - if (default is Setting.Inner) { - default.attachedValue = it - } - } -} - -inline fun Setting.value(default: T, crossinline initializer: T.() -> Unit): Value = - value(default).also { it.value.apply(initializer) } - -inline fun Setting.value(default: List): SettingListValue = valueImpl(default) - -@JvmName("valueMutable") -inline fun Setting.value(default: MutableList): MutableSettingListValue = valueImpl(default) - - -inline fun Setting.value(default: Set): SettingSetValue = valueImpl(default) - -@JvmName("valueMutable") -inline fun Setting.value(default: MutableSet): MutableSettingSetValue = valueImpl(default) - -/** - * 创建一个只引用对象而不跟踪其属性的值. - * - * @param T 类型. 必须拥有 [kotlinx.serialization.Serializable] 注解 (因此编译器会自动生成序列化器) - */ -@DangerousReferenceOnlyValue -@JvmName("valueDynamic") -@LowPriorityInOverloadResolution -inline fun Setting.value(default: T): Value = valueImpl(default) - -@RequiresOptIn( - """ - 这种只保存引用的 Value 可能会导致意料之外的结果, 在使用时须保持谨慎. - 对值的改变不会触发自动保存, 也不会同步到 UI 中. 在 UI 中只能编辑序列化之后的值. -""", level = RequiresOptIn.Level.WARNING -) -@Retention(AnnotationRetention.BINARY) -@Target(AnnotationTarget.FUNCTION) -annotation class DangerousReferenceOnlyValue - diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/_Value.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/_Value.kt deleted file mode 100644 index 6dc6f62be..000000000 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/_Value.kt +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright 2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -package net.mamoe.mirai.console.setting - -import kotlinx.serialization.KSerializer -import kotlin.properties.ReadWriteProperty -import kotlin.reflect.KProperty - -/** - * !!! This file is auto-generated by backend/codegen/src/main/kotlin/net.mamoe.mirai.console.codegen.ValuesCodegen.kt - * !!! for better performance - * !!! DO NOT MODIFY THIS FILE MANUALLY - */ - -sealed class Value : ReadWriteProperty { - abstract var value: T - - /** - * 用于更新 [value] 的序列化器 - */ - abstract val serializer: KSerializer - override fun getValue(thisRef: Setting, property: KProperty<*>): T = value - override fun setValue(thisRef: Setting, property: KProperty<*>, value: T) { - this.value = value - } - - override fun equals(other: Any?): Boolean { - if (other == null) return false - if (other::class != this::class) return false - other as Value<*> - return other.value == this.value - } - - override fun hashCode(): Int = value.hashCode() -} - -sealed class PrimitiveValue : Value() - -sealed class NumberValue : Value() -abstract class IntValue internal constructor() : NumberValue() -abstract class ShortValue internal constructor() : NumberValue() -abstract class ByteValue internal constructor() : NumberValue() -abstract class LongValue internal constructor() : NumberValue() -abstract class FloatValue internal constructor() : NumberValue() -abstract class DoubleValue internal constructor() : NumberValue() - -abstract class BooleanValue internal constructor() : PrimitiveValue() -abstract class CharValue internal constructor() : PrimitiveValue() -abstract class StringValue internal constructor() : PrimitiveValue() - -// T can be primitive array or typed Array -sealed class ArrayValue : Value() -sealed class PrimitiveArrayValue : ArrayValue() - -abstract class IntArrayValue internal constructor() : PrimitiveArrayValue(), Iterable { - override fun iterator(): Iterator = this.value.iterator() -} - -abstract class ShortArrayValue internal constructor() : PrimitiveArrayValue(), Iterable { - override fun iterator(): Iterator = this.value.iterator() -} - -abstract class ByteArrayValue internal constructor() : PrimitiveArrayValue(), Iterable { - override fun iterator(): Iterator = this.value.iterator() -} - -abstract class LongArrayValue internal constructor() : PrimitiveArrayValue(), Iterable { - override fun iterator(): Iterator = this.value.iterator() -} - -abstract class FloatArrayValue internal constructor() : PrimitiveArrayValue(), Iterable { - override fun iterator(): Iterator = this.value.iterator() -} - -abstract class DoubleArrayValue internal constructor() : PrimitiveArrayValue(), Iterable { - override fun iterator(): Iterator = this.value.iterator() -} - -abstract class BooleanArrayValue internal constructor() : PrimitiveArrayValue(), Iterable { - override fun iterator(): Iterator = this.value.iterator() -} - -abstract class CharArrayValue internal constructor() : PrimitiveArrayValue(), Iterable { - override fun iterator(): Iterator = this.value.iterator() -} - - -sealed class TypedPrimitiveArrayValue : ArrayValue>(), Iterable { - override fun iterator() = this.value.iterator() -} - -abstract class TypedIntArrayValue internal constructor() : TypedPrimitiveArrayValue() -abstract class TypedShortArrayValue internal constructor() : TypedPrimitiveArrayValue() -abstract class TypedByteArrayValue internal constructor() : TypedPrimitiveArrayValue() -abstract class TypedLongArrayValue internal constructor() : TypedPrimitiveArrayValue() -abstract class TypedFloatArrayValue internal constructor() : TypedPrimitiveArrayValue() -abstract class TypedDoubleArrayValue internal constructor() : TypedPrimitiveArrayValue() -abstract class TypedBooleanArrayValue internal constructor() : TypedPrimitiveArrayValue() -abstract class TypedCharArrayValue internal constructor() : TypedPrimitiveArrayValue() -abstract class TypedStringArrayValue internal constructor() : TypedPrimitiveArrayValue() - -sealed class ListValue : Value>(), List -abstract class IntListValue internal constructor() : ListValue() -abstract class ShortListValue internal constructor() : ListValue() -abstract class ByteListValue internal constructor() : ListValue() -abstract class LongListValue internal constructor() : ListValue() -abstract class FloatListValue internal constructor() : ListValue() -abstract class DoubleListValue internal constructor() : ListValue() -abstract class BooleanListValue internal constructor() : ListValue() -abstract class CharListValue internal constructor() : ListValue() -abstract class StringListValue internal constructor() : ListValue() - -abstract class SettingListValue internal constructor() : Value>(), List - -sealed class SetValue : Value>(), Set -abstract class IntSetValue internal constructor() : SetValue() -abstract class ShortSetValue internal constructor() : SetValue() -abstract class ByteSetValue internal constructor() : SetValue() -abstract class LongSetValue internal constructor() : SetValue() -abstract class FloatSetValue internal constructor() : SetValue() -abstract class DoubleSetValue internal constructor() : SetValue() -abstract class BooleanSetValue internal constructor() : SetValue() -abstract class CharSetValue internal constructor() : SetValue() -abstract class StringSetValue internal constructor() : SetValue() - -abstract class SettingSetValue internal constructor() : Value>(), Set - -abstract class SettingValue internal constructor() : Value() - -abstract class MutableListValue internal constructor() : Value>>(), MutableList - -abstract class MutableIntListValue internal constructor() : Value>(), MutableList -abstract class MutableShortListValue internal constructor() : Value>(), MutableList -abstract class MutableByteListValue internal constructor() : Value>(), MutableList -abstract class MutableLongListValue internal constructor() : Value>(), MutableList -abstract class MutableFloatListValue internal constructor() : Value>(), MutableList -abstract class MutableDoubleListValue internal constructor() : Value>(), MutableList -abstract class MutableBooleanListValue internal constructor() : Value>(), MutableList -abstract class MutableCharListValue internal constructor() : Value>(), MutableList -abstract class MutableStringListValue internal constructor() : Value>(), MutableList - -abstract class MutableSettingListValue internal constructor() : Value>(), MutableList - -abstract class MutableSetValue internal constructor() : Value>>(), MutableSet - -abstract class MutableIntSetValue internal constructor() : Value>(), MutableSet -abstract class MutableShortSetValue internal constructor() : Value>(), MutableSet -abstract class MutableByteSetValue internal constructor() : Value>(), MutableSet -abstract class MutableLongSetValue internal constructor() : Value>(), MutableSet -abstract class MutableFloatSetValue internal constructor() : Value>(), MutableSet -abstract class MutableDoubleSetValue internal constructor() : Value>(), MutableSet -abstract class MutableBooleanSetValue internal constructor() : Value>(), MutableSet -abstract class MutableCharSetValue internal constructor() : Value>(), MutableSet -abstract class MutableStringSetValue internal constructor() : Value>(), MutableSet - -abstract class MutableSettingSetValue internal constructor() : Value>(), MutableSet - - -/** - * 只引用这个对象, 而不跟踪其成员. - * 仅适用于基础类型, 用于 mutable list/map 等情况; 或标注了 [Serializable] 的类. - */ -abstract class DynamicReferenceValue internal constructor() : Value() diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/ConsoleBuiltInSetting.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/ConsoleBuiltInSetting.kt deleted file mode 100644 index 5597e81cb..000000000 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/ConsoleBuiltInSetting.kt +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -package net.mamoe.mirai.console.setting.internal - -import kotlinx.coroutines.Job -import net.mamoe.mirai.console.MiraiConsole -import net.mamoe.mirai.console.setting.Setting -import net.mamoe.mirai.console.setting.Value - - -internal abstract class ConsoleBuiltInSetting : Setting() { - private val track = - @Suppress("LeakingThis") - MiraiConsole.jvmSettingStorage.trackOn(this) - - init { - MiraiConsole.coroutineContext[Job]!!.invokeOnCompletion { - track.close() - } - } - - override fun onElementChanged(value: Value<*>) { - TODO() - } -} \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/_ValueImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/_ValueImpl.kt deleted file mode 100644 index e6ac2ab62..000000000 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/_ValueImpl.kt +++ /dev/null @@ -1,1380 +0,0 @@ -/* - * Copyright 2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -package net.mamoe.mirai.console.setting.internal - -import kotlinx.serialization.Decoder -import kotlinx.serialization.Encoder -import kotlinx.serialization.KSerializer -import kotlinx.serialization.SerialDescriptor -import kotlinx.serialization.builtins.* -import net.mamoe.mirai.console.setting.* - - -/** - * !!! This file is auto-generated by backend/codegen/src/kotlin/net.mamoe.mirai.console.codegen.ValueImplCodegen.kt - * !!! DO NOT MODIFY THIS FILE MANUALLY - */ - - -internal fun Setting.valueImpl(default: Int): IntValue { - return object : IntValue() { - private var internalValue: Int = default - override var value: Int - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = Int.serializer() - } -} - -internal fun Setting.valueImpl(default: Short): ShortValue { - return object : ShortValue() { - private var internalValue: Short = default - override var value: Short - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = Short.serializer() - } -} - -internal fun Setting.valueImpl(default: Byte): ByteValue { - return object : ByteValue() { - private var internalValue: Byte = default - override var value: Byte - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = Byte.serializer() - } -} - -internal fun Setting.valueImpl(default: Long): LongValue { - return object : LongValue() { - private var internalValue: Long = default - override var value: Long - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = Long.serializer() - } -} - -internal fun Setting.valueImpl(default: Float): FloatValue { - return object : FloatValue() { - private var internalValue: Float = default - override var value: Float - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = Float.serializer() - } -} - -internal fun Setting.valueImpl(default: Double): DoubleValue { - return object : DoubleValue() { - private var internalValue: Double = default - override var value: Double - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = Double.serializer() - } -} - -internal fun Setting.valueImpl(default: Boolean): BooleanValue { - return object : BooleanValue() { - private var internalValue: Boolean = default - override var value: Boolean - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = Boolean.serializer() - } -} - -internal fun Setting.valueImpl(default: Char): CharValue { - return object : CharValue() { - private var internalValue: Char = default - override var value: Char - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = Char.serializer() - } -} - -internal fun Setting.valueImpl(default: String): StringValue { - return object : StringValue() { - private var internalValue: String = default - override var value: String - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = String.serializer() - } -} - -internal fun Setting.valueImpl(default: IntArray): IntArrayValue { - return object : IntArrayValue() { - private var internalValue: IntArray = default - override var value: IntArray - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = IntArraySerializer() - } -} - -internal fun Setting.valueImpl(default: ShortArray): ShortArrayValue { - return object : ShortArrayValue() { - private var internalValue: ShortArray = default - override var value: ShortArray - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ShortArraySerializer() - } -} - -internal fun Setting.valueImpl(default: ByteArray): ByteArrayValue { - return object : ByteArrayValue() { - private var internalValue: ByteArray = default - override var value: ByteArray - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ByteArraySerializer() - } -} - -internal fun Setting.valueImpl(default: LongArray): LongArrayValue { - return object : LongArrayValue() { - private var internalValue: LongArray = default - override var value: LongArray - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = LongArraySerializer() - } -} - -internal fun Setting.valueImpl(default: FloatArray): FloatArrayValue { - return object : FloatArrayValue() { - private var internalValue: FloatArray = default - override var value: FloatArray - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = FloatArraySerializer() - } -} - -internal fun Setting.valueImpl(default: DoubleArray): DoubleArrayValue { - return object : DoubleArrayValue() { - private var internalValue: DoubleArray = default - override var value: DoubleArray - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = DoubleArraySerializer() - } -} - -internal fun Setting.valueImpl(default: BooleanArray): BooleanArrayValue { - return object : BooleanArrayValue() { - private var internalValue: BooleanArray = default - override var value: BooleanArray - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = BooleanArraySerializer() - } -} - -internal fun Setting.valueImpl(default: CharArray): CharArrayValue { - return object : CharArrayValue() { - private var internalValue: CharArray = default - override var value: CharArray - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = CharArraySerializer() - } -} - -internal fun Setting.valueImpl(default: Array): TypedIntArrayValue { - return object : TypedIntArrayValue() { - private var internalValue: Array = default - override var value: Array - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ArraySerializer(Int.serializer()) - } -} - -internal fun Setting.valueImpl(default: Array): TypedShortArrayValue { - return object : TypedShortArrayValue() { - private var internalValue: Array = default - override var value: Array - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ArraySerializer(Short.serializer()) - } -} - -internal fun Setting.valueImpl(default: Array): TypedByteArrayValue { - return object : TypedByteArrayValue() { - private var internalValue: Array = default - override var value: Array - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ArraySerializer(Byte.serializer()) - } -} - -internal fun Setting.valueImpl(default: Array): TypedLongArrayValue { - return object : TypedLongArrayValue() { - private var internalValue: Array = default - override var value: Array - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ArraySerializer(Long.serializer()) - } -} - -internal fun Setting.valueImpl(default: Array): TypedFloatArrayValue { - return object : TypedFloatArrayValue() { - private var internalValue: Array = default - override var value: Array - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ArraySerializer(Float.serializer()) - } -} - -internal fun Setting.valueImpl(default: Array): TypedDoubleArrayValue { - return object : TypedDoubleArrayValue() { - private var internalValue: Array = default - override var value: Array - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ArraySerializer(Double.serializer()) - } -} - -internal fun Setting.valueImpl(default: Array): TypedBooleanArrayValue { - return object : TypedBooleanArrayValue() { - private var internalValue: Array = default - override var value: Array - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ArraySerializer(Boolean.serializer()) - } -} - -internal fun Setting.valueImpl(default: Array): TypedCharArrayValue { - return object : TypedCharArrayValue() { - private var internalValue: Array = default - override var value: Array - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ArraySerializer(Char.serializer()) - } -} - -internal fun Setting.valueImpl(default: Array): TypedStringArrayValue { - return object : TypedStringArrayValue() { - private var internalValue: Array = default - override var value: Array - get() = internalValue - set(new) { - if (!new.contentEquals(internalValue)) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ArraySerializer(String.serializer()) - } -} - -internal fun Setting.valueImpl(default: List): IntListValue { - var internalValue: List = default - val delegt = dynamicList { internalValue } - return object : IntListValue(), List by delegt { - override var value: List - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ListSerializer(Int.serializer()) - } -} - -internal fun Setting.valueImpl(default: List): ShortListValue { - var internalValue: List = default - val delegt = dynamicList { internalValue } - return object : ShortListValue(), List by delegt { - override var value: List - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ListSerializer(Short.serializer()) - } -} - -internal fun Setting.valueImpl(default: List): ByteListValue { - var internalValue: List = default - val delegt = dynamicList { internalValue } - return object : ByteListValue(), List by delegt { - override var value: List - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ListSerializer(Byte.serializer()) - } -} - -internal fun Setting.valueImpl(default: List): LongListValue { - var internalValue: List = default - val delegt = dynamicList { internalValue } - return object : LongListValue(), List by delegt { - override var value: List - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ListSerializer(Long.serializer()) - } -} - -internal fun Setting.valueImpl(default: List): FloatListValue { - var internalValue: List = default - val delegt = dynamicList { internalValue } - return object : FloatListValue(), List by delegt { - override var value: List - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ListSerializer(Float.serializer()) - } -} - -internal fun Setting.valueImpl(default: List): DoubleListValue { - var internalValue: List = default - val delegt = dynamicList { internalValue } - return object : DoubleListValue(), List by delegt { - override var value: List - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ListSerializer(Double.serializer()) - } -} - -internal fun Setting.valueImpl(default: List): BooleanListValue { - var internalValue: List = default - val delegt = dynamicList { internalValue } - return object : BooleanListValue(), List by delegt { - override var value: List - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ListSerializer(Boolean.serializer()) - } -} - -internal fun Setting.valueImpl(default: List): CharListValue { - var internalValue: List = default - val delegt = dynamicList { internalValue } - return object : CharListValue(), List by delegt { - override var value: List - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ListSerializer(Char.serializer()) - } -} - -internal fun Setting.valueImpl(default: List): StringListValue { - var internalValue: List = default - val delegt = dynamicList { internalValue } - return object : StringListValue(), List by delegt { - override var value: List - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = ListSerializer(String.serializer()) - } -} - -internal fun Setting.valueImpl(default: Set): IntSetValue { - var internalValue: Set = default - val delegt = dynamicSet { internalValue } - return object : IntSetValue(), Set by delegt { - override var value: Set - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = SetSerializer(Int.serializer()) - } -} - -internal fun Setting.valueImpl(default: Set): ShortSetValue { - var internalValue: Set = default - val delegt = dynamicSet { internalValue } - return object : ShortSetValue(), Set by delegt { - override var value: Set - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = SetSerializer(Short.serializer()) - } -} - -internal fun Setting.valueImpl(default: Set): ByteSetValue { - var internalValue: Set = default - val delegt = dynamicSet { internalValue } - return object : ByteSetValue(), Set by delegt { - override var value: Set - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = SetSerializer(Byte.serializer()) - } -} - -internal fun Setting.valueImpl(default: Set): LongSetValue { - var internalValue: Set = default - val delegt = dynamicSet { internalValue } - return object : LongSetValue(), Set by delegt { - override var value: Set - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = SetSerializer(Long.serializer()) - } -} - -internal fun Setting.valueImpl(default: Set): FloatSetValue { - var internalValue: Set = default - val delegt = dynamicSet { internalValue } - return object : FloatSetValue(), Set by delegt { - override var value: Set - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = SetSerializer(Float.serializer()) - } -} - -internal fun Setting.valueImpl(default: Set): DoubleSetValue { - var internalValue: Set = default - val delegt = dynamicSet { internalValue } - return object : DoubleSetValue(), Set by delegt { - override var value: Set - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = SetSerializer(Double.serializer()) - } -} - -internal fun Setting.valueImpl(default: Set): BooleanSetValue { - var internalValue: Set = default - val delegt = dynamicSet { internalValue } - return object : BooleanSetValue(), Set by delegt { - override var value: Set - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = SetSerializer(Boolean.serializer()) - } -} - -internal fun Setting.valueImpl(default: Set): CharSetValue { - var internalValue: Set = default - val delegt = dynamicSet { internalValue } - return object : CharSetValue(), Set by delegt { - override var value: Set - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = SetSerializer(Char.serializer()) - } -} - -internal fun Setting.valueImpl(default: Set): StringSetValue { - var internalValue: Set = default - val delegt = dynamicSet { internalValue } - return object : StringSetValue(), Set by delegt { - override var value: Set - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer get() = SetSerializer(String.serializer()) - } -} - - -@JvmName("valueImplMutableIntList") -internal fun Setting.valueImpl( - default: MutableList -): MutableIntListValue { - var internalValue: MutableList = default - - val delegt = dynamicMutableList { internalValue } - return object : MutableIntListValue(), MutableList by delegt { - override var value: MutableList - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = ListSerializer(Int.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableList { - return delegate.deserialize(decoder).toMutableList().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableList) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableShortList") -internal fun Setting.valueImpl( - default: MutableList -): MutableShortListValue { - var internalValue: MutableList = default - - val delegt = dynamicMutableList { internalValue } - return object : MutableShortListValue(), MutableList by delegt { - override var value: MutableList - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = ListSerializer(Short.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableList { - return delegate.deserialize(decoder).toMutableList().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableList) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableByteList") -internal fun Setting.valueImpl( - default: MutableList -): MutableByteListValue { - var internalValue: MutableList = default - - val delegt = dynamicMutableList { internalValue } - return object : MutableByteListValue(), MutableList by delegt { - override var value: MutableList - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = ListSerializer(Byte.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableList { - return delegate.deserialize(decoder).toMutableList().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableList) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableLongList") -internal fun Setting.valueImpl( - default: MutableList -): MutableLongListValue { - var internalValue: MutableList = default - - val delegt = dynamicMutableList { internalValue } - return object : MutableLongListValue(), MutableList by delegt { - override var value: MutableList - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = ListSerializer(Long.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableList { - return delegate.deserialize(decoder).toMutableList().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableList) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableFloatList") -internal fun Setting.valueImpl( - default: MutableList -): MutableFloatListValue { - var internalValue: MutableList = default - - val delegt = dynamicMutableList { internalValue } - return object : MutableFloatListValue(), MutableList by delegt { - override var value: MutableList - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = ListSerializer(Float.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableList { - return delegate.deserialize(decoder).toMutableList().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableList) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableDoubleList") -internal fun Setting.valueImpl( - default: MutableList -): MutableDoubleListValue { - var internalValue: MutableList = default - - val delegt = dynamicMutableList { internalValue } - return object : MutableDoubleListValue(), MutableList by delegt { - override var value: MutableList - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = ListSerializer(Double.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableList { - return delegate.deserialize(decoder).toMutableList().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableList) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableBooleanList") -internal fun Setting.valueImpl( - default: MutableList -): MutableBooleanListValue { - var internalValue: MutableList = default - - val delegt = dynamicMutableList { internalValue } - return object : MutableBooleanListValue(), MutableList by delegt { - override var value: MutableList - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = ListSerializer(Boolean.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableList { - return delegate.deserialize(decoder).toMutableList().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableList) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableCharList") -internal fun Setting.valueImpl( - default: MutableList -): MutableCharListValue { - var internalValue: MutableList = default - - val delegt = dynamicMutableList { internalValue } - return object : MutableCharListValue(), MutableList by delegt { - override var value: MutableList - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = ListSerializer(Char.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableList { - return delegate.deserialize(decoder).toMutableList().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableList) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableStringList") -internal fun Setting.valueImpl( - default: MutableList -): MutableStringListValue { - var internalValue: MutableList = default - - val delegt = dynamicMutableList { internalValue } - return object : MutableStringListValue(), MutableList by delegt { - override var value: MutableList - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = ListSerializer(String.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableList { - return delegate.deserialize(decoder).toMutableList().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableList) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableIntSet") -internal fun Setting.valueImpl( - default: MutableSet -): MutableIntSetValue { - var internalValue: MutableSet = default - - val delegt = dynamicMutableSet { internalValue } - return object : MutableIntSetValue(), MutableSet by delegt { - override var value: MutableSet - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = SetSerializer(Int.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableSet { - return delegate.deserialize(decoder).toMutableSet().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableSet) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableShortSet") -internal fun Setting.valueImpl( - default: MutableSet -): MutableShortSetValue { - var internalValue: MutableSet = default - - val delegt = dynamicMutableSet { internalValue } - return object : MutableShortSetValue(), MutableSet by delegt { - override var value: MutableSet - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = SetSerializer(Short.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableSet { - return delegate.deserialize(decoder).toMutableSet().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableSet) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableByteSet") -internal fun Setting.valueImpl( - default: MutableSet -): MutableByteSetValue { - var internalValue: MutableSet = default - - val delegt = dynamicMutableSet { internalValue } - return object : MutableByteSetValue(), MutableSet by delegt { - override var value: MutableSet - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = SetSerializer(Byte.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableSet { - return delegate.deserialize(decoder).toMutableSet().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableSet) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableLongSet") -internal fun Setting.valueImpl( - default: MutableSet -): MutableLongSetValue { - var internalValue: MutableSet = default - - val delegt = dynamicMutableSet { internalValue } - return object : MutableLongSetValue(), MutableSet by delegt { - override var value: MutableSet - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = SetSerializer(Long.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableSet { - return delegate.deserialize(decoder).toMutableSet().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableSet) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableFloatSet") -internal fun Setting.valueImpl( - default: MutableSet -): MutableFloatSetValue { - var internalValue: MutableSet = default - - val delegt = dynamicMutableSet { internalValue } - return object : MutableFloatSetValue(), MutableSet by delegt { - override var value: MutableSet - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = SetSerializer(Float.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableSet { - return delegate.deserialize(decoder).toMutableSet().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableSet) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableDoubleSet") -internal fun Setting.valueImpl( - default: MutableSet -): MutableDoubleSetValue { - var internalValue: MutableSet = default - - val delegt = dynamicMutableSet { internalValue } - return object : MutableDoubleSetValue(), MutableSet by delegt { - override var value: MutableSet - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = SetSerializer(Double.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableSet { - return delegate.deserialize(decoder).toMutableSet().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableSet) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableBooleanSet") -internal fun Setting.valueImpl( - default: MutableSet -): MutableBooleanSetValue { - var internalValue: MutableSet = default - - val delegt = dynamicMutableSet { internalValue } - return object : MutableBooleanSetValue(), MutableSet by delegt { - override var value: MutableSet - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = SetSerializer(Boolean.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableSet { - return delegate.deserialize(decoder).toMutableSet().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableSet) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableCharSet") -internal fun Setting.valueImpl( - default: MutableSet -): MutableCharSetValue { - var internalValue: MutableSet = default - - val delegt = dynamicMutableSet { internalValue } - return object : MutableCharSetValue(), MutableSet by delegt { - override var value: MutableSet - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = SetSerializer(Char.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableSet { - return delegate.deserialize(decoder).toMutableSet().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableSet) { - delegate.serialize(encoder, value) - } - } - } -} - -@JvmName("valueImplMutableStringSet") -internal fun Setting.valueImpl( - default: MutableSet -): MutableStringSetValue { - var internalValue: MutableSet = default - - val delegt = dynamicMutableSet { internalValue } - return object : MutableStringSetValue(), MutableSet by delegt { - override var value: MutableSet - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - - private val outerThis get() = this - - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = SetSerializer(String.serializer()) - override val descriptor: SerialDescriptor get() = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableSet { - return delegate.deserialize(decoder).toMutableSet().observable { - onElementChanged(outerThis) - } - } - - override fun serialize(encoder: Encoder, value: MutableSet) { - delegate.serialize(encoder, value) - } - } - } -} - - -internal fun Setting.valueImpl(default: T): Value { - return object : SettingValue() { - private var internalValue: T = default - override var value: T - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer = object : KSerializer { - override val descriptor: SerialDescriptor - get() = internalValue.updaterSerializer.descriptor - - override fun deserialize(decoder: Decoder): T { - internalValue.updaterSerializer.deserialize(decoder) - return internalValue - } - - override fun serialize(encoder: Encoder, value: T) { - internalValue.updaterSerializer.serialize(encoder, SettingSerializerMark) - } - } - } -} - diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/internal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/internal.kt index 3ca8bbafb..b054b31b8 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/internal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/internal.kt @@ -12,140 +12,12 @@ package net.mamoe.mirai.console.setting.internal import kotlinx.serialization.* import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.builtins.serializer -import net.mamoe.mirai.console.setting.SerialName -import net.mamoe.mirai.console.setting.Setting -import net.mamoe.mirai.console.setting.Value import net.mamoe.mirai.utils.MiraiExperimentalAPI import net.mamoe.yamlkt.Yaml import net.mamoe.yamlkt.YamlConfiguration import kotlin.reflect.KProperty import kotlin.reflect.full.findAnnotation -internal abstract class SettingImpl { - - @JvmField - internal var valueList: MutableList, Setting.PropertyInfo>> = mutableListOf() - - @JvmField - internal var built: Boolean = false - - internal val updaterSerializer: KSerializer by lazy { - built = true - SettingUpdaterSerializer(this as Setting) - } - - internal val kotlinSerializer: KSerializer by lazy { - object : KSerializer { - override val descriptor: SerialDescriptor - get() = this@SettingImpl.updaterSerializer.descriptor - - override fun deserialize(decoder: Decoder): Setting { - this@SettingImpl.updaterSerializer.deserialize(decoder) - return this@SettingImpl as Setting - } - - override fun serialize(encoder: Encoder, value: Setting) { - this@SettingImpl.updaterSerializer.serialize( - encoder, - SettingSerializerMark - ) - } - } - } - - abstract fun onElementChanged(value: Value<*>) - - companion object { - @JvmStatic - internal val yamlForToString = - Yaml( - configuration = YamlConfiguration( - nonStrictNullability = true, - nonStrictNumber = true, - stringSerialization = YamlConfiguration.StringSerialization.NONE, - classSerialization = YamlConfiguration.MapSerialization.FLOW_MAP, - listSerialization = YamlConfiguration.ListSerialization.FLOW_SEQUENCE - ) - ) - } -} - -internal class SettingUpdaterSerializer( - private val instance: Setting -) : KSerializer { - override val descriptor: SerialDescriptor by lazy { - @OptIn(MiraiExperimentalAPI::class) - SerialDescriptor(instance.serialName) { - for ((value, prop) in instance.valueList) { - val (serialName, annotations) = prop - element(serialName, value.serializer.descriptor, annotations, true) - } - } - } - - @Suppress("UNCHECKED_CAST") // erased, no problem. - override fun deserialize(decoder: Decoder): SettingSerializerMark = decoder.decodeStructure(descriptor) { - if (this.decodeSequentially()) { - instance.valueList.forEachIndexed { index, (value, _) -> - val v = value as Value - v.value = this.decodeSerializableElement( - value.serializer.descriptor, - index, - v.serializer - ) - } - } else { - while (true) { - val index = this.decodeElementIndex(descriptor) - if (index == CompositeDecoder.READ_DONE) return@decodeStructure SettingSerializerMark - val value = instance.valueList[index].first as Value - value.value = this.decodeSerializableElement( - descriptor, - index, - value.serializer - ) - } - } - SettingSerializerMark - } - - private val emptyList = emptyList() - private val emptyListSerializer = ListSerializer(String.serializer()) - - override fun serialize(encoder: Encoder, value: SettingSerializerMark) { - if (instance.valueList.isEmpty()) { - emptyListSerializer.serialize(encoder, emptyList) - } else encoder.encodeStructure(descriptor) { - instance.valueList.forEachIndexed { index, (value, _) -> - @Suppress("UNCHECKED_CAST") // erased, no problem. - this.encodeElementSmart(descriptor, index, value) - } - } - } - -} - -// until https://github.com/Him188/yamlkt/issues/2 fixed -internal fun CompositeEncoder.encodeElementSmart( - descriptor: SerialDescriptor, - index: Int, - value: Value -) { - when (value.value::class) { - String::class -> this.encodeStringElement(descriptor, index, value.value as String) - Int::class -> this.encodeIntElement(descriptor, index, value.value as Int) - Byte::class -> this.encodeByteElement(descriptor, index, value.value as Byte) - Char::class -> this.encodeCharElement(descriptor, index, value.value as Char) - Long::class -> this.encodeLongElement(descriptor, index, value.value as Long) - Float::class -> this.encodeFloatElement(descriptor, index, value.value as Float) - Double::class -> this.encodeDoubleElement(descriptor, index, value.value as Double) - Boolean::class -> this.encodeBooleanElement(descriptor, index, value.value as Boolean) - else -> - @Suppress("UNCHECKED_CAST") - this.encodeSerializableElement(descriptor, index, value.serializer as KSerializer, value.value) - } -} - internal object SettingSerializerMark internal val KProperty<*>.serialNameOrPropertyName: String get() = this.findAnnotation()?.value ?: this.name diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/internalValueImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/internalValueImpl.kt deleted file mode 100644 index 113a9da03..000000000 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/internalValueImpl.kt +++ /dev/null @@ -1,324 +0,0 @@ -/* - * Copyright 2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ -@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") - -package net.mamoe.mirai.console.setting.internal - -import kotlinx.serialization.* -import kotlinx.serialization.builtins.ListSerializer -import kotlinx.serialization.builtins.SetSerializer -import net.mamoe.mirai.console.setting.* -import net.mamoe.yamlkt.YamlDynamicSerializer -import kotlin.internal.LowPriorityInOverloadResolution -import kotlin.reflect.KClass -import kotlin.reflect.KType -import kotlin.reflect.full.createInstance -import kotlin.reflect.typeOf - -/// region MUTABLE LIST - -@PublishedApi -@JvmName("valueImplSetting") -@Suppress("UNCHECKED_CAST") -internal inline fun Setting.valueImpl( - default: List -): SettingListValue = valueImpl(default, T::class.createInstance().serializer) - -@PublishedApi -@JvmName("valueImplSettingMutable") -@Suppress("UNCHECKED_CAST") -internal inline fun Setting.valueImpl( - default: MutableList -): MutableSettingListValue = valueImpl(default, T::class.createInstance().serializer) - - -/* -@PublishedApi -@JvmName("valueImpl1") -internal fun Setting.valueImpl( - default: MutableList, - valueMapper: (T) -> Value, - elementSerializer: KSerializer -): MutableListValue = valueImpl(default.mapTo(mutableListOf(), valueMapper), valueMapper, elementSerializer) -*/ -internal fun Setting.valueImpl( - default: MutableList>, - valueMapper: (T) -> Value, - elementSerializer: KSerializer -): MutableListValue { - var internalValue: MutableList> = default - - fun updateShadow(): MutableList = - internalValue.shadowMap(transform = { it.value }, transformBack = { valueMapper(it) }) - - var shadowed: MutableList = updateShadow() - - - val delegt = dynamicMutableList { shadowed } - return object : MutableListValue(), MutableList by delegt { - override var value: MutableList> - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - shadowed = updateShadow() - onElementChanged(this) - } - } - override val serializer: KSerializer>> = object : KSerializer>> { - private val delegate = ListSerializer(elementSerializer) - override val descriptor: SerialDescriptor = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableList> { - return delegate.deserialize(decoder).mapTo(mutableListOf(), valueMapper) - } - - override fun serialize(encoder: Encoder, value: MutableList>) { - delegate.serialize(encoder, value.map { it.value }) - } - - } - } -} - -@PublishedApi -internal fun Setting.valueImpl( - default: MutableList, - elementSerializer: KSerializer -): MutableSettingListValue { - var internalValue: MutableList = default - - val delegt = dynamicMutableList { internalValue } - return object : MutableSettingListValue(), MutableList by delegt { - override var value: MutableList - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = ListSerializer(elementSerializer) - override val descriptor: SerialDescriptor = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableList { - return delegate.deserialize(decoder).toMutableList() // TODO: 2020/5/17 ATTACH OBSERVER - } - - override fun serialize(encoder: Encoder, value: MutableList) { - delegate.serialize(encoder, value) - } - - } - } -} - -@PublishedApi -internal fun Setting.valueImpl( - default: List, - elementSerializer: KSerializer -): SettingListValue { - var internalValue: List = default - - val delegt = dynamicList { internalValue } - return object : SettingListValue(), List by delegt { - override var value: List - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = ListSerializer(elementSerializer) - override val descriptor: SerialDescriptor = delegate.descriptor - - override fun deserialize(decoder: Decoder): List { - return delegate.deserialize(decoder) // TODO: 2020/5/17 ATTACH OBSERVER - } - - override fun serialize(encoder: Encoder, value: List) { - delegate.serialize(encoder, value) - } - - } - } -} - -@PublishedApi -internal fun Setting.valueImpl( - default: Set, - elementSerializer: KSerializer -): SettingSetValue { - var internalValue: Set = default - - val delegt = dynamicSet { internalValue } - return object : SettingSetValue(), Set by delegt { - override var value: Set - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = SetSerializer(elementSerializer) - override val descriptor: SerialDescriptor = delegate.descriptor - - override fun deserialize(decoder: Decoder): Set { - return delegate.deserialize(decoder) // TODO: 2020/5/17 ATTACH OBSERVER - } - - override fun serialize(encoder: Encoder, value: Set) { - delegate.serialize(encoder, value) - } - - } - } -} - -@PublishedApi -internal fun Setting.valueImpl( - default: MutableSet, - elementSerializer: KSerializer -): MutableSettingSetValue { - var internalValue: MutableSet = default - - val delegt = dynamicMutableSet { internalValue } - return object : MutableSettingSetValue(), MutableSet by delegt { - override var value: MutableSet - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - onElementChanged(this) - } - } - override val serializer: KSerializer> = object : KSerializer> { - private val delegate = SetSerializer(elementSerializer) - override val descriptor: SerialDescriptor = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableSet { - return delegate.deserialize(decoder).toMutableSet() // TODO: 2020/5/17 ATTACH OBSERVER - } - - override fun serialize(encoder: Encoder, value: MutableSet) { - delegate.serialize(encoder, value) - } - - } - } -} - -// endregion - - -// region MUTABLE SET - -@PublishedApi -@JvmName("valueImplSetting") -@Suppress("UNCHECKED_CAST") -internal inline fun Setting.valueImpl( - default: Set -): SettingSetValue = valueImpl(default, T::class.createInstance().serializer) - -@PublishedApi -@JvmName("valueImplSettingMutable") -@Suppress("UNCHECKED_CAST") -internal inline fun Setting.valueImpl( - default: MutableSet -): MutableSettingSetValue = valueImpl(default, T::class.createInstance().serializer) - -/* -@JvmName("valueImpl1") -@PublishedApi -internal fun Setting.valueImpl( - default: MutableSet, - valueMapper: (T) -> Value, - elementSerializer: KSerializer -): MutableSetValue = valueImpl(default.mapTo(mutableSetOf(), valueMapper), valueMapper, elementSerializer) -*/ -@JvmName("valueImplMutable") -internal fun Setting.valueImpl( - default: MutableSet>, - valueMapper: (T) -> Value, - elementSerializer: KSerializer -): MutableSetValue { - var internalValue: MutableSet> = default - - fun updateShadow(): MutableSet = - internalValue.shadowMap(transform = { it.value }, transformBack = { valueMapper(it) }) - - var shadowed: MutableSet = updateShadow() - - val delegt = dynamicMutableSet { shadowed } - return object : MutableSetValue(), MutableSet by delegt { - override var value: MutableSet> - get() = internalValue - set(new) { - if (new != internalValue) { - internalValue = new - shadowed = updateShadow() - onElementChanged(this) - } - } - override val serializer: KSerializer>> = object : KSerializer>> { - private val delegate = SetSerializer(elementSerializer) - override val descriptor: SerialDescriptor = delegate.descriptor - - override fun deserialize(decoder: Decoder): MutableSet> { - return delegate.deserialize(decoder).mapTo(mutableSetOf(), valueMapper) - } - - override fun serialize(encoder: Encoder, value: MutableSet>) { - delegate.serialize(encoder, value.mapTo(mutableSetOf()) { it.value }) - } - } - } -} - -// endregion - -// region DYNAMIC PRIMITIVES AND SERIALIZABLE - -/** - * For primitives and serializable only - */ -@OptIn(ExperimentalStdlibApi::class) -@PublishedApi -@LowPriorityInOverloadResolution -internal inline fun Setting.valueImpl(default: T): Value = - valueImpl(default, T::class) - -@PublishedApi -internal fun Setting.valueImpl(default: T, clazz: KClass): Value { - if (default is Setting) @Suppress("UNCHECKED_CAST") return valueImpl(default as Setting) as Value - - @OptIn(ImplicitReflectionSerializer::class) - requireNotNull(clazz.serializerOrNull()) { - "${clazz.qualifiedName} is not serializable" - } - return object : DynamicReferenceValue() { - override var value: T = default - override val serializer: KSerializer = object : KSerializer { - override val descriptor: SerialDescriptor - get() = YamlDynamicSerializer.descriptor - - override fun deserialize(decoder: Decoder): T = - YamlDynamicSerializer.deserialize(decoder).smartCastPrimitive(clazz) - - override fun serialize(encoder: Encoder, value: T) = YamlDynamicSerializer.serialize(encoder, value) - } - } -} - -// endregion \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/utils/BotManagers.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/utils/BotManagers.kt index 65be10639..f3234a2ec 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/utils/BotManagers.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/utils/BotManagers.kt @@ -10,17 +10,8 @@ package net.mamoe.mirai.console.utils -import kotlinx.atomicfu.locks.withLock import net.mamoe.mirai.Bot -import net.mamoe.mirai.console.setting.Value -import net.mamoe.mirai.console.setting.internal.ConsoleBuiltInSetting -import net.mamoe.mirai.console.setting.value import net.mamoe.mirai.contact.User -import java.util.concurrent.TimeUnit -import java.util.concurrent.locks.Condition -import java.util.concurrent.locks.Lock -import java.util.concurrent.locks.ReentrantLock -import kotlin.properties.ReadWriteProperty /** diff --git a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommands.kt b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommands.kt deleted file mode 100644 index 797047519..000000000 --- a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommands.kt +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright 2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") - -package net.mamoe.mirai.console.command - -import net.mamoe.mirai.Bot -import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin -import net.mamoe.mirai.console.setting.value -import net.mamoe.mirai.message.data.* -import org.junit.jupiter.api.Test -import kotlin.test.assertEquals - - -val plugin = MyPlugin() - -class MyPlugin : KotlinPlugin() { - - inner class MySetting : PluginSetting() { - val int by value(1) - } -} - -/* -internal object TestCommand : PluginCommand( - plugin, - CommandDescriptor("test") { - param() - } -) { - override suspend fun CommandSender.onCommand(args: CommandArgs): Boolean { - val s = args.getReified() - sendMessage(s) - return true - } -}*/ - -internal class TestCommands { - @Test - fun testFlatten() { - assertEquals(listOf("test", "v1"), "test v1".flattenCommandComponents().toList()) - assertEquals(listOf("test", "v1"), PlainText("test v1").flattenCommandComponents().toList()) - assertEquals(listOf("test", "v1"), arrayOf("test ", "v1", " ").flattenCommandComponents().toList()) - assertEquals( - listOf("test", "v1"), - messageChainOf("test v1".toMessage(), " ".toMessage()).flattenCommandComponents().toList() - ) - } - - /* - @Test - fun testRegister() { - assertTrue(TestCommand.register()) - assertEquals(listOf("test"), TestCommand.allNames.single().toList()) - - assertFalse(TestCommand.register()) - assertFalse( - object : PluginCommand( - plugin, - CommandDescriptor("test") { - param() - } - ) { - override suspend fun CommandSender.onCommand(args: CommandArgs): Boolean { - val s = args.getReified() - sendMessage(s) - return true - } - }.register() - ) - } - - @Test - fun testExecute() = runBlocking { - TestCommand.register() - assertEquals( - "ok", - withSender { - execute("test", "arg") - }.contentToString() - ) - }*/ -} - - -internal inline fun withSender(block: CommandSender.() -> Unit): MessageChain { - val result = MessageChainBuilder() - val sender: CommandSender = object : CommandSender { - override val bot: Bot? - get() = null - - override suspend fun sendMessage(message: Message) { - result.add(message) - } - - } - sender.let(block) - return result.asMessageChain() -} \ No newline at end of file diff --git a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestComposite.kt b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestComposite.kt index 8782588bd..c1cfe009f 100644 --- a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestComposite.kt +++ b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestComposite.kt @@ -9,15 +9,19 @@ package net.mamoe.mirai.console.command +import net.mamoe.mirai.contact.Member +import net.mamoe.mirai.message.data.Image import org.junit.jupiter.api.Test object TestCompositeCommand : CompositeCommand( TestCommandOwner, - "name1", "name2", - description = """ - desc - """.trimIndent() -) + "groupManagement", "grpMgn" +) { + @SubCommand + suspend fun CommandSender.mute(image: Image, target: Member, seconds: Int) { + target.mute(seconds) + } +} internal class TestComposite {