Refactor: Move Minecraft runtime detection into OrangeX

Move the full runtime package from Loader to OrangeX (cn.xiaozhou233.orangex.runtime), including loader/version detection, probes and utils.

- OrangeX.start() now detects and stores RuntimeInfo, and prints runtime debug info

- Loader keeps only a self-contained GameClassLoaderResolver for bootstrapping the Minecraft ClassLoader; entry flow is otherwise unchanged

- Move gson dependency to OrangeX with shadow relocation; drop it from Loader
This commit is contained in:
xiaozhou233 committed 2026-11-04 00:45:58 +08:00
1 parent 34fc410a0c
commit 789303c665
17 files changed
+140 -42

No files matched your search

-7
View File
@@ -16,16 +16,9 @@ dependencies {
// Source: https://mvnrepository.com/artifact/org.projectlombok/lombok // Source: https://mvnrepository.com/artifact/org.projectlombok/lombok
compileOnly 'org.projectlombok:lombok:1.18.48' compileOnly 'org.projectlombok:lombok:1.18.48'
annotationProcessor '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') { tasks.named('shadowJar') {
destinationDirectory = rootProject.layout.projectDirectory.dir('out/tmp') destinationDirectory = rootProject.layout.projectDirectory.dir('out/tmp')
archiveFileName = "${project.name}.jar" archiveFileName = "${project.name}.jar"
} }
shadowJar {
relocate 'com.google.gson', 'cn.xiaozhou233.orangex.loader.shaded.gson'
}
@@ -1,8 +1,7 @@
package cn.xiaozhou233.orangex.loader; package cn.xiaozhou233.orangex.loader;
import cn.xiaozhou233.juiceagent.api.JuiceAgent; import cn.xiaozhou233.juiceagent.api.JuiceAgent;
import cn.xiaozhou233.orangex.loader.runtime.RuntimeDetector; import cn.xiaozhou233.orangex.loader.classloader.GameClassLoaderResolver;
import cn.xiaozhou233.orangex.loader.runtime.RuntimeInfo;
import java.io.File; import java.io.File;
@@ -17,8 +16,8 @@ public class Loader {
public static void entry() { public static void entry() {
System.out.println("OrangeX-Loader " + VERSION + " loaded!"); System.out.println("OrangeX-Loader " + VERSION + " loaded!");
RuntimeInfo info = RuntimeDetector.detect(); // Resolve the Minecraft ClassLoader (runtime detection itself lives in OrangeX)
RuntimeDetector.debug(info); ClassLoader gameClassLoader = GameClassLoaderResolver.resolve();
// Find client thread // Find client thread
for (Thread t : Thread.getAllStackTraces().keySet()) { for (Thread t : Thread.getAllStackTraces().keySet()) {
@@ -31,9 +30,9 @@ public class Loader {
throw new IllegalStateException("Cannot find Minecraft client thread"); throw new IllegalStateException("Cannot find Minecraft client thread");
} }
// Get Minecraft ClassLoader (prefer the detected game class loader) // Get Minecraft ClassLoader (prefer the resolved game class loader)
minecraftClassLoader = info.getMinecraftClassLoader() != null minecraftClassLoader = gameClassLoader != null
? info.getMinecraftClassLoader() ? gameClassLoader
: clientThread.getContextClassLoader(); : clientThread.getContextClassLoader();
if (minecraftClassLoader == null) { if (minecraftClassLoader == null) {
throw new IllegalStateException("Cannot find Minecraft ClassLoader"); throw new IllegalStateException("Cannot find Minecraft ClassLoader");
@@ -0,0 +1,91 @@
package cn.xiaozhou233.orangex.loader.classloader;
/**
* Loader-side bootstrap resolver: only resolves the Minecraft game class loader,
* which is required before OrangeX can be injected. Full runtime detection
* (loader type / Minecraft version) lives in OrangeX.
*/
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 = findClass(candidate, minecraftClasses);
if (minecraftClass != null) {
ClassLoader minecraftLoader = minecraftClass.getClassLoader();
return minecraftLoader == null ? candidate : minecraftLoader;
}
return null;
}
private static Class<?> findClass(ClassLoader loader, String... names) {
if (loader == null || names == null) {
return null;
}
for (String name : names) {
if (name == null || name.isEmpty()) {
continue;
}
String binaryName = name.indexOf('/') >= 0 ? name.replace('/', '.') : name;
try {
return Class.forName(binaryName, false, loader);
} catch (Throwable ignored) {
}
}
return null;
}
private GameClassLoaderResolver() {
}
}
+7
View File
@@ -14,9 +14,16 @@ dependencies {
// Source: https://mvnrepository.com/artifact/org.projectlombok/lombok // Source: https://mvnrepository.com/artifact/org.projectlombok/lombok
compileOnly 'org.projectlombok:lombok:1.18.48' compileOnly 'org.projectlombok:lombok:1.18.48'
annotationProcessor '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') { tasks.named('shadowJar') {
destinationDirectory = rootProject.layout.projectDirectory.dir('out/tmp') destinationDirectory = rootProject.layout.projectDirectory.dir('out/tmp')
archiveFileName = "${project.name}.jar" archiveFileName = "${project.name}.jar"
}
shadowJar {
relocate 'com.google.gson', 'cn.xiaozhou233.orangex.shaded.gson'
} }
@@ -1,20 +1,28 @@
package cn.xiaozhou233.orangex; package cn.xiaozhou233.orangex;
import cn.xiaozhou233.orangex.runtime.RuntimeDetector;
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
import lombok.Getter; import lombok.Getter;
public class OrangeX { public class OrangeX {
@Getter @Getter
private static final OrangeX instance = new OrangeX(); private static final OrangeX instance = new OrangeX();
@Getter
private RuntimeInfo runtimeInfo;
public OrangeX() { public OrangeX() {
System.out.println("OrangeX <init> start!"); System.out.println("OrangeX <init> start!");
} }
public void start() { public void start() {
System.out.println("OrangeX start!"); System.out.println("OrangeX start!");
runtimeInfo = RuntimeDetector.detect();
RuntimeDetector.debug(runtimeInfo);
} }
public void stop() { public void stop() {
System.out.println("OrangeX stop!"); System.out.println("OrangeX stop!");
} }
} }
@@ -1,4 +1,4 @@
package cn.xiaozhou233.orangex.loader.runtime; package cn.xiaozhou233.orangex.runtime;
public class ClassProbe { public class ClassProbe {
public static boolean exists(ClassLoader loader, String... names) { public static boolean exists(ClassLoader loader, String... names) {
@@ -1,4 +1,4 @@
package cn.xiaozhou233.orangex.loader.runtime; package cn.xiaozhou233.orangex.runtime;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@@ -1,10 +1,10 @@
package cn.xiaozhou233.orangex.loader.runtime; package cn.xiaozhou233.orangex.runtime;
import cn.xiaozhou233.orangex.loader.runtime.classloader.GameClassLoaderResolver; import cn.xiaozhou233.orangex.runtime.classloader.GameClassLoaderResolver;
import cn.xiaozhou233.orangex.loader.runtime.loader.LoaderDetector; import cn.xiaozhou233.orangex.runtime.loader.LoaderDetector;
import cn.xiaozhou233.orangex.loader.runtime.loader.LoaderType; import cn.xiaozhou233.orangex.runtime.loader.LoaderType;
import cn.xiaozhou233.orangex.loader.runtime.version.MinecraftVersion; import cn.xiaozhou233.orangex.runtime.version.MinecraftVersion;
import cn.xiaozhou233.orangex.loader.runtime.version.VersionDetector; import cn.xiaozhou233.orangex.runtime.version.VersionDetector;
public final class RuntimeDetector { public final class RuntimeDetector {
public static RuntimeInfo detect() { public static RuntimeInfo detect() {
@@ -1,7 +1,7 @@
package cn.xiaozhou233.orangex.loader.runtime; package cn.xiaozhou233.orangex.runtime;
import cn.xiaozhou233.orangex.loader.runtime.loader.LoaderType; import cn.xiaozhou233.orangex.runtime.loader.LoaderType;
import cn.xiaozhou233.orangex.loader.runtime.version.MinecraftVersion; import cn.xiaozhou233.orangex.runtime.version.MinecraftVersion;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
@@ -1,6 +1,6 @@
package cn.xiaozhou233.orangex.loader.runtime.classloader; package cn.xiaozhou233.orangex.runtime.classloader;
import cn.xiaozhou233.orangex.loader.runtime.ClassProbe; import cn.xiaozhou233.orangex.runtime.ClassProbe;
public final class GameClassLoaderResolver { public final class GameClassLoaderResolver {
@@ -1,6 +1,6 @@
package cn.xiaozhou233.orangex.loader.runtime.loader; package cn.xiaozhou233.orangex.runtime.loader;
import cn.xiaozhou233.orangex.loader.runtime.ClassProbe; import cn.xiaozhou233.orangex.runtime.ClassProbe;
public final class LoaderDetector { public final class LoaderDetector {
@@ -1,4 +1,4 @@
package cn.xiaozhou233.orangex.loader.runtime.loader; package cn.xiaozhou233.orangex.runtime.loader;
public enum LoaderType { public enum LoaderType {
VANILLA, VANILLA,
@@ -1,4 +1,4 @@
package cn.xiaozhou233.orangex.loader.runtime.utils; package cn.xiaozhou233.orangex.runtime.utils;
public class ForgeUtils { public class ForgeUtils {
public static int parseForgeVersion(Object value) { public static int parseForgeVersion(Object value) {
@@ -1,4 +1,4 @@
package cn.xiaozhou233.orangex.loader.runtime.utils; package cn.xiaozhou233.orangex.runtime.utils;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@@ -1,4 +1,4 @@
package cn.xiaozhou233.orangex.loader.runtime.utils; package cn.xiaozhou233.orangex.runtime.utils;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@@ -1,4 +1,4 @@
package cn.xiaozhou233.orangex.loader.runtime.version; package cn.xiaozhou233.orangex.runtime.version;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@@ -1,11 +1,11 @@
package cn.xiaozhou233.orangex.loader.runtime.version; package cn.xiaozhou233.orangex.runtime.version;
import cn.xiaozhou233.orangex.loader.runtime.ClassProbe; import cn.xiaozhou233.orangex.runtime.ClassProbe;
import cn.xiaozhou233.orangex.loader.runtime.MinecraftProbe; import cn.xiaozhou233.orangex.runtime.MinecraftProbe;
import cn.xiaozhou233.orangex.loader.runtime.loader.LoaderType; import cn.xiaozhou233.orangex.runtime.loader.LoaderType;
import cn.xiaozhou233.orangex.loader.runtime.utils.ForgeUtils; import cn.xiaozhou233.orangex.runtime.utils.ForgeUtils;
import cn.xiaozhou233.orangex.loader.runtime.utils.MinecraftUtils; import cn.xiaozhou233.orangex.runtime.utils.MinecraftUtils;
import cn.xiaozhou233.orangex.loader.runtime.utils.ReflectUtils; import cn.xiaozhou233.orangex.runtime.utils.ReflectUtils;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;