Skip to content

Commit d03418e

Browse files
committed
fixed serializables collection settings from being parsed as a string
1 parent 5511f79 commit d03418e

File tree

5 files changed

+18
-34
lines changed

5 files changed

+18
-34
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,13 @@ abstract class Configurable(
158158
immutableList: Collection<T> = defaultValue,
159159
description: String = "",
160160
displayClassName: Boolean = false,
161+
serialize: Boolean = false,
161162
noinline visibility: () -> Boolean = { true },
162163
) = Setting(
163164
name,
164165
description,
165166
if (displayClassName) ClassCollectionSetting(immutableList, defaultValue.toMutableList())
166-
else CollectionSetting(defaultValue.toMutableList(), immutableList, TypeToken.getParameterized(Collection::class.java, T::class.java).type),
167+
else CollectionSetting(defaultValue.toMutableList(), immutableList, TypeToken.getParameterized(Collection::class.java, T::class.java).type, serialize),
167168
this,
168169
visibility
169170
).register()

src/main/kotlin/com/lambda/config/settings/collections/BlockCollectionSetting.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
package com.lambda.config.settings.collections
1919

20-
import com.google.gson.JsonElement
2120
import com.google.gson.reflect.TypeToken
22-
import com.lambda.Lambda.gson
2321
import com.lambda.config.Setting
2422
import com.lambda.config.serializer.BlockCodec
2523
import com.lambda.gui.dsl.ImGuiBuilder
@@ -31,17 +29,9 @@ class BlockCollectionSetting(
3129
) : CollectionSetting<Block>(
3230
defaultValue,
3331
immutableCollection,
34-
TypeToken.getParameterized(Collection::class.java, Block::class.java).type
32+
TypeToken.getParameterized(Collection::class.java, Block::class.java).type,
33+
serialize = true,
3534
) {
3635
context(setting: Setting<*, MutableCollection<Block>>)
3736
override fun ImGuiBuilder.buildLayout() = buildComboBox("block") { BlockCodec.stringify(it) }
38-
39-
context(setting: Setting<*, MutableCollection<Block>>)
40-
override fun toJson(): JsonElement = gson.toJsonTree(value, type)
41-
42-
context(setting: Setting<*, MutableCollection<Block>>)
43-
override fun loadFromJson(serialized: JsonElement) {
44-
value = gson.fromJson<Collection<Block>>(serialized, type)
45-
.toMutableList()
46-
}
4737
}

src/main/kotlin/com/lambda/config/settings/collections/ClassCollectionSetting.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class ClassCollectionSetting<T : Any>(
3434
) : CollectionSetting<T>(
3535
defaultValue,
3636
immutableCollection,
37-
TypeToken.getParameterized(Collection::class.java, Any::class.java).type
37+
TypeToken.getParameterized(Collection::class.java, Any::class.java).type,
38+
serialize = false,
3839
) {
3940
context(setting: Setting<*, MutableCollection<T>>)
4041
override fun ImGuiBuilder.buildLayout() = buildComboBox("item") { it.className }

src/main/kotlin/com/lambda/config/settings/collections/CollectionSetting.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package com.lambda.config.settings.collections
1919

20-
import com.google.gson.JsonArray
2120
import com.google.gson.JsonElement
2221
import com.google.gson.reflect.TypeToken
2322
import com.lambda.Lambda.gson
@@ -47,7 +46,8 @@ import java.lang.reflect.Type
4746
open class CollectionSetting<R : Any>(
4847
defaultValue: MutableCollection<R>,
4948
private var immutableCollection: Collection<R>,
50-
type: Type
49+
type: Type,
50+
private val serialize: Boolean,
5151
) : SettingCore<MutableCollection<R>>(
5252
defaultValue,
5353
type
@@ -107,16 +107,18 @@ open class CollectionSetting<R : Any>(
107107

108108
context(setting: Setting<*, MutableCollection<R>>)
109109
override fun toJson(): JsonElement =
110-
gson.toJsonTree(value)
110+
gson.toJsonTree(value, type)
111111

112112
context(setting: Setting<*, MutableCollection<R>>)
113113
override fun loadFromJson(serialized: JsonElement) {
114-
val strList = gson.fromJson<Collection<String>>(serialized, strListType)
115-
.mapNotNull { str -> immutableCollection.find { it.toString() == str } }
116-
.toMutableList()
114+
val strList =
115+
if (serialize) gson.fromJson(serialized, type)
116+
else gson.fromJson<Collection<String>>(serialized, strListType)
117+
.mapNotNull { str -> immutableCollection.find { it.toString() == str } }
118+
.toMutableList()
117119

118-
value = strList
119-
}
120+
value = strList
121+
}
120122

121123
companion object {
122124
fun <T : CollectionSetting<R>, R : Any> Setting<T, MutableCollection<R>>.onSelect(block: SafeContext.(R) -> Unit) = apply {

src/main/kotlin/com/lambda/config/settings/collections/ItemCollectionSetting.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
package com.lambda.config.settings.collections
1919

20-
import com.google.gson.JsonElement
2120
import com.google.gson.reflect.TypeToken
22-
import com.lambda.Lambda.gson
2321
import com.lambda.config.Setting
2422
import com.lambda.config.serializer.ItemCodec
2523
import com.lambda.gui.dsl.ImGuiBuilder
@@ -31,17 +29,9 @@ class ItemCollectionSetting(
3129
) : CollectionSetting<Item>(
3230
defaultValue,
3331
immutableCollection,
34-
TypeToken.getParameterized(Collection::class.java, Item::class.java).type
32+
TypeToken.getParameterized(Collection::class.java, Item::class.java).type,
33+
serialize = true,
3534
) {
3635
context(setting: Setting<*, MutableCollection<Item>>)
3736
override fun ImGuiBuilder.buildLayout() = buildComboBox("item") { ItemCodec.stringify(it) }
38-
39-
context(setting: Setting<*, MutableCollection<Item>>)
40-
override fun toJson(): JsonElement = gson.toJsonTree(value, type)
41-
42-
context(setting: Setting<*, MutableCollection<Item>>)
43-
override fun loadFromJson(serialized: JsonElement) {
44-
value = gson.fromJson<Collection<Item>>(serialized, type)
45-
.toMutableList()
46-
}
4737
}

0 commit comments

Comments
 (0)