add initial game packets
This commit is contained in:
1 parent
621440690f
commit
8c82990b9a
45 files changed
+1806
-116
No files matched your search
@@ -4,12 +4,14 @@ import com.github.maltalex.ineter.base.IPAddress
|
||||
import com.github.maltalex.ineter.base.IPv4Address
|
||||
import com.github.maltalex.ineter.base.IPv6Address
|
||||
import io.netty.buffer.ByteBuf
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneOffset
|
||||
|
||||
fun ByteBuf.readCharLE(): Char {
|
||||
return ((readUnsignedByte().toInt() or (readUnsignedByte().toInt() shl 8)) and 0xFFFF).toChar()
|
||||
}
|
||||
|
||||
fun ByteBuf.writeCharLE(value: Char) {
|
||||
fun ByteBuf.writeCharLE(value: Char): ByteBuf = apply {
|
||||
writeByte(value.code and 0xFF)
|
||||
writeByte((value.code shr 8) and 0xFF)
|
||||
}
|
||||
@@ -23,7 +25,7 @@ fun ByteBuf.readUtf16LE(): String {
|
||||
return builder.toString()
|
||||
}
|
||||
|
||||
fun ByteBuf.writeUtf16LE(value: String) {
|
||||
fun ByteBuf.writeUtf16LE(value: String): ByteBuf = apply {
|
||||
for (c in value) {
|
||||
writeCharLE(c)
|
||||
}
|
||||
@@ -46,7 +48,7 @@ fun ByteBuf.readIpLE(): IPAddress {
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteBuf.writeIpLE(ipAddress: IPAddress) {
|
||||
fun ByteBuf.writeIpLE(ipAddress: IPAddress): ByteBuf = apply {
|
||||
when (ipAddress) {
|
||||
is IPv4Address -> {
|
||||
writeByte(4)
|
||||
@@ -71,7 +73,17 @@ fun ByteBuf.readIpRangeLE(): Pair<IPAddress, IPAddress> {
|
||||
return Pair(start, end)
|
||||
}
|
||||
|
||||
fun ByteBuf.writeIpRangeLE(start: IPAddress, end: IPAddress) {
|
||||
fun ByteBuf.writeIpRangeLE(start: IPAddress, end: IPAddress): ByteBuf = apply {
|
||||
writeIpLE(start)
|
||||
writeIpLE(end)
|
||||
}
|
||||
|
||||
fun ByteBuf.readEpochSeconds(): LocalDateTime =
|
||||
LocalDateTime.ofEpochSecond(readIntLE().toLong(), 0, ZoneOffset.UTC)
|
||||
|
||||
fun ByteBuf.writeEpochSecond(dateTime: LocalDateTime): ByteBuf =
|
||||
writeIntLE(dateTime.toEpochSecond(ZoneOffset.UTC).toInt())
|
||||
|
||||
fun ByteBuf.writeByte(byte: Byte): ByteBuf = writeByte(byte.toInt())
|
||||
|
||||
fun ByteBuf.writeShortLE(short: Short): ByteBuf = writeShortLE(short.toInt())
|
||||
@@ -57,7 +57,7 @@ abstract class Protocol {
|
||||
"Registering incoming packet: opcode=$opcode, deserializer=${deserializer::class.simpleName}"
|
||||
}
|
||||
|
||||
incomingPacketRegistry.put(opcode, deserializer)
|
||||
incomingPacketRegistry[opcode] = deserializer
|
||||
}
|
||||
|
||||
fun <T : Any> registerOutgoing(
|
||||
@@ -74,7 +74,7 @@ abstract class Protocol {
|
||||
error("Data class ${dataClass.simpleName} is already registered with an opcode!")
|
||||
}
|
||||
dataClassToOpcode[dataClass] = opcode
|
||||
outgoingPacketRegistry.put(opcode, serializer)
|
||||
outgoingPacketRegistry[opcode] = serializer
|
||||
}
|
||||
|
||||
fun getSerializer(opcode: Opcode): PacketSerializer<*>? {
|
||||
@@ -94,10 +94,9 @@ fun <T : Any> Protocol.incomingPacket(opcode: UByte, deserializer: PacketDeseria
|
||||
this.registerIncoming(Opcode(opcode), deserializer)
|
||||
}
|
||||
|
||||
fun <T : Any> Protocol.outgoingPacket(
|
||||
inline fun <reified T : Any> Protocol.outgoingPacket(
|
||||
opcode: UByte,
|
||||
serializer: PacketSerializer<T>,
|
||||
dataClass: KClass<T>
|
||||
serializer: PacketSerializer<T>
|
||||
) {
|
||||
this.registerOutgoing(Opcode(opcode), serializer, dataClass)
|
||||
this.registerOutgoing(Opcode(opcode), serializer, T::class)
|
||||
}
|
||||
Reference in new issue
Block a user