mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 21:15:53 +08:00
Refactor: Detect Minecraft runtime in Loader
- Add runtime detection package for class loader, loader type and version - Probe vanilla, Forge, NeoForge, Fabric and MCP installations - Read 26.x versions from version.json and support Forge version lookups - Unify structural probing in MinecraftProbe and use the game class loader for reflection - Shade gson into the loader jar under a relocated package - Restore OrangeX injection using the detected class loader
This commit is contained in:
1 parent
fc7a26f051
commit
60baa8d1ef
14 files changed
+828
-4
No files matched your search
+12
-1
@@ -12,9 +12,20 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation files("../libraries/JuiceAgent-API-1.4.1+build.1.jar")
|
||||
|
||||
// Source: https://mvnrepository.com/artifact/org.projectlombok/lombok
|
||||
compileOnly 'org.projectlombok:lombok:1.18.48'
|
||||
annotationProcessor 'org.projectlombok:lombok:1.18.48'
|
||||
|
||||
// Source: https://mvnrepository.com/artifact/com.google.code.gson/gson
|
||||
implementation 'com.google.code.gson:gson:2.15.0'
|
||||
}
|
||||
|
||||
tasks.named('shadowJar') {
|
||||
destinationDirectory = rootProject.layout.projectDirectory.dir('out/tmp')
|
||||
archiveFileName = "${project.name}.jar"
|
||||
}
|
||||
}
|
||||
|
||||
shadowJar {
|
||||
relocate 'com.google.gson', 'cn.xiaozhou233.orangex.loader.shaded.gson'
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package cn.xiaozhou233.orangex.loader;
|
||||
|
||||
import cn.xiaozhou233.juiceagent.api.JuiceAgent;
|
||||
import cn.xiaozhou233.orangex.loader.runtime.RuntimeDetector;
|
||||
import cn.xiaozhou233.orangex.loader.runtime.RuntimeInfo;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@@ -15,6 +17,9 @@ public class Loader {
|
||||
public static void entry() {
|
||||
System.out.println("OrangeX-Loader " + VERSION + " loaded!");
|
||||
|
||||
RuntimeInfo info = RuntimeDetector.detect();
|
||||
RuntimeDetector.debug(info);
|
||||
|
||||
// Find client thread
|
||||
for (Thread t : Thread.getAllStackTraces().keySet()) {
|
||||
if ("Client thread".equals(t.getName())) {
|
||||
@@ -26,8 +31,13 @@ public class Loader {
|
||||
throw new IllegalStateException("Cannot find Minecraft client thread");
|
||||
}
|
||||
|
||||
// Get Minecraft ClassLoader
|
||||
minecraftClassLoader = clientThread.getContextClassLoader();
|
||||
// Get Minecraft ClassLoader (prefer the detected game class loader)
|
||||
minecraftClassLoader = info.getMinecraftClassLoader() != null
|
||||
? info.getMinecraftClassLoader()
|
||||
: clientThread.getContextClassLoader();
|
||||
if (minecraftClassLoader == null) {
|
||||
throw new IllegalStateException("Cannot find Minecraft ClassLoader");
|
||||
}
|
||||
System.out.println("Minecraft ClassLoader: " + minecraftClassLoader);
|
||||
|
||||
// Dev Only: Load JuiceAgent
|
||||
@@ -60,4 +70,4 @@ public class Loader {
|
||||
public static boolean isDevEnvironment() {
|
||||
return Boolean.getBoolean("orangex.dev");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package cn.xiaozhou233.orangex.loader.runtime;
|
||||
|
||||
public class ClassProbe {
|
||||
public static boolean exists(ClassLoader loader, String... names) {
|
||||
return find(loader, names) != null;
|
||||
}
|
||||
|
||||
public static Class<?> find(ClassLoader loader, String... names) {
|
||||
if (loader == null)
|
||||
return null;
|
||||
|
||||
if (names == null)
|
||||
return null;
|
||||
|
||||
for (String name : names) {
|
||||
// Check for empty names
|
||||
if (name == null || name.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String binaryName = toBinaryName(name);
|
||||
|
||||
// Check for class existence
|
||||
try {
|
||||
return Class.forName(binaryName, false, loader);
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts both binary names ({@code net.minecraft.client.Minecraft}) and
|
||||
* internal names ({@code net/minecraft/client/Minecraft}).
|
||||
*/
|
||||
private static String toBinaryName(String name) {
|
||||
return name.indexOf('/') >= 0 ? name.replace('/', '.') : name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package cn.xiaozhou233.orangex.loader.runtime;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Probes the given loader. The game class and every anchor must be defined by
|
||||
* the same class loader, otherwise classes inherited from a parent loader can
|
||||
* produce false positives.
|
||||
*/
|
||||
public boolean probe(ClassLoader loader) {
|
||||
// Class Check
|
||||
Class<?> mcClass = ClassProbe.find(loader, minecraftClass);
|
||||
if (mcClass == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ClassLoader definingLoader = mcClass.getClassLoader();
|
||||
if (definingLoader == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Anchor Check
|
||||
for (String anchor : minecraftAnchors) {
|
||||
if (anchor == null || anchor.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Class<?> anchorClass = ClassProbe.find(definingLoader, anchor);
|
||||
if (anchorClass == null || anchorClass.getClassLoader() != definingLoader) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return matchesStructure(mcClass);
|
||||
}
|
||||
|
||||
private boolean matchesStructure(Class<?> mcClass) {
|
||||
try {
|
||||
// Method(Getter) Check
|
||||
if (minecraftGetter != null && !minecraftGetter.isEmpty()) {
|
||||
Method getter = mcClass.getDeclaredMethod(minecraftGetter);
|
||||
boolean getterOk = Modifier.isStatic(getter.getModifiers())
|
||||
&& getter.getReturnType() == mcClass;
|
||||
if (!getterOk) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Instance Field Check (Tips: Some version of minecraft does not have this field)
|
||||
if (minecraftInstanceField == null || minecraftInstanceField.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Field instance = mcClass.getDeclaredField(minecraftInstanceField);
|
||||
return Modifier.isStatic(instance.getModifiers())
|
||||
&& instance.getType() == mcClass;
|
||||
} catch (ReflectiveOperationException | SecurityException | LinkageError e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package cn.xiaozhou233.orangex.loader.runtime;
|
||||
|
||||
import cn.xiaozhou233.orangex.loader.runtime.classloader.GameClassLoaderResolver;
|
||||
import cn.xiaozhou233.orangex.loader.runtime.loader.LoaderDetector;
|
||||
import cn.xiaozhou233.orangex.loader.runtime.loader.LoaderType;
|
||||
import cn.xiaozhou233.orangex.loader.runtime.version.MinecraftVersion;
|
||||
import cn.xiaozhou233.orangex.loader.runtime.version.VersionDetector;
|
||||
|
||||
public final class RuntimeDetector {
|
||||
public static RuntimeInfo detect() {
|
||||
ClassLoader classLoader = GameClassLoaderResolver.resolve();
|
||||
LoaderType loaderType = LoaderDetector.detect(classLoader);
|
||||
MinecraftVersion version = VersionDetector.detect(classLoader, loaderType);
|
||||
return new RuntimeInfo(classLoader, loaderType, version);
|
||||
}
|
||||
|
||||
public static void debug(RuntimeInfo runtime) {
|
||||
System.out.println("======== Minecraft Runtime Info ========");
|
||||
|
||||
if (runtime == null) {
|
||||
System.out.println("RuntimeInfo: null");
|
||||
System.out.println("========================================");
|
||||
return;
|
||||
}
|
||||
|
||||
ClassLoader loader = runtime.getMinecraftClassLoader();
|
||||
MinecraftVersion version = runtime.getMinecraftVersion();
|
||||
int protocol = MinecraftVersion.getCurrentProtocolVersion();
|
||||
|
||||
System.out.println("Minecraft ClassLoader: " + loader);
|
||||
System.out.println("Context ClassLoader (current thread): " + Thread.currentThread().getContextClassLoader());
|
||||
if (loader != null) {
|
||||
StringBuilder hierarchy = new StringBuilder(loader.getClass().getName());
|
||||
ClassLoader parent = loader.getParent();
|
||||
while (parent != null) {
|
||||
hierarchy.append(" -> ").append(parent.getClass().getName());
|
||||
parent = parent.getParent();
|
||||
}
|
||||
System.out.println("ClassLoader Hierarchy: " + hierarchy);
|
||||
}
|
||||
System.out.println("Loader Type: " + (runtime.getLoaderType() == null ? "UNKNOWN" : runtime.getLoaderType().name()));
|
||||
System.out.println("Minecraft Version: " + (version == null ? "UNKNOWN" : version.name())
|
||||
+ " (version id " + protocol + ")");
|
||||
System.out.println("========================================");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package cn.xiaozhou233.orangex.loader.runtime;
|
||||
|
||||
import cn.xiaozhou233.orangex.loader.runtime.loader.LoaderType;
|
||||
import cn.xiaozhou233.orangex.loader.runtime.version.MinecraftVersion;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public class RuntimeInfo {
|
||||
private final ClassLoader minecraftClassLoader;
|
||||
private final LoaderType loaderType;
|
||||
private final MinecraftVersion minecraftVersion;
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package cn.xiaozhou233.orangex.loader.runtime.classloader;
|
||||
|
||||
import cn.xiaozhou233.orangex.loader.runtime.ClassProbe;
|
||||
|
||||
public final class GameClassLoaderResolver {
|
||||
|
||||
public static ClassLoader resolve() {
|
||||
ClassLoader loader = findGameThreadLoader();
|
||||
if (loader != null) {
|
||||
return loader;
|
||||
}
|
||||
|
||||
loader = findMinecraftLoader(Thread.currentThread().getContextClassLoader());
|
||||
if (loader != null) {
|
||||
return loader;
|
||||
}
|
||||
|
||||
return Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
|
||||
private static ClassLoader findGameThreadLoader() {
|
||||
// Client thread wins over render thread, regardless of thread iteration order
|
||||
ClassLoader clientLoader = null;
|
||||
ClassLoader renderLoader = null;
|
||||
|
||||
try {
|
||||
for (Thread thread : Thread.getAllStackTraces().keySet()) {
|
||||
String name = thread.getName();
|
||||
|
||||
if ("Client thread".equals(name)) {
|
||||
if (clientLoader == null) {
|
||||
clientLoader = thread.getContextClassLoader();
|
||||
}
|
||||
} else if ("Render thread".equals(name)) {
|
||||
if (renderLoader == null) {
|
||||
renderLoader = thread.getContextClassLoader();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
|
||||
return clientLoader != null ? clientLoader : renderLoader;
|
||||
}
|
||||
|
||||
private static ClassLoader findMinecraftLoader(ClassLoader candidate) {
|
||||
if (candidate == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String[] minecraftClasses = {
|
||||
"net.minecraft.client.Minecraft",
|
||||
"net.minecraft.class_310"
|
||||
};
|
||||
|
||||
Class<?> minecraftClass = ClassProbe.find(candidate, minecraftClasses);
|
||||
if (minecraftClass != null) {
|
||||
ClassLoader minecraftLoader = minecraftClass.getClassLoader();
|
||||
return minecraftLoader == null ? candidate : minecraftLoader;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private GameClassLoaderResolver() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package cn.xiaozhou233.orangex.loader.runtime.loader;
|
||||
|
||||
import cn.xiaozhou233.orangex.loader.runtime.ClassProbe;
|
||||
|
||||
public final class LoaderDetector {
|
||||
|
||||
public static LoaderType detect(ClassLoader loader) {
|
||||
if (loader == null) {
|
||||
return LoaderType.UNKNOWN;
|
||||
}
|
||||
|
||||
// NeoForge has higher priority because it is a Forge fork
|
||||
if (isNeoForge(loader)) {
|
||||
return LoaderType.NEOFORGE;
|
||||
}
|
||||
|
||||
// Forge
|
||||
if (isForge(loader)) {
|
||||
return LoaderType.FORGE;
|
||||
}
|
||||
|
||||
// Fabric
|
||||
if (isFabric(loader)) {
|
||||
return LoaderType.FABRIC;
|
||||
}
|
||||
|
||||
// MCP: Minecraft deobfuscated (development) environment
|
||||
// Has real class names (net.minecraft.client.Minecraft) but no mod loader
|
||||
if (isMCP(loader)) {
|
||||
return LoaderType.MCP;
|
||||
}
|
||||
|
||||
// No mod loader found, obfuscated vanilla
|
||||
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");
|
||||
}
|
||||
|
||||
private static boolean isMCP(ClassLoader loader) {
|
||||
try {
|
||||
Class<?> mcClass = Class.forName("net.minecraft.client.Minecraft", false, loader);
|
||||
mcClass.getDeclaredMethod("getMinecraft");
|
||||
return true;
|
||||
} catch (Throwable t) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package cn.xiaozhou233.orangex.loader.runtime.loader;
|
||||
|
||||
public enum LoaderType {
|
||||
VANILLA,
|
||||
MCP,
|
||||
FORGE,
|
||||
NEOFORGE,
|
||||
FABRIC,
|
||||
UNKNOWN
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package cn.xiaozhou233.orangex.loader.runtime.utils;
|
||||
|
||||
public class ForgeUtils {
|
||||
public static int parseForgeVersion(Object value) {
|
||||
if (value == null) {
|
||||
return -1;
|
||||
}
|
||||
String text = String.valueOf(value).trim();
|
||||
int start = 0;
|
||||
while (start < text.length() && !Character.isDigit(text.charAt(start))) {
|
||||
++start;
|
||||
}
|
||||
int end = start;
|
||||
while (end < text.length() && Character.isDigit(text.charAt(end))) {
|
||||
++end;
|
||||
}
|
||||
if (start == end) {
|
||||
return -1;
|
||||
}
|
||||
try {
|
||||
int parsed = Integer.parseInt(text.substring(start, end));
|
||||
if (parsed == 62) {
|
||||
return 100;
|
||||
}
|
||||
if (parsed == 65) {
|
||||
return 110;
|
||||
}
|
||||
return isKnownVersionId(parsed) ? parsed : -1;
|
||||
}
|
||||
catch (NumberFormatException ignored) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isKnownVersionId(int version) {
|
||||
switch (version) {
|
||||
case 13: case 15: case 23: case 28: case 35: case 36: case 37:
|
||||
case 47: case 50: case 51: case 52: case 54: case 55: case 56: case 60: case 61:
|
||||
case 100: case 110:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package cn.xiaozhou233.orangex.loader.runtime.utils;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class MinecraftUtils {
|
||||
public static int matchesVersionMetadata(Class<?> minecraftClass) {
|
||||
try (InputStream stream = minecraftClass.getResourceAsStream(
|
||||
"/version.json")) {
|
||||
if (stream == null) {
|
||||
return 0;
|
||||
}
|
||||
JsonElement element = JsonParser.parseReader(new InputStreamReader(
|
||||
stream, StandardCharsets.UTF_8));
|
||||
if (!element.isJsonObject()) {
|
||||
return 0;
|
||||
}
|
||||
JsonObject version = element.getAsJsonObject();
|
||||
int javaVersion = getInteger(version, "java_version");
|
||||
if (javaVersion != Integer.MIN_VALUE && javaVersion < 25) {
|
||||
return 0;
|
||||
}
|
||||
String id = getString(version, "id");
|
||||
String name = getString(version, "name");
|
||||
if (id != null && id.equals(name)) {
|
||||
boolean is261Family = id.startsWith("26.1")
|
||||
&& (id.length() == 4 || id.charAt(4) == '.');
|
||||
boolean is262Family = id.startsWith("26.2")
|
||||
&& (id.length() == 4 || id.charAt(4) == '.');
|
||||
if (is261Family) {
|
||||
return 100;
|
||||
}
|
||||
if (is262Family) {
|
||||
return 110;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
catch (Exception ignored) {
|
||||
return 0;
|
||||
}
|
||||
catch (LinkageError ignored) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private static String getString(JsonObject object, String name) {
|
||||
JsonElement value = object.get(name);
|
||||
return value != null && value.isJsonPrimitive()
|
||||
? value.getAsString() : null;
|
||||
}
|
||||
|
||||
private static int getInteger(JsonObject object, String name) {
|
||||
JsonElement value = object.get(name);
|
||||
return value != null && value.isJsonPrimitive()
|
||||
? value.getAsInt() : Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package cn.xiaozhou233.orangex.loader.runtime.utils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class ReflectUtils {
|
||||
public static Object readStaticField(String className, String fieldName) throws Exception {
|
||||
return readStaticField(null, className, fieldName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param loader the class loader used to resolve the owner class; when
|
||||
* {@code null} the caller's class loader is used
|
||||
*/
|
||||
public static Object readStaticField(ClassLoader loader, String className, String fieldName) throws Exception {
|
||||
Class<?> owner = loadClass(loader, className);
|
||||
Field field;
|
||||
try {
|
||||
field = owner.getField(fieldName);
|
||||
}
|
||||
catch (NoSuchFieldException ignored) {
|
||||
field = owner.getDeclaredField(fieldName);
|
||||
}
|
||||
field.setAccessible(true);
|
||||
return field.get(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param loader the class loader used to resolve the class; when
|
||||
* {@code null} the caller's class loader is used
|
||||
*/
|
||||
public static Class<?> loadClass(ClassLoader loader, String className) throws ClassNotFoundException {
|
||||
return loader == null
|
||||
? Class.forName(className)
|
||||
: Class.forName(className, false, loader);
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package cn.xiaozhou233.orangex.loader.runtime.version;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public enum MinecraftVersion {
|
||||
// Note: the numbers below are OrangeX's internal version ids, NOT the
|
||||
// Minecraft network protocol numbers (e.g. 1.8.9 is id 15 here, but its
|
||||
// network protocol is 47).
|
||||
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;
|
||||
}
|
||||
}
|
||||
+264
@@ -0,0 +1,264 @@
|
||||
package cn.xiaozhou233.orangex.loader.runtime.version;
|
||||
|
||||
import cn.xiaozhou233.orangex.loader.runtime.ClassProbe;
|
||||
import cn.xiaozhou233.orangex.loader.runtime.MinecraftProbe;
|
||||
import cn.xiaozhou233.orangex.loader.runtime.loader.LoaderType;
|
||||
import cn.xiaozhou233.orangex.loader.runtime.utils.ForgeUtils;
|
||||
import cn.xiaozhou233.orangex.loader.runtime.utils.MinecraftUtils;
|
||||
import cn.xiaozhou233.orangex.loader.runtime.utils.ReflectUtils;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public final class VersionDetector {
|
||||
|
||||
public static MinecraftVersion detect(ClassLoader loader, LoaderType loaderType) {
|
||||
int protocol = detectProtocol(loader, loaderType);
|
||||
MinecraftVersion.setCurrentProtocolVersion(protocol);
|
||||
return MinecraftVersion.fromProtocolId(protocol);
|
||||
}
|
||||
|
||||
private static int detectProtocol(ClassLoader loader, LoaderType loaderType) {
|
||||
Throwable lastFailure = null;
|
||||
|
||||
// Layer 1: Forge minorVersion (legacy Forge)
|
||||
try {
|
||||
Object minorVersion = ReflectUtils.readStaticField(loader,
|
||||
"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(loader,
|
||||
"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(loader,
|
||||
"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 = ReflectUtils.loadClass(loader,
|
||||
"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 (reuse the loader detection result)
|
||||
boolean isNeoForge = loaderType == LoaderType.NEOFORGE
|
||||
|| (loaderType == LoaderType.UNKNOWN && ClassProbe.exists(loader,
|
||||
"net.neoforged.fml.ModList",
|
||||
"net.neoforged.fml.loading.FMLLoader"));
|
||||
boolean isFabric = loaderType == LoaderType.FABRIC
|
||||
|| (loaderType == LoaderType.UNKNOWN && 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) {
|
||||
System.out.println("[VersionDetector] Forge/Fabric version detection failed, falling back to structural probe");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static int detectByStructuralProbe(ClassLoader loader, boolean isNeoForge, boolean isFabric) {
|
||||
Set<ClassLoader> loaders = candidateLoaders(loader);
|
||||
|
||||
boolean vanilla189 = probe(loaders, new MinecraftProbe("ave", "A", "S", "bew", "adm"));
|
||||
boolean vanilla1122 = probe(loaders, new MinecraftProbe("bib", "z", "R", "bud", "amu"));
|
||||
boolean vanilla1165 = probe(loaders, new MinecraftProbe("djz", "C", "F", "dwt", "dzm", "dzj", "brx"));
|
||||
boolean vanilla1201 = probe(loaders, new MinecraftProbe("enn", "N", "F", "few", "fiy", "fiv", "cmm"));
|
||||
boolean vanilla1206 = probe(loaders, new MinecraftProbe("ffh", "Q", "E", "fxx", "gcs", "gcp", "dca"));
|
||||
boolean vanilla1211 = probe(loaders, new MinecraftProbe("fgo", "Q", "E", "fzf", "geb", "gdy", "dcw"));
|
||||
boolean vanilla12111 = probe(loaders, new MinecraftProbe("gfj", "V", "A", "hif", "hnh", "hne", "dwo"));
|
||||
|
||||
boolean vanilla262 = isVanilla262Present(loaders);
|
||||
boolean fabric262 = vanilla262 && isFabric;
|
||||
|
||||
boolean excludesModern = vanilla262 || fabric262;
|
||||
|
||||
boolean neoForge1201 = isNeoForge && probe(loaders, new MinecraftProbe(
|
||||
"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 && probe(loaders, new MinecraftProbe(
|
||||
"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 && probe(loaders, new MinecraftProbe(
|
||||
"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"));
|
||||
|
||||
// MCP (deobfuscated) detection - only 1.8.9
|
||||
boolean mcp = probe(loaders, new MinecraftProbe(
|
||||
"net.minecraft.client.Minecraft", "getMinecraft", "theMinecraft",
|
||||
"net.minecraft.client.entity.EntityPlayerSP",
|
||||
"net.minecraft.client.gui.GuiIngame",
|
||||
"net.minecraft.client.renderer.EntityRenderer",
|
||||
"net.minecraft.client.network.NetHandlerPlayClient"));
|
||||
|
||||
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)
|
||||
+ (mcp ? 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;
|
||||
}
|
||||
if (mcp) return 15;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static boolean probe(Set<ClassLoader> loaders, MinecraftProbe minecraftProbe) {
|
||||
for (ClassLoader loader : loaders) {
|
||||
if (minecraftProbe.probe(loader)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
if (!new MinecraftProbe("net/minecraft/client/Minecraft", "getInstance", "instance").probe(loader)) {
|
||||
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;
|
||||
}
|
||||
|
||||
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() {
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user