diff --git a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueImplCodegen.kt b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueImplCodegen.kt index 1e06ea9e7..0401e5093 100644 --- a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueImplCodegen.kt +++ b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueImplCodegen.kt @@ -57,11 +57,13 @@ fun genAllValueImpl(): String = buildString { // PRIMITIVE for (number in NUMBERS + OTHER_PRIMITIVES) { appendln(genValueImpl(number, number, "$number.serializer()", false)) + appendln() } // PRIMITIVE ARRAYS for (number in NUMBERS + OTHER_PRIMITIVES.filterNot { it == "String" }) { appendln(genValueImpl("${number}Array", "${number}Array", "${number}ArraySerializer()", true)) + appendln() } // TYPED ARRAYS @@ -74,6 +76,7 @@ fun genAllValueImpl(): String = buildString { true ) ) + appendln() } // PRIMITIVE LISTS / SETS @@ -87,6 +90,7 @@ fun genAllValueImpl(): String = buildString { false ) ) + appendln() } } @@ -137,6 +141,40 @@ fun genAllValueImpl(): String = buildString { appendln() } } + + appendln() + + + appendln( + """ + 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) + } + } + } + } + """ + ) } fun genValueImpl(kotlinTypeName: String, miraiValueName: String, serializer: String, isArray: Boolean): String = diff --git a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValuesCodegen.kt b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValuesCodegen.kt index ff92d8851..d0d984b07 100644 --- a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValuesCodegen.kt +++ b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValuesCodegen.kt @@ -65,10 +65,7 @@ fun genPublicApi() = buildString { """ package net.mamoe.mirai.console.setting - import kotlinx.serialization.Decoder - import kotlinx.serialization.Encoder import kotlinx.serialization.KSerializer - import kotlinx.serialization.SerialDescriptor import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty """ @@ -221,42 +218,11 @@ sealed class Value : ReadWriteProperty { appendln() } - // FOR COMPLEX TYPES + // SETTING VALUE appendln( """ abstract class SettingValue internal constructor() : 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) - } - - }.bind( - getter = { internalValue }, - setter = { internalValue = it } - ) - } - } """ ) 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 index 257acbbb9..a28b5e9f4 100644 --- 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 @@ -9,10 +9,7 @@ package net.mamoe.mirai.console.setting -import kotlinx.serialization.Decoder -import kotlinx.serialization.Encoder import kotlinx.serialization.KSerializer -import kotlinx.serialization.SerialDescriptor import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty @@ -65,29 +62,36 @@ 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{ +sealed class TypedPrimitiveArrayValue : ArrayValue>(), Iterable { override fun iterator() = this.value.iterator() } @@ -101,7 +105,7 @@ abstract class TypedBooleanArrayValue internal constructor() : TypedPrimitiveArr abstract class TypedCharArrayValue internal constructor() : TypedPrimitiveArrayValue() abstract class TypedStringArrayValue internal constructor() : TypedPrimitiveArrayValue() -sealed class ListValue : Value>(), Iterable{ +sealed class ListValue : Value>(), Iterable { override fun iterator() = this.value.iterator() } @@ -135,37 +139,6 @@ abstract class SettingSetValue internal constructor() : Value internal constructor() : 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) - } - - }.bind( - getter = { internalValue }, - setter = { internalValue = it } - ) - } -} - abstract class MutableListValue internal constructor() : Value>>(), MutableList abstract class MutableIntListValue internal constructor() : Value>(), MutableList diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/_ValueImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/_ValueImpl.kt index 32662bd32..17ce4184b 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/_ValueImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/_ValueImpl.kt @@ -1,4 +1,3 @@ - /* * Copyright 2020 Mamoe Technologies and contributors. * @@ -38,6 +37,7 @@ internal fun Setting.valueImpl(default: Int): IntValue { override val serializer = Int.serializer() } } + internal fun Setting.valueImpl(default: Short): ShortValue { return object : ShortValue() { private var internalValue: Short = default @@ -52,6 +52,7 @@ internal fun Setting.valueImpl(default: Short): ShortValue { override val serializer = Short.serializer() } } + internal fun Setting.valueImpl(default: Byte): ByteValue { return object : ByteValue() { private var internalValue: Byte = default @@ -66,6 +67,7 @@ internal fun Setting.valueImpl(default: Byte): ByteValue { override val serializer = Byte.serializer() } } + internal fun Setting.valueImpl(default: Long): LongValue { return object : LongValue() { private var internalValue: Long = default @@ -80,6 +82,7 @@ internal fun Setting.valueImpl(default: Long): LongValue { override val serializer = Long.serializer() } } + internal fun Setting.valueImpl(default: Float): FloatValue { return object : FloatValue() { private var internalValue: Float = default @@ -94,6 +97,7 @@ internal fun Setting.valueImpl(default: Float): FloatValue { override val serializer = Float.serializer() } } + internal fun Setting.valueImpl(default: Double): DoubleValue { return object : DoubleValue() { private var internalValue: Double = default @@ -108,6 +112,7 @@ internal fun Setting.valueImpl(default: Double): DoubleValue { override val serializer = Double.serializer() } } + internal fun Setting.valueImpl(default: Boolean): BooleanValue { return object : BooleanValue() { private var internalValue: Boolean = default @@ -122,6 +127,7 @@ internal fun Setting.valueImpl(default: Boolean): BooleanValue { override val serializer = Boolean.serializer() } } + internal fun Setting.valueImpl(default: Char): CharValue { return object : CharValue() { private var internalValue: Char = default @@ -136,6 +142,7 @@ internal fun Setting.valueImpl(default: Char): CharValue { override val serializer = Char.serializer() } } + internal fun Setting.valueImpl(default: String): StringValue { return object : StringValue() { private var internalValue: String = default @@ -150,6 +157,7 @@ internal fun Setting.valueImpl(default: String): StringValue { override val serializer = String.serializer() } } + internal fun Setting.valueImpl(default: IntArray): IntArrayValue { return object : IntArrayValue() { private var internalValue: IntArray = default @@ -164,6 +172,7 @@ internal fun Setting.valueImpl(default: IntArray): IntArrayValue { override val serializer = IntArraySerializer() } } + internal fun Setting.valueImpl(default: ShortArray): ShortArrayValue { return object : ShortArrayValue() { private var internalValue: ShortArray = default @@ -178,6 +187,7 @@ internal fun Setting.valueImpl(default: ShortArray): ShortArrayValue { override val serializer = ShortArraySerializer() } } + internal fun Setting.valueImpl(default: ByteArray): ByteArrayValue { return object : ByteArrayValue() { private var internalValue: ByteArray = default @@ -192,6 +202,7 @@ internal fun Setting.valueImpl(default: ByteArray): ByteArrayValue { override val serializer = ByteArraySerializer() } } + internal fun Setting.valueImpl(default: LongArray): LongArrayValue { return object : LongArrayValue() { private var internalValue: LongArray = default @@ -206,6 +217,7 @@ internal fun Setting.valueImpl(default: LongArray): LongArrayValue { override val serializer = LongArraySerializer() } } + internal fun Setting.valueImpl(default: FloatArray): FloatArrayValue { return object : FloatArrayValue() { private var internalValue: FloatArray = default @@ -220,6 +232,7 @@ internal fun Setting.valueImpl(default: FloatArray): FloatArrayValue { override val serializer = FloatArraySerializer() } } + internal fun Setting.valueImpl(default: DoubleArray): DoubleArrayValue { return object : DoubleArrayValue() { private var internalValue: DoubleArray = default @@ -234,6 +247,7 @@ internal fun Setting.valueImpl(default: DoubleArray): DoubleArrayValue { override val serializer = DoubleArraySerializer() } } + internal fun Setting.valueImpl(default: BooleanArray): BooleanArrayValue { return object : BooleanArrayValue() { private var internalValue: BooleanArray = default @@ -248,6 +262,7 @@ internal fun Setting.valueImpl(default: BooleanArray): BooleanArrayValue { override val serializer = BooleanArraySerializer() } } + internal fun Setting.valueImpl(default: CharArray): CharArrayValue { return object : CharArrayValue() { private var internalValue: CharArray = default @@ -262,6 +277,7 @@ internal fun Setting.valueImpl(default: CharArray): CharArrayValue { override val serializer = CharArraySerializer() } } + internal fun Setting.valueImpl(default: Array): TypedIntArrayValue { return object : TypedIntArrayValue() { private var internalValue: Array = default @@ -276,6 +292,7 @@ internal fun Setting.valueImpl(default: Array): TypedIntArrayValue { override val serializer = ArraySerializer(Int.serializer()) } } + internal fun Setting.valueImpl(default: Array): TypedShortArrayValue { return object : TypedShortArrayValue() { private var internalValue: Array = default @@ -290,6 +307,7 @@ internal fun Setting.valueImpl(default: Array): TypedShortArrayValue { override val serializer = ArraySerializer(Short.serializer()) } } + internal fun Setting.valueImpl(default: Array): TypedByteArrayValue { return object : TypedByteArrayValue() { private var internalValue: Array = default @@ -304,6 +322,7 @@ internal fun Setting.valueImpl(default: Array): TypedByteArrayValue { override val serializer = ArraySerializer(Byte.serializer()) } } + internal fun Setting.valueImpl(default: Array): TypedLongArrayValue { return object : TypedLongArrayValue() { private var internalValue: Array = default @@ -318,6 +337,7 @@ internal fun Setting.valueImpl(default: Array): TypedLongArrayValue { override val serializer = ArraySerializer(Long.serializer()) } } + internal fun Setting.valueImpl(default: Array): TypedFloatArrayValue { return object : TypedFloatArrayValue() { private var internalValue: Array = default @@ -332,6 +352,7 @@ internal fun Setting.valueImpl(default: Array): TypedFloatArrayValue { override val serializer = ArraySerializer(Float.serializer()) } } + internal fun Setting.valueImpl(default: Array): TypedDoubleArrayValue { return object : TypedDoubleArrayValue() { private var internalValue: Array = default @@ -346,6 +367,7 @@ internal fun Setting.valueImpl(default: Array): TypedDoubleArrayValue { override val serializer = ArraySerializer(Double.serializer()) } } + internal fun Setting.valueImpl(default: Array): TypedBooleanArrayValue { return object : TypedBooleanArrayValue() { private var internalValue: Array = default @@ -360,6 +382,7 @@ internal fun Setting.valueImpl(default: Array): TypedBooleanArrayValue override val serializer = ArraySerializer(Boolean.serializer()) } } + internal fun Setting.valueImpl(default: Array): TypedCharArrayValue { return object : TypedCharArrayValue() { private var internalValue: Array = default @@ -374,6 +397,7 @@ internal fun Setting.valueImpl(default: Array): TypedCharArrayValue { override val serializer = ArraySerializer(Char.serializer()) } } + internal fun Setting.valueImpl(default: Array): TypedStringArrayValue { return object : TypedStringArrayValue() { private var internalValue: Array = default @@ -388,6 +412,7 @@ internal fun Setting.valueImpl(default: Array): TypedStringArrayValue { override val serializer = ArraySerializer(String.serializer()) } } + internal fun Setting.valueImpl(default: List): IntListValue { return object : IntListValue() { private var internalValue: List = default @@ -402,6 +427,7 @@ internal fun Setting.valueImpl(default: List): IntListValue { override val serializer = ListSerializer(Int.serializer()) } } + internal fun Setting.valueImpl(default: List): ShortListValue { return object : ShortListValue() { private var internalValue: List = default @@ -416,6 +442,7 @@ internal fun Setting.valueImpl(default: List): ShortListValue { override val serializer = ListSerializer(Short.serializer()) } } + internal fun Setting.valueImpl(default: List): ByteListValue { return object : ByteListValue() { private var internalValue: List = default @@ -430,6 +457,7 @@ internal fun Setting.valueImpl(default: List): ByteListValue { override val serializer = ListSerializer(Byte.serializer()) } } + internal fun Setting.valueImpl(default: List): LongListValue { return object : LongListValue() { private var internalValue: List = default @@ -444,6 +472,7 @@ internal fun Setting.valueImpl(default: List): LongListValue { override val serializer = ListSerializer(Long.serializer()) } } + internal fun Setting.valueImpl(default: List): FloatListValue { return object : FloatListValue() { private var internalValue: List = default @@ -458,6 +487,7 @@ internal fun Setting.valueImpl(default: List): FloatListValue { override val serializer = ListSerializer(Float.serializer()) } } + internal fun Setting.valueImpl(default: List): DoubleListValue { return object : DoubleListValue() { private var internalValue: List = default @@ -472,6 +502,7 @@ internal fun Setting.valueImpl(default: List): DoubleListValue { override val serializer = ListSerializer(Double.serializer()) } } + internal fun Setting.valueImpl(default: List): BooleanListValue { return object : BooleanListValue() { private var internalValue: List = default @@ -486,6 +517,7 @@ internal fun Setting.valueImpl(default: List): BooleanListValue { override val serializer = ListSerializer(Boolean.serializer()) } } + internal fun Setting.valueImpl(default: List): CharListValue { return object : CharListValue() { private var internalValue: List = default @@ -500,6 +532,7 @@ internal fun Setting.valueImpl(default: List): CharListValue { override val serializer = ListSerializer(Char.serializer()) } } + internal fun Setting.valueImpl(default: List): StringListValue { return object : StringListValue() { private var internalValue: List = default @@ -650,6 +683,7 @@ internal fun Setting.valueImpl(default: Set): StringSetValue { } } + @JvmName("valueImplMutableIntList") internal fun Setting.valueImpl( default: MutableList @@ -1281,3 +1315,30 @@ internal fun Setting.valueImpl( } +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) + } + } + } +} +