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
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(
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)" }
}

View File

@ -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,7 +104,7 @@ internal object BuiltInPermissionService : AbstractConcurrentPermissionService<P
ConcurrentSaveData(
PermissionImpl::class.createType(),
"PermissionService",
AutoSavePluginConfig()
)
@Suppress("RedundantVisibilityModifier")
@ -113,9 +112,9 @@ internal object BuiltInPermissionService : AbstractConcurrentPermissionService<P
internal class ConcurrentSaveData<P : Permission> 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<PermissionId, P>
by valueFromKType<MutableMap<PermissionId, P>>(
MutableMap::class.createType(
@ -136,8 +135,8 @@ internal object BuiltInPermissionService : AbstractConcurrentPermissionService<P
public operator fun <P : Permission> invoke(
permissionType: KType,
saveName: String,
delegate: PluginConfig,
): ConcurrentSaveData<P> = ConcurrentSaveData(permissionType, saveName, delegate, null)
// delegate: PluginConfig,
): ConcurrentSaveData<P> = ConcurrentSaveData(permissionType, saveName, null)
}
}
}