sync
This commit is contained in:
@@ -12,6 +12,7 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation(kotlin("stdlib"))
|
||||
@Suppress("VulnerableLibrariesLocal")
|
||||
compileOnly("io.github.waterfallmc:waterfall-api:1.19-R0.1-SNAPSHOT")
|
||||
compileOnly("net.md-5:bungeecord-proxy:1.19-R0.1-SNAPSHOT") {
|
||||
isTransitive = false
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
package cc.maxmc.msm.common
|
||||
|
||||
import cc.maxmc.msm.common.network.ClusterMsgCodec
|
||||
import io.netty.channel.Channel
|
||||
import net.md_5.bungee.BungeeCord
|
||||
|
||||
object PacketInjector {
|
||||
private val bungee = BungeeCord.getInstance()
|
||||
private val field = bungee::class.java.getDeclaredField("listeners").also {
|
||||
it.isAccessible = true
|
||||
}
|
||||
|
||||
fun inject() {
|
||||
val channel = getChannel()
|
||||
channel.pipeline().addFirst("ClusterMsgCodec", ClusterMsgCodec)
|
||||
}
|
||||
|
||||
private fun getChannel(): Channel {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val channelList = field.get(bungee) as Collection<Channel>
|
||||
while (true) {
|
||||
return channelList.firstOrNull() ?: continue
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package cc.maxmc.msm.common
|
||||
|
||||
object Placeholder
|
||||
@@ -2,8 +2,12 @@ package cc.maxmc.msm.common.network
|
||||
|
||||
import io.netty.buffer.ByteBuf
|
||||
|
||||
interface BungeePacket: Cloneable {
|
||||
fun encode(buf: ByteBuf)
|
||||
abstract class BungeePacket: Cloneable {
|
||||
abstract fun encode(buf: ByteBuf)
|
||||
|
||||
fun decode(buf: ByteBuf)
|
||||
abstract fun decode(buf: ByteBuf)
|
||||
|
||||
public override fun clone(): Any {
|
||||
return super.clone()
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,19 @@
|
||||
package cc.maxmc.msm.common.network
|
||||
|
||||
import cc.maxmc.msm.common.network.netty.NetworkRegistry
|
||||
import io.netty.buffer.ByteBuf
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import io.netty.handler.codec.ByteToMessageCodec
|
||||
|
||||
object ClusterMsgCodec : ByteToMessageCodec<BungeePacket>() {
|
||||
class ClusterMsgCodec(private val current: NetworkRegistry.PacketDirection) : ByteToMessageCodec<BungeePacket>() {
|
||||
override fun encode(ctx: ChannelHandlerContext, msg: BungeePacket, out: ByteBuf) {
|
||||
out.writeInt(NetworkRegistry.getPacketID(current, msg))
|
||||
msg.encode(out)
|
||||
}
|
||||
|
||||
override fun decode(ctx: ChannelHandlerContext, `in`: ByteBuf, out: MutableList<Any>) {
|
||||
|
||||
val id = `in`.readInt()
|
||||
val packet = NetworkRegistry.getPacketByID(current, id)
|
||||
packet.decode(`in`)
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package cc.maxmc.msm.common.network
|
||||
|
||||
import com.google.common.collect.HashBiMap
|
||||
|
||||
object NetworkRegistry {
|
||||
val parentBoundMap = HashBiMap.create<Int, BungeePacket>()
|
||||
val childBoundMap = HashBiMap.create<Int, BungeePacket>()
|
||||
|
||||
fun registerPacket(packet: BungeePacket, direction: PacketDirection) {
|
||||
if (direction == PacketDirection.ParentBound) {
|
||||
parentBoundMap
|
||||
} else {
|
||||
childBoundMap
|
||||
}.let {
|
||||
it[it.size] = packet
|
||||
}
|
||||
}
|
||||
|
||||
enum class PacketDirection {
|
||||
ParentBound,
|
||||
ChildBound;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package cc.maxmc.msm.common.network.netty
|
||||
|
||||
import cc.maxmc.msm.common.network.BungeePacket
|
||||
import cc.maxmc.msm.common.network.packet.CPacketDebug
|
||||
import cc.maxmc.msm.common.network.packet.PPacketDebug
|
||||
import com.google.common.collect.HashBiMap
|
||||
|
||||
object NetworkRegistry {
|
||||
private val parentBoundMap = HashBiMap.create<Int, BungeePacket>()
|
||||
private val childBoundMap = HashBiMap.create<Int, BungeePacket>()
|
||||
|
||||
init {
|
||||
registerPacket(PacketDirection.PARENT_BOUND, PPacketDebug())
|
||||
registerPacket(PacketDirection.CHILD_BOUND, CPacketDebug())
|
||||
}
|
||||
|
||||
private fun registerPacket(direction: PacketDirection, packet: BungeePacket) {
|
||||
if (direction == PacketDirection.PARENT_BOUND) {
|
||||
parentBoundMap
|
||||
} else {
|
||||
childBoundMap
|
||||
}.let {
|
||||
it[it.size] = packet
|
||||
}
|
||||
}
|
||||
|
||||
fun getPacketID(side: PacketDirection, packet: BungeePacket): Int {
|
||||
val map = if (side == PacketDirection.PARENT_BOUND) parentBoundMap else childBoundMap
|
||||
return map.inverse()[packet] ?: throw IllegalStateException("Packet does not in registry.")
|
||||
}
|
||||
|
||||
fun getPacketByID(side: PacketDirection, id: Int): BungeePacket {
|
||||
val map = if (side == PacketDirection.PARENT_BOUND) parentBoundMap else childBoundMap
|
||||
return map[id]?.clone() as? BungeePacket ?: throw IllegalStateException("Packet does not in registry.")
|
||||
}
|
||||
|
||||
enum class PacketDirection {
|
||||
PARENT_BOUND,
|
||||
CHILD_BOUND;
|
||||
|
||||
operator fun not(): PacketDirection {
|
||||
return if (this == PARENT_BOUND) CHILD_BOUND else PARENT_BOUND
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cc.maxmc.msm.common.network.packet
|
||||
|
||||
import cc.maxmc.msm.common.network.BungeePacket
|
||||
import io.netty.buffer.ByteBuf
|
||||
|
||||
class CPacketDebug(
|
||||
var content: String
|
||||
) : BungeePacket() {
|
||||
|
||||
constructor() : this("")
|
||||
|
||||
override fun encode(buf: ByteBuf) {
|
||||
val array = content.encodeToByteArray()
|
||||
buf.writeInt(array.size)
|
||||
buf.writeBytes(array)
|
||||
}
|
||||
|
||||
override fun decode(buf: ByteBuf) {
|
||||
val size = buf.readInt()
|
||||
content = buf.readBytes(size).array().decodeToString()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package cc.maxmc.msm.common.network.packet
|
||||
|
||||
import cc.maxmc.msm.common.network.BungeePacket
|
||||
import io.netty.buffer.ByteBuf
|
||||
|
||||
class PPacketDebug(
|
||||
var content: String
|
||||
) : BungeePacket() {
|
||||
constructor() : this("")
|
||||
|
||||
override fun encode(buf: ByteBuf) {
|
||||
val array = content.encodeToByteArray()
|
||||
buf.writeInt(array.size)
|
||||
buf.writeBytes(array)
|
||||
}
|
||||
|
||||
override fun decode(buf: ByteBuf) {
|
||||
val size = buf.readInt()
|
||||
content = buf.readBytes(size).array().decodeToString()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user