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 d7f912fb5..d518c32cd 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 @@ -41,12 +41,13 @@ private val DO_NOT_MODIFY = """ """.trimIndent() private val PACKAGE = """ -package net.mamoe.mirai.console.setting +package net.mamoe.mirai.console.setting.internal """.trimIndent() private val IMPORTS = """ import kotlinx.serialization.* import kotlinx.serialization.builtins.* +import net.mamoe.mirai.console.setting.* """.trimIndent() fun genAllValueImpl(): String = buildString { @@ -56,20 +57,20 @@ fun genAllValueImpl(): String = buildString { // PRIMITIVE for (number in NUMBERS + OTHER_PRIMITIVES) { - appendln(genValueImpl(number, number, "$number.serializer()", false)) + appendln(genPrimitiveValueImpl(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(genPrimitiveValueImpl("${number}Array", "${number}Array", "${number}ArraySerializer()", true)) appendln() } // TYPED ARRAYS for (number in NUMBERS + OTHER_PRIMITIVES) { appendln( - genValueImpl( + genPrimitiveValueImpl( "Array<${number}>", "Typed${number}Array", "ArraySerializer(${number}.serializer())", @@ -83,7 +84,8 @@ fun genAllValueImpl(): String = buildString { for (collectionName in listOf("List", "Set")) { for (number in NUMBERS + OTHER_PRIMITIVES) { appendln( - genValueImpl( + genCollectionValueImpl( + collectionName, "${collectionName}<${number}>", "${number}${collectionName}", "${collectionName}Serializer(${number}.serializer())", @@ -178,7 +180,12 @@ fun genAllValueImpl(): String = buildString { ) } -fun genValueImpl(kotlinTypeName: String, miraiValueName: String, serializer: String, isArray: Boolean): String = +fun genPrimitiveValueImpl( + kotlinTypeName: String, + miraiValueName: String, + serializer: String, + isArray: Boolean +): String = """ internal fun Setting.valueImpl(default: ${kotlinTypeName}): ${miraiValueName}Value { return object : ${miraiValueName}Value() { @@ -206,3 +213,38 @@ fun genValueImpl(kotlinTypeName: String, miraiValueName: String, serializer: Str } """.trimIndent() + "\n" + +fun genCollectionValueImpl( + collectionName: String, + kotlinTypeName: String, + miraiValueName: String, + serializer: String, + isArray: Boolean +): String = + """ + internal fun Setting.valueImpl(default: ${kotlinTypeName}): ${miraiValueName}Value { + var internalValue: $kotlinTypeName = default + return object : ${miraiValueName}Value(), $kotlinTypeName by dynamic$collectionName({ internalValue }) { + override var value: $kotlinTypeName + get() = internalValue + set(new) { + ${ + if (isArray) """ + if (!new.contentEquals(internalValue)) { + internalValue = new + onElementChanged(this) + } + """.trim() + else """ + if (new != internalValue) { + internalValue = new + onElementChanged(this) + } + """.trim() + } + } + override val serializer = $serializer + } + } + """.trimIndent() + "\n" + 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 35cdf17b0..4da6e25c1 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 @@ -157,13 +157,14 @@ sealed class Value : ReadWriteProperty { appendln() for (number in (NUMBERS + OTHER_PRIMITIVES).filterNot { it == "String" }) { - val template = """ + appendln( + """ abstract class ${number}ArrayValue internal constructor() : PrimitiveArrayValue<${number}Array>(), Iterable<${number}> { override fun iterator(): Iterator<${number}> = this.value.iterator() } """ - - appendln(template) + ) + appendln() } appendln() @@ -180,11 +181,11 @@ sealed class Value : ReadWriteProperty { appendln() for (number in (NUMBERS + OTHER_PRIMITIVES)) { - val template = """ + appendln( + """ abstract class Typed${number}ArrayValue internal constructor() : TypedPrimitiveArrayValue<${number}>() """ - - appendln(template) + ) } appendln() @@ -194,9 +195,7 @@ sealed class Value : ReadWriteProperty { appendln( """ - sealed class ${collectionName}Value : Value<${collectionName}>(), Iterable{ - override fun iterator() = this.value.iterator() - } + sealed class ${collectionName}Value : Value<${collectionName}>(), ${collectionName} """ ) 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 1aa6600b5..6dc6f62be 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 @@ -14,7 +14,7 @@ import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty /** - * !!! These primitive types are auto-generated by backend/codegen/src/main/kotlin/net.mamoe.mirai.console.codegen.ValuesCodegen.kt + * !!! 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 */ @@ -62,28 +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 { override fun iterator() = this.value.iterator() } @@ -98,10 +106,7 @@ abstract class TypedBooleanArrayValue internal constructor() : TypedPrimitiveArr abstract class TypedCharArrayValue internal constructor() : TypedPrimitiveArrayValue() abstract class TypedStringArrayValue internal constructor() : TypedPrimitiveArrayValue() -sealed class ListValue : Value>(), Iterable { - override fun iterator() = this.value.iterator() -} - +sealed class ListValue : Value>(), List abstract class IntListValue internal constructor() : ListValue() abstract class ShortListValue internal constructor() : ListValue() abstract class ByteListValue internal constructor() : ListValue() @@ -114,10 +119,7 @@ abstract class StringListValue internal constructor() : ListValue() abstract class SettingListValue internal constructor() : Value>(), List -sealed class SetValue : Value>(), Iterable { - override fun iterator() = this.value.iterator() -} - +sealed class SetValue : Value>(), Set abstract class IntSetValue internal constructor() : SetValue() abstract class ShortSetValue internal constructor() : SetValue() abstract class ByteSetValue internal constructor() : SetValue() 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 index ded01b9ef..983ee212e 100644 --- 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 @@ -414,8 +414,8 @@ internal fun Setting.valueImpl(default: Array): TypedStringArrayValue { } internal fun Setting.valueImpl(default: List): IntListValue { - return object : IntListValue() { - private var internalValue: List = default + var internalValue: List = default + return object : IntListValue(), List by dynamicList({ internalValue }) { override var value: List get() = internalValue set(new) { @@ -429,8 +429,8 @@ internal fun Setting.valueImpl(default: List): IntListValue { } internal fun Setting.valueImpl(default: List): ShortListValue { - return object : ShortListValue() { - private var internalValue: List = default + var internalValue: List = default + return object : ShortListValue(), List by dynamicList({ internalValue }) { override var value: List get() = internalValue set(new) { @@ -444,8 +444,8 @@ internal fun Setting.valueImpl(default: List): ShortListValue { } internal fun Setting.valueImpl(default: List): ByteListValue { - return object : ByteListValue() { - private var internalValue: List = default + var internalValue: List = default + return object : ByteListValue(), List by dynamicList({ internalValue }) { override var value: List get() = internalValue set(new) { @@ -459,8 +459,8 @@ internal fun Setting.valueImpl(default: List): ByteListValue { } internal fun Setting.valueImpl(default: List): LongListValue { - return object : LongListValue() { - private var internalValue: List = default + var internalValue: List = default + return object : LongListValue(), List by dynamicList({ internalValue }) { override var value: List get() = internalValue set(new) { @@ -474,8 +474,8 @@ internal fun Setting.valueImpl(default: List): LongListValue { } internal fun Setting.valueImpl(default: List): FloatListValue { - return object : FloatListValue() { - private var internalValue: List = default + var internalValue: List = default + return object : FloatListValue(), List by dynamicList({ internalValue }) { override var value: List get() = internalValue set(new) { @@ -489,8 +489,8 @@ internal fun Setting.valueImpl(default: List): FloatListValue { } internal fun Setting.valueImpl(default: List): DoubleListValue { - return object : DoubleListValue() { - private var internalValue: List = default + var internalValue: List = default + return object : DoubleListValue(), List by dynamicList({ internalValue }) { override var value: List get() = internalValue set(new) { @@ -504,8 +504,8 @@ internal fun Setting.valueImpl(default: List): DoubleListValue { } internal fun Setting.valueImpl(default: List): BooleanListValue { - return object : BooleanListValue() { - private var internalValue: List = default + var internalValue: List = default + return object : BooleanListValue(), List by dynamicList({ internalValue }) { override var value: List get() = internalValue set(new) { @@ -519,8 +519,8 @@ internal fun Setting.valueImpl(default: List): BooleanListValue { } internal fun Setting.valueImpl(default: List): CharListValue { - return object : CharListValue() { - private var internalValue: List = default + var internalValue: List = default + return object : CharListValue(), List by dynamicList({ internalValue }) { override var value: List get() = internalValue set(new) { @@ -534,8 +534,8 @@ internal fun Setting.valueImpl(default: List): CharListValue { } internal fun Setting.valueImpl(default: List): StringListValue { - return object : StringListValue() { - private var internalValue: List = default + var internalValue: List = default + return object : StringListValue(), List by dynamicList({ internalValue }) { override var value: List get() = internalValue set(new) { @@ -549,8 +549,8 @@ internal fun Setting.valueImpl(default: List): StringListValue { } internal fun Setting.valueImpl(default: Set): IntSetValue { - return object : IntSetValue() { - private var internalValue: Set = default + var internalValue: Set = default + return object : IntSetValue(), Set by dynamicSet({ internalValue }) { override var value: Set get() = internalValue set(new) { @@ -564,8 +564,8 @@ internal fun Setting.valueImpl(default: Set): IntSetValue { } internal fun Setting.valueImpl(default: Set): ShortSetValue { - return object : ShortSetValue() { - private var internalValue: Set = default + var internalValue: Set = default + return object : ShortSetValue(), Set by dynamicSet({ internalValue }) { override var value: Set get() = internalValue set(new) { @@ -579,8 +579,8 @@ internal fun Setting.valueImpl(default: Set): ShortSetValue { } internal fun Setting.valueImpl(default: Set): ByteSetValue { - return object : ByteSetValue() { - private var internalValue: Set = default + var internalValue: Set = default + return object : ByteSetValue(), Set by dynamicSet({ internalValue }) { override var value: Set get() = internalValue set(new) { @@ -594,8 +594,8 @@ internal fun Setting.valueImpl(default: Set): ByteSetValue { } internal fun Setting.valueImpl(default: Set): LongSetValue { - return object : LongSetValue() { - private var internalValue: Set = default + var internalValue: Set = default + return object : LongSetValue(), Set by dynamicSet({ internalValue }) { override var value: Set get() = internalValue set(new) { @@ -609,8 +609,8 @@ internal fun Setting.valueImpl(default: Set): LongSetValue { } internal fun Setting.valueImpl(default: Set): FloatSetValue { - return object : FloatSetValue() { - private var internalValue: Set = default + var internalValue: Set = default + return object : FloatSetValue(), Set by dynamicSet({ internalValue }) { override var value: Set get() = internalValue set(new) { @@ -624,8 +624,8 @@ internal fun Setting.valueImpl(default: Set): FloatSetValue { } internal fun Setting.valueImpl(default: Set): DoubleSetValue { - return object : DoubleSetValue() { - private var internalValue: Set = default + var internalValue: Set = default + return object : DoubleSetValue(), Set by dynamicSet({ internalValue }) { override var value: Set get() = internalValue set(new) { @@ -639,8 +639,8 @@ internal fun Setting.valueImpl(default: Set): DoubleSetValue { } internal fun Setting.valueImpl(default: Set): BooleanSetValue { - return object : BooleanSetValue() { - private var internalValue: Set = default + var internalValue: Set = default + return object : BooleanSetValue(), Set by dynamicSet({ internalValue }) { override var value: Set get() = internalValue set(new) { @@ -654,8 +654,8 @@ internal fun Setting.valueImpl(default: Set): BooleanSetValue { } internal fun Setting.valueImpl(default: Set): CharSetValue { - return object : CharSetValue() { - private var internalValue: Set = default + var internalValue: Set = default + return object : CharSetValue(), Set by dynamicSet({ internalValue }) { override var value: Set get() = internalValue set(new) { @@ -669,8 +669,8 @@ internal fun Setting.valueImpl(default: Set): CharSetValue { } internal fun Setting.valueImpl(default: Set): StringSetValue { - return object : StringSetValue() { - private var internalValue: Set = default + var internalValue: Set = default + return object : StringSetValue(), Set by dynamicSet({ internalValue }) { override var value: Set get() = internalValue set(new) { @@ -1354,10 +1354,7 @@ internal fun Setting.valueImpl(default: T): Value { } override fun serialize(encoder: Encoder, value: T) { - internalValue.updaterSerializer.serialize( - encoder, - SettingSerializerMark - ) + internalValue.updaterSerializer.serialize(encoder, SettingSerializerMark) } } }