diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt | 193 |
1 files changed, 84 insertions, 109 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt index 65c252bae..b0d5bf9c9 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt @@ -7,7 +7,6 @@ import at.hannibal2.skyhanni.data.jsonobjects.local.FriendsJson import at.hannibal2.skyhanni.data.jsonobjects.local.JacobContestsJson import at.hannibal2.skyhanni.data.jsonobjects.local.KnownFeaturesJson import at.hannibal2.skyhanni.data.jsonobjects.local.VisualWordsJson -import at.hannibal2.skyhanni.data.jsonobjects.other.HypixelApiTrophyFish import at.hannibal2.skyhanni.events.LorenzEvent import at.hannibal2.skyhanni.features.fishing.trophy.TrophyRarity import at.hannibal2.skyhanni.features.misc.update.UpdateManager @@ -21,16 +20,15 @@ import at.hannibal2.skyhanni.utils.LorenzVec import at.hannibal2.skyhanni.utils.NEUInternalName import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName import at.hannibal2.skyhanni.utils.NEUItems -import at.hannibal2.skyhanni.utils.NumberUtil.isInt import at.hannibal2.skyhanni.utils.SimpleTimeMark import at.hannibal2.skyhanni.utils.SimpleTimeMark.Companion.asTimeMark import at.hannibal2.skyhanni.utils.tracker.SkyHanniTracker +import com.google.gson.Gson import com.google.gson.GsonBuilder import com.google.gson.JsonObject import com.google.gson.TypeAdapter import com.google.gson.TypeAdapterFactory import com.google.gson.stream.JsonReader -import com.google.gson.stream.JsonToken import com.google.gson.stream.JsonWriter import io.github.notenoughupdates.moulconfig.annotations.ConfigLink import io.github.notenoughupdates.moulconfig.observer.PropertyTypeAdapterFactory @@ -62,122 +60,99 @@ private fun GsonBuilder.reigsterIfBeta(create: TypeAdapterFactory): GsonBuilder class ConfigManager { companion object { + fun createBaseGsonBuilder(): GsonBuilder { + return GsonBuilder().setPrettyPrinting() + .excludeFieldsWithoutExposeAnnotation() + .serializeSpecialFloatingPointValues() + .registerTypeAdapterFactory(PropertyTypeAdapterFactory()) + .registerTypeAdapterFactory(KotlinTypeAdapterFactory()) + .registerTypeAdapter(UUID::class.java, object : TypeAdapter<UUID>() { + override fun write(out: JsonWriter, value: UUID) { + out.value(value.toString()) + } - val gson = GsonBuilder().setPrettyPrinting() - .excludeFieldsWithoutExposeAnnotation() - .serializeSpecialFloatingPointValues() - .reigsterIfBeta(FeatureTogglesByDefaultAdapter) - .registerTypeAdapterFactory(PropertyTypeAdapterFactory()) - .registerTypeAdapterFactory(KotlinTypeAdapterFactory()) - .registerTypeAdapter(UUID::class.java, object : TypeAdapter<UUID>() { - override fun write(out: JsonWriter, value: UUID) { - out.value(value.toString()) - } - - override fun read(reader: JsonReader): UUID { - return UUID.fromString(reader.nextString()) - } - }.nullSafe()) - .registerTypeAdapter(LorenzVec::class.java, object : TypeAdapter<LorenzVec>() { - override fun write(out: JsonWriter, value: LorenzVec) { - value.run { out.value("$x:$y:$z") } - } + override fun read(reader: JsonReader): UUID { + return UUID.fromString(reader.nextString()) + } + }.nullSafe()) + .registerTypeAdapter(LorenzVec::class.java, object : TypeAdapter<LorenzVec>() { + override fun write(out: JsonWriter, value: LorenzVec) { + value.run { out.value("$x:$y:$z") } + } - override fun read(reader: JsonReader): LorenzVec { - return LorenzVec.decodeFromString(reader.nextString()) - } - }.nullSafe()) - .registerTypeAdapter(TrophyRarity::class.java, object : TypeAdapter<TrophyRarity>() { - override fun write(out: JsonWriter, value: TrophyRarity) { - value.run { out.value(value.name) } - } + override fun read(reader: JsonReader): LorenzVec { + return LorenzVec.decodeFromString(reader.nextString()) + } + }.nullSafe()) + .registerTypeAdapter(TrophyRarity::class.java, object : TypeAdapter<TrophyRarity>() { + override fun write(out: JsonWriter, value: TrophyRarity) { + value.run { out.value(value.name) } + } - override fun read(reader: JsonReader): TrophyRarity { - val text = reader.nextString() - return TrophyRarity.getByName(text) ?: error("Could not parse TrophyRarity from '$text'") - } - }.nullSafe()) - .registerTypeAdapter(ItemStack::class.java, object : TypeAdapter<ItemStack>() { - override fun write(out: JsonWriter, value: ItemStack) { - out.value(NEUItems.saveNBTData(value)) - } + override fun read(reader: JsonReader): TrophyRarity { + val text = reader.nextString() + return TrophyRarity.getByName(text) ?: error("Could not parse TrophyRarity from '$text'") + } + }.nullSafe()) + .registerTypeAdapter(ItemStack::class.java, object : TypeAdapter<ItemStack>() { + override fun write(out: JsonWriter, value: ItemStack) { + out.value(NEUItems.saveNBTData(value)) + } - override fun read(reader: JsonReader): ItemStack { - return NEUItems.loadNBTData(reader.nextString()) - } - }.nullSafe()) - .registerTypeAdapter(NEUInternalName::class.java, object : TypeAdapter<NEUInternalName>() { - override fun write(out: JsonWriter, value: NEUInternalName) { - out.value(value.asString()) - } + override fun read(reader: JsonReader): ItemStack { + return NEUItems.loadNBTData(reader.nextString()) + } + }.nullSafe()) + .registerTypeAdapter(NEUInternalName::class.java, object : TypeAdapter<NEUInternalName>() { + override fun write(out: JsonWriter, value: NEUInternalName) { + out.value(value.asString()) + } - override fun read(reader: JsonReader): NEUInternalName { - return reader.nextString().asInternalName() - } - }.nullSafe()) - .registerTypeAdapter(LorenzRarity::class.java, object : TypeAdapter<LorenzRarity>() { - override fun write(out: JsonWriter, value: LorenzRarity) { - out.value(value.name) - } + override fun read(reader: JsonReader): NEUInternalName { + return reader.nextString().asInternalName() + } + }.nullSafe()) + .registerTypeAdapter(LorenzRarity::class.java, object : TypeAdapter<LorenzRarity>() { + override fun write(out: JsonWriter, value: LorenzRarity) { + out.value(value.name) + } - override fun read(reader: JsonReader): LorenzRarity { - return LorenzRarity.valueOf(reader.nextString().uppercase()) - } - }.nullSafe()) - .registerTypeAdapter(IslandType::class.java, object : TypeAdapter<IslandType>() { - override fun write(out: JsonWriter, value: IslandType) { - out.value(value.name) - } + override fun read(reader: JsonReader): LorenzRarity { + return LorenzRarity.valueOf(reader.nextString().uppercase()) + } + }.nullSafe()) + .registerTypeAdapter(IslandType::class.java, object : TypeAdapter<IslandType>() { + override fun write(out: JsonWriter, value: IslandType) { + out.value(value.name) + } - override fun read(reader: JsonReader): IslandType { - return IslandType.valueOf(reader.nextString().uppercase()) - } - }.nullSafe()) - .registerTypeAdapter(TrackerDisplayMode::class.java, object : TypeAdapter<TrackerDisplayMode>() { - override fun write(out: JsonWriter, value: TrackerDisplayMode) { - out.value(value.name) - } + override fun read(reader: JsonReader): IslandType { + return IslandType.valueOf(reader.nextString().uppercase()) + } + }.nullSafe()) + .registerTypeAdapter(TrackerDisplayMode::class.java, object : TypeAdapter<TrackerDisplayMode>() { + override fun write(out: JsonWriter, value: TrackerDisplayMode) { + out.value(value.name) + } - override fun read(reader: JsonReader): TrackerDisplayMode { - return TrackerDisplayMode.valueOf(reader.nextString()) - } - }.nullSafe()) - .registerTypeAdapter(SimpleTimeMark::class.java, object : TypeAdapter<SimpleTimeMark>() { - override fun write(out: JsonWriter, value: SimpleTimeMark) { - out.value(value.toMillis()) - } + override fun read(reader: JsonReader): TrackerDisplayMode { + return TrackerDisplayMode.valueOf(reader.nextString()) + } + }.nullSafe()) + .registerTypeAdapter(SimpleTimeMark::class.java, object : TypeAdapter<SimpleTimeMark>() { + override fun write(out: JsonWriter, value: SimpleTimeMark) { + out.value(value.toMillis()) + } - override fun read(reader: JsonReader): SimpleTimeMark { - return reader.nextString().toLong().asTimeMark() - } - }.nullSafe()) - .registerTypeAdapter(HypixelApiTrophyFish::class.java, object : TypeAdapter<HypixelApiTrophyFish>() { - override fun write(out: JsonWriter, value: HypixelApiTrophyFish) {} - - override fun read(reader: JsonReader): HypixelApiTrophyFish { - val trophyFish = mutableMapOf<String, Int>() - var totalCaught = 0 - reader.beginObject() - while (reader.hasNext()) { - val key = reader.nextName() - if (key == "total_caught") { - totalCaught = reader.nextInt() - continue - } - if (reader.peek() == JsonToken.NUMBER) { - val valueAsString = reader.nextString() - if (valueAsString.isInt()) { - trophyFish[key] = valueAsString.toInt() - continue - } - } - reader.skipValue() + override fun read(reader: JsonReader): SimpleTimeMark { + return reader.nextString().toLong().asTimeMark() } - reader.endObject() - return HypixelApiTrophyFish(totalCaught, trophyFish) - } - }.nullSafe()) - .enableComplexMapKeySerialization() + }.nullSafe()) + .enableComplexMapKeySerialization() + } + + val gson: Gson = createBaseGsonBuilder() + .reigsterIfBeta(FeatureTogglesByDefaultAdapter) .create() var configDirectory = File("config/skyhanni") |