chore: move hex encode and decode into a shared common util (#62)

This commit is contained in:
Fiereu authored and GitHub committed 2026-07-25 21:37:53 +02:00
1 parent 9c52eeb8c6
commit 35f498f0de
10 files changed
+70 -35

No files matched your search

@@ -4,13 +4,11 @@ import de.fiereu.bytecodec.*
import de.fiereu.openmmo.common.Pokemon
import de.fiereu.openmmo.common.PokemonMove
import de.fiereu.openmmo.common.enums.*
private fun seg(hex: String): ByteArray =
ByteArray(hex.length / 2) { hex.substring(it * 2, it * 2 + 2).toInt(16).toByte() }
import de.fiereu.openmmo.common.utils.hexToBytes
private fun reserved(hex: String): Codec<Unit> =
object : Codec<Unit> {
private val bytes = seg(hex)
private val bytes = hex.hexToBytes()
override fun read(buf: ReadBuffer) {
if (bytes.isNotEmpty()) buf.readBytes(ByteArray(bytes.size))
@@ -1,6 +1,7 @@
package de.fiereu.openmmo.net.game.packets.battle
import de.fiereu.bytecodec.*
import de.fiereu.openmmo.common.utils.hexToBytes
/**
* The field state for a wild encounter (opcode 0x30). Each side sends a monster count that sizes
@@ -15,11 +16,8 @@ data class BattleFieldStatePacket(
val wildParty: List<BattleMonBlock>,
)
private fun seg(hex: String): ByteArray =
ByteArray(hex.length / 2) { hex.substring(it * 2, it * 2 + 2).toInt(16).toByte() }
private val PREAMBLE = seg("02000000000000000000000000ff000000000016000000ff00200006")
private val APPEARANCE = seg("ff00024c031aac0f00038001a400040000000000")
private val PREAMBLE = "02000000000000000000000000ff000000000016000000ff00200006".hexToBytes()
private val APPEARANCE = "ff00024c031aac0f00038001a400040000000000".hexToBytes()
object BattleFieldStatePacketCodec : PacketCodec<BattleFieldStatePacket>() {
override fun CodecScope<BattleFieldStatePacket>.body(): BattleFieldStatePacket {
@@ -2,14 +2,13 @@ package de.fiereu.openmmo.net.game
import de.fiereu.bytecodec.test.decodeBytes
import de.fiereu.bytecodec.test.encodeToBytes
import de.fiereu.openmmo.common.utils.toHex
import de.fiereu.openmmo.net.game.packets.battle.BattleMonBlock
import de.fiereu.openmmo.net.game.packets.battle.BattleSwitchInPacket
import de.fiereu.openmmo.net.game.packets.battle.BattleSwitchInPacketCodec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
private fun ByteArray.hex(): String = joinToString("") { "%02x".format(it) }
private fun patrat(): BattleMonBlock =
BattleMonBlock(
slot = 1,
@@ -49,7 +48,7 @@ class BattleSwitchInPacketTest :
fullBlock = false,
)
val bytes = BattleSwitchInPacketCodec.encodeToBytes(packet)
bytes.hex() shouldBe "000000010000ef010600000000000003ff0000000066666666"
bytes.toHex() shouldBe "000000010000ef010600000000000003ff0000000066666666"
}
// A benched monster coming out carries its full block then its active detail. The block
@@ -2,16 +2,13 @@ package de.fiereu.openmmo.net.game
import de.fiereu.bytecodec.test.decodeBytes
import de.fiereu.bytecodec.test.encodeToBytes
import de.fiereu.openmmo.common.utils.hexToBytes
import de.fiereu.openmmo.common.utils.toHex
import de.fiereu.openmmo.net.game.packets.dialog.CreatureDataArg
import de.fiereu.openmmo.net.game.packets.dialog.DialogActionPacketCodec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
private fun bytes(hex: String): ByteArray =
hex.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
private fun ByteArray.hex(): String = joinToString("") { "%02x".format(it) }
class DialogActionPacketTest :
FunSpec({
// Every case is a real 0x21 dialog action from the NPC interaction capture.
@@ -25,15 +22,15 @@ class DialogActionPacketTest :
test("round-trips every captured dialog action byte for byte") {
captures.forEach { hex ->
val packet = DialogActionPacketCodec.decodeBytes(bytes(hex))
DialogActionPacketCodec.encodeToBytes(packet).hex() shouldBe hex
val packet = DialogActionPacketCodec.decodeBytes(hex.hexToBytes())
DialogActionPacketCodec.encodeToBytes(packet).toHex() shouldBe hex
}
}
test("decodes the fields of a dialog action carrying a creature arg") {
val packet =
DialogActionPacketCodec.decodeBytes(
bytes("0d04f80600010ae008bf86c4c01ab004000001000992d00300"))
"0d04f80600010ae008bf86c4c01ab004000001000992d00300".hexToBytes())
packet.flags shouldBe 0x0d.toByte()
packet.actionType shouldBe 0x04.toByte()
packet.textId shouldBe 0x010006f8