mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 13:05:52 +08:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
40ca99f853 | ||
|
|
1699df9c5d | ||
|
|
0f2d9614e5 | ||
|
|
13c9bb622c |
No files matched your search
@@ -4,6 +4,8 @@ import cn.xiaozhou233.juiceagent.api.JuiceAgent;
|
||||
import cn.xiaozhou233.orangex.loader.classloader.GameClassLoaderResolver;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class Loader {
|
||||
public static final String VERSION = "1.0.0-SNAPSHOT";
|
||||
@@ -19,21 +21,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");
|
||||
}
|
||||
@@ -53,7 +49,7 @@ public class Loader {
|
||||
orangexFile = new File(OrangeXPath + "/OrangeX.jar");
|
||||
}
|
||||
System.out.println("OrangeX Injection: " + orangexFile.getAbsolutePath());
|
||||
JuiceAgent.addToClassLoader(orangexFile.getAbsolutePath(), minecraftClassLoader);
|
||||
addToGameClassLoader(orangexFile, minecraftClassLoader);
|
||||
|
||||
// Start OrangeX
|
||||
try {
|
||||
@@ -69,4 +65,68 @@ 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fabric's KnotClassLoader keeps its own code-source list and rejects jars
|
||||
* that were added with a plain URLClassLoader-style addURL. Expose the jar
|
||||
* through KnotClassDelegate#addCodeSource, which updates both the code
|
||||
* source list and the internal URL loader.
|
||||
*/
|
||||
private static void addToGameClassLoader(File jar, ClassLoader loader) {
|
||||
if (isKnotClassLoader(loader)) {
|
||||
try {
|
||||
addToKnotClassLoader(loader, jar.toPath());
|
||||
return;
|
||||
} catch (ReflectiveOperationException | RuntimeException e) {
|
||||
System.err.println(
|
||||
"Failed to expose " + jar.getName()
|
||||
+ " to Fabric KnotClassLoader: " + e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
JuiceAgent.addToClassLoader(jar.getAbsolutePath(), loader);
|
||||
}
|
||||
|
||||
private static boolean isKnotClassLoader(ClassLoader loader) {
|
||||
return loader != null
|
||||
&& "net.fabricmc.loader.impl.launch.knot.KnotClassLoader"
|
||||
.equals(loader.getClass().getName());
|
||||
}
|
||||
|
||||
private static void addToKnotClassLoader(ClassLoader loader, Path jar)
|
||||
throws ReflectiveOperationException {
|
||||
Method getDelegate = loader.getClass().getDeclaredMethod("getDelegate");
|
||||
getDelegate.setAccessible(true);
|
||||
Object delegate = getDelegate.invoke(loader);
|
||||
|
||||
Method addCodeSource = delegate.getClass()
|
||||
.getMethod("addCodeSource", Path.class);
|
||||
addCodeSource.setAccessible(true);
|
||||
addCodeSource.invoke(delegate, jar);
|
||||
}
|
||||
}
|
||||
+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,7 +1,10 @@
|
||||
package cn.xiaozhou233.orangex;
|
||||
|
||||
import cn.xiaozhou233.orangex.mapping.MappingManager;
|
||||
import cn.xiaozhou233.orangex.runtime.RuntimeDetector;
|
||||
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||
import cn.xiaozhou233.orangex.wrapper.MinecraftFactory;
|
||||
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
|
||||
import lombok.Getter;
|
||||
|
||||
public class OrangeX {
|
||||
@@ -20,6 +23,11 @@ public class OrangeX {
|
||||
|
||||
runtimeInfo = RuntimeDetector.detect();
|
||||
RuntimeDetector.debug(runtimeInfo);
|
||||
|
||||
MappingManager.init(runtimeInfo);
|
||||
|
||||
Minecraft minecraft = MinecraftFactory.create(runtimeInfo, MappingManager.getResolver());
|
||||
System.out.println(minecraft.getMinecraft());
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package cn.xiaozhou233.orangex.mapping;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MappingClass {
|
||||
|
||||
@Getter
|
||||
private final String sourceName;
|
||||
@Getter
|
||||
private final String targetName;
|
||||
|
||||
private final Map<String, MappingField> fields = new HashMap<>();
|
||||
private final Map<String, MappingMethod> methods = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Identity mapping: named and target names are the same for every member,
|
||||
* including members that were never registered explicitly.
|
||||
*/
|
||||
private final boolean identity;
|
||||
|
||||
public MappingClass(String sourceName, String targetName) {
|
||||
this(sourceName, targetName, false);
|
||||
}
|
||||
|
||||
private MappingClass(String sourceName, String targetName, boolean identity) {
|
||||
this.sourceName = sourceName;
|
||||
this.targetName = targetName;
|
||||
this.identity = identity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a class mapping that resolves any field or method to itself.
|
||||
* Used by environments that already run with named (MCP) members.
|
||||
*/
|
||||
public static MappingClass identity(String className) {
|
||||
return new MappingClass(className, className, true);
|
||||
}
|
||||
|
||||
public void addField(MappingField field) {
|
||||
fields.put(field.getSourceName(), field);
|
||||
}
|
||||
|
||||
public void addMethod(MappingMethod method) {
|
||||
methods.put(
|
||||
method.getSourceName() + '\0' + method.getSourceDescriptor(),
|
||||
method
|
||||
);
|
||||
}
|
||||
|
||||
public MappingField getField(String name) {
|
||||
MappingField field = fields.get(name);
|
||||
if (field != null || !identity) {
|
||||
return field;
|
||||
}
|
||||
return new MappingField(name, name);
|
||||
}
|
||||
|
||||
public MappingMethod getMethod(String name, String descriptor) {
|
||||
MappingMethod method = methods.get(name + '\0' + descriptor);
|
||||
if (method != null || !identity) {
|
||||
return method;
|
||||
}
|
||||
return new MappingMethod(name, descriptor, name, descriptor);
|
||||
}
|
||||
|
||||
public Map<String, MappingField> getFields() {
|
||||
return Collections.unmodifiableMap(fields);
|
||||
}
|
||||
|
||||
public Map<String, MappingMethod> getMethods() {
|
||||
return Collections.unmodifiableMap(methods);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package cn.xiaozhou233.orangex.mapping;
|
||||
|
||||
public class MappingField {
|
||||
|
||||
private final String sourceName;
|
||||
private final String targetName;
|
||||
|
||||
public MappingField(String sourceName, String targetName) {
|
||||
this.sourceName = sourceName;
|
||||
this.targetName = targetName;
|
||||
}
|
||||
|
||||
public String getSourceName() {
|
||||
return sourceName;
|
||||
}
|
||||
|
||||
public String getTargetName() {
|
||||
return targetName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
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;
|
||||
import cn.xiaozhou233.orangex.runtime.loader.LoaderType;
|
||||
import cn.xiaozhou233.orangex.runtime.version.MinecraftVersion;
|
||||
|
||||
public class MappingManager {
|
||||
|
||||
private static MappingResolver resolver;
|
||||
|
||||
public static void init(RuntimeInfo info) {
|
||||
MinecraftVersion minecraftVersion = info.getMinecraftVersion();
|
||||
LoaderType loaderType = info.getLoaderType();
|
||||
|
||||
switch (minecraftVersion) {
|
||||
case V1_8_9: {
|
||||
if(loaderType.equals(LoaderType.MCP)) {
|
||||
resolver = new MCPMappingResolver();
|
||||
} else if (loaderType.equals(LoaderType.VANILLA)) {
|
||||
resolver = new SrgMappingResolver("mappings/vanilla189/joined.srg");
|
||||
} else if (loaderType.equals(LoaderType.FORGE)) {
|
||||
resolver = new SrgMappingResolver("mappings/forge189/joined.srg");
|
||||
} else {
|
||||
throw new RuntimeException("Unsupported loader for 1.8.9: " + loaderType);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case V26_2: {
|
||||
// 26.x normally uses Mojang names. Fabric 26.2 currently ships
|
||||
// empty intermediary mappings, so it also runs with Mojang
|
||||
// names. Verify the runtime instead of trusting the loader type.
|
||||
if (usesMojangNames(info)) {
|
||||
resolver = new IdentityFallbackMappingResolver();
|
||||
} else {
|
||||
throw new RuntimeException(
|
||||
"Unsupported runtime naming for 26.2 ("
|
||||
+ loaderType + "): expected "
|
||||
+ "net.minecraft.client.Minecraft");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
throw new RuntimeException("Unsupported version " + minecraftVersion);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isInitialized() {
|
||||
return resolver != null;
|
||||
}
|
||||
|
||||
private static boolean usesMojangNames(RuntimeInfo info) {
|
||||
ClassLoader loader = info.getMinecraftClassLoader();
|
||||
if (loader == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
Class.forName("net.minecraft.client.Minecraft", false, loader);
|
||||
return true;
|
||||
} catch (Throwable ignored) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static MappingResolver getResolver() {
|
||||
if (!isInitialized()) {
|
||||
System.out.println("Resolver not initialized!");
|
||||
return null;
|
||||
}
|
||||
|
||||
return resolver;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package cn.xiaozhou233.orangex.mapping;
|
||||
|
||||
public class MappingMethod {
|
||||
|
||||
private final String sourceName;
|
||||
private final String sourceDescriptor;
|
||||
|
||||
private final String targetName;
|
||||
private final String targetDescriptor;
|
||||
|
||||
public MappingMethod(
|
||||
String sourceName,
|
||||
String sourceDescriptor,
|
||||
String targetName,
|
||||
String targetDescriptor
|
||||
) {
|
||||
this.sourceName = sourceName;
|
||||
this.sourceDescriptor = sourceDescriptor;
|
||||
this.targetName = targetName;
|
||||
this.targetDescriptor = targetDescriptor;
|
||||
}
|
||||
|
||||
public String getSourceName() {
|
||||
return sourceName;
|
||||
}
|
||||
|
||||
public String getSourceDescriptor() {
|
||||
return sourceDescriptor;
|
||||
}
|
||||
|
||||
public String getTargetName() {
|
||||
return targetName;
|
||||
}
|
||||
|
||||
public String getTargetDescriptor() {
|
||||
return targetDescriptor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.xiaozhou233.orangex.mapping;
|
||||
|
||||
public interface MappingResolver {
|
||||
|
||||
MappingClass mapClass(String className);
|
||||
|
||||
MappingField mapField(String owner, String fieldName);
|
||||
|
||||
MappingMethod mapMethod(
|
||||
String owner,
|
||||
String methodName,
|
||||
String descriptor
|
||||
);
|
||||
|
||||
}
|
||||
+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
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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 MCPMappingResolver 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
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
|
||||
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;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SrgMappingResolver implements MappingResolver {
|
||||
|
||||
private final Map<String, MappingClass> classes = new HashMap<>();
|
||||
|
||||
public SrgMappingResolver(String srgFile) {
|
||||
InputStream inputStream = getClass()
|
||||
.getClassLoader()
|
||||
.getResourceAsStream(srgFile);
|
||||
|
||||
if (inputStream == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"SRG file not found: " + srgFile
|
||||
);
|
||||
}
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
|
||||
|
||||
String line;
|
||||
int lineNumber = 0;
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
lineNumber++;
|
||||
|
||||
line = line.trim();
|
||||
|
||||
if (line.isEmpty() || line.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
parseLine(line);
|
||||
} catch (RuntimeException e) {
|
||||
throw new IllegalArgumentException(
|
||||
"Failed to parse SRG at line "
|
||||
+ lineNumber + ": " + line,
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(
|
||||
"Failed to read SRG file: " + srgFile,
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void parseLine(String line) {
|
||||
String[] parts = line.split("\\s+");
|
||||
|
||||
switch (parts[0]) {
|
||||
case "CL:":
|
||||
if (parts.length != 3) {
|
||||
throw new IllegalArgumentException("Invalid CL entry");
|
||||
}
|
||||
|
||||
// SRG: obfuscated -> named
|
||||
// Internal: named -> obfuscated
|
||||
getOrCreateClass(parts[2], parts[1]);
|
||||
break;
|
||||
|
||||
case "FD:":
|
||||
if (parts.length != 3) {
|
||||
throw new IllegalArgumentException("Invalid FD entry");
|
||||
}
|
||||
|
||||
String[] sourceField = splitMember(parts[1]);
|
||||
String[] targetField = splitMember(parts[2]);
|
||||
|
||||
getOrCreateClass(
|
||||
targetField[0],
|
||||
sourceField[0]
|
||||
).addField(new MappingField(
|
||||
targetField[1],
|
||||
sourceField[1]
|
||||
));
|
||||
|
||||
break;
|
||||
|
||||
case "MD:":
|
||||
if (parts.length != 5) {
|
||||
throw new IllegalArgumentException("Invalid MD entry");
|
||||
}
|
||||
|
||||
String[] sourceMethod = splitMember(parts[1]);
|
||||
String[] targetMethod = splitMember(parts[3]);
|
||||
|
||||
getOrCreateClass(
|
||||
targetMethod[0],
|
||||
sourceMethod[0]
|
||||
).addMethod(new MappingMethod(
|
||||
targetMethod[1],
|
||||
parts[4],
|
||||
sourceMethod[1],
|
||||
parts[2]
|
||||
));
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
// Ignore unsupported entries.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private MappingClass getOrCreateClass(
|
||||
String sourceName,
|
||||
String targetName
|
||||
) {
|
||||
MappingClass mappingClass = classes.get(sourceName);
|
||||
|
||||
if (mappingClass == null) {
|
||||
mappingClass = new MappingClass(sourceName, targetName);
|
||||
classes.put(sourceName, mappingClass);
|
||||
}
|
||||
|
||||
return mappingClass;
|
||||
}
|
||||
|
||||
private String[] splitMember(String member) {
|
||||
int index = member.lastIndexOf('/');
|
||||
|
||||
if (index < 0 || index == member.length() - 1) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid member: " + member
|
||||
);
|
||||
}
|
||||
|
||||
return new String[]{
|
||||
member.substring(0, index),
|
||||
member.substring(index + 1)
|
||||
};
|
||||
}
|
||||
|
||||
public MappingClass getMappingClass(String className) {
|
||||
return mapClass(className);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MappingClass mapClass(String className) {
|
||||
MappingClass mappingClass = classes.get(className);
|
||||
|
||||
if (mappingClass != null) {
|
||||
return mappingClass;
|
||||
}
|
||||
|
||||
// Return identity mapping when class is not found.
|
||||
return new MappingClass(className, className);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MappingField mapField(String owner, String fieldName) {
|
||||
MappingClass mappingClass = classes.get(owner);
|
||||
|
||||
if (mappingClass != null) {
|
||||
MappingField mappingField = mappingClass.getField(fieldName);
|
||||
|
||||
if (mappingField != null) {
|
||||
return mappingField;
|
||||
}
|
||||
}
|
||||
|
||||
// Return identity mapping when field is not found.
|
||||
return new MappingField(fieldName, fieldName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MappingMethod mapMethod(
|
||||
String owner,
|
||||
String methodName,
|
||||
String descriptor
|
||||
) {
|
||||
MappingClass mappingClass = classes.get(owner);
|
||||
|
||||
if (mappingClass != null) {
|
||||
MappingMethod mappingMethod =
|
||||
mappingClass.getMethod(methodName, descriptor);
|
||||
|
||||
if (mappingMethod != null) {
|
||||
return mappingMethod;
|
||||
}
|
||||
}
|
||||
|
||||
// Return identity mapping when method is not found.
|
||||
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() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.xiaozhou233.orangex.wrapper;
|
||||
|
||||
import cn.xiaozhou233.orangex.mapping.MappingResolver;
|
||||
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
|
||||
import cn.xiaozhou233.orangex.wrapper.internal.WrapperContext;
|
||||
import cn.xiaozhou233.orangex.wrapper.internal.WrapperRegistry;
|
||||
|
||||
/**
|
||||
* Public entry point of the cross-version wrapper.
|
||||
*
|
||||
* <p>Feature code should only depend on this class and the {@code api} package.
|
||||
* Version selection is delegated to {@link WrapperRegistry}.</p>
|
||||
*/
|
||||
public final class MinecraftFactory {
|
||||
|
||||
public static Minecraft create(RuntimeInfo runtimeInfo, MappingResolver mappingResolver) {
|
||||
return WrapperRegistry.createMinecraft(
|
||||
new WrapperContext(runtimeInfo, mappingResolver));
|
||||
}
|
||||
|
||||
private MinecraftFactory() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package cn.xiaozhou233.orangex.wrapper.api;
|
||||
|
||||
/**
|
||||
* Cross-version view of {@code net.minecraft.client.Minecraft}.
|
||||
*
|
||||
* <p>Only the native client instance is exposed for now. More operations will be
|
||||
* added incrementally as they are needed.</p>
|
||||
*/
|
||||
public interface Minecraft {
|
||||
|
||||
/**
|
||||
* Returns the native {@code net.minecraft.client.Minecraft} instance.
|
||||
*
|
||||
* <p>Feature code should treat the result as opaque; version-specific
|
||||
* handling belongs to the implementation.</p>
|
||||
*/
|
||||
Object getMinecraft();
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package cn.xiaozhou233.orangex.wrapper.common;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Immutable 3D position / vector used by the wrapper API.
|
||||
*/
|
||||
public final class Vec3 {
|
||||
|
||||
public static final Vec3 ZERO = new Vec3(0.0D, 0.0D, 0.0D);
|
||||
|
||||
private final double x;
|
||||
private final double y;
|
||||
private final double z;
|
||||
|
||||
public Vec3(double x, double y, double z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public double getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public double getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public Vec3 add(Vec3 other) {
|
||||
Objects.requireNonNull(other, "other");
|
||||
return new Vec3(x + other.x, y + other.y, z + other.z);
|
||||
}
|
||||
|
||||
public Vec3 subtract(Vec3 other) {
|
||||
Objects.requireNonNull(other, "other");
|
||||
return new Vec3(x - other.x, y - other.y, z - other.z);
|
||||
}
|
||||
|
||||
public double lengthSquared() {
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
public double length() {
|
||||
return Math.sqrt(lengthSquared());
|
||||
}
|
||||
|
||||
public double distanceSquared(Vec3 other) {
|
||||
Objects.requireNonNull(other, "other");
|
||||
double dx = x - other.x;
|
||||
double dy = y - other.y;
|
||||
double dz = z - other.z;
|
||||
return dx * dx + dy * dy + dz * dz;
|
||||
}
|
||||
|
||||
public double distanceTo(Vec3 other) {
|
||||
return Math.sqrt(distanceSquared(other));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if (this == object) {
|
||||
return true;
|
||||
}
|
||||
if (!(object instanceof Vec3)) {
|
||||
return false;
|
||||
}
|
||||
Vec3 other = (Vec3) object;
|
||||
return Double.compare(x, other.x) == 0
|
||||
&& Double.compare(y, other.y) == 0
|
||||
&& Double.compare(z, other.z) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Vec3{x=" + x + ", y=" + y + ", z=" + z + "}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package cn.xiaozhou233.orangex.wrapper.internal;
|
||||
|
||||
import cn.xiaozhou233.orangex.mapping.MappingResolver;
|
||||
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Shared services handed to every version provider.
|
||||
*
|
||||
* <p>Cross-version helpers belong here instead of being looked up through
|
||||
* singletons inside implementations. When a new service is needed (logger,
|
||||
* event bus, ...), add it here and expose a getter; provider signatures stay
|
||||
* stable.</p>
|
||||
*/
|
||||
public final class WrapperContext {
|
||||
|
||||
private final RuntimeInfo runtimeInfo;
|
||||
private final MappingResolver mappingResolver;
|
||||
|
||||
public WrapperContext(RuntimeInfo runtimeInfo, MappingResolver mappingResolver) {
|
||||
this.runtimeInfo = Objects.requireNonNull(runtimeInfo, "runtimeInfo");
|
||||
this.mappingResolver = Objects.requireNonNull(mappingResolver, "mappingResolver");
|
||||
}
|
||||
|
||||
public RuntimeInfo getRuntimeInfo() {
|
||||
return runtimeInfo;
|
||||
}
|
||||
|
||||
public MappingResolver getMappingResolver() {
|
||||
return mappingResolver;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package cn.xiaozhou233.orangex.wrapper.internal;
|
||||
|
||||
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
|
||||
|
||||
/**
|
||||
* Creates the wrappers for one Minecraft version.
|
||||
*
|
||||
* <p>A provider is stateless: all runtime data comes from the supplied
|
||||
* {@link WrapperContext}, so the same instance can serve the whole process.</p>
|
||||
*
|
||||
* <p>When new wrapper types are added (player, world, ...), add one factory
|
||||
* method per type here and implement it in every provider. Providers that do
|
||||
* not support a type should throw {@link UnsupportedOperationException}.</p>
|
||||
*/
|
||||
public interface WrapperProvider {
|
||||
|
||||
/**
|
||||
* Creates the wrapper for {@code net.minecraft.client.Minecraft}.
|
||||
*/
|
||||
Minecraft createMinecraft(WrapperContext context);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* Maps a Minecraft version to its {@link WrapperProvider}.
|
||||
*
|
||||
* <p><strong>To add a version:</strong> implement {@link WrapperProvider} in a
|
||||
* new {@code internal.vX_Y_Z} package and register it in the static block
|
||||
* below. No switch statements or other call sites need to change.</p>
|
||||
*/
|
||||
public final class WrapperRegistry {
|
||||
|
||||
private static final Map<MinecraftVersion, WrapperProvider> PROVIDERS =
|
||||
new EnumMap<MinecraftVersion, WrapperProvider>(MinecraftVersion.class);
|
||||
|
||||
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) {
|
||||
if (version == null) {
|
||||
throw new IllegalArgumentException("version must not be null");
|
||||
}
|
||||
if (provider == null) {
|
||||
throw new IllegalArgumentException("provider must not be null");
|
||||
}
|
||||
|
||||
WrapperProvider previous = PROVIDERS.put(version, provider);
|
||||
if (previous != null) {
|
||||
throw new IllegalStateException(
|
||||
"Wrapper provider already registered for " + version);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean supports(MinecraftVersion version) {
|
||||
return version != null && PROVIDERS.containsKey(version);
|
||||
}
|
||||
|
||||
public static Minecraft createMinecraft(WrapperContext context) {
|
||||
MinecraftVersion version = context.getRuntimeInfo().getMinecraftVersion();
|
||||
WrapperProvider provider = PROVIDERS.get(version);
|
||||
|
||||
if (provider == null) {
|
||||
throw new IllegalStateException(
|
||||
"Unsupported Minecraft version: " + version);
|
||||
}
|
||||
|
||||
return provider.createMinecraft(context);
|
||||
}
|
||||
|
||||
private WrapperRegistry() {
|
||||
}
|
||||
}
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
package cn.xiaozhou233.orangex.wrapper.internal.v1_8_9;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* 1.8.9 implementation of the Minecraft wrapper.
|
||||
*
|
||||
* <p>All Minecraft class and member names are resolved through
|
||||
* {@link MappingResolver}; the literal names used here are MCP (named) names,
|
||||
* never obfuscated or SRG names.</p>
|
||||
*/
|
||||
public final class V1_8_9Minecraft implements Minecraft {
|
||||
|
||||
private static final String MINECRAFT_CLASS = "net/minecraft/client/Minecraft";
|
||||
private static final String INSTANCE_FIELD = "theMinecraft";
|
||||
|
||||
private final RuntimeInfo runtimeInfo;
|
||||
private final MappingClass mappingMinecraft;
|
||||
|
||||
private Object instance;
|
||||
|
||||
public V1_8_9Minecraft(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 files use internal names (a/b/C); Class.forName needs binary
|
||||
// names (a.b.C). Vanilla target names such as "ave" are unaffected.
|
||||
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 the vanilla client, so {@link
|
||||
* Class#getField(String)} cannot see it. Look it up by declaration instead
|
||||
* and walk superclasses in case the field 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());
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package cn.xiaozhou233.orangex.wrapper.internal.v1_8_9;
|
||||
|
||||
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
|
||||
import cn.xiaozhou233.orangex.wrapper.internal.WrapperContext;
|
||||
import cn.xiaozhou233.orangex.wrapper.internal.WrapperProvider;
|
||||
|
||||
/**
|
||||
* Wrapper provider for Minecraft 1.8.9 (vanilla, MCP and Forge runtimes).
|
||||
*/
|
||||
public final class V1_8_9WrapperProvider implements WrapperProvider {
|
||||
|
||||
@Override
|
||||
public Minecraft createMinecraft(WrapperContext context) {
|
||||
return new V1_8_9Minecraft(
|
||||
context.getRuntimeInfo(),
|
||||
context.getMappingResolver());
|
||||
}
|
||||
}
|
||||
+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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Cross-version wrapper.
|
||||
*
|
||||
* <ul>
|
||||
* <li>{@code api} - stable contracts consumed by feature code. Depends on
|
||||
* the JDK only; never on Minecraft or mapping names.</li>
|
||||
* <li>{@code common} - version-independent value objects shared by the API
|
||||
* and implementations.</li>
|
||||
* <li>{@code internal} - version selection ({@code WrapperRegistry},
|
||||
* {@code WrapperContext}) and one {@code vX_Y_Z} package per supported
|
||||
* Minecraft version.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>Only {@code internal.vX_Y_Z} implementations may reference Minecraft
|
||||
* classes or mapping names. Feature code goes through {@code api} and creates
|
||||
* wrappers via {@link cn.xiaozhou233.orangex.wrapper.MinecraftFactory}.</p>
|
||||
*
|
||||
* <p>To support a new Minecraft version: add a {@code WrapperProvider} under
|
||||
* {@code internal.vX_Y_Z} and register it in
|
||||
* {@link cn.xiaozhou233.orangex.wrapper.internal.WrapperRegistry}.</p>
|
||||
*/
|
||||
package cn.xiaozhou233.orangex.wrapper;
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,20 @@
|
||||
import cn.xiaozhou233.orangex.mapping.MappingClass;
|
||||
import cn.xiaozhou233.orangex.mapping.MappingManager;
|
||||
import cn.xiaozhou233.orangex.mapping.MappingResolver;
|
||||
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||
import cn.xiaozhou233.orangex.runtime.loader.LoaderType;
|
||||
import cn.xiaozhou233.orangex.runtime.version.MinecraftVersion;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
MappingManager.init(new RuntimeInfo(
|
||||
null, LoaderType.FORGE, MinecraftVersion.V1_8_9, MinecraftVersion.V1_8_9.getId()
|
||||
));
|
||||
|
||||
MappingResolver resolver = MappingManager.getResolver();
|
||||
|
||||
System.out.println("Resolver: " + resolver);
|
||||
|
||||
System.out.println(resolver.mapMethod("net/minecraft/client/Minecraft", "getMinecraft", "()Lnet/minecraft/client/Minecraft;").getTargetDescriptor());
|
||||
}
|
||||
}
|
||||
@@ -155,3 +155,6 @@ RuntimeDetector.debug(info);
|
||||
- 如果只需检查 marker 类,使用 `ClassPresenceLoaderProbe`。
|
||||
- 如果需要结构判断,实现 `LoaderProbe`。
|
||||
3. 如果新加载器需要专属版本签名,在 `VersionSignatureRegistry` 中使用 `VersionSignature.forLoader(...)`。
|
||||
|
||||
版本检测完成后,如果需要为该版本提供跨版本 API,见
|
||||
[adding-wrapper-versions.md](adding-wrapper-versions.md)。
|
||||
@@ -0,0 +1,328 @@
|
||||
# 添加 Minecraft 版本的 Wrapper
|
||||
|
||||
本文说明在版本**已经被检测出来**之后,如何为它增加跨版本 Wrapper。
|
||||
|
||||
版本检测本身(`LoaderType`、`MinecraftVersion`、结构签名)见
|
||||
[adding-minecraft-versions.md](adding-minecraft-versions.md)。本文只处理:
|
||||
|
||||
```text
|
||||
RuntimeInfo (V1_X_Y)
|
||||
│
|
||||
▼
|
||||
WrapperRegistry ──► WrapperProvider ──► V1_X_YMinecraft implements Minecraft
|
||||
```
|
||||
|
||||
## 1. 目录结构
|
||||
|
||||
```text
|
||||
wrapper/
|
||||
├── MinecraftFactory.java # 唯一公开入口
|
||||
├── package-info.java
|
||||
├── api/
|
||||
│ └── Minecraft.java # 跨版本契约,只依赖 JDK
|
||||
├── common/
|
||||
│ └── Vec3.java # 与版本无关的值对象
|
||||
└── internal/
|
||||
├── WrapperContext.java # RuntimeInfo + MappingResolver
|
||||
├── WrapperProvider.java # 单版本 Wrapper 工厂
|
||||
├── WrapperRegistry.java # 版本 -> Provider 注册表
|
||||
└── v1_8_9/
|
||||
├── V1_8_9WrapperProvider.java
|
||||
└── V1_8_9Minecraft.java
|
||||
```
|
||||
|
||||
各层职责:
|
||||
|
||||
| 目录 | 职责 | 允许依赖 |
|
||||
|---|---|---|
|
||||
| `api` | 业务代码使用的稳定接口 | 仅 JDK |
|
||||
| `common` | 跨版本共享的值对象 | 仅 JDK |
|
||||
| `internal` | 版本选择、Provider、版本实现 | `api`、`common`、`mapping`、`runtime` |
|
||||
| `internal.vX_Y_Z` | 某一个版本的反射实现 | 可以出现 Minecraft 类名和 MCP 名称 |
|
||||
|
||||
规则:
|
||||
|
||||
- `api` / `common` 中**不要**出现 `net.minecraft.*`、SRG 名、混淆名或加载器类型。
|
||||
- 只有 `internal.vX_Y_Z` 可以看到 Minecraft 类名、字段名和方法名。
|
||||
- Provider 必须是无状态的;所有运行时数据从 `WrapperContext` 获取。
|
||||
- 实现类**不要**直接调用 `OrangeX.getInstance()` 或 `MappingManager.getResolver()`,依赖由构造器注入。
|
||||
|
||||
## 2. 调用链
|
||||
|
||||
```java
|
||||
// OrangeX.start()
|
||||
runtimeInfo = RuntimeDetector.detect();
|
||||
MappingManager.init(runtimeInfo);
|
||||
|
||||
Minecraft minecraft = MinecraftFactory.create(
|
||||
runtimeInfo, MappingManager.getResolver());
|
||||
```
|
||||
|
||||
`MinecraftFactory` 只是组装上下文:
|
||||
|
||||
```java
|
||||
public static Minecraft create(RuntimeInfo runtimeInfo,
|
||||
MappingResolver mappingResolver) {
|
||||
return WrapperRegistry.createMinecraft(
|
||||
new WrapperContext(runtimeInfo, mappingResolver));
|
||||
}
|
||||
```
|
||||
|
||||
`WrapperRegistry` 按 `MinecraftVersion` 查表,查不到就抛出
|
||||
`Unsupported Minecraft version`。
|
||||
|
||||
## 3. 添加一个新版本
|
||||
|
||||
以 1.12.2 为例。
|
||||
|
||||
### 3.1 前置条件
|
||||
|
||||
确认版本检测已经能输出正确的 `MinecraftVersion`:
|
||||
|
||||
```text
|
||||
Minecraft Version: V1_12_2 (version id 23)
|
||||
```
|
||||
|
||||
如果没有,先按 [adding-minecraft-versions.md](adding-minecraft-versions.md)
|
||||
添加内部版本 ID 和结构签名。
|
||||
|
||||
### 3.2 新建版本包
|
||||
|
||||
```text
|
||||
wrapper/internal/v1_12_2/
|
||||
├── V1_12_2WrapperProvider.java
|
||||
└── V1_12_2Minecraft.java
|
||||
```
|
||||
|
||||
包名使用 `v主版本_次版本_修订号`,类名使用 `V1_12_2Xxx`,与现有
|
||||
`v1_8_9` 保持一致。
|
||||
|
||||
### 3.3 实现 Minecraft Wrapper
|
||||
|
||||
```java
|
||||
package cn.xiaozhou233.orangex.wrapper.internal.v1_12_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;
|
||||
|
||||
public final class V1_12_2Minecraft implements Minecraft {
|
||||
|
||||
// 这里写 MCP(named)名称,混淆名由 MappingResolver 负责转换
|
||||
private static final String MINECRAFT_CLASS = "net/minecraft/client/Minecraft";
|
||||
private static final String INSTANCE_FIELD = "theMinecraft";
|
||||
|
||||
private final RuntimeInfo runtimeInfo;
|
||||
private final MappingClass mappingMinecraft;
|
||||
|
||||
private Object instance;
|
||||
|
||||
public V1_12_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);
|
||||
}
|
||||
|
||||
Class<?> minecraftClass;
|
||||
try {
|
||||
minecraftClass = Class.forName(
|
||||
mappingMinecraft.getTargetName().replace('/', '.'),
|
||||
false,
|
||||
runtimeInfo.getMinecraftClassLoader());
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IllegalStateException(
|
||||
"Cannot load Minecraft class "
|
||||
+ mappingMinecraft.getTargetName(), e);
|
||||
}
|
||||
|
||||
// 具体反射细节可参考 V1_8_9Minecraft#resolveInstance
|
||||
// 重点:优先 getDeclaredField + setAccessible,字段通常不是 public
|
||||
// 静态字段时传 null:field.get(null)
|
||||
throw new UnsupportedOperationException("TODO");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
实现约定:
|
||||
|
||||
- 字段名、方法名、参数描述符统一使用 MCP 名称。
|
||||
- 通过 `MappingClass` / `MappingResolver` 转换成当前运行时名称。
|
||||
- 映射文件里的类名是 internal name(`a/b/C`),传给 `Class.forName`
|
||||
前必须转成 binary name(`a.b.C`);`getTargetName().replace('/', '.')`。
|
||||
- 私有字段用 `getDeclaredField` + `setAccessible(true)`,不要用
|
||||
`getField`(它只能找到 public 字段)。
|
||||
- 需要跨线程访问的单例缓存加 `synchronized` 或 `volatile`。
|
||||
|
||||
### 3.4 实现 Provider
|
||||
|
||||
```java
|
||||
package cn.xiaozhou233.orangex.wrapper.internal.v1_12_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 V1_12_2WrapperProvider implements WrapperProvider {
|
||||
|
||||
@Override
|
||||
public Minecraft createMinecraft(WrapperContext context) {
|
||||
return new V1_12_2Minecraft(
|
||||
context.getRuntimeInfo(),
|
||||
context.getMappingResolver());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 3.5 注册 Provider
|
||||
|
||||
在 `wrapper/internal/WrapperRegistry.java` 的静态块中加一行:
|
||||
|
||||
```java
|
||||
static {
|
||||
register(MinecraftVersion.V1_8_9, new V1_8_9WrapperProvider());
|
||||
register(MinecraftVersion.V1_12_2, new V1_12_2WrapperProvider());
|
||||
}
|
||||
```
|
||||
|
||||
不需要改 `MinecraftFactory`、`OrangeX` 或任何业务代码。
|
||||
|
||||
### 3.6 支持该版本的映射
|
||||
|
||||
`MappingManager.init(runtimeInfo)` 目前会按版本和加载器选择
|
||||
`MappingResolver`。新版本通常需要:
|
||||
|
||||
1. 在 `MappingManager` 中增加版本分支:
|
||||
|
||||
```java
|
||||
case V1_12_2: {
|
||||
if (loaderType == LoaderType.MCP) {
|
||||
resolver = new MCPMappingResolver();
|
||||
} else if (loaderType == LoaderType.VANILLA) {
|
||||
resolver = new SrgMappingResolver("mappings/vanilla1122/joined.srg");
|
||||
} else if (loaderType == LoaderType.FORGE) {
|
||||
resolver = new SrgMappingResolver("mappings/forge1122/joined.srg");
|
||||
}
|
||||
break;
|
||||
}
|
||||
```
|
||||
|
||||
2. 把对应的 `joined.srg` 放到
|
||||
`OrangeX/src/main/resources/mappings/<loader>/joined.srg`。
|
||||
|
||||
3. 如果该版本使用 Mojang 映射(例如 NeoForge / 现代版本),不要复用 MCP
|
||||
名称直接当运行时名称,需要单独实现一个 `MappingResolver`,或者让
|
||||
`mapClass/mapField/mapMethod` 返回正确的运行时名称。
|
||||
|
||||
MCP 开发环境使用 `MappingClass.identity(...)`:没有显式注册的成员会按
|
||||
同名返回,因此 `theMinecraft` 这类 named 字段可以直接使用。
|
||||
|
||||
## 4. 添加新的 Wrapper 类型
|
||||
|
||||
当需要暴露 Player、World、Chat 等能力时,不要往 `Minecraft` 接口里堆方法,
|
||||
按能力单独建接口。
|
||||
|
||||
以 `Player` 为例:
|
||||
|
||||
```java
|
||||
// api/Player.java
|
||||
public interface Player {
|
||||
double getHealth();
|
||||
void setHealth(double health);
|
||||
}
|
||||
```
|
||||
|
||||
然后扩展 Provider:
|
||||
|
||||
```java
|
||||
public interface WrapperProvider {
|
||||
|
||||
Minecraft createMinecraft(WrapperContext context);
|
||||
|
||||
Player createPlayer(WrapperContext context);
|
||||
}
|
||||
```
|
||||
|
||||
每个版本的 Provider 实现自己的 `createPlayer`;不支持的版本抛
|
||||
`UnsupportedOperationException`。
|
||||
|
||||
最后在 `WrapperRegistry` 增加对应分发方法,并在 `MinecraftFactory` 暴露:
|
||||
|
||||
```java
|
||||
public static Player createPlayer(RuntimeInfo runtimeInfo,
|
||||
MappingResolver mappingResolver) {
|
||||
return WrapperRegistry.createPlayer(
|
||||
new WrapperContext(runtimeInfo, mappingResolver));
|
||||
}
|
||||
```
|
||||
|
||||
这样:
|
||||
|
||||
- 业务代码只依赖 `api`。
|
||||
- 新版本只需要实现同一个 Provider 接口。
|
||||
- 新能力只需要加一个 API + 一个分发方法。
|
||||
|
||||
## 5. 构建与验证
|
||||
|
||||
构建:
|
||||
|
||||
```bash
|
||||
./gradlew :OrangeX:shadowJar
|
||||
# 或完整打包
|
||||
./gradlew packageAll
|
||||
```
|
||||
|
||||
启动客户端后检查日志:
|
||||
|
||||
```text
|
||||
Loader Type: VANILLA
|
||||
Minecraft Version: V1_8_9 (version id 15)
|
||||
ave@71e820ad
|
||||
```
|
||||
|
||||
最后一行是 `MinecraftFactory.create(...).getMinecraft()` 的输出,能打印出
|
||||
实例就说明:
|
||||
|
||||
1. 版本识别正确;
|
||||
2. `WrapperRegistry` 找到了对应 Provider;
|
||||
3. `MappingResolver` 能解析该版本和加载器;
|
||||
4. 反射字段/方法名正确。
|
||||
|
||||
## 6. 常见问题
|
||||
|
||||
| 现象 | 原因 |
|
||||
|---|---|
|
||||
| `Unsupported Minecraft version: V1_X_Y` | 没有在 `WrapperRegistry` 注册 Provider。 |
|
||||
| `No mapping for field 'theMinecraft'` | 当前 `MappingResolver` 没有该字段的映射;检查 SRG 文件和 `MappingManager` 分支。 |
|
||||
| `NoSuchFieldException: S` | 使用了 `getField` 读取 private 字段;改用 `getDeclaredField` + `setAccessible(true)`。 |
|
||||
| `Cannot load Minecraft class ...` | 运行时类名错误,或 `RuntimeInfo` 的 ClassLoader 不对。 |
|
||||
| `Wrapper provider already registered for ...` | 同一个版本注册了两次,检查静态块。 |
|
||||
| 启动时报 `Unsupported version` | `MappingManager.init` 没有该版本的分支。 |
|
||||
|
||||
## 7. Checklist
|
||||
|
||||
- [ ] `MinecraftVersion` 已有该版本,且版本检测能正确输出。
|
||||
- [ ] `wrapper/internal/vX_Y_Z` 下有 `VX_Y_ZWrappedProvider` 和 `VX_Y_ZMinecraft`。
|
||||
- [ ] `WrapperRegistry` 已注册该 Provider。
|
||||
- [ ] `MappingManager` 已支持该版本 + 加载器组合。
|
||||
- [ ] SRG 资源已放到 `src/main/resources/mappings/...`。
|
||||
- [ ] 实现类中没有静态单例查询,依赖全部来自 `WrapperContext`。
|
||||
- [ ] `api` / `common` 中没有任何 Minecraft 或映射名称。
|
||||
- [ ] `./gradlew :OrangeX:shadowJar` 通过,并在真实客户端中打印出实例。
|
||||
Reference in new issue
Block a user