Referential value infrastructure

This commit is contained in:
Him188 2020-05-26 14:40:33 +08:00
parent 0359bcb89c
commit c8dd3fa49c
8 changed files with 77 additions and 15 deletions

View File

@ -101,7 +101,11 @@ fun genAllValueUseSite(): String = buildString {
require(this::class != default::class) {
"Recursive nesting is prohibited"
}
return valueImpl(default)
return valueImpl(default).also {
if (default is Setting.NestedSetting) {
default.attachedValue = it
}
}
}
inline fun <T : Setting> Setting.value(default: T, crossinline initializer: T.() -> Unit): Value<T> =

View File

@ -25,7 +25,7 @@ public class BotManager {
}
public static List<Long> getManagers(Bot bot) {
return BotHelperKt.getBotManagers(bot);
return BotManagers.getManagers(bot);
}
public static boolean isManager(Bot bot, long target) {

View File

@ -17,6 +17,7 @@ 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
@ -75,6 +76,10 @@ object MiraiConsole : CoroutineScope, IMiraiConsole {
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,6 +121,11 @@ internal interface IMiraiConsole : CoroutineScope {
* 内建的供 [JvmPlugin] 使用的 [SettingStorage]
*/
val jvmSettingStorage: SettingStorage
/**
* 内建的供 [ConsoleBuiltInSetting] 使用的 [SettingStorage]
*/
val consoleBuiltIntSettingStorage: SettingStorage
}
/**

View File

@ -36,6 +36,16 @@ typealias Comment = net.mamoe.yamlkt.Comment
@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<Annotation>,

View File

@ -124,7 +124,11 @@ fun <T : Setting> Setting.value(default: T): Value<T> {
require(this::class != default::class) {
"Recursive nesting is prohibited"
}
return valueImpl(default)
return valueImpl(default).also {
if (default is Setting.Inner) {
default.attachedValue = it
}
}
}
inline fun <T : Setting> Setting.value(default: T, crossinline initializer: T.() -> Unit): Value<T> =

View File

@ -0,0 +1,32 @@
/*
* 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()
}
}

View File

@ -17,7 +17,9 @@ 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
@ -291,6 +293,7 @@ internal fun <T : Any> Setting.valueImpl(
/**
* For primitives and serializable only
*/
@OptIn(ExperimentalStdlibApi::class)
@PublishedApi
@LowPriorityInOverloadResolution
internal inline fun <reified T : Any> Setting.valueImpl(default: T): Value<T> =

View File

@ -6,11 +6,21 @@
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:JvmName("BotManagers")
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
/**
@ -29,15 +39,4 @@ fun Bot.removeManager(long: Long) {
}
val Bot.managers: List<Long>
get() {
TODO()
}
fun Bot.checkManager(long: Long): Boolean {
return this.managers.contains(long)
}
fun getBotManagers(bot: Bot): List<Long> {
return bot.managers
}
get() = TODO()