Commit 1c6a3800 authored by jiahua.liu's avatar jiahua.liu

Yaml config Supported

parent 04fb98c8
...@@ -267,7 +267,7 @@ object MiraiConsole { ...@@ -267,7 +267,7 @@ object MiraiConsole {
} }
object MiraiProperties { object MiraiProperties {
var config = File("$path/mirai.json").loadAsConfig() var config = File("$path/mirai.yml").loadAsConfig()
var HTTP_API_ENABLE: Boolean by config.withDefaultWrite { true } var HTTP_API_ENABLE: Boolean by config.withDefaultWrite { true }
var HTTP_API_PORT: Int by config.withDefaultWrite { 8080 } var HTTP_API_PORT: Int by config.withDefaultWrite { 8080 }
......
...@@ -17,8 +17,9 @@ import kotlinx.serialization.* ...@@ -17,8 +17,9 @@ import kotlinx.serialization.*
import org.ini4j.Wini import org.ini4j.Wini
import org.yaml.snakeyaml.Yaml import org.yaml.snakeyaml.Yaml
import java.io.File import java.io.File
import java.util.LinkedHashMap import java.util.*
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import kotlin.collections.HashMap
import kotlin.properties.ReadWriteProperty import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KClass import kotlin.reflect.KClass
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
...@@ -194,7 +195,7 @@ fun <T : Any> Config._smartCast(propertyName: String, _class: KClass<T>): T { ...@@ -194,7 +195,7 @@ fun <T : Any> Config._smartCast(propertyName: String, _class: KClass<T>): T {
} }
interface ConfigSection : Config { interface ConfigSection : Config, MutableMap<String, Any> {
override fun getConfigSection(key: String): ConfigSection { override fun getConfigSection(key: String): ConfigSection {
return (get(key) ?: error("ConfigSection does not contain $key ")) as ConfigSection return (get(key) ?: error("ConfigSection does not contain $key ")) as ConfigSection
} }
...@@ -290,6 +291,21 @@ interface FileConfig : Config { ...@@ -290,6 +291,21 @@ interface FileConfig : Config {
fun serialize(config: ConfigSection): String fun serialize(config: ConfigSection): String
} }
open class ConfigSectionDelegation(
val delegation: MutableMap<String, Any>
) : ConfigSection, MutableMap<String, Any> by delegation {
override fun set(key: String, value: Any) {
delegation.put(key, value)
}
override fun asMap(): Map<String, Any> {
return delegation
}
override fun save() {
}
}
abstract class FileConfigImpl internal constructor( abstract class FileConfigImpl internal constructor(
private val file: File private val file: File
...@@ -299,6 +315,27 @@ abstract class FileConfigImpl internal constructor( ...@@ -299,6 +315,27 @@ abstract class FileConfigImpl internal constructor(
deserialize(file.readText()) deserialize(file.readText())
} }
override val size: Int
get() = content.size
override val entries: MutableSet<MutableMap.MutableEntry<String, Any>>
get() = content.entries
override val keys: MutableSet<String>
get() = content.keys
override val values: MutableCollection<Any>
get() = content.values
override fun containsKey(key: String): Boolean = content.containsKey(key)
override fun containsValue(value: Any): Boolean = content.containsValue(value)
override fun put(key: String, value: Any): Any? = content.put(key, value)
override fun isEmpty(): Boolean = content.isEmpty()
override fun putAll(from: Map<out String, Any>) = content.putAll(from)
override fun clear() = content.clear()
override fun remove(key: String): Any? = content.remove(key)
override fun save() { override fun save() {
if (!file.exists()) { if (!file.exists()) {
file.createNewFile() file.createNewFile()
...@@ -320,7 +357,9 @@ abstract class FileConfigImpl internal constructor( ...@@ -320,7 +357,9 @@ abstract class FileConfigImpl internal constructor(
} }
class JsonConfig internal constructor(file: File) : FileConfigImpl(file) { class JsonConfig internal constructor(
file: File
) : FileConfigImpl(file) {
@UnstableDefault @UnstableDefault
override fun deserialize(content: String): ConfigSection { override fun deserialize(content: String): ConfigSection {
if (content.isEmpty() || content.isBlank() || content == "{}") { if (content.isEmpty() || content.isBlank() || content == "{}") {
...@@ -344,22 +383,20 @@ class YamlConfig internal constructor(file: File) : FileConfigImpl(file) { ...@@ -344,22 +383,20 @@ class YamlConfig internal constructor(file: File) : FileConfigImpl(file) {
if (content.isEmpty() || content.isBlank()) { if (content.isEmpty() || content.isBlank()) {
return ConfigSectionImpl() return ConfigSectionImpl()
} }
val yamlObj = Yaml().load<LinkedHashMap<String, Any>>(content) return ConfigSectionDelegation(
return JSON.parseObject<ConfigSectionImpl>( Collections.synchronizedMap(
JSONObject.toJSONString(yamlObj), Yaml().load<LinkedHashMap<String, Any>>(content) as LinkedHashMap<String, Any>
object : TypeReference<ConfigSectionImpl>() {}, )
Feature.OrderedField
) )
} }
override fun serialize(config: ConfigSection): String { override fun serialize(config: ConfigSection): String {
val jsonStr = (JSONObject.toJSONString(config)) return Yaml().dumpAsMap(config)
return Yaml().dump(JSON.parseObject(jsonStr))
} }
} }
class IniConfig internal constructor(val file: File) : ConfigSection { class IniConfig internal constructor(val file: File) : FileConfigImpl(file) {
private val iniObj by lazy { private val iniObj by lazy {
Wini(file) Wini(file)
} }
...@@ -385,4 +422,12 @@ class IniConfig internal constructor(val file: File) : ConfigSection { ...@@ -385,4 +422,12 @@ class IniConfig internal constructor(val file: File) : ConfigSection {
override fun save() { override fun save() {
iniObj.store() iniObj.store()
} }
override fun deserialize(content: String): ConfigSection {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun serialize(config: ConfigSection): String {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment