mirror of
https://github.com/zaaarf/lillero-processor.git
synced 2024-11-14 23:09:20 +01:00
Compare commits
No commits in common. "8d26063266b8cdbacd23ae9abdec4cf3810a2049" and "4f8bde4ce277803ed7bed8034ea3d0c4df237e1b" have entirely different histories.
8d26063266
...
4f8bde4ce2
11 changed files with 414 additions and 164 deletions
|
@ -4,7 +4,7 @@ plugins {
|
|||
}
|
||||
|
||||
archivesBaseName = 'processor'
|
||||
version = versionDetails().lastTag
|
||||
version = gitVersion().split('-').getAt(0).replace('dirty', '')
|
||||
|
||||
java {
|
||||
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
|
||||
|
@ -27,6 +27,5 @@ repositories {
|
|||
dependencies {
|
||||
implementation 'com.squareup:javapoet:1.13.0'
|
||||
implementation 'org.ow2.asm:asm-commons:9.5'
|
||||
implementation 'ftbsc:lll:0.5.0'
|
||||
implementation 'ftbsc.lll:mapper:0.3.0'
|
||||
implementation 'ftbsc:lll:0.4.1'
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package ftbsc.lll.exceptions;
|
||||
|
||||
import ftbsc.lll.processor.tools.obfuscation.ObfuscationMapper;
|
||||
|
||||
/**
|
||||
* Thrown upon failure to find the requested mapping within a loaded {@link ObfuscationMapper}.
|
||||
*/
|
||||
public class MappingNotFoundException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* Constructs a new mapping not found exception for the specified mapping.
|
||||
* @param mapping the relevant mapping
|
||||
*/
|
||||
public MappingNotFoundException(String mapping) {
|
||||
super(String.format("Could not find mapping for %s!", mapping));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new mapping not found exception for the specified mapping
|
||||
* with the specified reason.
|
||||
* @param mapping the relevant mapping
|
||||
* @param reason the reason message
|
||||
*/
|
||||
public MappingNotFoundException(String mapping, String reason) {
|
||||
this(mapping + ": " + reason);
|
||||
}
|
||||
}
|
|
@ -8,9 +8,10 @@ import ftbsc.lll.IInjector;
|
|||
import ftbsc.lll.exceptions.AmbiguousDefinitionException;
|
||||
import ftbsc.lll.exceptions.OrphanElementException;
|
||||
import ftbsc.lll.processor.annotations.*;
|
||||
import ftbsc.lll.processor.containers.ClassContainer;
|
||||
import ftbsc.lll.processor.containers.InjectorInfo;
|
||||
import ftbsc.lll.processor.containers.MethodContainer;
|
||||
import ftbsc.lll.processor.tools.ProcessorOptions;
|
||||
import ftbsc.lll.processor.tools.containers.ClassContainer;
|
||||
import ftbsc.lll.processor.tools.containers.InjectorInfo;
|
||||
import ftbsc.lll.processor.tools.containers.MethodContainer;
|
||||
import ftbsc.lll.proxies.ProxyType;
|
||||
import ftbsc.lll.proxies.impl.TypeProxy;
|
||||
|
||||
|
@ -28,8 +29,8 @@ import java.io.PrintWriter;
|
|||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static ftbsc.lll.processor.utils.ASTUtils.*;
|
||||
import static ftbsc.lll.processor.utils.JavaPoetUtils.*;
|
||||
import static ftbsc.lll.processor.tools.ASTUtils.*;
|
||||
import static ftbsc.lll.processor.tools.JavaPoetUtils.*;
|
||||
|
||||
/**
|
||||
* The actual annotation processor behind the magic.
|
||||
|
@ -47,7 +48,7 @@ public class LilleroProcessor extends AbstractProcessor {
|
|||
/**
|
||||
* An object representing the various options passed to the processor.
|
||||
*/
|
||||
private ProcessorOptions options = null;
|
||||
public final ProcessorOptions options = new ProcessorOptions(processingEnv);
|
||||
|
||||
/**
|
||||
* Method overriding default implementation to manually pass supported options.
|
||||
|
@ -58,16 +59,6 @@ public class LilleroProcessor extends AbstractProcessor {
|
|||
return ProcessorOptions.SUPPORTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link ProcessorOptions} for this instance, creating the object if
|
||||
* it hasn't been already.
|
||||
* @return the {@link ProcessorOptions} for this instance
|
||||
*/
|
||||
public ProcessorOptions getProcessorOptions() {
|
||||
if(this.options == null) this.options = new ProcessorOptions(this.processingEnv);
|
||||
return this.options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Where the actual processing happens.
|
||||
* It filters through whatever annotated class it's fed, and checks whether it contains
|
||||
|
@ -99,7 +90,7 @@ public class LilleroProcessor extends AbstractProcessor {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (!this.getProcessorOptions().noServiceProvider && !this.injectors.isEmpty()) {
|
||||
if (!this.options.noServiceProvider && !this.injectors.isEmpty()) {
|
||||
generateServiceProvider();
|
||||
return true;
|
||||
} else return false;
|
||||
|
@ -143,7 +134,7 @@ public class LilleroProcessor extends AbstractProcessor {
|
|||
//find class information
|
||||
Patch patchAnn = cl.getAnnotation(Patch.class);
|
||||
ClassContainer targetClass = ClassContainer.from(
|
||||
patchAnn, Patch::value, patchAnn.innerName(), this.getProcessorOptions()
|
||||
patchAnn, Patch::value, patchAnn.innerName(), this.options
|
||||
);
|
||||
//find package information
|
||||
Element packageElement = cl.getEnclosingElement();
|
||||
|
@ -165,7 +156,7 @@ public class LilleroProcessor extends AbstractProcessor {
|
|||
//take care of TypeProxies and FieldProxies first
|
||||
for(VariableElement proxyVar : finders) {
|
||||
ProxyType type = getProxyType(proxyVar);
|
||||
if(type == ProxyType.METHOD && proxyVar.getAnnotation(Find.class).name().isEmpty()) {
|
||||
if(type == ProxyType.METHOD && proxyVar.getAnnotation(Find.class).name().equals("")) {
|
||||
//methods without a specified name will be handled later
|
||||
methodFinders.add(proxyVar);
|
||||
continue;
|
||||
|
@ -174,17 +165,17 @@ public class LilleroProcessor extends AbstractProcessor {
|
|||
if(type == ProxyType.TYPE) {
|
||||
//find and validate
|
||||
ClassContainer clazz = ClassContainer.findOrFallback(
|
||||
ClassContainer.from(cl, this.getProcessorOptions()),
|
||||
ClassContainer.from(cl, this.options),
|
||||
patchAnn,
|
||||
proxyVar.getAnnotation(Find.class),
|
||||
this.getProcessorOptions()
|
||||
this.options
|
||||
);
|
||||
//types can be generated with a single instruction
|
||||
constructorBuilder.addStatement(
|
||||
"super.$L = $T.from($S, 0, $L)",
|
||||
proxyVar.getSimpleName().toString(),
|
||||
TypeProxy.class,
|
||||
clazz.data.nameMapped.replace('/', '.'), //use obf name, at runtime it will be obfuscated
|
||||
clazz.fqnObf, //use obf name, at runtime it will be obfuscated
|
||||
clazz.elem == null ? 0 : mapModifiers(clazz.elem.getModifiers())
|
||||
);
|
||||
} else if(type == ProxyType.FIELD)
|
||||
|
@ -193,7 +184,7 @@ public class LilleroProcessor extends AbstractProcessor {
|
|||
null,
|
||||
null,
|
||||
constructorBuilder,
|
||||
this.getProcessorOptions()
|
||||
this.options
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -224,11 +215,11 @@ public class LilleroProcessor extends AbstractProcessor {
|
|||
.collect(Collectors.toList());
|
||||
|
||||
//throw exception if user is a moron and defined a finder and an injector with the same name
|
||||
if(!finderCandidates.isEmpty() && !injectorCandidates.isEmpty())
|
||||
if(finderCandidates.size() != 0 && injectorCandidates.size() != 0)
|
||||
throw new AmbiguousDefinitionException(
|
||||
String.format("Target specified user %s, but name was used by both a finder and injector.", targetAnn.of())
|
||||
);
|
||||
else if(finderCandidates.isEmpty() && injectorCandidates.isEmpty())
|
||||
else if(finderCandidates.size() == 0 && injectorCandidates.size() == 0)
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING,
|
||||
String.format(
|
||||
"Found orphan @Target annotation on method %s.%s pointing at method %s, it will be ignored!",
|
||||
|
@ -237,11 +228,11 @@ public class LilleroProcessor extends AbstractProcessor {
|
|||
targetAnn.of()
|
||||
)
|
||||
);
|
||||
else if(finderCandidates.isEmpty() && injectorCandidates.size() != 1)
|
||||
else if(finderCandidates.size() == 0 && injectorCandidates.size() != 1)
|
||||
throw new AmbiguousDefinitionException(
|
||||
String.format("Found multiple candidate injectors for target %s::%s!", cl.getSimpleName(), tg.getSimpleName())
|
||||
);
|
||||
else if(injectorCandidates.isEmpty() && finderCandidates.size() != 1)
|
||||
else if(injectorCandidates.size() == 0 && finderCandidates.size() != 1)
|
||||
throw new AmbiguousDefinitionException(
|
||||
String.format("Found multiple candidate finders for target %s::%s!", cl.getSimpleName(), tg.getSimpleName())
|
||||
);
|
||||
|
@ -252,7 +243,7 @@ public class LilleroProcessor extends AbstractProcessor {
|
|||
matchedInjectors.add(injector);
|
||||
toGenerate.put(
|
||||
String.format("%sInjector%d", cl.getSimpleName(), iterationNumber),
|
||||
new InjectorInfo(injector, tg, targetAnn, this.getProcessorOptions())
|
||||
new InjectorInfo(injector, tg, targetAnn, this.options)
|
||||
);
|
||||
iterationNumber++; //increment is only used by injectors
|
||||
} else {
|
||||
|
@ -264,7 +255,7 @@ public class LilleroProcessor extends AbstractProcessor {
|
|||
tg,
|
||||
targetAnn,
|
||||
constructorBuilder,
|
||||
this.getProcessorOptions()
|
||||
this.options
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -289,13 +280,9 @@ public class LilleroProcessor extends AbstractProcessor {
|
|||
.addMethod(constructorBuilder.build())
|
||||
.addMethod(buildStringReturnMethod("name", injName))
|
||||
.addMethod(buildStringReturnMethod("reason", toGenerate.get(injName).reason))
|
||||
.addMethod(buildStringReturnMethod("targetClass", this.getProcessorOptions().obfuscateInjectorMetadata
|
||||
? targetClass.data.nameMapped.replace('/', '.')
|
||||
: targetClass.data.name.replace('/', '.')))
|
||||
.addMethod(buildStringReturnMethod("methodName", this.getProcessorOptions().obfuscateInjectorMetadata
|
||||
? target.data.nameMapped : target.data.signature.name))
|
||||
.addMethod(buildStringReturnMethod("methodDesc", this.getProcessorOptions().obfuscateInjectorMetadata
|
||||
? target.descriptorObf : target.data.signature.name))
|
||||
.addMethod(buildStringReturnMethod("targetClass", this.options.obfuscateInjectorMetadata ? targetClass.fqnObf : targetClass.fqn))
|
||||
.addMethod(buildStringReturnMethod("methodName", this.options.obfuscateInjectorMetadata ? target.nameObf : target.name))
|
||||
.addMethod(buildStringReturnMethod("methodDesc", this.options.obfuscateInjectorMetadata ? target.descriptorObf : target.descriptor))
|
||||
.addMethods(generateDummies(cl))
|
||||
.addMethod(generateInjector(toGenerate.get(injName), this.processingEnv))
|
||||
.build();
|
||||
|
|
|
@ -1,15 +1,12 @@
|
|||
package ftbsc.lll.processor.utils;
|
||||
package ftbsc.lll.processor.tools;
|
||||
|
||||
import ftbsc.lll.exceptions.AmbiguousDefinitionException;
|
||||
import ftbsc.lll.exceptions.MappingNotFoundException;
|
||||
import ftbsc.lll.exceptions.NotAProxyException;
|
||||
import ftbsc.lll.exceptions.TargetNotFoundException;
|
||||
import ftbsc.lll.mapper.tools.Mapper;
|
||||
import ftbsc.lll.mapper.tools.data.ClassData;
|
||||
import ftbsc.lll.mapper.tools.data.FieldData;
|
||||
import ftbsc.lll.mapper.tools.data.MethodData;
|
||||
import ftbsc.lll.processor.annotations.Target;
|
||||
import ftbsc.lll.processor.containers.ClassContainer;
|
||||
import ftbsc.lll.processor.tools.containers.ClassContainer;
|
||||
import ftbsc.lll.processor.tools.obfuscation.ObfuscationMapper;
|
||||
import ftbsc.lll.proxies.ProxyType;
|
||||
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
|
@ -213,59 +210,35 @@ public class ASTUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link ClassData} corresponding to the given fully-qualified name,
|
||||
* or creates a false one with the same, non-obfuscated name twice.
|
||||
* @param name the internal name of the class to convert
|
||||
* @param mapper the {@link Mapper} to use, may be null
|
||||
* Finds the class name and maps it to the correct format.
|
||||
* @param name the fully qualified name of the class to convert
|
||||
* @param options the {@link ProcessorOptions} to be used
|
||||
* @return the fully qualified class name
|
||||
* @since 0.6.1
|
||||
* @since 0.3.0
|
||||
*/
|
||||
public static ClassData getClassData(String name, Mapper mapper) {
|
||||
public static String findClassName(String name, ProcessorOptions options) {
|
||||
try {
|
||||
name = name.replace('.', '/'); //just in case
|
||||
if(mapper != null)
|
||||
return mapper.getClassData(name);
|
||||
} catch(MappingNotFoundException ignored) {}
|
||||
return new ClassData(name, name);
|
||||
return options.mapper == null ? name : options.mapper.obfuscateClass(name).replace('/', '.');
|
||||
} catch(MappingNotFoundException e) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link MethodData} corresponding to the method matching the given
|
||||
* name, parent and descriptor, or creates a dummy one with fake data if no
|
||||
* valid mapping is found.
|
||||
* @param parent the internal name of the parent class
|
||||
* @param name the name of the member
|
||||
* @param descriptor the descriptor of the method
|
||||
* @param mapper the {@link Mapper} to use, may be null
|
||||
* @return the fully qualified class name
|
||||
* @since 0.6.1
|
||||
* Finds the member name and maps it to the correct format.
|
||||
* @param parentFQN the unobfuscated FQN of the parent class
|
||||
* @param memberName the name of the member
|
||||
* @param methodDescriptor the descriptor of the method, may be null
|
||||
* @param mapper the {@link ObfuscationMapper} to use, may be null
|
||||
* @return the internal class name
|
||||
* @since 0.3.0
|
||||
*/
|
||||
public static MethodData getMethodData(String parent, String name, String descriptor, Mapper mapper) {
|
||||
public static String findMemberName(String parentFQN, String memberName, String methodDescriptor, ObfuscationMapper mapper) {
|
||||
try {
|
||||
name = name.replace('.', '/'); //just in case
|
||||
if(mapper != null)
|
||||
return mapper.getMethodData(parent, name, descriptor);
|
||||
} catch(MappingNotFoundException ignored) {}
|
||||
return new MethodData(getClassData(name, mapper), name, name, descriptor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link FieldData} corresponding to the field matching the given
|
||||
* name and parent, or creates a dummy one with fake data if no valid
|
||||
* mapping is found.
|
||||
* @param parent the internal name of the parent class
|
||||
* @param name the name of the member
|
||||
* @param mapper the {@link Mapper} to use, may be null
|
||||
* @return the fully qualified class name
|
||||
* @since 0.6.1
|
||||
*/
|
||||
public static FieldData getFieldData(String parent, String name, Mapper mapper) {
|
||||
try {
|
||||
name = name.replace('.', '/'); //just in case
|
||||
if(mapper != null)
|
||||
return mapper.getFieldData(parent, name);
|
||||
} catch(MappingNotFoundException ignored) {}
|
||||
return new FieldData(getClassData(name, mapper), name, name);
|
||||
return mapper == null ? memberName : mapper.obfuscateMember(parentFQN, memberName, methodDescriptor);
|
||||
} catch(MappingNotFoundException e) {
|
||||
return memberName;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -285,7 +258,7 @@ public class ASTUtils {
|
|||
ClassContainer parent, String name, String descr,
|
||||
boolean strict, boolean field, ProcessingEnvironment env) {
|
||||
if(parent.elem == null)
|
||||
throw new TargetNotFoundException("parent class", parent.data.name);
|
||||
throw new TargetNotFoundException("parent class", parent.fqn);
|
||||
//try to find by name
|
||||
List<Element> candidates = parent.elem.getEnclosedElements()
|
||||
.stream()
|
||||
|
@ -293,37 +266,36 @@ public class ASTUtils {
|
|||
.filter(e -> e.getSimpleName().contentEquals(name))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if(candidates.isEmpty())
|
||||
throw new TargetNotFoundException(field ? "field" : "method", name, parent.data.name);
|
||||
if(candidates.size() == 0)
|
||||
throw new TargetNotFoundException(field ? "field" : "method", name, parent.fqn);
|
||||
|
||||
if(candidates.size() == 1 && (!strict || descr == null))
|
||||
return candidates.get(0);
|
||||
|
||||
if(descr == null) {
|
||||
throw new AmbiguousDefinitionException(String.format(
|
||||
"Found %d members named %s in class %s!", candidates.size(), name, parent.data.name));
|
||||
throw new AmbiguousDefinitionException(
|
||||
String.format("Found %d members named %s in class %s!", candidates.size(), name, parent.fqn)
|
||||
);
|
||||
} else {
|
||||
if(field) {
|
||||
//fields can verify the signature for extra safety
|
||||
//but there can only be 1 field with a given name
|
||||
if(!descriptorFromType(candidates.get(0).asType(), env).equals(descr))
|
||||
throw new TargetNotFoundException("field", String.format(
|
||||
"%s with descriptor %s", name, descr), parent.data.name);
|
||||
throw new TargetNotFoundException("field", String.format("%s with descriptor %s", name, descr), parent.fqn);
|
||||
} else {
|
||||
candidates = candidates.stream()
|
||||
.map(e -> (ExecutableElement) e)
|
||||
.filter(strict
|
||||
? c -> descr.equals(descriptorFromExecutableElement(c, env))
|
||||
: c -> descr.split("\\)")[0].equalsIgnoreCase(
|
||||
descriptorFromExecutableElement(c, env).split("\\)")[0])
|
||||
: c -> descr.split("\\)")[0].equalsIgnoreCase(descriptorFromExecutableElement(c, env).split("\\)")[0])
|
||||
).collect(Collectors.toList());
|
||||
}
|
||||
if(candidates.isEmpty())
|
||||
throw new TargetNotFoundException("method", String.format(
|
||||
"%s %s", name, descr), parent.data.name);
|
||||
if(candidates.size() == 0)
|
||||
throw new TargetNotFoundException("method", String.format("%s %s", name, descr), parent.fqn);
|
||||
if(candidates.size() > 1)
|
||||
throw new AmbiguousDefinitionException(String.format(
|
||||
"Found %d methods named %s in class %s!", candidates.size(), name, parent.data.name));
|
||||
throw new AmbiguousDefinitionException(
|
||||
String.format("Found %d methods named %s in class %s!", candidates.size(), name, parent.fqn)
|
||||
);
|
||||
return candidates.get(0);
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
package ftbsc.lll.processor.utils;
|
||||
package ftbsc.lll.processor.tools;
|
||||
|
||||
import com.squareup.javapoet.*;
|
||||
import ftbsc.lll.processor.ProcessorOptions;
|
||||
import ftbsc.lll.processor.annotations.Find;
|
||||
import ftbsc.lll.processor.annotations.Target;
|
||||
import ftbsc.lll.processor.containers.ClassContainer;
|
||||
import ftbsc.lll.processor.containers.FieldContainer;
|
||||
import ftbsc.lll.processor.containers.InjectorInfo;
|
||||
import ftbsc.lll.processor.containers.MethodContainer;
|
||||
import ftbsc.lll.processor.tools.containers.ClassContainer;
|
||||
import ftbsc.lll.processor.tools.containers.FieldContainer;
|
||||
import ftbsc.lll.processor.tools.containers.InjectorInfo;
|
||||
import ftbsc.lll.processor.tools.containers.MethodContainer;
|
||||
import ftbsc.lll.proxies.ProxyType;
|
||||
import ftbsc.lll.proxies.impl.FieldProxy;
|
||||
import ftbsc.lll.proxies.impl.MethodProxy;
|
||||
|
@ -16,8 +15,8 @@ import javax.annotation.processing.ProcessingEnvironment;
|
|||
import javax.lang.model.element.*;
|
||||
import java.util.HashSet;
|
||||
|
||||
import static ftbsc.lll.processor.utils.ASTUtils.getProxyType;
|
||||
import static ftbsc.lll.processor.utils.ASTUtils.mapModifiers;
|
||||
import static ftbsc.lll.processor.tools.ASTUtils.getProxyType;
|
||||
import static ftbsc.lll.processor.tools.ASTUtils.mapModifiers;
|
||||
|
||||
/**
|
||||
* Collection of static utils that rely on JavaPoet to function.
|
||||
|
@ -66,13 +65,13 @@ public class JavaPoetUtils {
|
|||
if(isMethod) {
|
||||
MethodContainer mc = MethodContainer.from(stub, t, f, options);
|
||||
descriptorObf = mc.descriptorObf;
|
||||
nameObf = mc.data.nameMapped;
|
||||
nameObf = mc.nameObf;
|
||||
parent = mc.parent;
|
||||
target = mc.elem;
|
||||
} else {
|
||||
FieldContainer fc = FieldContainer.from(var, options);
|
||||
descriptorObf = fc.descriptorObf;
|
||||
nameObf = fc.data.nameMapped;
|
||||
nameObf = fc.nameObf;
|
||||
parent = fc.parent;
|
||||
target = fc.elem;
|
||||
}
|
||||
|
@ -89,7 +88,7 @@ public class JavaPoetUtils {
|
|||
con.addStatement(
|
||||
"$L.setParent($S, $L)",
|
||||
builderName,
|
||||
parent.data.nameMapped.replace('/', '.'),
|
||||
parent.fqnObf,
|
||||
parent.elem == null ? 0 : mapModifiers(parent.elem.getModifiers())
|
||||
);
|
||||
|
|
@ -1,13 +1,16 @@
|
|||
package ftbsc.lll.processor;
|
||||
package ftbsc.lll.processor.tools;
|
||||
|
||||
import ftbsc.lll.IInjector;
|
||||
import ftbsc.lll.mapper.MapperProvider;
|
||||
import ftbsc.lll.mapper.tools.Mapper;
|
||||
import ftbsc.lll.exceptions.InvalidResourceException;
|
||||
import ftbsc.lll.processor.tools.obfuscation.ObfuscationMapper;
|
||||
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import java.io.*;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
|
@ -30,10 +33,10 @@ public class ProcessorOptions {
|
|||
public final ProcessingEnvironment env;
|
||||
|
||||
/**
|
||||
* The {@link Mapper} used to convert classes and variables
|
||||
* The {@link ObfuscationMapper} used to convert classes and variables
|
||||
* to their obfuscated equivalent. Will be null when no mapper is in use.
|
||||
*/
|
||||
public final Mapper mapper;
|
||||
public final ObfuscationMapper mapper;
|
||||
|
||||
/**
|
||||
* Whether the processor should issue warnings when compiling code anonymous
|
||||
|
@ -59,10 +62,29 @@ public class ProcessorOptions {
|
|||
public ProcessorOptions(ProcessingEnvironment env) {
|
||||
this.env = env;
|
||||
String location = env.getOptions().get("mappingsFile");
|
||||
if(location != null) {
|
||||
List<String> lines = MapperProvider.fetchFromLocalOrRemote(location);
|
||||
this.mapper = MapperProvider.getMapper(lines).getMapper(lines, true);
|
||||
} else this.mapper = null;
|
||||
if(location == null)
|
||||
this.mapper = null;
|
||||
else {
|
||||
InputStream targetStream;
|
||||
try {
|
||||
URI target = new URI(location);
|
||||
targetStream = target.toURL().openStream();
|
||||
} catch(URISyntaxException | IOException e) {
|
||||
//may be a local file path
|
||||
File f = new File(location);
|
||||
if(!f.exists())
|
||||
throw new InvalidResourceException(location);
|
||||
try {
|
||||
targetStream = new FileInputStream(f);
|
||||
} catch(FileNotFoundException ex) {
|
||||
throw new InvalidResourceException(location);
|
||||
}
|
||||
}
|
||||
//assuming its tsrg file
|
||||
//todo: replace crappy homebaked parser with actual library
|
||||
this.mapper = new ObfuscationMapper(new BufferedReader(new InputStreamReader(targetStream,
|
||||
StandardCharsets.UTF_8)).lines());
|
||||
}
|
||||
this.anonymousClassWarning = parseBooleanArg(env.getOptions().get("anonymousClassWarning"), true);
|
||||
this.obfuscateInjectorMetadata = parseBooleanArg(env.getOptions().get("obfuscateInjectorMetadata"), true);
|
||||
this.noServiceProvider = parseBooleanArg(env.getOptions().get("noServiceProvider"), false);
|
|
@ -1,10 +1,9 @@
|
|||
package ftbsc.lll.processor.containers;
|
||||
package ftbsc.lll.processor.tools.containers;
|
||||
|
||||
import ftbsc.lll.exceptions.TargetNotFoundException;
|
||||
import ftbsc.lll.mapper.tools.data.ClassData;
|
||||
import ftbsc.lll.processor.annotations.Find;
|
||||
import ftbsc.lll.processor.annotations.Patch;
|
||||
import ftbsc.lll.processor.ProcessorOptions;
|
||||
import ftbsc.lll.processor.tools.ProcessorOptions;
|
||||
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
|
@ -12,7 +11,7 @@ import javax.tools.Diagnostic;
|
|||
import java.lang.annotation.Annotation;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static ftbsc.lll.processor.utils.ASTUtils.*;
|
||||
import static ftbsc.lll.processor.tools.ASTUtils.*;
|
||||
|
||||
/**
|
||||
* Container for information about a class.
|
||||
|
@ -21,9 +20,15 @@ import static ftbsc.lll.processor.utils.ASTUtils.*;
|
|||
*/
|
||||
public class ClassContainer {
|
||||
/**
|
||||
* The {@link ClassData} for the class represented by this container.
|
||||
* The fully-qualified name of the class.
|
||||
*/
|
||||
public final ClassData data;
|
||||
public final String fqn;
|
||||
|
||||
/**
|
||||
* The obfuscated fully-qualified name of the class.
|
||||
* If the mapper passed is null, then this will be identical to {@link #fqn}
|
||||
*/
|
||||
public final String fqnObf;
|
||||
|
||||
/**
|
||||
* The {@link Element} corresponding to the class.
|
||||
|
@ -79,7 +84,8 @@ public class ClassContainer {
|
|||
throw new TargetNotFoundException("class", inner);
|
||||
}
|
||||
}
|
||||
this.data = getClassData(fqnBuilder.toString(), options.mapper);
|
||||
this.fqn = fqnBuilder.toString();
|
||||
this.fqnObf = findClassName(this.fqn, options);
|
||||
this.elem = elem;
|
||||
}
|
||||
|
||||
|
@ -125,6 +131,6 @@ public class ClassContainer {
|
|||
public static ClassContainer findOrFallback(ClassContainer fallback, Patch p, Find f, ProcessorOptions options) {
|
||||
if(f == null) return ClassContainer.from(p, Patch::value, p.innerName(), options);
|
||||
ClassContainer cl = ClassContainer.from(f, Find::value, f.innerName(), options);
|
||||
return cl.data.name.equals("java/lang/Object") ? fallback : cl;
|
||||
return cl.fqn.equals("java.lang.Object") ? fallback : cl;
|
||||
}
|
||||
}
|
|
@ -1,11 +1,9 @@
|
|||
package ftbsc.lll.processor.containers;
|
||||
package ftbsc.lll.processor.tools.containers;
|
||||
|
||||
import ftbsc.lll.exceptions.AmbiguousDefinitionException;
|
||||
import ftbsc.lll.mapper.tools.MappingUtils;
|
||||
import ftbsc.lll.mapper.tools.data.FieldData;
|
||||
import ftbsc.lll.processor.annotations.Find;
|
||||
import ftbsc.lll.processor.annotations.Patch;
|
||||
import ftbsc.lll.processor.ProcessorOptions;
|
||||
import ftbsc.lll.processor.tools.ProcessorOptions;
|
||||
import org.objectweb.asm.Type;
|
||||
|
||||
import javax.lang.model.element.TypeElement;
|
||||
|
@ -13,7 +11,7 @@ import javax.lang.model.element.VariableElement;
|
|||
import javax.lang.model.type.TypeKind;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
|
||||
import static ftbsc.lll.processor.utils.ASTUtils.*;
|
||||
import static ftbsc.lll.processor.tools.ASTUtils.*;
|
||||
|
||||
/**
|
||||
* Container for information about a field.
|
||||
|
@ -22,15 +20,21 @@ import static ftbsc.lll.processor.utils.ASTUtils.*;
|
|||
*/
|
||||
public class FieldContainer {
|
||||
/**
|
||||
* The {@link FieldData} for the field represented by this container.
|
||||
* The name of the field.
|
||||
*/
|
||||
public final FieldData data;
|
||||
public final String name;
|
||||
|
||||
/**
|
||||
* The descriptor of the field.
|
||||
*/
|
||||
public final String descriptor;
|
||||
|
||||
/**
|
||||
* The obfuscated name of the field.
|
||||
* If the mapper passed is null, then this will be identical to {@link #name}.
|
||||
*/
|
||||
public final String nameObf;
|
||||
|
||||
/**
|
||||
* The obfuscated descriptor of the field.
|
||||
* If the mapper passed is null, then this will be identical to {@link #descriptor}.
|
||||
|
@ -62,15 +66,15 @@ public class FieldContainer {
|
|||
if(descriptor == null)
|
||||
throw new AmbiguousDefinitionException("Cannot use name-based lookups for fields of unverifiable classes!");
|
||||
this.elem = null;
|
||||
this.name = name;
|
||||
this.descriptor = descriptor;
|
||||
} else {
|
||||
this.elem = (VariableElement) findMember(parent, name, descriptor, descriptor != null, true, options.env);
|
||||
this.name = this.elem.getSimpleName().toString();
|
||||
this.descriptor = descriptorFromType(this.elem.asType(), options.env);
|
||||
name = this.elem.getSimpleName().toString();
|
||||
}
|
||||
this.data = getFieldData(parent.data.name, name, options.mapper);
|
||||
this.descriptorObf = options.mapper == null ? this.descriptor
|
||||
: MappingUtils.mapType(Type.getType(this.descriptor), options.mapper, false).getDescriptor();
|
||||
this.descriptorObf = options.mapper == null ? this.descriptor : options.mapper.obfuscateType(Type.getType(this.descriptor)).getDescriptor();
|
||||
this.nameObf = findMemberName(parent.fqn, this.name, null, options.mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -90,7 +94,7 @@ public class FieldContainer {
|
|||
ClassContainer.from((TypeElement) finder.getEnclosingElement(), options), patchAnn, f, options
|
||||
);
|
||||
|
||||
String name = f.name().isEmpty() ? finder.getSimpleName().toString() : f.name();
|
||||
String name = f.name().equals("") ? finder.getSimpleName().toString() : f.name();
|
||||
String descriptor;
|
||||
TypeMirror fieldType = getTypeFromAnnotation(f, Find::type, options.env);
|
||||
if(fieldType.toString().equals("java.lang.Object")) {
|
||||
|
@ -100,7 +104,7 @@ public class FieldContainer {
|
|||
descriptor = //jank af but this is temporary anyway
|
||||
"L" + ClassContainer.from(
|
||||
f, Find::type, f.typeInner(), options
|
||||
).data.nameMapped + ";";
|
||||
).fqnObf.replace('.', '/') + ";";
|
||||
else descriptor = descriptorFromType(fieldType, options.env);
|
||||
}
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
package ftbsc.lll.processor.containers;
|
||||
package ftbsc.lll.processor.tools.containers;
|
||||
|
||||
import ftbsc.lll.processor.annotations.Injector;
|
||||
import ftbsc.lll.processor.annotations.Target;
|
||||
import ftbsc.lll.processor.ProcessorOptions;
|
||||
import ftbsc.lll.processor.tools.ProcessorOptions;
|
||||
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
|
||||
|
@ -43,4 +43,4 @@ public class InjectorInfo {
|
|||
this.reason = injector.getAnnotation(Injector.class).reason();
|
||||
this.target = MethodContainer.from(targetStub, targetAnn, null, options);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,18 +1,16 @@
|
|||
package ftbsc.lll.processor.containers;
|
||||
package ftbsc.lll.processor.tools.containers;
|
||||
|
||||
import ftbsc.lll.exceptions.AmbiguousDefinitionException;
|
||||
import ftbsc.lll.exceptions.TargetNotFoundException;
|
||||
import ftbsc.lll.mapper.tools.MappingUtils;
|
||||
import ftbsc.lll.mapper.tools.data.MethodData;
|
||||
import ftbsc.lll.processor.annotations.Find;
|
||||
import ftbsc.lll.processor.annotations.Patch;
|
||||
import ftbsc.lll.processor.annotations.Target;
|
||||
import ftbsc.lll.processor.ProcessorOptions;
|
||||
import ftbsc.lll.processor.tools.ProcessorOptions;
|
||||
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
|
||||
import static ftbsc.lll.processor.utils.ASTUtils.*;
|
||||
import static ftbsc.lll.processor.tools.ASTUtils.*;
|
||||
|
||||
/**
|
||||
* Container for information about a method.
|
||||
|
@ -21,14 +19,24 @@ import static ftbsc.lll.processor.utils.ASTUtils.*;
|
|||
*/
|
||||
public class MethodContainer {
|
||||
/**
|
||||
* The {@link MethodData} for the method represented by this container.
|
||||
* The name of the method.
|
||||
*/
|
||||
public final MethodData data;
|
||||
public final String name;
|
||||
|
||||
/**
|
||||
* The descriptor of the method.
|
||||
*/
|
||||
public final String descriptor;
|
||||
|
||||
/**
|
||||
* The obfuscated name of the method.
|
||||
* If the mapper passed is null, then this will be identical to {@link #name}.
|
||||
*/
|
||||
public final String nameObf;
|
||||
|
||||
/**
|
||||
* The obfuscated descriptor of the field.
|
||||
* If the mapper passed is null, this will be identical to the one inside
|
||||
* {@link #data}.
|
||||
* If the mapper passed is null, then this will be identical to {@link #descriptor}.
|
||||
*/
|
||||
public final String descriptorObf;
|
||||
|
||||
|
@ -60,17 +68,18 @@ public class MethodContainer {
|
|||
if(descriptor == null)
|
||||
throw new AmbiguousDefinitionException("Cannot use name-based lookups for methods of unverifiable classes!");
|
||||
this.elem = null;
|
||||
this.name = name;
|
||||
this.descriptor = descriptor;
|
||||
} else {
|
||||
ExecutableElement tmp = (ExecutableElement) findMember(
|
||||
parent, name, descriptor, descriptor != null && strict,false, options.env
|
||||
);
|
||||
this.elem = bridge ? findSyntheticBridge((TypeElement) this.parent.elem, tmp, options.env) : tmp;
|
||||
name = this.elem.getSimpleName().toString();
|
||||
descriptor = descriptorFromExecutableElement(this.elem, options.env);
|
||||
this.name = this.elem.getSimpleName().toString();
|
||||
this.descriptor = descriptorFromExecutableElement(this.elem, options.env);
|
||||
}
|
||||
this.data = getMethodData(parent.data.name, name, descriptor, options.mapper);
|
||||
this.descriptorObf = options.mapper == null ? this.data.signature.descriptor
|
||||
: MappingUtils.mapMethodDescriptor(this.data.signature.descriptor, options.mapper, false);
|
||||
this.descriptorObf = options.mapper == null ? this.descriptor : options.mapper.obfuscateMethodDescriptor(this.descriptor);
|
||||
this.nameObf = findMemberName(parent.fqn, this.name, this.descriptor, options.mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -90,7 +99,7 @@ public class MethodContainer {
|
|||
ClassContainer parent = ClassContainer.findOrFallback(
|
||||
ClassContainer.from((TypeElement) stub.getEnclosingElement(), options), patchAnn, f, options
|
||||
);
|
||||
String name = !t.methodName().isEmpty()
|
||||
String name = !t.methodName().equals("")
|
||||
? t.methodName() //name was specified in target
|
||||
: stub.getSimpleName().toString();
|
||||
String descriptor = t.strict()
|
|
@ -0,0 +1,225 @@
|
|||
package ftbsc.lll.processor.tools.obfuscation;
|
||||
|
||||
import ftbsc.lll.exceptions.AmbiguousDefinitionException;
|
||||
import ftbsc.lll.exceptions.MappingNotFoundException;
|
||||
import ftbsc.lll.tools.DescriptorBuilder;
|
||||
import org.objectweb.asm.Type;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Parses a .tsrg file into a mapper capable of converting from
|
||||
* deobfuscated names to obfuscated ones.
|
||||
* Obviously, it may only be used at runtime if the .tsrg file is
|
||||
* included in the resources. However, in that case, I'd recommend
|
||||
* using the built-in Forge one and refrain from including an extra
|
||||
* resource for no good reason.
|
||||
* TODO: CSV format
|
||||
* @since 0.2.0
|
||||
*/
|
||||
public class ObfuscationMapper {
|
||||
|
||||
/**
|
||||
* A Map using the deobfuscated names as keys,
|
||||
* holding information for that class as value.
|
||||
*/
|
||||
private final Map<String, ObfuscationData> mapper = new HashMap<>();
|
||||
|
||||
/**
|
||||
* The public constructor.
|
||||
* Should be passed a {@link Stream} of Strings, one representing each line.
|
||||
* Whether they contain line endings or not is irrelevant.
|
||||
* @param str a {@link Stream} of strings
|
||||
*/
|
||||
public ObfuscationMapper(Stream<String> str) {
|
||||
AtomicReference<String> currentClass = new AtomicReference<>("");
|
||||
str.forEach(l -> {
|
||||
if(l == null) return;
|
||||
if(l.startsWith("\t"))
|
||||
mapper.get(currentClass.get()).addMember(l);
|
||||
else {
|
||||
String[] sp = l.split(" ");
|
||||
ObfuscationData s = new ObfuscationData(sp[0], sp[1]);
|
||||
currentClass.set(s.unobf);
|
||||
mapper.put(s.unobf, s);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the obfuscated name of the class.
|
||||
* @param name the unobfuscated internal name of the desired class
|
||||
* @return the obfuscated name of the class
|
||||
* @throws MappingNotFoundException if no mapping is found
|
||||
*/
|
||||
public String obfuscateClass(String name) {
|
||||
ObfuscationData data = mapper.get(name.replace('.', '/'));
|
||||
if(data == null)
|
||||
throw new MappingNotFoundException(name);
|
||||
else return data.obf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the obfuscated name of a class member (field or method).
|
||||
* The method signature must be in this format: "methodName methodDescriptor",
|
||||
* with a space, because that's how it is in .tsrg files.
|
||||
* @param parentName the unobfuscated internal name of the parent class
|
||||
* @param memberName the field name or method signature
|
||||
* @param methodDescriptor the optional descriptor of the member, may be null or partial
|
||||
* @return the obfuscated name of the given member
|
||||
* @throws MappingNotFoundException if no mapping is found
|
||||
*/
|
||||
public String obfuscateMember(String parentName, String memberName, String methodDescriptor) {
|
||||
ObfuscationData data = mapper.get(parentName.replace('.', '/'));
|
||||
if(data == null)
|
||||
throw new MappingNotFoundException(parentName + "::" + memberName);
|
||||
return data.get(memberName, methodDescriptor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obfuscates a method descriptor, replacing its class references
|
||||
* with their obfuscated counterparts.
|
||||
* @param descriptor a {@link String} containing the descriptor
|
||||
* @return the obfuscated descriptor
|
||||
* @since 0.5.1
|
||||
*/
|
||||
public String obfuscateMethodDescriptor(String descriptor) {
|
||||
Type method = Type.getMethodType(descriptor);
|
||||
Type[] arguments = method.getArgumentTypes();
|
||||
Type returnType = method.getReturnType();
|
||||
|
||||
Type[] obfArguments = new Type[arguments.length];
|
||||
for(int i = 0; i < obfArguments.length; i++)
|
||||
obfArguments[i] = this.obfuscateType(arguments[i]);
|
||||
|
||||
return Type.getMethodDescriptor(this.obfuscateType(returnType), obfArguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a {@link Type} it returns its obfuscated counterpart.
|
||||
* @param type the type in question
|
||||
* @return the obfuscated type
|
||||
* @since 0.5.1
|
||||
*/
|
||||
public Type obfuscateType(Type type) {
|
||||
//unwrap arrays
|
||||
Type unwrapped = type;
|
||||
int arrayLevel = 0;
|
||||
while(unwrapped.getSort() == org.objectweb.asm.Type.ARRAY) {
|
||||
unwrapped = unwrapped.getElementType();
|
||||
arrayLevel++;
|
||||
}
|
||||
|
||||
//if it's a primitive no operation is needed
|
||||
if(type.getSort() < org.objectweb.asm.Type.ARRAY)
|
||||
return type;
|
||||
|
||||
String internalName = type.getInternalName();
|
||||
|
||||
String internalNameObf;
|
||||
try {
|
||||
internalNameObf = this.obfuscateClass(internalName);
|
||||
return Type.getType(DescriptorBuilder.nameToDescriptor(internalNameObf, arrayLevel));
|
||||
} catch(MappingNotFoundException e) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Private class used internally for storing information about each
|
||||
* class. It's private because there is no good reason anyone would
|
||||
* want to access this outside of this class.
|
||||
*/
|
||||
private static class ObfuscationData {
|
||||
/**
|
||||
* The unobfuscated name (FQN with '/' instad of '.') of the class.
|
||||
*/
|
||||
private final String unobf;
|
||||
|
||||
/**
|
||||
* The obfuscated internal name (FQN with '/' instad of '.') of the class.
|
||||
*/
|
||||
private final String obf;
|
||||
|
||||
/**
|
||||
* A {@link Map} tying each member's name or signature to its
|
||||
* obfuscated counterpart.
|
||||
*/
|
||||
private final Map<String, String> members;
|
||||
|
||||
/**
|
||||
* The constructor. It takes in the names (obfuscated and non-obfuscated)
|
||||
* of a class.
|
||||
* @param unobf the unobfuscated name
|
||||
* @param obf the obfuscated name
|
||||
*/
|
||||
private ObfuscationData(String unobf, String obf) {
|
||||
this.unobf = unobf;
|
||||
this.obf = obf;
|
||||
this.members = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a member to the target class.
|
||||
* For fields only the names are required; for methods,
|
||||
* this takes in the full signature ({@code name + " " + space}).
|
||||
* @param s the String representing the declaration line
|
||||
*/
|
||||
public void addMember(String s) {
|
||||
String[] split = s.trim().split(" ");
|
||||
if(split.length == 2) //field
|
||||
members.put(split[0], split[1]);
|
||||
else if (split.length == 3) //method
|
||||
members.put(split[0] + " " + split[1], split[2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an obfuscated member given the method name and a method descriptor,
|
||||
* which may be partial (i.e. not include return type) or null if the member
|
||||
* is not a method.
|
||||
* @param memberName member name
|
||||
* @param methodDescriptor the method descriptor, or null if it's not a method
|
||||
* @return the requested obfuscated name, or null if nothing was found
|
||||
* @throws AmbiguousDefinitionException if not enough data was given to uniquely identify a mapping
|
||||
*/
|
||||
public String get(String memberName, String methodDescriptor) {
|
||||
|
||||
//find all keys that start with the name
|
||||
List<String> candidates = members.keySet().stream().filter(
|
||||
m -> m.split(" ")[0].equals(memberName)
|
||||
).collect(Collectors.toList());
|
||||
|
||||
if(methodDescriptor != null) {
|
||||
String signature = String.format("%s %s", memberName, methodDescriptor);
|
||||
candidates = candidates.stream().filter(
|
||||
m -> m.equals(signature)
|
||||
).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
switch(candidates.size()) {
|
||||
case 0:
|
||||
throw new MappingNotFoundException(String.format(
|
||||
"%s.%s%s",
|
||||
this.unobf,
|
||||
memberName,
|
||||
methodDescriptor == null ? "" : "()"
|
||||
));
|
||||
case 1:
|
||||
return members.get(candidates.get(0));
|
||||
default:
|
||||
throw new AmbiguousDefinitionException(String.format(
|
||||
"Mapper could not uniquely identify member %s.%s%s, found %d!",
|
||||
this.unobf,
|
||||
memberName,
|
||||
methodDescriptor == null ? "" : "()",
|
||||
candidates.size()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue