ModVersionGuard 运行时安全守衛 (MC 26.2)
本文件深入闡釋 Minecraft 26.2 下 Item Clumps 的零相依性运行时類別載入器检测、版本守衛架構与位元組碼安全防護機制。
📊 特性資訊框
| 属性 | 数值 |
|---|---|
| 系统名称 | Knot 類別載入器版本守衛 |
| 目標类 | net.instantgratification.item_clumps.util.ModVersionGuard |
| 驗證目標类 | net.minecraft.world.entity.item.ItemEntity |
| 基础库驗證 | net.dasik.social.api.config.ConfigHelper(需要 DasikLibrary $\ge 1.7.4$) |
| 安全目標 | 防止在不相容的 Minecraft API 下启动导致世界存档損壞 |
🛡️ Knot 類別載入器架構与安全解析
在 Fabric Loader 中,模组类是通过不同類別載入器加载的(Knot 類別載入器与系统应用程序加载器)。若不查询当前线程上下文加载器,普通的 Class.forName(name) 可能意外報錯或解析到過時类。
Item Clumps 实现了雙階段段類別載入解析機制:
┌───────────────────────────────────┐
│ ModVersionGuard.checkClass Invoked│
└─────────────────┬─────────────────┘
│
▼
┌───────────────────────────────────┐
│ Query Knot Thread Context Loader │
│ Thread.currentThread() │
│ .getContextClassLoader() │
└─────────────────┬─────────────────┘
│
┌────────────────┴────────────────┐
│ (Success) │ (ClassNotFoundException)
▼ ▼
┌───────────────────────────┐ ┌───────────────────────────┐
│ Game Continues Execution │ │ Fallback to Current Loader│
│ Safe Runtime Verified │ │ ModVersionGuard.class │
└───────────────────────────┘ │ .getClassLoader() │
└─────────────┬─────────────┘
│
┌────────────────┴────────────────┐
│ (Success) │ (ClassNotFoundException)
▼ ▼
┌───────────────────────────┐ ┌───────────────────────────┐
│ Game Continues Execution │ │ Throw Descriptive Warning │
│ Safe Runtime Verified │ │ Halt Corrupted World Load │
└───────────────────────────┘ └───────────────────────────┘💻 真實源码实现参考
源自 Minecraft 26.2 的 ModVersionGuard.java:
java
// Copyright (C) 2026 Dasik (Rifaditya) | GNU GPLv3
package net.instantgratification.item_clumps.util;
public final class ModVersionGuard {
private ModVersionGuard() {}
public static void checkClass(String modName, String requiredClassName) {
ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
ClassLoader currentLoader = ModVersionGuard.class.getClassLoader();
try {
if (contextLoader != null) {
Class.forName(requiredClassName, false, contextLoader);
} else {
Class.forName(requiredClassName, false, currentLoader);
}
} catch (ClassNotFoundException e) {
try {
Class.forName(requiredClassName, false, currentLoader);
} catch (ClassNotFoundException e2) {
throw new RuntimeException("\n" +
"=====================================================================\n" +
" [PRE-RELEASE / VERSION GUARD WARNING] " + modName + "\n" +
"---------------------------------------------------------------------\n" +
" CRITICAL: Incompatible Minecraft Game Runtime or Missing Class!\n" +
" Required Class : " + requiredClassName + "\n" +
" Status : UNRESOLVED AT RUNTIME\n\n" +
" Safety Protection:\n" +
" Execution halted to prevent unreleased/incompatible build deployment\n" +
" or broken world state save corruption.\n\n" +
" Troubleshooting Steps:\n" +
" 1. Verify target Minecraft version (26.2+ release drop).\n" +
" 2. Ensure all required dependencies (Fabric API, DasikLibrary) are loaded.\n" +
" 3. Build/Download a verified matching release JAR from Modrinth/CurseForge.\n" +
"=====================================================================");
}
}
}
}