blob: 58e0ac0bff83b3af83ce95307de0bb9ab48af632 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package com.examplemod.config
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.json.Json
import java.io.File
import kotlinx.serialization.encodeToString
import kotlinx.serialization.decodeFromString
@Serializable
data class PersistentData(
var exampleData: Map<String, String> = mapOf("key1" to "entry1"),
var moreExampleData: Int = 5
) {
fun save() {
configFile.writeText(Json.encodeToString(this))
}
companion object {
private val configFile: File = File(ExampleMod.configDirectory,"data.json")
fun load(): PersistentData {
val data = if (!configFile.exists()) {
configFile.createNewFile()
PersistentData()
} else configFile.runCatching {
Json.decodeFromString<PersistentData>(this.readText())
}.getOrNull() ?: PersistentData()
return data.apply {
this.save()
}
}
}
}
|