|
1 | 1 | package dev.skidfuscator.gradle; |
2 | 2 |
|
3 | | -import dev.skidfuscator.obfuscator.Skidfuscator; |
4 | | -import dev.skidfuscator.obfuscator.SkidfuscatorSession; |
5 | 3 | import org.gradle.api.Action; |
6 | 4 | import org.gradle.api.Task; |
| 5 | +import org.gradle.api.file.FileCollection; |
7 | 6 |
|
8 | 7 | import javax.inject.Inject; |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | +import java.lang.reflect.Constructor; |
| 11 | +import java.lang.reflect.InvocationTargetException; |
| 12 | +import java.lang.reflect.Method; |
| 13 | +import java.net.URL; |
| 14 | +import java.net.URLClassLoader; |
| 15 | +import java.util.ArrayList; |
9 | 16 | import java.util.List; |
10 | 17 |
|
11 | 18 | public class SkidfuscatorCompileAction implements Action<Task> { |
12 | 19 |
|
13 | | - private final SkidfuscatorSession session; |
14 | | - private final List<String> excludes; |
| 20 | + private final SkidfuscatorSpec spec; |
| 21 | + private final SkidfuscatorRuntime runtime; |
15 | 22 |
|
16 | 23 | @Inject |
17 | | - public SkidfuscatorCompileAction(SkidfuscatorSession session, List<String> exemptionString) { |
18 | | - this.session = session; |
19 | | - this.excludes = exemptionString; |
| 24 | + public SkidfuscatorCompileAction(SkidfuscatorSpec spec, SkidfuscatorRuntime runtime) { |
| 25 | + this.spec = spec; |
| 26 | + this.runtime = runtime; |
20 | 27 | } |
21 | 28 |
|
22 | 29 | @Override |
23 | 30 | public void execute(Task task) { |
24 | | - Skidfuscator skidfuscator = new Skidfuscator(this.session); |
25 | | - this.excludes.forEach(exclude -> skidfuscator.getExemptAnalysis().add(exclude)); |
26 | | - skidfuscator.run(); |
| 31 | + try { |
| 32 | + this.executeObfuscator(); |
| 33 | + } catch (Throwable throwable) { |
| 34 | + throw new IllegalStateException("Failed to compile with skidfuscator", throwable); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private void executeObfuscator() throws IOException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { |
| 39 | + FileCollection fileCollection = runtime.fetchClasspath(); |
| 40 | + List<URL> urls = new ArrayList<>(); |
| 41 | + for (File file : fileCollection) |
| 42 | + urls.add(file.toURI().toURL()); |
| 43 | + |
| 44 | + try (URLClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[0]))) { |
| 45 | + Class<?> sessionClass = classLoader.loadClass("dev.skidfuscator.obfuscator.SkidfuscatorSession"); |
| 46 | + Class<?> obfuscatorClass = classLoader.loadClass("dev.skidfuscator.obfuscator.Skidfuscator"); |
| 47 | + Object session = this.buildSkidfuscatorSession(sessionClass); |
| 48 | + Object obfuscator = obfuscatorClass.getDeclaredConstructor(sessionClass).newInstance(session); |
| 49 | + |
| 50 | + this.addToExemptAnalysis(obfuscator); |
| 51 | + |
| 52 | + // run obfuscator! |
| 53 | + Method run = obfuscator.getClass().getMethod("run"); |
| 54 | + run.invoke(obfuscator); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private void addToExemptAnalysis(Object obfuscator) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { |
| 59 | + Class<?> obfuscatorClass = obfuscator.getClass(); |
| 60 | + Method getExemptAnalysis = obfuscatorClass.getMethod("getExemptAnalysis"); |
| 61 | + Object exemptAnalysis = getExemptAnalysis.invoke(obfuscator); |
| 62 | + |
| 63 | + Method method = exemptAnalysis.getClass().getMethod("add", String.class); |
| 64 | + for (String exclude : this.spec.getExcludes()) |
| 65 | + method.invoke(exemptAnalysis, exclude); |
| 66 | + } |
| 67 | + |
| 68 | + private Object buildSkidfuscatorSession(Class<?> aClass) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { |
| 69 | + Constructor<?> constructor = aClass.getDeclaredConstructor( |
| 70 | + File.class, // input |
| 71 | + File.class, // output |
| 72 | + File[].class, // libs |
| 73 | + File.class, // mappings |
| 74 | + File.class, // exempt |
| 75 | + File.class, // runtime |
| 76 | + boolean.class, // phantom |
| 77 | + boolean.class, // jmod |
| 78 | + boolean.class, // fuckit |
| 79 | + boolean.class // analytics |
| 80 | + ); |
| 81 | + return constructor.newInstance( |
| 82 | + spec.getInput(), spec.getOutput(), spec.getLibs(), spec.getMappings(), spec.getExempt(), spec.getRuntime(), |
| 83 | + spec.isPhantom(), spec.isJmod(), spec.isFuckit(), spec.isAnalytics()); |
27 | 84 | } |
28 | 85 | } |
0 commit comments