package gq.malwarefight.nosession.tweaks.cleanup; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import net.minecraft.launchwrapper.ITweaker; import net.minecraft.launchwrapper.Launch; import net.minecraft.launchwrapper.LaunchClassLoader; import net.minecraftforge.fml.common.asm.ASMTransformerWrapper; import java.io.File; import java.lang.reflect.*; import java.net.URL; import java.net.URLStreamHandler; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import gq.malwarefight.nosession.utils.Utils; public class CleanupTweaker implements ITweaker { /** * Cached to avoid expensive reflection operations */ public Method makeWrapper; public CleanupTweaker() throws NoSuchMethodException { makeWrapper = ASMTransformerWrapper.class.getDeclaredMethod("makeWrapper", String.class); makeWrapper.setAccessible(true); } @Override @SuppressWarnings("unchecked") public void acceptOptions(List args, File gameDir, File assetsDir, String profile) { // add the FMLTweaker ArrayList tweakerList = (ArrayList) Launch.blackboard.get("TweakClasses"); tweakerList.add("net.minecraftforge.fml.common.launcher.FMLTweaker"); // reset ASMTransformerWrapper Class cls = ASMTransformerWrapper.class; try { Utils.setStaticValue(cls, "wrapperModMap", new HashMap()); Utils.setStaticValue(cls, "wrapperParentMap", new HashMap()); //noinspection UnstableApiUsage Utils.setStaticValue(cls, "wrapperCache", CacheBuilder.newBuilder().maximumSize(30L).weakValues().build(new CacheLoader() { public byte[] load(String file) throws Exception { return (byte[]) makeWrapper.invoke(null, file); } })); Utils.setStaticValue(cls, "asmGenRoot", new URL("asmgen", null, -1, "/", getAsmGenHandler())); Utils.setStaticValue(cls, "injected", false); } catch (Exception e) { System.err.println("NoSession: Fixing ASMTransformerWrapper failed. Things may break."); e.printStackTrace(); } } @Override public void injectIntoClassLoader(LaunchClassLoader classLoader) {} @Override public String getLaunchTarget() { return "net.minecraft.client.main.Main"; } @Override public String[] getLaunchArguments() { return new String[0]; } @SuppressWarnings("unchecked") private URLStreamHandler getAsmGenHandler() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { Class cls = ASMTransformerWrapper.class; for (Class candidate: cls.getDeclaredClasses()) { if (candidate.getSimpleName().equals("ASMGenHandler")) { Method pgdc = Class.class.getDeclaredMethod("privateGetDeclaredConstructors", boolean.class); pgdc.setAccessible(true); Constructor constructor = ((Constructor[]) pgdc.invoke(candidate, false))[0]; constructor.setAccessible(true); return constructor.newInstance(); } } throw new RuntimeException("NoSession: unable to get the ASMGenHandler"); } }