aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/events/NeuRepositoryReloadEvent.kt
blob: 9feb170c1f08804226f1f96b35d39860fa87b151 (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
package at.hannibal2.skyhanni.events

import at.hannibal2.skyhanni.config.ConfigManager
import at.hannibal2.skyhanni.data.repo.RepoError
import at.hannibal2.skyhanni.data.repo.RepoUtils
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.NEUItems.manager
import at.hannibal2.skyhanni.utils.json.fromJson
import at.hannibal2.skyhanni.utils.json.getJson
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonSyntaxException
import java.io.File
import java.lang.reflect.Type

class NeuRepositoryReloadEvent : LorenzEvent() {
    fun getConstant(file: String): JsonObject? = File(manager.repoLocation, "constants/$file.json").getJson()

    inline fun <reified T : Any> readConstant(file: String, gson: Gson = ConfigManager.gson): T {
        val data = getConstant(file) ?: ErrorManager.skyHanniError("$file failed to load from neu repo!")
        return try {
            gson.fromJson<T>(data)
        } catch (e: JsonSyntaxException) {
            ErrorManager.logErrorWithData(
                e, "$file failed to read from neu repo!",
                "data" to data,
            )
            throw e
        }
    }

    inline fun <reified T : Any> getConstant(constant: String, type: Type? = null, gson: Gson = ConfigManager.gson): T = try {
        if (!manager.repoLocation.exists()) throw RepoError("NEU-Repo folder does not exist!")
        RepoUtils.getConstant(manager.repoLocation, constant, gson, T::class.java, type)
    } catch (e: Exception) {
        throw RepoError("Repo parsing error while trying to read constant '$constant'", e)
    }
}