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 351ea18ea..45fa24f88 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,6 +9,8 @@ package net.mamoe.mirai.console.internal.data +import kotlinx.serialization.json.Json +import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.console.data.* import net.mamoe.mirai.console.internal.command.qualifiedNameOrTip import net.mamoe.mirai.console.util.ConsoleExperimentalApi @@ -59,23 +61,28 @@ internal open class MultiFilePluginDataStorageImpl( return file.toFile().also { it.createNewFile() } } + private val json = Json { + prettyPrint = true + ignoreUnknownKeys = true + isLenient = true + allowStructuredMapKeys = true + } + + private val yaml = Yaml.default + @ConsoleExperimentalApi public override fun store(holder: PluginDataHolder, instance: PluginData) { - 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 getPluginDataFile(holder, instance).writeText( kotlin.runCatching { yaml.encodeToString(instance.updaterSerializer, Unit) + }.recoverCatching { + // Just use mainLogger for convenience. + MiraiConsole.mainLogger.warning( + "Could not save ${instance.saveName} in YAML format due to exception in YAML encoder. " + + "Please report this exception and relevant configurations to https://github.com/mamoe/mirai-console/issues/new", + it + ) + json.encodeToString(instance.updaterSerializer, Unit) }.getOrElse { throw IllegalStateException("Exception while saving $instance, saveName=${instance.saveName}", it) }