From ba3fe945f737488a573590dd79a5600113240658 Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 9 Sep 2020 23:22:34 +0800 Subject: [PATCH] Fix PermissionService config saving --- .../data/MultiFilePluginDataStorageImpl.kt | 25 ++++++++++++++----- .../permission/BuiltInPermissionServices.kt | 13 +++++----- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/MultiFilePluginDataStorageImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/MultiFilePluginDataStorageImpl.kt index c0c917b56..28bd99f1a 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/MultiFilePluginDataStorageImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/MultiFilePluginDataStorageImpl.kt @@ -9,15 +9,19 @@ 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.permission.ExperimentalPermission import net.mamoe.mirai.console.util.ConsoleExperimentalAPI import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.SilentLogger import net.mamoe.mirai.utils.debug import net.mamoe.mirai.utils.warning import net.mamoe.yamlkt.Yaml -import net.mamoe.yamlkt.YamlConfiguration import java.io.File import java.nio.file.Path @@ -62,16 +66,25 @@ internal open class MultiFilePluginDataStorageImpl( @ConsoleExperimentalAPI 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( mapSerialization = YamlConfiguration.MapSerialization.FLOW_MAP, listSerialization = YamlConfiguration.ListSerialization.FLOW_SEQUENCE, classSerialization = YamlConfiguration.MapSerialization.FLOW_MAP ) - ) else Yaml.default - + )*/ else Yaml.default 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)" } } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt index 61b7c9bd3..e0c6f26ac 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt @@ -10,7 +10,6 @@ package net.mamoe.mirai.console.permission 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.value import net.mamoe.mirai.console.data.valueFromKType @@ -105,17 +104,17 @@ internal object BuiltInPermissionService : AbstractConcurrentPermissionService

private constructor( permissionType: KType, public override val saveName: String, - delegate: PluginConfig, + // delegate: PluginConfig, @Suppress("UNUSED_PARAMETER") primaryConstructorMark: Any? - ) : PluginConfig by delegate { + ) : AutoSavePluginConfig() { public val permissions: MutableMap by valueFromKType>( MutableMap::class.createType( @@ -136,8 +135,8 @@ internal object BuiltInPermissionService : AbstractConcurrentPermissionService

invoke( permissionType: KType, saveName: String, - delegate: PluginConfig, - ): ConcurrentSaveData

= ConcurrentSaveData(permissionType, saveName, delegate, null) + // delegate: PluginConfig, + ): ConcurrentSaveData

= ConcurrentSaveData(permissionType, saveName, null) } } } \ No newline at end of file