add initial game packets
This commit is contained in:
1 parent
621440690f
commit
8c82990b9a
45 files changed
+1806
-116
No files matched your search
@@ -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())
|
||||
}
|
||||
}
|
||||
+71
@@ -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<CharacterEntry>)
|
||||
|
||||
data class CharacterEntry(
|
||||
val characterInfo: CharacterInfo,
|
||||
val skinSet: SkinSet,
|
||||
val guildId: Int? = null,
|
||||
val pokemon: List<Pokemon>
|
||||
) {
|
||||
init {
|
||||
require(pokemon.size <= 6) { "Party size can't be more than 6 pokemon" }
|
||||
}
|
||||
}
|
||||
|
||||
class CharacterListSerializer : PacketSerializer<CharacterListPacket> {
|
||||
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<CharacterListPacket> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
+57
@@ -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<ChatMessagePacket> {
|
||||
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<ChatMessagePacket> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-5
@@ -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<JoinGamePacket> {
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
+15
-28
@@ -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<JoinResponsePacket> {
|
||||
class JoinResponseSerializer : PacketSerializer<JoinResponsePacket> {
|
||||
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<JoinResponsePacket> {
|
||||
}
|
||||
}
|
||||
|
||||
class JoinResponsePacketDeserializer : PacketDeserializer<JoinResponsePacketDeserializer.JoinResponsePacket> {
|
||||
class JoinResponseDeserializer : PacketDeserializer<JoinResponseDeserializer.JoinResponsePacket> {
|
||||
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<JoinResponsePacketDese
|
||||
val serverCurrentSecond = buffer.readUnsignedIntLE().toInt()
|
||||
|
||||
return JoinResponsePacket(
|
||||
true,
|
||||
JoinResponsePacket.GameStats(playtime, rewardPoints, balance),
|
||||
JoinResponsePacket.TimeInfo(serverDayStartSecond, serverCurrentSecond)
|
||||
)
|
||||
true,
|
||||
JoinResponsePacket.GameStats(playtime, rewardPoints, balance),
|
||||
JoinResponsePacket.TimeInfo(serverDayStartSecond, serverCurrentSecond))
|
||||
}
|
||||
}
|
||||
}
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
package de.fiereu.openmmo.protocols.game.packets
|
||||
|
||||
import de.fiereu.openmmo.common.enums.Direction
|
||||
import de.fiereu.openmmo.common.enums.EntityStatus
|
||||
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.SkinSet
|
||||
import de.fiereu.openmmo.protocols.game.packets.codecs.readSkinsLE
|
||||
import de.fiereu.openmmo.protocols.game.packets.codecs.writeSkinsLE
|
||||
import de.fiereu.openmmo.protocols.readUtf16LE
|
||||
import de.fiereu.openmmo.protocols.writeShortLE
|
||||
import de.fiereu.openmmo.protocols.writeUtf16LE
|
||||
import io.netty.buffer.ByteBuf
|
||||
|
||||
data class LoadEntityPacket(
|
||||
val entityId: Long,
|
||||
val skin: SkinSet,
|
||||
val name: String,
|
||||
val regionId: Int,
|
||||
val bankId: Int,
|
||||
val mapId: Int,
|
||||
val x: Int,
|
||||
val y: Int,
|
||||
val facing: Direction,
|
||||
val status: EntityStatus,
|
||||
val hasFollower: Boolean,
|
||||
val followerDexId: Short,
|
||||
)
|
||||
|
||||
class LoadEntitySerializer : PacketSerializer<LoadEntityPacket> {
|
||||
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<LoadEntityPacket> {
|
||||
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,
|
||||
)
|
||||
}
|
||||
}
|
||||
+219
@@ -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<Tile2D>,
|
||||
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<LoadMapPacket> {
|
||||
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<LoadMapPacket> {
|
||||
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<Tile2D>()
|
||||
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)
|
||||
}
|
||||
}
|
||||
+53
@@ -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<Pokemon>,
|
||||
) {
|
||||
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<PokemonContainerPacket> {
|
||||
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<PokemonContainerPacket> {
|
||||
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<Pokemon>(readByte().toInt()) { readPokemonLE() }
|
||||
return PokemonContainerPacket(pokemonContainer, hasChange, delete, pokemon)
|
||||
}
|
||||
}
|
||||
+19
@@ -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<RenderScreenPacket> {
|
||||
override fun serialize(packet: RenderScreenPacket, buffer: ByteBuf) {
|
||||
buffer.writeBoolean(packet.renderScreen)
|
||||
}
|
||||
}
|
||||
|
||||
class RenderScreenDeserializer : PacketDeserializer<RenderScreenPacket> {
|
||||
override fun deserialize(buffer: ByteBuf): RenderScreenPacket {
|
||||
return RenderScreenPacket(buffer.readBoolean())
|
||||
}
|
||||
}
|
||||
+17
@@ -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<RequestCharactersPacket> {
|
||||
override fun serialize(packet: RequestCharactersPacket, buffer: ByteBuf) {}
|
||||
}
|
||||
|
||||
class RequestCharactersDeserializer : PacketDeserializer<RequestCharactersPacket> {
|
||||
override fun deserialize(buffer: ByteBuf): RequestCharactersPacket {
|
||||
return RequestCharactersPacket()
|
||||
}
|
||||
}
|
||||
+17
@@ -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<RequestPlayerPacket> {
|
||||
override fun serialize(packet: RequestPlayerPacket, buffer: ByteBuf) {}
|
||||
}
|
||||
|
||||
class RequestPlayerDeserializePacket : PacketDeserializer<RequestPlayerPacket> {
|
||||
override fun deserialize(buffer: ByteBuf): RequestPlayerPacket {
|
||||
return RequestPlayerPacket()
|
||||
}
|
||||
}
|
||||
+25
@@ -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<SelectCharacterPacket> {
|
||||
override fun serialize(packet: SelectCharacterPacket, buffer: ByteBuf) {
|
||||
buffer.apply {
|
||||
buffer.writeLongLE(packet.characterId)
|
||||
buffer.writeLongLE(packet.characterIdHash)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SelectCharacterDeserializer : PacketDeserializer<SelectCharacterPacket> {
|
||||
override fun deserialize(buffer: ByteBuf): SelectCharacterPacket =
|
||||
buffer.run {
|
||||
val characterId = buffer.readLong()
|
||||
val characterIdHash = buffer.readLong()
|
||||
return SelectCharacterPacket(characterId, characterIdHash)
|
||||
}
|
||||
}
|
||||
+30
@@ -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<SelectedCharacterPacket> {
|
||||
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<SelectedCharacterPacket> {
|
||||
override fun deserialize(buffer: ByteBuf): SelectedCharacterPacket =
|
||||
buffer.run {
|
||||
val hasCharacter = readBoolean()
|
||||
val character = if (hasCharacter) readCharacterInfoLE(false) else null
|
||||
return SelectedCharacterPacket(character)
|
||||
}
|
||||
}
|
||||
+9
-12
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
+136
@@ -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)
|
||||
}
|
||||
+179
@@ -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)
|
||||
}
|
||||
@@ -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, Skin>(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> = 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<SkinSlot>,
|
||||
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
|
||||
}
|
||||
+14
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user