summaryrefslogtreewikicommitdiff
path: root/core/src/config/util
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2026-03-28 23:09:18 -0400
committerFreya Murphy <freya@freyacat.org>2026-03-28 23:09:18 -0400
commit2352b5324ae1f7a37361f067de836c3da6b1f1e5 (patch)
tree4153b8c74f84dffc9863d29c12d253a2ca857463 /core/src/config/util
parentadd wiki (diff)
downloadkenshinshideandseek2-2352b5324ae1f7a37361f067de836c3da6b1f1e5.tar.gz
kenshinshideandseek2-2352b5324ae1f7a37361f067de836c3da6b1f1e5.tar.bz2
kenshinshideandseek2-2352b5324ae1f7a37361f067de836c3da6b1f1e5.zip
2.0.0-alpha2v2.0.0-alpha2
- Added command /hs map blockhunt disguise - Auto complete block names in blockhunt commands - Fixed void generating chunks in non map save worlds - Fixed onDamage having no attacker - Fixed onDamage not respawning seeker properly - Fixed config deserialize not handeling nulls - Fixed config deserialize looking at more properties then it should
Diffstat (limited to 'core/src/config/util')
-rw-r--r--core/src/config/util/Deserialize.kt45
1 files changed, 30 insertions, 15 deletions
diff --git a/core/src/config/util/Deserialize.kt b/core/src/config/util/Deserialize.kt
index 8a5be59..58e23ce 100644
--- a/core/src/config/util/Deserialize.kt
+++ b/core/src/config/util/Deserialize.kt
@@ -12,26 +12,40 @@ import kotlin.reflect.full.createInstance
import kotlin.reflect.full.declaredFunctions
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.full.memberProperties
+import kotlin.reflect.full.primaryConstructor
import org.yaml.snakeyaml.Yaml
fun <T : Any> deserializeClass(type: KClass<T>, data: Map<String, Any?>): T {
require(type.isData) { "$type is not a data class" }
+ val props = type.memberProperties.associateBy { it.name }
+
val propValues =
- type.memberProperties.associateWith { prop ->
- val value = data[prop.name] ?: return@associateWith null
- val propType = prop.returnType.classifier as KClass<*>
- val innerTypes =
- prop.returnType.arguments.map { it.type?.classifier as? KClass<*> }.filterNotNull()
- deserializeField(propType, innerTypes, prop.name, value)
- }
+ type.primaryConstructor!!
+ .parameters
+ .map { props[it.name]!! }
+ .associateWith { prop ->
+ val value = data[prop.name]
+ val propType = prop.returnType.classifier as KClass<*>
+ val innerTypes =
+ prop.returnType.arguments
+ .map { it.type?.classifier as? KClass<*> }
+ .filterNotNull()
+
+ // allow null if type is null
+ if (prop.returnType.isMarkedNullable == true && value == null)
+ return@associateWith null
+
+ deserializeField(propType, innerTypes, prop.name, value)
+ }
val instance = type.createInstance()
for ((prop, value) in propValues) {
- if (value != null) {
- (prop as? KMutableProperty1<*, *>)?.setter?.call(instance, value)
- ?: error("${prop.name} is not mutable")
- }
+ if (value == null && !prop.returnType.isMarkedNullable)
+ error("${prop.name} cannot be null")
+
+ (prop as? KMutableProperty1<*, *>)?.setter?.call(instance, value)
+ ?: error("${prop.name} is not mutable")
}
val migrateFunction = instance::class.declaredFunctions.singleOrNull { it.name == "migrate" }
@@ -92,14 +106,15 @@ fun <T : Any> deserializeField(
type.isData ->
deserializeClass<T>(
type,
- value as? Map<String, Any?> ?: error("$key: expected map for data class $type"),
+ value as? Map<String, Any?>
+ ?: error("$key: expected map for data class $type, got $value"),
)
type.java.isEnum ->
deserializeEnum(
type as KClass<Enum<*>>,
key,
- value as? String ?: error("$key: expected string for enum value"),
+ value as? String ?: error("$key: expected string for enum value, got $value"),
)
as T
@@ -107,7 +122,7 @@ fun <T : Any> deserializeField(
deserializeList(
innerTypes?.firstOrNull() ?: error("$key: innerType not set"),
key,
- value as? List<*> ?: error("$key: expected list for type $type"),
+ value as? List<*> ?: error("$key: expected list for type $type, got $value"),
)
as T
@@ -116,7 +131,7 @@ fun <T : Any> deserializeField(
innerTypes?.firstOrNull() ?: error("key type not set"),
innerTypes.getOrNull(1) ?: error("value type not set"),
key,
- value as? Map<*, *> ?: error("$key: expected map for type $type"),
+ value as? Map<*, *> ?: error("$key: expected map for type $type, got $value"),
)
as T