From 49b85cf2916f99d7d799fc9ce9e960f4fb23e197 Mon Sep 17 00:00:00 2001 From: TONY_All Date: Wed, 15 Mar 2023 11:45:35 +0800 Subject: [PATCH] sync --- child-side/build.gradle.kts | 12 +++++ .../cc/maxmc/msm/parent/MultiServerMan.kt | 18 ++++++++ child-side/src/main/resources/bungee.yml | 3 ++ common/build.gradle.kts | 1 + .../cc/maxmc/msm/common/PacketInjector.kt | 25 ----------- .../kotlin/cc/maxmc/msm/common/Placeholder.kt | 3 ++ .../maxmc/msm/common/network/BungeePacket.kt | 10 +++-- .../msm/common/network/ClusterMsgCodec.kt | 8 +++- .../msm/common/network/NetworkRegistry.kt | 23 ---------- .../common/network/netty/NetworkRegistry.kt | 45 +++++++++++++++++++ .../msm/common/network/packet/CPacketDebug.kt | 22 +++++++++ .../msm/common/network/packet/PPacketDebug.kt | 21 +++++++++ .../cc/maxmc/msm/parent/MultiServerMan.kt | 14 +++--- .../maxmc/msm/parent/manager/ChildManager.kt | 11 +++++ .../cc/maxmc/msm/parent/misc/ChildBungee.kt | 3 ++ .../cc/maxmc/msm/parent/network/NetManager.kt | 9 ++++ parent-side/src/main/kotlin/command/Send.kt | 12 +++++ 17 files changed, 179 insertions(+), 61 deletions(-) create mode 100644 child-side/src/main/kotlin/cc/maxmc/msm/parent/MultiServerMan.kt create mode 100644 child-side/src/main/resources/bungee.yml delete mode 100644 common/src/main/kotlin/cc/maxmc/msm/common/PacketInjector.kt create mode 100644 common/src/main/kotlin/cc/maxmc/msm/common/Placeholder.kt delete mode 100644 common/src/main/kotlin/cc/maxmc/msm/common/network/NetworkRegistry.kt create mode 100644 common/src/main/kotlin/cc/maxmc/msm/common/network/netty/NetworkRegistry.kt create mode 100644 common/src/main/kotlin/cc/maxmc/msm/common/network/packet/CPacketDebug.kt create mode 100644 common/src/main/kotlin/cc/maxmc/msm/common/network/packet/PPacketDebug.kt create mode 100644 parent-side/src/main/kotlin/cc/maxmc/msm/parent/manager/ChildManager.kt create mode 100644 parent-side/src/main/kotlin/cc/maxmc/msm/parent/misc/ChildBungee.kt create mode 100644 parent-side/src/main/kotlin/cc/maxmc/msm/parent/network/NetManager.kt create mode 100644 parent-side/src/main/kotlin/command/Send.kt diff --git a/child-side/build.gradle.kts b/child-side/build.gradle.kts index 0d17a48..2fe8a39 100644 --- a/child-side/build.gradle.kts +++ b/child-side/build.gradle.kts @@ -1,5 +1,6 @@ plugins { kotlin("jvm") + id("com.github.johnrengelman.shadow") } group = "cc.maxmc.msm.child" @@ -11,5 +12,16 @@ repositories { dependencies { implementation(kotlin("stdlib")) + implementation(project(":api")) + implementation(project(":common")) + @Suppress("VulnerableLibrariesLocal") compileOnly("io.github.waterfallmc:waterfall-api:1.19-R0.1-SNAPSHOT") +} + +tasks.shadowJar { + relocate("kotlin", "cc.maxmc.msm.lib.kotlin") +} + +tasks.build { + dependsOn(tasks.shadowJar) } \ No newline at end of file diff --git a/child-side/src/main/kotlin/cc/maxmc/msm/parent/MultiServerMan.kt b/child-side/src/main/kotlin/cc/maxmc/msm/parent/MultiServerMan.kt new file mode 100644 index 0000000..c2ffe87 --- /dev/null +++ b/child-side/src/main/kotlin/cc/maxmc/msm/parent/MultiServerMan.kt @@ -0,0 +1,18 @@ +package cc.maxmc.msm.child + +import cc.maxmc.msm.common.Placeholder +import cc.maxmc.msm.common.network.netty.NetworkRegistry +import net.md_5.bungee.api.plugin.Plugin + +class MultiServerMan : Plugin() { + + override fun onEnable() { + Placeholder.inject(NetworkRegistry.PacketDirection.CHILD_BOUND) + + } + + override fun onDisable() { + + } + +} \ No newline at end of file diff --git a/child-side/src/main/resources/bungee.yml b/child-side/src/main/resources/bungee.yml new file mode 100644 index 0000000..be19e8a --- /dev/null +++ b/child-side/src/main/resources/bungee.yml @@ -0,0 +1,3 @@ +name: MultiServerMan-Parent +main: cc.maxmc.msm.child.MultiServerMan +author: TONY_All \ No newline at end of file diff --git a/common/build.gradle.kts b/common/build.gradle.kts index c754beb..d545f78 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -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 diff --git a/common/src/main/kotlin/cc/maxmc/msm/common/PacketInjector.kt b/common/src/main/kotlin/cc/maxmc/msm/common/PacketInjector.kt deleted file mode 100644 index 248c5da..0000000 --- a/common/src/main/kotlin/cc/maxmc/msm/common/PacketInjector.kt +++ /dev/null @@ -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 - while (true) { - return channelList.firstOrNull() ?: continue - } - } -} \ No newline at end of file diff --git a/common/src/main/kotlin/cc/maxmc/msm/common/Placeholder.kt b/common/src/main/kotlin/cc/maxmc/msm/common/Placeholder.kt new file mode 100644 index 0000000..2c19f26 --- /dev/null +++ b/common/src/main/kotlin/cc/maxmc/msm/common/Placeholder.kt @@ -0,0 +1,3 @@ +package cc.maxmc.msm.common + +object Placeholder \ No newline at end of file diff --git a/common/src/main/kotlin/cc/maxmc/msm/common/network/BungeePacket.kt b/common/src/main/kotlin/cc/maxmc/msm/common/network/BungeePacket.kt index 7c8ad02..bdc4d75 100644 --- a/common/src/main/kotlin/cc/maxmc/msm/common/network/BungeePacket.kt +++ b/common/src/main/kotlin/cc/maxmc/msm/common/network/BungeePacket.kt @@ -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() + } } \ No newline at end of file diff --git a/common/src/main/kotlin/cc/maxmc/msm/common/network/ClusterMsgCodec.kt b/common/src/main/kotlin/cc/maxmc/msm/common/network/ClusterMsgCodec.kt index 67cacac..9fd4cd3 100644 --- a/common/src/main/kotlin/cc/maxmc/msm/common/network/ClusterMsgCodec.kt +++ b/common/src/main/kotlin/cc/maxmc/msm/common/network/ClusterMsgCodec.kt @@ -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() { +class ClusterMsgCodec(private val current: NetworkRegistry.PacketDirection) : ByteToMessageCodec() { 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) { - + val id = `in`.readInt() + val packet = NetworkRegistry.getPacketByID(current, id) + packet.decode(`in`) } } \ No newline at end of file diff --git a/common/src/main/kotlin/cc/maxmc/msm/common/network/NetworkRegistry.kt b/common/src/main/kotlin/cc/maxmc/msm/common/network/NetworkRegistry.kt deleted file mode 100644 index 4fb23e8..0000000 --- a/common/src/main/kotlin/cc/maxmc/msm/common/network/NetworkRegistry.kt +++ /dev/null @@ -1,23 +0,0 @@ -package cc.maxmc.msm.common.network - -import com.google.common.collect.HashBiMap - -object NetworkRegistry { - val parentBoundMap = HashBiMap.create() - val childBoundMap = HashBiMap.create() - - fun registerPacket(packet: BungeePacket, direction: PacketDirection) { - if (direction == PacketDirection.ParentBound) { - parentBoundMap - } else { - childBoundMap - }.let { - it[it.size] = packet - } - } - - enum class PacketDirection { - ParentBound, - ChildBound; - } -} \ No newline at end of file diff --git a/common/src/main/kotlin/cc/maxmc/msm/common/network/netty/NetworkRegistry.kt b/common/src/main/kotlin/cc/maxmc/msm/common/network/netty/NetworkRegistry.kt new file mode 100644 index 0000000..5114379 --- /dev/null +++ b/common/src/main/kotlin/cc/maxmc/msm/common/network/netty/NetworkRegistry.kt @@ -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() + private val childBoundMap = HashBiMap.create() + + 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 + } + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/cc/maxmc/msm/common/network/packet/CPacketDebug.kt b/common/src/main/kotlin/cc/maxmc/msm/common/network/packet/CPacketDebug.kt new file mode 100644 index 0000000..0cf4c10 --- /dev/null +++ b/common/src/main/kotlin/cc/maxmc/msm/common/network/packet/CPacketDebug.kt @@ -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() + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/cc/maxmc/msm/common/network/packet/PPacketDebug.kt b/common/src/main/kotlin/cc/maxmc/msm/common/network/packet/PPacketDebug.kt new file mode 100644 index 0000000..37391d1 --- /dev/null +++ b/common/src/main/kotlin/cc/maxmc/msm/common/network/packet/PPacketDebug.kt @@ -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() + } +} \ No newline at end of file diff --git a/parent-side/src/main/kotlin/cc/maxmc/msm/parent/MultiServerMan.kt b/parent-side/src/main/kotlin/cc/maxmc/msm/parent/MultiServerMan.kt index 3334200..9aa2fcd 100644 --- a/parent-side/src/main/kotlin/cc/maxmc/msm/parent/MultiServerMan.kt +++ b/parent-side/src/main/kotlin/cc/maxmc/msm/parent/MultiServerMan.kt @@ -1,17 +1,15 @@ package cc.maxmc.msm.parent -import cc.maxmc.msm.common.PacketInjector +import cc.maxmc.msm.common.Placeholder +import cc.maxmc.msm.common.network.netty.NetworkRegistry +import command.Send import net.md_5.bungee.api.ProxyServer import net.md_5.bungee.api.plugin.Plugin -class MultiServerMan: Plugin() { - +class MultiServerMan : Plugin() { override fun onEnable() { - Thread.sleep(5000) - ProxyServer.getInstance().scheduler.runAsync(this) { - println("injecting") - PacketInjector.inject() - } + ProxyServer.getInstance().pluginManager.registerCommand(this, Send) + Placeholder.inject(NetworkRegistry.PacketDirection.PARENT_BOUND) } override fun onDisable() { diff --git a/parent-side/src/main/kotlin/cc/maxmc/msm/parent/manager/ChildManager.kt b/parent-side/src/main/kotlin/cc/maxmc/msm/parent/manager/ChildManager.kt new file mode 100644 index 0000000..f00a889 --- /dev/null +++ b/parent-side/src/main/kotlin/cc/maxmc/msm/parent/manager/ChildManager.kt @@ -0,0 +1,11 @@ +package cc.maxmc.msm.parent.manager + +import cc.maxmc.msm.parent.misc.ChildBungee + +object ChildManager { + val children = ArrayList() + + fun registerChild(child: ChildBungee) { + + } +} \ No newline at end of file diff --git a/parent-side/src/main/kotlin/cc/maxmc/msm/parent/misc/ChildBungee.kt b/parent-side/src/main/kotlin/cc/maxmc/msm/parent/misc/ChildBungee.kt new file mode 100644 index 0000000..42d4d57 --- /dev/null +++ b/parent-side/src/main/kotlin/cc/maxmc/msm/parent/misc/ChildBungee.kt @@ -0,0 +1,3 @@ +package cc.maxmc.msm.parent.misc + +data class ChildBungee(val addr: String) diff --git a/parent-side/src/main/kotlin/cc/maxmc/msm/parent/network/NetManager.kt b/parent-side/src/main/kotlin/cc/maxmc/msm/parent/network/NetManager.kt new file mode 100644 index 0000000..f9e66df --- /dev/null +++ b/parent-side/src/main/kotlin/cc/maxmc/msm/parent/network/NetManager.kt @@ -0,0 +1,9 @@ +package cc.maxmc.msm.parent.network + +import cc.maxmc.msm.common.network.netty.NetworkRegistry + +object NetManager { + private val current = NetworkRegistry.PacketDirection.PARENT_BOUND + + +} \ No newline at end of file diff --git a/parent-side/src/main/kotlin/command/Send.kt b/parent-side/src/main/kotlin/command/Send.kt new file mode 100644 index 0000000..8f003c1 --- /dev/null +++ b/parent-side/src/main/kotlin/command/Send.kt @@ -0,0 +1,12 @@ +package command + +import net.md_5.bungee.api.CommandSender +import net.md_5.bungee.api.plugin.Command + +object Send: Command("debugP") { + override fun execute(sender: CommandSender, args: Array) { + val content = args.joinToString(" ") + + sender.sendMessage("Success.") + } +} \ No newline at end of file