mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 13:05:52 +08:00
Feat: Add Minecraft 26.2 wrapper support
- Implement the 26.2 Minecraft wrapper using the private instance field - Register the 26.2 provider and use identity mappings for Mojang names - Fix MappingManager fallthrough for the 26.2 case - Accept Render thread and resolve the game class loader by class visibility
This commit is contained in:
1 parent
0f2d9614e5
commit
1699df9c5d
8 files changed
+303
-63
No files matched your search
@@ -19,21 +19,15 @@ public class Loader {
|
||||
// Resolve the Minecraft ClassLoader (runtime detection itself lives in OrangeX)
|
||||
ClassLoader gameClassLoader = GameClassLoaderResolver.resolve();
|
||||
|
||||
// Find client thread
|
||||
for (Thread t : Thread.getAllStackTraces().keySet()) {
|
||||
if ("Client thread".equals(t.getName())) {
|
||||
clientThread = t;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (clientThread == null) {
|
||||
throw new IllegalStateException("Cannot find Minecraft client thread");
|
||||
}
|
||||
// Find the game thread. Modern clients use "Render thread"; legacy
|
||||
// clients use "Client thread". It is only needed as a class loader
|
||||
// fallback, so a miss here must not abort injection.
|
||||
clientThread = findGameThread();
|
||||
|
||||
// Get Minecraft ClassLoader (prefer the resolved game class loader)
|
||||
minecraftClassLoader = gameClassLoader != null
|
||||
? gameClassLoader
|
||||
: clientThread.getContextClassLoader();
|
||||
: clientThread != null ? clientThread.getContextClassLoader() : null;
|
||||
if (minecraftClassLoader == null) {
|
||||
throw new IllegalStateException("Cannot find Minecraft ClassLoader");
|
||||
}
|
||||
@@ -69,4 +63,28 @@ public class Loader {
|
||||
public static boolean isDevEnvironment() {
|
||||
return Boolean.getBoolean("orangex.dev");
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy clients call the game thread "Client thread"; 1.13+ calls it
|
||||
* "Render thread". Return whichever is available.
|
||||
*/
|
||||
private static Thread findGameThread() {
|
||||
Thread renderThread = null;
|
||||
|
||||
try {
|
||||
for (Thread thread : Thread.getAllStackTraces().keySet()) {
|
||||
String name = thread.getName();
|
||||
|
||||
if ("Client thread".equals(name)) {
|
||||
return thread;
|
||||
}
|
||||
if ("Render thread".equals(name) && renderThread == null) {
|
||||
renderThread = thread;
|
||||
}
|
||||
}
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
|
||||
return renderThread;
|
||||
}
|
||||
}
|
||||
+58
-26
@@ -7,18 +7,69 @@ package cn.xiaozhou233.orangex.loader.classloader;
|
||||
*/
|
||||
public final class GameClassLoaderResolver {
|
||||
|
||||
/**
|
||||
* Class names that identify the game class loader across supported
|
||||
* runtimes: Mojang/Forge/MCP, Fabric intermediary, and 1.8 vanilla
|
||||
* obfuscated clients.
|
||||
*/
|
||||
private static final String[] MINECRAFT_CLASSES = {
|
||||
"net.minecraft.client.Minecraft",
|
||||
"net.minecraft.class_310",
|
||||
"ave"
|
||||
};
|
||||
|
||||
public static ClassLoader resolve() {
|
||||
ClassLoader loader = findGameThreadLoader();
|
||||
if (loader != null) {
|
||||
return loader;
|
||||
ClassLoader threadLoader = findGameThreadLoader();
|
||||
|
||||
ClassLoader resolved = definingLoaderOf(threadLoader);
|
||||
if (resolved != null) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
loader = findMinecraftLoader(Thread.currentThread().getContextClassLoader());
|
||||
if (loader != null) {
|
||||
return loader;
|
||||
ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
|
||||
resolved = definingLoaderOf(contextLoader);
|
||||
if (resolved != null) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
return Thread.currentThread().getContextClassLoader();
|
||||
ClassLoader ownLoader = GameClassLoaderResolver.class.getClassLoader();
|
||||
resolved = definingLoaderOf(ownLoader);
|
||||
if (resolved != null) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
try {
|
||||
resolved = definingLoaderOf(ClassLoader.getSystemClassLoader());
|
||||
if (resolved != null) {
|
||||
return resolved;
|
||||
}
|
||||
} catch (SecurityException ignored) {
|
||||
// Security manager may forbid system loader lookup.
|
||||
}
|
||||
|
||||
// No Minecraft class is visible yet; keep the previous best effort.
|
||||
if (threadLoader != null) {
|
||||
return threadLoader;
|
||||
}
|
||||
if (contextLoader != null) {
|
||||
return contextLoader;
|
||||
}
|
||||
return ownLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the loader that actually defines the Minecraft class, falling
|
||||
* back to the candidate itself when the class is defined by the bootstrap
|
||||
* loader.
|
||||
*/
|
||||
private static ClassLoader definingLoaderOf(ClassLoader candidate) {
|
||||
Class<?> minecraftClass = findClass(candidate, MINECRAFT_CLASSES);
|
||||
if (minecraftClass == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ClassLoader definingLoader = minecraftClass.getClassLoader();
|
||||
return definingLoader != null ? definingLoader : candidate;
|
||||
}
|
||||
|
||||
private static ClassLoader findGameThreadLoader() {
|
||||
@@ -46,25 +97,6 @@ public final class GameClassLoaderResolver {
|
||||
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;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.xiaozhou233.orangex.mapping;
|
||||
|
||||
import cn.xiaozhou233.orangex.mapping.impl.IdentityFallbackMappingResolver;
|
||||
import cn.xiaozhou233.orangex.mapping.impl.MCPMappingResolver;
|
||||
import cn.xiaozhou233.orangex.mapping.impl.SrgMappingResolver;
|
||||
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||
@@ -29,6 +30,21 @@ public class MappingManager {
|
||||
break;
|
||||
}
|
||||
|
||||
case V26_2: {
|
||||
if (loaderType == LoaderType.VANILLA
|
||||
|| loaderType == LoaderType.MCP
|
||||
|| loaderType == LoaderType.NEOFORGE) {
|
||||
// 26.x runtimes use Mojang names for classes, fields and
|
||||
// methods, so no obfuscation mapping is needed.
|
||||
resolver = new IdentityFallbackMappingResolver();
|
||||
} else {
|
||||
throw new RuntimeException(
|
||||
"Unsupported loader for 26.2: " + loaderType);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
throw new RuntimeException("Unsupported version " + minecraftVersion);
|
||||
}
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package cn.xiaozhou233.orangex.mapping.impl;
|
||||
|
||||
import cn.xiaozhou233.orangex.mapping.MappingClass;
|
||||
import cn.xiaozhou233.orangex.mapping.MappingField;
|
||||
import cn.xiaozhou233.orangex.mapping.MappingMethod;
|
||||
import cn.xiaozhou233.orangex.mapping.MappingResolver;
|
||||
|
||||
public class IdentityFallbackMappingResolver implements MappingResolver {
|
||||
|
||||
@Override
|
||||
public MappingClass mapClass(String className) {
|
||||
return MappingClass.identity(className);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MappingField mapField(String owner, String fieldName) {
|
||||
return new MappingField(fieldName, fieldName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MappingMethod mapMethod(
|
||||
String owner,
|
||||
String methodName,
|
||||
String descriptor
|
||||
) {
|
||||
return new MappingMethod(
|
||||
methodName,
|
||||
descriptor,
|
||||
methodName,
|
||||
descriptor
|
||||
);
|
||||
}
|
||||
}
|
||||
+58
-26
@@ -4,18 +4,69 @@ import cn.xiaozhou233.orangex.runtime.probe.ClassProbe;
|
||||
|
||||
public final class GameClassLoaderResolver {
|
||||
|
||||
/**
|
||||
* Class names that identify the game class loader across supported
|
||||
* runtimes: Mojang/Forge/MCP, Fabric intermediary, and 1.8 vanilla
|
||||
* obfuscated clients.
|
||||
*/
|
||||
private static final String[] MINECRAFT_CLASSES = {
|
||||
"net.minecraft.client.Minecraft",
|
||||
"net.minecraft.class_310",
|
||||
"ave"
|
||||
};
|
||||
|
||||
public static ClassLoader resolve() {
|
||||
ClassLoader loader = findGameThreadLoader();
|
||||
if (loader != null) {
|
||||
return loader;
|
||||
ClassLoader threadLoader = findGameThreadLoader();
|
||||
|
||||
ClassLoader resolved = definingLoaderOf(threadLoader);
|
||||
if (resolved != null) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
loader = findMinecraftLoader(Thread.currentThread().getContextClassLoader());
|
||||
if (loader != null) {
|
||||
return loader;
|
||||
ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
|
||||
resolved = definingLoaderOf(contextLoader);
|
||||
if (resolved != null) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
return Thread.currentThread().getContextClassLoader();
|
||||
ClassLoader ownLoader = GameClassLoaderResolver.class.getClassLoader();
|
||||
resolved = definingLoaderOf(ownLoader);
|
||||
if (resolved != null) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
try {
|
||||
resolved = definingLoaderOf(ClassLoader.getSystemClassLoader());
|
||||
if (resolved != null) {
|
||||
return resolved;
|
||||
}
|
||||
} catch (SecurityException ignored) {
|
||||
// Security manager may forbid system loader lookup.
|
||||
}
|
||||
|
||||
// No Minecraft class is visible yet; keep the previous best effort.
|
||||
if (threadLoader != null) {
|
||||
return threadLoader;
|
||||
}
|
||||
if (contextLoader != null) {
|
||||
return contextLoader;
|
||||
}
|
||||
return ownLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the loader that actually defines the Minecraft class, falling
|
||||
* back to the candidate itself when the class is defined by the bootstrap
|
||||
* loader.
|
||||
*/
|
||||
private static ClassLoader definingLoaderOf(ClassLoader candidate) {
|
||||
Class<?> minecraftClass = ClassProbe.find(candidate, MINECRAFT_CLASSES);
|
||||
if (minecraftClass == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ClassLoader definingLoader = minecraftClass.getClassLoader();
|
||||
return definingLoader != null ? definingLoader : candidate;
|
||||
}
|
||||
|
||||
private static ClassLoader findGameThreadLoader() {
|
||||
@@ -43,25 +94,6 @@ public final class GameClassLoaderResolver {
|
||||
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() {
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package cn.xiaozhou233.orangex.wrapper.internal;
|
||||
import cn.xiaozhou233.orangex.runtime.version.MinecraftVersion;
|
||||
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
|
||||
import cn.xiaozhou233.orangex.wrapper.internal.v1_8_9.V1_8_9WrapperProvider;
|
||||
import cn.xiaozhou233.orangex.wrapper.internal.v26_2.V26_2WrapperProvider;
|
||||
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
@@ -21,6 +22,7 @@ public final class WrapperRegistry {
|
||||
|
||||
static {
|
||||
register(MinecraftVersion.V1_8_9, new V1_8_9WrapperProvider());
|
||||
register(MinecraftVersion.V26_2, new V26_2WrapperProvider());
|
||||
}
|
||||
|
||||
public static void register(MinecraftVersion version, WrapperProvider provider) {
|
||||
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package cn.xiaozhou233.orangex.wrapper.internal.v26_2;
|
||||
|
||||
import cn.xiaozhou233.orangex.mapping.MappingClass;
|
||||
import cn.xiaozhou233.orangex.mapping.MappingField;
|
||||
import cn.xiaozhou233.orangex.mapping.MappingResolver;
|
||||
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
/**
|
||||
* Minecraft 26.2 implementation of the Minecraft wrapper.
|
||||
*
|
||||
* <p>26.x runtimes already use Mojang names, so the mapping resolver is an
|
||||
* identity mapping. The singleton field is still private and must be read via
|
||||
* {@link Class#getDeclaredField(String)}.</p>
|
||||
*/
|
||||
public final class V26_2Minecraft implements Minecraft {
|
||||
|
||||
private static final String MINECRAFT_CLASS = "net/minecraft/client/Minecraft";
|
||||
private static final String INSTANCE_FIELD = "instance";
|
||||
|
||||
private final RuntimeInfo runtimeInfo;
|
||||
private final MappingClass mappingMinecraft;
|
||||
|
||||
private Object instance;
|
||||
|
||||
public V26_2Minecraft(RuntimeInfo runtimeInfo,
|
||||
MappingResolver mappingResolver) {
|
||||
this.runtimeInfo = runtimeInfo;
|
||||
this.mappingMinecraft = mappingResolver.mapClass(MINECRAFT_CLASS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Object getMinecraft() {
|
||||
if (instance == null) {
|
||||
instance = resolveInstance();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private Object resolveInstance() {
|
||||
MappingField mappingField = mappingMinecraft.getField(INSTANCE_FIELD);
|
||||
if (mappingField == null) {
|
||||
throw new IllegalStateException(
|
||||
"No mapping for field '" + INSTANCE_FIELD + "' on "
|
||||
+ MINECRAFT_CLASS);
|
||||
}
|
||||
|
||||
// Mapping names are internal names (a/b/C); Class.forName needs
|
||||
// binary names (a.b.C).
|
||||
String targetClassName = mappingMinecraft.getTargetName().replace('/', '.');
|
||||
Class<?> minecraftClass;
|
||||
try {
|
||||
minecraftClass = Class.forName(
|
||||
targetClassName, false, runtimeInfo.getMinecraftClassLoader());
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IllegalStateException(
|
||||
"Cannot load Minecraft class " + targetClassName, e);
|
||||
}
|
||||
|
||||
Field field = findStaticField(minecraftClass, mappingField.getTargetName());
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
return field.get(null);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalStateException(
|
||||
"Cannot read Minecraft instance field " + field.getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The singleton field is private in modern clients. Search the declaring
|
||||
* class first and walk superclasses in case it is inherited.
|
||||
*/
|
||||
private static Field findStaticField(Class<?> owner, String name) {
|
||||
for (Class<?> current = owner; current != null; current = current.getSuperclass()) {
|
||||
try {
|
||||
Field field = current.getDeclaredField(name);
|
||||
if (Modifier.isStatic(field.getModifiers())) {
|
||||
return field;
|
||||
}
|
||||
} catch (NoSuchFieldException ignored) {
|
||||
// Continue with the superclass.
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalStateException(
|
||||
"Cannot find static field '" + name + "' on " + owner.getName());
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package cn.xiaozhou233.orangex.wrapper.internal.v26_2;
|
||||
|
||||
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
|
||||
import cn.xiaozhou233.orangex.wrapper.internal.WrapperContext;
|
||||
import cn.xiaozhou233.orangex.wrapper.internal.WrapperProvider;
|
||||
|
||||
public final class V26_2WrapperProvider implements WrapperProvider {
|
||||
|
||||
@Override
|
||||
public Minecraft createMinecraft(WrapperContext context) {
|
||||
return new V26_2Minecraft(
|
||||
context.getRuntimeInfo(),
|
||||
context.getMappingResolver());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user