mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 21:15:53 +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;
|
||||
|
||||
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,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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user