Add debug info to fail load, use same defineMethod instance
This commit is contained in:
		| @@ -1,6 +1,7 @@ | ||||
| package me.libraryaddict.disguise.utilities.reflection.asm; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import lombok.Getter; | ||||
| import org.objectweb.asm.*; | ||||
|  | ||||
| import java.io.IOException; | ||||
| @@ -14,9 +15,16 @@ import java.util.Map; | ||||
|  * Created by libraryaddict on 17/02/2020. | ||||
|  */ | ||||
| public class Asm13 implements IAsm { | ||||
|     @Getter | ||||
|     private Method defineMethod; | ||||
|  | ||||
|     public Asm13() throws NoSuchMethodException { | ||||
|         defineMethod = getDefineClassMethod(); | ||||
|     } | ||||
|  | ||||
|     public Class<?> createClassWithoutMethods(String className, | ||||
|             ArrayList<Map.Entry<String, String>> illegalMethods) throws IOException, InvocationTargetException, | ||||
|             IllegalAccessException, NoSuchMethodException, NoSuchFieldException { | ||||
|             IllegalAccessException, NoSuchFieldException { | ||||
|         ClassReader cr = new ClassReader( | ||||
|                 getClass().getClassLoader().getResourceAsStream(className.replace(".", "/") + ".class")); | ||||
|         ClassWriter writer = new ClassWriter(cr, 0); | ||||
| @@ -42,14 +50,13 @@ public class Asm13 implements IAsm { | ||||
|         Field field = loader.getClass().getDeclaredField("classes"); | ||||
|         field.setAccessible(true); | ||||
|         Map<String, Class<?>> map = (Map<String, Class<?>>) field.get(loader); | ||||
|         Class newClass = (Class<?>) getDefineClassMethod() | ||||
|                 .invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length); | ||||
|         Class newClass = (Class<?>) defineMethod.invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length); | ||||
|  | ||||
|         map.put(className, newClass); | ||||
|         return newClass; | ||||
|     } | ||||
|  | ||||
|     private static Method getDefineClassMethod() throws NoSuchMethodException { | ||||
|     private Method getDefineClassMethod() throws NoSuchMethodException { | ||||
|         Method defineClass = ClassLoader.class | ||||
|                 .getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class); | ||||
|         defineClass.setAccessible(true); | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| package me.libraryaddict.disguise.utilities.reflection.asm; | ||||
|  | ||||
| import lombok.Getter; | ||||
| import org.bukkit.craftbukkit.libs.org.objectweb.asm.*; | ||||
|  | ||||
| import java.io.IOException; | ||||
| @@ -13,9 +14,16 @@ import java.util.Map; | ||||
|  * Created by libraryaddict on 17/02/2020. | ||||
|  */ | ||||
| public class Asm14 implements IAsm { | ||||
|     @Getter | ||||
|     private Method defineMethod; | ||||
|  | ||||
|     public Asm14() throws NoSuchMethodException { | ||||
|         defineMethod = getDefineClassMethod(); | ||||
|     } | ||||
|  | ||||
|     public Class<?> createClassWithoutMethods(String className, | ||||
|             ArrayList<Map.Entry<String, String>> illegalMethods) throws IOException, InvocationTargetException, | ||||
|             IllegalAccessException, NoSuchMethodException, NoSuchFieldException { | ||||
|             IllegalAccessException, NoSuchFieldException { | ||||
|         ClassReader cr = new ClassReader( | ||||
|                 getClass().getClassLoader().getResourceAsStream(className.replace(".", "/") + ".class")); | ||||
|         ClassWriter writer = new ClassWriter(cr, 0); | ||||
| @@ -41,16 +49,13 @@ public class Asm14 implements IAsm { | ||||
|         Field field = loader.getClass().getDeclaredField("classes"); | ||||
|         field.setAccessible(true); | ||||
|         Map<String, Class<?>> map = (Map<String, Class<?>>) field.get(loader); | ||||
|         Class newClass = | ||||
|  | ||||
|                 (Class<?>) getDefineClassMethod() | ||||
|                         .invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length); | ||||
|         Class newClass = (Class<?>) defineMethod.invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length); | ||||
|  | ||||
|         map.put(className, newClass); | ||||
|         return newClass; | ||||
|     } | ||||
|  | ||||
|     private static Method getDefineClassMethod() throws NoSuchMethodException { | ||||
|     private Method getDefineClassMethod() throws NoSuchMethodException { | ||||
|         Method defineClass = ClassLoader.class | ||||
|                 .getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class); | ||||
|         defineClass.setAccessible(true); | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| package me.libraryaddict.disguise.utilities.reflection.asm; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import me.libraryaddict.disguise.LibsDisguises; | ||||
| import me.libraryaddict.disguise.utilities.DisguiseUtilities; | ||||
| import me.libraryaddict.disguise.utilities.reflection.NmsVersion; | ||||
| @@ -78,19 +79,21 @@ public class WatcherSanitizer { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|  | ||||
|         IAsm asm; | ||||
|  | ||||
|         if (NmsVersion.v1_14.isSupported()) { | ||||
|             asm = new Asm14(); | ||||
|         } else { | ||||
|             if (!NmsVersion.v1_13.isSupported()) { | ||||
|                 new AsmDownloader(); | ||||
|             } | ||||
|  | ||||
|             asm = new Asm13(); | ||||
|         } | ||||
|         ArrayList<String> mapped = new ArrayList<>(); | ||||
|  | ||||
|         try (InputStream stream = LibsDisguises.getInstance().getResource("ANTI_PIRACY_ENCRYPTION")) { | ||||
|             IAsm asm; | ||||
|  | ||||
|             if (NmsVersion.v1_14.isSupported()) { | ||||
|                 asm = new Asm14(); | ||||
|             } else { | ||||
|                 if (!NmsVersion.v1_13.isSupported()) { | ||||
|                     new AsmDownloader(); | ||||
|                 } | ||||
|  | ||||
|                 asm = new Asm13(); | ||||
|             } | ||||
|  | ||||
|             List<String> lines = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines() | ||||
|                     .collect(Collectors.toList()); | ||||
|  | ||||
| @@ -115,10 +118,12 @@ public class WatcherSanitizer { | ||||
|  | ||||
|             for (Map.Entry<String, ArrayList<Map.Entry<String, String>>> entry : toRemove.entrySet()) { | ||||
|                 Class result = asm.createClassWithoutMethods(entry.getKey(), entry.getValue()); | ||||
|                 mapped.add(entry.getKey()); | ||||
|             } | ||||
|         } | ||||
|         catch (IOException | NoClassDefFoundError | IllegalAccessException | InvocationTargetException | NoSuchMethodException | NoSuchFieldException e) { | ||||
|         catch (IOException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | NoSuchFieldException | LinkageError e) { | ||||
|             e.printStackTrace(); | ||||
|             LibsDisguises.getInstance().getLogger().severe("Registered: " + new Gson().toJson(mapped)); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user