Skip to content

Commit b11d65c

Browse files
committed
fix(bukkit-nms): 改进 asm 类转译缓存机制
• 增加环境信息作为缓存键的一部分确保跨环境兼容性 • 添加 getTargetEnvironmentInfo 方法收集服务端版本信息 • 缓存逻辑从仅依赖源码版本改为结合环境信息生成复合键
1 parent afc7c93 commit b11d65c

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

module/bukkit-nms/src/main/kotlin/taboolib/module/nms/AsmClassTranslation.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ class AsmClassTranslation(val source: String) {
5151
}
5252
val bytes = inputStream.readBytes()
5353
val srcVersion = bytes.digest()
54+
// 将源代码版本与环境信息组合,生成复合缓存键
55+
val targetEnvironmentInfo = getTargetEnvironmentInfo()
56+
val combinedVersion = (srcVersion + targetEnvironmentInfo).digest()
5457
// 若存在缓存则直接读取
55-
val (cacheClass, cost) = execution { BinaryCache.read("remap/$source", srcVersion) { AsmClassLoader.createNewClass(source, it) } }
58+
val (cacheClass, cost) = execution { BinaryCache.read("remap/$source", combinedVersion) { AsmClassLoader.createNewClass(source, it) } }
5659
if (cacheClass != null) {
5760
debug("[AsmClassTranslation] 从缓存中加载 $source,用时 $cost 毫秒。")
5861
return cacheClass
@@ -73,10 +76,22 @@ class AsmClassTranslation(val source: String) {
7376
classReader.accept(ClassRemapper(classWriter, remapper), 0)
7477
val newBytes = classWriter.toByteArray()
7578
// 缓存
76-
BinaryCache.save("remap/$source", srcVersion) { newBytes }
79+
BinaryCache.save("remap/$source", combinedVersion) { newBytes }
7780
AsmClassLoader.createNewClass(source, newBytes)
7881
}
7982
debug("[AsmClassTranslation] 转译 $source,用时 $cost2 毫秒。")
8083
return newClass
8184
}
85+
86+
/**
87+
* 获取目标运行环境信息,用于区分不同的服务端环境
88+
* 当环境发生变化时(如从 Spigot 切换到 Arclight),将强制重新转译
89+
*/
90+
private fun getTargetEnvironmentInfo(): String {
91+
val mcRunningVersion = MinecraftVersion.runningVersion
92+
val mcNmsVersion = MinecraftVersion.minecraftVersion
93+
val isUniversal = MinecraftVersion.isUniversal
94+
val isUniversalCB = MinecraftVersion.isUniversalCraftBukkit
95+
return "mcRunning:$mcRunningVersion-nms:$mcNmsVersion-universal:$isUniversal-universalCB:$isUniversalCB"
96+
}
8297
}

0 commit comments

Comments
 (0)