feat(protocol): add the gtl packets and codecs (#95)

This commit is contained in:
Fiereu authored and GitHub committed 2026-08-07 09:18:33 +02:00
1 parent f785cc5a80
commit 4289ecca5e
60 files changed
+781 -274

No files matched your search

@@ -8,6 +8,7 @@ import de.fiereu.openmmo.net.game.packets.*
import de.fiereu.openmmo.net.game.packets.battle.*
import de.fiereu.openmmo.net.game.packets.battle.moves.*
import de.fiereu.openmmo.net.game.packets.dialog.*
import de.fiereu.openmmo.net.game.packets.gtl.*
import de.fiereu.openmmo.net.game.packets.guild.*
object GameProtocol : Protocol() {
@@ -309,7 +310,7 @@ object GameProtocol : Protocol() {
s2c<NamedCategoryEntriesPacket>(0x6Fu, NamedCategoryEntriesPacketCodec)
c2s<StorageBoxRequestPacket>(0x70u, StorageBoxRequestPacketCodec)
c2s<GtlMarketListingsRequestPacket>(0x70u, GtlMarketListingsRequestPacketCodec)
s2c<GtlMarketListingsPacket>(0x70u, GtlMarketListingsPacketCodec)
c2s<GtlPurchaseListingPacket>(0x71u, GtlPurchaseListingPacketCodec)
@@ -426,8 +427,8 @@ object GameProtocol : Protocol() {
c2s<CreateMarketListingPacket>(0x9Au, CreateMarketListingPacketCodec)
s2c<BattleSlotAnimationResetPacket>(0x9Au, BattleSlotAnimationResetPacketCodec)
c2s<GtlSellPokemonListingPacket>(0x9Bu, GtlSellPokemonListingPacketCodec)
s2c<BattleActionOutcomePacket>(0x9Bu, BattleActionOutcomePacketCodec)
c2s<GtlSearchPageRequestPacket>(0x9Bu, GtlSearchPageRequestPacketCodec)
s2c<GtlSearchPagePacket>(0x9Bu, GtlSearchPagePacketCodec)
c2s<GtlConfirmPurchasePacket>(0x9Cu, GtlConfirmPurchasePacketCodec)
s2c<BattleDisconnectMessagePacket>(0x9Cu, BattleDisconnectMessagePacketCodec)
@@ -565,7 +566,7 @@ object GameProtocol : Protocol() {
c2s<BattleRewardSelectPacket>(0xE3u, BattleRewardSelectPacketCodec)
c2s<BattleChallengeRequestPacket>(0xE4u, BattleChallengeRequestPacketCodec)
c2s<GtlListingsPageRequestPacket>(0xE4u, GtlListingsPageRequestPacketCodec)
s2c<EntityMovePacket>(0xE4u, EntityMovePacketCodec)
s2c<GbaEntityMovePacket>(0xEAu, GbaEntityMovePacketCodec)
@@ -53,9 +53,9 @@ private fun evsFromWire(hp: Int, atk: Int, def: Int, spd: Int, spAtk: Int, spDef
private fun ivsFromBits(bits: Int): IVs = decompressIVs(bits)
/**
* The party/PC monster record. The named fields are decoded from real captures. The reserved
* segments hold species metadata and still-unlabelled structure kept as reference bytes, so the
* layout round-trips exactly.
* The monster record. The named fields are decoded from real captures. The reserved segments hold
* species metadata and still-unlabelled structure kept as reference bytes, so the layout
* round-trips exactly.
*/
object PokemonCodec : PacketCodec<Pokemon>() {
override fun CodecScope<Pokemon>.body(): Pokemon {
@@ -1,59 +0,0 @@
package de.fiereu.openmmo.net.game.packets
import de.fiereu.bytecodec.*
data class TradeListingEntry(
val a: Int,
val b: Int,
val c: Long,
)
data class TradeListing(
val typeId: Short,
val price: Int,
val fieldB: Int,
val fieldC: Int,
val fieldD: Int,
val entries: List<TradeListingEntry>,
)
data class GtlListingsPagePacket(
val page: Byte,
val entryKind: Int,
val totalCount: Int,
val listings: List<TradeListing>,
)
private val TradeListingEntryCodec: Codec<TradeListingEntry> =
object : PacketCodec<TradeListingEntry>() {
override fun CodecScope<TradeListingEntry>.body(): TradeListingEntry {
val a = field(S32LE) { it.a }
val b = field(S32LE) { it.b }
val c = field(S64LE) { it.c }
return TradeListingEntry(a, b, c)
}
}
private val TradeListingCodec: Codec<TradeListing> =
object : PacketCodec<TradeListing>() {
override fun CodecScope<TradeListing>.body(): TradeListing {
val typeId = field(S16LE) { it.typeId }
val price = field(S32LE) { it.price }
val fieldB = field(S32LE) { it.fieldB }
val fieldC = field(S32LE) { it.fieldC }
val fieldD = field(S32LE) { it.fieldD }
val entries = field(TradeListingEntryCodec.listPrefixed(U8)) { it.entries }
return TradeListing(typeId, price, fieldB, fieldC, fieldD, entries)
}
}
object GtlListingsPagePacketCodec : PacketCodec<GtlListingsPagePacket>() {
override fun CodecScope<GtlListingsPagePacket>.body(): GtlListingsPagePacket {
val page = field(S8) { it.page }
val entryKind = field(U8) { it.entryKind }
field(S16LE) { 0 }
val totalCount = field(S32LE) { it.totalCount }
val listings = field(TradeListingCodec.listPrefixed(U8)) { it.listings }
return GtlListingsPagePacket(page, entryKind, totalCount, listings)
}
}
@@ -1,44 +0,0 @@
package de.fiereu.openmmo.net.game.packets
import de.fiereu.bytecodec.*
data class GtlSellPokemonListingPacket(
val listingType: Byte,
val entryKindId: Byte,
val listingCategory: Byte,
val itemTypeId: Short,
val criteriaCount: Int,
val criteriaData: ByteArray,
) {
override fun equals(other: Any?): Boolean =
other is GtlSellPokemonListingPacket &&
listingType == other.listingType &&
entryKindId == other.entryKindId &&
listingCategory == other.listingCategory &&
itemTypeId == other.itemTypeId &&
criteriaCount == other.criteriaCount &&
criteriaData.contentEquals(other.criteriaData)
override fun hashCode(): Int {
var r = listingType.toInt()
r = r * 31 + entryKindId
r = r * 31 + listingCategory
r = r * 31 + itemTypeId
r = r * 31 + criteriaCount
r = r * 31 + criteriaData.contentHashCode()
return r
}
}
object GtlSellPokemonListingPacketCodec : PacketCodec<GtlSellPokemonListingPacket>() {
override fun CodecScope<GtlSellPokemonListingPacket>.body(): GtlSellPokemonListingPacket {
val listingType = field(S8) { it.listingType }
val entryKindId = field(S8) { it.entryKindId }
val listingCategory = field(S8) { it.listingCategory }
val itemTypeId = field(S16LE) { it.itemTypeId }
val criteriaCount = field(U8) { it.criteriaCount }
val criteriaData = field(RemainingBytes) { it.criteriaData }
return GtlSellPokemonListingPacket(
listingType, entryKindId, listingCategory, itemTypeId, criteriaCount, criteriaData)
}
}
@@ -1,12 +0,0 @@
package de.fiereu.openmmo.net.game.packets
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
class StorageBoxRequestPacket
object StorageBoxRequestPacketCodec : PacketCodec<StorageBoxRequestPacket>() {
override fun CodecScope<StorageBoxRequestPacket>.body(): StorageBoxRequestPacket {
return StorageBoxRequestPacket()
}
}
@@ -1,81 +0,0 @@
package de.fiereu.openmmo.net.game.packets.battle
import de.fiereu.bytecodec.*
data class BattleChallengeRequestPacket(
val flags: Byte,
val entryKindId: Byte,
val battleFormat: Byte,
val levelCap: Short,
val allowedSpecies: List<Short>,
val ruleSet: Byte,
val ruleParam: Short,
val extraRule: ByteArray?,
) {
override fun equals(other: Any?): Boolean =
other is BattleChallengeRequestPacket &&
flags == other.flags &&
entryKindId == other.entryKindId &&
battleFormat == other.battleFormat &&
levelCap == other.levelCap &&
allowedSpecies == other.allowedSpecies &&
ruleSet == other.ruleSet &&
ruleParam == other.ruleParam &&
(extraRule?.contentEquals(other.extraRule ?: ByteArray(0)) ?: (other.extraRule == null))
override fun hashCode(): Int {
var r = flags.toInt()
r = r * 31 + entryKindId
r = r * 31 + battleFormat
r = r * 31 + levelCap
r = r * 31 + allowedSpecies.hashCode()
r = r * 31 + ruleSet
r = r * 31 + ruleParam
r = r * 31 + (extraRule?.contentHashCode() ?: 0)
return r
}
}
private val ExtraRuleBytes: Codec<ByteArray> =
object : Codec<ByteArray> {
override fun read(buf: ReadBuffer): ByteArray {
val data = ByteArray(buf.remaining())
if (data.isNotEmpty()) buf.readBytes(data)
return data
}
override fun write(buf: WriteBuffer, value: ByteArray) {
if (value.isNotEmpty()) buf.writeBytes(value)
}
}
object BattleChallengeRequestPacketCodec : PacketCodec<BattleChallengeRequestPacket>() {
override fun CodecScope<BattleChallengeRequestPacket>.body(): BattleChallengeRequestPacket {
val flags = field(S8) { it.flags }
val entryKindId = field(S8) { it.entryKindId }
val battleFormat = field(S8) { it.battleFormat }
val levelCap = field(S16LE) { it.levelCap }
val allowedSpecies = field(S16LE.listPrefixed(U8)) { it.allowedSpecies }
val ruleSet = field(S8) { it.ruleSet }
val ruleParam = field(S16LE) { it.ruleParam }
val present = field(U8) { if (it.extraRule != null) 1 else 0 }
val extraRule =
if (present != 0) {
field(ExtraRuleBytes) {
it.extraRule ?: throw MalformedPacketException("extraRule required")
}
} else {
null
}
return BattleChallengeRequestPacket(
flags,
entryKindId,
battleFormat,
levelCap,
allowedSpecies,
ruleSet,
ruleParam,
extraRule,
)
}
}
@@ -1,4 +1,4 @@
package de.fiereu.openmmo.net.game.packets
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
@@ -1,4 +1,4 @@
package de.fiereu.openmmo.net.game.packets
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.*
@@ -0,0 +1,30 @@
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.Codec
import de.fiereu.bytecodec.U8
import de.fiereu.bytecodec.enumBy
/**
* A search filter. [payloadSize] is how many bytes follow the id, so a filter list can be walked
* without knowing what each one means. [SPECIES] sizes itself instead.
*/
enum class GtlFilterKind(val id: Int, val payloadSize: Int) {
SPECIES(0, VARIABLE),
GENDER(1, 1),
NATURE(2, 1),
MIN_LEVEL(3, 1),
MAX_LEVEL(4, 1),
IV(5, 3),
EGG_TYPE(7, 1),
SHINY(8, 1),
MIN_PRICE(9, 4),
MAX_PRICE(10, 4),
TIER(11, 1),
UNKNOWN_12(12, 1),
UNKNOWN_13(13, 1),
}
private const val VARIABLE = -1
val GtlFilterKindCodec: Codec<GtlFilterKind> =
enumBy(U8, lookup = { id -> GtlFilterKind.entries.first { it.id == id } }, keyOf = { it.id })
@@ -0,0 +1,12 @@
package de.fiereu.openmmo.net.game.packets.gtl
data class GtlItemListing(
override val listingId: Long,
override val price: Int,
override val listedAt: Int,
override val expiresAt: Int,
override val quantity: Short,
val itemId: Short,
/** Zero in every captured row. */
val itemState: Byte,
) : GtlSearchListing
@@ -1,4 +1,4 @@
package de.fiereu.openmmo.net.game.packets
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.*
@@ -0,0 +1,21 @@
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.Codec
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
import de.fiereu.bytecodec.S16LE
import de.fiereu.bytecodec.S32LE
data class GtlItemQuote(
val itemId: Short,
val price: Int,
)
internal val GtlItemQuoteCodec: Codec<GtlItemQuote> =
object : PacketCodec<GtlItemQuote>() {
override fun CodecScope<GtlItemQuote>.body(): GtlItemQuote {
val itemId = field(S16LE) { it.itemId }
val price = field(S32LE) { it.price }
return GtlItemQuote(itemId, price)
}
}
@@ -0,0 +1,16 @@
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.Codec
import de.fiereu.bytecodec.U8
import de.fiereu.bytecodec.enumBy
/** The board a request or a page belongs to. It also decides how a listing row is laid out. */
enum class GtlListKind(val id: Int) {
POKEMON(0),
ITEM(1),
/** Every captured page of these is empty, so the row layout is unverified. */
OWN_LISTINGS(2),
}
val GtlListKindCodec: Codec<GtlListKind> =
enumBy(U8, lookup = { id -> GtlListKind.entries.first { it.id == id } }, keyOf = { it.id })
@@ -1,4 +1,4 @@
package de.fiereu.openmmo.net.game.packets
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
@@ -1,4 +1,4 @@
package de.fiereu.openmmo.net.game.packets
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
@@ -1,4 +1,4 @@
package de.fiereu.openmmo.net.game.packets
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
@@ -0,0 +1,28 @@
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
import de.fiereu.bytecodec.S16LE
import de.fiereu.bytecodec.S32LE
import de.fiereu.bytecodec.S8
import de.fiereu.bytecodec.U8
import de.fiereu.bytecodec.listPrefixed
data class GtlListingsPagePacket(
val requestId: Byte,
val listKind: GtlListKind,
val page: Short,
val totalCount: Int,
val listings: List<TradeListing>,
)
object GtlListingsPagePacketCodec : PacketCodec<GtlListingsPagePacket>() {
override fun CodecScope<GtlListingsPagePacket>.body(): GtlListingsPagePacket {
val requestId = field(S8) { it.requestId }
val listKind = field(GtlListKindCodec) { it.listKind }
val page = field(S16LE) { it.page }
val totalCount = field(S32LE) { it.totalCount }
val listings = field(TradeListingCodec.listPrefixed(U8)) { it.listings }
return GtlListingsPagePacket(requestId, listKind, page, totalCount, listings)
}
}
@@ -0,0 +1,29 @@
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
import de.fiereu.bytecodec.S16LE
import de.fiereu.bytecodec.S8
import de.fiereu.bytecodec.padding
/** [requestId] counts up per board and comes back in [GtlListingsPagePacket]. */
data class GtlListingsPageRequestPacket(
val requestId: Byte,
val listKind: GtlListKind,
val categoryId: Byte,
val page: Short,
)
object GtlListingsPageRequestPacketCodec : PacketCodec<GtlListingsPageRequestPacket>() {
override fun CodecScope<GtlListingsPageRequestPacket>.body(): GtlListingsPageRequestPacket {
val requestId = field(S8) { it.requestId }
val listKind = field(GtlListKindCodec) { it.listKind }
val categoryId = field(S8) { it.categoryId }
val page = field(S16LE) { it.page }
padding(1)
// Price filters the client leaves unset.
padding(3, 0xFF.toByte())
padding(1)
return GtlListingsPageRequestPacket(requestId, listKind, categoryId, page)
}
}
@@ -0,0 +1,62 @@
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.Bool
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
import de.fiereu.bytecodec.S16LE
import de.fiereu.bytecodec.S32LE
import de.fiereu.bytecodec.S8
import de.fiereu.bytecodec.reserved
data class GtlMarketListing(
val listingId: Int,
val typeId: Byte,
val sellerId: Byte,
val speciesId: Short,
val value1: Int,
val value2: Int,
val value3: Int,
val ownListing: Boolean,
val value4: Int,
val value5: Int,
val value6: Int,
val value7: Int,
val form: Byte,
val rowIndex: Int,
)
internal object GtlMarketListingCodec : PacketCodec<GtlMarketListing>() {
override fun CodecScope<GtlMarketListing>.body(): GtlMarketListing {
val listingId = field(S32LE, GtlMarketListing::listingId)
val typeId = field(S8, GtlMarketListing::typeId)
val sellerId = field(S8, GtlMarketListing::sellerId)
val speciesId = field(S16LE, GtlMarketListing::speciesId)
val value1 = field(S32LE, GtlMarketListing::value1)
val value2 = field(S32LE, GtlMarketListing::value2)
val value3 = field(S32LE, GtlMarketListing::value3)
val ownListing = field(Bool, GtlMarketListing::ownListing)
val value4 = field(S32LE, GtlMarketListing::value4)
val value5 = field(S32LE, GtlMarketListing::value5)
val value6 = field(S32LE, GtlMarketListing::value6)
val value7 = field(S32LE, GtlMarketListing::value7)
val form = field(S8, GtlMarketListing::form)
reserved(byte = 1)
val rowIndex = field(S32LE, GtlMarketListing::rowIndex)
return GtlMarketListing(
listingId,
typeId,
sellerId,
speciesId,
value1,
value2,
value3,
ownListing,
value4,
value5,
value6,
value7,
form,
rowIndex,
)
}
}
@@ -1,23 +1,13 @@
package de.fiereu.openmmo.net.game.packets
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.*
data class GtlMarketListing(
val listingId: Int,
val typeId: Byte,
val sellerId: Byte,
val speciesId: Short,
val value1: Int,
val value2: Int,
val value3: Int,
val ownListing: Boolean,
val value4: Int,
val value5: Int,
val value6: Int,
val value7: Int,
val form: Byte,
val rowIndex: Int,
)
import de.fiereu.bytecodec.Bool
import de.fiereu.bytecodec.Codec
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
import de.fiereu.bytecodec.ReadBuffer
import de.fiereu.bytecodec.U8
import de.fiereu.bytecodec.WriteBuffer
import de.fiereu.bytecodec.bytesPrefixed
data class GtlMarketListingsPacket(
val fullReset: Boolean,
@@ -43,42 +33,6 @@ data class GtlMarketListingsPacket(
}
}
private object GtlMarketListingCodec : PacketCodec<GtlMarketListing>() {
override fun CodecScope<GtlMarketListing>.body(): GtlMarketListing {
val listingId = field(S32LE, GtlMarketListing::listingId)
val typeId = field(S8, GtlMarketListing::typeId)
val sellerId = field(S8, GtlMarketListing::sellerId)
val speciesId = field(S16LE, GtlMarketListing::speciesId)
val value1 = field(S32LE, GtlMarketListing::value1)
val value2 = field(S32LE, GtlMarketListing::value2)
val value3 = field(S32LE, GtlMarketListing::value3)
val ownListing = field(Bool, GtlMarketListing::ownListing)
val value4 = field(S32LE, GtlMarketListing::value4)
val value5 = field(S32LE, GtlMarketListing::value5)
val value6 = field(S32LE, GtlMarketListing::value6)
val value7 = field(S32LE, GtlMarketListing::value7)
val form = field(S8, GtlMarketListing::form)
reserved(byte = 0)
val rowIndex = field(S32LE, GtlMarketListing::rowIndex)
return GtlMarketListing(
listingId,
typeId,
sellerId,
speciesId,
value1,
value2,
value3,
ownListing,
value4,
value5,
value6,
value7,
form,
rowIndex,
)
}
}
private val GtlMarketListingListPrefixedU8: Codec<List<GtlMarketListing>> =
object : Codec<List<GtlMarketListing>> {
override fun read(buf: ReadBuffer): List<GtlMarketListing> {
@@ -0,0 +1,12 @@
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
class GtlMarketListingsRequestPacket
object GtlMarketListingsRequestPacketCodec : PacketCodec<GtlMarketListingsRequestPacket>() {
override fun CodecScope<GtlMarketListingsRequestPacket>.body(): GtlMarketListingsRequestPacket {
return GtlMarketListingsRequestPacket()
}
}
@@ -1,4 +1,4 @@
package de.fiereu.openmmo.net.game.packets
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
@@ -0,0 +1,15 @@
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.openmmo.common.Pokemon
data class GtlPokemonListing(
override val listingId: Long,
override val price: Int,
override val listedAt: Int,
override val expiresAt: Int,
override val quantity: Short,
/** A row can say it carries no record. Every captured row carries one. */
val pokemon: Pokemon?,
/** The battle stats the board shows, in EV wire order hp, atk, def, spd, spAtk, spDef. */
val stats: List<Short>,
) : GtlSearchListing
@@ -1,4 +1,4 @@
package de.fiereu.openmmo.net.game.packets
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.*
@@ -1,4 +1,4 @@
package de.fiereu.openmmo.net.game.packets
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
@@ -1,4 +1,4 @@
package de.fiereu.openmmo.net.game.packets
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.*
@@ -0,0 +1,10 @@
package de.fiereu.openmmo.net.game.packets.gtl
/** A filter whose body has not been decoded yet. */
data class GtlRawFilter(override val kind: GtlFilterKind, val payload: ByteArray) :
GtlSearchFilter {
override fun equals(other: Any?): Boolean =
other is GtlRawFilter && kind == other.kind && payload.contentEquals(other.payload)
override fun hashCode(): Int = kind.hashCode() * 31 + payload.contentHashCode()
}
@@ -0,0 +1,35 @@
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.Codec
import de.fiereu.bytecodec.ReadBuffer
import de.fiereu.bytecodec.S16LE
import de.fiereu.bytecodec.U16LE
import de.fiereu.bytecodec.WriteBuffer
sealed interface GtlSearchFilter {
val kind: GtlFilterKind
}
internal val GtlSearchFilterCodec: Codec<GtlSearchFilter> =
object : Codec<GtlSearchFilter> {
override fun read(buf: ReadBuffer): GtlSearchFilter {
val kind = GtlFilterKindCodec.read(buf)
if (kind == GtlFilterKind.SPECIES) {
return GtlSpeciesFilter(List(U16LE.read(buf)) { S16LE.read(buf) })
}
val payload = ByteArray(kind.payloadSize)
buf.readBytes(payload)
return GtlRawFilter(kind, payload)
}
override fun write(buf: WriteBuffer, value: GtlSearchFilter) {
GtlFilterKindCodec.write(buf, value.kind)
when (value) {
is GtlSpeciesFilter -> {
U16LE.write(buf, value.speciesIds.size)
value.speciesIds.forEach { S16LE.write(buf, it) }
}
is GtlRawFilter -> buf.writeBytes(value.payload)
}
}
}
@@ -1,4 +1,4 @@
package de.fiereu.openmmo.net.game.packets
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
@@ -0,0 +1,67 @@
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.Codec
import de.fiereu.bytecodec.ReadBuffer
import de.fiereu.bytecodec.S16LE
import de.fiereu.bytecodec.S32LE
import de.fiereu.bytecodec.S64LE
import de.fiereu.bytecodec.S8
import de.fiereu.bytecodec.U8
import de.fiereu.bytecodec.WriteBuffer
import de.fiereu.openmmo.net.game.codecs.PokemonCodec
sealed interface GtlSearchListing {
val listingId: Long
val price: Int
val listedAt: Int
val expiresAt: Int
val quantity: Short
}
// Every row names its own kind, so an own listings page can mix monsters and items.
// TODO(gtl): decode an own listings row. The old scraper reads three more bytes after the
// quantity and then an item id even for a monster row, but no captured page has one.
internal fun gtlSearchListingCodec(pageKind: GtlListKind): Codec<GtlSearchListing> =
object : Codec<GtlSearchListing> {
override fun read(buf: ReadBuffer): GtlSearchListing {
require(pageKind != GtlListKind.OWN_LISTINGS) { "own listing rows are not decoded yet" }
val listingId = S64LE.read(buf)
val rowKind = GtlListKindCodec.read(buf)
val price = S32LE.read(buf)
val listedAt = S32LE.read(buf)
val expiresAt = S32LE.read(buf)
val quantity = S16LE.read(buf)
if (rowKind == GtlListKind.ITEM) {
val itemId = S16LE.read(buf)
val itemState = S8.read(buf)
return GtlItemListing(listingId, price, listedAt, expiresAt, quantity, itemId, itemState)
}
val hasRecord = U8.read(buf) == 1
val pokemon = if (hasRecord) PokemonCodec.read(buf) else null
val stats = if (hasRecord) List(6) { S16LE.read(buf) } else emptyList()
return GtlPokemonListing(listingId, price, listedAt, expiresAt, quantity, pokemon, stats)
}
override fun write(buf: WriteBuffer, value: GtlSearchListing) {
S64LE.write(buf, value.listingId)
GtlListKindCodec.write(
buf,
if (value is GtlItemListing) GtlListKind.ITEM else GtlListKind.POKEMON,
)
S32LE.write(buf, value.price)
S32LE.write(buf, value.listedAt)
S32LE.write(buf, value.expiresAt)
S16LE.write(buf, value.quantity)
when (value) {
is GtlItemListing -> {
S16LE.write(buf, value.itemId)
S8.write(buf, value.itemState)
}
is GtlPokemonListing -> {
U8.write(buf, if (value.pokemon == null) 0 else 1)
value.pokemon?.let { PokemonCodec.write(buf, it) }
value.stats.forEach { S16LE.write(buf, it) }
}
}
}
}
@@ -0,0 +1,36 @@
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
import de.fiereu.bytecodec.S16LE
import de.fiereu.bytecodec.S32LE
import de.fiereu.bytecodec.S8
import de.fiereu.bytecodec.U8
import de.fiereu.bytecodec.listPrefixed
/** [requestId] is echoed from the request that asked for this page. */
data class GtlSearchPagePacket(
val requestId: Byte,
val listKind: GtlListKind,
val page: Short,
val totalMatches: Int,
val listings: List<GtlSearchListing>,
val quotes: List<GtlItemQuote>?,
)
object GtlSearchPagePacketCodec : PacketCodec<GtlSearchPagePacket>() {
override fun CodecScope<GtlSearchPagePacket>.body(): GtlSearchPagePacket {
val requestId = field(S8) { it.requestId }
val listKind = field(GtlListKindCodec) { it.listKind }
val page = field(S16LE) { it.page }
val totalMatches = field(S32LE) { it.totalMatches }
val listings = field(gtlSearchListingCodec(listKind).listPrefixed(U8)) { it.listings }
val quotes =
if (listKind == GtlListKind.ITEM) {
field(GtlItemQuoteCodec.listPrefixed(U8)) { it.quotes ?: emptyList() }
} else {
null
}
return GtlSearchPagePacket(requestId, listKind, page, totalMatches, listings, quotes)
}
}
@@ -0,0 +1,28 @@
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
import de.fiereu.bytecodec.S16LE
import de.fiereu.bytecodec.S8
import de.fiereu.bytecodec.U8
import de.fiereu.bytecodec.listPrefixed
/** [requestId] counts up per board and comes back in [GtlSearchPagePacket]. */
data class GtlSearchPageRequestPacket(
val requestId: Byte,
val listKind: GtlListKind,
val categoryId: Byte,
val page: Short,
val filters: List<GtlSearchFilter> = emptyList(),
)
object GtlSearchPageRequestPacketCodec : PacketCodec<GtlSearchPageRequestPacket>() {
override fun CodecScope<GtlSearchPageRequestPacket>.body(): GtlSearchPageRequestPacket {
val requestId = field(S8) { it.requestId }
val listKind = field(GtlListKindCodec) { it.listKind }
val categoryId = field(S8) { it.categoryId }
val page = field(S16LE) { it.page }
val filters = field(GtlSearchFilterCodec.listPrefixed(U8)) { it.filters }
return GtlSearchPageRequestPacket(requestId, listKind, categoryId, page, filters)
}
}
@@ -0,0 +1,6 @@
package de.fiereu.openmmo.net.game.packets.gtl
/** Limits a monster search to these dex ids. */
data class GtlSpeciesFilter(val speciesIds: List<Short>) : GtlSearchFilter {
override val kind = GtlFilterKind.SPECIES
}
@@ -1,4 +1,4 @@
package de.fiereu.openmmo.net.game.packets
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
@@ -0,0 +1,31 @@
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.Codec
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
import de.fiereu.bytecodec.S16LE
import de.fiereu.bytecodec.S32LE
import de.fiereu.bytecodec.U8
import de.fiereu.bytecodec.listPrefixed
data class TradeListing(
val typeId: Short,
val price: Int,
val fieldB: Int,
val fieldC: Int,
val fieldD: Int,
val entries: List<TradeListingEntry>,
)
internal val TradeListingCodec: Codec<TradeListing> =
object : PacketCodec<TradeListing>() {
override fun CodecScope<TradeListing>.body(): TradeListing {
val typeId = field(S16LE) { it.typeId }
val price = field(S32LE) { it.price }
val fieldB = field(S32LE) { it.fieldB }
val fieldC = field(S32LE) { it.fieldC }
val fieldD = field(S32LE) { it.fieldD }
val entries = field(TradeListingEntryCodec.listPrefixed(U8)) { it.entries }
return TradeListing(typeId, price, fieldB, fieldC, fieldD, entries)
}
}
@@ -0,0 +1,23 @@
package de.fiereu.openmmo.net.game.packets.gtl
import de.fiereu.bytecodec.Codec
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
import de.fiereu.bytecodec.S32LE
import de.fiereu.bytecodec.S64LE
data class TradeListingEntry(
val a: Int,
val b: Int,
val c: Long,
)
internal val TradeListingEntryCodec: Codec<TradeListingEntry> =
object : PacketCodec<TradeListingEntry>() {
override fun CodecScope<TradeListingEntry>.body(): TradeListingEntry {
val a = field(S32LE) { it.a }
val b = field(S32LE) { it.b }
val c = field(S64LE) { it.c }
return TradeListingEntry(a, b, c)
}
}
@@ -0,0 +1,15 @@
package de.fiereu.openmmo.net.game
import de.fiereu.bytecodec.ByteArrayReadBuffer
private object Fixtures
/**
* Reads a captured payload from `src/test/resources/fixtures`. The path is
* `<server>/<direction>/<packet id>/<name>.bin` and the name ends in the client version the capture
* came from.
*/
fun fixture(path: String): ByteArrayReadBuffer =
ByteArrayReadBuffer(
Fixtures.javaClass.getResourceAsStream("/fixtures/$path")?.readBytes()
?: error("no fixture at /fixtures/$path"))
@@ -0,0 +1,46 @@
package de.fiereu.openmmo.net.game
import de.fiereu.openmmo.net.game.packets.gtl.GtlListKind
import de.fiereu.openmmo.net.game.packets.gtl.GtlListingsPagePacketCodec
import de.fiereu.openmmo.net.game.packets.gtl.GtlListingsPageRequestPacketCodec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
class GtlListingsPagePacketTest :
FunSpec({
test("a captured price list request round trips") {
val buf = fixture("game/c2s/e4/price_list_request_31914.bin")
val request = GtlListingsPageRequestPacketCodec.read(buf)
request.requestId shouldBe 3.toByte()
request.listKind shouldBe GtlListKind.ITEM
request.categoryId shouldBe 0.toByte()
request.page shouldBe 2.toShort()
buf.remaining() shouldBe 0
}
test("a captured price list page is consumed to the last byte") {
val buf = fixture("game/s2c/fe/price_history_32710.bin")
val page = GtlListingsPagePacketCodec.read(buf)
page.requestId shouldBe 1.toByte()
page.listKind shouldBe GtlListKind.ITEM
page.page shouldBe 0.toShort()
page.totalCount shouldBe 1184
page.listings.size shouldBe 10
page.listings.first().typeId shouldBe 5072.toShort()
page.listings.first().price shouldBe 1773
page.listings.first().entries.size shouldBe 1
buf.remaining() shouldBe 0
}
test("a later price list page reports which page it answers") {
val buf = fixture("game/s2c/fe/price_history_page2_31914.bin")
val page = GtlListingsPagePacketCodec.read(buf)
page.requestId shouldBe 3.toByte()
page.page shouldBe 2.toShort()
page.listings.size shouldBe 10
buf.remaining() shouldBe 0
}
})
@@ -0,0 +1,34 @@
package de.fiereu.openmmo.net.game
import de.fiereu.bytecodec.ByteArrayReadBuffer
import de.fiereu.openmmo.net.game.packets.gtl.GtlMarketListingsPacketCodec
import de.fiereu.openmmo.net.game.packets.gtl.GtlMarketListingsRequestPacketCodec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
class GtlMarketListingsPacketTest :
FunSpec({
test("opening the board asks for every listing with an empty body") {
val request = ByteArrayReadBuffer(ByteArray(0))
GtlMarketListingsRequestPacketCodec.read(request)
request.remaining() shouldBe 0
}
test("a captured board page is consumed to the last byte") {
val buf = fixture("game/s2c/70/board_page_31914.bin")
val board = GtlMarketListingsPacketCodec.read(buf)
board.fullReset shouldBe false
board.applyToBoards shouldBe true
// The capture cuts the last row short, so it holds 7 rows and not the 8 it declares.
board.listings.size shouldBe 7
val first = board.listings.first()
first.listingId shouldBe 301
first.speciesId shouldBe 3384.toShort()
first.value1 shouldBe 2000
first.ownListing shouldBe true
first.rowIndex shouldBe -1
buf.remaining() shouldBe 0
}
})
@@ -0,0 +1,31 @@
package de.fiereu.openmmo.net.game
import de.fiereu.bytecodec.ByteArrayReadBuffer
import de.fiereu.openmmo.common.utils.hexToBytes
import de.fiereu.openmmo.net.game.packets.CategoryFlagsPacketCodec
import de.fiereu.openmmo.net.game.packets.gtl.GtlOpenSessionPacketCodec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
class GtlOpenSessionPacketTest :
FunSpec({
test("the first open carries no timestamp") {
val buf = fixture("game/c2s/a5/open_session_first_31914.bin")
GtlOpenSessionPacketCodec.read(buf).sessionTimestamp shouldBe 0L
buf.remaining() shouldBe 0
}
test("the next open echoes the timestamp the server just sent") {
// The flag bytes are opaque, so only the header comes from the capture.
val flagBytes = ("01" + "85C2F6559E010000" + "3603" + "00".repeat(822)).hexToBytes()
val flagBuf = ByteArrayReadBuffer(flagBytes)
val flags = CategoryFlagsPacketCodec.read(flagBuf)
flags.entryKind shouldBe 1
flags.flagBits.size shouldBe 822
flagBuf.remaining() shouldBe 0
val reopen = fixture("game/c2s/a5/open_session_reopen_31914.bin")
GtlOpenSessionPacketCodec.read(reopen).sessionTimestamp shouldBe flags.timestampMillis
reopen.remaining() shouldBe 0
}
})
@@ -0,0 +1,65 @@
package de.fiereu.openmmo.net.game
import de.fiereu.openmmo.net.game.packets.gtl.GtlItemListing
import de.fiereu.openmmo.net.game.packets.gtl.GtlListKind
import de.fiereu.openmmo.net.game.packets.gtl.GtlPokemonListing
import de.fiereu.openmmo.net.game.packets.gtl.GtlSearchPagePacketCodec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
class GtlSearchPagePacketTest :
FunSpec({
test("a captured item page is consumed to the last byte") {
val buf = fixture("game/s2c/9b/item_page_32710.bin")
val page = GtlSearchPagePacketCodec.read(buf)
page.requestId shouldBe 7.toByte()
page.listKind shouldBe GtlListKind.ITEM
page.page shouldBe 0.toShort()
page.totalMatches shouldBe 73
page.listings.size shouldBe 10
val first = page.listings.first() as GtlItemListing
first.listingId shouldBe 0x1ACFDD4BEA482000L
first.price shouldBe 1900
first.quantity shouldBe 2.toShort()
first.itemId shouldBe 1111.toShort()
page.quotes!!.map { it.itemId } shouldBe listOf<Short>(1118, 1116, 1115, 1114, 1113, 1111)
buf.remaining() shouldBe 0
}
test("a captured monster page is consumed to the last byte") {
val buf = fixture("game/s2c/9b/monster_page_32710.bin")
val page = GtlSearchPagePacketCodec.read(buf)
page.requestId shouldBe 1.toByte()
page.listKind shouldBe GtlListKind.POKEMON
page.totalMatches shouldBe 216090
page.listings.size shouldBe 10
val first = page.listings.first() as GtlPokemonListing
first.listingId shouldBe 0x1ACFF6ACDDC82000L
first.price shouldBe 11000
first.pokemon!!.dexId shouldBe 246
first.pokemon!!.ot shouldBe "DraganwMax"
first.pokemon!!.level shouldBe 54
// The board repeats the record's own hp as the first stat.
first.stats shouldBe listOf<Short>(127, 88, 68, 64, 61, 68)
first.pokemon!!.hp shouldBe 127
page.quotes shouldBe null
buf.remaining() shouldBe 0
}
test("a page that is not an item board carries no quote strip") {
val buf = fixture("game/s2c/9b/own_listings_page_31914.bin")
val page = GtlSearchPagePacketCodec.read(buf)
page.listKind shouldBe GtlListKind.OWN_LISTINGS
page.totalMatches shouldBe 0
page.listings shouldBe emptyList()
page.quotes shouldBe null
buf.remaining() shouldBe 0
}
})
@@ -0,0 +1,66 @@
package de.fiereu.openmmo.net.game
import de.fiereu.openmmo.net.game.packets.gtl.GtlFilterKind
import de.fiereu.openmmo.net.game.packets.gtl.GtlListKind
import de.fiereu.openmmo.net.game.packets.gtl.GtlSearchPageRequestPacketCodec
import de.fiereu.openmmo.net.game.packets.gtl.GtlSpeciesFilter
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
class GtlSearchPageRequestPacketTest :
FunSpec({
test("an unfiltered search carries no filters") {
val buf = fixture("game/c2s/9b/own_listings_request_31914.bin")
val request = GtlSearchPageRequestPacketCodec.read(buf)
request.requestId shouldBe 3.toByte()
request.listKind shouldBe GtlListKind.OWN_LISTINGS
request.categoryId shouldBe 13.toByte()
request.page shouldBe 0.toShort()
request.filters shouldBe emptyList()
buf.remaining() shouldBe 0
}
test("a search filtered to one species carries its dex id") {
val buf = fixture("game/c2s/9b/one_species_filter_32710.bin")
val request = GtlSearchPageRequestPacketCodec.read(buf)
request.requestId shouldBe 4.toByte()
request.filters shouldBe listOf(GtlSpeciesFilter(listOf<Short>(132)))
buf.remaining() shouldBe 0
}
test("a search filtered to thirteen species carries all of them") {
val buf = fixture("game/c2s/9b/thirteen_species_filter_32710.bin")
val request = GtlSearchPageRequestPacketCodec.read(buf)
request.listKind shouldBe GtlListKind.ITEM
val species = request.filters.single() as GtlSpeciesFilter
species.speciesIds.size shouldBe 13
species.speciesIds.first() shouldBe 1118.toShort()
buf.remaining() shouldBe 0
}
test("a filtered search carries one entry per filter") {
val buf = fixture("game/c2s/9b/eleven_filters_32710.bin")
val request = GtlSearchPageRequestPacketCodec.read(buf)
request.filters.size shouldBe 11
request.filters.first() shouldBe GtlSpeciesFilter(listOf<Short>(132))
request.filters.map { it.kind } shouldBe
listOf(
GtlFilterKind.SPECIES,
GtlFilterKind.UNKNOWN_13,
GtlFilterKind.UNKNOWN_12,
GtlFilterKind.NATURE,
GtlFilterKind.GENDER,
GtlFilterKind.IV,
GtlFilterKind.IV,
GtlFilterKind.IV,
GtlFilterKind.IV,
GtlFilterKind.IV,
GtlFilterKind.IV,
)
buf.remaining() shouldBe 0
}
})
@@ -0,0 +1,20 @@
package de.fiereu.openmmo.net.game
import de.fiereu.bytecodec.ByteArrayReadBuffer
import de.fiereu.openmmo.net.game.packets.SceneObjectStatesPacketCodec
import de.fiereu.openmmo.net.game.packets.gtl.GtlTradeLogRequestPacketCodec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
class GtlTradeLogRequestPacketTest :
FunSpec({
test("an empty trade log is a single zero byte") {
val request = ByteArrayReadBuffer(ByteArray(0))
GtlTradeLogRequestPacketCodec.read(request)
request.remaining() shouldBe 0
val reply = fixture("game/s2c/5e/empty_trade_log_31914.bin")
SceneObjectStatesPacketCodec.read(reply).objects shouldBe emptyList()
reply.remaining() shouldBe 0
}
})
@@ -0,0 +1,14 @@
# Fixtures
Raw packet payloads from real captures, one file per packet.
<server>/<direction>/<packet id>/<name>_<client version>.bin
Packet id is lowercase hex without a prefix. The version matters: the wire format changes between
clients.
```kotlin
val buf = fixture("game/s2c/9b/monster_page_32710.bin")
GtlSearchPagePacketCodec.read(buf)
buf.remaining() shouldBe 0
```
@@ -33,7 +33,6 @@ import de.fiereu.openmmo.net.game.packets.battle.BattleActionSelectPacket
import de.fiereu.openmmo.net.game.packets.battle.BattleActionSubmitPacket
import de.fiereu.openmmo.net.game.packets.battle.BattleAppearancePacket
import de.fiereu.openmmo.net.game.packets.battle.BattleCancelRequestPacket
import de.fiereu.openmmo.net.game.packets.battle.BattleChallengeRequestPacket
import de.fiereu.openmmo.net.game.packets.battle.BattleChatMessagePacket
import de.fiereu.openmmo.net.game.packets.battle.BattleLeavePacket
import de.fiereu.openmmo.net.game.packets.battle.BattlePartySlotSelectPacket
@@ -153,7 +152,6 @@ constructor(
on<BattleTransitionReadyPacket> { event -> battleService.onBattlePacket(event) }
on<BattleTeamPreviewConfirmPacket> { event -> battleService.onBattlePacket(event) }
on<BattleRewardSelectPacket> { event -> battleService.onBattlePacket(event) }
on<BattleChallengeRequestPacket> { event -> battleService.onBattlePacket(event) }
on<MapLoadedAckPacket> { event -> battleService.onClientReady(event) }
// The client sends an empty heartbeat packet.
@@ -9,7 +9,6 @@ import de.fiereu.openmmo.net.game.packets.KeepAlivePacket
import de.fiereu.openmmo.net.game.packets.MovementPacket
import de.fiereu.openmmo.net.game.packets.RemoveFriendPacket
import de.fiereu.openmmo.net.game.packets.battle.BattleActionPacket
import de.fiereu.openmmo.net.game.packets.battle.BattleChallengeRequestPacket
import de.fiereu.openmmo.server.game.config.GameServerConfig
import de.fiereu.openmmo.server.game.di.DaggerGameServerComponent
import io.kotest.core.spec.style.FunSpec
@@ -37,6 +36,5 @@ class GameAppHandlerTest :
handler.isRegistered(BlockPlayerPacket::class) shouldBe true
handler.isRegistered(DeleteCharacterPacket::class) shouldBe true
handler.isRegistered(BattleActionPacket::class) shouldBe true
handler.isRegistered(BattleChallengeRequestPacket::class) shouldBe true
}
})