11package com.algorithmlx.dimore.init.config
22
3+ import com.algorithmlx.dimore.init.post.ExampleBlock
4+ import com.algorithmlx.dimore.init.post.PostBlock
35import kotlinx.serialization.*
46import kotlinx.serialization.json.*
57import java.io.File
@@ -9,7 +11,7 @@ import kotlin.reflect.full.hasAnnotation
911import kotlin.reflect.full.memberProperties
1012
1113@OptIn(ExperimentalSerializationApi ::class )
12- object ConfigManager {
14+ object CommentedJSONManager {
1315 private const val COMMENT_MULTILINE_START = " /*"
1416 private const val COMMENT_MULTILINE_END = " */"
1517 private const val COMMENT_STAR = " *"
@@ -27,32 +29,50 @@ object ConfigManager {
2729 private set
2830
2931 fun load () {
30- val configCache: MutableMap <String , Any > = mutableMapOf ()
31-
3232 val configFile = File (" config/dimore/dimore.json" )
33- if (! configFile.exists()) {
34- configFile.parentFile.mkdirs()
35- save(configCache, configFile, config, DimensionalOresConfig ::class )
33+ val defaultBlockFile = File (" config/dimore/custom/_example_block.json" )
34+
35+ config = saveOrLoad(configFile, config, DimensionalOresConfig ::class )
36+
37+ if (! defaultBlockFile.parentFile.exists())
38+ saveOrLoad(defaultBlockFile, ExampleBlock , PostBlock ::class )
39+ }
40+
41+ inline fun <reified T : Any > saveOrLoad (file : File , obj : T , clazz : KClass <T >): T {
42+ val cache = mutableMapOf<String , Any >()
43+ return saveOrLoad(cache, file, obj, clazz)
44+ }
45+
46+ inline fun <reified T : Any > saveOrLoad (cache : MutableMap <String , Any >, file : File , obj : T , clazz : KClass <T >): T {
47+ val value = if (! file.exists()) {
48+ file.parentFile.mkdirs()
49+ save(cache, file, obj, clazz)
50+
51+ obj
3652 } else {
3753 try {
38- config = json.decodeFromStream(configFile .inputStream())
54+ json.decodeFromStream(file .inputStream())
3955 } catch (e: Exception ) {
4056 e.printStackTrace()
41- save(configCache, configFile, config, DimensionalOresConfig ::class )
57+ save(cache, file, obj, clazz)
58+
59+ obj
4260 }
4361 }
4462
45- updateCache(configCache, config)
63+ updateCache(cache, obj)
64+
65+ return value
4666 }
4767
48- private inline fun <reified T : Any > save (cache : MutableMap <String , Any >, configFile : File , toSave : T , clazz : KClass <T >) {
68+ inline fun <reified T : Any > save (cache : MutableMap <String , Any >, configFile : File , toSave : T , clazz : KClass <T >) {
4969 val jsonStr = json.encodeToString(toSave)
5070 val commentedStr = injectComments(jsonStr, clazz)
5171 configFile.writeText(commentedStr)
5272 updateCache(cache, toSave)
5373 }
5474
55- private inline fun <reified T > updateCache (cache : MutableMap <String , Any >, config : T ) {
75+ inline fun <reified T > updateCache (cache : MutableMap <String , Any >, config : T ) {
5676 val jsonObject = json.encodeToJsonElement(config).jsonObject
5777 val flat = flatten(jsonObject)
5878 cache.putAll(flat)
@@ -64,7 +84,7 @@ object ConfigManager {
6484 @JvmStatic
6585 fun getInt (cache : Map <String , Any >, key : String ): Int = cache[key] as ? Int ? : 0
6686
67- private fun flatten (el : JsonElement , pr : String = ""): Map <String , Any > {
87+ fun flatten (el : JsonElement , pr : String = ""): Map <String , Any > {
6888 val m = mutableMapOf<String , Any >()
6989 when (el) {
7090 is JsonObject -> {
0 commit comments