Skip to content

Commit

Permalink
Merge pull request #5 from way-zer/fix-load-time
Browse files Browse the repository at this point in the history
修复指令和资源包加载时机问题
  • Loading branch information
way-zer authored Feb 16, 2022
2 parents 39a1041 + b6b9d6f commit d8aff49
Showing 1 changed file with 49 additions and 17 deletions.
66 changes: 49 additions & 17 deletions loaderMod/src/main/kotlin/cf/wayzer/contentsMod/ContentsLoader.kt
Original file line number Diff line number Diff line change
@@ -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<Class<*>, Cons<Packet>>
}
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<Prov<out Packet>>
}
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() }
Expand All @@ -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")
Expand Down

0 comments on commit d8aff49

Please sign in to comment.