aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
blob: 66a9f67c6faa1ca0c257cc01f02bc6cca0b02986 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package at.hannibal2.skyhanni.config

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.ConfigLoadEvent
import com.google.gson.GsonBuilder
import java.io.*
import java.nio.charset.StandardCharsets

class ConfigManager(val mod: SkyHanniMod) {
    companion object {
        val gson = GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create()
    }

    var configDirectory = File("config/skyhanni")
    private var configFile: File? = null


    fun firstLoad() {
        try {
            configDirectory.mkdir()
        } catch (ignored: Exception) {
        }

        configFile = File(configDirectory, "config.json")

        if (configFile!!.exists()) {
            try {
                BufferedReader(InputStreamReader(FileInputStream(configFile!!), StandardCharsets.UTF_8)).use { reader ->
                    SkyHanniMod.feature = gson.fromJson(reader,
                        Features::class.java)
                }
                ConfigLoadEvent().postAndCatch()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        if (SkyHanniMod.feature == null) {
            SkyHanniMod.feature = Features()
            saveConfig()
        }
    }

    fun saveConfig() {
        try {
            configFile!!.createNewFile()
            BufferedWriter(OutputStreamWriter(FileOutputStream(configFile!!), StandardCharsets.UTF_8)).use { writer ->
                writer.write(gson.toJson(SkyHanniMod.feature))
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
}