Annotate all the things

This commit is contained in:
FalsePattern committed 2022-08-06 13:12:55 +02:00
1 parent 980e8a8776
commit da1ef6c8c7
63 files changed
+623 -369

No files matched your search

@@ -20,6 +20,7 @@
*/
package com.falsepattern.lib.mapping;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.internal.CoreLoadingPlugin;
import com.falsepattern.lib.internal.FalsePatternLib;
import com.falsepattern.lib.mapping.storage.Lookup;
@@ -35,10 +36,13 @@ import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import java.util.HashMap;
import java.util.Map;
@StableAPI(since = "0.10.0")
public class MappingManager {
private static final Lookup<UniversalClass> internalLookup = new Lookup<>();
private static final Lookup<UniversalClass> regularLookup = new Lookup<>();
private static final Map<String, String> stringPool = new HashMap<>();
private static boolean initialized = false;
@SneakyThrows
@@ -47,8 +51,6 @@ public class MappingManager {
return;
}
initialized = true;
long start = System.nanoTime();
val stringPool = new HashMap<String, String>();
{
val classMappings =
ResourceUtil.getResourceStringFromJar("/classes.csv", FalsePatternLib.class).split("\n");
@@ -64,7 +66,7 @@ public class MappingManager {
for (int i = 1; i < fieldMappings.length; i++) {
val line = fieldMappings[i].split(",");
val clazz = internalLookup.get(MappingType.Notch, line[0].substring(0, line[0].lastIndexOf('/')));
new UniversalField(clazz, line, stringPool);
UniversalField.createAndAddToParent(clazz, line, stringPool);
}
}
{
@@ -73,13 +75,12 @@ public class MappingManager {
for (int i = 1; i < methodMappings.length; i++) {
val line = methodMappings[i].split(",");
val clazz = internalLookup.get(MappingType.Notch, line[0].substring(0, line[0].lastIndexOf('/')));
new UniversalMethod(clazz, line, stringPool);
UniversalMethod.createAndAddToParent(clazz, line, stringPool);
}
}
long end = System.nanoTime();
System.out.println("Parsed in " + (end - start) / 1000000 + "ms");
}
@StableAPI.Expose
public static UniversalClass classForName(NameType nameType, MappingType mappingType, String className)
throws ClassNotFoundException {
initialize();
@@ -99,6 +100,7 @@ public class MappingManager {
}
}
@StableAPI.Expose
public static boolean containsClass(NameType nameType, MappingType mappingType, String className) {
switch (nameType) {
case Internal:
@@ -110,6 +112,7 @@ public class MappingManager {
}
}
@StableAPI.Expose
public static UniversalField getField(FieldInsnNode instruction)
throws ClassNotFoundException, NoSuchFieldException {
if (!CoreLoadingPlugin.isObfuscated()) {
@@ -136,6 +139,7 @@ public class MappingManager {
}
}
@StableAPI.Expose
public static UniversalMethod getMethod(MethodInsnNode instruction)
throws ClassNotFoundException, NoSuchMethodException {
if (!CoreLoadingPlugin.isObfuscated()) {
@@ -20,6 +20,7 @@
*/
package com.falsepattern.lib.mapping.storage;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.mapping.types.MappingType;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
@@ -34,6 +35,7 @@ import java.util.Set;
@Accessors(fluent = true)
@EqualsAndHashCode
@StableAPI(since = "0.10.0")
public class Lookup<T> {
private final Set<T> values = new HashSet<>();
private final Map<String, T> notch = new HashMap<>();
@@ -50,10 +52,12 @@ public class Lookup<T> {
}
}
@StableAPI.Expose
public boolean contains(T value) {
return values.contains(value);
}
@StableAPI.Expose
public boolean containsKey(MappingType mappingType, String key) {
switch (mappingType) {
case Notch:
@@ -67,6 +71,7 @@ public class Lookup<T> {
}
}
@StableAPI.Expose
public void unwrap(@NonNull MappedString mappedString, @NonNull T value) {
if (contains(value)) {
//Collision avoidance.
@@ -81,6 +86,7 @@ public class Lookup<T> {
mcp.put(mappedString.mcp, value);
}
@StableAPI.Expose
public T get(MappingType mappingType, String key) throws LookupException {
T result;
switch (mappingType) {
@@ -102,6 +108,7 @@ public class Lookup<T> {
return result;
}
@StableAPI(since = "0.10.0")
public static class LookupException extends Exception {
public LookupException(String message) {
super(message);
@@ -20,6 +20,7 @@
*/
package com.falsepattern.lib.mapping.storage;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.mapping.types.MappingType;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
@@ -36,23 +37,32 @@ import java.util.function.Function;
@EqualsAndHashCode
@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@StableAPI(since = "0.10.0")
public class MappedString {
@StableAPI.Expose
public final String notch;
@StableAPI.Expose
public final String srg;
@StableAPI.Expose
public final String mcp;
@StableAPI.Expose
public MappedString(String[] source, int offset, int stride, Function<String, String> remapper, Map<String, String> stringPool) {
notch = stringPool.computeIfAbsent(remapper.apply(source[offset]), (str) -> str);
srg = stringPool.computeIfAbsent(remapper.apply(source[offset + stride]), (str) -> str);
mcp = stringPool.computeIfAbsent(remapper.apply(source[offset + stride * 2]), (str) -> str);
}
@StableAPI.Expose
public static MappedString fuse(MappedString a, MappedString b, String delimiter, Map<String, String> stringPool) {
return new MappedString(stringPool.computeIfAbsent(a.notch + delimiter + b.notch, (str) -> str),
stringPool.computeIfAbsent(a.srg + delimiter + b.srg, (str) -> str),
stringPool.computeIfAbsent(a.mcp + delimiter + b.mcp, (str) -> str));
}
@StableAPI.Expose
public String get(MappingType type) {
switch (type) {
case Notch:
@@ -20,8 +20,14 @@
*/
package com.falsepattern.lib.mapping.types;
import com.falsepattern.lib.StableAPI;
@StableAPI(since = "0.10.0")
public enum MappingType {
@StableAPI.Expose
Notch,
@StableAPI.Expose
SRG,
@StableAPI.Expose
MCP
}
@@ -20,7 +20,12 @@
*/
package com.falsepattern.lib.mapping.types;
import com.falsepattern.lib.StableAPI;
@StableAPI(since = "0.10.0")
public enum NameType {
@StableAPI.Expose
Internal,
@StableAPI.Expose
Regular
}
@@ -20,6 +20,7 @@
*/
package com.falsepattern.lib.mapping.types;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.internal.CoreLoadingPlugin;
import com.falsepattern.lib.mapping.storage.Lookup;
import com.falsepattern.lib.mapping.storage.MappedString;
@@ -33,12 +34,13 @@ import java.util.Map;
@Accessors(fluent = true)
@ToString(onlyExplicitlyIncluded = true)
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@StableAPI(since = "0.10.0")
public class UniversalClass {
@Getter
@Getter(onMethod_ = @StableAPI.Expose)
@ToString.Include
@EqualsAndHashCode.Include
public final MappedString internalName;
@Getter
@Getter(onMethod_ = @StableAPI.Expose)
@ToString.Include
@EqualsAndHashCode.Include
public final MappedString regularName;
@@ -50,25 +52,27 @@ public class UniversalClass {
@Getter
private MappingType realClassMapping = null;
@StableAPI.Expose
public UniversalClass(String[] names, Map<String, String> stringPool) {
internalName = new MappedString(names, 0, 1, (str) -> str, stringPool);
regularName = new MappedString(names, 0, 1, (str) -> str.replace('/', '.'), stringPool);
}
public void addField(UniversalField field) {
void addField(UniversalField field) {
if (field.parent != this) {
throw new IllegalArgumentException("Field's parent is not this class");
}
fields.unwrap(field.name, field);
}
public void addMethod(UniversalMethod method) {
void addMethod(UniversalMethod method) {
if (method.parent != this) {
throw new IllegalArgumentException("Method's parent is not this class");
}
methods.unwrap(method.fusedNameDescriptor, method);
}
@StableAPI.Expose
public Class<?> asJavaClass() throws ClassNotFoundException {
if (javaClassCache != null) {
return javaClassCache;
@@ -93,6 +97,7 @@ public class UniversalClass {
return javaClassCache;
}
@StableAPI.Expose
public String getName(NameType nameType, MappingType mappingType) {
switch (nameType) {
case Internal:
@@ -104,10 +109,12 @@ public class UniversalClass {
}
}
@StableAPI.Expose
public String getNameAsDescriptor(MappingType mappingType) {
return "L" + getName(NameType.Internal, mappingType) + ";";
}
@StableAPI.Expose
public UniversalField getField(MappingType mappingType, String fieldName) throws NoSuchFieldException {
try {
return fields.get(mappingType, fieldName);
@@ -116,6 +123,7 @@ public class UniversalClass {
}
}
@StableAPI.Expose
public UniversalMethod getMethod(MappingType mappingType, String methodName, String methodDescriptor)
throws NoSuchMethodException {
try {
@@ -20,11 +20,13 @@
*/
package com.falsepattern.lib.mapping.types;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.mapping.storage.MappedString;
import com.falsepattern.lib.util.ReflectionUtil;
import com.falsepattern.lib.internal.ReflectionUtil;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.ToString;
import lombok.experimental.Accessors;
import lombok.val;
@@ -36,27 +38,35 @@ import java.util.Map;
@Accessors(fluent = true)
@ToString
@EqualsAndHashCode
@StableAPI(since = "0.10.0")
public class UniversalField {
@Getter
@Getter(onMethod_ = @StableAPI.Expose)
@ToString.Exclude
@EqualsAndHashCode.Exclude
public final UniversalClass parent;
@Getter
@Getter(onMethod_ = @StableAPI.Expose)
public final MappedString name;
private Field javaFieldCache = null;
public UniversalField(@NonNull UniversalClass parent, String[] names, Map<String, String> stringPool) {
private UniversalField(@NonNull UniversalClass parent, String[] names, Map<String, String> stringPool) {
this.parent = parent;
name = new MappedString(names, 0, 1, (str) -> str.substring(str.lastIndexOf('/') + 1), stringPool);
parent.addField(this);
}
@StableAPI.Expose
public static void createAndAddToParent(@NonNull UniversalClass parent, String[] names, Map<String, String> stringPool) {
new UniversalField(parent, names, stringPool);
}
@StableAPI.Expose
public String getName(MappingType mappingType) {
return name.get(mappingType);
}
@StableAPI.Expose
public Field asJavaField() throws ClassNotFoundException, NoSuchFieldException {
if (javaFieldCache != null) {
return javaFieldCache;
@@ -67,6 +77,18 @@ public class UniversalField {
return javaFieldCache;
}
/**
* A convenience method for {@link Field#get(Object)} with a {@link SneakyThrows} annotation, so that
* you don't need to manually handle exceptions.
*/
@SuppressWarnings("unchecked")
@StableAPI.Expose
public <T> T get(Object instance) throws NoSuchFieldException, ClassNotFoundException, IllegalAccessException {
val field = asJavaField();
return (T) field.get(instance);
}
@StableAPI.Expose
public FieldInsnNode asInstruction(int opcode, MappingType mapping, String descriptor) {
return new FieldInsnNode(opcode, parent.getName(NameType.Internal, mapping), getName(mapping), descriptor);
}
@@ -20,11 +20,13 @@
*/
package com.falsepattern.lib.mapping.types;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.mapping.storage.MappedString;
import com.falsepattern.lib.util.ReflectionUtil;
import com.falsepattern.lib.internal.ReflectionUtil;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.ToString;
import lombok.experimental.Accessors;
import lombok.val;
@@ -38,22 +40,25 @@ import java.util.Map;
@Accessors(fluent = true)
@ToString
@EqualsAndHashCode
@StableAPI(since = "0.10.0")
public class UniversalMethod {
@Getter
@Getter(onMethod_ = @StableAPI.Expose)
@ToString.Exclude
@EqualsAndHashCode.Exclude
public final UniversalClass parent;
@Getter
@Getter(onMethod_ = @StableAPI.Expose)
public final MappedString name;
@Getter
@Getter(onMethod_ = @StableAPI.Expose)
public final MappedString descriptor;
@Getter
@Getter(onMethod_ = @StableAPI.Expose)
public final MappedString fusedNameDescriptor;
private Method javaMethodCache = null;
public UniversalMethod(@NonNull UniversalClass parent, String[] names, Map<String, String> stringPool) {
private UniversalMethod(@NonNull UniversalClass parent, String[] names, Map<String, String> stringPool) {
this.parent = parent;
name = new MappedString(names, 0, 2, (str) -> str.substring(str.lastIndexOf('/') + 1), stringPool);
descriptor = new MappedString(names, 1, 2, (str) -> str, stringPool);
@@ -61,6 +66,11 @@ public class UniversalMethod {
parent.addMethod(this);
}
@StableAPI.Expose
public static void createAndAddToParent(@NonNull UniversalClass parent, String[] names, Map<String, String> stringPool) {
new UniversalMethod(parent, names, stringPool);
}
private static Class<?>[] decodeMethodDescriptor(String desc) throws ClassNotFoundException {
val result = new ArrayList<Class<?>>();
val buf = new StringBuilder();
@@ -124,14 +134,17 @@ public class UniversalMethod {
return result.toArray(new Class[0]);
}
@StableAPI.Expose
public String getName(MappingType mappingType) {
return name.get(mappingType);
}
@StableAPI.Expose
public String getDescriptor(MappingType mappingType) {
return descriptor.get(mappingType);
}
@StableAPI.Expose
public Method asJavaMethod() throws ClassNotFoundException, NoSuchMethodException {
if (javaMethodCache != null) {
return javaMethodCache;
@@ -143,15 +156,34 @@ public class UniversalMethod {
return javaMethodCache;
}
/**
* A convenience method for {@link Method#invoke(Object, Object...)} with a {@link SneakyThrows} annotation, so that
* you don't need to manually handle exceptions.
*/
@SuppressWarnings("unchecked")
@SneakyThrows
@StableAPI.Expose
public <T> T invoke(Object instance, Object... arguments) {
val m = asJavaMethod();
return (T) m.invoke(instance, arguments);
}
@StableAPI.Expose
public <T> T invokeStatic(Object... arguments) {
return invoke(null, arguments);
}
/**
* This is only here for completeness' sake, given that MethodInsnNode itself also has a deprecated yet functional constructor.
*/
@Deprecated
@StableAPI.Expose
public MethodInsnNode asInstruction(int opcode, MappingType mapping) {
return new MethodInsnNode(opcode, parent.getName(NameType.Internal, mapping), getName(mapping),
getDescriptor(mapping));
}
@StableAPI.Expose
public MethodInsnNode asInstruction(int opcode, MappingType mapping, boolean itf) {
return new MethodInsnNode(opcode, parent.getName(NameType.Internal, mapping), getName(mapping),
getDescriptor(mapping), itf);