diff --git a/mirai-console/build.gradle.kts b/mirai-console/build.gradle.kts index 1ce7990b8..fd393c5dc 100644 --- a/mirai-console/build.gradle.kts +++ b/mirai-console/build.gradle.kts @@ -40,5 +40,7 @@ dependencies { runtimeOnly(files("../mirai-core/build/classes/kotlin/jvm/main")) api(kotlin("serialization")) api(group = "com.alibaba", name = "fastjson", version = "1.2.62") + api(group = "org.yaml", name = "snakeyaml", version = "1.25") + api(group = "org.ini4j", name = "ini4j", version = "0.5.4") // classpath is not set correctly by IDE } \ No newline at end of file diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/plugins/ConfigSection.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/plugins/ConfigSection.kt index 5c6b26c66..6d8a8c86f 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/plugins/ConfigSection.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/plugins/ConfigSection.kt @@ -14,7 +14,10 @@ import com.alibaba.fastjson.JSONObject import com.alibaba.fastjson.TypeReference import com.alibaba.fastjson.parser.Feature import kotlinx.serialization.* +import org.ini4j.Wini +import org.yaml.snakeyaml.Yaml import java.io.File +import java.util.LinkedHashMap import java.util.concurrent.ConcurrentHashMap import kotlin.properties.ReadWriteProperty import kotlin.reflect.KClass @@ -191,7 +194,6 @@ fun Config._smartCast(propertyName: String, _class: KClass): T { } - interface ConfigSection : Config { override fun getConfigSection(key: String): ConfigSection { return (get(key) ?: error("ConfigSection does not contain $key ")) as ConfigSection @@ -339,22 +341,48 @@ class JsonConfig internal constructor(file: File) : FileConfigImpl(file) { class YamlConfig internal constructor(file: File) : FileConfigImpl(file) { override fun deserialize(content: String): ConfigSection { - TODO("崔崔还没有写") //To change body of created functions use File | Settings | File Templates. + if (content.isEmpty() || content.isBlank()) { + return ConfigSectionImpl() + } + val yamlObj = Yaml().load>(content) + return JSON.parseObject( + JSONObject.toJSONString(yamlObj), + object : TypeReference() {}, + Feature.OrderedField + ) } override fun serialize(config: ConfigSection): String { - TODO("崔崔还没有写") //To change body of created functions use File | Settings | File Templates. + val jsonStr = (JSONObject.toJSONString(config)) + return Yaml().dump(JSON.parseObject(jsonStr)) } } -class IniConfig internal constructor(file: File) : FileConfigImpl(file) { - override fun deserialize(content: String): ConfigSection { - TODO("崔崔还没有写") //To change body of created functions use File | Settings | File Templates. +class IniConfig internal constructor(val file: File) : ConfigSection { + private val iniObj by lazy { + Wini(file) + } + private val rootSection + get() = iniObj["root"] + + override fun asMap(): Map { + return JSON.parseObject( + JSONObject.toJSONString(rootSection), + object : TypeReference() {}, + Feature.OrderedField + ).asMap() } - override fun serialize(config: ConfigSection): String { - TODO("崔崔还没有写") //To change body of created functions use File | Settings | File Templates. + override fun set(key: String, value: Any) { + iniObj.put("root", key, value) } -} + override fun get(key: String): Any? { + return iniObj.get("root", key) + } + + override fun save() { + iniObj.store() + } +} \ No newline at end of file