From f048890c541491e16d6d961c93afd89063a47a8d Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 20 Aug 2020 14:30:29 +0800 Subject: [PATCH] Fix MultiFileSettingStorage --- .../internal/setting/SettingStorage internal.kt | 16 +++++++--------- .../mirai/console/setting/SettingStorage.kt | 2 +- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/setting/SettingStorage internal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/setting/SettingStorage internal.kt index 4e73755a0..5987eae66 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/setting/SettingStorage internal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/setting/SettingStorage internal.kt @@ -127,15 +127,13 @@ internal open class MultiFileSettingStorageImpl( ) : SettingStorage, MultiFileSettingStorage { public override fun load(holder: SettingHolder, settingClass: Class): T = with(settingClass.kotlin) { - val file = getSettingFile(holder, settingClass::class) + val file = getSettingFile(holder, this) @Suppress("UNCHECKED_CAST") val instance = objectInstance ?: this.createInstanceOrNull() ?: kotlin.run { - if (settingClass != Setting::class.java) { - throw IllegalArgumentException( - "Cannot create Setting instance. Make sure settingClass is Setting::class.java or a Kotlin's object, " + - "or has a constructor which either has no parameters or all parameters of which are optional" - ) + require(settingClass == Setting::class.java) { + "Cannot create Setting instance. Make sure settingClass is Setting::class.java or a Kotlin's object, " + + "or has a constructor which either has no parameters or all parameters of which are optional" } if (holder is AutoSaveSettingHolder) { AutoSaveSetting(holder) as T? @@ -144,9 +142,9 @@ internal open class MultiFileSettingStorageImpl( "Cannot create Setting instance. Make sure 'holder' is a AutoSaveSettingHolder, " + "or 'setting' is an object or has a constructor which either has no parameters or all parameters of which are optional" ) - if (file.exists() && file.isFile && file.canRead()) { - Yaml.default.decodeFromString(instance.updaterSerializer, file.readText()) - } + file.createNewFile() + check(file.exists() && file.isFile && file.canRead()) { "${file.absolutePath} cannot be read" } + Yaml.default.decodeFromString(instance.updaterSerializer, file.readText()) instance } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/SettingStorage.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/SettingStorage.kt index acea390a2..65306eb3f 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/SettingStorage.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/SettingStorage.kt @@ -96,7 +96,7 @@ public interface MemorySettingStorage : SettingStorage, Map, } /** - * 在内存存储所有 [Setting] 实例的 [SettingStorage]. + * 用多个文件存储 [Setting] 实例的 [SettingStorage]. */ public interface MultiFileSettingStorage : SettingStorage { /**