diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/CharacterInfo.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/CharacterInfo.kt new file mode 100644 index 0000000..48508a2 --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/CharacterInfo.kt @@ -0,0 +1,29 @@ +package de.fiereu.openmmo.common + +import java.time.LocalDateTime + +data class CharacterInfo( + val id: Long, + val name: String, + val namePrefix: String = "", + val userId: Int, + val rivalSex: Byte, + val lastLogin: LocalDateTime, + val createdAt: LocalDateTime, + val money: Int, + val permissions: Int, + val remainingSafariSteps: Short, + val remainingSafariBalls: Byte, + val pcExtraSlots: Byte, + val battleBoxExtraSlots: Byte, + val templateAmount: Byte, + val positionRegionId: Byte, + val positionBankId: Byte, + val positionMapId: Byte, + val positionX: Short, + val positionY: Short, + val repelLeft: Short, + val repelItemId: Short, + val lureLeft: Short, + val lureItemId: Short +) diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/Pokemon.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/Pokemon.kt new file mode 100644 index 0000000..b91c7d6 --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/Pokemon.kt @@ -0,0 +1,38 @@ +package de.fiereu.openmmo.common + +import de.fiereu.openmmo.common.enums.EVs +import de.fiereu.openmmo.common.enums.IVs +import de.fiereu.openmmo.common.enums.PokemonContainer +import de.fiereu.openmmo.common.enums.PokemonNature +import java.time.LocalDateTime + +data class Pokemon( + val id: Long, + val container: PokemonContainer, + val containerSlot: Short, + val dexId: Int, + val seed: Int, + val ot: String, + val nickname: String, + val level: Byte, + val hp: Short, + val xp: Int, + val eVs: EVs, + val iVs: IVs, + val moves: List, + val isShiny: Boolean, + val hasHiddenAbility: Boolean, + val isAlpha: Boolean, + val isSecret: Boolean, + val isFatefulEncounter: Boolean, + val isRaidEncounter: Boolean, + val caughtAt: LocalDateTime +) { + val nature: PokemonNature = PokemonNature.entries[seed % PokemonNature.entries.size] + + init { + require(moves.size <= 4) { "A Pokemon can't have more than 4 moves" } + } +} + +data class PokemonMove(val id: Short, var pp: Byte) diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/Tile2D.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/Tile2D.kt new file mode 100644 index 0000000..71492f8 --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/Tile2D.kt @@ -0,0 +1,3 @@ +package de.fiereu.openmmo.common + +data class Tile2D(val material: Short, val collision: Byte) diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/ChatType.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/ChatType.kt new file mode 100644 index 0000000..be22a60 --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/ChatType.kt @@ -0,0 +1,23 @@ +package de.fiereu.openmmo.common.enums + +enum class ChatType { + NORMAL, + UNK1, + UNK2, + SHOUT, + WHISPER, + TRADE, + GLOBAL, + CHANNEL, + TEAM, + LINK, + UNK3, + UNK4, + UNK5, + UNK6, + UNK7, + UNK8, + SYSTEM_ANNOUNCEMENTS, + GAME_NOTIFICATIONS, + BATTLE +} diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Direction.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Direction.kt new file mode 100644 index 0000000..4d575ba --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Direction.kt @@ -0,0 +1,8 @@ +package de.fiereu.openmmo.common.enums + +enum class Direction { + NORTH, + SOUTH, + EAST, + WEST +} diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/EncounterType.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/EncounterType.kt new file mode 100644 index 0000000..a8195e8 --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/EncounterType.kt @@ -0,0 +1,13 @@ +package de.fiereu.openmmo.common.enums + +enum class EncounterType { + RANDOM, + GYM_STYLE, + VILLAIN_TEAM_STYLE, + UNKNOWN_0x03, + TOP_4_1, + TOP_4_2, + TOP_4_3, + TOP_4_4, + BIG_RED_BALL +} diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/EntityStatus.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/EntityStatus.kt new file mode 100644 index 0000000..b20de7e --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/EntityStatus.kt @@ -0,0 +1,12 @@ +package de.fiereu.openmmo.common.enums + +enum class EntityStatus { + NONE, + WILD, + PVP, + SPECTATE, + WANTS_REMATCH, + NO_REMATCH, + TRAINER_AGGRO, + DISCONNECTED +} diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Flavor.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Flavor.kt new file mode 100644 index 0000000..dbdfa21 --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Flavor.kt @@ -0,0 +1,11 @@ +package de.fiereu.openmmo.common.enums + +enum class Flavor { + SPICY, + DRY, + SWEET, + BITTER, + SOUR, + /* IDK about this one. The client uses it with id -128 */ + NONE +} diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Lighting.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Lighting.kt new file mode 100644 index 0000000..c42db2c --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Lighting.kt @@ -0,0 +1,7 @@ +package de.fiereu.openmmo.common.enums + +enum class Lighting { + REGULAR, + DARK_FLASH_USABLE, + DARK_FLASH_UNUSABLE +} diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/MapType.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/MapType.kt new file mode 100644 index 0000000..6d69c8f --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/MapType.kt @@ -0,0 +1,14 @@ +package de.fiereu.openmmo.common.enums + +enum class MapType { + UNKNOWN_0x00, + UNKNOWN_0x01, + CITY, + ROUTE, + UNDERGROUND, + UNDERWATER, + UNKNOWN_0x06, + UNKNOWN_0x07, + INSIDE, + SECRET_BASE +} diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/PokemonContainer.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/PokemonContainer.kt new file mode 100644 index 0000000..8fa168e --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/PokemonContainer.kt @@ -0,0 +1,14 @@ +package de.fiereu.openmmo.common.enums + +enum class PokemonContainer { + PC, + PARTY, + TRADE, + DAYCARE, + RENTAL_PARTY, + MAIL, + AUCTION, + VOID, + DELETED, + EVENT +} diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/PokemonNature.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/PokemonNature.kt new file mode 100644 index 0000000..5d2db9e --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/PokemonNature.kt @@ -0,0 +1,34 @@ +package de.fiereu.openmmo.common.enums + +enum class PokemonNature( + val increasedStat: PokemonStat, + val decreasedStat: PokemonStat, + val favoriteFlavor: Flavor, + val dislikedFlavor: Flavor +) { + HARDY(PokemonStat.ATTACK, PokemonStat.ATTACK, Flavor.SPICY, Flavor.SPICY), + LONELY(PokemonStat.ATTACK, PokemonStat.DEFENSE, Flavor.SPICY, Flavor.SOUR), + BRAVE(PokemonStat.ATTACK, PokemonStat.SPEED, Flavor.SPICY, Flavor.SWEET), + ADAMANT(PokemonStat.ATTACK, PokemonStat.SP_ATTACK, Flavor.SPICY, Flavor.DRY), + NAUGHTY(PokemonStat.ATTACK, PokemonStat.SP_DEFENSE, Flavor.SPICY, Flavor.BITTER), + BOLD(PokemonStat.DEFENSE, PokemonStat.ATTACK, Flavor.SOUR, Flavor.SPICY), + DOCILE(PokemonStat.DEFENSE, PokemonStat.DEFENSE, Flavor.SOUR, Flavor.SOUR), + RELAXED(PokemonStat.DEFENSE, PokemonStat.SPEED, Flavor.SOUR, Flavor.SWEET), + IMPISH(PokemonStat.DEFENSE, PokemonStat.SP_ATTACK, Flavor.SOUR, Flavor.DRY), + LAX(PokemonStat.DEFENSE, PokemonStat.SP_DEFENSE, Flavor.SOUR, Flavor.BITTER), + TIMID(PokemonStat.SPEED, PokemonStat.ATTACK, Flavor.SWEET, Flavor.SPICY), + HASTY(PokemonStat.SPEED, PokemonStat.DEFENSE, Flavor.SWEET, Flavor.SOUR), + SERIOUS(PokemonStat.SPEED, PokemonStat.SPEED, Flavor.SWEET, Flavor.SWEET), + JOLLY(PokemonStat.SPEED, PokemonStat.SP_ATTACK, Flavor.SWEET, Flavor.DRY), + NAIVE(PokemonStat.SPEED, PokemonStat.SP_DEFENSE, Flavor.SWEET, Flavor.BITTER), + MODEST(PokemonStat.SP_ATTACK, PokemonStat.ATTACK, Flavor.DRY, Flavor.SPICY), + MILD(PokemonStat.SP_ATTACK, PokemonStat.DEFENSE, Flavor.DRY, Flavor.SOUR), + QUIET(PokemonStat.SP_ATTACK, PokemonStat.SPEED, Flavor.DRY, Flavor.SWEET), + BASHFUL(PokemonStat.SP_ATTACK, PokemonStat.SP_ATTACK, Flavor.DRY, Flavor.DRY), + RASH(PokemonStat.SP_ATTACK, PokemonStat.SP_DEFENSE, Flavor.DRY, Flavor.BITTER), + CALM(PokemonStat.SP_DEFENSE, PokemonStat.ATTACK, Flavor.BITTER, Flavor.SPICY), + GENTLE(PokemonStat.SP_DEFENSE, PokemonStat.DEFENSE, Flavor.BITTER, Flavor.SOUR), + SASSY(PokemonStat.SP_DEFENSE, PokemonStat.SPEED, Flavor.BITTER, Flavor.SWEET), + CAREFUL(PokemonStat.SP_DEFENSE, PokemonStat.SP_ATTACK, Flavor.BITTER, Flavor.DRY), + QUIRKY(PokemonStat.SP_DEFENSE, PokemonStat.SP_DEFENSE, Flavor.BITTER, Flavor.BITTER) +} diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/PokemonRarity.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/PokemonRarity.kt new file mode 100644 index 0000000..19ce5f5 --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/PokemonRarity.kt @@ -0,0 +1,10 @@ +package de.fiereu.openmmo.common.enums + +enum class PokemonRarity { + SHINY, + HIDDEN_ABILITY, + ALPHA, + SECRET, + FATEFUL_ENCOUNTER, + RAID_ENCOUNTER +} diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/PokemonStats.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/PokemonStats.kt new file mode 100644 index 0000000..d89ca54 --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/PokemonStats.kt @@ -0,0 +1,138 @@ +package de.fiereu.openmmo.common.enums + +import java.util.* + +enum class PokemonStat { + HP, + ATTACK, + DEFENSE, + SP_ATTACK, + SP_DEFENSE, + SPEED +} + +open class PokemonStats(private val individualCap: Int, private val totalCap: Int) : + EnumMap(PokemonStat.entries.associateWith { 0.toByte() }) { + + val total + get() = hp + atk + def + spAtk + spDef + spd + + private fun cr(value: Int): Byte { + if (value <= 0) error("Value can't be <= 0") + if (value > individualCap) error("Value exceeds individual capacity") + if (total > totalCap) error("Total value exceeds capacity") + return value.toByte() + } + + var hp + get() = get(PokemonStat.HP)!!.toInt() + set(value) = set(PokemonStat.HP, cr(value)) + + var atk + get() = get(PokemonStat.ATTACK)!!.toInt() + set(value) = set(PokemonStat.ATTACK, cr(value)) + + var def + get() = get(PokemonStat.DEFENSE)!!.toInt() + set(value) = set(PokemonStat.DEFENSE, cr(value)) + + var spAtk + get() = get(PokemonStat.SP_ATTACK)!!.toInt() + set(value) = set(PokemonStat.SP_ATTACK, cr(value)) + + var spDef + get() = get(PokemonStat.SP_DEFENSE)!!.toInt() + set(value) = set(PokemonStat.SP_DEFENSE, cr(value)) + + var spd + get() = get(PokemonStat.SPEED)!!.toInt() + set(value) = set(PokemonStat.SPEED, cr(value)) +} + +class EVs : PokemonStats(252, 510) + +class IVs : PokemonStats(31, 155) + +fun IVs.compress(): Int = + (((hp and 31) shl 0) or + ((atk and 31) shl 5) or + ((def and 31) shl 10) or + ((spAtk and 31) shl 15) or + ((spDef and 31) shl 20) or + ((spd and 31) shl 25)) + +fun decompressIVs(value: Int): IVs = + IVs().apply { + hp = (value shr 0) and 31 + atk = (value shr 5) and 31 + def = (value shr 10) and 31 + spAtk = (value shr 15) and 31 + spDef = (value shr 20) and 31 + spd = (value shr 25) and 31 + } + +enum class PokemonBattleStat { + HP, + ATTACK, + DEFENSE, + SP_ATTACK, + SP_DEFENSE, + SPEED, + ACCURACY, + EVASION +} + +// Not sure if this is correct just copying from EVs here +class PokemonBattleStats : + EnumMap(PokemonBattleStat.entries.associateWith { 0.toByte() }) { + private val individualCap: Int = 252 + private val totalCap: Int = 510 + + val total + get() = hp + atk + def + spAtk + spDef + spd + acc + evs + + private fun cr(value: Int): Byte { + if (value <= 0) error("Value can't be <= 0") + if (value > individualCap) error("Value exceeds individual capacity") + if (total > totalCap) error("Total value exceeds capacity") + return value.toByte() + } + + var hp + get() = get(PokemonBattleStat.HP)!!.toInt() + set(value) = set(PokemonBattleStat.HP, cr(value)) + + var atk + get() = get(PokemonBattleStat.ATTACK)!!.toInt() + set(value) = set(PokemonBattleStat.ATTACK, cr(value)) + + var def + get() = get(PokemonBattleStat.DEFENSE)!!.toInt() + set(value) = set(PokemonBattleStat.DEFENSE, cr(value)) + + var spAtk + get() = get(PokemonBattleStat.SP_ATTACK)!!.toInt() + set(value) = set(PokemonBattleStat.SP_ATTACK, cr(value)) + + var spDef + get() = get(PokemonBattleStat.SP_DEFENSE)!!.toInt() + set(value) = set(PokemonBattleStat.SP_DEFENSE, cr(value)) + + var spd + get() = get(PokemonBattleStat.SPEED)!!.toInt() + set(value) = set(PokemonBattleStat.SPEED, cr(value)) + + var acc + get() = get(PokemonBattleStat.ACCURACY)!!.toInt() + set(value) = set(PokemonBattleStat.SPEED, value.toByte()) + + var evs + get() = get(PokemonBattleStat.EVASION)!!.toInt() + set(value) = set(PokemonBattleStat.EVASION, value.toByte()) + + init { + // just guessing that "252 / 2" => 100% + acc = 252 / 2 + evs = 252 / 2 + } +} diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/PokemonType.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/PokemonType.kt new file mode 100644 index 0000000..773d00e --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/PokemonType.kt @@ -0,0 +1,23 @@ +package de.fiereu.openmmo.common.enums + +enum class PokemonType { + NORMAL, + FIGHTING, + FLYING, + POISON, + GROUND, + ROCK, + BUG, + GHOST, + STEEL, + QUESTIONQUESTIONQUESTION, + FIRE, + WATER, + GRASS, + ELECTRIC, + PSYCHIC, + ICE, + DRAGON, + DARK, + NONE +} diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/SkinSlot.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/SkinSlot.kt new file mode 100644 index 0000000..ffa3e61 --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/SkinSlot.kt @@ -0,0 +1,16 @@ +package de.fiereu.openmmo.common.enums + +enum class SkinSlot { + FOREHEAD, + HAT, + HAIR, + EYES, + FACIAL_HAIR, + BACK, + TOP, + GLOVES, + FOOTWEAR, + LEGGINGS, + FISHING_ROD, + BIKE +} diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Weather.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Weather.kt new file mode 100644 index 0000000..00fa4eb --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Weather.kt @@ -0,0 +1,61 @@ +package de.fiereu.openmmo.common.enums + +enum class Weather(val battleWeather: BattleWeather = BattleWeather.NORMAL) { + IN_HOUSE_WEATHER, + SUNNY_WEATHER_WITH_CLOUDS_IN_WATER, + REGULAR_WEATHER, + RAINY_WEATHER(BattleWeather.SHITTY_RAIN), + THREE_SNOW_FLAKES(BattleWeather.SNOW), + RAIN_WITH_THUNDER(BattleWeather.SHITTY_RAIN), + STEADY_MIST, + STEADY_SNOW, + SAND_STORM(BattleWeather.SANDSTORM), + MIST_FROM_TOP_RIGHT, + DENSE_BRIGHT_MIST, + CLOUDY, + UNDERGROUND_FLASHES, + HEAVY_RAIN_WITH_THUNDER(BattleWeather.SHITTY_RAIN), + UNDERWATER_MIST, + UNKNOWN_THUNDER(BattleWeather.SHITTY_RAIN), + DAY_DEPENDANT, + CUSTOM_SNOW, + GEN4_NONE, + GEN4_UNK0, + GEN4_RAIN(BattleWeather.SHITTY_RAIN), + GEN4_HEAVY_RAIN(BattleWeather.SHITTY_RAIN), + GEN4_HEAVY_RAIN_WITH_THUNDER(BattleWeather.SHITTY_RAIN), + GEN4_SNOW(BattleWeather.SHITTY_RAIN), + GEN4_HEAVY_SNOW(BattleWeather.SHITTY_RAIN), + GEN4_HAIL(BattleWeather.SNOW), + GEN4_CLEAR, + GEN4_ASHDUST, + GEN4_SANDSTORM(BattleWeather.SANDSTORM), + GEN4_SPECIAL_ICY, + GEN4_SPECIAL_ROCKS, + GEN4_UNK, + GEN4_HEAVY_FOG(BattleWeather.FOG), + GEN4_HEAVY_FOG_WITH_DARKNESS, + GEN4_CAVE_FLASH, + GEN4_FOREST_TREE_SHADOWS, + GEN4_DARKNESS, + GEN4_GREEN_HAZE, + GEN4_RED_HAZE, + GEN4_BLUE_HAZE, + GEN4_RAIN2, + GEN4_UNK2, + GEN4_HEAVY_SNOW2, + GEN4_HEAVY_SNOW3, + GEN4_SNOW2 +} + +enum class BattleWeather { + NORMAL, + SUNNY, + RAIN, + SANDSTORM, + FOG, + SNOW, + SHITTY_RAIN, + EVENT_HALLOWEEN_STORM, + EVENT_CNY_DARKNESS +} diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/utils/Gzip.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/utils/Gzip.kt new file mode 100644 index 0000000..78c229b --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/utils/Gzip.kt @@ -0,0 +1,17 @@ +package de.fiereu.openmmo.common.utils + +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.util.zip.GZIPInputStream +import java.util.zip.GZIPOutputStream + +fun ByteArray.gzipCompress(): ByteArray { + val baos = ByteArrayOutputStream() + val gzip = GZIPOutputStream(baos) + gzip.write(this) + gzip.close() + return baos.toByteArray() +} + +fun ByteArray.gzipDecompress(): ByteArray = + GZIPInputStream(ByteArrayInputStream(this)).buffered().readBytes() diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/utils/Regions.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/utils/Regions.kt new file mode 100644 index 0000000..8accad8 --- /dev/null +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/utils/Regions.kt @@ -0,0 +1,5 @@ +package de.fiereu.openmmo.common.utils + +fun isNdsRegion(regionId: Int): Boolean { + return regionId == 2 || regionId == 3 || regionId == 4 || regionId == 10 +} diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/GameProtocol.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/GameProtocol.kt index 427b5fa..32aac49 100644 --- a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/GameProtocol.kt +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/GameProtocol.kt @@ -1,12 +1,30 @@ package de.fiereu.openmmo.protocols.game import de.fiereu.openmmo.protocols.Protocol -import de.fiereu.openmmo.protocols.game.packets.JoinGamePacket +import de.fiereu.openmmo.protocols.game.packets.CharacterListDeserializer +import de.fiereu.openmmo.protocols.game.packets.CharacterListSerializer +import de.fiereu.openmmo.protocols.game.packets.ChatMessageDeserialize +import de.fiereu.openmmo.protocols.game.packets.ChatMessageSerialize import de.fiereu.openmmo.protocols.game.packets.JoinPacketDeserializer import de.fiereu.openmmo.protocols.game.packets.JoinPacketSerializer -import de.fiereu.openmmo.protocols.game.packets.JoinResponsePacket -import de.fiereu.openmmo.protocols.game.packets.JoinResponsePacketDeserializer -import de.fiereu.openmmo.protocols.game.packets.JoinResponsePacketSerializer +import de.fiereu.openmmo.protocols.game.packets.JoinResponseDeserializer +import de.fiereu.openmmo.protocols.game.packets.JoinResponseSerializer +import de.fiereu.openmmo.protocols.game.packets.LoadEntityDeserializer +import de.fiereu.openmmo.protocols.game.packets.LoadEntitySerializer +import de.fiereu.openmmo.protocols.game.packets.LoadMapPacketDeserializer +import de.fiereu.openmmo.protocols.game.packets.LoadMapPacketSerializer +import de.fiereu.openmmo.protocols.game.packets.PokemonContainerDeserializer +import de.fiereu.openmmo.protocols.game.packets.PokemonContainerSerializer +import de.fiereu.openmmo.protocols.game.packets.RenderScreenDeserializer +import de.fiereu.openmmo.protocols.game.packets.RenderScreenSerializer +import de.fiereu.openmmo.protocols.game.packets.RequestCharactersDeserializer +import de.fiereu.openmmo.protocols.game.packets.RequestCharactersPacketSerializer +import de.fiereu.openmmo.protocols.game.packets.RequestPlayerDeserializePacket +import de.fiereu.openmmo.protocols.game.packets.RequestPlayerSerializer +import de.fiereu.openmmo.protocols.game.packets.SelectCharacterDeserializer +import de.fiereu.openmmo.protocols.game.packets.SelectCharacterSerializer +import de.fiereu.openmmo.protocols.game.packets.SelectedCharacterDeserializer +import de.fiereu.openmmo.protocols.game.packets.SelectedCharacterSerializer import de.fiereu.openmmo.protocols.incomingPacket import de.fiereu.openmmo.protocols.outgoingPacket @@ -15,22 +33,40 @@ import de.fiereu.openmmo.protocols.outgoingPacket * is complete. */ abstract class GameProtocol : Protocol() { - override val async: Boolean - get() = true - override val compressed: Boolean - get() = true + override val async: Boolean = true + override val compressed: Boolean = true } class GameServerProtocol : GameProtocol() { init { incomingPacket(0x01u, JoinPacketDeserializer()) - outgoingPacket(0x01u, JoinResponsePacketSerializer(), JoinResponsePacket::class) + outgoingPacket(0x01u, JoinResponseSerializer()) + incomingPacket(0x02u, RequestCharactersDeserializer()) + outgoingPacket(0x02u, CharacterListSerializer()) + incomingPacket(0x04u, SelectCharacterDeserializer()) + outgoingPacket(0x04u, SelectedCharacterSerializer()) + incomingPacket(0x05u, RequestPlayerDeserializePacket()) + outgoingPacket(0x05u, LoadEntitySerializer()) + outgoingPacket(0x09u, ChatMessageSerialize()) + outgoingPacket(0x10u, LoadMapPacketSerializer()) + outgoingPacket(0x13u, PokemonContainerSerializer()) + outgoingPacket(0xB4u, RenderScreenSerializer()) } } class GameClientProtocol : GameProtocol() { init { - outgoingPacket(0x01u, JoinPacketSerializer(), JoinGamePacket::class) - incomingPacket(0x01u, JoinResponsePacketDeserializer()) + outgoingPacket(0x01u, JoinPacketSerializer()) + incomingPacket(0x01u, JoinResponseDeserializer()) + outgoingPacket(0x02u, RequestCharactersPacketSerializer()) + incomingPacket(0x02u, CharacterListDeserializer()) + outgoingPacket(0x04u, SelectCharacterSerializer()) + incomingPacket(0x04u, SelectedCharacterDeserializer()) + outgoingPacket(0x05u, RequestPlayerSerializer()) + incomingPacket(0x05u, LoadEntityDeserializer()) + incomingPacket(0x09u, ChatMessageDeserialize()) + incomingPacket(0x10u, LoadMapPacketDeserializer()) + incomingPacket(0x13u, PokemonContainerDeserializer()) + incomingPacket(0xB4u, RenderScreenDeserializer()) } } diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/CharactersListPacket.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/CharactersListPacket.kt new file mode 100644 index 0000000..d480bac --- /dev/null +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/CharactersListPacket.kt @@ -0,0 +1,71 @@ +package de.fiereu.openmmo.protocols.game.packets + +import de.fiereu.openmmo.common.CharacterInfo +import de.fiereu.openmmo.common.Pokemon +import de.fiereu.openmmo.common.enums.SkinSlot +import de.fiereu.openmmo.protocols.PacketDeserializer +import de.fiereu.openmmo.protocols.PacketSerializer +import de.fiereu.openmmo.protocols.game.packets.codecs.* +import de.fiereu.openmmo.protocols.readUtf16LE +import de.fiereu.openmmo.protocols.writeUtf16LE +import io.netty.buffer.ByteBuf + +data class CharacterListPacket(val characters: List) + +data class CharacterEntry( + val characterInfo: CharacterInfo, + val skinSet: SkinSet, + val guildId: Int? = null, + val pokemon: List +) { + init { + require(pokemon.size <= 6) { "Party size can't be more than 6 pokemon" } + } +} + +class CharacterListSerializer : PacketSerializer { + override fun serialize(packet: CharacterListPacket, buffer: ByteBuf) = + buffer.run { + writeByte(packet.characters.size) + packet.characters.forEach { characterEntry -> + writeCharacterInfoLE(useLong = false, characterEntry.characterInfo) + writeSkinsLE(characterEntry.skinSet) + // TODO find out why we send 2 times the skin data + writeSkinsLE(characterEntry.skinSet, writeByte = false) + + val hasGuild = characterEntry.guildId != null + buffer.writeBoolean(hasGuild) + if (hasGuild) { + writeUtf16LE("") // unused + writeIntLE(characterEntry.guildId) + } + + buffer.writeByte(characterEntry.pokemon.size) + characterEntry.pokemon.forEach(::writePokemonLE) + } + } +} + +class CharacterListDeserializer : PacketDeserializer { + override fun deserialize(buffer: ByteBuf): CharacterListPacket = + buffer.run { + val size = buffer.readByte().toInt() + val characters = + buildList(size) { + val characterInfo = readCharacterInfoLE(useLong = true) + val skinSet1 = readSkinsLE(SkinSlot.entries, readByte = true) + val skinSet2 = readSkinsLE(SkinSlot.entries, readByte = false) + + val guildId = + if (readBoolean()) { + readUtf16LE() // unused + readIntLE() + } else null + + val partySize = readUnsignedByte().toInt() + val pokemon = buildList(partySize) { repeat(partySize) { add(readPokemonLE()) } } + add(CharacterEntry(characterInfo, skinSet1, guildId, pokemon)) + } + return CharacterListPacket(characters) + } +} diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/ChatMessagePacket.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/ChatMessagePacket.kt new file mode 100644 index 0000000..1364c80 --- /dev/null +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/ChatMessagePacket.kt @@ -0,0 +1,57 @@ +package de.fiereu.openmmo.protocols.game.packets + +import de.fiereu.openmmo.common.enums.ChatType +import de.fiereu.openmmo.common.enums.Language +import de.fiereu.openmmo.protocols.PacketDeserializer +import de.fiereu.openmmo.protocols.PacketSerializer +import de.fiereu.openmmo.protocols.readUtf16LE +import de.fiereu.openmmo.protocols.writeUtf16LE +import io.netty.buffer.ByteBuf + +data class ChatMessagePacket( + val type: ChatType, + val language: Language?, + val message: String, + val sender: String? +) + +class ChatMessageSerialize : PacketSerializer { + override fun serialize(packet: ChatMessagePacket, buffer: ByteBuf) { + buffer.apply { + writeByte(packet.type.ordinal) + when (packet.type) { + ChatType.TEAM -> { + writeUtf16LE(packet.message) + } + else -> { + require(packet.sender != null) { "sender must not be null" } + require(packet.language != null) { "language must not be null" } + writeLongLE(0) // TODO some id + writeUtf16LE(packet.sender) + writeByte(packet.language.ordinal) + writeByte(-1) + writeUtf16LE(packet.message) + } + } + } + } +} + +class ChatMessageDeserialize : PacketDeserializer { + override fun deserialize(buffer: ByteBuf): ChatMessagePacket { + when (val type = ChatType.entries[buffer.readByte().toInt()]) { + ChatType.LINK -> { + val message = buffer.readUtf16LE() + return ChatMessagePacket(type, Language.OTHER, message, null) + } + else -> { + buffer.readIntLE() + val sender = buffer.readUtf16LE() + val language = Language.entries[buffer.readByte().toInt()] + buffer.readByte() + val message = buffer.readUtf16LE() + return ChatMessagePacket(type, language, message, sender) + } + } + } +} diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/JoinGamePacket.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/JoinGamePacket.kt index f655a1b..18a82d6 100644 --- a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/JoinGamePacket.kt +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/JoinGamePacket.kt @@ -15,16 +15,16 @@ abstract class GameAuthenticationData /** * Used when a player reconnects to the game server after a disconnection. This data typically - * includes the player's unique identifier and a session key that was issued by the login server to + * includes the sessions unique id and a session key that was issued by the login server to * re-establish the session. */ -data class ReconnectAuthData(val playerId: Long, val sessionKey: ByteArray) : +data class ReconnectAuthData(val sessionId: Long, val sessionKey: ByteArray) : GameAuthenticationData() /** * Used when a player is joining the game server for the first time after logging in. This data - * typically includes the user's unique identifier and a session key that was issued by the login - * server to authenticate the session. + * typically includes the user's unique id and a session key that was issued by the login server to + * authenticate the session. */ data class NewAuthData( val userId: Int, @@ -59,7 +59,7 @@ class JoinPacketSerializer : PacketSerializer { } is ReconnectAuthData -> { buffer.writeByte(0x01) - buffer.writeLongLE(packet.authData.playerId) + buffer.writeLongLE(packet.authData.sessionId) buffer.writeByte(packet.authData.sessionKey.size) buffer.writeBytes(packet.authData.sessionKey) } diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/JoinResponsePacket.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/JoinResponsePacket.kt index 7a8277e..46cd419 100644 --- a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/JoinResponsePacket.kt +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/JoinResponsePacket.kt @@ -14,18 +14,14 @@ import java.time.ZoneId * seems to also be sent as some kind of time sync packet */ data class JoinResponsePacket( - val canJoin: Boolean, - val stats: GameStats? = null - // we dont need TimeInfo here since it can be calculated on the fly + val canJoin: Boolean, + val stats: GameStats? = null + // we dont need TimeInfo here since it can be calculated on the fly ) { - data class GameStats( - val playtime: Int, - val rewardPoints: Int, - val balance: Int - ) + data class GameStats(val playtime: Int, val rewardPoints: Int, val balance: Int) } -class JoinResponsePacketSerializer : PacketSerializer { +class JoinResponseSerializer : PacketSerializer { override fun serialize(packet: JoinResponsePacket, buffer: ByteBuf) { buffer.writeBoolean(packet.canJoin) if (!packet.canJoin) return // no further data is sent @@ -47,24 +43,16 @@ class JoinResponsePacketSerializer : PacketSerializer { } } -class JoinResponsePacketDeserializer : PacketDeserializer { +class JoinResponseDeserializer : PacketDeserializer { data class JoinResponsePacket( - val canJoin: Boolean, - val stats: GameStats? = null, - val time: TimeInfo? = null + val canJoin: Boolean, + val stats: GameStats? = null, + val time: TimeInfo? = null ) { - data class GameStats( - val playtime: Int, - val rewardPoints: Int, - val balance: Int - ) - - data class TimeInfo( - val serverDayStartSecond: Int, - val serverCurrentSecond: Int - ) + data class GameStats(val playtime: Int, val rewardPoints: Int, val balance: Int) + data class TimeInfo(val serverDayStartSecond: Int, val serverCurrentSecond: Int) } override fun deserialize(buffer: ByteBuf): JoinResponsePacket { @@ -84,9 +72,8 @@ class JoinResponsePacketDeserializer : PacketDeserializer { + override fun serialize(packet: LoadEntityPacket, buffer: ByteBuf) = + buffer.run { + writeLong(packet.entityId) + writeByte(0) + writeSkinsLE(packet.skin) + writeUtf16LE(packet.name) + writeByte(packet.regionId) + writeByte(packet.bankId) + writeByte(packet.mapId) + writeShortLE(packet.x) + writeShortLE(packet.y) + writeByte(packet.facing.ordinal) + writeByte(0) + + writeByte(0) + writeByte(packet.status.ordinal) + + var flags = 0 + if (packet.hasFollower) flags = flags or 0x04 + writeShortLE(flags) + if (flags and 0x01 != 0) { + writeByte(0) + } + + if (flags and 0x02 != 0) { + writeByte(0) + writeShortLE(flags) + } + + if (flags and 0x04 != 0) { + writeShortLE(packet.followerDexId) + } + + if (flags and 0x08 != 0) { + writeByte(0) + } + + if (flags and 0x10 != 0) { + writeIntLE(0) + writeUtf16LE("") + } + } +} + +class LoadEntityDeserializer : PacketDeserializer { + override fun deserialize(buffer: ByteBuf): LoadEntityPacket = + buffer.run { + val entityId = readLong() + readByte() // Skip padding byte + val skin = readSkinsLE(SkinSlot.entries) + val name = readUtf16LE() + val regionId = readByte().toInt() + val bankId = readByte().toInt() + val mapId = readByte().toInt() + val x = readShortLE() + val y = readShortLE() + val facing = Direction.entries[readByte().toInt()] + readByte() // Skip padding byte + + readByte() // Skip padding byte + val status = EntityStatus.entries[readByte().toInt()] + + val flags = readUnsignedShortLE() + if (flags and 0x01 != 0) { + readByte() + } + + if (flags and 0x02 != 0) { + readByte() + readUnsignedShortLE() + } + + val hasFollower = (flags and 0x04) != 0 + val followerDexId = + if (hasFollower) { + readShortLE() + } else { + 0 + } + + if (flags and 0x08 != 0) { + readByte() + } + + if (flags and 0x10 != 0) { + readInt() // Skip int value + readUtf16LE() // Skip string + } + + LoadEntityPacket( + entityId = entityId, + skin = skin, + name = name, + regionId = regionId, + bankId = bankId, + mapId = mapId, + x = x.toInt(), + y = y.toInt(), + facing = facing, + status = status, + hasFollower = hasFollower, + followerDexId = followerDexId, + ) + } +} diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/LoadMapPacket.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/LoadMapPacket.kt new file mode 100644 index 0000000..ef93d2b --- /dev/null +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/LoadMapPacket.kt @@ -0,0 +1,219 @@ +package de.fiereu.openmmo.protocols.game.packets + +import de.fiereu.openmmo.common.Tile2D +import de.fiereu.openmmo.common.enums.EncounterType +import de.fiereu.openmmo.common.enums.Lighting +import de.fiereu.openmmo.common.enums.MapType +import de.fiereu.openmmo.common.enums.Weather +import de.fiereu.openmmo.common.utils.gzipCompress +import de.fiereu.openmmo.common.utils.gzipDecompress +import de.fiereu.openmmo.common.utils.isNdsRegion +import de.fiereu.openmmo.protocols.PacketDeserializer +import de.fiereu.openmmo.protocols.PacketSerializer +import de.fiereu.openmmo.protocols.game.packets.codecs.readTile2DLE +import de.fiereu.openmmo.protocols.game.packets.codecs.writeTile2DLE +import de.fiereu.openmmo.protocols.readUtf16LE +import de.fiereu.openmmo.protocols.writeUtf16LE +import io.netty.buffer.ByteBuf + +data class LoadMapPacket( + val reloadPlayer: Boolean, + val deleteCache: Boolean, + val regionId: Int, + val bankId: Int, + val mapId: Int, + val mapData: MapData +) { + init { + when (mapData) { + is MapData.NdsMapData -> require(isNdsRegion(regionId)) { "MapData is invalid" } + is MapData.GbaMapData -> require(!isNdsRegion(regionId)) { "MapData is invalid" } + } + } +} + +sealed interface MapData { + data class NdsMapData(val lighting: Lighting, val weather: Weather, val mapType: MapType) : + MapData + + data class GbaMapData( + val width: Int, + val height: Int, + val paletteIdx1: Int, + val paletteIdx2: Int, + val borderWidth: Int, + val borderHeight: Int, + val borderTiles: List, + val lighting: Lighting, + val weather: Weather, + val mapType: MapType, + val encounterType: EncounterType + ) : MapData { + init { + require(borderWidth * borderHeight == borderTiles.size) { + "borderTiles size doesnt match dimensions" + } + require(borderWidth * borderHeight > 0) { "BorderWidth & Height must be greater than zero" } + } + } +} + +class LoadMapPacketSerializer : PacketSerializer { + override fun serialize(packet: LoadMapPacket, buffer: ByteBuf) { + buffer.apply { + var flags = 0 + if (packet.deleteCache) flags = flags or 1 + if (packet.reloadPlayer) flags = flags or 2 + + writeByte(flags) + + writeByte(packet.regionId) // Client will stop parsing if the region is not loaded + writeByte(packet.bankId) + writeByte(packet.mapId) + writeByte(0) + + when (packet.mapData) { + is MapData.NdsMapData -> { + writeShortLE(0) + val unk = 0 + writeByte(unk) + for (i in 0 until unk) { + writeShortLE(0) + writeShortLE(0) + } + + writeByte(packet.mapData.lighting.ordinal) + writeByte(packet.mapData.weather.ordinal) + writeByte(packet.mapData.mapType.ordinal) + } + is MapData.GbaMapData -> { + writeIntLE(packet.mapData.width) + writeIntLE(packet.mapData.height) + writeIntLE(packet.mapData.paletteIdx1) + writeIntLE(packet.mapData.paletteIdx2) + writeByte(packet.mapData.borderWidth) + writeByte(packet.mapData.borderHeight) + writeShortLE(0) + writeByte(0) + writeByte(packet.mapData.lighting.ordinal) + writeByte(packet.mapData.weather.ordinal) + writeByte(packet.mapData.mapType.ordinal) + writeByte(packet.mapData.encounterType.ordinal) + + for (tile2D in packet.mapData.borderTiles) { + writeTile2DLE(tile2D) + } + + val hasUnknown = false + writeBoolean(hasUnknown) + if (hasUnknown) { + val unknown = ByteArray(0) + writeIntLE(unknown.size) + writeBytes(unknown.gzipCompress()) + } + + val connectionCount = 0 + writeByte(0) + for (i in 0 until connectionCount) { + writeByte(0) + writeIntLE(0) + writeByte(0) + writeByte(0) + } + + val hasUnknown2 = false + writeBoolean(hasUnknown2) + if (hasUnknown2) { + writeLongLE(0) + writeUtf16LE("") + } + } + } + } + } +} + +class LoadMapPacketDeserializer : PacketDeserializer { + override fun deserialize(buffer: ByteBuf): LoadMapPacket = + buffer.let { + val flags = buffer.readByte().toInt() + val deleteCache = (flags and 1) != 0 + val reloadPlayer = (flags and 2) != 0 + + val regionId = buffer.readByte().toInt() + val bankId = buffer.readByte().toInt() + val mapId = buffer.readByte().toInt() + buffer.readByte() + + val mapData = + if (isNdsRegion(regionId)) { + // NDS format + buffer.readShortLE() + val unk = buffer.readByte().toInt() + for (i in 0 until unk) { + buffer.readShortLE() + buffer.readShortLE() + } + val lighting = Lighting.entries[buffer.readByte().toInt()] + val weather = Weather.entries[buffer.readByte().toInt()] + val mapType = MapType.entries[buffer.readByte().toInt()] + MapData.NdsMapData(lighting, weather, mapType) + } else { + // GBA format + val width = buffer.readIntLE() + val height = buffer.readIntLE() + val paletteIdx1 = buffer.readIntLE() + val paletteIdx2 = buffer.readIntLE() + val borderWidth = buffer.readByte().toInt() + val borderHeight = buffer.readByte().toInt() + buffer.readShortLE() + buffer.readByte() + val lighting = Lighting.entries[buffer.readByte().toInt()] + val weather = Weather.entries[buffer.readByte().toInt()] + val mapType = MapType.entries[buffer.readByte().toInt()] + val encounterType = EncounterType.entries[buffer.readByte().toInt()] + + val borderTiles = mutableListOf() + for (i in 0 until (borderWidth * borderHeight)) { + borderTiles.add(buffer.readTile2DLE()) + } + + val hasUnknown = buffer.readBoolean() + if (hasUnknown) { + val unknownSize = buffer.readIntLE() + val unknownCompressed = ByteArray(unknownSize) + buffer.readBytes(unknownCompressed) + unknownCompressed.gzipDecompress() + } + + val connectionCount = buffer.readByte().toInt() + for (i in 0 until connectionCount) { + buffer.readByte() + buffer.readIntLE() + buffer.readByte() + buffer.readByte() + } + + val hasUnknown2 = buffer.readBoolean() + if (hasUnknown2) { + buffer.readLongLE() + buffer.readUtf16LE() + } + + MapData.GbaMapData( + width, + height, + paletteIdx1, + paletteIdx2, + borderWidth, + borderHeight, + borderTiles, + lighting, + weather, + mapType, + encounterType) + } + + LoadMapPacket(reloadPlayer, deleteCache, regionId, bankId, mapId, mapData) + } +} diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/PokemonContainerPacket.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/PokemonContainerPacket.kt new file mode 100644 index 0000000..0c60ad3 --- /dev/null +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/PokemonContainerPacket.kt @@ -0,0 +1,53 @@ +package de.fiereu.openmmo.protocols.game.packets + +import de.fiereu.openmmo.common.Pokemon +import de.fiereu.openmmo.common.enums.PokemonContainer +import de.fiereu.openmmo.protocols.PacketDeserializer +import de.fiereu.openmmo.protocols.PacketSerializer +import de.fiereu.openmmo.protocols.game.packets.codecs.readPokemonLE +import de.fiereu.openmmo.protocols.game.packets.codecs.writePokemonLE +import io.netty.buffer.ByteBuf + +data class PokemonContainerPacket( + val container: PokemonContainer, + val hasChange: Boolean, + val delete: Boolean, + val pokemon: List, +) { + init { + if (delete) + require(hasChange) { + "Pokemon container cannot be deleted if the packet doesn't change anything" + } + if (delete) + require(pokemon.isEmpty()) { "Deleting a Pokemon container shouldn't have any pokemon" } + } +} + +class PokemonContainerSerializer : PacketSerializer { + override fun serialize(packet: PokemonContainerPacket, buffer: ByteBuf) = + buffer.run { + writeByte(packet.container.ordinal) + var flags = 0 + if (packet.hasChange) flags = flags or 1 + if (packet.delete) flags = flags or 2 + writeByte(flags) + if (packet.delete) return + writeByte(packet.pokemon.size) + for (pokemon in packet.pokemon) { + writePokemonLE(pokemon) + } + } +} + +class PokemonContainerDeserializer : PacketDeserializer { + override fun deserialize(buffer: ByteBuf): PokemonContainerPacket = + buffer.run { + val pokemonContainer = PokemonContainer.entries[buffer.readByte().toInt()] + val flags = buffer.readByte().toInt() + val hasChange = (flags and 1) != 0 + val delete = (flags and 2) != 0 + val pokemon = buildList(readByte().toInt()) { readPokemonLE() } + return PokemonContainerPacket(pokemonContainer, hasChange, delete, pokemon) + } +} diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/RenderScreenPacket.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/RenderScreenPacket.kt new file mode 100644 index 0000000..4d986e6 --- /dev/null +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/RenderScreenPacket.kt @@ -0,0 +1,19 @@ +package de.fiereu.openmmo.protocols.game.packets + +import de.fiereu.openmmo.protocols.PacketDeserializer +import de.fiereu.openmmo.protocols.PacketSerializer +import io.netty.buffer.ByteBuf + +data class RenderScreenPacket(val renderScreen: Boolean) + +class RenderScreenSerializer : PacketSerializer { + override fun serialize(packet: RenderScreenPacket, buffer: ByteBuf) { + buffer.writeBoolean(packet.renderScreen) + } +} + +class RenderScreenDeserializer : PacketDeserializer { + override fun deserialize(buffer: ByteBuf): RenderScreenPacket { + return RenderScreenPacket(buffer.readBoolean()) + } +} diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/RequestCharactersPacket.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/RequestCharactersPacket.kt new file mode 100644 index 0000000..9aa0071 --- /dev/null +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/RequestCharactersPacket.kt @@ -0,0 +1,17 @@ +package de.fiereu.openmmo.protocols.game.packets + +import de.fiereu.openmmo.protocols.PacketDeserializer +import de.fiereu.openmmo.protocols.PacketSerializer +import io.netty.buffer.ByteBuf + +class RequestCharactersPacket() + +class RequestCharactersPacketSerializer : PacketSerializer { + override fun serialize(packet: RequestCharactersPacket, buffer: ByteBuf) {} +} + +class RequestCharactersDeserializer : PacketDeserializer { + override fun deserialize(buffer: ByteBuf): RequestCharactersPacket { + return RequestCharactersPacket() + } +} diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/RequestPlayerPacket.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/RequestPlayerPacket.kt new file mode 100644 index 0000000..b59e01a --- /dev/null +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/RequestPlayerPacket.kt @@ -0,0 +1,17 @@ +package de.fiereu.openmmo.protocols.game.packets + +import de.fiereu.openmmo.protocols.PacketDeserializer +import de.fiereu.openmmo.protocols.PacketSerializer +import io.netty.buffer.ByteBuf + +class RequestPlayerPacket + +class RequestPlayerSerializer : PacketSerializer { + override fun serialize(packet: RequestPlayerPacket, buffer: ByteBuf) {} +} + +class RequestPlayerDeserializePacket : PacketDeserializer { + override fun deserialize(buffer: ByteBuf): RequestPlayerPacket { + return RequestPlayerPacket() + } +} diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/SelectCharacterPacket.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/SelectCharacterPacket.kt new file mode 100644 index 0000000..0773a19 --- /dev/null +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/SelectCharacterPacket.kt @@ -0,0 +1,25 @@ +package de.fiereu.openmmo.protocols.game.packets + +import de.fiereu.openmmo.protocols.PacketDeserializer +import de.fiereu.openmmo.protocols.PacketSerializer +import io.netty.buffer.ByteBuf + +data class SelectCharacterPacket(val characterId: Long, val characterIdHash: Long) + +class SelectCharacterSerializer : PacketSerializer { + override fun serialize(packet: SelectCharacterPacket, buffer: ByteBuf) { + buffer.apply { + buffer.writeLongLE(packet.characterId) + buffer.writeLongLE(packet.characterIdHash) + } + } +} + +class SelectCharacterDeserializer : PacketDeserializer { + override fun deserialize(buffer: ByteBuf): SelectCharacterPacket = + buffer.run { + val characterId = buffer.readLong() + val characterIdHash = buffer.readLong() + return SelectCharacterPacket(characterId, characterIdHash) + } +} diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/SelectedCharacterPacket.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/SelectedCharacterPacket.kt new file mode 100644 index 0000000..6ae63f1 --- /dev/null +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/SelectedCharacterPacket.kt @@ -0,0 +1,30 @@ +package de.fiereu.openmmo.protocols.game.packets + +import de.fiereu.openmmo.common.CharacterInfo +import de.fiereu.openmmo.protocols.PacketDeserializer +import de.fiereu.openmmo.protocols.PacketSerializer +import de.fiereu.openmmo.protocols.game.packets.codecs.readCharacterInfoLE +import de.fiereu.openmmo.protocols.game.packets.codecs.writeCharacterInfoLE +import io.netty.buffer.ByteBuf + +data class SelectedCharacterPacket(val character: CharacterInfo?) + +class SelectedCharacterSerializer : PacketSerializer { + override fun serialize(packet: SelectedCharacterPacket, buffer: ByteBuf) { + buffer.apply { + val hasCharacter = packet.character != null + writeBoolean(hasCharacter) + if (!hasCharacter) return + writeCharacterInfoLE(false, packet.character) + } + } +} + +class SelectedCharacterDeserializer : PacketDeserializer { + override fun deserialize(buffer: ByteBuf): SelectedCharacterPacket = + buffer.run { + val hasCharacter = readBoolean() + val character = if (hasCharacter) readCharacterInfoLE(false) else null + return SelectedCharacterPacket(character) + } +} diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/builders/JoinResponsePacketBuilder.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/builders/JoinResponsePacketBuilder.kt index b86110c..7bc0bd0 100644 --- a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/builders/JoinResponsePacketBuilder.kt +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/builders/JoinResponsePacketBuilder.kt @@ -2,9 +2,7 @@ package de.fiereu.openmmo.protocols.game.packets.builders import de.fiereu.openmmo.protocols.game.packets.JoinResponsePacket -class JoinResponsePacketBuilder( - private val canJoin: Boolean -) { +class JoinResponsePacketBuilder(private val canJoin: Boolean) { private var playtime: Int = 0 private var rewardPoints: Int = 0 private var balance: Int = 0 @@ -35,14 +33,13 @@ class JoinResponsePacketBuilder( } fun build(): JoinResponsePacket { - require(canJoin) { "Cannot build a JoinResponsePacket with canJoin=false. Use JoinResponsePacketBuilder.reject() instead." } + require(canJoin) { + "Cannot build a JoinResponsePacket with canJoin=false. Use JoinResponsePacketBuilder.reject() instead." + } return JoinResponsePacket( - canJoin = true, - stats = JoinResponsePacket.GameStats( - playtime = playtime, - rewardPoints = rewardPoints, - balance = balance - ) - ) + canJoin = true, + stats = + JoinResponsePacket.GameStats( + playtime = playtime, rewardPoints = rewardPoints, balance = balance)) } -} \ No newline at end of file +} diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/codecs/CharacterInfo.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/codecs/CharacterInfo.kt new file mode 100644 index 0000000..8e59bdc --- /dev/null +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/codecs/CharacterInfo.kt @@ -0,0 +1,136 @@ +package de.fiereu.openmmo.protocols.game.packets.codecs + +import de.fiereu.openmmo.common.CharacterInfo +import de.fiereu.openmmo.protocols.readEpochSeconds +import de.fiereu.openmmo.protocols.readUtf16LE +import de.fiereu.openmmo.protocols.writeEpochSecond +import de.fiereu.openmmo.protocols.writeUtf16LE +import io.netty.buffer.ByteBuf + +fun ByteBuf.writeCharacterInfoLE(useLong: Boolean, characterInfo: CharacterInfo) = apply { + writeLongLE(characterInfo.id) + writeUtf16LE(characterInfo.name) + writeUtf16LE("") + writeIntLE(characterInfo.userId) + writeByte(characterInfo.rivalSex.toInt()) + writeEpochSecond(characterInfo.lastLogin) + if (useLong) { + writeLongLE(0) + } + writeEpochSecond(characterInfo.createdAt) + writeIntLE(0) // unused + writeByte(0) // unused + writeIntLE(0) + writeIntLE(characterInfo.money) + writeShortLE(0) + writeIntLE(0) + writeByte(characterInfo.permissions) + writeByte(0) // unused + writeByte(0) + writeIntLE(0) + repeat(8) { + writeByte(0) // unused + } + writeShortLE(characterInfo.remainingSafariSteps.toInt()) + writeByte(characterInfo.remainingSafariBalls.toInt()) + writeIntLE(0) + writeByte(characterInfo.pcExtraSlots.toInt()) + writeByte(characterInfo.battleBoxExtraSlots.toInt()) + writeByte(characterInfo.templateAmount.toInt()) + writeByte(0) // unused + writeByte(0) // unused + writeByte(0) // unused + writeByte(characterInfo.positionRegionId.toInt()) + writeByte(characterInfo.positionBankId.toInt()) + writeByte(characterInfo.positionMapId.toInt()) + writeByte(0) // unused + writeShortLE(characterInfo.positionX.toInt()) + writeShortLE(characterInfo.positionY.toInt()) + writeByte(0) // unused + writeByte(0) // unused + writeShortLE(characterInfo.repelLeft.toInt()) + writeShortLE(characterInfo.repelItemId.toInt()) + writeByte(0) // 100 + writeShortLE(characterInfo.lureItemId.toInt()) + writeShortLE(characterInfo.lureLeft.toInt()) + val bytes = ByteArray(0) + writeByte(bytes.size) + writeBytes(bytes) +} + +fun ByteBuf.readCharacterInfoLE(useLong: Boolean): CharacterInfo = let { + val id = readLongLE() + val name = readUtf16LE() + val namePrefix = readUtf16LE() + val userId = readIntLE() + val rivalSex = readByte() + val lastLogin = readEpochSeconds() + if (useLong) { + readLongLE() + } + val createdAt = readEpochSeconds() + readIntLE() // unused + readByte() // unused + readIntLE() + val money = readIntLE() + readShortLE() + readIntLE() + val permissions = readByte().toInt() + readByte() // unused + readByte() + readIntLE() + repeat(8) { + readByte() // unused + } + val remainingSafariSteps = readShortLE() + val remainingSafariBalls = readByte() + readIntLE() + val pcExtraSlots = readByte() + val battleBoxExtraSlots = readByte() + val templateAmount = readByte() + readByte() // unused + readByte() // unused + readByte() // unused + val positionRegionId = readByte() + val positionBankId = readByte() + val positionMapId = readByte() + readByte() // unused + val positionX = readShortLE() + val positionY = readShortLE() + readByte() // unused + readByte() // unused + val repelLeft = readShortLE() + val repelItemId = readShortLE() + readByte() + val lureItemId = readShortLE() + val lureLeft = readShortLE() + val arraySize = readByte().toInt() + if (arraySize > 0) { + val bytes = ByteArray(arraySize) + readBytes(bytes) + } + return CharacterInfo( + id, + name, + namePrefix, + userId, + rivalSex, + lastLogin, + createdAt, + money, + permissions, + remainingSafariSteps, + remainingSafariBalls, + pcExtraSlots, + battleBoxExtraSlots, + templateAmount, + positionRegionId, + positionBankId, + positionMapId, + positionX, + positionY, + repelLeft, + repelItemId, + lureLeft, + lureItemId) +} diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/codecs/Pokemon.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/codecs/Pokemon.kt new file mode 100644 index 0000000..45b7f38 --- /dev/null +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/codecs/Pokemon.kt @@ -0,0 +1,179 @@ +package de.fiereu.openmmo.protocols.game.packets.codecs + +import de.fiereu.openmmo.common.Pokemon +import de.fiereu.openmmo.common.PokemonMove +import de.fiereu.openmmo.common.enums.EVs +import de.fiereu.openmmo.common.enums.PokemonContainer +import de.fiereu.openmmo.common.enums.PokemonRarity +import de.fiereu.openmmo.common.enums.compress +import de.fiereu.openmmo.common.enums.decompressIVs +import de.fiereu.openmmo.protocols.readEpochSeconds +import de.fiereu.openmmo.protocols.readUtf16LE +import de.fiereu.openmmo.protocols.writeByte +import de.fiereu.openmmo.protocols.writeEpochSecond +import de.fiereu.openmmo.protocols.writeShortLE +import de.fiereu.openmmo.protocols.writeUtf16LE +import io.netty.buffer.ByteBuf + +fun ByteBuf.writePokemonLE(pokemon: Pokemon) = apply { + writeLongLE(pokemon.id) + writeByte(0) + writeLongLE(0) + writeLongLE(0) // unused + writeByte(pokemon.container.ordinal) + writeShortLE(pokemon.containerSlot) + writeShortLE(pokemon.dexId) + writeIntLE(pokemon.seed) + writeLongLE(0) // unused + writeUtf16LE(pokemon.ot) + writeUtf16LE(pokemon.nickname) + writeByte(0) + writeByte(0) + writeByte(pokemon.level) + writeShortLE(pokemon.hp) + writeShortLE(0) + writeIntLE(pokemon.xp) + writeByte(0) + writeShortLE(0) + pokemon.moves.forEach { move -> writeShortLE(move.id) } + pokemon.moves.forEach { move -> writeByte(move.pp) } + arrayOf(0, 0, 0, 0).forEach { writeShortLE(it) } + + writeByte(pokemon.eVs.hp) + writeByte(pokemon.eVs.atk) + writeByte(pokemon.eVs.def) + writeByte(pokemon.eVs.spd) + writeByte(pokemon.eVs.spAtk) + writeByte(pokemon.eVs.spDef) + + writeByte(0) + writeByte(0) + writeByte(0) + writeByte(0) + writeByte(0) + writeByte(0) + writeByte(0) + writeByte(0) + writeByte(0) + writeByte(0) + writeByte(0) + + writeIntLE(pokemon.iVs.compress()) + writeByte(0) + writeLongLE(0) + + var rarityMask = 0 + if (pokemon.isShiny) rarityMask = rarityMask or (1 shl PokemonRarity.SHINY.ordinal) + if (pokemon.hasHiddenAbility) + rarityMask = rarityMask or (1 shl PokemonRarity.HIDDEN_ABILITY.ordinal) + if (pokemon.isAlpha) rarityMask = rarityMask or (1 shl PokemonRarity.ALPHA.ordinal) + if (pokemon.isSecret) rarityMask = rarityMask or (1 shl PokemonRarity.SECRET.ordinal) + if (pokemon.isFatefulEncounter) + rarityMask = rarityMask or (1 shl PokemonRarity.FATEFUL_ENCOUNTER.ordinal) + if (pokemon.isRaidEncounter) + rarityMask = rarityMask or (1 shl PokemonRarity.RAID_ENCOUNTER.ordinal) + writeShortLE(rarityMask) + + writeEpochSecond(pokemon.caughtAt) + writeShortLE(0) + writeByte(0) + writeByte(0) + + val unk = ByteArray(0) + writeByte(unk.size) + writeBytes(unk) + + writeBytes(ByteArray(12) { 0.toByte() }) +} + +fun ByteBuf.readPokemonLE(): Pokemon { + val id = readLongLE() + readByte() + readLongLE() + readLongLE() // unused + val container = PokemonContainer.entries[readUnsignedByte().toInt()] + val containerSlot = readShortLE() + val dexId = readUnsignedShortLE().toInt() + val seed = readIntLE() + readLongLE() // unused + val ot = readUtf16LE() + val nickname = readUtf16LE() + readByte() + readByte() + val level = readByte() + val currentHp = readShortLE() + readShortLE() + val xp = readIntLE() + readByte() + readShortLE() + val moves = List(4) { PokemonMove(readShortLE(), 0) } + moves.forEach { move -> move.pp = readByte() } + repeat(4) { readByte() } + val eVs = + EVs().apply { + hp = readByte().toInt() + atk = readByte().toInt() + def = readByte().toInt() + spd = readByte().toInt() + spAtk = readByte().toInt() + spDef = readByte().toInt() + } + readByte() + readByte() + readByte() + readByte() + readByte() + readByte() + readByte() + readByte() + readByte() + readByte() + readByte() + readByte() // unused + readByte() + readByte() + readByte() + readByte() + readByte() + val iVs = decompressIVs(readIntLE()) + readByte() + readLongLE() + + val rarityMask = readUnsignedShortLE() + val isShiny = (rarityMask and (1 shl PokemonRarity.SHINY.ordinal)) != 0 + val hasHiddenAbility = (rarityMask and (1 shl PokemonRarity.HIDDEN_ABILITY.ordinal)) != 0 + val isAlpha = (rarityMask and (1 shl PokemonRarity.ALPHA.ordinal)) != 0 + val isSecret = (rarityMask and (1 shl PokemonRarity.SECRET.ordinal)) != 0 + val isFatefulEncounter = (rarityMask and (1 shl PokemonRarity.FATEFUL_ENCOUNTER.ordinal)) != 0 + val isRaidEncounter = (rarityMask and (1 shl PokemonRarity.RAID_ENCOUNTER.ordinal)) != 0 + + val caughtAt = readEpochSeconds() + readShortLE() + readByte() + readByte() + + val unknown = ByteArray(readByte().toInt()) + readBytes(unknown) + + return Pokemon( + id, + container, + containerSlot, + dexId, + seed, + ot, + nickname, + level, + currentHp, + xp, + eVs, + iVs, + moves, + isShiny, + hasHiddenAbility, + isAlpha, + isSecret, + isFatefulEncounter, + isRaidEncounter, + caughtAt) +} diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/codecs/Skin.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/codecs/Skin.kt new file mode 100644 index 0000000..4945660 --- /dev/null +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/codecs/Skin.kt @@ -0,0 +1,65 @@ +package de.fiereu.openmmo.protocols.game.packets.codecs + +import de.fiereu.openmmo.common.enums.SkinSlot +import io.netty.buffer.ByteBuf +import java.util.EnumMap +import kotlin.enums.EnumEntries + +data class Skin(val slot: SkinSlot, val type: UShort?, val color: UByte?) + +class SkinSet() : EnumMap(SkinSlot::class.java) { + fun put(skin: Skin) { + this[skin.slot] = skin + } + + override fun put(key: SkinSlot, value: Skin): Skin? { + require(key == value.slot) { "Slot mismatch: $key != ${value.slot}" } + return super.put(key, value) + } +} + +fun ByteBuf.writeSkinsLE( + skinSet: SkinSet, + slots: EnumEntries = SkinSlot.entries, + writeByte: Boolean = true +) = apply { + if (writeByte) { + writeByte(0) + } + val mask = slots.fold(0) { acc, slot -> acc or (1 shl slot.ordinal) } + writeShortLE(mask) + + slots.forEach { slot -> + val skin = skinSet[slot] ?: Skin(slot, 0u, 0u) + val type = skin.type ?: 0x3FFFu + val color = skin.color ?: 0x3Fu + require(type <= 0x3FFFu) { "Skin type too large: ${skin.type}" } + require(color <= 0x3Fu) { "Skin color too large: ${skin.color}" } + + val compressed = (type and 0x3FFFu).toInt() or ((color and 0x3Fu).toInt() shl 10) + + writeShortLE(compressed) + } +} + +fun ByteBuf.readSkinsLE( + slots: EnumEntries, + readByte: Boolean = true, +): SkinSet { + if (readByte) { + readByte() + } + val mask = readUnsignedShortLE() + val skinSet = SkinSet() + + slots.forEach { slot -> + if ((mask and (1 shl slot.ordinal)) != 0) { + val compressed = readUnsignedShortLE() + val type = (compressed and 0x3FFF).toUShort() + val color = ((compressed shr 10) and 0x3F).toUByte() + skinSet.put(Skin(slot, type, color)) + } + } + + return skinSet +} diff --git a/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/codecs/Tile2D.kt b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/codecs/Tile2D.kt new file mode 100644 index 0000000..36801fd --- /dev/null +++ b/protocol.game/src/main/kotlin/de/fiereu/openmmo/protocols/game/packets/codecs/Tile2D.kt @@ -0,0 +1,14 @@ +package de.fiereu.openmmo.protocols.game.packets.codecs + +import de.fiereu.openmmo.common.Tile2D +import io.netty.buffer.ByteBuf + +fun ByteBuf.readTile2DLE(): Tile2D { + val raw = readShortLE().toInt() + return Tile2D(material = (raw and 0x3FF).toShort(), collision = ((raw shr 10) and 0x3F).toByte()) +} + +fun ByteBuf.writeTile2DLE(tile: Tile2D) { + val raw = ((tile.collision.toInt() and 0x3F) shl 10) or (tile.material.toInt() and 0x3FF) + writeShortLE(raw) +} diff --git a/protocol.login/src/main/kotlin/de/fiereu/openmmo/protocols/tls/LoginProtocol.kt b/protocol.login/src/main/kotlin/de/fiereu/openmmo/protocols/tls/LoginProtocol.kt index 2c4556b..ec78fb5 100644 --- a/protocol.login/src/main/kotlin/de/fiereu/openmmo/protocols/tls/LoginProtocol.kt +++ b/protocol.login/src/main/kotlin/de/fiereu/openmmo/protocols/tls/LoginProtocol.kt @@ -3,43 +3,30 @@ package de.fiereu.openmmo.protocols.tls import de.fiereu.openmmo.protocols.Protocol import de.fiereu.openmmo.protocols.incomingPacket import de.fiereu.openmmo.protocols.outgoingPacket -import de.fiereu.openmmo.protocols.tls.packets.ExistingSessionPacket -import de.fiereu.openmmo.protocols.tls.packets.GameServerListPacket import de.fiereu.openmmo.protocols.tls.packets.GameServerListPacketDeserializer import de.fiereu.openmmo.protocols.tls.packets.GameServerListPacketSerializer -import de.fiereu.openmmo.protocols.tls.packets.GameServerNodesPacket import de.fiereu.openmmo.protocols.tls.packets.GameServerNodesPacketDeserializer import de.fiereu.openmmo.protocols.tls.packets.GameServerNodesPacketSerializer -import de.fiereu.openmmo.protocols.tls.packets.JoinGameServerPacket import de.fiereu.openmmo.protocols.tls.packets.JoinGameServerPacketDeserializer import de.fiereu.openmmo.protocols.tls.packets.JoinGameServerPacketSerializer -import de.fiereu.openmmo.protocols.tls.packets.LoginKickPacket import de.fiereu.openmmo.protocols.tls.packets.LoginKickPacketDeserializer import de.fiereu.openmmo.protocols.tls.packets.LoginKickPacketSerializer -import de.fiereu.openmmo.protocols.tls.packets.LoginRequestPacket import de.fiereu.openmmo.protocols.tls.packets.LoginRequestPacketDeserializer import de.fiereu.openmmo.protocols.tls.packets.LoginRequestPacketSerializer -import de.fiereu.openmmo.protocols.tls.packets.LoginResponsePacket import de.fiereu.openmmo.protocols.tls.packets.LoginResponsePacketDeserializer import de.fiereu.openmmo.protocols.tls.packets.LoginResponsePacketSerializer -import de.fiereu.openmmo.protocols.tls.packets.MfaChallengePacket import de.fiereu.openmmo.protocols.tls.packets.MfaChallengePacketDeserializer import de.fiereu.openmmo.protocols.tls.packets.MfaChallengePacketSerializer -import de.fiereu.openmmo.protocols.tls.packets.MfaResponsePacket import de.fiereu.openmmo.protocols.tls.packets.MfaResponsePacketDeserializer import de.fiereu.openmmo.protocols.tls.packets.MfaResponsePacketSerializer -import de.fiereu.openmmo.protocols.tls.packets.RequestGameServerListPacket import de.fiereu.openmmo.protocols.tls.packets.RequestGameServerListPacketDeserializer import de.fiereu.openmmo.protocols.tls.packets.RequestGameServerListPacketSerializer -import de.fiereu.openmmo.protocols.tls.packets.SentCredentialsPacket import de.fiereu.openmmo.protocols.tls.packets.SentCredentialsPacketDeserializer import de.fiereu.openmmo.protocols.tls.packets.SentCredentialsPacketSerializer import de.fiereu.openmmo.protocols.tls.packets.SentExistingSessionPacketDeserializer import de.fiereu.openmmo.protocols.tls.packets.SentExistingSessionPacketSerializer -import de.fiereu.openmmo.protocols.tls.packets.ToSConfirmationPacket import de.fiereu.openmmo.protocols.tls.packets.ToSConfirmationPacketDeserializer import de.fiereu.openmmo.protocols.tls.packets.ToSConfirmationPacketSerializer -import de.fiereu.openmmo.protocols.tls.packets.ToSPacket import de.fiereu.openmmo.protocols.tls.packets.ToSPacketDeserializer import de.fiereu.openmmo.protocols.tls.packets.ToSPacketSerializer @@ -48,43 +35,40 @@ import de.fiereu.openmmo.protocols.tls.packets.ToSPacketSerializer * complete. */ abstract class LoginProtocol() : Protocol() { - override val async: Boolean - get() = true - override val compressed: Boolean - get() = false + override val async: Boolean = true + override val compressed: Boolean = false } class LoginServerProtocol() : LoginProtocol() { init { - outgoingPacket(0x01u, LoginResponsePacketSerializer(), LoginResponsePacket::class) + outgoingPacket(0x01u, LoginResponsePacketSerializer()) incomingPacket(0x02u, RequestGameServerListPacketDeserializer()) incomingPacket(0x03u, JoinGameServerPacketDeserializer()) - outgoingPacket(0x03u, GameServerNodesPacketSerializer(), GameServerNodesPacket::class) + outgoingPacket(0x03u, GameServerNodesPacketSerializer()) incomingPacket(0x04u, ToSConfirmationPacketDeserializer()) - outgoingPacket(0x05u, LoginKickPacketSerializer(), LoginKickPacket::class) - outgoingPacket(0x07u, SentCredentialsPacketSerializer(), SentCredentialsPacket::class) - outgoingPacket(0x08u, MfaChallengePacketSerializer(), MfaChallengePacket::class) + outgoingPacket(0x05u, LoginKickPacketSerializer()) + outgoingPacket(0x07u, SentCredentialsPacketSerializer()) + outgoingPacket(0x08u, MfaChallengePacketSerializer()) incomingPacket(0x08u, MfaResponsePacketDeserializer()) incomingPacket(0x11u, LoginRequestPacketDeserializer()) - outgoingPacket(0x14u, ToSPacketSerializer(), ToSPacket::class) - outgoingPacket(0x22u, GameServerListPacketSerializer(), GameServerListPacket::class) - outgoingPacket(0x26u, SentExistingSessionPacketSerializer(), ExistingSessionPacket::class) + outgoingPacket(0x14u, ToSPacketSerializer()) + outgoingPacket(0x22u, GameServerListPacketSerializer()) + outgoingPacket(0x26u, SentExistingSessionPacketSerializer()) } } class LoginClientProtocol() : LoginProtocol() { init { incomingPacket(0x01u, LoginResponsePacketDeserializer()) - outgoingPacket( - 0x02u, RequestGameServerListPacketSerializer(), RequestGameServerListPacket::class) - outgoingPacket(0x03u, JoinGameServerPacketSerializer(), JoinGameServerPacket::class) + outgoingPacket(0x02u, RequestGameServerListPacketSerializer()) + outgoingPacket(0x03u, JoinGameServerPacketSerializer()) incomingPacket(0x03u, GameServerNodesPacketDeserializer()) - outgoingPacket(0x04u, ToSConfirmationPacketSerializer(), ToSConfirmationPacket::class) + outgoingPacket(0x04u, ToSConfirmationPacketSerializer()) incomingPacket(0x05u, LoginKickPacketDeserializer()) incomingPacket(0x07u, SentCredentialsPacketDeserializer()) incomingPacket(0x08u, MfaChallengePacketDeserializer()) - outgoingPacket(0x08u, MfaResponsePacketSerializer(), MfaResponsePacket::class) - outgoingPacket(0x11u, LoginRequestPacketSerializer(), LoginRequestPacket::class) + outgoingPacket(0x08u, MfaResponsePacketSerializer()) + outgoingPacket(0x11u, LoginRequestPacketSerializer()) incomingPacket(0x14u, ToSPacketDeserializer()) incomingPacket(0x22u, GameServerListPacketDeserializer()) incomingPacket(0x26u, SentExistingSessionPacketDeserializer()) diff --git a/protocol.tls/src/main/kotlin/de/fiereu/openmmo/protocols/tls/TlsProtocol.kt b/protocol.tls/src/main/kotlin/de/fiereu/openmmo/protocols/tls/TlsProtocol.kt index 70d2112..b4e78a4 100644 --- a/protocol.tls/src/main/kotlin/de/fiereu/openmmo/protocols/tls/TlsProtocol.kt +++ b/protocol.tls/src/main/kotlin/de/fiereu/openmmo/protocols/tls/TlsProtocol.kt @@ -22,25 +22,22 @@ import java.security.interfaces.ECPublicKey * (e.g. LoginProtocol) after the handshake is complete. */ abstract class TlsProtocol() : Protocol() { - override val async: Boolean - get() = false - override val compressed: Boolean - get() = false + override val async: Boolean = false + override val compressed: Boolean = false } class TlsServerProtocol(serverKeyPair: Pair) : TlsProtocol() { init { incomingPacket(0x00u, ClientHelloPacketDeserializer()) - outgoingPacket( - 0x01u, ServerHelloPacketSerializer(serverKeyPair.first), ServerHelloPacket::class) + outgoingPacket(0x01u, ServerHelloPacketSerializer(serverKeyPair.first)) incomingPacket(0x02u, ClientReadyPacketDeserializer()) } } class TlsClientProtocol() : TlsProtocol() { init { - outgoingPacket(0x00u, ClientHelloPacketSerializer(), ClientHelloPacket::class) + outgoingPacket(0x00u, ClientHelloPacketSerializer()) incomingPacket(0x01u, ServerHelloPacketDeserializer()) - outgoingPacket(0x02u, ClientReadyPacketSerializer(), ClientReadyPacket::class) + outgoingPacket(0x02u, ClientReadyPacketSerializer()) } } diff --git a/protocol/src/main/kotlin/de/fiereu/openmmo/protocols/ByteBufExtension.kt b/protocol/src/main/kotlin/de/fiereu/openmmo/protocols/ByteBufExtension.kt index fe6d95d..3d1059b 100644 --- a/protocol/src/main/kotlin/de/fiereu/openmmo/protocols/ByteBufExtension.kt +++ b/protocol/src/main/kotlin/de/fiereu/openmmo/protocols/ByteBufExtension.kt @@ -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 { 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()) diff --git a/protocol/src/main/kotlin/de/fiereu/openmmo/protocols/Protocol.kt b/protocol/src/main/kotlin/de/fiereu/openmmo/protocols/Protocol.kt index 78301ac..57641fe 100644 --- a/protocol/src/main/kotlin/de/fiereu/openmmo/protocols/Protocol.kt +++ b/protocol/src/main/kotlin/de/fiereu/openmmo/protocols/Protocol.kt @@ -57,7 +57,7 @@ abstract class Protocol { "Registering incoming packet: opcode=$opcode, deserializer=${deserializer::class.simpleName}" } - incomingPacketRegistry.put(opcode, deserializer) + incomingPacketRegistry[opcode] = deserializer } fun 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 Protocol.incomingPacket(opcode: UByte, deserializer: PacketDeseria this.registerIncoming(Opcode(opcode), deserializer) } -fun Protocol.outgoingPacket( +inline fun Protocol.outgoingPacket( opcode: UByte, - serializer: PacketSerializer, - dataClass: KClass + serializer: PacketSerializer ) { - this.registerOutgoing(Opcode(opcode), serializer, dataClass) + this.registerOutgoing(Opcode(opcode), serializer, T::class) } diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/GameServer.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/GameServer.kt index 29f4692..8430e70 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/GameServer.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/GameServer.kt @@ -22,7 +22,9 @@ fun main() { .withTlsConfig(tlsConfig) .withPublicKey(KeyLoader.loadPemECPublicKey(resource("game.public.pem"))) .withPrivateKey(KeyLoader.loadPemECPrivateKey(resource("game.private.pem"))) - .withChannelHandlerProvider { GameProtocolHandler(GameServerProtocol(), serverConfig, coroutineScope) } + .withChannelHandlerProvider { + GameProtocolHandler(GameServerProtocol(), serverConfig, coroutineScope) + } .build() server.start() } diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/protocol/game/GameProtocolHandler.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/protocol/game/GameProtocolHandler.kt index cca1f03..9f87da5 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/protocol/game/GameProtocolHandler.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/protocol/game/GameProtocolHandler.kt @@ -1,23 +1,52 @@ package de.fiereu.openmmo.server.game.protocol.game +import de.fiereu.openmmo.common.CharacterInfo +import de.fiereu.openmmo.common.Pokemon +import de.fiereu.openmmo.common.Tile2D +import de.fiereu.openmmo.common.enums.ChatType +import de.fiereu.openmmo.common.enums.Direction +import de.fiereu.openmmo.common.enums.EVs +import de.fiereu.openmmo.common.enums.EncounterType +import de.fiereu.openmmo.common.enums.EntityStatus +import de.fiereu.openmmo.common.enums.IVs +import de.fiereu.openmmo.common.enums.Language +import de.fiereu.openmmo.common.enums.Lighting +import de.fiereu.openmmo.common.enums.MapType +import de.fiereu.openmmo.common.enums.PokemonContainer +import de.fiereu.openmmo.common.enums.Weather import de.fiereu.openmmo.protocols.Protocol +import de.fiereu.openmmo.protocols.game.packets.CharacterEntry +import de.fiereu.openmmo.protocols.game.packets.CharacterListPacket +import de.fiereu.openmmo.protocols.game.packets.ChatMessagePacket import de.fiereu.openmmo.protocols.game.packets.JoinGamePacket +import de.fiereu.openmmo.protocols.game.packets.LoadEntityPacket +import de.fiereu.openmmo.protocols.game.packets.LoadMapPacket +import de.fiereu.openmmo.protocols.game.packets.MapData +import de.fiereu.openmmo.protocols.game.packets.PokemonContainerPacket +import de.fiereu.openmmo.protocols.game.packets.RenderScreenPacket +import de.fiereu.openmmo.protocols.game.packets.RequestCharactersPacket +import de.fiereu.openmmo.protocols.game.packets.RequestPlayerPacket +import de.fiereu.openmmo.protocols.game.packets.SelectCharacterPacket +import de.fiereu.openmmo.protocols.game.packets.SelectedCharacterPacket +import de.fiereu.openmmo.protocols.game.packets.codecs.SkinSet import de.fiereu.openmmo.server.config.ServerConfig import de.fiereu.openmmo.server.game.protocol.game.ext.accept import de.fiereu.openmmo.server.game.protocol.game.ext.buildAndRespond import de.fiereu.openmmo.server.netty.handlers.ProtocolHandler import de.fiereu.openmmo.server.protocol.PacketEvent +import de.fiereu.openmmo.server.protocol.respond import io.github.oshai.kotlinlogging.KotlinLogging import io.netty.channel.ChannelHandlerContext +import java.time.LocalDateTime import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch private val log = KotlinLogging.logger {} class GameProtocolHandler( - protocol: Protocol, - serverConfig: ServerConfig, - private val coroutineScope: CoroutineScope, + protocol: Protocol, + serverConfig: ServerConfig, + private val coroutineScope: CoroutineScope, ) : ProtocolHandler(protocol, serverConfig) { override fun onActive(ctx: ChannelHandlerContext) { @@ -29,6 +58,10 @@ class GameProtocolHandler( coroutineScope.launch { when (event.packet) { is JoinGamePacket -> onJoinGame(event as PacketEvent) + is RequestCharactersPacket -> + onCharacterRequest(event as PacketEvent) + is SelectCharacterPacket -> onCharacterSelected(event as PacketEvent) + is RequestPlayerPacket -> onRequestPlayerPacket(event as PacketEvent) else -> log.warn { "Unhandled game packet type: ${event.packet::class.simpleName}" } } } @@ -41,10 +74,139 @@ class GameProtocolHandler( } // TODO handle player joining the game e.g. authentication - event.accept() - .withPlaytime(1337) - .withRewardPoints(420) - .withBalance(187) - .buildAndRespond(event) + event.accept().withPlaytime(1337).withRewardPoints(420).withBalance(187).buildAndRespond(event) + } + + fun onCharacterRequest(event: PacketEvent) { + log.info { "Player requested character list." } + // TODO fetch character list from database and send it to the client + event.respond( + CharacterListPacket( + listOf( + CharacterEntry( + CharacterInfo( + id = 1, + name = "Character", + namePrefix = "Test D.", + userId = 1, + rivalSex = 0, + lastLogin = LocalDateTime.of(2025, 1, 1, 12, 0), + createdAt = LocalDateTime.of(2020, 1, 1, 12, 0), + money = 1_000_000, + permissions = 8, + remainingSafariSteps = 0, + remainingSafariBalls = 0, + pcExtraSlots = 0, + battleBoxExtraSlots = 0, + templateAmount = 0, + positionRegionId = 1, + positionBankId = 1, + positionMapId = 1, + positionX = 100, + positionY = 100, + repelLeft = 0, + repelItemId = 0, + lureLeft = 0, + lureItemId = 0), + skinSet = SkinSet(), + null, + pokemon = + listOf( + Pokemon( + id = 1, + container = PokemonContainer.PARTY, + containerSlot = 0, + dexId = 497, + seed = 0, + ot = "ich", + nickname = "whhhat?", + level = 100, + hp = 187, + xp = 0, + eVs = EVs(), + iVs = IVs(), + moves = emptyList(), + isShiny = false, + hasHiddenAbility = false, + isAlpha = true, + isSecret = false, + isFatefulEncounter = false, + isRaidEncounter = false, + caughtAt = LocalDateTime.of(2020, 1, 1, 12, 0), + )))))) + } + + fun onCharacterSelected(event: PacketEvent) { + log.info { "Player selected character ${event.packet.characterId}" } + // We need to send the party first. + // Without the HudGui won't render + event.respond( + PokemonContainerPacket( + PokemonContainer.PARTY, hasChange = true, delete = false, pokemon = emptyList())) + event.respond( + SelectedCharacterPacket( + CharacterInfo( + id = 1, + name = "Character", + namePrefix = "Test D.", + userId = 1, + rivalSex = 0, + lastLogin = LocalDateTime.of(2025, 1, 1, 12, 0), + createdAt = LocalDateTime.of(2020, 1, 1, 12, 0), + money = 1_000_000, + permissions = 8, + remainingSafariSteps = 0, + remainingSafariBalls = 0, + pcExtraSlots = 0, + battleBoxExtraSlots = 0, + templateAmount = 0, + positionRegionId = 1, + positionBankId = 1, + positionMapId = 1, + positionX = 100, + positionY = 100, + repelLeft = 0, + repelItemId = 0, + lureLeft = 0, + lureItemId = 0))) + event.respond( + ChatMessagePacket(ChatType.GAME_NOTIFICATIONS, Language.EN, "Welcome to OpenMMO!", "")) + event.respond( + LoadMapPacket( + true, + true, + 0, + 6, + 6, + MapData.GbaMapData( + 15, + 10, + 12, + 14, + 2, + 2, + listOf(Tile2D(8, 0), Tile2D(8, 0), Tile2D(8, 0), Tile2D(8, 0)), + Lighting.REGULAR, + Weather.IN_HOUSE_WEATHER, + MapType.INSIDE, + EncounterType.RANDOM))) + } + + fun onRequestPlayerPacket(event: PacketEvent) { + event.respond( + LoadEntityPacket( + 1, + SkinSet(), + "Character", + 0, + 6, + 6, + 8, + 5, + Direction.SOUTH, + EntityStatus.NONE, + hasFollower = false, + 0)) + event.respond(RenderScreenPacket(true)) } } diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/protocol/game/ext/JoinGameResponsePacketExtension.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/protocol/game/ext/JoinGameResponsePacketExtension.kt index 3fa2ea1..0581c67 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/protocol/game/ext/JoinGameResponsePacketExtension.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/protocol/game/ext/JoinGameResponsePacketExtension.kt @@ -13,4 +13,4 @@ fun PacketEvent.accept() = JoinResponsePacketBuilder.accept() fun JoinResponsePacketBuilder.buildAndRespond(event: PacketEvent) { event.respond(this.build()) -} \ No newline at end of file +} diff --git a/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/LoginServer.kt b/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/LoginServer.kt index 6da0866..5020633 100644 --- a/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/LoginServer.kt +++ b/server.login/src/main/kotlin/de/fiereu/openmmo/server/login/LoginServer.kt @@ -23,7 +23,7 @@ fun main() { .withPublicKey(KeyLoader.loadPemECPublicKey(resource("game.public.pem"))) .withPrivateKey(KeyLoader.loadPemECPrivateKey(resource("game.private.pem"))) .withChannelHandlerProvider { - LoginProtocolHandler(LoginServerProtocol(), serverConfig, coroutineScope) + LoginProtocolHandler(LoginServerProtocol(), serverConfig, coroutineScope) } .build() server.start()