repackage

This commit is contained in:
2023-05-04 21:46:09 +08:00
parent de1ad1d31b
commit 4db42f957c
61 changed files with 276 additions and 287 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ plugins {
kotlin("jvm")
}
group = "cc.maxmc.msm.common"
group = "com.mohist.mistyrain.common"
repositories {
mavenCentral()
@@ -1,3 +0,0 @@
package cc.maxmc.msm.common
object Placeholder
@@ -1,6 +0,0 @@
package cc.maxmc.msm.common.event
import io.netty.channel.Channel
import net.md_5.bungee.api.plugin.Event
class ChannelActiveEvent(val channel: Channel): Event()
@@ -1,6 +0,0 @@
package cc.maxmc.msm.common.event
import io.netty.channel.Channel
import net.md_5.bungee.api.plugin.Event
class ChannelInactiveEvent(val channel: Channel): Event()
@@ -1,19 +0,0 @@
package cc.maxmc.msm.common.network.packet
import cc.maxmc.msm.api.misc.ServerInfo
import cc.maxmc.msm.common.network.BungeePacket
import cc.maxmc.msm.common.utils.readServerInfo
import cc.maxmc.msm.common.utils.writeServerInfo
import io.netty.buffer.ByteBuf
class CPacketEndServer(
var server: ServerInfo
) : BungeePacket() {
override fun encode(buf: ByteBuf) {
buf.writeServerInfo(server)
}
override fun decode(buf: ByteBuf) {
server = buf.readServerInfo()
}
}
@@ -1,11 +0,0 @@
package cc.maxmc.msm.common.network.packet
import cc.maxmc.msm.common.network.BungeePacket
import io.netty.buffer.ByteBuf
class CPacketGetInfo : BungeePacket() {
override fun encode(buf: ByteBuf) {}
override fun decode(buf: ByteBuf) {}
}
@@ -1,24 +0,0 @@
package cc.maxmc.msm.common.utils
import cc.maxmc.msm.common.network.BungeePacket
import io.netty.channel.Channel
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlin.coroutines.Continuation
import kotlin.coroutines.suspendCoroutine
val pluginScope = CoroutineScope(SupervisorJob() + CoroutineExceptionHandler { _, except ->
log("§c执行异步操作时出现异常 ${except.message}")
except.printStackTrace()
})
val awaiting =
ArrayList<Triple<Class<out BungeePacket>, (Channel, BungeePacket) -> Boolean, Continuation<BungeePacket>>>()
suspend inline fun <T : BungeePacket> awaitPacket(packetClass: Class<T>, noinline filter: (Channel, T) -> Boolean) =
suspendCoroutine<T> {
val triple = Triple(packetClass, filter, it)
@Suppress("UNCHECKED_CAST")
awaiting.add(triple as Triple<Class<out BungeePacket>, (Channel, BungeePacket) -> Boolean, Continuation<BungeePacket>>)
}
@@ -0,0 +1,3 @@
package com.mohist.mistyrain.common
object Placeholder
@@ -0,0 +1,6 @@
package com.mohist.mistyrain.common.event
import io.netty.channel.Channel
import net.md_5.bungee.api.plugin.Event
class ChannelActiveEvent(val channel: Channel) : Event()
@@ -0,0 +1,6 @@
package com.mohist.mistyrain.common.event
import io.netty.channel.Channel
import net.md_5.bungee.api.plugin.Event
class ChannelInactiveEvent(val channel: Channel) : Event()
@@ -1,7 +1,7 @@
package cc.maxmc.msm.common.event
package com.mohist.mistyrain.common.event
import cc.maxmc.msm.common.network.BungeePacket
import io.netty.channel.Channel
import net.md_5.bungee.api.plugin.Event
data class PacketReceiveEvent(val channel: Channel, val packet: BungeePacket) : Event()
data class PacketReceiveEvent(val channel: Channel, val packet: com.mohist.mistyrain.common.network.BungeePacket) :
Event()
@@ -1,8 +1,8 @@
package cc.maxmc.msm.common.network
package com.mohist.mistyrain.common.network
import io.netty.buffer.ByteBuf
abstract class BungeePacket: Cloneable {
abstract class BungeePacket : Cloneable {
abstract fun encode(buf: ByteBuf)
abstract fun decode(buf: ByteBuf)
@@ -1,10 +1,9 @@
package cc.maxmc.msm.common.network
package com.mohist.mistyrain.common.network
import cc.maxmc.msm.common.event.ChannelActiveEvent
import cc.maxmc.msm.common.event.ChannelInactiveEvent
import cc.maxmc.msm.common.event.PacketReceiveEvent
import cc.maxmc.msm.common.utils.awaiting
import cc.maxmc.msm.common.utils.log
import com.mohist.mistyrain.common.event.ChannelActiveEvent
import com.mohist.mistyrain.common.event.ChannelInactiveEvent
import com.mohist.mistyrain.common.event.PacketReceiveEvent
import com.mohist.mistyrain.common.utils.awaiting
import io.netty.channel.ChannelHandler.Sharable
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.SimpleChannelInboundHandler
@@ -14,8 +13,8 @@ import kotlin.coroutines.resume
object ClusterPacketHandler : SimpleChannelInboundHandler<BungeePacket>() {
override fun channelRead0(ctx: ChannelHandlerContext, msg: BungeePacket) {
awaiting.filter { it.first == msg::class.java }.filter { it.second(ctx.channel(), msg) }.forEach {
it.third.resume(msg)
}
it.third.resume(msg)
}
PacketReceiveEvent(ctx.channel(), msg).callEvent()
}
@@ -1,13 +1,16 @@
package cc.maxmc.msm.common.network.netty
package com.mohist.mistyrain.common.network.netty
import cc.maxmc.msm.common.network.BungeePacket
import cc.maxmc.msm.common.utils.debug
import io.netty.buffer.ByteBuf
import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.ByteToMessageCodec
class ClusterMsgCodec(private val current: NetworkRegistry.PacketDirection) : ByteToMessageCodec<BungeePacket>() {
override fun encode(ctx: ChannelHandlerContext, msg: BungeePacket, out: ByteBuf) {
class ClusterMsgCodec(private val current: NetworkRegistry.PacketDirection) :
ByteToMessageCodec<com.mohist.mistyrain.common.network.BungeePacket>() {
override fun encode(
ctx: ChannelHandlerContext,
msg: com.mohist.mistyrain.common.network.BungeePacket,
out: ByteBuf
) {
val id = NetworkRegistry.getPacketID(current, msg)
out.writeInt(id)
msg.encode(out)
@@ -1,12 +1,11 @@
package cc.maxmc.msm.common.network.netty
package com.mohist.mistyrain.common.network.netty
import cc.maxmc.msm.common.network.BungeePacket
import cc.maxmc.msm.common.network.packet.*
import com.google.common.collect.HashBiMap
import com.mohist.mistyrain.common.network.packet.*
object NetworkRegistry {
private val parentBoundMap = HashBiMap.create<Int, Class<out BungeePacket>>()
private val childBoundMap = HashBiMap.create<Int, Class<out BungeePacket>>()
private val parentBoundMap = HashBiMap.create<Int, Class<out com.mohist.mistyrain.common.network.BungeePacket>>()
private val childBoundMap = HashBiMap.create<Int, Class<out com.mohist.mistyrain.common.network.BungeePacket>>()
init {
registerPacket(PacketDirection.PARENT_BOUND, PPacketDebug::class.java)
@@ -27,7 +26,10 @@ object NetworkRegistry {
}
private fun registerPacket(direction: PacketDirection, packet: Class<out BungeePacket>) {
private fun registerPacket(
direction: PacketDirection,
packet: Class<out com.mohist.mistyrain.common.network.BungeePacket>
) {
if (direction == PacketDirection.PARENT_BOUND) {
parentBoundMap
} else {
@@ -37,12 +39,12 @@ object NetworkRegistry {
}
}
fun getPacketID(side: PacketDirection, packet: BungeePacket): Int {
fun getPacketID(side: PacketDirection, packet: com.mohist.mistyrain.common.network.BungeePacket): Int {
val map = if (side == PacketDirection.PARENT_BOUND) childBoundMap else parentBoundMap
return map.inverse()[packet::class.java] ?: throw IllegalStateException("Packet does not in registry.")
}
fun getPacketByID(side: PacketDirection, id: Int): BungeePacket {
fun getPacketByID(side: PacketDirection, id: Int): com.mohist.mistyrain.common.network.BungeePacket {
val map = if (side == PacketDirection.PARENT_BOUND) parentBoundMap else childBoundMap
return map[id]?.newInstance() ?: throw IllegalStateException("Packet does not in registry.")
}
@@ -1,16 +1,15 @@
package cc.maxmc.msm.common.network.packet
package com.mohist.mistyrain.common.network.packet
import cc.maxmc.msm.api.misc.MatchInfo
import cc.maxmc.msm.common.network.BungeePacket
import cc.maxmc.msm.common.utils.readMatchInfo
import cc.maxmc.msm.common.utils.writeMatchInfo
import com.mohist.mistyrain.api.misc.MatchInfo
import com.mohist.mistyrain.common.utils.readMatchInfo
import com.mohist.mistyrain.common.utils.writeMatchInfo
import io.netty.buffer.ByteBuf
import net.md_5.bungee.protocol.DefinedPacket
import java.util.*
sealed class CPacketAPICallback(
var uid: UUID
) : BungeePacket() {
) : com.mohist.mistyrain.common.network.BungeePacket() {
override fun encode(buf: ByteBuf) {
DefinedPacket.writeUUID(uid, buf)
}
@@ -1,11 +1,10 @@
package cc.maxmc.msm.common.network.packet
package com.mohist.mistyrain.common.network.packet
import cc.maxmc.msm.common.network.BungeePacket
import io.netty.buffer.ByteBuf
data class CPacketDebug(
var content: String = ""
) : BungeePacket() {
) : com.mohist.mistyrain.common.network.BungeePacket() {
override fun encode(buf: ByteBuf) {
val array = content.encodeToByteArray()
@@ -0,0 +1,18 @@
package com.mohist.mistyrain.common.network.packet
import com.mohist.mistyrain.api.misc.ServerInfo
import com.mohist.mistyrain.common.utils.readServerInfo
import com.mohist.mistyrain.common.utils.writeServerInfo
import io.netty.buffer.ByteBuf
class CPacketEndServer(
var server: ServerInfo
) : com.mohist.mistyrain.common.network.BungeePacket() {
override fun encode(buf: ByteBuf) {
buf.writeServerInfo(server)
}
override fun decode(buf: ByteBuf) {
server = buf.readServerInfo()
}
}
@@ -0,0 +1,10 @@
package com.mohist.mistyrain.common.network.packet
import io.netty.buffer.ByteBuf
class CPacketGetInfo : com.mohist.mistyrain.common.network.BungeePacket() {
override fun encode(buf: ByteBuf) {}
override fun decode(buf: ByteBuf) {}
}
@@ -1,16 +1,15 @@
package cc.maxmc.msm.common.network.packet
package com.mohist.mistyrain.common.network.packet
import cc.maxmc.msm.api.misc.ServerInfo
import cc.maxmc.msm.common.network.BungeePacket
import cc.maxmc.msm.common.utils.readServerInfo
import cc.maxmc.msm.common.utils.writeServerInfo
import com.mohist.mistyrain.api.misc.ServerInfo
import com.mohist.mistyrain.common.utils.readServerInfo
import com.mohist.mistyrain.common.utils.writeServerInfo
import io.netty.buffer.ByteBuf
import net.md_5.bungee.protocol.DefinedPacket
class CPacketRequestServer(
var type: String = "",
var serverInfo: ServerInfo = ServerInfo(),
) : BungeePacket() {
) : com.mohist.mistyrain.common.network.BungeePacket() {
override fun encode(buf: ByteBuf) {
DefinedPacket.writeString(type, buf)
buf.writeServerInfo(serverInfo)
@@ -1,7 +1,6 @@
package cc.maxmc.msm.common.network.packet
package com.mohist.mistyrain.common.network.packet
import cc.maxmc.msm.api.misc.EnumAPI
import cc.maxmc.msm.common.network.BungeePacket
import com.mohist.mistyrain.api.misc.EnumAPI
import io.netty.buffer.ByteBuf
import net.md_5.bungee.protocol.DefinedPacket
import java.util.*
@@ -9,7 +8,7 @@ import java.util.*
sealed class PPacketAPICall(
var api: EnumAPI = EnumAPI.GET_SERVER,
var uid: UUID = UUID.randomUUID()
) : BungeePacket() {
) : com.mohist.mistyrain.common.network.BungeePacket() {
override fun encode(buf: ByteBuf) {
DefinedPacket.writeVarInt(api.ordinal, buf)
DefinedPacket.writeUUID(uid, buf)
@@ -1,11 +1,10 @@
package cc.maxmc.msm.common.network.packet
package com.mohist.mistyrain.common.network.packet
import cc.maxmc.msm.common.network.BungeePacket
import io.netty.buffer.ByteBuf
import net.md_5.bungee.protocol.DefinedPacket
class PPacketChildInfo(var portRange: MutableSet<Int> = HashSet(), var types: MutableSet<String> = HashSet()) :
BungeePacket() {
com.mohist.mistyrain.common.network.BungeePacket() {
override fun encode(buf: ByteBuf) {
DefinedPacket.writeVarInt(portRange.size, buf)
portRange.forEach {
@@ -1,11 +1,10 @@
package cc.maxmc.msm.common.network.packet
package com.mohist.mistyrain.common.network.packet
import cc.maxmc.msm.common.network.BungeePacket
import io.netty.buffer.ByteBuf
data class PPacketDebug(
var content: String = ""
) : BungeePacket() {
) : com.mohist.mistyrain.common.network.BungeePacket() {
override fun encode(buf: ByteBuf) {
val array = content.encodeToByteArray()
@@ -1,14 +1,13 @@
package cc.maxmc.msm.common.network.packet
package com.mohist.mistyrain.common.network.packet
import cc.maxmc.msm.api.misc.ServerInfo
import cc.maxmc.msm.common.network.BungeePacket
import cc.maxmc.msm.common.utils.readServerInfo
import cc.maxmc.msm.common.utils.writeServerInfo
import com.mohist.mistyrain.api.misc.ServerInfo
import com.mohist.mistyrain.common.utils.readServerInfo
import com.mohist.mistyrain.common.utils.writeServerInfo
import io.netty.buffer.ByteBuf
class PPacketServerStarted(
var serverInfo: ServerInfo = ServerInfo()
) : BungeePacket() {
) : com.mohist.mistyrain.common.network.BungeePacket() {
override fun encode(buf: ByteBuf) {
buf.writeServerInfo(serverInfo)
}
@@ -0,0 +1,26 @@
package com.mohist.mistyrain.common.utils
import io.netty.channel.Channel
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlin.coroutines.Continuation
import kotlin.coroutines.suspendCoroutine
val pluginScope = CoroutineScope(SupervisorJob() + CoroutineExceptionHandler { _, except ->
log("§c执行异步操作时出现异常 ${except.message}")
except.printStackTrace()
})
val awaiting =
ArrayList<Triple<Class<out com.mohist.mistyrain.common.network.BungeePacket>, (Channel, com.mohist.mistyrain.common.network.BungeePacket) -> Boolean, Continuation<com.mohist.mistyrain.common.network.BungeePacket>>>()
suspend inline fun <T : com.mohist.mistyrain.common.network.BungeePacket> awaitPacket(
packetClass: Class<T>,
noinline filter: (Channel, T) -> Boolean
) =
suspendCoroutine<T> {
val triple = Triple(packetClass, filter, it)
@Suppress("UNCHECKED_CAST")
awaiting.add(triple as Triple<Class<out com.mohist.mistyrain.common.network.BungeePacket>, (Channel, com.mohist.mistyrain.common.network.BungeePacket) -> Boolean, Continuation<com.mohist.mistyrain.common.network.BungeePacket>>)
}
@@ -1,4 +1,4 @@
package cc.maxmc.msm.common.utils
package com.mohist.mistyrain.common.utils
import net.md_5.bungee.api.ProxyServer
import net.md_5.bungee.api.chat.TextComponent
@@ -1,10 +1,9 @@
package cc.maxmc.msm.common.utils
package com.mohist.mistyrain.common.utils
import cc.maxmc.msm.api.misc.MatchInfo
import cc.maxmc.msm.api.misc.ServerInfo
import cc.maxmc.msm.common.network.ClusterPacketHandler
import cc.maxmc.msm.common.network.netty.ClusterMsgCodec
import cc.maxmc.msm.common.network.netty.NetworkRegistry
import com.mohist.mistyrain.api.misc.MatchInfo
import com.mohist.mistyrain.api.misc.ServerInfo
import com.mohist.mistyrain.common.network.netty.ClusterMsgCodec
import com.mohist.mistyrain.common.network.netty.NetworkRegistry
import io.netty.buffer.ByteBuf
import io.netty.channel.Channel
import io.netty.channel.ChannelInitializer
@@ -23,7 +22,7 @@ fun pipelineInit(direction: NetworkRegistry.PacketDirection) = channelInit<Socke
pipeline().addLast("frame_decoder", Varint21FrameDecoder())
pipeline().addLast("frame_prepender", Varint21LengthFieldPrepender())
pipeline().addLast("codec", ClusterMsgCodec(direction))
pipeline().addLast("packet_handler", ClusterPacketHandler)
pipeline().addLast("packet_handler", com.mohist.mistyrain.common.network.ClusterPacketHandler)
}
fun ByteBuf.writeServerInfo(info: ServerInfo) {