From 7caab88e7ad9359c32f3e2572200e3f41e79acf7 Mon Sep 17 00:00:00 2001 From: Obique PSWG Date: Thu, 30 Apr 2020 16:19:38 -0400 Subject: [PATCH] Initial version of packet updates --- .../Encodable.java => data/GameVersion.kt} | 20 ++- .../common/data/encodables/gcw/GcwRegion.java | 66 -------- .../gcw/{GcwRegionZone.java => GcwRegion.kt} | 65 +++----- .../data/encodables/gcw/GcwRegionZone.kt | 56 +++++++ .../projectswg/common/encoding/Encodable.kt | 153 ++++++++++++++++++ .../common/network/packets/SWGPacket.java | 115 ------------- .../common/network/packets/SWGPacket.kt | 91 +++++++++++ .../holo/{HoloPacket.java => HoloPacket.kt} | 30 ++-- 8 files changed, 347 insertions(+), 249 deletions(-) rename src/main/java/com/projectswg/common/{encoding/Encodable.java => data/GameVersion.kt} (77%) delete mode 100644 src/main/java/com/projectswg/common/data/encodables/gcw/GcwRegion.java rename src/main/java/com/projectswg/common/data/encodables/gcw/{GcwRegionZone.java => GcwRegion.kt} (50%) create mode 100644 src/main/java/com/projectswg/common/data/encodables/gcw/GcwRegionZone.kt create mode 100644 src/main/java/com/projectswg/common/encoding/Encodable.kt delete mode 100644 src/main/java/com/projectswg/common/network/packets/SWGPacket.java create mode 100644 src/main/java/com/projectswg/common/network/packets/SWGPacket.kt rename src/main/java/com/projectswg/common/network/packets/swg/holo/{HoloPacket.java => HoloPacket.kt} (60%) diff --git a/src/main/java/com/projectswg/common/encoding/Encodable.java b/src/main/java/com/projectswg/common/data/GameVersion.kt similarity index 77% rename from src/main/java/com/projectswg/common/encoding/Encodable.java rename to src/main/java/com/projectswg/common/data/GameVersion.kt index 65a0889..8a8392b 100644 --- a/src/main/java/com/projectswg/common/encoding/Encodable.java +++ b/src/main/java/com/projectswg/common/data/GameVersion.kt @@ -1,5 +1,5 @@ /*********************************************************************************** - * Copyright (c) 2018 /// Project SWG /// www.projectswg.com * + * Copyright (c) 2020 /// Project SWG /// www.projectswg.com * * * * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * @@ -7,29 +7,27 @@ * continue playing a game similar to the one they used to play. We are basing * * it on the final publish of the game prior to end-game events. * * * - * This file is part of PSWGCommon. * + * This file is part of Holocore. * * * * --------------------------------------------------------------------------------* * * - * PSWGCommon is free software: you can redistribute it and/or modify * + * Holocore is free software: you can redistribute it and/or modify * * it under the terms of the GNU Affero General Public License as * * published by the Free Software Foundation, either version 3 of the * * License, or (at your option) any later version. * * * - * PSWGCommon is distributed in the hope that it will be useful, * + * Holocore is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Affero General Public License for more details. * * * * You should have received a copy of the GNU Affero General Public License * - * along with PSWGCommon. If not, see . * + * along with Holocore. If not, see . * ***********************************************************************************/ -package com.projectswg.common.encoding; -import com.projectswg.common.network.NetBuffer; +package com.projectswg.common.data -public interface Encodable { - void decode(NetBuffer data); - byte [] encode(); - int getLength(); +enum class GameVersion { + CU, + NGE } diff --git a/src/main/java/com/projectswg/common/data/encodables/gcw/GcwRegion.java b/src/main/java/com/projectswg/common/data/encodables/gcw/GcwRegion.java deleted file mode 100644 index f39945e..0000000 --- a/src/main/java/com/projectswg/common/data/encodables/gcw/GcwRegion.java +++ /dev/null @@ -1,66 +0,0 @@ -/*********************************************************************************** - * Copyright (c) 2018 /// Project SWG /// www.projectswg.com * - * * - * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * - * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * - * Our goal is to create an emulator which will provide a server for players to * - * continue playing a game similar to the one they used to play. We are basing * - * it on the final publish of the game prior to end-game events. * - * * - * This file is part of Holocore. * - * * - * --------------------------------------------------------------------------------* - * * - * Holocore is free software: you can redistribute it and/or modify * - * it under the terms of the GNU Affero General Public License as * - * published by the Free Software Foundation, either version 3 of the * - * License, or (at your option) any later version. * - * * - * Holocore is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU Affero General Public License for more details. * - * * - * You should have received a copy of the GNU Affero General Public License * - * along with Holocore. If not, see . * - ***********************************************************************************/ -package com.projectswg.common.data.encodables.gcw; - -import com.projectswg.common.encoding.Encodable; -import com.projectswg.common.network.NetBuffer; - -import java.util.Collection; - -public class GcwRegion implements Encodable { - - private final String planetName; - private final Collection zones; - - public GcwRegion(String planetName, Collection zones) { - this.planetName = planetName; - this.zones = zones; - } - - @Override - public void decode(NetBuffer data) { - throw new UnsupportedOperationException(); - } - - @Override - public byte[] encode() { - NetBuffer buffer = NetBuffer.allocate(getLength()); - - buffer.addAscii(planetName); - buffer.addList(zones); - - return buffer.array(); - } - - @Override - public int getLength() { - int nameLength = 2 + planetName.length(); - int zonesLength = 4 + zones.stream().mapToInt(GcwRegionZone::getLength).sum(); - - return nameLength + zonesLength; - } -} diff --git a/src/main/java/com/projectswg/common/data/encodables/gcw/GcwRegionZone.java b/src/main/java/com/projectswg/common/data/encodables/gcw/GcwRegion.kt similarity index 50% rename from src/main/java/com/projectswg/common/data/encodables/gcw/GcwRegionZone.java rename to src/main/java/com/projectswg/common/data/encodables/gcw/GcwRegion.kt index 1213041..75a4f7a 100644 --- a/src/main/java/com/projectswg/common/data/encodables/gcw/GcwRegionZone.java +++ b/src/main/java/com/projectswg/common/data/encodables/gcw/GcwRegion.kt @@ -1,67 +1,50 @@ /*********************************************************************************** * Copyright (c) 2018 /// Project SWG /// www.projectswg.com * - * * + * * * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * * Our goal is to create an emulator which will provide a server for players to * * continue playing a game similar to the one they used to play. We are basing * * it on the final publish of the game prior to end-game events. * - * * + * * * This file is part of Holocore. * - * * + * * * --------------------------------------------------------------------------------* - * * + * * * Holocore is free software: you can redistribute it and/or modify * * it under the terms of the GNU Affero General Public License as * * published by the Free Software Foundation, either version 3 of the * * License, or (at your option) any later version. * - * * + * * * Holocore is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Affero General Public License for more details. * - * * + * * * You should have received a copy of the GNU Affero General Public License * - * along with Holocore. If not, see . * - ***********************************************************************************/ -package com.projectswg.common.data.encodables.gcw; + * along with Holocore. If not, see //www.gnu.org/licenses/>. * + */ +package com.projectswg.common.data.encodables.gcw -import com.projectswg.common.encoding.Encodable; -import com.projectswg.common.network.NetBuffer; +import com.projectswg.common.encoding.Encodable2 +import com.projectswg.common.encoding.EncodableBuilder +import com.projectswg.common.encoding.EncodableBuilder.Companion.ascii +import com.projectswg.common.encoding.EncodableBuilder.Companion.list -public class GcwRegionZone implements Encodable { +class GcwRegion() : Encodable2(builder) { - private final String name; - private final float x; - private final float z; - private final float radius; + var planetName: String = "" + val zones = ArrayList() - public GcwRegionZone(String name, float x, float z, float radius) { - this.name = name; - this.x = x; - this.z = z; - this.radius = radius; + constructor(planetName: String, zones: Collection) : this() { + this.planetName = planetName + this.zones.addAll(zones) } - @Override - public void decode(NetBuffer data) { - - } - - @Override - public byte[] encode() { - NetBuffer buffer = NetBuffer.allocate(getLength()); - - buffer.addAscii(name); - buffer.addFloat(x); - buffer.addFloat(z); - buffer.addFloat(radius); - - return buffer.array(); - } - - @Override - public int getLength() { - return 2 + name.length() + Float.BYTES * 3; + companion object { + private val builder = EncodableBuilder(arrayOf( + ascii(GcwRegion::planetName), + list(GcwRegion::zones) + )) } } diff --git a/src/main/java/com/projectswg/common/data/encodables/gcw/GcwRegionZone.kt b/src/main/java/com/projectswg/common/data/encodables/gcw/GcwRegionZone.kt new file mode 100644 index 0000000..3e84138 --- /dev/null +++ b/src/main/java/com/projectswg/common/data/encodables/gcw/GcwRegionZone.kt @@ -0,0 +1,56 @@ +/*********************************************************************************** + * Copyright (c) 2018 /// Project SWG /// www.projectswg.com * + * * + * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * + * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * + * Our goal is to create an emulator which will provide a server for players to * + * continue playing a game similar to the one they used to play. We are basing * + * it on the final publish of the game prior to end-game events. * + * * + * This file is part of Holocore. * + * * + * --------------------------------------------------------------------------------* + * * + * Holocore is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Affero General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * Holocore is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Affero General Public License for more details. * + * * + * You should have received a copy of the GNU Affero General Public License * + * along with Holocore. If not, see //www.gnu.org/licenses/>. * + */ +package com.projectswg.common.data.encodables.gcw + +import com.projectswg.common.encoding.Encodable2 +import com.projectswg.common.encoding.EncodableBuilder +import com.projectswg.common.encoding.EncodableBuilder.Companion.ascii +import com.projectswg.common.encoding.EncodableBuilder.Companion.float32 + +class GcwRegionZone() : Encodable2(builder) { + + var name: String = "" + var x: Double = 0.0 + var z: Double = 0.0 + var radius: Double = 0.0 + + constructor(name: String, x: Double, z: Double, radius: Double) : this() { + this.name = name + this.x = x + this.z = z + this.radius = radius + } + + companion object { + private val builder = EncodableBuilder(arrayOf( + ascii(GcwRegionZone::name), + float32(GcwRegionZone::x), + float32(GcwRegionZone::z), + float32(GcwRegionZone::radius) + )) + } +} diff --git a/src/main/java/com/projectswg/common/encoding/Encodable.kt b/src/main/java/com/projectswg/common/encoding/Encodable.kt new file mode 100644 index 0000000..b308512 --- /dev/null +++ b/src/main/java/com/projectswg/common/encoding/Encodable.kt @@ -0,0 +1,153 @@ +/*********************************************************************************** + * Copyright (c) 2018 /// Project SWG /// www.projectswg.com * + * * + * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * + * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * + * Our goal is to create an emulator which will provide a server for players to * + * continue playing a game similar to the one they used to play. We are basing * + * it on the final publish of the game prior to end-game events. * + * * + * This file is part of PSWGCommon. * + * * + * --------------------------------------------------------------------------------* + * * + * PSWGCommon is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Affero General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * PSWGCommon is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Affero General Public License for more details. * + * * + * You should have received a copy of the GNU Affero General Public License * + * along with PSWGCommon. If not, see //www.gnu.org/licenses/>. * + */ +package com.projectswg.common.encoding + +import com.projectswg.common.data.GameVersion +import com.projectswg.common.network.NetBuffer +import kotlin.reflect.KMutableProperty1 +import kotlin.reflect.KProperty1 + +interface Encodable { + fun decode(data: NetBuffer) + fun encode(): ByteArray + val length: Int +} + +abstract class Encodable2>(private val builder: EncodableBuilder) : Encodable { + + override fun decode(data: NetBuffer) = builder.decode(this as T, GameVersion.NGE, data) + override fun encode(): ByteArray = builder.encode(this as T, GameVersion.NGE) + override val length: Int + get() = builder.length(this as T, GameVersion.NGE) + + fun decode(data: NetBuffer, version: GameVersion) = builder.decode(this as T, version, data) + fun encode(version: GameVersion): ByteArray = builder.encode(this as T, version) + fun getLength(version: GameVersion): Int = builder.length(this as T, version) + +} + +data class EncoderOperation(val encode: (K, GameVersion, NetBuffer) -> Unit, val decode: (K, GameVersion, NetBuffer) -> Unit, val length: (K, GameVersion) -> Int) +data class EncoderOperationFundamental(val encode: (T, GameVersion, NetBuffer) -> Unit, val decode: (GameVersion, NetBuffer) -> T, val length: (T, GameVersion) -> Int) + +class EncodableBuilder>(private val variables: Array>) { + + fun decode(instance: T, version: GameVersion, data: NetBuffer) { + for (variable in variables) { + variable.decode(instance, version, data) + } + } + + fun encode(instance: T, version: GameVersion): ByteArray { + val ret = NetBuffer.allocate(length(instance, version)) + for (variable in variables) { + variable.encode(instance, version, ret) + } + return ret.array() + } + + fun length(instance: T, version: GameVersion): Int { + var length = 0 + for (variable in variables) { + length += variable.length(instance, version) + } + return length + } + + companion object { + fun conditional(prop: KMutableProperty1, cu: ((KMutableProperty1) -> EncoderOperation)? = null, nge: ((KMutableProperty1) -> EncoderOperation)? = null): EncoderOperation { + if (cu == null && nge != null) { + val op = nge(prop) + return EncoderOperation( + { inst, ver, buf -> if (ver == GameVersion.NGE) op.encode(inst, ver, buf) }, + { inst, ver, buf -> if (ver == GameVersion.NGE) op.decode(inst, ver, buf) }, + { inst, ver -> if (ver == GameVersion.NGE) op.length(inst, ver) else 0 }) + } else if (cu != null && nge == null) { + val op = cu(prop) + return EncoderOperation( + { inst, ver, buf -> if (ver == GameVersion.CU) op.encode(inst, ver, buf) }, + { inst, ver, buf -> if (ver == GameVersion.CU) op.decode(inst, ver, buf) }, + { inst, ver -> if (ver == GameVersion.CU) op.length(inst, ver) else 0 }) + } else if (cu != null && nge != null) { + val cuOp = cu(prop) + val ngeOp = nge(prop) + return EncoderOperation( + { inst, ver, buf -> if (ver == GameVersion.CU) cuOp.encode(inst, ver, buf) else if (ver == GameVersion.NGE) ngeOp.encode(inst, ver, buf) }, + { inst, ver, buf -> if (ver == GameVersion.CU) cuOp.decode(inst, ver, buf) else if (ver == GameVersion.NGE) ngeOp.decode(inst, ver, buf) }, + { inst, ver -> if (ver == GameVersion.CU) cuOp.length(inst, ver) else if (ver == GameVersion.NGE) ngeOp.length(inst, ver) else 0 }) + } + + throw IllegalArgumentException("CU or NGE must be defined") + } + + inline fun > list(prop: KProperty1>) = list(prop, ::encodable) + fun > list(prop: KProperty1, encoder: () -> EncoderOperationFundamental): EncoderOperation { + val op = encoder() + return EncoderOperation( + encode = { inst, ver, buf -> + val list = prop.get(inst) + buf.addInt(list.size) + for (item in list) + op.encode(item, ver, buf) + }, + decode = { inst, ver, buf -> + val length = buf.int + val list = prop.get(inst) + list.clear() + for (i in 0 until length) { + list.add(op.decode(ver, buf)) + } + }, + length = { inst, ver -> + var length = 4 + for (item in prop.get(inst)) + length += op.length(item, ver) + length + } + ) + } + + inline fun > encodable(prop: KMutableProperty1) = EncoderOperation({ inst, _, buf -> buf.addEncodable(prop.get(inst)) }, { inst, _, buf -> prop.set(inst, buf.getEncodable((K::class.java))) }, { inst, _ -> prop.get(inst).length }) + fun ascii(prop: KMutableProperty1) = EncoderOperation({ inst, _, buf -> buf.addAscii(prop.get(inst)) }, { inst, _, buf -> prop.set(inst, buf.ascii) }, { inst, _ -> 2 + prop.get(inst).length }) + fun unicode(prop: KMutableProperty1) = EncoderOperation({ inst, _, buf -> buf.addUnicode(prop.get(inst)) }, { inst, _, buf -> prop.set(inst, buf.unicode) }, { inst, _ -> 4 + prop.get(inst).length*2 }) + fun float32(prop: KMutableProperty1) = EncoderOperation({ inst, _, buf -> buf.addFloat(prop.get(inst).toFloat()) }, { inst, _, buf -> prop.set(inst, buf.float.toDouble()) }, { _, _ -> 4 }) + fun int8(prop: KMutableProperty1) = EncoderOperation({ inst, _, buf -> buf.addByte(prop.get(inst)) }, { inst, _, buf -> prop.set(inst, buf.byte.toInt()) }, { _, _ -> 1 }) + fun int16(prop: KMutableProperty1) = EncoderOperation({ inst, _, buf -> buf.addShort(prop.get(inst)) }, { inst, _, buf -> prop.set(inst, buf.short.toInt()) }, { _, _ -> 2 }) + fun int32(prop: KMutableProperty1) = EncoderOperation({ inst, _, buf -> buf.addInt(prop.get(inst)) }, { inst, _, buf -> prop.set(inst, buf.int) }, { _, _ -> 4 }) + fun int64(prop: KMutableProperty1) = EncoderOperation({ inst, _, buf -> buf.addLong(prop.get(inst)) }, { inst, _, buf -> prop.set(inst, buf.long) }, { _, _ -> 8 }) + + inline fun > encodable() = EncoderOperationFundamental({ prop, _, buf -> buf.addEncodable(prop) }, { _, buf -> buf.getEncodable(K::class.java) }, Encodable2::getLength) + fun ascii() = EncoderOperationFundamental({ prop, _, buf -> buf.addAscii(prop) }, { _, buf -> buf.ascii }, { str, _ -> 2 + str.length }) + fun unicode() = EncoderOperationFundamental({ prop, _, buf -> buf.addUnicode(prop) }, { _, buf -> buf.unicode }, { str, _ -> 4 + str.length*2 }) + fun float32() = EncoderOperationFundamental({ prop, _, buf -> buf.addFloat(prop.toFloat()) }, { _, buf -> buf.float.toDouble() }, { _, _ -> 4 }) + fun int8() = EncoderOperationFundamental({ prop, _, buf -> buf.addByte(prop) }, { _, buf -> buf.byte.toInt() }, { _, _ -> 1 }) + fun int16() = EncoderOperationFundamental({ prop, _, buf -> buf.addShort(prop) }, { _, buf -> buf.short.toInt() }, { _, _ -> 2 }) + fun int32() = EncoderOperationFundamental({ prop, _, buf -> buf.addInt(prop) }, { _, buf -> buf.int }, { _, _ -> 4 }) + fun int64() = EncoderOperationFundamental({ prop, _, buf -> buf.addLong(prop) }, { _, buf -> buf.long }, { _, _ -> 8 }) + + } + +} diff --git a/src/main/java/com/projectswg/common/network/packets/SWGPacket.java b/src/main/java/com/projectswg/common/network/packets/SWGPacket.java deleted file mode 100644 index c5a020b..0000000 --- a/src/main/java/com/projectswg/common/network/packets/SWGPacket.java +++ /dev/null @@ -1,115 +0,0 @@ -/*********************************************************************************** - * Copyright (c) 2018 /// Project SWG /// www.projectswg.com * - * * - * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * - * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * - * Our goal is to create an emulator which will provide a server for players to * - * continue playing a game similar to the one they used to play. We are basing * - * it on the final publish of the game prior to end-game events. * - * * - * This file is part of PSWGCommon. * - * * - * --------------------------------------------------------------------------------* - * * - * PSWGCommon is free software: you can redistribute it and/or modify * - * it under the terms of the GNU Affero General Public License as * - * published by the Free Software Foundation, either version 3 of the * - * License, or (at your option) any later version. * - * * - * PSWGCommon is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU Affero General Public License for more details. * - * * - * You should have received a copy of the GNU Affero General Public License * - * along with PSWGCommon. If not, see . * - ***********************************************************************************/ - -package com.projectswg.common.network.packets; - -import java.net.SocketAddress; - -import com.projectswg.common.data.CRC; -import me.joshlarson.jlcommon.log.Log; -import com.projectswg.common.network.NetBuffer; - -public abstract class SWGPacket { - - private SocketAddress socketAddress; - private PacketType type; - private int crc; - - public SWGPacket() { - this.socketAddress = null; - this.type = PacketType.UNKNOWN; - this.crc = 0; - } - - /** - * Sets the socket address that this packet was sent to or received from. Setting this value after it's received, or before it's sent has no effect - * - * @param socketAddress the socket address - */ - public void setSocketAddress(SocketAddress socketAddress) { - this.socketAddress = socketAddress; - } - - public SocketAddress getSocketAddress() { - return socketAddress; - } - - public int getSWGOpcode() { - return crc; - } - - public PacketType getPacketType() { - return type; - } - - public boolean checkDecode(NetBuffer data, int crc) { - data.getShort(); - this.crc = data.getInt(); - this.type = PacketType.fromCrc(crc); - if (this.crc == crc) - return true; - Log.w("SWG Opcode does not match actual! Expected: 0x%08X Actual: 0x%08X", crc, getSWGOpcode()); - return false; - } - - public abstract void decode(NetBuffer data); - public abstract NetBuffer encode(); - - protected void packetAssert(boolean condition, String constraint) { - if (!condition) - throw new PacketSerializationException(this, constraint); - } - - public static int getCrc(String string) { - return CRC.getCrc(string); - } - - @Override - public final String toString() { - return getClass().getSimpleName() + "[" + getPacketData() + "]"; - } - - protected String getPacketData() { - return "?"; - } - - protected final String createPacketInformation(Object ... data) { - assert data.length % 2 == 0; - StringBuilder str = new StringBuilder(); - for (int i = 0; i+1 < data.length; i += 2) { - assert data[i] instanceof String; - String key = data[i] == null ? "null" : data[i].toString(); - if (i > 0) - str.append(' '); - str.append(key); - str.append('='); - str.append(data[i+1]); - } - return str.toString(); - } - -} diff --git a/src/main/java/com/projectswg/common/network/packets/SWGPacket.kt b/src/main/java/com/projectswg/common/network/packets/SWGPacket.kt new file mode 100644 index 0000000..62401bc --- /dev/null +++ b/src/main/java/com/projectswg/common/network/packets/SWGPacket.kt @@ -0,0 +1,91 @@ +/*********************************************************************************** + * Copyright (c) 2018 /// Project SWG /// www.projectswg.com * + * * + * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * + * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * + * Our goal is to create an emulator which will provide a server for players to * + * continue playing a game similar to the one they used to play. We are basing * + * it on the final publish of the game prior to end-game events. * + * * + * This file is part of PSWGCommon. * + * * + * --------------------------------------------------------------------------------* + * * + * PSWGCommon is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Affero General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * PSWGCommon is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Affero General Public License for more details. * + * * + * You should have received a copy of the GNU Affero General Public License * + * along with PSWGCommon. If not, see //www.gnu.org/licenses/>. * + */ +package com.projectswg.common.network.packets + +import com.projectswg.common.data.CRC +import com.projectswg.common.network.NetBuffer +import me.joshlarson.jlcommon.log.Log +import java.net.SocketAddress + +abstract class SWGPacket { + /** + * The socket address that this packet was sent to or received from. Setting this value after it's received, or before it's sent has no effect + */ + var socketAddress: SocketAddress? = null + var packetType: PacketType = PacketType.UNKNOWN + private set + protected open val packetData: String + get() = "?" + private var crc: Int = 0 + + abstract fun decode(data: NetBuffer) + abstract fun encode(): NetBuffer + + protected fun packetAssert(condition: Boolean, constraint: String?) { + if (!condition) throw PacketSerializationException(this, constraint) + } + + protected fun createPacketInformation(vararg data: Any?): String { + assert(data.size % 2 == 0) + val str = StringBuilder() + var i = 0 + while (i + 1 < data.size) { + assert(data[i] is String) + val key = if (data[i] == null) "null" else data[i].toString() + if (i > 0) str.append(' ') + str.append(key) + str.append('=') + str.append(data[i + 1]) + i += 2 + } + return str.toString() + } + + fun decode(op: NetBuffer.() -> Unit) { + + } + + fun checkDecode(data: NetBuffer, crc: Int): Boolean { + data.short + this.crc = data.int + this.packetType = PacketType.fromCrc(crc) + if (this.crc == crc) + return true + Log.w("SWG Opcode does not match actual! Expected: 0x%08X Actual: 0x%08X", crc, this.crc) + return false + } + + override fun toString(): String = javaClass.simpleName + "[" + packetData + "]" + + companion object { + @JvmStatic + fun getCrc(string: String?): Int { + return CRC.getCrc(string) + } + } + +} diff --git a/src/main/java/com/projectswg/common/network/packets/swg/holo/HoloPacket.java b/src/main/java/com/projectswg/common/network/packets/swg/holo/HoloPacket.kt similarity index 60% rename from src/main/java/com/projectswg/common/network/packets/swg/holo/HoloPacket.java rename to src/main/java/com/projectswg/common/network/packets/swg/holo/HoloPacket.kt index b3bbdbc..7e19817 100644 --- a/src/main/java/com/projectswg/common/network/packets/swg/holo/HoloPacket.java +++ b/src/main/java/com/projectswg/common/network/packets/swg/holo/HoloPacket.kt @@ -1,37 +1,35 @@ /*********************************************************************************** * Copyright (c) 2018 /// Project SWG /// www.projectswg.com * - * * + * * * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * * Our goal is to create an emulator which will provide a server for players to * * continue playing a game similar to the one they used to play. We are basing * * it on the final publish of the game prior to end-game events. * - * * + * * * This file is part of PSWGCommon. * - * * + * * * --------------------------------------------------------------------------------* - * * + * * * PSWGCommon is free software: you can redistribute it and/or modify * * it under the terms of the GNU Affero General Public License as * * published by the Free Software Foundation, either version 3 of the * * License, or (at your option) any later version. * - * * + * * * PSWGCommon is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Affero General Public License for more details. * - * * + * * * You should have received a copy of the GNU Affero General Public License * - * along with PSWGCommon. If not, see . * - ***********************************************************************************/ -package com.projectswg.common.network.packets.swg.holo; + * along with PSWGCommon. If not, see //www.gnu.org/licenses/>. * + */ +package com.projectswg.common.network.packets.swg.holo -import com.projectswg.common.network.NetBuffer; -import com.projectswg.common.network.packets.SWGPacket; +import com.projectswg.common.network.NetBuffer +import com.projectswg.common.network.packets.SWGPacket -public abstract class HoloPacket extends SWGPacket { - - public abstract void decode(NetBuffer data); - public abstract NetBuffer encode(); - +abstract class HoloPacket : SWGPacket() { + abstract override fun decode(data: NetBuffer) + abstract override fun encode(): NetBuffer }