From b6b9d6f6ca9aee7ff5a81474f256328289295d0c Mon Sep 17 00:00:00 2001 From: way-zer Date: Wed, 16 Feb 2022 21:02:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8C=87=E4=BB=A4=E5=92=8C?= =?UTF-8?q?=E8=B5=84=E6=BA=90=E5=8C=85=E5=8A=A0=E8=BD=BD=E6=97=B6=E6=9C=BA?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cf/wayzer/contentsMod/ContentsLoader.kt | 66 ++++++++++++++----- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/loaderMod/src/main/kotlin/cf/wayzer/contentsMod/ContentsLoader.kt b/loaderMod/src/main/kotlin/cf/wayzer/contentsMod/ContentsLoader.kt index 3fb53ec..abaa463 100644 --- a/loaderMod/src/main/kotlin/cf/wayzer/contentsMod/ContentsLoader.kt +++ b/loaderMod/src/main/kotlin/cf/wayzer/contentsMod/ContentsLoader.kt @@ -1,24 +1,69 @@ package cf.wayzer.contentsMod -import arc.Events +import arc.func.Cons +import arc.func.Prov +import arc.struct.ObjectMap +import arc.struct.Seq import arc.util.Log import mindustry.Vars import mindustry.ctype.Content import mindustry.ctype.ContentList import mindustry.ctype.ContentType -import mindustry.game.EventType.ResetEvent import mindustry.gen.Call +import mindustry.gen.ClientPacketReliableCallPacket import mindustry.mod.Mod +import mindustry.net.Net +import mindustry.net.Packet +import mindustry.net.Packets import kotlin.system.measureTimeMillis +class MYClientPacketReliableCallPacket : ClientPacketReliableCallPacket() { + override fun getPriority(): Int { + if (type == "ContentsLoader|load") return Packet.priorityHigh + return super.getPriority() + } +} + @Suppress("unused") class ContentsLoader : Mod() { override fun init() { - Events.on(ResetEvent::class.java) { + Vars.content = MyContentLoader + + val clientListeners = Net::class.java.getDeclaredField("clientListeners").run { + isAccessible = true + @Suppress("UNCHECKED_CAST") + get(Vars.net) as ObjectMap, Cons> + } + val bak = clientListeners[Packets.WorldStream::class.java]!! + Vars.net.handleClient(Packets.WorldStream::class.java) { + beforeWorldLoad() + bak.get(it) + } + + val packetProvs = Net::class.java.getDeclaredField("packetProvs").run { + isAccessible = true + @Suppress("UNCHECKED_CAST") + get(null) as Seq> + } + packetProvs[Net.getPacketId(ClientPacketReliableCallPacket()).toInt()] = ::MYClientPacketReliableCallPacket + Vars.netClient.addPacketHandler("ContentsLoader|load") { + Log.infoTag("ContentsLoader", "ToLoad $it") + Call.serverPacketReliable("ContentsLoader|load", loadType(it)) + } + Log.infoTag("ContentsLoader", "Finish Load Mod") + } + + @Suppress("unused", "MemberVisibilityCanBePrivate") + companion object API { + infix fun ContentList.eq(contentList: ContentList?): Boolean { + return javaClass == contentList?.javaClass + } + + fun beforeWorldLoad() { //fastPath if (MyContentLoader.contents.all { it.content eq it.lastContent }) { MyContentLoader.contents.forEach { it.content = it.default } - return@on + return } MyContentLoader.contents.forEach { val time = measureTimeMillis { it.load() } @@ -35,19 +80,6 @@ class ContentsLoader : Mod() { Log.infoTag("ContentsLoader", "Content.load costs ${timeLoad}ms") } } - Vars.content = MyContentLoader - Vars.netClient.addPacketHandler("ContentsLoader|load") { - Log.infoTag("ContentsLoader", "ToLoad $it") - Call.serverPacketReliable("ContentsLoader|load", loadType(it)) - } - Log.infoTag("ContentsLoader", "Finish Load Mod") - } - - @Suppress("unused", "MemberVisibilityCanBePrivate") - companion object API { - infix fun ContentList.eq(contentList: ContentList?): Boolean { - return javaClass == contentList?.javaClass - } fun maskChanged(type: ContentType) { val c = MyContentLoader.contentMap[type] ?: throw IllegalArgumentException("Not Support Overwrite ContentType")