Login into server complete
This commit is contained in:
57 files changed
+1622
-65
No files matched your search
@@ -7,6 +7,8 @@
|
||||
|
||||
package cn.rtast.libmc.common
|
||||
|
||||
import kotlin.uuid.Uuid
|
||||
|
||||
@Suppress("CLASSNAME")
|
||||
public expect class _Buffer {
|
||||
public constructor()
|
||||
@@ -17,12 +19,15 @@ public expect class _Buffer {
|
||||
public fun writeInt(value: Int, endian: ByteOrder = ByteOrder.BIG_ENDIAN)
|
||||
public fun writeLong(value: Long, endian: ByteOrder = ByteOrder.BIG_ENDIAN)
|
||||
public fun writeBytes(bytes: ByteArray)
|
||||
public fun writeBoolean(value: Boolean)
|
||||
|
||||
public fun readByte(): Byte
|
||||
public fun readShort(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Short
|
||||
public fun readInt(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Int
|
||||
public fun readLong(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Long
|
||||
public fun readBytes(length: Int): ByteArray
|
||||
public fun readBoolean(): Boolean
|
||||
|
||||
public fun toByteArray(): ByteArray
|
||||
public fun hasRemaining(): Boolean
|
||||
public fun close()
|
||||
@@ -30,4 +35,29 @@ public expect class _Buffer {
|
||||
public val remaining: Long
|
||||
}
|
||||
|
||||
public fun ByteArray.wrap(): _Buffer = _Buffer(this)
|
||||
public fun ByteArray.wrap(): _Buffer = _Buffer(this)
|
||||
|
||||
public fun _Buffer.writeUuid(uuid: Uuid): Unit = uuid.toLongs { mostSignificantBits, leastSignificantBits ->
|
||||
this.writeLong(mostSignificantBits)
|
||||
this.writeLong(leastSignificantBits)
|
||||
}
|
||||
|
||||
public fun _Buffer.readUuid(): Uuid {
|
||||
val most = this.readLong()
|
||||
val least = this.readLong()
|
||||
return Uuid.fromLongs(most, least)
|
||||
}
|
||||
|
||||
public fun _Buffer.writeVarInt(value: Int): Unit = VarIntCodec.encode(this, value)
|
||||
public fun _Buffer.readVarInt(): Int = VarIntCodec.decode(this)
|
||||
|
||||
public fun _Buffer.writeMcString(value: String): Unit = McStringCodec.encode(this, value)
|
||||
public fun _Buffer.readMcString(): String = McStringCodec.decode(this)
|
||||
|
||||
public fun _ReadChannel.readPacketFrame(): _Buffer {
|
||||
val length = this.readVarInt()
|
||||
val frameBytes = this.readBytes(length)
|
||||
return _Buffer().apply {
|
||||
writeBytes(frameBytes)
|
||||
}
|
||||
}
|
||||
@@ -21,4 +21,18 @@ public expect class _ReadChannel {
|
||||
public expect class _WriteChannel {
|
||||
public fun writeFully(value: ByteArray, startIndex: Int = 0, endIndex: Int = value.size)
|
||||
public fun flush()
|
||||
}
|
||||
|
||||
public fun _ReadChannel.readVarInt(): Int {
|
||||
var numRead = 0
|
||||
var result = 0
|
||||
var read: Byte
|
||||
do {
|
||||
read = this.readByte()
|
||||
val value = (read.toInt() and 0x7F)
|
||||
result = result or (value shl (7 * numRead))
|
||||
numRead++
|
||||
if (numRead > 5) throw IllegalArgumentException("VarInt is too big")
|
||||
} while ((read.toInt() and 0x80) != 0)
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
package cn.rtast.libmc.common
|
||||
|
||||
public object VarIntCodec : PacketCodec<Int> {
|
||||
override fun encode(buffer: _Buffer, value: Int) {
|
||||
var v = value
|
||||
while (true) {
|
||||
if ((v and 0x7F.inv()) == 0) {
|
||||
buffer.writeByte(v.toByte())
|
||||
return
|
||||
}
|
||||
buffer.writeByte(((v and 0x7F) or 0x80).toByte())
|
||||
v = v ushr 7
|
||||
}
|
||||
}
|
||||
|
||||
override fun decode(buffer: _Buffer): Int {
|
||||
var numRead = 0
|
||||
var result = 0
|
||||
var read: Byte
|
||||
do {
|
||||
read = buffer.readByte()
|
||||
val value = (read.toInt() and 0x7F)
|
||||
result = result or (value shl (7 * numRead))
|
||||
numRead++
|
||||
if (numRead > 5) throw IllegalArgumentException("VarInt is too big")
|
||||
} while ((read.toInt() and 0x80) != 0)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
public object McStringCodec : PacketCodec<String> {
|
||||
override fun encode(buffer: _Buffer, value: String) {
|
||||
val bytes = value.encodeToByteArray()
|
||||
VarIntCodec.encode(buffer, bytes.size)
|
||||
buffer.writeBytes(bytes)
|
||||
}
|
||||
|
||||
override fun decode(buffer: _Buffer): String {
|
||||
val length = VarIntCodec.decode(buffer)
|
||||
val bytes = buffer.readBytes(length)
|
||||
return bytes.decodeToString()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/4
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.libmc.common
|
||||
|
||||
public interface MinecraftPacket {
|
||||
public val packetId: Int
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/4
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.libmc.common
|
||||
|
||||
public fun <T : MinecraftPacket> _WriteChannel.sendPacket(packet: T, codec: PacketCodec<T>) {
|
||||
val bodyBuffer = _Buffer()
|
||||
bodyBuffer.write(packet.packetId, VarIntCodec)
|
||||
codec.encode(bodyBuffer, packet)
|
||||
val frameBuffer = _Buffer()
|
||||
frameBuffer.write(bodyBuffer.size, VarIntCodec)
|
||||
frameBuffer.writeBuffer(bodyBuffer)
|
||||
val bytes = frameBuffer.toByteArray()
|
||||
this.writeFully(bytes, 0, bytes.size)
|
||||
this.flush()
|
||||
}
|
||||
@@ -63,6 +63,8 @@ public actual class _Buffer {
|
||||
outStream.write(bytes)
|
||||
}
|
||||
|
||||
public actual fun writeBoolean(value: Boolean): Unit = writeByte(if (value) 0x01 else 0x00)
|
||||
|
||||
private fun ensureReadArray(): ByteArray {
|
||||
var buf = readBuffer
|
||||
if (buf == null) {
|
||||
@@ -89,6 +91,8 @@ public actual class _Buffer {
|
||||
return result
|
||||
}
|
||||
|
||||
public actual fun readBoolean(): Boolean = this.readByte() != 0x00.toByte()
|
||||
|
||||
public actual fun toByteArray(): ByteArray = outStream.toByteArray()
|
||||
public actual fun hasRemaining(): Boolean = readOffset < ensureReadArray().size
|
||||
|
||||
|
||||
@@ -40,6 +40,8 @@ public actual class _Buffer {
|
||||
}
|
||||
|
||||
public actual fun writeBytes(bytes: ByteArray): Unit = _delegateBuf.write(bytes)
|
||||
public actual fun writeBoolean(value: Boolean): Unit = _delegateBuf.writeByte(if (value) 0x01 else 0x00)
|
||||
|
||||
public actual fun readByte(): Byte = _delegateBuf.readByte()
|
||||
public actual fun readShort(endian: ByteOrder): Short {
|
||||
val v = _delegateBuf.readShort()
|
||||
@@ -57,6 +59,8 @@ public actual class _Buffer {
|
||||
}
|
||||
|
||||
public actual fun readBytes(length: Int): ByteArray = _delegateBuf.readByteArray(length)
|
||||
public actual fun readBoolean(): Boolean = _delegateBuf.readByte() != 0x00.toByte()
|
||||
|
||||
public actual fun toByteArray(): ByteArray {
|
||||
val copy = _delegateBuf.peek()
|
||||
return try {
|
||||
|
||||
Reference in New Issue
Block a user