Fix PermissionService config saving

This commit is contained in:
Him188 2020-09-09 23:22:34 +08:00
parent a881235caa
commit ba3fe945f7
2 changed files with 25 additions and 13 deletions

View File

@ -9,15 +9,19 @@
package net.mamoe.mirai.console.internal.data package net.mamoe.mirai.console.internal.data
import net.mamoe.mirai.console.data.* import kotlinx.serialization.json.Json
import net.mamoe.mirai.console.data.MultiFilePluginDataStorage
import net.mamoe.mirai.console.data.PluginData
import net.mamoe.mirai.console.data.PluginDataHolder
import net.mamoe.mirai.console.data.PluginDataStorage
import net.mamoe.mirai.console.internal.command.qualifiedNameOrTip import net.mamoe.mirai.console.internal.command.qualifiedNameOrTip
import net.mamoe.mirai.console.permission.ExperimentalPermission
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.MiraiLogger
import net.mamoe.mirai.utils.SilentLogger import net.mamoe.mirai.utils.SilentLogger
import net.mamoe.mirai.utils.debug import net.mamoe.mirai.utils.debug
import net.mamoe.mirai.utils.warning import net.mamoe.mirai.utils.warning
import net.mamoe.yamlkt.Yaml import net.mamoe.yamlkt.Yaml
import net.mamoe.yamlkt.YamlConfiguration
import java.io.File import java.io.File
import java.nio.file.Path import java.nio.file.Path
@ -62,16 +66,25 @@ internal open class MultiFilePluginDataStorageImpl(
@ConsoleExperimentalAPI @ConsoleExperimentalAPI
public override fun store(holder: PluginDataHolder, instance: PluginData) { public override fun store(holder: PluginDataHolder, instance: PluginData) {
val yaml = if (instance is PluginConfig) Yaml( @OptIn(ExperimentalPermission::class)
val yaml = if (instance.saveName == "PermissionService") Json {
prettyPrint = true
ignoreUnknownKeys = true
isLenient = true
allowStructuredMapKeys = true
} /*Yaml(
configuration = YamlConfiguration( configuration = YamlConfiguration(
mapSerialization = YamlConfiguration.MapSerialization.FLOW_MAP, mapSerialization = YamlConfiguration.MapSerialization.FLOW_MAP,
listSerialization = YamlConfiguration.ListSerialization.FLOW_SEQUENCE, listSerialization = YamlConfiguration.ListSerialization.FLOW_SEQUENCE,
classSerialization = YamlConfiguration.MapSerialization.FLOW_MAP classSerialization = YamlConfiguration.MapSerialization.FLOW_MAP
) )
) else Yaml.default )*/ else Yaml.default
getPluginDataFile(holder, instance).writeText( getPluginDataFile(holder, instance).writeText(
yaml.encodeToString(instance.updaterSerializer, Unit) kotlin.runCatching {
yaml.encodeToString(instance.updaterSerializer, Unit)
}.getOrElse {
throw IllegalStateException("Exception while saving $instance, saveName=${instance.saveName}", it)
}
) )
logger.debug { "Successfully saved PluginData: ${instance.saveName} (containing ${instance.valueNodes.size} properties)" } logger.debug { "Successfully saved PluginData: ${instance.saveName} (containing ${instance.valueNodes.size} properties)" }
} }

View File

@ -10,7 +10,6 @@
package net.mamoe.mirai.console.permission package net.mamoe.mirai.console.permission
import net.mamoe.mirai.console.data.AutoSavePluginConfig import net.mamoe.mirai.console.data.AutoSavePluginConfig
import net.mamoe.mirai.console.data.PluginConfig
import net.mamoe.mirai.console.data.PluginDataExtensions.withDefault import net.mamoe.mirai.console.data.PluginDataExtensions.withDefault
import net.mamoe.mirai.console.data.value import net.mamoe.mirai.console.data.value
import net.mamoe.mirai.console.data.valueFromKType import net.mamoe.mirai.console.data.valueFromKType
@ -105,17 +104,17 @@ internal object BuiltInPermissionService : AbstractConcurrentPermissionService<P
ConcurrentSaveData( ConcurrentSaveData(
PermissionImpl::class.createType(), PermissionImpl::class.createType(),
"PermissionService", "PermissionService",
AutoSavePluginConfig()
) )
@Suppress("RedundantVisibilityModifier") @Suppress("RedundantVisibilityModifier")
@ExperimentalPermission @ExperimentalPermission
internal class ConcurrentSaveData<P : Permission> private constructor( internal class ConcurrentSaveData<P : Permission> private constructor(
permissionType: KType, permissionType: KType,
public override val saveName: String, public override val saveName: String,
delegate: PluginConfig, // delegate: PluginConfig,
@Suppress("UNUSED_PARAMETER") primaryConstructorMark: Any? @Suppress("UNUSED_PARAMETER") primaryConstructorMark: Any?
) : PluginConfig by delegate { ) : AutoSavePluginConfig() {
public val permissions: MutableMap<PermissionId, P> public val permissions: MutableMap<PermissionId, P>
by valueFromKType<MutableMap<PermissionId, P>>( by valueFromKType<MutableMap<PermissionId, P>>(
MutableMap::class.createType( MutableMap::class.createType(
@ -136,8 +135,8 @@ internal object BuiltInPermissionService : AbstractConcurrentPermissionService<P
public operator fun <P : Permission> invoke( public operator fun <P : Permission> invoke(
permissionType: KType, permissionType: KType,
saveName: String, saveName: String,
delegate: PluginConfig, // delegate: PluginConfig,
): ConcurrentSaveData<P> = ConcurrentSaveData(permissionType, saveName, delegate, null) ): ConcurrentSaveData<P> = ConcurrentSaveData(permissionType, saveName, null)
} }
} }
} }