137 lines
3.6 KiB
Kotlin
137 lines
3.6 KiB
Kotlin
package io.annaclemens.xivchat.model.message
|
|
|
|
import io.annaclemens.xivchat.model.ClientOperation
|
|
import org.msgpack.core.MessagePack
|
|
import java.util.*
|
|
|
|
class ClientMessage(private val content: String) {
|
|
fun encode(): ByteArray {
|
|
val packer = MessagePack.newDefaultBufferPacker()
|
|
packer.packArrayHeader(1)
|
|
packer.packString(this.content)
|
|
packer.close()
|
|
|
|
val bytes = packer.toByteArray()
|
|
val buf = ByteArray(1 + bytes.size)
|
|
buf[0] = ClientOperation.Message.code
|
|
bytes.copyInto(buf, 1)
|
|
|
|
return buf
|
|
}
|
|
}
|
|
|
|
class Ping {
|
|
companion object {
|
|
fun encode(): ByteArray {
|
|
val bytes = ByteArray(1)
|
|
bytes[0] = ClientOperation.Ping.code
|
|
return bytes
|
|
}
|
|
}
|
|
}
|
|
|
|
class ClientShutdown {
|
|
companion object {
|
|
fun encode(): ByteArray {
|
|
val bytes = ByteArray(1)
|
|
bytes[0] = ClientOperation.Shutdown.code
|
|
return bytes
|
|
}
|
|
}
|
|
}
|
|
|
|
@OptIn(ExperimentalUnsignedTypes::class)
|
|
class ClientBacklog(private val amount: UShort) {
|
|
fun encode(): ByteArray {
|
|
val packer = MessagePack.newDefaultBufferPacker()
|
|
packer.packArrayHeader(1)
|
|
packer.packShort(this.amount.toShort())
|
|
packer.close()
|
|
|
|
val bytes = packer.toByteArray()
|
|
val buf = ByteArray(1 + bytes.size)
|
|
buf[0] = ClientOperation.Backlog.code
|
|
bytes.copyInto(buf, 1)
|
|
|
|
return buf
|
|
}
|
|
}
|
|
|
|
class ClientCatchUp(private val after: Date) {
|
|
fun encode(): ByteArray {
|
|
val packer = MessagePack.newDefaultBufferPacker()
|
|
packer.packArrayHeader(1)
|
|
packer.packLong(this.after.time)
|
|
packer.close()
|
|
|
|
val bytes = packer.toByteArray()
|
|
val buf = ByteArray(1 + bytes.size)
|
|
buf[0] = ClientOperation.CatchUp.code
|
|
bytes.copyInto(buf, 1)
|
|
|
|
return buf
|
|
}
|
|
}
|
|
|
|
class ClientPlayerList(private val type: PlayerListType) {
|
|
fun encode(): ByteArray {
|
|
val packer = MessagePack.newDefaultBufferPacker()
|
|
packer.packArrayHeader(1)
|
|
packer.packByte(this.type.code)
|
|
packer.close()
|
|
|
|
val bytes = packer.toByteArray()
|
|
val buf = ByteArray(1 + bytes.size)
|
|
buf[0] = ClientOperation.PlayerList.code
|
|
bytes.copyInto(buf, 1)
|
|
|
|
return buf
|
|
}
|
|
}
|
|
|
|
class ClientChannel(private val channel: InputChannel) {
|
|
fun encode(): ByteArray {
|
|
val packer = MessagePack.newDefaultBufferPacker()
|
|
packer.packArrayHeader(1)
|
|
packer.packByte(this.channel.code)
|
|
packer.close()
|
|
|
|
val bytes = packer.toByteArray()
|
|
val buf = ByteArray(1 + bytes.size)
|
|
buf[0] = ClientOperation.Channel.code
|
|
bytes.copyInto(buf, 1)
|
|
|
|
return buf
|
|
}
|
|
}
|
|
|
|
class ClientPreferences(private val preferences: Map<ClientPreference, Any>) {
|
|
fun encode(): ByteArray {
|
|
val packer = MessagePack.newDefaultBufferPacker()
|
|
packer.packArrayHeader(1)
|
|
packer.packMapHeader(this.preferences.size)
|
|
for (entry in this.preferences) {
|
|
packer.packByte(entry.key.code.toByte())
|
|
packer.packBoolean(entry.value == true) // TODO: fix
|
|
}
|
|
packer.close()
|
|
|
|
val bytes = packer.toByteArray()
|
|
val buf = ByteArray(1 + bytes.size)
|
|
buf[0] = ClientOperation.Preferences.code
|
|
bytes.copyInto(buf, 1)
|
|
|
|
return buf
|
|
}
|
|
}
|
|
|
|
enum class ClientPreference(val code: UByte) {
|
|
BacklogNewestMessagesFirst(0u),
|
|
TargetingListSupport(1u);
|
|
|
|
companion object {
|
|
private val map = values().associateBy(ClientPreference::code)
|
|
fun fromCode(code: UByte) = this.map[code]
|
|
}
|
|
}
|