code cleanup

This commit is contained in:
FalsePattern committed 2022-07-31 20:41:56 +02:00
1 parent eaec528f5d
commit 316908baae
76 files changed
+691 -622

No files matched your search

@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
*
@@ -37,20 +37,21 @@ import org.objectweb.asm.tree.MethodInsnNode;
import java.util.HashMap;
public class MappingManager {
private static boolean initialized = false;
private static final Lookup<UniversalClass> internalLookup = new Lookup<>();
private static final Lookup<UniversalClass> regularLookup = new Lookup<>();
private static boolean initialized = false;
@SneakyThrows
private static synchronized void initialize() {
if (initialized) return;
if (initialized) {
return;
}
initialized = true;
long start = System.nanoTime();
val stringPool = new HashMap<String, String>();
{
val classMappings = ResourceUtil.getResourceStringFromJar("/classes.csv", FalsePatternLib.class).split("\n");
val classMappings =
ResourceUtil.getResourceStringFromJar("/classes.csv", FalsePatternLib.class).split("\n");
for (int i = 1; i < classMappings.length; i++) {
val line = classMappings[i].split(",");
val clazz = new UniversalClass(line, stringPool);
@@ -67,7 +68,8 @@ public class MappingManager {
}
}
{
val methodMappings = ResourceUtil.getResourceStringFromJar("/methods.csv", FalsePatternLib.class).split("\n");
val methodMappings =
ResourceUtil.getResourceStringFromJar("/methods.csv", FalsePatternLib.class).split("\n");
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('/')));
@@ -78,7 +80,8 @@ public class MappingManager {
System.out.println("Parsed in " + (end - start) / 1000000 + "ms");
}
public static UniversalClass classForName(NameType nameType, MappingType mappingType, String className) throws ClassNotFoundException {
public static UniversalClass classForName(NameType nameType, MappingType mappingType, String className)
throws ClassNotFoundException {
initialize();
try {
switch (nameType) {
@@ -90,7 +93,9 @@ public class MappingManager {
throw new IllegalArgumentException("Invalid enum value " + nameType);
}
} catch (Lookup.LookupException e) {
throw new ClassNotFoundException("Could not find class \"" + className + "\" with " + nameType.name().toLowerCase() + " name in the " + mappingType.name() + " mapping.");
throw new ClassNotFoundException(
"Could not find class \"" + className + "\" with " + nameType.name().toLowerCase() +
" name in the " + mappingType.name() + " mapping.");
}
}
@@ -105,22 +110,27 @@ public class MappingManager {
}
}
public static UniversalField getField(FieldInsnNode instruction) throws ClassNotFoundException,
NoSuchFieldException {
public static UniversalField getField(FieldInsnNode instruction)
throws ClassNotFoundException, NoSuchFieldException {
if (!CoreLoadingPlugin.isObfuscated()) {
try {
return classForName(NameType.Internal, MappingType.MCP, instruction.owner).getField(MappingType.MCP, instruction.name);
return classForName(NameType.Internal, MappingType.MCP, instruction.owner).getField(MappingType.MCP,
instruction.name);
} catch (ClassNotFoundException e) {
throw new ClassNotFoundException("Could not find the class " + instruction.owner + " in the MCP mappings. Are you sure it's a Minecraft class? (we're in dev, cannot use SRG or Notch here).");
throw new ClassNotFoundException("Could not find the class " + instruction.owner +
" in the MCP mappings. Are you sure it's a Minecraft class? (we're in dev, cannot use SRG or Notch here).");
}
} else {
try {
return classForName(NameType.Internal, MappingType.SRG, instruction.owner).getField(MappingType.SRG, instruction.name);
return classForName(NameType.Internal, MappingType.SRG, instruction.owner).getField(MappingType.SRG,
instruction.name);
} catch (ClassNotFoundException e) {
try {
return classForName(NameType.Internal, MappingType.Notch, instruction.owner).getField(MappingType.Notch, instruction.name);
return classForName(NameType.Internal, MappingType.Notch, instruction.owner).getField(
MappingType.Notch, instruction.name);
} catch (ClassNotFoundException ex) {
throw new ClassNotFoundException("Could not find the class " + instruction.owner + " neither in the SRG nor in the Notch mappings. Are you sure it's a Minecraft class? (we're in obf, cannot use MCP here)");
throw new ClassNotFoundException("Could not find the class " + instruction.owner +
" neither in the SRG nor in the Notch mappings. Are you sure it's a Minecraft class? (we're in obf, cannot use MCP here)");
}
}
}
@@ -130,18 +140,25 @@ public class MappingManager {
throws ClassNotFoundException, NoSuchMethodException {
if (!CoreLoadingPlugin.isObfuscated()) {
try {
return classForName(NameType.Internal, MappingType.MCP, instruction.owner).getMethod(MappingType.MCP, instruction.name, instruction.desc);
return classForName(NameType.Internal, MappingType.MCP, instruction.owner).getMethod(MappingType.MCP,
instruction.name,
instruction.desc);
} catch (ClassNotFoundException e) {
throw new ClassNotFoundException("Could not find the class " + instruction.owner + " in the MCP mappings. Are you sure it's a Minecraft class? (we're in dev, cannot use SRG or Notch here).");
throw new ClassNotFoundException("Could not find the class " + instruction.owner +
" in the MCP mappings. Are you sure it's a Minecraft class? (we're in dev, cannot use SRG or Notch here).");
}
} else {
try {
return classForName(NameType.Internal, MappingType.SRG, instruction.owner).getMethod(MappingType.SRG, instruction.name, instruction.desc);
return classForName(NameType.Internal, MappingType.SRG, instruction.owner).getMethod(MappingType.SRG,
instruction.name,
instruction.desc);
} catch (ClassNotFoundException e) {
try {
return classForName(NameType.Internal, MappingType.Notch, instruction.owner).getMethod(MappingType.Notch, instruction.name, instruction.desc);
return classForName(NameType.Internal, MappingType.Notch, instruction.owner).getMethod(
MappingType.Notch, instruction.name, instruction.desc);
} catch (ClassNotFoundException ex) {
throw new ClassNotFoundException("Could not find the class " + instruction.owner + " neither in the SRG nor in the Notch mappings. Are you sure it's a Minecraft class? (we're in obf, cannot use MCP here)");
throw new ClassNotFoundException("Could not find the class " + instruction.owner +
" neither in the SRG nor in the Notch mappings. Are you sure it's a Minecraft class? (we're in obf, cannot use MCP here)");
}
}
}
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
*
@@ -40,6 +40,16 @@ public class Lookup<T> {
private final Map<String, T> srg = new HashMap<>();
private final Map<String, T> mcp = new HashMap<>();
private static <T> void removeFirst(Iterator<Map.Entry<String, T>> iterator, T value) {
while (iterator.hasNext()) {
val entry = iterator.next();
if (entry.getValue().equals(value)) {
iterator.remove();
return;
}
}
}
public boolean contains(T value) {
return values.contains(value);
}
@@ -92,16 +102,6 @@ public class Lookup<T> {
return result;
}
private static <T> void removeFirst(Iterator<Map.Entry<String, T>> iterator, T value) {
while (iterator.hasNext()) {
val entry = iterator.next();
if (entry.getValue().equals(value)) {
iterator.remove();
return;
}
}
}
public static class LookupException extends Exception {
public LookupException(String message) {
super(message);
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
*
@@ -40,16 +40,17 @@ public class MappedString {
public final String notch;
public final String srg;
public final String mcp;
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);
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);
}
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));
stringPool.computeIfAbsent(a.srg + delimiter + b.srg, (str) -> str),
stringPool.computeIfAbsent(a.mcp + delimiter + b.mcp, (str) -> str));
}
public String get(MappingType type) {
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
*
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
*
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
*
@@ -116,7 +116,8 @@ public class UniversalClass {
}
}
public UniversalMethod getMethod(MappingType mappingType, String methodName, String methodDescriptor) throws NoSuchMethodException {
public UniversalMethod getMethod(MappingType mappingType, String methodName, String methodDescriptor)
throws NoSuchMethodException {
try {
return methods.get(mappingType, methodName + methodDescriptor);
} catch (Lookup.LookupException e) {
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
*
@@ -20,8 +20,8 @@
*/
package com.falsepattern.lib.mapping.types;
import com.falsepattern.lib.util.ReflectionUtil;
import com.falsepattern.lib.mapping.storage.MappedString;
import com.falsepattern.lib.util.ReflectionUtil;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
*
@@ -20,8 +20,8 @@
*/
package com.falsepattern.lib.mapping.types;
import com.falsepattern.lib.util.ReflectionUtil;
import com.falsepattern.lib.mapping.storage.MappedString;
import com.falsepattern.lib.util.ReflectionUtil;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
@@ -61,30 +61,12 @@ public class UniversalMethod {
parent.addMethod(this);
}
public String getName(MappingType mappingType) {
return name.get(mappingType);
}
public String getDescriptor(MappingType mappingType) {
return descriptor.get(mappingType);
}
public Method asJavaMethod() throws ClassNotFoundException, NoSuchMethodException {
if (javaMethodCache != null) {
return javaMethodCache;
}
val parentClass = parent.asJavaClass();
javaMethodCache = parentClass.getDeclaredMethod(getName(parent.realClassMapping()), decodeMethodDescriptor(getDescriptor(parent.realClassMapping())));
ReflectionUtil.jailBreak(javaMethodCache);
return javaMethodCache;
}
private static Class<?>[] decodeMethodDescriptor(String desc) throws ClassNotFoundException {
val result = new ArrayList<Class<?>>();
val buf = new StringBuilder();
boolean readingReference = false;
int arrayDimensions = 0;
for (val c: desc.toCharArray()) {
for (val c : desc.toCharArray()) {
Class<?> parsedClass = null;
if (readingReference) {
if (c == ';') {
@@ -95,9 +77,12 @@ public class UniversalMethod {
buf.append(c);
continue;
}
} else if (c == '(') continue;
else if (c == ')') break;
else switch (c) {
} else if (c == '(') {
continue;
} else if (c == ')') {
break;
} else {
switch (c) {
case 'B':
parsedClass = byte.class;
break;
@@ -128,6 +113,7 @@ public class UniversalMethod {
case '[':
arrayDimensions++;
continue;
}
}
for (int i = 0; i < arrayDimensions; i++) {
parsedClass = Array.newInstance(parsedClass, 0).getClass();
@@ -138,15 +124,36 @@ public class UniversalMethod {
return result.toArray(new Class[0]);
}
public String getName(MappingType mappingType) {
return name.get(mappingType);
}
public String getDescriptor(MappingType mappingType) {
return descriptor.get(mappingType);
}
public Method asJavaMethod() throws ClassNotFoundException, NoSuchMethodException {
if (javaMethodCache != null) {
return javaMethodCache;
}
val parentClass = parent.asJavaClass();
javaMethodCache = parentClass.getDeclaredMethod(getName(parent.realClassMapping()), decodeMethodDescriptor(
getDescriptor(parent.realClassMapping())));
ReflectionUtil.jailBreak(javaMethodCache);
return javaMethodCache;
}
/**
* This is only here for completeness' sake, given that MethodInsnNode itself also has a deprecated yet functional constructor.
*/
@Deprecated
public MethodInsnNode asInstruction(int opcode, MappingType mapping) {
return new MethodInsnNode(opcode, parent.getName(NameType.Internal, mapping), getName(mapping), getDescriptor(mapping));
return new MethodInsnNode(opcode, parent.getName(NameType.Internal, mapping), getName(mapping),
getDescriptor(mapping));
}
public MethodInsnNode asInstruction(int opcode, MappingType mapping, boolean itf) {
return new MethodInsnNode(opcode, parent.getName(NameType.Internal, mapping), getName(mapping), getDescriptor(mapping), itf);
return new MethodInsnNode(opcode, parent.getName(NameType.Internal, mapping), getName(mapping),
getDescriptor(mapping), itf);
}
}