Skip to content

Commit

Permalink
Update Config.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
MEFRREEX authored May 10, 2023
1 parent 0c4418c commit 2b8ba6f
Showing 1 changed file with 2 additions and 34 deletions.
36 changes: 2 additions & 34 deletions src/main/kotlin/com/mefrreex/vkbot/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,43 +53,11 @@ class Config(private val file: File) {
}

fun set(key: String, value: Any) {
val keys = key.split("\\.".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
var current = config
for (i in 0 until keys.size - 1) {
val next = current!![keys[i]] as MutableMap<String, Any>?
if (next == null) {
val newMap = mutableMapOf<String, Any>()
current[keys[i]] = newMap
current = newMap
} else {
current = next
}
}
current!![keys[keys.size - 1]] = value
// TODO
}

fun save() {
val content = yaml!!.dump(config)
val lines = mutableListOf<String>()

// Считываем старое содержимое файла
if (file.exists()) {
file.forEachLine { line ->
lines.add(line)
}
}

// Обновляем содержимое файла
val writer = file.bufferedWriter()
for (line in lines) {
if (line.trim().startsWith("#")) {
writer.write(line)
writer.newLine()
}
}
writer.write(content)
writer.flush()
writer.close()
// TODO
}

}

0 comments on commit 2b8ba6f

Please sign in to comment.