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)
}
}