Feat: New LoaderDetector and VersionDetector

This commit is contained in:
xiaozhou233 committed 2026-08-25 16:27:12 +08:00
1 parent 19b9a1e2f5
commit 6ddc3b2cdb
6 files changed
+487 -33

No files matched your search

@@ -0,0 +1,52 @@
package cn.xiaozhou233.orangex;
import cn.xiaozhou233.orangex.runtime.ClassProbe;
public class MinecraftProbe {
private final String minecraftClass;
private final String minecraftGetter;
private final String minecraftInstanceField;
private final String[] minecraftAnchors;
public MinecraftProbe(String minecraftClass, String minecraftGetter, String minecraftInstanceField, String... minecraftAnchors) {
this.minecraftClass = minecraftClass;
this.minecraftGetter = minecraftGetter;
this.minecraftInstanceField = minecraftInstanceField;
this.minecraftAnchors = minecraftAnchors;
}
public boolean probe(ClassLoader loader) {
// Class Check
Class<?> mcClass = ClassProbe.find(loader, minecraftClass);
if (mcClass == null) {
return false;
}
// Method(Getter) Check
try {
mcClass.getDeclaredMethod(minecraftGetter);
} catch (NoSuchMethodException e) {
return false;
}
// Instance Field Check (Tips: Some version of minecraft does not have this field)
try {
mcClass.getDeclaredField(minecraftInstanceField);
} catch (NoSuchFieldException ignore) {
// empty -> don't check (does not have minecraft field)
// not empty -> return false
System.out.println("[DEBUG] minecraftInstanceField: " + minecraftInstanceField + " -> " + !minecraftInstanceField.isEmpty());
if(!minecraftInstanceField.isEmpty())
return false;
}
// Anchor Check
for (String anchor : minecraftAnchors) {
if (!ClassProbe.exists(loader, anchor)) {
return false;
}
}
return true;
}
}
@@ -16,10 +16,22 @@ public final class RuntimeDetector {
public static void debug() {
RuntimeInfo runtime = detect();
ClassLoader loader = runtime.getMinecraftClassLoader();
MinecraftVersion version = runtime.getMinecraftVersion();
int protocol = MinecraftVersion.getCurrentProtocolVersion();
System.out.println("======== Minecraft Runtime Info ========");
System.out.println("Minecraft ClassLoader: " + runtime.getMinecraftClassLoader().getClass().getName());
System.out.println("Minecraft ClassLoader: " + loader.getClass().getName());
System.out.println("Context ClassLoader: " + Thread.currentThread().getContextClassLoader());
System.out.print("ClassLoader Hierarchy: " + loader.getClass().getName());
ClassLoader parent = loader.getParent();
while (parent != null) {
System.out.print(" -> " + parent.getClass().getName());
parent = parent.getParent();
}
System.out.println();
System.out.println("Loader Type: " + runtime.getLoaderType().name());
System.out.println("Minecraft Version: " + runtime.getMinecraftVersion().name());
System.out.println("Minecraft Version: " + version.name() + " (protocol " + protocol + ")");
System.out.println("========================================");
}
}
@@ -2,35 +2,52 @@ package cn.xiaozhou233.orangex.runtime.loader;
import cn.xiaozhou233.orangex.runtime.ClassProbe;
public class LoaderDetector {
public final class LoaderDetector {
public static LoaderType detect(ClassLoader loader) {
if(loader == null)
if (loader == null) {
return LoaderType.UNKNOWN;
}
// Loader Class Check
boolean isNeoForge = ClassProbe.exists(loader, "net.neoforged.fml.ModList", "net.neoforged.fml.loading.FMLLoader");
boolean isFabric = ClassProbe.exists(loader, "net.fabricmc.loader.api.FabricLoader", "net.fabricmc.loader.impl.launch.knot.KnotClassLoader");
boolean isForge = ClassProbe.exists(loader, "net.minecraftforge.fml.loading.FMLLoader", "net.minecraftforge.common.ForgeVersion", "net.minecraftforge.fml.common.Loader");
// number of matches
int matches = (isNeoForge ? 1 : 0) + (isFabric ? 1 : 0) + (isForge ? 1 : 0);
// Matches 0: no mod loader detected, Vanilla
if (matches == 0)
return LoaderType.VANILLA;
// Matches > 1 : WTF Why are there multiple Loaders? Unknown
if (matches > 1)
return LoaderType.UNKNOWN;
if (isNeoForge)
// NeoForge has higher priority because it is a Forge fork
if (isNeoForge(loader)) {
return LoaderType.NEOFORGE;
if (isFabric)
return LoaderType.FABRIC;
if (isForge)
}
// Forge
if (isForge(loader)) {
return LoaderType.FORGE;
}
return LoaderType.UNKNOWN;
// Fabric
if (isFabric(loader)) {
return LoaderType.FABRIC;
}
// No mod loader found
return LoaderType.VANILLA;
}
}
private static boolean isNeoForge(ClassLoader loader) {
return ClassProbe.exists(loader,
"net.neoforged.fml.ModList",
"net.neoforged.fml.loading.FMLLoader");
}
private static boolean isForge(ClassLoader loader) {
return ClassProbe.exists(loader,
"net.minecraftforge.fml.common.Loader")
||
ClassProbe.exists(loader,
"net.minecraftforge.fml.loading.FMLLoader");
}
private static boolean isFabric(ClassLoader loader) {
return ClassProbe.exists(loader,
"net.fabricmc.loader.api.FabricLoader");
}
}
@@ -2,6 +2,7 @@ package cn.xiaozhou233.orangex.runtime.loader;
public enum LoaderType {
VANILLA,
MCP,
FORGE,
NEOFORGE,
FABRIC,
@@ -1,10 +1,69 @@
package cn.xiaozhou233.orangex.runtime.version;
import lombok.Getter;
import lombok.Setter;
public enum MinecraftVersion {
V1_8_9,
V1_12_2,
V1_16_5,
V1_21_1,
V26_2,
UNKNOWN;
V1_7_10(13),
V1_8_9(15),
V1_12_2(23),
V1_14_4(28),
V1_16_5(35),
V1_16_5_ACTUAL(36),
V1_17(37),
V1_20_1(47),
V1_20_6(50),
V1_21_0(51),
V1_21_1(52),
V1_21_4(54),
V1_21_5(55),
V1_21_6(56),
V1_21_10(60),
V1_21_11(61),
V26_1(100),
V26_2(110),
UNKNOWN(0);
@Setter
@Getter
public static int currentProtocolVersion;
@Getter
private final int protocolVersion;
MinecraftVersion(int protocolVersion) {
this.protocolVersion = protocolVersion;
}
public boolean is(int current) {
return current == this.protocolVersion;
}
public boolean isNot(int current) {
return current != this.protocolVersion;
}
public boolean greaterThan(int current) {
return current > this.protocolVersion;
}
public boolean greaterThanOrEqual(int current) {
return current >= this.protocolVersion;
}
public boolean lessThan(int current) {
return current < this.protocolVersion;
}
public boolean lessThanOrEqual(int current) {
return current <= this.protocolVersion;
}
public static MinecraftVersion fromProtocolId(int protocolId) {
for (MinecraftVersion version : values()) {
if (version.protocolVersion == protocolId) {
return version;
}
}
return UNKNOWN;
}
}
@@ -1,10 +1,323 @@
package cn.xiaozhou233.orangex.runtime.version;
import cn.xiaozhou233.orangex.runtime.ClassProbe;
import cn.xiaozhou233.orangex.runtime.loader.LoaderType;
import cn.xiaozhou233.orangex.runtime.utils.ForgeUtils;
import cn.xiaozhou233.orangex.runtime.utils.MinecraftUtils;
import cn.xiaozhou233.orangex.runtime.utils.ReflectUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.LinkedHashSet;
import java.util.Set;
public final class VersionDetector {
private static int detectedVersion;
public static MinecraftVersion detect(ClassLoader loader, LoaderType loaderType) {
return MinecraftVersion.UNKNOWN;
int protocol = detectProtocol(loader);
MinecraftVersion.setCurrentProtocolVersion(protocol);
return MinecraftVersion.fromProtocolId(protocol);
}
private static int detectProtocol(ClassLoader loader) {
Throwable lastFailure = null;
// Layer 1: Forge minorVersion (legacy Forge)
try {
Object minorVersion = ReflectUtils.readStaticField(
"net.minecraftforge.common.ForgeVersion", "minorVersion");
if (minorVersion instanceof Number) {
return ((Number) minorVersion).intValue();
}
lastFailure = new IllegalStateException("ForgeVersion.minorVersion is not numeric");
} catch (Throwable t) {
lastFailure = t;
}
// Layer 2: FMLLoader.forgeVersion (modern Forge/NeoForge)
try {
Object forgeVersion = ReflectUtils.readStaticField(
"net.minecraftforge.fml.loading.FMLLoader", "forgeVersion");
int parsed = ForgeUtils.parseForgeVersion(forgeVersion);
if (parsed >= 0) {
return parsed;
}
lastFailure = new IllegalStateException("FMLLoader.forgeVersion not supported: " + forgeVersion);
} catch (Throwable t) {
lastFailure = t;
}
// Layer 3: ForgeVersion.forgeVersion (newer Forge)
try {
Object forgeVersion = ReflectUtils.readStaticField(
"net.minecraftforge.common.ForgeVersion", "forgeVersion");
int parsed = ForgeUtils.parseForgeVersion(forgeVersion);
if (parsed >= 0) {
return parsed;
}
lastFailure = new IllegalStateException("ForgeVersion.forgeVersion not supported: " + forgeVersion);
} catch (Throwable t) {
lastFailure = t;
}
// Layer 4: FMLLoader.versionInfo().forgeVersion() (newest Forge)
try {
Class<?> fmlLoader = Class.forName("net.minecraftforge.fml.loading.FMLLoader");
Object versionInfo = fmlLoader.getMethod("versionInfo").invoke(null);
Object forgeVersion = versionInfo.getClass().getMethod("forgeVersion").invoke(versionInfo);
int parsed = ForgeUtils.parseForgeVersion(forgeVersion);
if (parsed >= 0) {
return parsed;
}
lastFailure = new IllegalStateException("FMLLoader.versionInfo().forgeVersion() not supported: " + forgeVersion);
} catch (Throwable t) {
lastFailure = t;
}
// Layer 5: Fabric/NeoForge marker detection
boolean isNeoForge = ClassProbe.exists(loader,
"net.neoforged.fml.ModList",
"net.neoforged.fml.loading.FMLLoader");
boolean isFabric = ClassProbe.exists(loader,
"net.fabricmc.loader.api.FabricLoader");
// Layer 6: Structural detection via obfuscated/mojmap class probing
int structuralVersion = detectByStructuralProbe(loader, isNeoForge, isFabric);
if (structuralVersion != 0) {
return structuralVersion;
}
// Layer 7: version.json detection for 26.x
int modernVersion = detectModernVersion(loader);
if (modernVersion != 0) {
return modernVersion;
}
if (lastFailure != null) {
lastFailure.printStackTrace();
}
return 0;
}
private static int detectByStructuralProbe(ClassLoader loader, boolean isNeoForge, boolean isFabric) {
Set<ClassLoader> loaders = candidateLoaders(loader);
boolean vanilla189 = isRuntimePresent(loaders, "ave", "A", "S", "bew", "adm");
boolean vanilla1122 = isRuntimePresent(loaders, "bib", "z", "R", "bud", "amu");
boolean vanilla1165 = isRuntimePresent(loaders, "djz", "C", "F", "dwt", "dzm", "dzj", "brx");
boolean vanilla1201 = isRuntimePresent(loaders, "enn", "N", "F", "few", "fiy", "fiv", "cmm");
boolean vanilla1206 = isRuntimePresent(loaders, "ffh", "Q", "E", "fxx", "gcs", "gcp", "dca");
boolean vanilla1211 = isRuntimePresent(loaders, "fgo", "Q", "E", "fzf", "geb", "gdy", "dcw");
boolean vanilla12111 = isRuntimePresent(loaders, "gfj", "V", "A", "hif", "hnh", "hne", "dwo");
boolean vanilla262 = isVanilla262Present(loaders);
boolean fabric262 = vanilla262 && isFabric;
boolean excludesModern = vanilla262 || fabric262;
boolean neoForge1201 = isNeoForge && isMojmapPresent(loaders,
"net/minecraft/client/Minecraft", "getInstance", "instance",
"net/minecraft/client/multiplayer/ClientLevel",
"net/minecraft/client/player/LocalPlayer",
"net/minecraft/client/player/AbstractClientPlayer",
"net/minecraft/world/level/Level")
&& !excludesModern && !vanilla12111;
boolean neoForge1211 = isNeoForge && isMojmapPresent(loaders,
"net/minecraft/client/Minecraft", "getInstance", "instance",
"net/minecraft/client/multiplayer/ClientLevel",
"net/minecraft/client/player/LocalPlayer",
"net/minecraft/client/player/AbstractClientPlayer",
"net/minecraft/world/level/Level",
"net/minecraft/world/item/component/ItemContainerContents")
&& !excludesModern && !vanilla12111;
boolean fabric12111 = isFabric && isRuntimePresent(loaders,
"net/minecraft/class_310", "method_1551", "field_1700",
"net/minecraft/class_638", "net/minecraft/class_746",
"net/minecraft/class_742", "net/minecraft/class_1937",
"com/mojang/blaze3d/buffers/GpuBuffer",
"com/mojang/blaze3d/systems/CommandEncoder");
int matchingVersions = (vanilla189 ? 1 : 0)
+ (vanilla1122 ? 1 : 0)
+ (vanilla1165 ? 1 : 0)
+ (vanilla1201 || neoForge1201 ? 1 : 0)
+ (vanilla1211 || neoForge1211 ? 1 : 0)
+ (vanilla1206 ? 1 : 0)
+ (vanilla12111 || fabric12111 ? 1 : 0)
+ (vanilla262 || fabric262 ? 1 : 0);
if (matchingVersions == 1) {
if (vanilla189) return 15;
if (vanilla1122) return 23;
if (vanilla1165) return 36;
if (vanilla1201 || neoForge1201) return 47;
if (vanilla1211 || neoForge1211) return 52;
if (vanilla1206) return 50;
if (vanilla12111 || fabric12111) return 61;
if (vanilla262) {
int modernProtocol = getModernProtocol(loaders);
return modernProtocol != 0 ? modernProtocol : 110;
}
}
return 0;
}
private static boolean isRuntimePresent(Set<ClassLoader> loaders,
String obfuscatedClass,
String getterName,
String instanceFieldName,
String... anchorClasses) {
for (ClassLoader loader : loaders) {
Class<?> mcClass = resolveClass(obfuscatedClass, loader);
if (mcClass == null) continue;
ClassLoader definingLoader = mcClass.getClassLoader();
if (definingLoader == null) continue;
boolean anchorsMatch = true;
for (String anchor : anchorClasses) {
Class<?> anchorClass = resolveClass(anchor, loader);
if (anchorClass == null || anchorClass.getClassLoader() != definingLoader) {
anchorsMatch = false;
break;
}
}
if (!anchorsMatch) continue;
if (matchesMinecraftStructure(mcClass, getterName, instanceFieldName)) {
return true;
}
}
return false;
}
private static boolean isMojmapPresent(Set<ClassLoader> loaders,
String minecraftClass,
String getterName,
String instanceFieldName,
String... anchorClasses) {
for (ClassLoader loader : loaders) {
Class<?> mcClass = resolveClass(minecraftClass, loader);
if (mcClass == null) continue;
ClassLoader definingLoader = mcClass.getClassLoader();
if (definingLoader == null) continue;
boolean anchorsMatch = true;
for (String anchor : anchorClasses) {
Class<?> anchorClass = resolveClass(anchor, definingLoader);
if (anchorClass == null) {
anchorsMatch = false;
break;
}
}
if (!anchorsMatch) continue;
if (matchesMinecraftStructure(mcClass, getterName, instanceFieldName)) {
return true;
}
}
return false;
}
private static boolean matchesMinecraftStructure(Class<?> minecraftClass,
String getterName,
String instanceFieldName) {
try {
Method getter = minecraftClass.getDeclaredMethod(getterName);
boolean getterOk = Modifier.isStatic(getter.getModifiers())
&& getter.getReturnType() == minecraftClass;
if (!getterOk) return false;
if (instanceFieldName == null || instanceFieldName.isEmpty()) {
return true;
}
Field instance = minecraftClass.getDeclaredField(instanceFieldName);
return Modifier.isStatic(instance.getModifiers())
&& instance.getType() == minecraftClass;
} catch (ReflectiveOperationException | SecurityException | LinkageError e) {
return false;
}
}
private static boolean isVanilla262Present(Set<ClassLoader> loaders) {
return getModernProtocol(loaders) != 0;
}
private static int getModernProtocol(Set<ClassLoader> loaders) {
for (ClassLoader loader : loaders) {
Class<?> mcClass = resolveClass("net/minecraft/client/Minecraft", loader);
if (mcClass == null) continue;
ClassLoader definingLoader = mcClass.getClassLoader();
if (definingLoader == null) continue;
Class<?> clientLevel = resolveClass("net/minecraft/client/multiplayer/ClientLevel", definingLoader);
Class<?> localPlayer = resolveClass("net/minecraft/client/player/LocalPlayer", definingLoader);
Class<?> abstractPlayer = resolveClass("net/minecraft/client/player/AbstractClientPlayer", definingLoader);
Class<?> level = resolveClass("net/minecraft/world/level/Level", definingLoader);
if (clientLevel == null || localPlayer == null
|| abstractPlayer == null || level == null
|| clientLevel.getSuperclass() != level
|| localPlayer.getSuperclass() != abstractPlayer) {
continue;
}
if (matchesMinecraftStructure(mcClass, "getInstance", "instance")) {
int protocol = MinecraftUtils.matchesVersionMetadata(mcClass);
if (protocol != 0) {
return protocol;
}
}
}
return 0;
}
private static int detectModernVersion(ClassLoader loader) {
Set<ClassLoader> loaders = candidateLoaders(loader);
return getModernProtocol(loaders);
}
private static Class<?> resolveClass(String internalName, ClassLoader loader) {
String binaryName = internalName.replace('/', '.');
try {
return Class.forName(binaryName, false, loader);
} catch (ClassNotFoundException | LinkageError | SecurityException e) {
return null;
}
}
private static Set<ClassLoader> candidateLoaders(ClassLoader primary) {
Set<ClassLoader> loaders = new LinkedHashSet<>();
if (primary != null) {
loaders.add(primary);
}
ClassLoader context = Thread.currentThread().getContextClassLoader();
if (context != null) {
loaders.add(context);
}
ClassLoader self = VersionDetector.class.getClassLoader();
if (self != null) {
loaders.add(self);
}
try {
ClassLoader system = ClassLoader.getSystemClassLoader();
if (system != null) {
loaders.add(system);
}
} catch (SecurityException ignored) {
}
return loaders;
}
private VersionDetector() {
}
}