feat: no-mappings-available mode

This commit is contained in:
zaaarf 2023-03-08 15:53:48 +01:00
parent f6539d4a07
commit 036dec2bb2
No known key found for this signature in database
GPG key ID: 82240E075E31FA4C

View file

@ -103,11 +103,26 @@ public class LilleroProcessor extends AbstractProcessor {
} }
} }
/**
* Finds the class name and maps it to the correct format.
* @param name the fully qualified name of the class to convert
* @param mapper the {@link SrgMapper} to use, may be null
* @implNote De facto, there is never any difference between the SRG and MCP name of a class.
* In theory, differences only arise between SRG/MCP names and Notch (fully obfuscated)
* names. However, this method still performs a conversion - just in case there is an
* odd one out.
* @return the fully qualified class name
* @since 0.3.0
*/
private static String findClassName(String name, SrgMapper mapper) {
return mapper == null ? name : mapper.mapClass(name, obfuscatedEnvironment).replace('/', '.');
}
/** /**
* Finds the class name and maps it to the correct format. * Finds the class name and maps it to the correct format.
* @param patchAnn the {@link Patch} annotation containing target class info * @param patchAnn the {@link Patch} annotation containing target class info
* @param methodAnn the {@link FindMethod} annotation to fall back on, may be null * @param methodAnn the {@link FindMethod} annotation to fall back on, may be null
* @param mapper the {@link SrgMapper} to use * @param mapper the {@link SrgMapper} to use, may be null
* @implNote De facto, there is never any difference between the SRG and MCP name of a class. * @implNote De facto, there is never any difference between the SRG and MCP name of a class.
* In theory, differences only arise between SRG/MCP names and Notch (fully obfuscated) * In theory, differences only arise between SRG/MCP names and Notch (fully obfuscated)
* names. However, this method still performs a conversion - just in case there is an * names. However, this method still performs a conversion - just in case there is an
@ -120,13 +135,13 @@ public class LilleroProcessor extends AbstractProcessor {
methodAnn == null || methodAnn.parent() == Object.class methodAnn == null || methodAnn.parent() == Object.class
? getClassFullyQualifiedName(patchAnn.value()) ? getClassFullyQualifiedName(patchAnn.value())
: getClassFullyQualifiedName(methodAnn.parent()); : getClassFullyQualifiedName(methodAnn.parent());
return mapper.mapClass(fullyQualifiedName, obfuscatedEnvironment).replace('/', '.'); return findClassName(fullyQualifiedName, mapper);
} }
/** /**
* Finds the class name and maps it to the correct format. * Finds the class name and maps it to the correct format.
* @param patchAnn the {@link Patch} annotation containing target class info * @param patchAnn the {@link Patch} annotation containing target class info
* @param mapper the {@link SrgMapper} to use * @param mapper the {@link SrgMapper} to use, may be null
* @return the internal class name * @return the internal class name
* @since 0.3.0 * @since 0.3.0
*/ */
@ -134,31 +149,35 @@ public class LilleroProcessor extends AbstractProcessor {
return findClassName(patchAnn, null, mapper); return findClassName(patchAnn, null, mapper);
} }
/**
* Finds the member name and maps it to the correct format.
* @param parentFQN the already mapped FQN of the parent class
* @param memberName the name of the member
* @param mapper the {@link SrgMapper} to use, may be null
* @return the internal class name
* @since 0.3.0
*/
private static String findMemberName(String parentFQN, String memberName, SrgMapper mapper) {
return mapper == null ? memberName : mapper.mapMember(parentFQN, memberName, obfuscatedEnvironment);
}
/** /**
* Finds the method name and maps it to the correct format. * Finds the method name and maps it to the correct format.
* @param parentFQN the already mapped FQN of the parent class * @param parentFQN the already mapped FQN of the parent class
* @param methodAnn the {@link FindMethod} annotation to fall back on, may be null * @param methodAnn the {@link FindMethod} annotation to fall back on, may be null
* @param stub the {@link ExecutableElement} for the stub * @param stub the {@link ExecutableElement} for the stub
* @param mapper the {@link SrgMapper} to use * @param mapper the {@link SrgMapper} to use, may be null
* @return the internal class name * @return the internal class name
* @since 0.3.0 * @since 0.3.0
*/ */
private static String findMethodName(String parentFQN, FindMethod methodAnn, ExecutableElement stub, SrgMapper mapper) { private static String findMethodName(String parentFQN, FindMethod methodAnn, ExecutableElement stub, SrgMapper mapper) {
String methodName = methodAnn == null ? stub.getSimpleName().toString() : methodAnn.name(); String methodName = methodAnn == null ? stub.getSimpleName().toString() : methodAnn.name();
try { try {
methodName = mapper.mapMember( methodName = findMemberName(parentFQN, methodName, mapper);
parentFQN,
methodName,
obfuscatedEnvironment
);
} catch(MappingNotFoundException e) { } catch(MappingNotFoundException e) {
//not found: try again with the name of the annotated method //not found: try again with the name of the annotated method
if(methodAnn == null) { if(methodAnn == null) {
methodName = mapper.mapMember( methodName = findMemberName(parentFQN, stub.getSimpleName().toString(), mapper);
parentFQN,
stub.getSimpleName().toString(),
obfuscatedEnvironment
);
} else throw e; } else throw e;
} }
return methodName; return methodName;
@ -261,11 +280,11 @@ public class LilleroProcessor extends AbstractProcessor {
private VariableElement findField(ExecutableElement stub, SrgMapper mapper) { private VariableElement findField(ExecutableElement stub, SrgMapper mapper) {
Patch patchAnn = stub.getEnclosingElement().getAnnotation(Patch.class); Patch patchAnn = stub.getEnclosingElement().getAnnotation(Patch.class);
FindField fieldAnn = stub.getAnnotation(FindField.class); FindField fieldAnn = stub.getAnnotation(FindField.class);
String parentName = mapper.mapClass(getClassFullyQualifiedName( String parentName = findClassName(getClassFullyQualifiedName(
fieldAnn.parent().equals(Object.class) fieldAnn.parent().equals(Object.class)
? patchAnn.value() ? patchAnn.value()
: fieldAnn.parent() : fieldAnn.parent()
), obfuscatedEnvironment); ), mapper);
String name = fieldAnn.name().equals("") String name = fieldAnn.name().equals("")
? stub.getSimpleName().toString() ? stub.getSimpleName().toString()
: fieldAnn.name(); : fieldAnn.name();
@ -288,16 +307,14 @@ public class LilleroProcessor extends AbstractProcessor {
* @param cl the {@link TypeElement} for the given class * @param cl the {@link TypeElement} for the given class
*/ */
private void generateInjectors(TypeElement cl) { private void generateInjectors(TypeElement cl) {
SrgMapper mapper; SrgMapper mapper = null;
try { //TODO: cant we get it from local? try { //TODO: cant we get it from local?
URL url = new URL("https://data.fantabos.co/output.tsrg"); URL url = new URL("https://data.fantabos.co/output.tsrg");
InputStream is = url.openStream(); InputStream is = url.openStream();
mapper = new SrgMapper(new BufferedReader(new InputStreamReader(is, mapper = new SrgMapper(new BufferedReader(new InputStreamReader(is,
StandardCharsets.UTF_8)).lines()); StandardCharsets.UTF_8)).lines());
is.close(); is.close();
} catch(IOException e) { } catch(IOException ignored) {} //TODO: proper handling
throw new RuntimeException("Could not open the specified TSRG file!", e);
} //todo attempt to proceed without mappings
//find class information //find class information
Patch patchAnn = cl.getAnnotation(Patch.class); Patch patchAnn = cl.getAnnotation(Patch.class);