Skip to content

Commit db2070f

Browse files
committed
Fix type erasure for all collection settings
1 parent ee4e415 commit db2070f

File tree

10 files changed

+45
-28
lines changed

10 files changed

+45
-28
lines changed

common/src/main/kotlin/com/lambda/config/Configurable.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.lambda.util.Nameable
2121
import net.minecraft.block.Block
2222
import net.minecraft.util.math.BlockPos
2323
import java.awt.Color
24+
import java.lang.reflect.Type
2425

2526
/**
2627
* Represents a set of [AbstractSetting]s that are associated with the [name] of the [Configurable].
@@ -163,9 +164,10 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
163164
inline fun <reified T : Any> setting(
164165
name: String,
165166
defaultValue: List<T>,
167+
type: Type,
166168
description: String = "",
167169
noinline visibility: () -> Boolean = { true },
168-
) = ListSetting(name, defaultValue, description, visibility).also {
170+
) = ListSetting(name, defaultValue, type, description, visibility).also {
169171
settings.add(it)
170172
}
171173

@@ -189,9 +191,10 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
189191
inline fun <reified K : Any, V : Any> setting(
190192
name: String,
191193
defaultValue: Map<K, V>,
194+
type: Type,
192195
description: String = "",
193196
noinline visibility: () -> Boolean = { true },
194-
) = MapSetting(name, defaultValue, description, visibility).also {
197+
) = MapSetting(name, defaultValue, type, description, visibility).also {
195198
settings.add(it)
196199
}
197200

@@ -215,9 +218,10 @@ abstract class Configurable(configuration: Configuration) : Jsonable, Nameable {
215218
inline fun <reified T : Any> setting(
216219
name: String,
217220
defaultValue: Set<T>,
221+
type: Type,
218222
description: String = "",
219223
noinline visibility: () -> Boolean = { true },
220-
) = SetSetting(name, defaultValue, description, visibility).also {
224+
) = SetSetting(name, defaultValue, type, description, visibility).also {
221225
settings.add(it)
222226
}
223227

common/src/main/kotlin/com/lambda/config/Configuration.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ abstract class Configuration : Jsonable {
9090
}
9191
.onFailure {
9292
val message = "Failed to load ${configName.capitalize()} config, loading backup"
93-
LOG.error(message)
93+
LOG.error(message, it)
9494
this@Configuration.logError(message)
9595
runCatching { load(backup) }
9696
.onSuccess {

common/src/main/kotlin/com/lambda/config/serializer/gui/TagWindowSerializer.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.lambda.config.serializer.gui
22

33
import com.google.gson.*
44
import com.lambda.gui.impl.clickgui.windows.TagWindow
5+
import com.lambda.module.tag.ModuleTag
56
import com.lambda.util.math.Vec2d
67
import java.lang.reflect.Type
78

@@ -14,6 +15,15 @@ object TagWindowSerializer : JsonSerializer<TagWindow>, JsonDeserializer<TagWind
1415
): JsonElement = src?.let {
1516
JsonObject().apply {
1617
addProperty("title", it.title)
18+
add("tags", JsonArray().apply {
19+
it.tags.forEach { tag ->
20+
add(tag.name)
21+
// add(JsonObject().apply {
22+
// addProperty("name", tag.name)
23+
// addProperty("color", tag.color.rgb)
24+
// })
25+
}
26+
})
1727
addProperty("width", it.width)
1828
addProperty("height", it.height)
1929
addProperty("isOpen", it.isOpen)
@@ -29,9 +39,14 @@ object TagWindowSerializer : JsonSerializer<TagWindow>, JsonDeserializer<TagWind
2939
typeOfT: Type?,
3040
context: JsonDeserializationContext?,
3141
) = json?.asJsonObject?.let {
32-
TagWindow().apply {
33-
width = it["width"].asDouble
42+
TagWindow(
43+
tags = it["tags"].asJsonArray.map { tag ->
44+
ModuleTag(tag.asString)
45+
}.toSet(),
46+
title = it["title"].asString,
47+
width = it["width"].asDouble,
3448
height = it["height"].asDouble
49+
).apply {
3550
isOpen = it["isOpen"].asBoolean
3651
position = Vec2d(
3752
it["position"].asJsonArray[0].asDouble,
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package com.lambda.config.settings.collections
22

33
import com.google.gson.JsonElement
4-
import com.google.gson.reflect.TypeToken
5-
import com.lambda.Lambda.LOG
64
import com.lambda.Lambda.gson
75
import com.lambda.config.AbstractSetting
8-
import com.lambda.util.DynamicReflectionSerializer.dynamicString
6+
import java.lang.reflect.Type
97

10-
class ListSetting<T>(
8+
class ListSetting<T : Any>(
119
override val name: String,
1210
defaultValue: List<T>,
11+
private val type: Type,
1312
description: String,
1413
visibility: () -> Boolean,
1514
) : AbstractSetting<List<T>>(
@@ -18,10 +17,6 @@ class ListSetting<T>(
1817
visibility
1918
) {
2019
override fun loadFromJson(serialized: JsonElement) {
21-
val listType = object : TypeToken<List<T>>() {}.type
22-
LOG.info("Loading $name with value $serialized current value ${value.dynamicString()} $value and type ${listType.typeName}")
23-
val dese = gson.fromJson<List<T>>(serialized, listType)
24-
value = dese
25-
LOG.info("Loaded $name with value ${value.dynamicString()} $value and type ${listType.typeName}")
20+
value = gson.fromJson(serialized, type)
2621
}
2722
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.lambda.config.settings.collections
22

3-
import com.google.common.reflect.TypeToken
43
import com.google.gson.JsonElement
54
import com.lambda.Lambda.gson
65
import com.lambda.config.AbstractSetting
6+
import java.lang.reflect.Type
77

88
class MapSetting<K, V>(
99
override val name: String,
1010
defaultValue: Map<K, V>,
11+
private val type: Type,
1112
description: String,
1213
visibility: () -> Boolean,
1314
) : AbstractSetting<Map<K, V>>(
@@ -16,7 +17,6 @@ class MapSetting<K, V>(
1617
visibility
1718
) {
1819
override fun loadFromJson(serialized: JsonElement) {
19-
val mapType = object : TypeToken<Map<K, V>>() {}.type
20-
value = gson.fromJson(serialized, mapType)
20+
value = gson.fromJson(serialized, type)
2121
}
2222
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.lambda.config.settings.collections
22

33
import com.google.gson.JsonElement
4-
import com.google.gson.reflect.TypeToken
54
import com.lambda.Lambda.gson
65
import com.lambda.config.AbstractSetting
6+
import java.lang.reflect.Type
77

88
class SetSetting<T : Any>(
99
override val name: String,
1010
defaultValue: Set<T>,
11+
private val type: Type,
1112
description: String,
1213
visibility: () -> Boolean,
1314
) : AbstractSetting<Set<T>>(
@@ -16,7 +17,6 @@ class SetSetting<T : Any>(
1617
visibility
1718
) {
1819
override fun loadFromJson(serialized: JsonElement) {
19-
val setType = object : TypeToken<Set<T>>() {}.type
20-
value = gson.fromJson(serialized, setType)
20+
value = gson.fromJson(serialized, type)
2121
}
2222
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.lambda.gui.impl.clickgui
22

3+
import com.google.gson.reflect.TypeToken
34
import com.lambda.config.Configurable
45
import com.lambda.config.configurations.GuiConfig
56
import com.lambda.gui.impl.clickgui.windows.TagWindow
67

78
object GuiConfigurable : Configurable(GuiConfig) {
8-
override val name = "ClickGui"
9-
val windows = setting("windows", listOf(TagWindow()))
9+
override val name = "gui"
10+
val windows = setting("windows", listOf(TagWindow()), object : TypeToken<List<TagWindow>>() {}.type)
1011
}

common/src/main/kotlin/com/lambda/gui/impl/clickgui/windows/TagWindow.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import com.lambda.module.modules.client.ClickGui
77
import com.lambda.module.tag.ModuleTag
88

99
class TagWindow(
10-
private val tags: Set<ModuleTag> = emptySet(),
11-
override val title: String = "Test Window",
10+
val tags: Set<ModuleTag> = emptySet(),
11+
override var title: String = "Test Window",
1212
override var width: Double = 110.0,
1313
override var height: Double = 300.0
1414
) : WindowComponent<ModuleButton>() {

common/src/main/kotlin/com/lambda/module/Module.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lambda.module
22

3+
import com.google.gson.reflect.TypeToken
34
import com.lambda.config.AbstractSetting
45
import com.lambda.config.Configurable
56
import com.lambda.config.Configuration
@@ -98,7 +99,7 @@ abstract class Module(
9899
private val isEnabledSetting = setting("Enabled", enabledByDefault, visibility = { false })
99100
private val keybindSetting = setting("Keybind", defaultKeybind)
100101
private val isVisible = setting("Visible", true)
101-
val customTags = setting("Tags", defaultTags, visibility = { false })
102+
val customTags = setting("Tags", defaultTags, object : TypeToken<Set<ModuleTag>>() {}.type, visibility = { false })
102103

103104
var isEnabled by isEnabledSetting
104105
override val isMuted: Boolean

common/src/main/kotlin/com/lambda/module/modules/Packetlogger.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lambda.module.modules
22

3+
import com.google.gson.reflect.TypeToken
34
import com.lambda.Lambda
45
import com.lambda.Lambda.mc
56
import com.lambda.event.EventFlow.lambdaScope
@@ -37,8 +38,8 @@ object Packetlogger : Module(
3738
private val networkSide by setting("Network Side", NetworkSide.ANY, "Side of the network to log packets from")
3839
private val logTicks by setting("Log Ticks", true, "Show game ticks in the log")
3940
private val scope by setting("Scope", Scope.ANY, "Scope of packets to log")
40-
private val whitelist by setting("Whitelist Packets", listOf<String>(), "Packets to whitelist") { scope == Scope.WHITELIST }
41-
private val blacklist by setting("Blacklist Packets", listOf<String>(), "Packets to blacklist") { scope == Scope.BLACKLIST }
41+
private val whitelist by setting("Whitelist Packets", listOf<String>(), object : TypeToken<List<String>>() {}.type, "Packets to whitelist") { scope == Scope.WHITELIST }
42+
private val blacklist by setting("Blacklist Packets", listOf<String>(), object : TypeToken<List<String>>() {}.type, "Packets to blacklist") { scope == Scope.BLACKLIST }
4243
private val maxRecursionDepth by setting("Max Recursion Depth", 6, 1..10, 1, "Maximum recursion depth for packet serialization")
4344
private val logConcurrent by setting("Build Data Concurrent", false, "Whether to serialize packets concurrently. Will not save packets in chronological order but wont lag the game.")
4445

0 commit comments

Comments
 (0)