Update all codec to suspend; added a default ProtocolCryptoContext implementation
This commit is contained in:
296 files changed
+2199
-1818
No files matched your search
@@ -7,12 +7,12 @@
|
||||
|
||||
package cn.rtast.libmc.nbt
|
||||
|
||||
import cn.rtast.libmc.common.ByteOrder
|
||||
import cn.rtast.libmc.common.BytesBuffer
|
||||
import cn.rtast.libmc.common.stream.ByteOrder
|
||||
import cn.rtast.libmc.common.stream.BytesBuffer
|
||||
|
||||
public class BytesBufferNBTInput(override val order: ByteOrder, private val buffer: BytesBuffer) : NBTInput {
|
||||
override fun readByte(): Byte = buffer.readByte()
|
||||
override fun readBytes(count: Int): ByteArray = buffer.readBytes(count)
|
||||
override suspend fun readByte(): Byte = buffer.readByte()
|
||||
override suspend fun readBytes(count: Int): ByteArray = buffer.readBytes(count)
|
||||
}
|
||||
|
||||
public fun BytesBuffer.toNBTInput(order: ByteOrder = ByteOrder.BIG_ENDIAN): BytesBufferNBTInput =
|
||||
|
||||
@@ -7,17 +7,17 @@
|
||||
|
||||
package cn.rtast.libmc.nbt
|
||||
|
||||
import cn.rtast.libmc.common.ByteOrder
|
||||
import cn.rtast.libmc.common.BytesBuffer
|
||||
import cn.rtast.libmc.common.stream.ByteOrder
|
||||
import cn.rtast.libmc.common.stream.BytesBuffer
|
||||
|
||||
public class BytesBufferNBTOutput(
|
||||
override val order: ByteOrder,
|
||||
override val root: NBTTag.CompoundTag,
|
||||
private val buffer: BytesBuffer,
|
||||
) : NBTOutput {
|
||||
override fun writeByte(value: Byte): Unit = buffer.writeByte(value)
|
||||
override fun writeBytes(value: ByteArray): Unit = buffer.writeBytes(value)
|
||||
override fun toByteArray(): ByteArray = buffer.toByteArray()
|
||||
override suspend fun writeByte(value: Byte): Unit = buffer.writeByte(value)
|
||||
override suspend fun writeBytes(value: ByteArray): Unit = buffer.writeBytes(value)
|
||||
override suspend fun toByteArray(): ByteArray = buffer.toByteArray()
|
||||
}
|
||||
|
||||
public fun BytesBuffer.toNBTOutput(
|
||||
|
||||
@@ -7,33 +7,33 @@
|
||||
|
||||
package cn.rtast.libmc.nbt
|
||||
|
||||
import cn.rtast.libmc.common.ByteOrder
|
||||
import cn.rtast.libmc.common.stream.ByteOrder
|
||||
|
||||
public interface NBTInput {
|
||||
public val order: ByteOrder
|
||||
|
||||
public fun readByte(): Byte
|
||||
public fun readBytes(count: Int): ByteArray
|
||||
public suspend fun readByte(): Byte
|
||||
public suspend fun readBytes(count: Int): ByteArray
|
||||
|
||||
public fun readLong(): Long = if (order == ByteOrder.BIG_ENDIAN) readLongBE() else readLongLE()
|
||||
public fun readFloat(): Float = if (order == ByteOrder.BIG_ENDIAN) readFloatBE() else readFloatLE()
|
||||
public fun readDouble(): Double = if (order == ByteOrder.BIG_ENDIAN) readDoubleBE() else readDoubleLE()
|
||||
public fun readShort(): Short = if (order == ByteOrder.BIG_ENDIAN) readShortBE() else readShortLE()
|
||||
public fun readInt(): Int = if (order == ByteOrder.BIG_ENDIAN) readIntBE() else readIntLE()
|
||||
public suspend fun readLong(): Long = if (order == ByteOrder.BIG_ENDIAN) readLongBE() else readLongLE()
|
||||
public suspend fun readFloat(): Float = if (order == ByteOrder.BIG_ENDIAN) readFloatBE() else readFloatLE()
|
||||
public suspend fun readDouble(): Double = if (order == ByteOrder.BIG_ENDIAN) readDoubleBE() else readDoubleLE()
|
||||
public suspend fun readShort(): Short = if (order == ByteOrder.BIG_ENDIAN) readShortBE() else readShortLE()
|
||||
public suspend fun readInt(): Int = if (order == ByteOrder.BIG_ENDIAN) readIntBE() else readIntLE()
|
||||
|
||||
// big endian
|
||||
public fun readLongBE(): Long =
|
||||
public suspend fun readLongBE(): Long =
|
||||
(readIntBE().toLong() shl 32) or (readIntBE().toLong() and 0xFFFFFFFFL)
|
||||
|
||||
public fun readFloatBE(): Float = Float.fromBits(readIntBE())
|
||||
public fun readDoubleBE(): Double = Double.fromBits(readLongBE())
|
||||
public fun readShortBE(): Short {
|
||||
public suspend fun readFloatBE(): Float = Float.fromBits(readIntBE())
|
||||
public suspend fun readDoubleBE(): Double = Double.fromBits(readLongBE())
|
||||
public suspend fun readShortBE(): Short {
|
||||
val b1 = readByte().toInt() and 0xFF
|
||||
val b2 = readByte().toInt() and 0xFF
|
||||
return ((b1 shl 8) or b2).toShort()
|
||||
}
|
||||
|
||||
public fun readIntBE(): Int {
|
||||
public suspend fun readIntBE(): Int {
|
||||
return ((readByte().toInt() and 0xFF) shl 24) or
|
||||
((readByte().toInt() and 0xFF) shl 16) or
|
||||
((readByte().toInt() and 0xFF) shl 8) or
|
||||
@@ -41,7 +41,7 @@ public interface NBTInput {
|
||||
}
|
||||
|
||||
// little endian
|
||||
public fun readLongLE(): Long {
|
||||
public suspend fun readLongLE(): Long {
|
||||
return ((readByte().toInt() and 0xFF).toLong() shl 0) or
|
||||
((readByte().toInt() and 0xFF).toLong() shl 8) or
|
||||
((readByte().toInt() and 0xFF).toLong() shl 16) or
|
||||
@@ -52,14 +52,14 @@ public interface NBTInput {
|
||||
(readByte().toInt() and 0xFF).toLong()
|
||||
}
|
||||
|
||||
public fun readFloatLE(): Float = Float.fromBits(readIntLE())
|
||||
public suspend fun readFloatLE(): Float = Float.fromBits(readIntLE())
|
||||
|
||||
public fun readDoubleLE(): Double = Double.fromBits(readLongLE())
|
||||
public suspend fun readDoubleLE(): Double = Double.fromBits(readLongLE())
|
||||
|
||||
public fun readShortLE(): Short =
|
||||
public suspend fun readShortLE(): Short =
|
||||
((readByte().toInt() and 0xFF) or (readByte().toInt() and 0xFF shl 8)).toShort()
|
||||
|
||||
public fun readIntLE(): Int =
|
||||
public suspend fun readIntLE(): Int =
|
||||
((readByte().toInt() and 0xFF)) or
|
||||
((readByte().toInt() and 0xFF) shl 8) or
|
||||
((readByte().toInt() and 0xFF) shl 16) or
|
||||
|
||||
@@ -7,44 +7,44 @@
|
||||
|
||||
package cn.rtast.libmc.nbt
|
||||
|
||||
import cn.rtast.libmc.common.ByteOrder
|
||||
import cn.rtast.libmc.common.stream.ByteOrder
|
||||
|
||||
public interface NBTOutput {
|
||||
public val order: ByteOrder
|
||||
public val root: NBTTag.CompoundTag
|
||||
|
||||
public fun writeByte(value: Byte)
|
||||
public fun writeBytes(value: ByteArray)
|
||||
public suspend fun writeByte(value: Byte)
|
||||
public suspend fun writeBytes(value: ByteArray)
|
||||
|
||||
public fun writeShort(value: Short): Unit =
|
||||
public suspend fun writeShort(value: Short): Unit =
|
||||
if (order == ByteOrder.BIG_ENDIAN) writeShortBE(value) else writeShortLE(value)
|
||||
|
||||
public fun writeInt(value: Int): Unit =
|
||||
public suspend fun writeInt(value: Int): Unit =
|
||||
if (order == ByteOrder.BIG_ENDIAN) writeIntBE(value) else writeIntLE(value)
|
||||
|
||||
public fun writeLong(value: Long): Unit =
|
||||
public suspend fun writeLong(value: Long): Unit =
|
||||
if (order == ByteOrder.BIG_ENDIAN) writeLongBE(value) else writeLongLE(value)
|
||||
|
||||
public fun writeFloat(value: Float): Unit =
|
||||
public suspend fun writeFloat(value: Float): Unit =
|
||||
if (order == ByteOrder.BIG_ENDIAN) writeFloatBE(value) else writeFloatLE(value)
|
||||
|
||||
public fun writeDouble(value: Double): Unit =
|
||||
public suspend fun writeDouble(value: Double): Unit =
|
||||
if (order == ByteOrder.BIG_ENDIAN) writeDoubleBE(value) else writeDoubleLE(value)
|
||||
|
||||
// big endian
|
||||
public fun writeShortBE(value: Short) {
|
||||
public suspend fun writeShortBE(value: Short) {
|
||||
writeByte(((value.toInt() shr 8) and 0xFF).toByte())
|
||||
writeByte((value.toInt() and 0xFF).toByte())
|
||||
}
|
||||
|
||||
public fun writeIntBE(value: Int) {
|
||||
public suspend fun writeIntBE(value: Int) {
|
||||
writeByte(((value shr 24) and 0xFF).toByte())
|
||||
writeByte(((value shr 16) and 0xFF).toByte())
|
||||
writeByte(((value shr 8) and 0xFF).toByte())
|
||||
writeByte((value and 0xFF).toByte())
|
||||
}
|
||||
|
||||
public fun writeLongBE(value: Long) {
|
||||
public suspend fun writeLongBE(value: Long) {
|
||||
writeByte(((value shr 56).toInt() and 0xFF).toByte())
|
||||
writeByte(((value shr 48).toInt() and 0xFF).toByte())
|
||||
writeByte(((value shr 40).toInt() and 0xFF).toByte())
|
||||
@@ -55,28 +55,28 @@ public interface NBTOutput {
|
||||
writeByte((value.toInt() and 0xFF).toByte())
|
||||
}
|
||||
|
||||
public fun writeFloatBE(value: Float) {
|
||||
public suspend fun writeFloatBE(value: Float) {
|
||||
writeIntBE(value.toBits())
|
||||
}
|
||||
|
||||
public fun writeDoubleBE(value: Double) {
|
||||
public suspend fun writeDoubleBE(value: Double) {
|
||||
writeLongBE(value.toBits())
|
||||
}
|
||||
|
||||
// Little Endian
|
||||
public fun writeShortLE(value: Short) {
|
||||
public suspend fun writeShortLE(value: Short) {
|
||||
writeByte((value.toInt() and 0xFF).toByte())
|
||||
writeByte(((value.toInt() shr 8) and 0xFF).toByte())
|
||||
}
|
||||
|
||||
public fun writeIntLE(value: Int) {
|
||||
public suspend fun writeIntLE(value: Int) {
|
||||
writeByte((value and 0xFF).toByte())
|
||||
writeByte(((value shr 8) and 0xFF).toByte())
|
||||
writeByte(((value shr 16) and 0xFF).toByte())
|
||||
writeByte(((value shr 24) and 0xFF).toByte())
|
||||
}
|
||||
|
||||
public fun writeLongLE(value: Long) {
|
||||
public suspend fun writeLongLE(value: Long) {
|
||||
writeByte(((value and 0xFF).toInt()).toByte())
|
||||
writeByte((((value shr 8) and 0xFF).toInt()).toByte())
|
||||
writeByte((((value shr 16) and 0xFF).toInt()).toByte())
|
||||
@@ -87,10 +87,7 @@ public interface NBTOutput {
|
||||
writeByte((((value shr 56) and 0xFF).toInt()).toByte())
|
||||
}
|
||||
|
||||
public fun writeFloatLE(value: Float): Unit = writeIntLE(value.toBits())
|
||||
|
||||
public fun writeDoubleLE(value: Double): Unit = writeLongLE(value.toBits())
|
||||
|
||||
|
||||
public fun toByteArray(): ByteArray
|
||||
public suspend fun writeFloatLE(value: Float): Unit = writeIntLE(value.toBits())
|
||||
public suspend fun writeDoubleLE(value: Double): Unit = writeLongLE(value.toBits())
|
||||
public suspend fun toByteArray(): ByteArray
|
||||
}
|
||||
@@ -7,13 +7,13 @@
|
||||
|
||||
package cn.rtast.libmc.nbt
|
||||
|
||||
public fun NBTInput.readStringTag(): String {
|
||||
public suspend fun NBTInput.readStringTag(): String {
|
||||
val length = readShort().toInt() and 0xFFFF
|
||||
val bytes = readBytes(length)
|
||||
return bytes.decodeToString()
|
||||
}
|
||||
|
||||
public fun NBTInput.readListTag(): NBTTag.ListTag {
|
||||
public suspend fun NBTInput.readListTag(): NBTTag.ListTag {
|
||||
val elementTypeId = readByte().toInt() and 0xFF
|
||||
val elementType = NBTType.fromID(elementTypeId)
|
||||
val length = readInt()
|
||||
@@ -22,7 +22,7 @@ public fun NBTInput.readListTag(): NBTTag.ListTag {
|
||||
return NBTTag.ListTag(elementType, list)
|
||||
}
|
||||
|
||||
public fun NBTInput.readCompoundTag(): NBTTag.CompoundTag {
|
||||
public suspend fun NBTInput.readCompoundTag(): NBTTag.CompoundTag {
|
||||
val map = LinkedHashMap<String, NBTTag>()
|
||||
while (true) {
|
||||
val typeId = readByte().toInt() and 0xFF
|
||||
@@ -35,7 +35,7 @@ public fun NBTInput.readCompoundTag(): NBTTag.CompoundTag {
|
||||
return NBTTag.CompoundTag(map)
|
||||
}
|
||||
|
||||
public fun NBTInput.readTagPayload(type: NBTType): NBTTag =
|
||||
public suspend fun NBTInput.readTagPayload(type: NBTType): NBTTag =
|
||||
when (type) {
|
||||
NBTType.Byte -> NBTTag.ByteTag(readByte())
|
||||
NBTType.Short -> NBTTag.ShortTag(readShort())
|
||||
@@ -52,7 +52,7 @@ public fun NBTInput.readTagPayload(type: NBTType): NBTTag =
|
||||
NBTType.End -> error("TAG_End no payload")
|
||||
}
|
||||
|
||||
public fun NBTInput.readCompound(): NBTTag {
|
||||
public suspend fun NBTInput.readCompound(): NBTTag {
|
||||
val map = LinkedHashMap<String, NBTTag>()
|
||||
while (true) {
|
||||
val typeId = readByte().toInt()
|
||||
@@ -67,7 +67,7 @@ public fun NBTInput.readCompound(): NBTTag {
|
||||
return NBTTag.ListTag(NBTType.Compound, map.values.toMutableList())
|
||||
}
|
||||
|
||||
public fun NBTInput.readRootCompound(): NBTCompound {
|
||||
public suspend fun NBTInput.readRootCompound(): NBTCompound {
|
||||
val rootType = NBTType.fromID(readByte().toInt())
|
||||
require(rootType == NBTType.Compound) { "Root tag must be TAG_Compound" }
|
||||
val nameLen = readShort().toInt() and 0xFFFF
|
||||
@@ -76,7 +76,7 @@ public fun NBTInput.readRootCompound(): NBTCompound {
|
||||
return NBTCompound(name, root)
|
||||
}
|
||||
|
||||
public fun NBTInput.readNetworkCompound(): NBTCompound {
|
||||
public suspend fun NBTInput.readNetworkCompound(): NBTCompound {
|
||||
return when (val type = NBTType.fromID(readByte().toInt() and 0xFF)) {
|
||||
NBTType.Compound -> NBTCompound("", readCompoundTag())
|
||||
NBTType.String -> NBTCompound("", NBTTag.CompoundTag(linkedMapOf("text" to NBTTag.StringTag(readStringTag()))))
|
||||
|
||||
@@ -8,13 +8,13 @@
|
||||
package cn.rtast.libmc.nbt
|
||||
|
||||
|
||||
public fun NBTOutput.writeStringTag(value: String) {
|
||||
public suspend fun NBTOutput.writeStringTag(value: String) {
|
||||
val bytes = value.encodeToByteArray()
|
||||
writeShort(bytes.size.toShort())
|
||||
writeBytes(bytes)
|
||||
}
|
||||
|
||||
public fun NBTOutput.writeTagPayload(tag: NBTTag) {
|
||||
public suspend fun NBTOutput.writeTagPayload(tag: NBTTag) {
|
||||
when (tag) {
|
||||
is NBTTag.ByteTag -> writeByte(tag.value)
|
||||
is NBTTag.ShortTag -> writeShort(tag.value)
|
||||
@@ -56,14 +56,14 @@ public fun NBTOutput.writeTagPayload(tag: NBTTag) {
|
||||
}
|
||||
}
|
||||
|
||||
public fun NBTOutput.writeRootNBTCompound(name: String = ""): ByteArray {
|
||||
public suspend fun NBTOutput.writeRootNBTCompound(name: String = ""): ByteArray {
|
||||
writeByte(NBTType.Compound.id)
|
||||
writeStringTag(name)
|
||||
writeTagPayload(root)
|
||||
return toByteArray()
|
||||
}
|
||||
|
||||
public fun NBTOutput.writeNetworkCompound(compound: NBTCompound) {
|
||||
public suspend fun NBTOutput.writeNetworkCompound(compound: NBTCompound) {
|
||||
val compoundTag = compound.element as? NBTTag.CompoundTag
|
||||
?: throw IllegalArgumentException("NBTCompound element must be an NBTTag.CompoundTag")
|
||||
writeByte(NBTType.Compound.id)
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
package test
|
||||
|
||||
import cn.rtast.libmc.common.wrap
|
||||
import cn.rtast.libmc.common.stream.wrap
|
||||
import cn.rtast.libmc.nbt.NbtReader
|
||||
import org.junit.Test
|
||||
import java.io.File
|
||||
|
||||
Reference in New Issue
Block a user