Formatted code

This commit is contained in:
2026-09-06 14:58:15 +08:00
parent 78a1bdd2da
commit 4a9f35ffdd
14 files changed
+98 -95

No files matched your search

@@ -7,8 +7,6 @@
package cn.rtast.libmc.common
import kotlin.uuid.Uuid
public expect class BytesBuffer {
public constructor()
public constructor(bytes: ByteArray)
@@ -42,82 +40,6 @@ public expect class BytesBuffer {
@Suppress("NOTHING_TO_INLINE")
public inline fun ByteArray.wrap(): BytesBuffer = BytesBuffer(this)
public fun BytesBuffer.writeUuid(uuid: Uuid): Unit = uuid.toLongs { mostSignificantBits, leastSignificantBits ->
this.writeLong(mostSignificantBits)
this.writeLong(leastSignificantBits)
}
public fun BytesBuffer.readUuid(): Uuid {
val most = this.readLong()
val least = this.readLong()
return Uuid.fromLongs(most, least)
}
public fun BytesBuffer.writeVarInt(value: Int): Unit = VarIntCodec.encode(this, value)
public fun BytesBuffer.readVarInt(): Int = VarIntCodec.decode(this)
public fun BytesBuffer.writeVarLong(value: Long): Unit = VarLongCodec.encode(this, value)
public fun BytesBuffer.readVarLong(): Long = VarLongCodec.decode(this)
public fun BytesBuffer.writeMcString(value: String): Unit = McStringCodec.encode(this, value)
public fun BytesBuffer.readMcString(): String = McStringCodec.decode(this)
public fun BytesBuffer.readPrefixedByteArray(): ByteArray {
val length = this.readVarInt()
val data = this.readBytes(length)
return data
}
public fun BytesBuffer.writePrefixedByteArray(data: ByteArray) {
this.writeVarInt(data.size)
this.writeBytes(data)
}
public fun BytesBuffer.readOptionalPrefixedByteArray(): ByteArray? {
val isPresent = this.readBoolean()
return if (isPresent) {
val length = this.readVarInt()
this.readBytes(length)
} else null
}
public fun BytesBuffer.writeOptionalPrefixedByteArray(data: ByteArray?) {
if (data != null) {
this.writeBoolean(true)
this.writeVarInt(data.size)
this.writeBytes(data)
} else this.writeBoolean(false)
}
public fun BytesBuffer.readPrefixedVarIntArray(): List<Int> {
val length = readVarInt()
val list = ArrayList<Int>(length)
repeat(length) { list.add(readVarInt()) }
return list
}
public fun BytesBuffer.writePrefixedVarIntArray(value: List<Int>) {
writeVarInt(value.size)
for (item in value) writeVarInt(item)
}
public fun BytesBuffer.readPrefixedStringArray(): List<String> {
val length = readVarInt()
val list = ArrayList<String>(length)
repeat(length) { list.add(readMcString()) }
return list
}
public fun BytesBuffer.writePrefixedStringArray(value: List<String>) {
writeVarInt(value.size)
for (item in value) writeMcString(item)
}
public inline fun <T> BytesBuffer.writePrefixedArray(value: List<T>, writeItem: BytesBuffer.(T) -> Unit) {
writeVarInt(value.size)
for (item in value) writeItem(item)
}
public fun ReadChannel.readPacketFrame(): BytesBuffer {
val length = this.readVarInt()
return this.readBytes(length).wrap()
@@ -6,6 +6,8 @@
package cn.rtast.libmc.common
import kotlin.uuid.Uuid
public object VarIntCodec : PacketCodec<Int> {
override fun encode(buffer: BytesBuffer, value: Int) {
var v = value
@@ -95,4 +97,80 @@ public inline fun <T> BytesBuffer.writeIdOrX(value: IdOrX<T>, writeX: BytesBuffe
public inline fun <T> BytesBuffer.readIdOrX(readX: BytesBuffer.() -> T): IdOrX<T> {
val id = this.readVarInt()
return if (id == 0) IdOrX.Inline(this.readX()) else IdOrX.Reference(id - 1)
}
public fun BytesBuffer.writeUuid(uuid: Uuid): Unit = uuid.toLongs { mostSignificantBits, leastSignificantBits ->
this.writeLong(mostSignificantBits)
this.writeLong(leastSignificantBits)
}
public fun BytesBuffer.readUuid(): Uuid {
val most = this.readLong()
val least = this.readLong()
return Uuid.fromLongs(most, least)
}
public fun BytesBuffer.writeVarInt(value: Int): Unit = VarIntCodec.encode(this, value)
public fun BytesBuffer.readVarInt(): Int = VarIntCodec.decode(this)
public fun BytesBuffer.writeVarLong(value: Long): Unit = VarLongCodec.encode(this, value)
public fun BytesBuffer.readVarLong(): Long = VarLongCodec.decode(this)
public fun BytesBuffer.writeMcString(value: String): Unit = McStringCodec.encode(this, value)
public fun BytesBuffer.readMcString(): String = McStringCodec.decode(this)
public fun BytesBuffer.readPrefixedByteArray(): ByteArray {
val length = this.readVarInt()
val data = this.readBytes(length)
return data
}
public fun BytesBuffer.writePrefixedByteArray(data: ByteArray) {
this.writeVarInt(data.size)
this.writeBytes(data)
}
public fun BytesBuffer.readOptionalPrefixedByteArray(): ByteArray? {
val isPresent = this.readBoolean()
return if (isPresent) {
val length = this.readVarInt()
this.readBytes(length)
} else null
}
public fun BytesBuffer.writeOptionalPrefixedByteArray(data: ByteArray?) {
if (data != null) {
this.writeBoolean(true)
this.writeVarInt(data.size)
this.writeBytes(data)
} else this.writeBoolean(false)
}
public fun BytesBuffer.readPrefixedVarIntArray(): List<Int> {
val length = readVarInt()
val list = ArrayList<Int>(length)
repeat(length) { list.add(readVarInt()) }
return list
}
public fun BytesBuffer.writePrefixedVarIntArray(value: List<Int>) {
writeVarInt(value.size)
for (item in value) writeVarInt(item)
}
public fun BytesBuffer.readPrefixedStringArray(): List<String> {
val length = readVarInt()
val list = ArrayList<String>(length)
repeat(length) { list.add(readMcString()) }
return list
}
public fun BytesBuffer.writePrefixedStringArray(value: List<String>) {
writeVarInt(value.size)
for (item in value) writeMcString(item)
}
public inline fun <T> BytesBuffer.writePrefixedArray(value: List<T>, writeItem: BytesBuffer.(T) -> Unit) {
writeVarInt(value.size)
for (item in value) writeItem(item)
}
@@ -10,7 +10,7 @@ package cn.rtast.libmc.common.packet
/**
* reserved packet
*/
public data class UnknownPacket(val packetId: Int, val data: ByteArray): MinecraftPacket {
public data class UnknownPacket(val packetId: Int, val data: ByteArray) : MinecraftPacket {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
@@ -7,7 +7,7 @@
package cn.rtast.libmc.common
import io.ktor.utils.io.*
import io.ktor.utils.io.bits.reverseByteOrder
import io.ktor.utils.io.bits.*
import kotlinx.coroutines.runBlocking
public actual class ReadChannel(private val _readChannel: ByteReadChannel) {