Skip to content

Commit 71c661c

Browse files
committed
Fernflower bug (#26)
1 parent f35fcf0 commit 71c661c

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/main/java/the/bytecode/club/jda/JDA.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.objectweb.asm.ClassReader;
55
import org.objectweb.asm.ClassWriter;
66
import org.objectweb.asm.tree.ClassNode;
7+
import org.objectweb.asm.tree.InnerClassNode;
78
import the.bytecode.club.jda.api.ExceptionUI;
89
import the.bytecode.club.jda.api.Plugin;
910
import the.bytecode.club.jda.api.PluginLoader;
@@ -168,9 +169,20 @@ public static ClassNode getClassNode(String containerName, String name) {
168169
return container.getClassNode(classFileName);
169170
}
170171
}
172+
171173
return null;
172174
}
173175

176+
public static final String HACK_PREFIX = "\0JDA-hack";
177+
178+
public static File getClassFileProxy(ClassNode cn) {
179+
return new File('/' + HACK_PREFIX, cn.name + ".class");
180+
}
181+
182+
public static File getClassFileProxy(InnerClassNode cn) {
183+
return new File('/' + HACK_PREFIX, cn.name + ".class");
184+
}
185+
174186
public static byte[] getFileBytes(String containerName, String name) {
175187
for (FileContainer container : files) {
176188
if (container.name.equals(containerName) && container.getData().containsKey(name)) {
@@ -191,8 +203,12 @@ public static String getClassfileName(ClassNode cn) {
191203
return cn.name + ".class";
192204
}
193205

206+
public static String extractProxyClassName(String fileName) {
207+
return extractClassName(fileName.substring(fileName.indexOf(HACK_PREFIX) + HACK_PREFIX.length() + 1));
208+
}
209+
194210
public static String extractClassName(String fileName) {
195-
return fileName.substring(0, fileName.length() - 6);
211+
return fileName.replace(File.separator, "/").substring(0, fileName.length() - 6);
196212
}
197213

198214
protected static byte[] fixBytes(byte[] in) {

src/main/java/the/bytecode/club/jda/api/PluginLoader.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package the.bytecode.club.jda.api;
22

3+
import the.bytecode.club.jda.JDA;
4+
35
import java.io.*;
46
import java.net.MalformedURLException;
57
import java.net.URL;
@@ -18,7 +20,7 @@ public URL getResource(String name) {
1820
}
1921
};
2022

21-
InputStream metaInfStream = loader.getResourceAsStream("\0JDA-hack:META-INF/MANIFEST.MF");
23+
InputStream metaInfStream = loader.getResourceAsStream(JDA.HACK_PREFIX + ":META-INF/MANIFEST.MF");
2224
if (metaInfStream == null) {
2325
System.out.println("Invalid plugin " + pluginFileName + ": no manifest");
2426
return null;

src/main/java/the/bytecode/club/jda/decompilers/FernflowerDecompiler.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
*/
2828

2929
public final class FernflowerDecompiler extends Decompiler {
30-
3130
public FernflowerDecompiler() {
3231
for (Settings setting : Settings.values()) {
3332
settings.registerSetting(setting);
@@ -48,7 +47,11 @@ public String decompileClassNode(String containerName, final ClassNode cn) {
4847
result.set(null);
4948

5049
BaseDecompiler baseDecompiler = new BaseDecompiler((externalPath, internalPath) -> {
51-
ClassNode requestedCn = JDA.getClassNode(containerName, JDA.extractClassName(new File(externalPath).getName()));
50+
ClassNode requestedCn = JDA.getClassNode(containerName, JDA.extractProxyClassName(externalPath));
51+
if (requestedCn == null) {
52+
System.err.println("Couldn't load " + externalPath);
53+
return new byte[0];
54+
}
5255
byte[] bytes = JDA.getClassBytes(containerName, requestedCn);
5356
byte[] clone = new byte[bytes.length];
5457
System.arraycopy(bytes, 0, clone, 0, bytes.length);
@@ -94,9 +97,9 @@ public void closeArchive(String s, String s1) {
9497
}
9598
}, options, new PrintStreamLogger(System.out));
9699

97-
baseDecompiler.addSpace(new File(cn.name + ".class"), true);
100+
baseDecompiler.addSpace(JDA.getClassFileProxy(cn), true);
98101
for (InnerClassNode innerCn : cn.innerClasses)
99-
baseDecompiler.addSpace(new File(innerCn.name + ".class"), true);
102+
baseDecompiler.addSpace(JDA.getClassFileProxy(innerCn), true);
100103
baseDecompiler.decompileContext();
101104
while (true) {
102105
if (result.get() != null) {

0 commit comments

Comments
 (0)