Skip to content

Commit

Permalink
[6.1.0][dev] 优化 nms-proxy 逻辑, 增加 nms-util 预热
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Feb 25, 2024
1 parent ea8456d commit 7078148
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion common/src/main/java/taboolib/common/PrimitiveLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ static void loadFile(File file, boolean isIsolated, boolean isExternal, String[]
jar = new File(getCacheFile(), hash + ".jar");
// 文件为空 || 开发模式 || 强制重定向
if ((!jar.exists() && jar.length() == 0) || (IS_FORCE_DOWNLOAD_IN_DEV_MODE && IS_DEV_MODE) || forceRelocate) {
PrimitiveIO.println("Relocating ...");
// PrimitiveIO.println("Relocating ...");
jar.getParentFile().mkdirs();
new JarRelocator(PrimitiveIO.copyFile(file, File.createTempFile(file.getName(), ".jar")), jar, rel).run();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package taboolib.module.nms

import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import java.util.concurrent.CompletableFuture

@Awake(LifeCycle.ENABLE)
fun nmsPreheat(): CompletableFuture<Void> {
return CompletableFuture.runAsync {
nmsProxy<NMSEntity>()
nmsProxy<NMSItem>()
nmsProxy<NMSItemTag>()
nmsProxy<NMSLight>()
nmsProxy<NMSMessage>()
nmsProxy<NMSParticle>()
nmsProxy<NMSScoreboard>()
nmsProxy<NMSSign>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package taboolib.module.nms
import org.objectweb.asm.commons.Remapper
import org.objectweb.asm.signature.SignatureReader
import org.objectweb.asm.signature.SignatureWriter
import taboolib.common.util.unsafeLazy
import java.util.concurrent.ConcurrentHashMap

/**
* TabooLib
Expand All @@ -22,9 +22,10 @@ open class MinecraftRemapper : Remapper() {
val obc1 = "org/bukkit/craftbukkit/v1_.*?/".toRegex()
val obc2 = "org/bukkit/craftbukkit/${MinecraftVersion.minecraftVersion}/"

val mapping by unsafeLazy {
MinecraftVersion.mapping
}
/**
* 缓存类的父类和接口
*/
val parentsCacheMap = ConcurrentHashMap<String, List<String>>()

/**
* 在 1.17 版本下进行字段转换
Expand All @@ -34,8 +35,10 @@ open class MinecraftRemapper : Remapper() {
*/
override fun mapFieldName(owner: String, name: String, descriptor: String): String {
if (MinecraftVersion.isUniversal) {
val universal = translate(owner).replace('/', '.')
return mapping.fields.firstOrNull { it.path == universal && it.translateName == name }?.mojangName ?: name
// 当前运行时的 Owner 名称
val runningOwner = translate(owner).replace('/', '.')
val findPath = parentsCacheMap.getOrPut(runningOwner) { findParents(runningOwner).reversed() }
return MinecraftVersion.mapping.fields.find { it.path in findPath && it.translateName == name }?.mojangName ?: name
}
return name
}
Expand All @@ -50,8 +53,10 @@ open class MinecraftRemapper : Remapper() {
}
SignatureReader(descriptor).accept(signatureWriter)
val desc = signatureWriter.toString()
val universal = translate(owner).replace('/', '.')
return mapping.methods.firstOrNull { it.path == universal && it.translateName == name && it.descriptor == desc }?.mojangName ?: name
// 当前运行时的 Owner 名称
val runningOwner = translate(owner).replace('/', '.')
val findPath = parentsCacheMap.getOrPut(runningOwner) { findParents(runningOwner).reversed() }
return MinecraftVersion.mapping.methods.find { it.path in findPath && it.translateName == name && it.descriptor == desc }?.mojangName ?: name
}
return name
}
Expand All @@ -77,18 +82,39 @@ open class MinecraftRemapper : Remapper() {
// 将低版本包名替换为高版本包名
// net/minecraft/server/v1_17_R1/EntityPlayer -> net/minecraft/server/level/EntityPlayer
if (key.startsWith("net/minecraft/server/v1_")) {
mapping.classMap[key.substringAfterLast('/', "")]?.replace('.', '/') ?: key
MinecraftVersion.mapping.classMap[key.substringAfterLast('/', "")]?.replace('.', '/') ?: key
} else {
key
}
} else {
// 将高版本包名替换为低版本包名
// net/minecraft/server/level/EntityPlayer -> net/minecraft/server/v1_17_R1/EntityPlayer
if (mapping.classMap.containsValue(key.replace('.', '/'))) {
if (MinecraftVersion.mapping.classMap.containsValue(key.replace('.', '/'))) {
"net/minecraft/server/${MinecraftVersion.minecraftVersion}/${key.substringAfterLast('/', "")}"
} else {
key.replace(nms1, nms2)
}
}
}

/**
* 获取类的所有父类和接口
*/
fun findParents(owner: String): Set<String> {
if (owner.startsWith("net.minecraft") || owner.startsWith("com.mojang")) {
try {
val find = hashSetOf<String>()
find += owner
val forName = Class.forName(owner)
find += forName.interfaces.map { it.name }
val superclass = forName.superclass
if (superclass != null && superclass.name != "java.lang.Object") {
find += findParents(superclass.name)
}
return find
} catch (_: Throwable) {
}
}
return hashSetOf(owner)
}
}

0 comments on commit 7078148

Please sign in to comment.