mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 13:05:52 +08:00
Refactor: Add version provider registry to wrapper
- Add WrapperContext, WrapperProvider and WrapperRegistry for version dispatch - Move the 1.8.9 implementation to internal.v1_8_9 with constructor injection - Support identity mappings for MCP development environments - Document the wrapper structure and how to add a Minecraft version
This commit is contained in:
1 parent
8b9dbce0f7
commit
13c9bb622c
22 files changed
+51295
No files matched your search
@@ -1,7 +1,10 @@
|
|||||||
package cn.xiaozhou233.orangex;
|
package cn.xiaozhou233.orangex;
|
||||||
|
|
||||||
|
import cn.xiaozhou233.orangex.mapping.MappingManager;
|
||||||
import cn.xiaozhou233.orangex.runtime.RuntimeDetector;
|
import cn.xiaozhou233.orangex.runtime.RuntimeDetector;
|
||||||
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
import cn.xiaozhou233.orangex.runtime.RuntimeInfo;
|
||||||
|
import cn.xiaozhou233.orangex.wrapper.MinecraftFactory;
|
||||||
|
import cn.xiaozhou233.orangex.wrapper.api.Minecraft;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
public class OrangeX {
|
public class OrangeX {
|
||||||
@@ -20,6 +23,11 @@ public class OrangeX {
|
|||||||
|
|
||||||
runtimeInfo = RuntimeDetector.detect();
|
runtimeInfo = RuntimeDetector.detect();
|
||||||
RuntimeDetector.debug(runtimeInfo);
|
RuntimeDetector.debug(runtimeInfo);
|
||||||
|
|
||||||
|
MappingManager.init(runtimeInfo);
|
||||||
|
|
||||||
|
Minecraft minecraft = MinecraftFactory.create(runtimeInfo, MappingManager.getResolver());
|
||||||
|
System.out.println(minecraft.getMinecraft());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
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,49 @@
|
|||||||
|
package cn.xiaozhou233.orangex.mapping;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new RuntimeException("Unsupported version " + minecraftVersion);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isInitialized() {
|
||||||
|
return resolver != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,59 @@
|
|||||||
|
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 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());
|
||||||
|
}
|
||||||
|
|
||||||
|
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() {
|
||||||
|
}
|
||||||
|
}
|
||||||
+89
@@ -0,0 +1,89 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
String targetClassName = mappingMinecraft.getTargetName();
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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`。
|
- 如果只需检查 marker 类,使用 `ClassPresenceLoaderProbe`。
|
||||||
- 如果需要结构判断,实现 `LoaderProbe`。
|
- 如果需要结构判断,实现 `LoaderProbe`。
|
||||||
3. 如果新加载器需要专属版本签名,在 `VersionSignatureRegistry` 中使用 `VersionSignature.forLoader(...)`。
|
3. 如果新加载器需要专属版本签名,在 `VersionSignatureRegistry` 中使用 `VersionSignature.forLoader(...)`。
|
||||||
|
|
||||||
|
版本检测完成后,如果需要为该版本提供跨版本 API,见
|
||||||
|
[adding-wrapper-versions.md](adding-wrapper-versions.md)。
|
||||||
@@ -0,0 +1,326 @@
|
|||||||
|
# 添加 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(),
|
||||||
|
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` 转换成当前运行时名称。
|
||||||
|
- 私有字段用 `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