Skip to content

Commit

Permalink
WIP Format-spefic encodeTo/decodeFromNbtTag functions
Browse files Browse the repository at this point in the history
TagNbt from any NbtFormat
  • Loading branch information
BenWoodworth committed Sep 11, 2024
1 parent 303f6f4 commit 038f0a2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 47 deletions.
60 changes: 20 additions & 40 deletions api/knbt.api
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
public final class net/benwoodworth/knbt/BedrockNbt : net/benwoodworth/knbt/BinaryNbtFormat {
public final fun decodeFromNbtTag (Lkotlinx/serialization/DeserializationStrategy;Lnet/benwoodworth/knbt/NbtNamed;)Ljava/lang/Object;
public final fun encodeToNbtTag (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Lnet/benwoodworth/knbt/NbtNamed;
public fun getConfiguration ()Lnet/benwoodworth/knbt/BedrockNbtConfiguration;
public synthetic fun getConfiguration ()Lnet/benwoodworth/knbt/BinaryNbtFormatConfiguration;
public synthetic fun getConfiguration ()Lnet/benwoodworth/knbt/NbtFormatConfiguration;
Expand All @@ -23,6 +25,8 @@ public final class net/benwoodworth/knbt/BedrockNbtKt {
}

public final class net/benwoodworth/knbt/BedrockNetworkNbt : net/benwoodworth/knbt/BinaryNbtFormat {
public final fun decodeFromNbtTag (Lkotlinx/serialization/DeserializationStrategy;Lnet/benwoodworth/knbt/NbtTag;)Ljava/lang/Object;
public final fun encodeToNbtTag (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Lnet/benwoodworth/knbt/NbtTag;
public fun getConfiguration ()Lnet/benwoodworth/knbt/BedrockNetworkNbtConfiguration;
public synthetic fun getConfiguration ()Lnet/benwoodworth/knbt/BinaryNbtFormatConfiguration;
public synthetic fun getConfiguration ()Lnet/benwoodworth/knbt/NbtFormatConfiguration;
Expand Down Expand Up @@ -73,6 +77,8 @@ public abstract interface annotation class net/benwoodworth/knbt/ExperimentalNbt
}

public final class net/benwoodworth/knbt/JavaNbt : net/benwoodworth/knbt/BinaryNbtFormat {
public final fun decodeFromNbtTag (Lkotlinx/serialization/DeserializationStrategy;Lnet/benwoodworth/knbt/NbtNamed;)Ljava/lang/Object;
public final fun encodeToNbtTag (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Lnet/benwoodworth/knbt/NbtNamed;
public synthetic fun getConfiguration ()Lnet/benwoodworth/knbt/BinaryNbtFormatConfiguration;
public fun getConfiguration ()Lnet/benwoodworth/knbt/JavaNbtConfiguration;
public synthetic fun getConfiguration ()Lnet/benwoodworth/knbt/NbtFormatConfiguration;
Expand All @@ -97,6 +103,8 @@ public final class net/benwoodworth/knbt/JavaNbtKt {
}

public final class net/benwoodworth/knbt/JavaNetworkNbt : net/benwoodworth/knbt/BinaryNbtFormat {
public final fun decodeFromNbtTag (Lkotlinx/serialization/DeserializationStrategy;Lnet/benwoodworth/knbt/NbtTag;)Ljava/lang/Object;
public final fun encodeToNbtTag (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Lnet/benwoodworth/knbt/NbtTag;
public synthetic fun getConfiguration ()Lnet/benwoodworth/knbt/BinaryNbtFormatConfiguration;
public fun getConfiguration ()Lnet/benwoodworth/knbt/JavaNetworkNbtConfiguration;
public synthetic fun getConfiguration ()Lnet/benwoodworth/knbt/NbtFormatConfiguration;
Expand Down Expand Up @@ -524,8 +532,8 @@ public final class net/benwoodworth/knbt/NbtFloat$Companion {
}

public abstract class net/benwoodworth/knbt/NbtFormat : kotlinx/serialization/SerialFormat {
public final fun decodeFromNbtTag (Lkotlinx/serialization/DeserializationStrategy;Lnet/benwoodworth/knbt/NbtNamed;)Ljava/lang/Object;
public final fun encodeToNbtTag (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Lnet/benwoodworth/knbt/NbtNamed;
protected final fun decodeFromNbtTagUnsafe (Lkotlinx/serialization/DeserializationStrategy;Lnet/benwoodworth/knbt/NbtNamed;)Ljava/lang/Object;
protected final fun encodeToNbtTagUnsafe (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Lnet/benwoodworth/knbt/NbtNamed;
public abstract fun getConfiguration ()Lnet/benwoodworth/knbt/NbtFormatConfiguration;
}

Expand Down Expand Up @@ -1036,44 +1044,14 @@ public abstract class net/benwoodworth/knbt/NbtTransformingSerializer : kotlinx/
protected fun transformSerialize (Lnet/benwoodworth/knbt/NbtTag;)Lnet/benwoodworth/knbt/NbtTag;
}

public abstract class net/benwoodworth/knbt/NbtVariant {
}

public final class net/benwoodworth/knbt/NbtVariant$Bedrock : net/benwoodworth/knbt/NbtVariant {
public static final field INSTANCE Lnet/benwoodworth/knbt/NbtVariant$Bedrock;
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class net/benwoodworth/knbt/NbtVariant$BedrockNetwork : net/benwoodworth/knbt/NbtVariant {
public static final field INSTANCE Lnet/benwoodworth/knbt/NbtVariant$BedrockNetwork;
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class net/benwoodworth/knbt/NbtVariant$Java : net/benwoodworth/knbt/NbtVariant {
public static final field INSTANCE Lnet/benwoodworth/knbt/NbtVariant$Java;
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class net/benwoodworth/knbt/NbtVariant$JavaNetwork : net/benwoodworth/knbt/NbtVariant {
public fun <init> (I)V
public fun equals (Ljava/lang/Object;)Z
public final fun getProtocolVersion ()I
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public abstract interface annotation class net/benwoodworth/knbt/OkioApi : java/lang/annotation/Annotation {
}

public class net/benwoodworth/knbt/StringifiedNbt : net/benwoodworth/knbt/NbtFormat, kotlinx/serialization/StringFormat {
public static final field Default Lnet/benwoodworth/knbt/StringifiedNbt$Default;
public final fun decodeFromNbtTag (Lkotlinx/serialization/DeserializationStrategy;Lnet/benwoodworth/knbt/NbtTag;)Ljava/lang/Object;
public fun decodeFromString (Lkotlinx/serialization/DeserializationStrategy;Ljava/lang/String;)Ljava/lang/Object;
public final fun encodeToNbtTag (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Lnet/benwoodworth/knbt/NbtTag;
public fun encodeToString (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Ljava/lang/String;
public synthetic fun getConfiguration ()Lnet/benwoodworth/knbt/NbtFormatConfiguration;
public fun getConfiguration ()Lnet/benwoodworth/knbt/StringifiedNbtConfiguration;
Expand Down Expand Up @@ -1106,8 +1084,9 @@ public final class net/benwoodworth/knbt/StringifiedNbtKt {

public class net/benwoodworth/knbt/TagNbt : net/benwoodworth/knbt/NbtFormat {
public static final field Default Lnet/benwoodworth/knbt/TagNbt$Default;
public synthetic fun getConfiguration ()Lnet/benwoodworth/knbt/NbtFormatConfiguration;
public fun getConfiguration ()Lnet/benwoodworth/knbt/TagNbtConfiguration;
public final fun decodeFromNbtTag (Lkotlinx/serialization/DeserializationStrategy;Lnet/benwoodworth/knbt/NbtNamed;)Ljava/lang/Object;
public final fun encodeToNbtTag (Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)Lnet/benwoodworth/knbt/NbtNamed;
public fun getConfiguration ()Lnet/benwoodworth/knbt/NbtFormatConfiguration;
public fun getSerializersModule ()Lkotlinx/serialization/modules/SerializersModule;
}

Expand All @@ -1116,8 +1095,8 @@ public final class net/benwoodworth/knbt/TagNbt$Default : net/benwoodworth/knbt/

public final class net/benwoodworth/knbt/TagNbtBuilder : net/benwoodworth/knbt/NbtFormatBuilder {
public fun <init> ()V
public fun <init> (Lnet/benwoodworth/knbt/TagNbt;)V
public synthetic fun <init> (Lnet/benwoodworth/knbt/TagNbt;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lnet/benwoodworth/knbt/NbtFormat;)V
public synthetic fun <init> (Lnet/benwoodworth/knbt/NbtFormat;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun build$knbt ()Lnet/benwoodworth/knbt/NbtFormat;
}

Expand All @@ -1128,8 +1107,9 @@ public final class net/benwoodworth/knbt/TagNbtConfiguration : net/benwoodworth/
}

public final class net/benwoodworth/knbt/TagNbtKt {
public static final fun TagNbt (Lnet/benwoodworth/knbt/TagNbt;Lkotlin/jvm/functions/Function1;)Lnet/benwoodworth/knbt/TagNbt;
public static synthetic fun TagNbt$default (Lnet/benwoodworth/knbt/TagNbt;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lnet/benwoodworth/knbt/TagNbt;
public static final fun TagNbt (Lnet/benwoodworth/knbt/NbtFormat;)Lnet/benwoodworth/knbt/TagNbt;
public static final fun TagNbt (Lnet/benwoodworth/knbt/NbtFormat;Lkotlin/jvm/functions/Function1;)Lnet/benwoodworth/knbt/TagNbt;
public static synthetic fun TagNbt$default (Lnet/benwoodworth/knbt/NbtFormat;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lnet/benwoodworth/knbt/TagNbt;
}

public final class net/benwoodworth/knbt/_DeprecationsKt {
Expand Down
3 changes: 2 additions & 1 deletion src/commonMain/kotlin/NbtContentPolymorphicSerializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ public abstract class NbtContentPolymorphicSerializer<T : Any>(private val baseC
val input = decoder.asNbtDecoder()
val tree = input.decodeNbtTag()

val nbt = TagNbt(input.nbt)
val actualSerializer = selectDeserializer(tree) as KSerializer<T>
val tagName = actualSerializer.descriptor.nbtName
return input.nbt.decodeFromNbtTag(actualSerializer, NbtNamed(tagName, tree))
return nbt.decodeFromNbtTag(actualSerializer, NbtNamed(tagName, tree))
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/commonMain/kotlin/NbtTransformingSerializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,18 @@ public abstract class NbtTransformingSerializer<T : Any>(

final override fun serialize(encoder: Encoder, value: T) {
val output = encoder.asNbtEncoder()
var tag = output.nbt.encodeToNbtTag(tSerializer, value).value
val nbt = TagNbt(output.nbt)
var tag = nbt.encodeToNbtTag(tSerializer, value).value
tag = transformSerialize(tag)
output.encodeNbtTag(tag)
}

final override fun deserialize(decoder: Decoder): T {
val input = decoder.asNbtDecoder()
val nbt = TagNbt(input.nbt)
val tag = input.decodeNbtTag()
val transformed = transformDeserialize(tag)
return input.nbt.decodeFromNbtTag(tSerializer, NbtNamed(nbtName, transformed))
return nbt.decodeFromNbtTag(tSerializer, NbtNamed(nbtName, transformed))
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/commonMain/kotlin/TagNbt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private val tagNbtCapabilities = NbtCapabilities(
)

public open class TagNbt internal constructor(
override val configuration: TagNbtConfiguration,
override val configuration: NbtFormatConfiguration,
override val serializersModule: SerializersModule,
) : NbtFormat() {
override val name get() = "NbtTag"
Expand Down Expand Up @@ -46,18 +46,24 @@ public open class TagNbt internal constructor(
}

/**
* Creates an instance of [TagNbt] configured from the optionally given [TagNbt instance][from]
* Creates an instance of [TagNbt] configured from the optionally given [NbtFormat instance][from]
* and adjusted with [builderAction].
*/
public fun TagNbt(
from: TagNbt? = null,
from: NbtFormat? = null,
builderAction: TagNbtBuilder.() -> Unit,
): TagNbt {
val builder = TagNbtBuilder(from)
builder.builderAction()
return builder.build()
}

/**
* Creates an instance of [TagNbt] configured from the given [NbtFormat instance][from].
*/
public fun TagNbt(from: NbtFormat): TagNbt =
TagNbt(from.configuration, from.serializersModule)

/**
* Serializes the given [value] into an equivalent named [NbtTag] using a serializer retrieved from the reified type
* parameter.
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/TagNbtConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class TagNbtConfiguration internal constructor(
")"
}

public class TagNbtBuilder(nbt: TagNbt? = null) : NbtFormatBuilder(nbt) {
public class TagNbtBuilder(nbt: NbtFormat? = null) : NbtFormatBuilder(nbt) {
override fun build(): TagNbt {
return TagNbt(
configuration = TagNbtConfiguration(
Expand Down

0 comments on commit 038f0a2

Please sign in to comment.