diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/JPluginConfig.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/JPluginConfig.kt new file mode 100644 index 000000000..917a59843 --- /dev/null +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/JPluginConfig.kt @@ -0,0 +1,34 @@ +/* + * 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.data + +/** + * 一个插件的配置数据, 用于和用户交互. + * + * 用户可通过 mirai-console 前端 (如在 Android 中可视化实现) 修改这些配置, 修改会自动写入这个对象中. + * + * **提示**: + * 插件内部的数据应用 [PluginData] 存储, 而不能使用 [PluginConfig]. + * + * ### 实现 + * + * 在 [JPluginData] 的示例基础上, 修改类定义 + * ```java + * // 原 + * public class AccountPluginData extends JPluginData { + * // 修改为 + * public class AccountPluginConfig extends JPluginConfig { + * ``` + * 即可将一个 [PluginData] 变更为 [PluginConfig]. + * + * @see JPluginData + * @see PluginConfig + */ +public abstract class JPluginConfig(delegate: PluginData) : JPluginData(delegate), PluginConfig \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginConfig.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginConfig.kt index 8ba7ba9cc..6ab72da7d 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginConfig.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginConfig.kt @@ -19,6 +19,24 @@ import kotlin.annotation.AnnotationTarget.* * * **提示**: * 插件内部的数据应用 [PluginData] 存储, 而不能使用 [PluginConfig]. + * + * ## 实现 + * 对使用者来说, [PluginConfig] 与 [PluginData] 实现几乎相同. 目前仅需在 [PluginData] 使用的基础上添加接口实现即可. + * + * ### Kotlin + * 在 [PluginData] 的示例基础上, 修改对象定义 + * ``` + * // 原 + * object MyPluginData : PluginData by PluginMain.loadPluginData() + * // 修改为 + * object MyPluginConfig : PluginConfig by PluginMain.loadPluginConfig() + * ``` + * 即可将一个 [PluginData] 变更为 [PluginConfig]. + * + * ### Java + * 见 [JPluginConfig] + * + * @see PluginData */ @ExperimentalPluginConfig public interface PluginConfig : PluginData diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginData.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginData.kt index 86dbfe8c5..4de85b27b 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginData.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginData.kt @@ -47,10 +47,11 @@ import kotlin.reflect.KType * ``` * object PluginMain : KotlinPlugin() * - * object AccountPluginData : PluginData by PluginMain.loadPluginData() { + * object MyPluginData : PluginData by PluginMain.loadPluginData() { * val list: MutableList by value(mutableListOf("a", "b")) // mutableListOf("a", "b") 是初始值, 可以省略 * val custom: Map by value() // 使用 kotlinx-serialization 序列化的类型. (目前还不支持) - * var custom2: CustomData by value() // 允许 var + * var long: Long by value(0) // 允许 var + * var int by value(0) // 可以使用类型推断, 但更推荐使用 `var long: Long by value(0)` 这种定义方式. * } * * @Serializable diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/valueFromKTypeImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/valueFromKTypeImpl.kt index a4dd1d8a1..b9463a6b4 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/valueFromKTypeImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/valueFromKTypeImpl.kt @@ -16,9 +16,9 @@ import net.mamoe.mirai.console.data.PluginData import net.mamoe.mirai.console.data.SerializableValue.Companion.serializableValueWith import net.mamoe.mirai.console.data.SerializerAwareValue import net.mamoe.mirai.console.data.valueFromKType +import net.mamoe.mirai.console.internal.command.qualifiedNameOrTip import kotlin.reflect.KClass import kotlin.reflect.KType -import kotlin.reflect.full.createInstance as createInstanceKotlin private val primitiveCollectionsImplemented by lazy { false @@ -101,24 +101,36 @@ internal fun PluginData.valueFromKTypeImpl(type: KType): SerializerAwareValue<*> internal fun KClass<*>.createInstanceSmart(): Any? { return when (this) { + Byte::class -> 0.toByte() + Short::class -> 0.toShort() + Int::class -> 0 + Long::class -> 0L + Float::class -> 0.toFloat() + Double::class -> 0.0 + + Boolean::class -> false + + String::class -> "" + MutableMap::class, Map::class, LinkedHashMap::class, HashMap::class - -> mutableMapOf() + -> LinkedHashMap() MutableList::class, List::class, ArrayList::class - -> mutableListOf() + -> ArrayList() MutableSet::class, Set::class, LinkedHashSet::class, HashSet::class - -> mutableSetOf() + -> LinkedHashSet() - else -> createInstanceKotlin() + else -> createInstanceOrNull() + ?: error("Cannot create instance or find a initial value for ${this.qualifiedNameOrTip}") } }